44 #define CHILD_ID_LIGHT 1
46 #define EPROM_LIGHT_STATE 1
47 #define EPROM_DIMMER_LEVEL 2
52 #define SN "Dimmable Light"
55 int16_t LastLightState=LIGHT_OFF;
56 int16_t LastDimValue=100;
58 MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
59 MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
64 int LightState=
loadState(EPROM_LIGHT_STATE);
66 LastLightState=LightState;
67 int DimValue=
loadState(EPROM_DIMMER_LEVEL);
68 if ((DimValue>0)&&(DimValue<=100)) {
70 LastDimValue=DimValue;
75 SetCurrentState2Hardware();
77 Serial.println(
"Node ready to receive messages..." );
85 present(CHILD_ID_LIGHT, S_DIMMER );
94 if (message.
getType() == V_LIGHT) {
95 Serial.println(
"V_LIGHT command received..." );
97 int lstate= atoi( message.
data );
98 if ((lstate<0)||(lstate>1)) {
99 Serial.println(
"V_LIGHT data invalid (should be 0/1)" );
102 LastLightState=lstate;
103 saveState(EPROM_LIGHT_STATE, LastLightState);
105 if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) {
110 saveState(EPROM_DIMMER_LEVEL, LastDimValue);
116 }
else if (message.
getType() == V_DIMMER) {
117 Serial.println(
"V_DIMMER command received..." );
118 int dimvalue= atoi( message.
data );
119 if ((dimvalue<0)||(dimvalue>100)) {
120 Serial.println(
"V_DIMMER data invalid (should be 0..100)" );
124 LastLightState=LIGHT_OFF;
126 LastLightState=LIGHT_ON;
127 LastDimValue=dimvalue;
128 saveState(EPROM_DIMMER_LEVEL, LastDimValue);
131 Serial.println(
"Invalid command received..." );
136 SetCurrentState2Hardware();
139 void SetCurrentState2Hardware()
141 if (LastLightState==LIGHT_OFF) {
142 Serial.println(
"Light state: OFF" );
144 Serial.print(
"Light state: ON, Level: " );
145 Serial.println( LastDimValue );
149 SendCurrentState2Controller();
152 void SendCurrentState2Controller()
154 if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
155 send(dimmerMsg.
set((int16_t)0));
157 send(dimmerMsg.
set(LastDimValue));