From c3b4806e85e567d00b85ec847edfc11cc991d4f6 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 21 Jan 2019 17:09:51 +0200 Subject: [PATCH] add led emission normalization --- targets/stm32l432/src/app.h | 4 ++-- targets/stm32l432/src/device.c | 7 ++++++- targets/stm32l432/src/led.c | 31 +++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/targets/stm32l432/src/app.h b/targets/stm32l432/src/app.h index c14a7ed..1da200d 100644 --- a/targets/stm32l432/src/app.h +++ b/targets/stm32l432/src/app.h @@ -53,10 +53,10 @@ void hw_init(void); // 0xRRGGBB #define LED_INIT_VALUE 0x000800 #define LED_WINK_VALUE 0x000010 -#define LED_MAX_SCALER 30 +#define LED_MAX_SCALER 15 #define LED_MIN_SCALER 1 // # of ms between each change in LED -#define HEARTBEAT_PERIOD 100 +#define HEARTBEAT_PERIOD 150 // Each LED channel will be multiplied by a integer between LED_MAX_SCALER // and LED_MIN_SCALER to cause the slow pulse. E.g. // #define LED_INIT_VALUE 0x301000 diff --git a/targets/stm32l432/src/device.c b/targets/stm32l432/src/device.c index 7bc5f48..6187ff6 100644 --- a/targets/stm32l432/src/device.c +++ b/targets/stm32l432/src/device.c @@ -209,9 +209,14 @@ void heartbeat() val++; } - if (val > LED_MAX_SCALER || val < LED_MIN_SCALER) + if (val >= LED_MAX_SCALER || val <= LED_MIN_SCALER) { state = !state; + + if (val > LED_MAX_SCALER) + val = LED_MAX_SCALER; + if (val < LED_MIN_SCALER) + val = LED_MIN_SCALER; } #ifdef LED_WINK_VALUE diff --git a/targets/stm32l432/src/led.c b/targets/stm32l432/src/led.c index 0bae6d8..ce9b7a0 100644 --- a/targets/stm32l432/src/led.c +++ b/targets/stm32l432/src/led.c @@ -29,20 +29,39 @@ #include "device.h" #include "log.h" +// normalization formula: 16.06*x^0.33 = (0%-100%) +// here values: value * 10 +uint8_t norm_k[] = { + 0, 80, 101, 115, 127, 137, 145, 153, 159, 166, + 172, 177, 182, 187, 192, 196, 200, 205, 208, 212, + 216, 219, 223, 226, 229, 232, 235, 238, 241, 245}; +#define norm_k_len sizeof(norm_k) + +uint32_t led_normalization(uint8_t value) +{ + if (value > norm_k_len - 1) + { + return value * 10; + } else { + return norm_k[value]; + } +} + void led_rgb(uint32_t hex) { - uint32_t r = hex >> 16; - uint32_t g = (hex >> 8)&0xff; - uint32_t b = hex & 0xff; + uint32_t r = led_normalization((hex >> 16) & 0xff); + uint32_t g = led_normalization((hex >> 8) & 0xff); + uint32_t b = led_normalization(hex & 0xff); // CCR2 == blue // CCR3 == red // CCR4 == green // map and scale colors - TIM2->CCR2 = 1000 - (b * 1000)/(256); - TIM2->CCR3 = 1000 - (r * 1000)/(256*6); - TIM2->CCR4 = 1000 - (g * 1000)/(256); + // normalization table values: value * 10 + TIM2->CCR2 = 1000 - (b * 100)/(256); + TIM2->CCR3 = 1000 - (r * 100)/(256*6); + TIM2->CCR4 = 1000 - (g * 100)/(256); } void led_test_colors()