diff --git a/ion/include/ion.h b/ion/include/ion.h index 33039e69f..77de5fb56 100644 --- a/ion/include/ion.h +++ b/ion/include/ion.h @@ -1,6 +1,7 @@ #ifndef ION_ION_H #define ION_ION_H +#include #include #include #include diff --git a/ion/include/ion/backlight.h b/ion/include/ion/backlight.h new file mode 100644 index 000000000..bf464fb43 --- /dev/null +++ b/ion/include/ion/backlight.h @@ -0,0 +1,15 @@ +#ifndef ION_BACKLIGHT_H +#define ION_BACKLIGHT_H + +#include + +namespace Ion { +namespace Backlight { + +void setBrightness(uint8_t b); +uint8_t brightness(); + +} +} + +#endif diff --git a/ion/include/ion/display.h b/ion/include/ion/display.h index 44a45d511..cd6412586 100644 --- a/ion/include/ion/display.h +++ b/ion/include/ion/display.h @@ -21,8 +21,6 @@ void pushRect(KDRect r, const KDColor * pixels); void pushRectUniform(KDRect r, KDColor c); void pullRect(KDRect r, KDColor * pixels); -void setBacklightIntensity(uint8_t intensity); - constexpr int Width = 320; constexpr int Height = 240; diff --git a/ion/src/device/Makefile b/ion/src/device/Makefile index eb48ec8c4..49aae4355 100644 --- a/ion/src/device/Makefile +++ b/ion/src/device/Makefile @@ -1,6 +1,7 @@ include ion/src/shared/Makefile include ion/src/device/boot/Makefile objs += $(addprefix ion/src/device/, \ + backlight.o \ battery.o\ device.o\ display.o\ diff --git a/ion/src/device/backlight.cpp b/ion/src/device/backlight.cpp new file mode 100644 index 000000000..93e4e0498 --- /dev/null +++ b/ion/src/device/backlight.cpp @@ -0,0 +1,80 @@ +#include +#include "regs/regs.h" +#include "backlight.h" + +/* This driver controls the RT9365 LED driver. + * This chip allows the brightness to be set to 16 different values. It starts + * at full brightness on power on. Applying a pulse on the EN pin will select + * the next value in decreasing order. Once it reaches the minimal value, the + * next pulse will loop back to full brightness. */ + +// Public Ion::Backlight methods + +namespace Ion { +namespace Backlight { + +void setBrightness(uint8_t b) { + Device::setLevel(b >> 4); +} + +uint8_t brightness() { + return Device::level() << 4; +} + +} +} + +// Private Ion::Backlight::Device methods + +namespace Ion { +namespace Backlight { +namespace Device { + +static uint8_t sLevel; + +void init() { + GPIOC.MODER()->setMode(6, GPIO::MODER::Mode::Output); + sLevel = 0xF; + resume(); +} + +void suspend() { + GPIOC.ODR()->set(6, false); + msleep(3); // Might not need to be blocking +} + +void resume() { + GPIOC.ODR()->set(6, true); + usleep(50); + uint8_t level = sLevel; + sLevel = 0xF; + setLevel(level); +} + +void setLevel(uint8_t level) { + // From sLevel = 12 to level 7 : 5 pulses + // From sLevel = 5 to level 9 : 12 pulses (5 to go to level 16, and 7 to 9) + if (sLevel < level) { + sendPulses(16 + sLevel - level); + } else { + sendPulses(sLevel - level); + } + sLevel = level; +} + +uint8_t level() { + return sLevel; +} + +void sendPulses(int n) { + for (int i=0; iset(6, false); + usleep(20); + GPIOC.ODR()->set(6, true); + usleep(20); + } +} + +} +} +} diff --git a/ion/src/device/backlight.h b/ion/src/device/backlight.h new file mode 100644 index 000000000..9122e42ad --- /dev/null +++ b/ion/src/device/backlight.h @@ -0,0 +1,27 @@ +#ifndef ION_DEVICE_BACKLIGHT_H +#define ION_DEVICE_BACKLIGHT_H + +#include + +namespace Ion { +namespace Backlight { +namespace Device { + +/* Pin | Role | Mode | Function + * -----+-------------------+-----------------------+---------- + * PC6 | Backlight Enable | Output | + */ + +void init(); +void suspend(); +void resume(); +void setLevel(uint8_t level); +uint8_t level(); + +void sendPulses(int n); + +} +} +} + +#endif diff --git a/ion/src/device/device.cpp b/ion/src/device/device.cpp index cfe69f9ab..74fac25cb 100644 --- a/ion/src/device/device.cpp +++ b/ion/src/device/device.cpp @@ -9,6 +9,7 @@ extern "C" { #include "keyboard.h" #include "battery.h" #include "sd_card.h" +#include "backlight.h" #define USE_SD_CARD 0 @@ -37,8 +38,7 @@ void Ion::Device::init() { Display::Device::init(); Keyboard::Device::init(); Battery::Device::init(); - - Display::setBacklightIntensity(0xFF); + Backlight::Device::init(); #if USE_SD_CARD SDCard::Device::init(); #endif diff --git a/ion/src/device/led.cpp b/ion/src/device/led.cpp index f79637ac5..d2169d5d4 100644 --- a/ion/src/device/led.cpp +++ b/ion/src/device/led.cpp @@ -11,9 +11,6 @@ void Ion::LED::setColor(KDColor c) { TIM3.CCR4()->set(Device::dutyCycleForUInt8(c.green())); } -void Ion::Display::setBacklightIntensity(uint8_t intensity) { -} - // Private Ion::Device::LED methods namespace Ion {