Skip to content

Commit 229af25

Browse files
committed
Merge branch 'development'
Conflicts: libraries/MySensors/MySensor.cpp
2 parents cfbcead + ae9332b commit 229af25

File tree

6 files changed

+157
-22
lines changed

6 files changed

+157
-22
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,43 +450,49 @@ uint8_t MySensor::getChildRoute(uint8_t childId) {
450450
}
451451

452452

453-
int continueTimer = true;
453+
int8_t pinIntTrigger = 0;
454454
void wakeUp() //place to send the interrupts
455455
{
456-
continueTimer = false;
456+
pinIntTrigger = 1;
457+
}
458+
void wakeUp2() //place to send the second interrupts
459+
{
460+
pinIntTrigger = 2;
457461
}
458462

459463
void MySensor::internalSleep(unsigned long ms) {
460-
while (continueTimer && ms >= 8000) { LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); ms -= 8000; }
461-
if (continueTimer && ms >= 4000) { LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF); ms -= 4000; }
462-
if (continueTimer && ms >= 2000) { LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); ms -= 2000; }
463-
if (continueTimer && ms >= 1000) { LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); ms -= 1000; }
464-
if (continueTimer && ms >= 500) { LowPower.powerDown(SLEEP_500MS, ADC_OFF, BOD_OFF); ms -= 500; }
465-
if (continueTimer && ms >= 250) { LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); ms -= 250; }
466-
if (continueTimer && ms >= 125) { LowPower.powerDown(SLEEP_120MS, ADC_OFF, BOD_OFF); ms -= 120; }
467-
if (continueTimer && ms >= 64) { LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF); ms -= 60; }
468-
if (continueTimer && ms >= 32) { LowPower.powerDown(SLEEP_30MS, ADC_OFF, BOD_OFF); ms -= 30; }
469-
if (continueTimer && ms >= 16) { LowPower.powerDown(SLEEP_15Ms, ADC_OFF, BOD_OFF); ms -= 15; }
464+
while (!pinIntTrigger && ms >= 8000) { LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); ms -= 8000; }
465+
if (!pinIntTrigger && ms >= 4000) { LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF); ms -= 4000; }
466+
if (!pinIntTrigger && ms >= 2000) { LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF); ms -= 2000; }
467+
if (!pinIntTrigger && ms >= 1000) { LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); ms -= 1000; }
468+
if (!pinIntTrigger && ms >= 500) { LowPower.powerDown(SLEEP_500MS, ADC_OFF, BOD_OFF); ms -= 500; }
469+
if (!pinIntTrigger && ms >= 250) { LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF); ms -= 250; }
470+
if (!pinIntTrigger && ms >= 125) { LowPower.powerDown(SLEEP_120MS, ADC_OFF, BOD_OFF); ms -= 120; }
471+
if (!pinIntTrigger && ms >= 64) { LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF); ms -= 60; }
472+
if (!pinIntTrigger && ms >= 32) { LowPower.powerDown(SLEEP_30MS, ADC_OFF, BOD_OFF); ms -= 30; }
473+
if (!pinIntTrigger && ms >= 16) { LowPower.powerDown(SLEEP_15Ms, ADC_OFF, BOD_OFF); ms -= 15; }
470474
}
471475

472476
void MySensor::sleep(unsigned long ms) {
473477
// Let serial prints finish (debug, log etc)
474478
Serial.flush();
475479
RF24::powerDown();
476-
continueTimer = true;
480+
pinIntTrigger = 0;
477481
internalSleep(ms);
478482
}
479483

