State Machine

Aus Ethersex_Wiki
Wechseln zu: Navigation, Suche
CONTROL_START

ECMD_GLOBAL(state,255);
ECMD_GLOBAL(c,0);

PIN_INPUT(Key_green)
PIN_INPUT(Key_red)
PIN_OUTPUT(LED_green)
PIN_OUTPUT(LED_red)
PIN_OUTPUT(Pulserelais_on)
PIN_OUTPUT(Pulserelais_off)

THREAD(states)
   ON state==0 DO
      PIN_SET(LED_red);
      PIN_CLEAR(LED_green);
      PIN_SET(Pulserelais_off);
      ESEND(192.168.28.1, "Off\");
      state=10;
      c=0;
   END
   ON state==10 DO
      c=c+1;
      ON c==20 DO
         c=0;
         PIN_CLEAR(Pulserelais_on);
         PIN_CLEAR(Pulserelais_off);
         state=100;
      END
   END

   ON state==1 DO
      PIN_SET(LED_green);
      PIN_CLEAR(LED_red);
      PIN_SET(Pulserelais_on);
      ESEND(192.168.28.1, "On\");
      state=11;
      c=0;
   END
   ON state==11 DO
      c=c+1;
      ON c==20 DO
         c=0;
         PIN_CLEAR(Pulserelais_on);
         PIN_CLEAR(Pulserelais_off);
         state=101;
      END
   END

   ON state==2 DO
      PIN_CLEAR(LED_red);
      PIN_CLEAR(LED_green);
      ESEND(192.168.28.1, "Cleaning\");
      state=20;
   END
   ON state==20 DO
      c=c+1;
      ON c==15 DO
         PIN_SET(LED_green);
      END
      ON c==30 DO
         PIN_CLEAR(LED_green);
         c=0;
      END
   END
THREAD_END(states)

ON PIN_FALLING(Key_green) DO
   state=1;
END

ON PIN_FALLING(Key_red) DO
   ON state==101 DO
      state=2;
   END
   ON state==20 DO
      state=0;
   END
END

ON STARTUP DO
   ESEND(192.168.28.1, "Start\");
   state=0;
   THREAD_RESTART(states);
END

CONTROL_END

It is a state machine to control a pulse relais with two coils for a coffeemachine. If you press the green button the machine is switched on and the green LED light up. At the same time the server gets the message "On". If you press the red button it sends a message "Cleaning" to the server and starts to flash the green LED. If you press it again it sends "Off" and switches the relais off. Then the red LED lights up. The server can control the circuit by sending a message like this:

echo "c6 set state 1" | netcat -q1 coffeemachine 2701

To check the status it should send something like:

echo "c6 get state" | netcat -q1 coffeemachine 2701

Is the answer e.g. "state 100" the machine is off. The job is done without timers because there was no space left in the device. With TCP, control6, named pins and rfm12 it nearly takes 16K.