Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions src/components/brightness/BrightnessController.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,83 @@
#include "BrightnessController.h"
#include <hal/nrf_gpio.h>
#include "displayapp/screens/Symbols.h"

#include "nrf_pwm.h"
#include "drivers/PinMap.h"

using namespace Pinetime::Controllers;

void BrightnessController::Init() {

// Use only LIGHT1 mosfet with 30R resistor for now
nrf_gpio_cfg_output(PinMap::LcdBacklightLow);
nrf_gpio_cfg_output(PinMap::LcdBacklightMedium);
nrf_gpio_cfg_output(PinMap::LcdBacklightHigh);

nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
nrf_gpio_pin_clear(PinMap::LcdBacklightMedium);
nrf_gpio_pin_clear(PinMap::LcdBacklightHigh);

static nrf_pwm_sequence_t seq;

seq.values.p_common = pwmSequence;
seq.length = 1;
seq.repeats = 0;
seq.end_delay = 0;

uint32_t out_pins[] = {PinMap::LcdBacklightHigh, PinMap::LcdBacklightMedium, PinMap::LcdBacklightLow, NRF_PWM_PIN_NOT_CONNECTED};

nrf_pwm_pins_set(NRF_PWM0, out_pins);
nrf_pwm_enable(NRF_PWM0);
// With 8 MHz and 10000 reload timer PWM frequency is 712 Hz
nrf_pwm_configure(NRF_PWM0, NRF_PWM_CLK_8MHz, NRF_PWM_MODE_UP, 10000);
nrf_pwm_loop_set(NRF_PWM0, 0);
nrf_pwm_decoder_set(NRF_PWM0, NRF_PWM_LOAD_COMMON, NRF_PWM_STEP_AUTO);
nrf_pwm_sequence_set(NRF_PWM0, 0, &seq);
nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0);

Set(level);
}


void BrightnessController::setPwm(uint16_t val)
{
pwmSequence[0] = val;
nrf_pwm_task_trigger(NRF_PWM0, NRF_PWM_TASK_SEQSTART0);
}

void BrightnessController::pwmEnable(bool enable)
{
if(enable)
{
nrf_pwm_enable(NRF_PWM0);
uint32_t out_pins[] = {PinMap::LcdBacklightHigh, PinMap::LcdBacklightMedium, PinMap::LcdBacklightLow, NRF_PWM_PIN_NOT_CONNECTED};
nrf_pwm_pins_set(NRF_PWM0, out_pins);
}
else
{
uint32_t out_pins[] = {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED};
nrf_pwm_pins_set(NRF_PWM0, out_pins);
nrf_pwm_disable(NRF_PWM0);
}
}

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);
setPwm(10000);
break;
case Levels::Medium:
nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
nrf_gpio_pin_clear(PinMap::LcdBacklightMedium);
nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
setPwm(3800);
break;
case Levels::Low:
nrf_gpio_pin_clear(PinMap::LcdBacklightLow);
nrf_gpio_pin_set(PinMap::LcdBacklightMedium);
nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
setPwm(830);
break;
case Levels::Off:
nrf_gpio_pin_set(PinMap::LcdBacklightLow);
nrf_gpio_pin_set(PinMap::LcdBacklightMedium);
nrf_gpio_pin_set(PinMap::LcdBacklightHigh);
setPwm(0);
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/brightness/BrightnessController.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ namespace Pinetime {
private:
Levels level = Levels::High;
Levels backupLevel = Levels::High;
uint16_t pwmSequence[1] = {10000};

void setPwm(uint16_t val);
void pwmEnable(bool enable);

};
}
}