480-
bool MySensor::sleep(int interrupt, int mode, unsigned long ms) {
484+
bool MySensor::sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) {
481485
// Let serial prints finish (debug, log etc)
482486
bool pinTriggeredWakeup = true;
483487
Serial.flush();
484488
RF24::powerDown();
485-
attachInterrupt(interrupt, wakeUp, mode); //Interrupt on pin 3 for any change in solar power
489+
attachInterrupt(interrupt, wakeUp, mode);
486490
if (ms>0) {
487-
continueTimer = true;
491+
pinIntTrigger = 0;
488492
sleep(ms);
489-
pinTriggeredWakeup = !continueTimer;
493+
if (0 == pinIntTrigger) {
494+
pinTriggeredWakeup = false;
495+
}
490496
} else {
491497
Serial.flush();
492498
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
@@ -495,7 +501,32 @@ bool MySensor::sleep(int interrupt, int mode, unsigned long ms) {
495501
return pinTriggeredWakeup;
496502
}
497503

504+
int8_t MySensor::sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) {
505+
int8_t retVal = 1;
506+
Serial.flush(); // Let serial prints finish (debug, log etc)
507+
RF24::powerDown();
508+
attachInterrupt(interrupt1, wakeUp, mode1);
509+
attachInterrupt(interrupt2, wakeUp2, mode2);
510+
if (ms>0) {
511+
pinIntTrigger = 0;
512+
sleep(ms);
513+
if (0 == pinIntTrigger) {
514+
retVal = -1;
515+
}
516+
} else {
517+
Serial.flush();
518+
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
519+
}
520+
detachInterrupt(interrupt1);
521+
detachInterrupt(interrupt2);
498522

523+
if (1 == pinIntTrigger) {
524+
retVal = (int8_t)interrupt1;
525+
} else if (2 == pinIntTrigger) {
526+
retVal = (int8_t)interrupt2;
527+
}
528+
return retVal;
529+
}
499530

500531
#ifdef DEBUG
501532
void MySensor::debugPrint(const char *fmt, ... ) {

libraries/MySensors/MySensor.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,20 @@ class MySensor : public RF24
219219
* @param ms Number of milliseconds to sleep or 0 to sleep forever
220220
* @return true if wake up was triggered by pin change and false means timer woke it up.
221221
*/
222-
bool sleep(int interrupt, int mode, unsigned long ms=0);
222+
bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms=0);
223+
224+
/**
225+
* Sleep (PowerDownMode) the Arduino and radio. Wake up on timer or pin change for two separate interrupts.
226+
* See: http://arduino.cc/en/Reference/attachInterrupt for details on modes and which pin
227+
* is assigned to what interrupt. On Nano/Pro Mini: 0=Pin2, 1=Pin3
228+
* @param interrupt1 First interrupt that should trigger the wakeup
229+
* @param mode1 Mode for first interrupt (RISING, FALLING, CHANGE)
230+
* @param interrupt2 Second interrupt that should trigger the wakeup
231+
* @param mode2 Mode for second interrupt (RISING, FALLING, CHANGE)
232+
* @param ms Number of milliseconds to sleep or 0 to sleep forever
233+
* @return Interrupt number wake up was triggered by pin change and negative if timer woke it up.
234+
*/
235+
int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);
223236

224237

