Skip to content

Commit 7125bf0

Browse files
KooLruuser2684
authored andcommitted
Neopixel sensor improvements
1 parent 43e6242 commit 7125bf0

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

NodeManagerLibrary.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@
2727
#define OFF 0
2828
#define ON 1
2929

30-
// define interrupt pins
31-
#define INTERRUPT_PIN_1 3
32-
#define INTERRUPT_PIN_2 2
33-
34-
// define eeprom addresses
35-
#define EEPROM_SLEEP_SAVED 0
36-
#define EEPROM_SLEEP_1 5
37-
#define EEPROM_SLEEP_2 6
38-
#define EEPROM_SLEEP_3 7
39-
#define EEPROM_USER_START 100
4030

4131
/***********************************
4232
Chip type
@@ -57,10 +47,32 @@
5747
#if defined(ESP8266) || defined(MY_GATEWAY_ESP8266)
5848
#define CHIP_ESP8266
5949
#endif
60-
#if !defined(CHIP_ESP8266) && !defined(CHIP_STM32)
50+
#if defined (MYBOARDNRF5)
51+
#define CHIP_NRF5
52+
#endif
53+
54+
#if !defined(CHIP_ESP8266) && !defined(CHIP_STM32) && !defined(CHIP_NRF5)
6155
#define CHIP_AVR
6256
#endif
6357

58+
// define interrupt pins
59+
60+
#if defined(CHIP_STM32)
61+
#define INTERRUPT_PIN_1 PB8
62+
#define INTERRUPT_PIN_2 2
63+
#else
64+
#define INTERRUPT_PIN_1 3
65+
#define INTERRUPT_PIN_2 2
66+
#endif
67+
68+
// define eeprom addresses
69+
#define EEPROM_SLEEP_SAVED 0
70+
#define EEPROM_SLEEP_1 5
71+
#define EEPROM_SLEEP_2 6
72+
#define EEPROM_SLEEP_3 7
73+
#define EEPROM_USER_START 100
74+
75+
6476
/***********************************
6577
Default configuration settings
6678
*/

