Skip to content

Commit b3d5503

Browse files
add optional support for WS2812 RGB LED, fixes #106
specific indexes in the LED color time sequence are used for: - dark separator indicates "start of sequence" (also used for LED init and re-init) - color according to indications (3bits encoded via RGB color) (can be extended to show up to 32 indication bits) (dark if there is nothing to indicate) - white separator indicates "start of radiation display" (also works as LED test, white = R on + G on + B on) - color according to radiation (repeated, displayed most of the time) dark (unusually low / 0) green (normal) yellow (more than normal) red (much more than normal)
1 parent 8167eb7 commit b3d5503

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

multigeiger/multigeiger.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
#include <Adafruit_Sensor.h>
8484
#include <Adafruit_BME280.h>
8585

86+
// for RGB LED
87+
#include "status_led.h"
8688

8789
// Check if a CPU (board) with LoRa is selected. If not, deactivate SEND2LORA.
8890
#if !((CPU==LORA) || (CPU==STICK))
@@ -553,6 +555,9 @@ void loop() {
553555

554556
Dose_Rate = Count_Rate * GMC_factor_uSvph; // ... and dose rate
555557

558+
// indicate status on RGB LED (if any)
559+
indicate(Dose_Rate, I_TEST);
560+
556561
// calculate the radiation over the complete time from start
557562
accumulated_Count_Rate = 0.0;
558563
if (accumulated_time != 0) {

multigeiger/status_led.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+

multigeiger/status_led.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// show status indication via WS2812 RGB LED
2+
3+
// indication value used to reset indications to "all off"
4+
#define I_RESET 0
5+
// indication value used to mask no bit, e.g. in compute_color
6+
#define I_NONE 0
7+
8+
// indications, values are 32bit bit masks, valid values are 2^N
9+
#define I_TEST 1 // reserved to test the indication code
10+
#define I_HV_ERROR 2 // there is a problem with high voltage generation
11+
#define I_CONN_ERROR 4 // there is a problem with the network connection
12+
13+
// indicate radiation and special indications via a color time sequence.
14+
// you should call this in regular time intervals [e.g. 1s].
15+
void indicate(float radiation, unsigned int indication);

multigeiger/userdefines-example.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
// Note: The TTN configuration needs to be done in lorawan.cpp (starting at line 65).
6262
#define SEND2LORA 0
6363

64+
// 0: no RGB status LED, 1: WS2812B status LED
65+
#define STATUS_LED 1
66+
6467
// *********************************************************************************
6568
// END of user changeable parameters. Do not edit beyond this point!
6669
// *********************************************************************************

platformio-example.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ lib_deps=
4444
Adafruit BME280 Library
4545
Adafruit Unified Sensor
4646
IotWebConf
47+
NeoPixelBus
4748

0 commit comments

Comments
 (0)