From 998b9356fef2aff04e8c24ddd1c8ab1c0db235c5 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 2 Jan 2016 22:15:51 +0100 Subject: [PATCH 1/2] campapp: RGB LED amplifying Brightness of RGB LED animations varies a lot, and sometimes desired brightness depends on the environment and situation. Aside of editing the animations directly, there was no way to adjust the brightness of the LEDs. This commit introduces a new config entry, "rgbleds_amp", which allows to adjust the brightness of RGB LEDs in campapp animations. The default value of 8 is a neutral setting. Keep in mind that with static animations amp setting does not apply immediately (either the frame delay must pass or the new animation has to be loaded). --- campapp/rgb_leds.c | 31 ++++++++++++++++++++++++++++++- r0ketlib/config.c | 1 + r0ketlib/config.h | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/campapp/rgb_leds.c b/campapp/rgb_leds.c index 399ca3a1..cb21a1a8 100644 --- a/campapp/rgb_leds.c +++ b/campapp/rgb_leds.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #define MAX_LED_FRAMES 50 @@ -40,7 +41,35 @@ void tick_rgbLeds(void) { if(GLOBAL(rgbleds)) { if(frames > 0) { if(ctr == 0) { - ws2812_sendarray(&leds[framectr*3*8+2], 3*8); + unsigned char amplified[3*8]; + + // determine amplifier level based on the config + signed char amplvl = GLOBAL(rgbleds_amp) - 8; + if (amplvl > 0) { + amplvl = round(sqrt(pow(2,amplvl+1))); + } else if (amplvl < 0) { + amplvl = -round(sqrt(pow(2,-amplvl+1))); + } else { + amplvl = 1; + } + + // iterate through every value in the frame (3 channels * 8 LEDs) + for (int i=0; i<3*8; i++) { + // copy original value + unsigned char origval = leds[(framectr*3*8+2)+i]; + // set the amplified value + if (amplvl >= 0) { + amplified[i] = origval * amplvl; + if (amplvl != 0 && amplified[i] / amplvl != origval) { + // overflow! + amplified[i] = 255; + } + } else { + amplified[i] = origval / -amplvl; + } + } + + ws2812_sendarray(&lified[0], 3*8); framectr++; if(framectr >= frames) framectr = 0; diff --git a/r0ketlib/config.c b/r0ketlib/config.c index 8712cf70..2e5ef535 100644 --- a/r0ketlib/config.c +++ b/r0ketlib/config.c @@ -35,6 +35,7 @@ struct CDESC the_config[]= { {"nickbg", 255, 0, 255, 1, CFG_TYPE_DEVEL}, {"vdd_fix", 0, 0, 1, 0, 0}, {"rgbleds", 0, 0, 1, 0, 0}, + {"rgbleds_amp", 8, 0, 16, 0, 0}, { NULL, 0, 0, 0 , 0, 0}, }; diff --git a/r0ketlib/config.h b/r0ketlib/config.h index f43537bf..c6eb0f8c 100644 --- a/r0ketlib/config.h +++ b/r0ketlib/config.h @@ -49,6 +49,7 @@ extern char ledfile[]; #define GLOBALnickbg (the_config[13].value) #define GLOBALvdd_fix (the_config[14].value) #define GLOBALrgbleds (the_config[15].value) +#define GLOBALrgbleds_amp (the_config[16].value) #define GLOBAL(x) GLOBAL ## x From 044e6cb7a1169f79639a594c02e03cb2c6857540 Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Sat, 9 Jan 2016 03:42:57 +0100 Subject: [PATCH 2/2] campapp: rgb_leds: use standard types Internets say that's the good way so they must be right. https://matt.sh/howto-c --- campapp/rgb_leds.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/campapp/rgb_leds.c b/campapp/rgb_leds.c index cb21a1a8..53bdde4b 100644 --- a/campapp/rgb_leds.c +++ b/campapp/rgb_leds.c @@ -7,15 +7,16 @@ #include #include #include +#include #include #define MAX_LED_FRAMES 50 #define BUF_SIZE 3*8*MAX_LED_FRAMES+2 -unsigned char leds[BUF_SIZE]; -unsigned int frames = 0; -unsigned int ctr = 0; -unsigned int framectr = 0; +uint8_t leds[BUF_SIZE]; +uint8_t frames = 0; +uint16_t ctr = 0; +uint8_t framectr = 0; void readRgbLedFile(void) { int size = getFileSize(GLOBAL(ledfile)); @@ -41,10 +42,10 @@ void tick_rgbLeds(void) { if(GLOBAL(rgbleds)) { if(frames > 0) { if(ctr == 0) { - unsigned char amplified[3*8]; + uint8_t amplified[3*8]; // determine amplifier level based on the config - signed char amplvl = GLOBAL(rgbleds_amp) - 8; + int8_t amplvl = GLOBAL(rgbleds_amp) - 8; if (amplvl > 0) { amplvl = round(sqrt(pow(2,amplvl+1))); } else if (amplvl < 0) { @@ -54,9 +55,9 @@ void tick_rgbLeds(void) { } // iterate through every value in the frame (3 channels * 8 LEDs) - for (int i=0; i<3*8; i++) { + for (int8_t i=0; i<3*8; i++) { // copy original value - unsigned char origval = leds[(framectr*3*8+2)+i]; + uint8_t origval = leds[(framectr*3*8+2)+i]; // set the amplified value if (amplvl >= 0) { amplified[i] = origval * amplvl;