Atm_digital.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "Atm_digital.hpp"
  2. Atm_digital& Atm_digital::begin( int pin, int debounce /* = 20 */, bool activeLow /* = false */, bool pullUp /* = false */ ) {
  3. // clang-format off
  4. const static state_t state_table[] PROGMEM = {
  5. /* ON_ENTER ON_LOOP ON_EXIT EVT_TIMER EVT_HIGH EVT_LOW ELSE */
  6. /* IDLE */ -1, -1, -1, -1, WAITH, -1, -1,
  7. /* WAITH */ -1, -1, -1, VHIGH, -1, IDLE, -1,
  8. /* VHIGH */ ENT_HIGH, -1, -1, -1, -1, WAITL, -1,
  9. /* WAITL */ -1, -1, -1, VLOW, VHIGH, -1, -1,
  10. /* VLOW */ ENT_LOW, -1, -1, -1, -1, -1, IDLE,
  11. };
  12. // clang-format on
  13. Machine::begin( state_table, ELSE );
  14. this->pin = pin;
  15. this->activeLow = activeLow;
  16. timer.set( debounce );
  17. indicator = -1;
  18. pinMode( pin, pullUp ? INPUT_PULLUP : INPUT );
  19. return *this;
  20. }
  21. int Atm_digital::event( int id ) {
  22. switch ( id ) {
  23. case EVT_TIMER:
  24. return timer.expired( this );
  25. case EVT_HIGH:
  26. return ( !digitalRead( pin ) != !activeLow ); // XOR
  27. case EVT_LOW:
  28. return !( !digitalRead( pin ) != !activeLow );
  29. }
  30. return 0;
  31. }
  32. void Atm_digital::action( int id ) {
  33. switch ( id ) {
  34. case ENT_HIGH:
  35. connection[ON_CHANGE_TRUE].push( state() );
  36. if ( indicator > -1 ) digitalWrite( indicator, !HIGH != !indicatorActiveLow );
  37. return;
  38. case ENT_LOW:
  39. connection[ON_CHANGE_FALSE].push( state() );
  40. if ( indicator > -1 ) digitalWrite( indicator, !LOW != !indicatorActiveLow );
  41. return;
  42. }
  43. }
  44. int Atm_digital::state( void ) {
  45. return ( current == VHIGH || current == WAITL );
  46. }
  47. Atm_digital& Atm_digital::led( int led, bool activeLow /* = false */ ) {
  48. indicator = led;
  49. indicatorActiveLow = activeLow;
  50. pinMode( indicator, OUTPUT );
  51. return *this;
  52. }
  53. Atm_digital& Atm_digital::onChange( bool status, atm_cb_push_t callback, int idx /* = 0 */ ) {
  54. connection[status ? ON_CHANGE_TRUE : ON_CHANGE_FALSE].set( callback, idx );
  55. return *this;
  56. }
  57. Atm_digital& Atm_digital::onChange( bool status, Machine& machine, int event /* = 0 */ ) {
  58. connection[status ? ON_CHANGE_TRUE : ON_CHANGE_FALSE].set( &machine, event );
  59. return *this;
  60. }
  61. Atm_digital& Atm_digital::onChange( atm_cb_push_t callback, int idx /* = 0 */ ) {
  62. connection[ON_CHANGE_FALSE].set( callback, idx );
  63. connection[ON_CHANGE_TRUE].set( callback, idx );
  64. return *this;
  65. }
  66. Atm_digital& Atm_digital::onChange( Machine& machine, int event /* = 0 */ ) {
  67. connection[ON_CHANGE_FALSE].set( &machine, event );
  68. connection[ON_CHANGE_TRUE].set( &machine, event );
  69. return *this;
  70. }
  71. Atm_digital& Atm_digital::trace( Stream& stream ) {
  72. setTrace( &stream, atm_serial_debug::trace, "DIGITAL\0EVT_TIMER\0EVT_HIGH\0EVT_LOW\0ELSE\0IDLE\0WAITH\0VHIGH\0WAITL\0VLOW" );
  73. return *this;
  74. }