From 724a2402abc59bab57b6a4c7df5b36aaf0e4ea56 Mon Sep 17 00:00:00 2001 From: true Date: Sun, 22 Oct 2023 00:40:01 -0700 Subject: [PATCH] Added initial RGBLED program, basic button handling Just another rainbow thing. --- include/hsv2rgb.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++ include/rgbprog.h | 16 ++++++++ include/userio.h | 1 + src/hsv2rgb.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ src/led.c | 6 +-- src/main.c | 7 ++++ src/probe.c | 29 +++++++++----- src/rgbprog.c | 88 +++++++++++++++++++++++++++++++++++++++--- src/userio.c | 25 +++++++++++- 9 files changed, 344 insertions(+), 20 deletions(-) create mode 100644 include/hsv2rgb.h create mode 100644 include/rgbprog.h create mode 100644 src/hsv2rgb.c diff --git a/include/hsv2rgb.h b/include/hsv2rgb.h new file mode 100644 index 0000000..2b36f5c --- /dev/null +++ b/include/hsv2rgb.h @@ -0,0 +1,98 @@ +#ifndef _INC_HSV2RGB_H +#define _INC_HSV2RGB_H + + + +#include + + +typedef struct color_rgb { + uint8_t r; + uint8_t g; + uint8_t b; +} color_rgb; + +typedef struct color_hsv { + int16_t h; + uint8_t s; + uint8_t v; +} color_hsv; + + +#define HSV_HUE_SEXTANT 256 +#define HSV_HUE_STEPS (6 * HSV_HUE_SEXTANT) + +#define HSV_HUE_MIN 0 +#define HSV_HUE_MAX (HSV_HUE_STEPS - 1) +#define HSV_SAT_MIN 0 +#define HSV_SAT_MAX 255 +#define HSV_VAL_MIN 0 +#define HSV_VAL_MAX 255 + +/* Options: */ +#define HSV_USE_SEXTANT_TEST /* Limit the hue to 0...360 degrees */ + + +void hsv2rgb_8b(int16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g , uint8_t *b); + + +/* + * Macros that are common to all implementations + */ +#ifdef HSV_USE_SEXTANT_TEST +#define HSV_SEXTANT_TEST(sextant) \ + if((sextant) > 5) { \ + (sextant) = 5; \ + } + +#else +#define HSV_SEXTANT_TEST(sextant) +#endif + +/* + * Pointer swapping: + * sext. r g b r<>b g<>b r <> g result + * 0 0 0 v u c !u v c u v c + * 0 0 1 d v c d v c + * 0 1 0 c v u u v c u v c + * 0 1 1 c d v v d c d v c d v c + * 1 0 0 u c v u v c u v c + * 1 0 1 v c d v d c d v c d v c + * + * if(sextant & 2) + * r <-> b + * + * if(sextant & 4) + * g <-> b + * + * if(!(sextant & 6) { + * if(!(sextant & 1)) + * r <-> g + * } else { + * if(sextant & 1) + * r <-> g + * } + */ +#define HSV_SWAPPTR(a,b) do { uint8_t *tmp = (a); (a) = (b); (b) = tmp; } while(0) +#define HSV_POINTER_SWAP(sextant,r,g,b) \ + do { \ + if((sextant) & 2) { \ + HSV_SWAPPTR((r), (b)); \ + } \ + if((sextant) & 4) { \ + HSV_SWAPPTR((g), (b)); \ + } \ + if(!((sextant) & 6)) { \ + if(!((sextant) & 1)) { \ + HSV_SWAPPTR((r), (g)); \ + } \ + } else { \ + if((sextant) & 1) { \ + HSV_SWAPPTR((r), (g)); \ + } \ + } \ + } while(0) + + + +#endif /* _INC_HSV2RGB_H */ diff --git a/include/rgbprog.h b/include/rgbprog.h new file mode 100644 index 0000000..9a5b281 --- /dev/null +++ b/include/rgbprog.h @@ -0,0 +1,16 @@ +/* + * rgbprog.h: making your eyes light up + * + * file creation: 20231021 2338 + */ + +#ifndef _INC_RGBPROG_H +#define _INC_RGBPROG_H + + + +void rgbprog_run(); + + + +#endif /* _INC_RGBPROG_H */ \ No newline at end of file diff --git a/include/userio.h b/include/userio.h index 103758d..2f64924 100644 --- a/include/userio.h +++ b/include/userio.h @@ -30,6 +30,7 @@ extern uint8_t knob[2]; void userio_parse(); uint8_t userio_get_mode(); +int16_t userio_get_btn(); diff --git a/src/hsv2rgb.c b/src/hsv2rgb.c new file mode 100644 index 0000000..45bab96 --- /dev/null +++ b/src/hsv2rgb.c @@ -0,0 +1,94 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 B. Stultiens + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + + +#include "hsv2rgb.h" + + +void hsv2rgb_8b(int16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g , uint8_t *b) +{ + uint8_t sextant; + uint8_t bb; + uint16_t ww; + uint8_t h_fraction; + + if (!(s)) { + *(r) = *(g) = *(b) = (v); + return; + } + + sextant = h >> 8; + HSV_SEXTANT_TEST(sextant); // Optional: Limit hue sextants to defined space + + HSV_POINTER_SWAP(sextant, r, g, b); // Swap pointers depending which sextant we are in + + *g = v; // Top level + + // Perform actual calculations + /* + * Bottom level: v * (1.0 - s) + * --> (v * (255 - s) + error_corr) / 256 + */ + bb = ~s; + ww = v * bb; + ww += 1; // Error correction + ww += ww >> 8; // Error correction + *b = ww >> 8; + + h_fraction = h & 0xff; // 0...255 + + if(!(sextant & 1)) { + // *r = ...slope_up...; + /* + * Slope up: v * (1.0 - s * (1.0 - h)) + * --> (v * (255 - (s * (256 - h) + error_corr1) / 256) + error_corr2) / 256 + */ + ww = !h_fraction ? ((uint16_t)s << 8) : (s * (uint8_t)(-h_fraction)); + ww += ww >> 8; // Error correction 1 + bb = ww >> 8; + bb = ~bb; + ww = v * bb; + ww += v >> 1; // Error correction 2 + *r = ww >> 8; + } else { + // *r = ...slope_down...; + /* + * Slope down: v * (1.0 - s * h) + * --> (v * (255 - (s * h + error_corr1) / 256) + error_corr2) / 256 + */ + ww = s * h_fraction; + ww += ww >> 8; // Error correction 1 + bb = ww >> 8; + bb = ~bb; + ww = v * bb; + ww += v >> 1; // Error correction 2 + *r = ww >> 8; + + /* + * A perfect match for h_fraction == 0 implies: + * *r = (ww >> 8) + (h_fraction ? 0 : 1) + * However, this is an extra calculation that may not be required. + */ + } +} diff --git a/src/led.c b/src/led.c index 0547b89..563d77b 100644 --- a/src/led.c +++ b/src/led.c @@ -47,9 +47,9 @@ void led_next() if (!led_sel || (led_mode == MODE_RGB)) { // set PWMs in RGB mode, or when right LED set - PWM_RGB_R = rgb[led_sel][RED]; - PWM_RGB_G = rgb[led_sel][GRN]; - PWM_RGB_B = rgb[led_sel][BLU]; + PWM_RGB_R = (rgb[led_sel][RED] >> 1) + (rgb[led_sel][RED] >> 2); // 75% + PWM_RGB_G = (rgb[led_sel][GRN] >> 2) + (rgb[led_sel][GRN] >> 3); // 62% + PWM_RGB_B = (rgb[led_sel][BLU] ) + (rgb[led_sel][BLU] << 2); // 125% } else { // clear PWMs in piezo mode when piezo should be active PWM_RGB_R = PWM_RGB_G = PWM_RGB_B = 0; diff --git a/src/main.c b/src/main.c index bf3968c..f753f7c 100644 --- a/src/main.c +++ b/src/main.c @@ -142,6 +142,13 @@ __attribute__ ((long_call, section(".ramfunc"))) void SysTick_Handler(void) // shifted counter for use in the program cs = ctr >> 2; + // run LED program (if in LED mode) at 256Hz + if (!(cs & 0x3)) { + if (userio_get_mode() == MODE_FUN) { + rgbprog_run(); + } + } + // adc tested to result in about 61 reads/second if (!adc_next()) { // adc has new computed results diff --git a/src/probe.c b/src/probe.c index 02d375a..d7aba3e 100644 --- a/src/probe.c +++ b/src/probe.c @@ -35,7 +35,9 @@ static uint16_t vref; + static uint8_t latch = 0; +static uint8_t buzzer = 0; @@ -47,6 +49,11 @@ static inline void probe_measure_cont() probe = adc_avg[ADC_PROBE]; v_ext = adc_avg[ADC_VREF_EXT]; + // if the button has been pushed, toggle the buzzer + if (userio_get_btn() > 0) { + buzzer ^= 1; + } + // this LED will not set anything, // but zero it out anyway led_setrgb(1, 0, 0, 0); @@ -61,13 +68,15 @@ static inline void probe_measure_cont() } if (latch) { + // indicate continuity if (x >= 2420) latch--; // hysteresis - led_setrgb(0, 0, 255, 0); + led_setrgb(0, 0, 200, ((buzzer) ? 500 : 0)); led_buzz(0); - if (knob[0] > 128) led_buzz(1); + if (buzzer) led_buzz(1); } else { - led_setrgb(0, 64, 0, 0); + // idle + led_setrgb(0, 120, 0, ((buzzer) ? 8 : 0)); led_buzz(0); } } @@ -86,22 +95,22 @@ static inline void probe_measure_diode() led_setrgb(0, 0, 0, 0); if (p < DIODE_SHORT) { - // off on short + // show off on short led_setrgb(1, 0, 0, 0); } if (p >= DIODE_OPEN) { - // red on open - led_setrgb(1, 255, 0, 0); + // show red on open + led_setrgb(1, 120, 0, 0); } if (p >= 1940 && p < 2650) { - // green on regular or schottky diodes - led_setrgb(1, 0, 255, 0); + // show green on regular or schottky diodes + led_setrgb(1, 0, 200, 0); } if (p >= 2650 && p < 3800) { - // blue on LEDs - led_setrgb(1, 0, 0, 255); + // show blue on LEDs + led_setrgb(1, 0, 0, 500); } // if we haven't set a value, flash red to indicate error state diff --git a/src/rgbprog.c b/src/rgbprog.c index d6532fb..473d095 100644 --- a/src/rgbprog.c +++ b/src/rgbprog.c @@ -6,20 +6,96 @@ #include "led.h" +#include "hsv2rgb.h" +#include "userio.h" -void rgbprog_continuity() +void rgbprog_rainbow(); +void rgbprog_rainbow_offset(); + +static void (*proglist[8])(void) = { + rgbprog_rainbow, + rgbprog_rainbow_offset, +}; + + + +static uint8_t prog_active; +static uint8_t prog_next; +static uint8_t prog_cntr; + +static uint16_t brightness = 0; + +static color_hsv hsv[2]; +static color_rgb rgb[2]; + + + +void rgbprog_run() { + uint32_t i; + uint8_t j; -} - -void rgbprog_diode() -{ - + // if the button has been pushed, change the brightness + if (userio_get_btn() > 0) { + brightness++; + if (brightness > 2) brightness = 0; + } + + // which program to run? + j = 0; + for (i = 32; i <= 256; i += 32) { + if (knob[0] < i) { + if (proglist[j]) { + proglist[j](); + } + break; + } + j++; + } } void rgbprog_rainbow() { + int16_t w; + w = knob[1]; + w *= 2; + w = 256 - w; + + hsv[0].h += w; + if (hsv[0].h < 0) hsv[0].h += 0x3000; + hsv[0].h %= 0x3000; + + hsv2rgb_8b(hsv[0].h >> 3, 255, 255, &rgb[0].r, &rgb[0].g, &rgb[0].b); + led_setrgb(0, rgb[0].r << brightness, rgb[0].g << brightness, rgb[0].b << brightness); + led_setrgb(1, rgb[0].r << brightness, rgb[0].g << brightness, rgb[0].b << brightness); +} + +void rgbprog_rainbow_offset() +{ + rgbprog_rainbow(); + + hsv[1].h = hsv[0].h; + hsv[1].h += (knob[0] - 32) << 6; + hsv[1].h %= 0x3000; + + hsv2rgb_8b(hsv[1].h >> 3, 255, 255, &rgb[0].r, &rgb[0].g, &rgb[0].b); + led_setrgb(1, rgb[0].r << brightness, rgb[0].g << brightness, rgb[0].b << brightness); +} + +void rgbprog_randcolorfade() +{ + +} + +void rgbprog_randcolorfade_single() +{ + +} + +void rgbprog_randcolorfade_dual() +{ + } \ No newline at end of file diff --git a/src/userio.c b/src/userio.c index 7d29bd2..d7a5e4a 100644 --- a/src/userio.c +++ b/src/userio.c @@ -131,6 +131,7 @@ void userio_parse() // knobs { + // SET0 is always connected, so just copy and scale it knob[0] = adc_avg[ADC_SET0] >> 4; // SET1/VREFEXT input can be SET1 or VREFEXT, depending on PROBESEL. @@ -143,7 +144,7 @@ void userio_parse() } else { x = adc_avg[ADC_SET1] << 12; // maximum possible level x /= SET1_MAX; // normalize to 4096max - knob[0] = (x >> 4) & 0xff; // reduce to 8-bit + knob[1] = (x >> 4) & 0xff; // reduce to 8-bit } } } @@ -152,4 +153,26 @@ void userio_parse() uint8_t userio_get_mode() { return mode; +} + +/* + * returns the button state. + * -1 = button is currently being pressed + * 0 = button is not being pressed + * >0 = button is not being pressed, but was (just?) released and held for X ticks + */ +int16_t userio_get_btn() +{ + int16_t ret; + + if (btn) return -1; + if (!btn) { + if (btn_held) { + ret = btn_held; + btn_held = 0; + return ret; + } + } + + return 0; } \ No newline at end of file