Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add PWM Backlight
  • Loading branch information
hubmartin committed Aug 12, 2021
commit 266178d7ae1490060924410bf1d551919edd280c
51 changes: 39 additions & 12 deletions src/components/brightness/BrightnessController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,65 @@
#include <hal/nrf_gpio.h>
#include "displayapp/screens/Symbols.h"

#include "nrf_pwm.h"

using namespace Pinetime::Controllers;

void BrightnessController::Init() {

// Use only LIGHT1 mosfet with 30R resistor for now
nrf_gpio_cfg_output(pinLcdBacklight1);
nrf_gpio_cfg_output(pinLcdBacklight2);
nrf_gpio_cfg_output(pinLcdBacklight3);

nrf_gpio_pin_clear(pinLcdBacklight1);
nrf_gpio_pin_clear(pinLcdBacklight2);
nrf_gpio_pin_clear(pinLcdBacklight3);

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[] = {pinLcdBacklight3, pinLcdBacklight2, pinLcdBacklight1, 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::Set(BrightnessController::Levels level) {

this->level = level;
switch (level) {
default:
case Levels::High:
nrf_gpio_pin_clear(pinLcdBacklight1);
nrf_gpio_pin_clear(pinLcdBacklight2);
nrf_gpio_pin_clear(pinLcdBacklight3);
setPwm(10000);
break;
case Levels::Medium:
nrf_gpio_pin_clear(pinLcdBacklight1);
nrf_gpio_pin_clear(pinLcdBacklight2);
nrf_gpio_pin_set(pinLcdBacklight3);
setPwm(5000);
break;
case Levels::Low:
nrf_gpio_pin_clear(pinLcdBacklight1);
nrf_gpio_pin_set(pinLcdBacklight2);
nrf_gpio_pin_set(pinLcdBacklight3);
setPwm(500);
break;
case Levels::Off:
nrf_gpio_pin_set(pinLcdBacklight1);
nrf_gpio_pin_set(pinLcdBacklight2);
nrf_gpio_pin_set(pinLcdBacklight3);
setPwm(0);
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/brightness/BrightnessController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ namespace Pinetime {
static constexpr uint8_t pinLcdBacklight3 = 23;
Levels level = Levels::High;
Levels backupLevel = Levels::High;
uint16_t pwmSequence[1] = {10000};

void setPwm(uint16_t val);

};
}
}