Skip to content

Commit 549f8f9

Browse files
authored
Adding control for brightness to neopixel sensor (#409)
1 parent 76b19a7 commit 549f8f9

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,15 @@ Each sensor class may expose additional methods.
831831
~~~c
832832
// set how many NeoPixels are attached
833833
void setNumPixels(int value);
834+
// set default brightness
835+
void setDefaultBrightness(int value) {
834836
// format expected is:
835837
//<pixel_number from>-<pixel_number to>,<RGB color in a packed 24 bit format>
836838
//<pixel_number>,<RGB color in a packed 24 bit format>
837839
//<RGB color in a packed 24 bit format>
838840
void setColor(char* string);
841+
// brightness for all LEDs
842+
void setBrightness(int value)
839843
~~~
840844
841845
* SensorFPM10A

sensors/SensorNeopixel.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,28 @@ class SensorNeopixel: public Sensor {
3737
Adafruit_NeoPixel* _pixels;
3838
#endif
3939
int _num_pixels = 16;
40+
int _default_brightness = 255;
4041

4142
public:
4243
SensorNeopixel(int8_t pin, uint8_t child_id = 255): Sensor(pin) {
4344
_name = "NEOPIXEL";
4445
children.allocateBlocks(1);
46+
// child for controlling the color
4547
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);
4650
};
4751

4852
// set how many NeoPixels are attached
4953
void setNumPixels(int value) {
5054
_num_pixels = value;
5155
};
5256

57+
// set default brightness
58+
void setDefaultBrightness(int value) {
59+
_default_brightness = value;
60+
};
61+
5362
// format expeted is "<pixel_number>,<RGB color in a packed 32 bit format>"
5463
//string format:
5564
//RRGGBB color for all LEDs
@@ -101,6 +110,16 @@ class SensorNeopixel: public Sensor {
101110
child->setValue(string);
102111
};
103112

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+
104123
// define what to do during setup
105124
void onSetup() {
106125
#if defined(CHIP_STM32)
@@ -110,16 +129,23 @@ class SensorNeopixel: public Sensor {
110129
#endif
111130
_pixels->begin();
112131
_pixels->show();
132+
_pixels->setBrightness(_default_brightness);
113133
};
114134

115135
// what to do as the main task when receiving a message
116136
void onReceive(MyMessage* message) {
117137
Child* child = getChild(message->sensor);
118138
if (child == nullptr) return;
119139
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 {
120146
char* string = (char*)message->getString();
121147
setColor(string);
122148
}
123-
};
149+
};
124150
};
125151
#endif

0 commit comments

Comments
 (0)