blink.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <Automaton.h>
  2. class Atm_blink : public Machine {
  3. public:
  4. Atm_blink( void ) : Machine() {};
  5. short pin;
  6. atm_timer_millis timer;
  7. enum { IDLE, LED_ON, LED_OFF }; // STATES
  8. enum { EVT_TIMER, EVT_ON, EVT_OFF, ELSE }; // EVENTS
  9. enum { ENT_ON, ENT_OFF }; // ACTIONS
  10. Atm_blink & begin( int attached_pin, uint32_t blinkrate ) {
  11. const static state_t state_table[] PROGMEM = {
  12. /* ON_ENTER ON_LOOP ON_EXIT EVT_TIMER EVT_ON EVT_OFF ELSE */
  13. /* IDLE */ ENT_OFF, -1, -1, -1, LED_ON, -1, -1,
  14. /* LED_ON */ ENT_ON, -1, -1, LED_OFF, -1, IDLE, -1,
  15. /* LED_OFF */ ENT_OFF, -1, -1, LED_ON, -1, IDLE, -1,
  16. };
  17. Machine::begin( state_table, ELSE );
  18. pin = attached_pin;
  19. timer.set( blinkrate );
  20. pinMode( pin, OUTPUT );
  21. return *this;
  22. }
  23. int event( int id ) {
  24. switch ( id ) {
  25. case EVT_TIMER :
  26. return timer.expired( this );
  27. }
  28. return 0;
  29. }
  30. void action( int id ) {
  31. switch ( id ) {
  32. case ENT_ON :
  33. digitalWrite( pin, HIGH );
  34. return;
  35. case ENT_OFF :
  36. digitalWrite( pin, LOW );
  37. return;
  38. }
  39. }
  40. };
  41. Atm_blink led;
  42. void setup() {
  43. led.begin( 4, 200 ); // Setup a blink machine on pin 4
  44. led.trigger( led.EVT_ON ); // Turn it on
  45. }
  46. void loop() {
  47. led.cycle();
  48. }