diff --git a/sim/components/brightness/BrightnessController.cpp b/sim/components/brightness/BrightnessController.cpp index 5935b8c..3e9a985 100644 --- a/sim/components/brightness/BrightnessController.cpp +++ b/sim/components/brightness/BrightnessController.cpp @@ -2,40 +2,39 @@ // #include #include "displayapp/screens/Symbols.h" #include "drivers/PinMap.h" + +#include // std::ignore + using namespace Pinetime::Controllers; void BrightnessController::Init() { - // nrf_gpio_cfg_output(PinMap::LcdBacklightLow); - // nrf_gpio_cfg_output(PinMap::LcdBacklightMedium); - // nrf_gpio_cfg_output(PinMap::LcdBacklightHigh); Set(level); } +void BrightnessController::ApplyBrightness(uint16_t rawBrightness) { + std::ignore = rawBrightness; +} + void BrightnessController::Set(BrightnessController::Levels level) { this->level = level; - // switch (level) { - // default: - // case Levels::High: - // nrf_gpio_pin_clear(PinMap::LcdBacklightLow); - // nrf_gpio_pin_clear(PinMap::LcdBacklightMedium); - // nrf_gpio_pin_clear(PinMap::LcdBacklightHigh); - // break; - // case Levels::Medium: - // nrf_gpio_pin_clear(PinMap::LcdBacklightLow); - // nrf_gpio_pin_clear(PinMap::LcdBacklightMedium); - // nrf_gpio_pin_set(PinMap::LcdBacklightHigh); - // break; - // case Levels::Low: - // nrf_gpio_pin_clear(PinMap::LcdBacklightLow); - // nrf_gpio_pin_set(PinMap::LcdBacklightMedium); - // nrf_gpio_pin_set(PinMap::LcdBacklightHigh); - // break; - // case Levels::Off: - // nrf_gpio_pin_set(PinMap::LcdBacklightLow); - // nrf_gpio_pin_set(PinMap::LcdBacklightMedium); - // nrf_gpio_pin_set(PinMap::LcdBacklightHigh); - // break; - // } + switch (level) { + default: + case Levels::High: + ApplyBrightness(3 * timerPeriod); + break; + case Levels::Medium: + ApplyBrightness(2 * timerPeriod); + break; + case Levels::Low: + ApplyBrightness(timerPeriod); + break; + case Levels::AlwaysOn: + ApplyBrightness(timerPeriod / 10); + break; + case Levels::Off: + ApplyBrightness(0); + break; + } } void BrightnessController::Lower() { @@ -74,14 +73,6 @@ BrightnessController::Levels BrightnessController::Level() const { return level; } -void BrightnessController::Backup() { - backupLevel = level; -} - -void BrightnessController::Restore() { - Set(backupLevel); -} - void BrightnessController::Step() { switch (level) { case Levels::Low: @@ -97,7 +88,6 @@ void BrightnessController::Step() { break; } } - const char* BrightnessController::GetIcon() { switch (level) { case Levels::Medium: @@ -123,4 +113,4 @@ const char* BrightnessController::ToString() { default: return "???"; } -} \ No newline at end of file +} diff --git a/sim/components/brightness/BrightnessController.h b/sim/components/brightness/BrightnessController.h index 4ee4921..0bd7028 100644 --- a/sim/components/brightness/BrightnessController.h +++ b/sim/components/brightness/BrightnessController.h @@ -2,6 +2,9 @@ #include +//#include "nrf_ppi.h" +//#include "nrfx_gpiote.h" + namespace Pinetime { namespace Controllers { class BrightnessController { @@ -15,15 +18,30 @@ namespace Pinetime { void Higher(); void Step(); - void Backup(); - void Restore(); - const char* GetIcon(); const char* ToString(); private: Levels level = Levels::High; - Levels backupLevel = Levels::High; + static constexpr uint8_t UNSET = UINT8_MAX; + uint8_t lastPin = UNSET; + // Maximum time (μs) it takes for the RTC to fully stop + static constexpr uint8_t rtcStopTime = 46; + // Frequency of timer used for PWM (Hz) + static constexpr uint16_t timerFrequency = 32768; + // Backlight PWM frequency (Hz) + static constexpr uint16_t pwmFreq = 1000; + // Wraparound point in timer ticks + // Defines the number of brightness levels between each pin + static constexpr uint16_t timerPeriod = timerFrequency / pwmFreq; + // Warning: nimble reserves some PPIs + // https://github.com/InfiniTimeOrg/InfiniTime/blob/034d83fe6baf1ab3875a34f8cee387e24410a824/src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c#L53 + // SpiMaster uses PPI 0 for an erratum workaround + // Channel 1, 2 should be free to use + //static constexpr nrf_ppi_channel_t ppiBacklightOn = NRF_PPI_CHANNEL1; + //static constexpr nrf_ppi_channel_t ppiBacklightOff = NRF_PPI_CHANNEL2; + + void ApplyBrightness(uint16_t val); }; } }