225238

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Interrupt driven binary switch example with dual interrupts
2+
// Author: Patrick 'Anticimex' Fallberg
3+
// Connect one button or door/window reed switch between
4+
// digitial I/O pin 3 (BUTTON_PIN below) and GND and the other
5+
// one in similar fashion on digital I/O pin 2.
6+
// This example is designed to fit Arduino Nano/Pro Mini
7+
8+
#include <MySensor.h>
9+
#include <SPI.h>
10+
11+
#define SKETCH_NAME "Binary Sensor"
12+
#define SKETCH_MAJOR_VER "1"
13+
#define SKETCH_MINOR_VER "0"
14+
15+
#define PRIMARY_CHILD_ID 3
16+
#define SECONDARY_CHILD_ID 4
17+
18+
#define PRIMARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
19+
#define SECONDARY_BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch
20+
21+
#if (PRIMARY_BUTTON_PIN < 2 || PRIMARY_BUTTON_PIN > 3)
22+
#error PRIMARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
23+
#endif
24+
#if (SECONDARY_BUTTON_PIN < 2 || SECONDARY_BUTTON_PIN > 3)
25+
#error SECONDARY_BUTTON_PIN must be either 2 or 3 for interrupts to work
26+
#endif
27+
#if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
28+
#error PRIMARY_BUTTON_PIN and BUTTON_PIN2 cannot be the same
29+
#endif
30+
#if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
31+
#error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
32+
#endif
33+
34+
MySensor sensor_node;
35+
36+
// Change to V_LIGHT if you use S_LIGHT in presentation below
37+
MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
38+
MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
39+
40+
void setup()
41+
{
42+
sensor_node.begin();
43+
44+
// Setup the buttons
45+
pinMode(PRIMARY_BUTTON_PIN, INPUT);
46+
pinMode(SECONDARY_BUTTON_PIN, INPUT);
47+
48+
// Activate internal pull-ups
49+
digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
50+
digitalWrite(SECONDARY_BUTTON_PIN, HIGH);
51+
52+
// Send the sketch version information to the gateway and Controller
53+
sensor_node.sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER"."SKETCH_MINOR_VER);
54+
55+
// Register binary input sensor to sensor_node (they will be created as child devices)
56+
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
57+
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
58+
sensor_node.present(PRIMARY_CHILD_ID, S_DOOR);
59+
sensor_node.present(SECONDARY_CHILD_ID, S_DOOR);
60+
}
61+
62+
// Loop will iterate on changes on the BUTTON_PINs
63+
void loop()
64+
{
65+
uint8_t value;
66+
static uint8_t sentValue=2;
67+
static uint8_t sentValue2=2;
68+
69+
// Short delay to allow buttons to properly settle
70+
sensor_node.sleep(5);
71+
72+
value = digitalRead(PRIMARY_BUTTON_PIN);
73+
74+
if (value != sentValue) {
75+
// Value has changed from last transmission, send the updated value
76+
sensor_node.send(msg.set(value==HIGH ? 1 : 0));
77+
sentValue = value;
78+
}
79+
80+
value = digitalRead(SECONDARY_BUTTON_PIN);
81+
82+
if (value != sentValue2) {
83+
// Value has changed from last transmission, send the updated value
84+
sensor_node.send(msg2.set(value==HIGH ? 1 : 0));
85+
sentValue2 = value;
86+
}
87+
88+
// Sleep until something happens with the sensor
89+
sensor_node.sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
90+
}

libraries/MySensors/utility/RF24.cpp

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ uint8_t RF24::get_status(void)
270270
}
271271

272272
/****************************************************************************/
273+
#if !defined (MINIMAL)
273274

274275
void RF24::print_status(uint8_t status)
275276
{
@@ -325,7 +326,7 @@ void RF24::print_address_register(const char* name, uint8_t reg, uint8_t qty)
325326

326327
printf_P(PSTR("\r\n"));
327328
}
328-
329+
#endif
329330
/****************************************************************************/
330331

331332
RF24::RF24(uint8_t _cepin, uint8_t _cspin):

libraries/MySensors/utility/RF24.h

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ class RF24
863863
* @return Current value of status register
864864
*/
865865
uint8_t get_status(void);
866-
866+
#if !defined (MINIMAL)
867867
/**
868868
* Decode and print the given status to stdout
869869
*
@@ -907,7 +907,7 @@ class RF24
907907
* @param qty How many successive registers to print
908908
*/
909909
void print_address_register(const char* name, uint8_t reg, uint8_t qty = 1);
910-
910+
#endif
911911
/**
912912
* Turn on or off the special features of the chip
913913
*

libraries/MySensors/utility/RF24_config.h

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <stddef.h>
2222

2323
//TMRh20:
24-
//#define MINIMAL
24+
#define MINIMAL // saves 432 bytes!
2525

2626
// Define _BV for non-Arduino platforms and for Arduino DUE
2727
#if defined (ARDUINO)

0 commit comments

Comments
 (0)