Update examples

This commit is contained in:
Henrik Ekblad
2015-09-19 23:54:35 +02:00
parent 9ed25eb8dc
commit 96f702fa4f
44 changed files with 970 additions and 764 deletions
@@ -28,8 +28,14 @@
* Stores the last light state and level in eeprom.
*
*/
#include <SPI.h>
// Enable debug prints
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
#include <MySensor.h>
#define CHILD_ID_LIGHT 1
@@ -46,24 +52,24 @@
int LastLightState=LIGHT_OFF;
int LastDimValue=100;
MySensor gw;
MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
void setup()
{
gw.begin(incomingMessage, AUTO, false);
// Initialize library and add callback for incoming messages (signing is required)
setIncomingCallback(incomingMessage);
// Send the Sketch Version Information to the Gateway
gw.sendSketchInfo(SN, SV);
sendSketchInfo(SN, SV);
gw.present(CHILD_ID_LIGHT, S_DIMMER );
present(CHILD_ID_LIGHT, S_DIMMER );
//Retreive our last light state from the eprom
int LightState=gw.loadState(EPROM_LIGHT_STATE);
int LightState=loadState(EPROM_LIGHT_STATE);
if (LightState<=1) {
LastLightState=LightState;
int DimValue=gw.loadState(EPROM_DIMMER_LEVEL);
int DimValue=loadState(EPROM_DIMMER_LEVEL);
if ((DimValue>0)&&(DimValue<=100)) {
//There should be no Dim value of 0, this would mean LIGHT_OFF
LastDimValue=DimValue;
@@ -78,9 +84,6 @@ void setup()
void loop()
{
//delay(1000); // Removed by hek
// Process incoming messages (like config and light state from controller)
gw.process();
}
void incomingMessage(const MyMessage &message)
@@ -94,14 +97,14 @@ void incomingMessage(const MyMessage &message)
return;
}
LastLightState=lstate;
gw.saveState(EPROM_LIGHT_STATE, LastLightState);
saveState(EPROM_LIGHT_STATE, LastLightState);
if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) {
//In the case that the Light State = On, but the dimmer value is zero,
//then something (probably the controller) did something wrong,
//for the Dim value to 100%
LastDimValue=100;
gw.saveState(EPROM_DIMMER_LEVEL, LastDimValue);
saveState(EPROM_DIMMER_LEVEL, LastDimValue);
}
//When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value
@@ -121,7 +124,7 @@ void incomingMessage(const MyMessage &message)
else {
LastLightState=LIGHT_ON;
LastDimValue=dimvalue;
gw.saveState(EPROM_DIMMER_LEVEL, LastDimValue);
saveState(EPROM_DIMMER_LEVEL, LastDimValue);
}
}
else {
@@ -150,10 +153,10 @@ void SetCurrentState2Hardware()
void SendCurrentState2Controller()
{
if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
gw.send(dimmerMsg.set(0));
send(dimmerMsg.set(0));
}
else {
gw.send(dimmerMsg.set(LastDimValue));
send(dimmerMsg.set(LastDimValue));
}
}