Atm_button.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "Atm_button.hpp"
  2. // Add option for button press callback (for reading i2c buttons etc)
  3. Atm_button& Atm_button::begin( int attached_pin ) {
  4. // clang-format off
  5. const static state_t state_table[] PROGMEM = {
  6. /* Standard Mode: press/repeat */
  7. /* ON_ENTER ON_LOOP ON_EXIT EVT_LMODE EVT_TIMER EVT_DELAY EVT_REPEAT EVT_PRESS EVT_RELEASE EVT_COUNTER EVT_AUTO ELSE */
  8. /* IDLE */ -1, -1, -1, LIDLE, -1, -1, -1, WAIT, -1, -1, AUTO_ST, -1,
  9. /* WAIT */ -1, -1, -1, -1, PRESSED, -1, -1, -1, IDLE, -1, -1, -1,
  10. /* PRESSED */ ENT_PRESS, -1, -1, -1, -1, REPEAT, -1, -1, RELEASE, -1, -1, -1,
  11. /* REPEAT */ ENT_PRESS, -1, -1, -1, -1, -1, REPEAT, -1, RELEASE, -1, -1, -1,
  12. /* RELEASE */ ENT_RELEASE, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, IDLE,
  13. /* Long Press Mode: press/long press */
  14. /* LIDLE */ -1, -1, -1, -1, -1, -1, -1, LWAIT, -1, -1, -1, -1,
  15. /* LWAIT */ ENT_LSTART, -1, -1, -1, LPRESSED, -1, -1, -1, LIDLE, -1, -1, -1,
  16. /* LPRESSED */ ENT_LCOUNT, -1, -1, -1, -1, LPRESSED, -1, -1, LRELEASE, WRELEASE, -1, -1,
  17. /* LRELEASE */ ENT_LRELEASE, -1, EXT_WRELEASE, -1, -1, -1, -1, -1, -1, -1, -1, LIDLE,
  18. /* WRELEASE */ ENT_LRELEASE, -1, EXT_WRELEASE, -1, -1, -1, -1, -1, LIDLE, -1, -1, -1,
  19. /* AUTO_ST */ ENT_AUTO, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, IDLE,
  20. };
  21. // clang-format on
  22. Machine::begin( state_table, ELSE );
  23. pin = attached_pin;
  24. counter_longpress.set( 0 );
  25. timer_debounce.set( DEBOUNCE );
  26. timer_delay.set( ATM_TIMER_OFF );
  27. timer_repeat.set( ATM_TIMER_OFF );
  28. timer_auto.set( ATM_TIMER_OFF );
  29. pinMode( pin, INPUT_PULLUP );
  30. return *this;
  31. }
  32. int Atm_button::event( int id ) {
  33. switch ( id ) {
  34. case EVT_LMODE:
  35. return counter_longpress.value > 0;
  36. case EVT_TIMER:
  37. return timer_debounce.expired( this );
  38. case EVT_DELAY:
  39. return timer_delay.expired( this );
  40. case EVT_REPEAT:
  41. return timer_repeat.expired( this );
  42. case EVT_AUTO:
  43. return timer_auto.expired( this );
  44. case EVT_PRESS:
  45. return !digitalRead( pin );
  46. case EVT_RELEASE:
  47. return digitalRead( pin );
  48. case EVT_COUNTER:
  49. return counter_longpress.expired();
  50. }
  51. return 0;
  52. }
  53. void Atm_button::action( int id ) {
  54. int press;
  55. switch ( id ) {
  56. case ENT_PRESS:
  57. onpress.push( auto_press );
  58. longpress[0].push( 1 );
  59. return;
  60. case ENT_AUTO:
  61. onpress.push( 1 );
  62. longpress[0].push( 1 );
  63. return;
  64. case ENT_RELEASE:
  65. case EXT_WRELEASE:
  66. onrelease.push( 0 );
  67. return;
  68. case ENT_LSTART:
  69. counter_longpress.set( longpress_max );
  70. return;
  71. case ENT_LCOUNT:
  72. counter_longpress.decrement();
  73. press = ( longpress_max - counter_longpress.value );
  74. if ( onpress.mode() == atm_connector::MODE_PUSHCB ) {
  75. onpress.push( press * -1 );
  76. }
  77. return;
  78. case ENT_LRELEASE:
  79. press = ( longpress_max - counter_longpress.value );
  80. onpress.push( press );
  81. if ( press == 1 || press == 2 ) {
  82. longpress[press-1].push( press );
  83. }
  84. return;
  85. }
  86. }
  87. Atm_button& Atm_button::onPress( atm_cb_push_t callback, int idx /* = 0 */ ) {
  88. onpress.set( callback, idx );
  89. return *this;
  90. }
  91. Atm_button& Atm_button::onPress( Machine& machine, int event /* = 0 */ ) {
  92. onpress.set( &machine, event );
  93. return *this;
  94. }
  95. Atm_button& Atm_button::onPress( int id, atm_cb_push_t callback, int idx /* = 0 */ ) {
  96. if ( id == 1 || id == 2 )
  97. longpress[id-1].set( callback, idx );
  98. return *this;
  99. }
  100. Atm_button& Atm_button::onPress( int id, Machine& machine, int event /* = 0 */ ) {
  101. if ( id == 1 || id == 2 )
  102. longpress[id-1].set( &machine, event );
  103. return *this;
  104. }
  105. Atm_button& Atm_button::onRelease( atm_cb_push_t callback, int idx /* = 0 */ ) {
  106. onrelease.set( callback, idx );
  107. return *this;
  108. }
  109. Atm_button& Atm_button::onRelease( Machine& machine, int event /* = 0 */ ) {
  110. onrelease.set( &machine, event );
  111. return *this;
  112. }
  113. Atm_button& Atm_button::debounce( int delay ) {
  114. timer_debounce.set( delay );
  115. return *this;
  116. }
  117. Atm_button& Atm_button::longPress( int max, int delay ) {
  118. longpress_max = max;
  119. counter_longpress.set( longpress_max );
  120. timer_delay.set( delay );
  121. return *this;
  122. }
  123. Atm_button& Atm_button::repeat( int delay /* = 500 */, int speed /* = 50 */ ) {
  124. timer_delay.set( delay );
  125. timer_repeat.set( speed );
  126. return *this;
  127. }
  128. Atm_button& Atm_button::autoPress( int delay, int press /* = 1 */ ) {
  129. auto_press = press;
  130. timer_auto.set( delay );
  131. return *this;
  132. }
  133. Atm_button& Atm_button::trace( Stream& stream ) {
  134. setTrace( &stream, atm_serial_debug::trace,
  135. "BUTTON\0EVT_LMODE\0EVT_TIMER\0EVT_DELAY\0EVT_REPEAT\0EVT_PRESS\0EVT_RELEASE\0EVT_COUNTER\0EVT_"
  136. "AUTO_ST\0ELSE\0IDLE\0WAIT\0PRESSED\0REPEAT\0RELEASE\0LIDLE\0LWAIT\0LPRESSED\0LRELEASE\0WRELEASE\0AUTO" );
  137. return *this;
  138. }