|
| 1 | +// show status indication via WS2812 RGB LED |
| 2 | + |
| 3 | +#include "userdefines.h" |
| 4 | + |
| 5 | +#if STATUS_LED==1 // we have a WS2812 RGB LED! |
| 6 | + |
| 7 | +#include <NeoPixelBus.h> |
| 8 | + |
| 9 | +#include "status_led.h" |
| 10 | + |
| 11 | +#define PIXEL_COUNT 1 |
| 12 | +#define PIXEL_PIN 25 |
| 13 | + |
| 14 | +RgbColor red(255, 0, 0); |
| 15 | +RgbColor green(0, 255, 0); |
| 16 | +RgbColor blue(0, 0, 255); |
| 17 | +RgbColor yellow(255, 255, 0); |
| 18 | +RgbColor white(255, 255, 255); |
| 19 | +RgbColor black(0, 0, 0); |
| 20 | + |
| 21 | +static NeoPixelBus<NeoRgbFeature, Neo800KbpsMethod> LEDs(PIXEL_COUNT, PIXEL_PIN); |
| 22 | + |
| 23 | +static RgbColor last_col = black; |
| 24 | + |
| 25 | +static void set_LED(RgbColor col) { |
| 26 | + if (col == last_col) |
| 27 | + return; // nothing to change |
| 28 | + |
| 29 | + LEDs.SetPixelColor(0, col); // only 1 LED at index 0 |
| 30 | + LEDs.Show(); |
| 31 | + last_col = col; |
| 32 | +} |
| 33 | + |
| 34 | +static void init_LED(void) { |
| 35 | + LEDs.Begin(); // all LEDs off |
| 36 | + LEDs.Show(); |
| 37 | + last_col = black; // consistency sw state == hw state |
| 38 | +} |
| 39 | + |
| 40 | +static void compute_color(unsigned int indication, |
| 41 | + unsigned int mask_r, unsigned int mask_g, unsigned int mask_b, |
| 42 | + RgbColor *color) { |
| 43 | + // assign to *color depending on whether the r/g/b masked bits are set in the indication value |
| 44 | + *color = RgbColor((indication & mask_r) ? 255 : 0, |
| 45 | + (indication & mask_g) ? 255 : 0, |
| 46 | + (indication & mask_b) ? 255 : 0) |
| 47 | +} |
| 48 | + |
| 49 | +#define IDX_W 2 |
| 50 | +#define CSL 30 |
| 51 | + |
| 52 | +void indicate(float radiation, unsigned int indication) { |
| 53 | + // radiation [uSv/h] given to set the primary color R that is shown most of the time |
| 54 | + // indication: 32 bit flags to indicate additional stuff, see status_led.h. |
| 55 | + // |
| 56 | + // this code will generate a time sequence of LED colors: |
| 57 | + // index color |
| 58 | + // ------------------------------------------------------ |
| 59 | + // 0 dark (LED init) |
| 60 | + // 1 indication color 1 |
| 61 | + // ... reserved for more indications |
| 62 | + // IDX_W white (LED test) |
| 63 | + // ...+1 radiation color |
| 64 | + // ... radiation color |
| 65 | + // CSL-1 radiation color |
| 66 | + |
| 67 | + RgbColor col; |
| 68 | + static int index = 0; // index counting modulo COLOR_SEQUENCE_LENGTH |
| 69 | + |
| 70 | + switch (index) { |
| 71 | + case 0: // show a fixed dark separator after LED init |
| 72 | + init_LED(); // (re-)init the LED |
| 73 | + col = black; |
| 74 | + break; |
| 75 | + case 1: |
| 76 | + // indication color 1: R G B |
| 77 | + compute_color(indication, I_CONN_ERROR, I_TEST, I_HV_ERROR, &col); |
| 78 | + break; |
| 79 | + case IDX_W: // show a fixed white separator and LED test |
| 80 | + col = white; |
| 81 | + break; |
| 82 | + default: // show radiation color |
| 83 | + if (radiation > 1.0) { |
| 84 | + col = red; |
| 85 | + } else if (radiation > 0.2) { |
| 86 | + col = yellow; |
| 87 | + } else if (radiation > 0.01) { |
| 88 | + col = green; |
| 89 | + } else { |
| 90 | + col = black; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + set_LED(col); |
| 95 | + |
| 96 | + if (++index == CSL) |
| 97 | + index = 0; |
| 98 | +} |
| 99 | + |
| 100 | +#else // no STATUS_LED |
| 101 | + |
| 102 | +void indicate(float radiation, unsigned int indication) { |
| 103 | + // do nothing |
| 104 | +} |
| 105 | + |
| 106 | +#endif // STATUS_LED |
| 107 | + |
0 commit comments