Skip to content

Commit 6a60f2b

Browse files
committed
#20 LED Behavior updates
1 parent e851540 commit 6a60f2b

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

source/board/microbitv2/flexio_pwm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ void flexio_pwm_set_dutycycle(uint8_t duty)
6868
flexio_timer_config_t fxioTimerConfig;
6969

7070
/* Check parameter */
71-
if (duty > 99)
71+
if (duty >= 255)
7272
{
73-
duty = 99;
73+
duty = 254;
7474
PIN_RED_LED_GPIO->PDDR |= PIN_RED_LED;
7575
PIN_RED_LED_GPIO->PSOR = PIN_RED_LED;
7676
PORT_SetPinMux(PIN_RED_LED_PORT, PIN_RED_LED_BIT, kPORT_MuxAsGpio);
@@ -107,8 +107,8 @@ void flexio_pwm_set_dutycycle(uint8_t duty)
107107
/* sum = DEMO_FLEXIO_CLOCK_FREQUENCY / freq_H */
108108
sum = (DEMO_FLEXIO_CLOCK_FREQUENCY * 2 / freq_Hz + 1) / 2;
109109
/* Calculate the nearest integer value for lowerValue, the high period of the pwm output */
110-
/* lowerValue = sum * duty / 100 */
111-
lowerValue = (sum * duty / 50 + 1) / 2;
110+
/* lowerValue = sum * duty / 255 */
111+
lowerValue = (sum * duty / 127 + 1) / 2;
112112
/* Calculate upper value, the low period of the pwm output */
113113
upperValue = sum - lowerValue;
114114
fxioTimerConfig.timerCompare = ((upperValue - 1) << 8U) | (lowerValue - 1);

source/board/microbitv2/microbitv2.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
#define BRD_ID_1_UPPER_THR_V 935 // Upper threshold in mV for 100nF and 4700R
5353
#define BRD_ID_1_LOWER_THR_V 268 // Lower threshold in mV for 100nF and 4700R
5454

55-
#define PWR_LED_ON_MAX_BRIGHTNESS 100 // Max LED Brightness (PC Connected)
56-
#define PWR_LED_INIT_FADE_BRIGHTNESS 80 // Initial fade LED Brightness
57-
#define PWR_LED_ON_BATT_BRIGHTNESS 40 // LED Brightness while being powered by battery
58-
#define PWR_LED_FADEOUT_MIN_BRIGHTNESS 30 // Minimum LED brightness when fading out
55+
#define PWR_LED_ON_MAX_BRIGHTNESS 255 // Max LED Brightness (PC Connected)
56+
#define PWR_LED_INIT_FADE_BRIGHTNESS 200 // Initial fade LED Brightness
57+
#define PWR_LED_ON_BATT_BRIGHTNESS 100 // LED Brightness while being powered by battery
58+
#define PWR_LED_FADEOUT_MIN_BRIGHTNESS 80 // Minimum LED brightness when fading out
5959
#define PWR_LED_SLEEP_STATE_DEFAULT true
6060
#define AUTOMATIC_SLEEP_DEFAULT true
6161

@@ -120,6 +120,7 @@ static uint32_t read_file_data_txt(uint32_t sector_offset, uint8_t *data, uint32
120120
// shutdown state
121121
static main_shutdown_state_t main_shutdown_state = MAIN_SHUTDOWN_WAITING;
122122
static uint8_t shutdown_led_dc = PWR_LED_ON_MAX_BRIGHTNESS;
123+
static uint8_t final_fade_led_dc = 0;
123124
static uint8_t power_led_max_duty_cycle = PWR_LED_ON_MAX_BRIGHTNESS;
124125
static uint8_t initial_fade_brightness = PWR_LED_INIT_FADE_BRIGHTNESS;
125126
static bool power_led_sleep_state_on = PWR_LED_SLEEP_STATE_DEFAULT;
@@ -211,10 +212,10 @@ static void set_board_id(mb_version_t board_version) {
211212
}
212213
}
213214

214-
// Apply a gamma curve to the LED. Input brightness between 0-100
215+
// Apply a gamma curve to the LED. Input brightness between 0-255
215216
static inline uint8_t get_led_gamma(uint8_t brightness) {
216217
uint8_t duty_cycle;
217-
duty_cycle = (brightness * brightness * brightness + 5000) / 10000;
218+
duty_cycle = (brightness * brightness * brightness + 32512) / (255*255);
218219
return duty_cycle;
219220
}
220221

@@ -382,11 +383,12 @@ void board_30ms_hook()
382383
case MAIN_SHUTDOWN_REACHED:
383384
// Hold LED in min brightness
384385
shutdown_led_dc = PWR_LED_FADEOUT_MIN_BRIGHTNESS;
386+
final_fade_led_dc = get_led_gamma(PWR_LED_FADEOUT_MIN_BRIGHTNESS);
385387
break;
386388
case MAIN_SHUTDOWN_REACHED_FADE:
387389
// Fast fade to off
388-
if (shutdown_led_dc > 0) {
389-
shutdown_led_dc--;
390+
if (final_fade_led_dc > 0) {
391+
final_fade_led_dc--;
390392
}
391393
break;
392394
case MAIN_USER_EVENT:
@@ -450,10 +452,10 @@ void board_30ms_hook()
450452
if (shutdown_led_dc < 10) {
451453
shutdown_led_dc++;
452454
} else if (shutdown_led_dc == 10) {
453-
shutdown_led_dc = 100;
454-
} else if (shutdown_led_dc <= 90) {
455+
shutdown_led_dc = PWR_LED_ON_MAX_BRIGHTNESS;
456+
} else if (shutdown_led_dc <= (PWR_LED_ON_MAX_BRIGHTNESS - 10)) {
455457
shutdown_led_dc = 0;
456-
} else if (shutdown_led_dc > 90) {
458+
} else if (shutdown_led_dc > (PWR_LED_ON_MAX_BRIGHTNESS - 10)) {
457459
shutdown_led_dc--;
458460
}
459461

@@ -471,7 +473,7 @@ void board_30ms_hook()
471473
}
472474
} else {
473475
blink_in_progress = 10;
474-
shutdown_led_dc = 100;
476+
shutdown_led_dc = PWR_LED_ON_MAX_BRIGHTNESS;
475477
}
476478

477479
if (blink_in_progress == 0) {
@@ -481,8 +483,14 @@ void board_30ms_hook()
481483
default:
482484
break;
483485
}
484-
uint8_t gamma_led_dc = get_led_gamma(shutdown_led_dc);
485-
flexio_pwm_set_dutycycle(gamma_led_dc);
486+
487+
// Use gamma curve except in final fade
488+
if (main_shutdown_state != MAIN_SHUTDOWN_REACHED_FADE) {
489+
uint8_t gamma_led_dc = get_led_gamma(shutdown_led_dc);
490+
flexio_pwm_set_dutycycle(gamma_led_dc);
491+
} else {
492+
flexio_pwm_set_dutycycle(final_fade_led_dc);
493+
}
486494

487495
// Remount if requested.
488496
if (do_remount) {

0 commit comments

Comments
 (0)