Atm_command.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "Atm_command.hpp"
  2. Atm_command& Atm_command::begin( Stream& stream, char buffer[], int size ) {
  3. // clang-format off
  4. const static state_t state_table[] PROGMEM = {
  5. /* ON_ENTER ON_LOOP ON_EXIT EVT_INPUT EVT_EOL ELSE */
  6. /* IDLE */ -1, -1, -1, READCHAR, -1, -1,
  7. /* READCHAR */ ENT_READCHAR, -1, -1, READCHAR, SEND, -1,
  8. /* SEND */ ENT_SEND, -1, -1, -1, -1, IDLE,
  9. };
  10. // clang-format on
  11. Machine::begin( state_table, ELSE );
  12. this->stream = &stream;
  13. this->buffer = buffer;
  14. bufsize = size;
  15. bufptr = 0;
  16. separatorChar = " ";
  17. lastch = '\0';
  18. return *this;
  19. }
  20. int Atm_command::event( int id ) {
  21. switch ( id ) {
  22. case EVT_INPUT:
  23. return stream->available();
  24. case EVT_EOL:
  25. return buffer[bufptr - 1] == '\n' || buffer[bufptr - 1] == '\r' || bufptr >= bufsize;
  26. }
  27. return 0;
  28. }
  29. void Atm_command::action( int id ) {
  30. switch ( id ) {
  31. case ENT_READCHAR:
  32. if ( stream->available() ) {
  33. char ch = stream->read();
  34. if ( strchr( separatorChar, ch ) == NULL ) {
  35. buffer[bufptr++] = ch;
  36. lastch = ch;
  37. } else {
  38. if ( lastch != '\0' ) buffer[bufptr++] = '\0';
  39. lastch = '\0';
  40. }
  41. }
  42. return;
  43. case ENT_SEND:
  44. buffer[--bufptr] = '\0';
  45. oncommand.push( lookup( 0, commands ) );
  46. lastch = '\0';
  47. bufptr = 0;
  48. return;
  49. }
  50. }
  51. Atm_command& Atm_command::onCommand( atm_cb_push_t callback, int idx /* = 0 */ ) {
  52. oncommand.set( callback, idx );
  53. return *this;
  54. }
  55. Atm_command& Atm_command::list( const char* cmds ) {
  56. commands = cmds;
  57. return *this;
  58. }
  59. Atm_command& Atm_command::separator( const char sep[] ) {
  60. separatorChar = sep;
  61. return *this;
  62. }
  63. char* Atm_command::arg( int id ) {
  64. int cnt = 0;
  65. int i;
  66. if ( id == 0 ) return buffer;
  67. for ( i = 0; i < bufptr; i++ ) {
  68. if ( buffer[i] == '\0' ) {
  69. if ( ++cnt == id ) {
  70. i++;
  71. break;
  72. }
  73. }
  74. }
  75. return &buffer[i];
  76. }
  77. int Atm_command::lookup( int id, const char* cmdlist ) {
  78. int cnt = 0;
  79. char* arg = this->arg( id );
  80. char* a = arg;
  81. while ( cmdlist[0] != '\0' ) {
  82. while ( cmdlist[0] != '\0' && toupper( cmdlist[0] ) == toupper( a[0] ) ) {
  83. cmdlist++;
  84. a++;
  85. }
  86. if ( a[0] == '\0' && ( cmdlist[0] == ' ' || cmdlist[0] == '\0' ) ) return cnt;
  87. while ( cmdlist[0] != ' ' && cmdlist[0] != '\0' ) cmdlist++;
  88. cmdlist++;
  89. a = arg;
  90. cnt++;
  91. }
  92. return -1;
  93. }
  94. Atm_command& Atm_command::trace( Stream& stream ) {
  95. setTrace( &stream, atm_serial_debug::trace, "COMMAND\0EVT_INPUT\0EVT_EOL\0ELSE\0IDLE\0READCHAR\0SEND" );
  96. return *this;
  97. }