@@ -37,19 +37,28 @@ class SensorNeopixel: public Sensor {
37
37
Adafruit_NeoPixel* _pixels;
38
38
#endif
39
39
int _num_pixels = 16 ;
40
+ int _default_brightness = 255 ;
40
41
41
42
public:
42
43
SensorNeopixel (int8_t pin, uint8_t child_id = 255 ): Sensor(pin) {
43
44
_name = " NEOPIXEL" ;
44
45
children.allocateBlocks (1 );
46
+ // child for controlling the color
45
47
new Child (this ,STRING,nodeManager.getAvailableChildId (child_id), S_COLOR_SENSOR, V_RGB ,_name);
48
+ // child for controlling the brightness
49
+ new Child (this ,INT,nodeManager.getAvailableChildId (child_id+1 ),S_LIGHT_LEVEL,V_LEVEL,_name);
46
50
};
47
51
48
52
// set how many NeoPixels are attached
49
53
void setNumPixels (int value) {
50
54
_num_pixels = value;
51
55
};
52
56
57
+ // set default brightness
58
+ void setDefaultBrightness (int value) {
59
+ _default_brightness = value;
60
+ };
61
+
53
62
// format expeted is "<pixel_number>,<RGB color in a packed 32 bit format>"
54
63
// string format:
55
64
// RRGGBB color for all LEDs
@@ -101,6 +110,16 @@ class SensorNeopixel: public Sensor {
101
110
child->setValue (string);
102
111
};
103
112
113
+ // int format:
114
+ // 0-255 brighntess for all LEDs
115
+ void setBrightness (int value) {
116
+ Child* child = children.get (2 );
117
+ _pixels->setBrightness (value);
118
+ _pixels->show ();
119
+ // send value back
120
+ child->setValue (value);
121
+ };
122
+
104
123
// define what to do during setup
105
124
void onSetup () {
106
125
#if defined(CHIP_STM32)
@@ -110,16 +129,23 @@ class SensorNeopixel: public Sensor {
110
129
#endif
111
130
_pixels->begin ();
112
131
_pixels->show ();
132
+ _pixels->setBrightness (_default_brightness);
113
133
};
114
134
115
135
// what to do as the main task when receiving a message
116
136
void onReceive (MyMessage* message) {
117
137
Child* child = getChild (message->sensor );
118
138
if (child == nullptr ) return ;
119
139
if (message->getCommand () == C_SET && message->type == child->getType ()) {
140
+ if (message->type == V_LEVEL) {
141
+ int value = (int )message->getInt ();
142
+ setBrightness (value);
143
+ }
144
+ }
145
+ else {
120
146
char * string = (char *)message->getString ();
121
147
setColor (string);
122
148
}
123
- };
149
+ };
124
150
};
125
151
#endif
0 commit comments