NodeManagerLibrary.ino

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,11 @@ void Sensor::loop(MyMessage* message) {
633633
_track_last_value && child->isNewValue() ||
634634
_track_last_value && child->force_update_timer->isRunning() && child->force_update_timer->isOver()
635635
)
636-
#endif
637636
child->sendValue();
637+
#else
638+
child->sendValue();
639+
#endif
640+
638641
}
639642
#if FEATURE_HOOKING == ON
640643
// if a hook function is defined, call it
@@ -3934,17 +3937,16 @@ void SensorAPDS9960::onInterrupt() {
39343937
SensorNeopixel::SensorNeopixel(NodeManager& node_manager, int pin, int child_id): Sensor(node_manager, pin) {
39353938
_name = "NEOPIXEL";
39363939
children.allocateBlocks(1);
3937-
new ChildInt(this, _node->getAvailableChildId(child_id), S_COLOR_SENSOR, V_RGB ,_name);
3940+
new ChildString(this, _node->getAvailableChildId(child_id), S_COLOR_SENSOR, V_RGB ,_name);
39383941
}
39393942

39403943
// setter/getter
39413944
void SensorNeopixel::setNumPixels(int value) {
39423945
_num_pixels = value;
39433946
}
3944-
39453947
// what to do during setup
39463948
void SensorNeopixel::onSetup() {
3947-
#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4)
3949+
#if defined(CHIP_STM32)
39483950
_pixels = new NeoMaple(_num_pixels, NEO_GRB + NEO_KHZ800);
39493951
#else
39503952
_pixels = new Adafruit_NeoPixel(_num_pixels, _pin, NEO_GRB + NEO_KHZ800);
@@ -3968,39 +3970,65 @@ void SensorNeopixel::onReceive(MyMessage* message) {
39683970
}
39693971
}
39703972

3973+
//string format:
3974+
//RRGGBB color for all LEDs
3975+
//LED,RRGGBB color for specific LED
3976+
//LEDfrom-LEDto,RRGGBB color for LED range from LEDfrom to LEDto
39713977
void SensorNeopixel::setColor(char* string) {
39723978
Child* child = children.get(1);
39733979
long color = 0;
3980+
//find separator
39743981
char * p = strstr(string, ",");
39753982
if (p){
3976-
char pixelnum[6];
3983+
//extract LED or LED range part
3984+
char pixelnum[10];
39773985
int pos = (int) (p - string);
3986+
if (pos >= 10)
3987+
return;
39783988
strncpy(pixelnum, string, pos);
39793989
pixelnum[pos] = 0;
39803990

3981-
int pixel_num = atoi(pixelnum);
3982-
3983-
color = strtol(string + pos+1, NULL, 16);
3991+
int pixel_num = 0;
3992+
int pixel_end = 0;
3993+
//may be range, try find range separator -
3994+
char * r = strstr(pixelnum, "-");
3995+
if (r){
3996+
pixel_end = atoi(r+1);
3997+
*r = 0; //null terminating instead of delimeter
3998+
pixel_num = atoi(pixelnum);
3999+
}
4000+
else{
4001+
pixel_num = atoi(pixelnum);
4002+
pixel_end = pixel_num;
4003+
}
4004+
color = strtol(string + pos + 1, NULL, 16);
39844005
#if FEATURE_DEBUG == ON
39854006
Serial.print(_name);
39864007
Serial.print(F(" I="));
39874008
Serial.print(child->child_id);
39884009
Serial.print(F(" N="));
39894010
Serial.print(pixel_num);
4011+
if (pixel_num != pixel_end)
4012+
{
4013+
Serial.print(F("-"));
4014+
Serial.print(pixel_end);
4015+
}
39904016
Serial.print(F(" C="));
39914017
Serial.println(color);
39924018
#endif
3993-
_pixels->setPixelColor(pixel_num,color);
4019+
//set LED to color
4020+
for(int i=pixel_num;i<=pixel_end;i++)
4021+
_pixels->setPixelColor(i,color);
39944022
}
39954023
else //set All pixels to single color
39964024
{
39974025
color = strtol(string, NULL, 16);
39984026
for(int i=0;i<_num_pixels;i++)
39994027
_pixels->setPixelColor(i,color);
40004028
}
4001-
40024029
_pixels->show();
4003-
((ChildInt*)child)->setValueInt(color);
4030+
//send value back
4031+
((ChildString*)child)->setValueString(string);
40044032
}
40054033

40064034
#endif
@@ -4697,14 +4725,15 @@ void NodeManager::hello() {
46974725

46984726
// reboot the board
46994727
void NodeManager::reboot() {
4700-
#ifdef CHIP_AVR
47014728
#if FEATURE_DEBUG == ON
47024729
Serial.println(F("REBOOT"));
47034730
#endif
47044731
if (_reboot_pin > -1) {
47054732
// reboot the board through the reboot pin which is connected to RST by setting it to low
47064733
digitalWrite(_reboot_pin, LOW);
4707-
} else {
4734+
}
4735+
#ifdef CHIP_AVR
4736+
else {
47084737
// Software reboot with watchdog timer. Enter Watchdog Configuration mode:
47094738
WDTCSR |= (1<<WDCE) | (1<<WDE);
47104739
// Reset enable
@@ -4789,13 +4818,21 @@ void NodeManager::setupInterrupts() {
47894818
pinMode(INTERRUPT_PIN_1,INPUT);
47904819
if (_interrupt_1_initial > -1) digitalWrite(INTERRUPT_PIN_1,_interrupt_1_initial);
47914820
// for non sleeping nodes, we need to handle the interrupt by ourselves
4821+
#if defined(CHIP_STM32)
4822+
if (_status != SLEEP) attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN_1), _onInterrupt_1, (ExtIntTriggerMode)_interrupt_1_mode);
4823+
#else
47924824
if (_status != SLEEP) attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN_1), _onInterrupt_1, _interrupt_1_mode);
4825+
#endif
47934826
}
47944827
if (_interrupt_2_mode != MODE_NOT_DEFINED) {
47954828
pinMode(INTERRUPT_PIN_2, INPUT);
47964829
if (_interrupt_2_initial > -1) digitalWrite(INTERRUPT_PIN_2,_interrupt_2_initial);
47974830
// for non sleeping nodes, we need to handle the interrupt by ourselves
4831+
#if defined(CHIP_STM32)
4832+
if (_status != SLEEP) attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN_2), _onInterrupt_2, (ExtIntTriggerMode)_interrupt_2_mode);
4833+
#else
47984834
if (_status != SLEEP) attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN_2), _onInterrupt_2, _interrupt_2_mode);
4835+
#endif
47994836
}
48004837
#if FEATURE_DEBUG == ON
48014838
Serial.print(F("INT P="));

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,10 @@ Each sensor class exposes additional methods.
705705
~~~c
706706
// set how many NeoPixels are attached
707707
void setNumPixels(int value);
708-
// format expeted is "<pixel_number>,<RGB color in a packed 32 bit format>"
708+
// format expected is:
709+
//<pixel_number from>-<pixel_number to>,<RGB color in a packed 24 bit format>
710+
//<pixel_number>,<RGB color in a packed 24 bit format>
711+
//<RGB color in a packed 24 bit format>
709712
void setColor(char* string);
710713
~~~
711714

0 commit comments

Comments
 (0)