listen serial input and react by callback registered.
Dependents: Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO
SerialInputReactionHandler.cpp@1:fd211f137803, 2019-11-12 (annotated)
- Committer:
- aktk
- Date:
- Tue Nov 12 02:41:58 2019 +0000
- Revision:
- 1:fd211f137803
- Parent:
- 0:ec916055f0dd
- Child:
- 2:4718a4eaf340
make the process that confirms arrow symbols a function and applied it to listenKBSingleInput();
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aktk | 0:ec916055f0dd | 1 | #include "SerialInputReactionHandler.h" |
aktk | 0:ec916055f0dd | 2 | |
aktk | 0:ec916055f0dd | 3 | char const * const SerialInputReactionHandler::ARROW_UP = "\x1b\x5b\x41"; |
aktk | 0:ec916055f0dd | 4 | char const * const SerialInputReactionHandler::ARROW_DOWN = "\x1b\x5b\x42"; |
aktk | 0:ec916055f0dd | 5 | char const * const SerialInputReactionHandler::ARROW_RIGHT= "\x1b\x5b\x43"; |
aktk | 0:ec916055f0dd | 6 | char const * const SerialInputReactionHandler::ARROW_LEFT = "\x1b\x5b\x44"; |
aktk | 0:ec916055f0dd | 7 | |
aktk | 0:ec916055f0dd | 8 | SerialInputReactionHandler::SerialInputReactionHandler( |
aktk | 0:ec916055f0dd | 9 | Callback<int(char const * const)> arg_callback_onCommand |
aktk | 0:ec916055f0dd | 10 | ) |
aktk | 0:ec916055f0dd | 11 | { |
aktk | 0:ec916055f0dd | 12 | funcIfInput[KB_SINGLE_INPUT] = &SerialInputReactionHandler::listenKBSingleInput; |
aktk | 0:ec916055f0dd | 13 | funcIfInput[KB_TILL_ENTER] = &SerialInputReactionHandler::listenKBTillEnter; |
aktk | 0:ec916055f0dd | 14 | callback_onCommand = arg_callback_onCommand; |
aktk | 0:ec916055f0dd | 15 | } |
aktk | 0:ec916055f0dd | 16 | |
aktk | 0:ec916055f0dd | 17 | void SerialInputReactionHandler::attach( |
aktk | 0:ec916055f0dd | 18 | Callback<int (char const * const)> arg_callback_onCommand |
aktk | 0:ec916055f0dd | 19 | ) |
aktk | 0:ec916055f0dd | 20 | { |
aktk | 0:ec916055f0dd | 21 | callback_onCommand = arg_callback_onCommand; |
aktk | 0:ec916055f0dd | 22 | } |
aktk | 0:ec916055f0dd | 23 | |
aktk | 0:ec916055f0dd | 24 | void SerialInputReactionHandler::startReception( |
aktk | 0:ec916055f0dd | 25 | Serial * arg_serial_socket, |
aktk | 0:ec916055f0dd | 26 | InputMode arg_mode |
aktk | 0:ec916055f0dd | 27 | ) |
aktk | 0:ec916055f0dd | 28 | { |
aktk | 0:ec916055f0dd | 29 | m_serial_socket = arg_serial_socket; |
aktk | 0:ec916055f0dd | 30 | m_input_mode = arg_mode; |
aktk | 0:ec916055f0dd | 31 | m_serial_socket->attach(callback(this, &SerialInputReactionHandler::sig_bind)); |
aktk | 0:ec916055f0dd | 32 | } |
aktk | 0:ec916055f0dd | 33 | |
aktk | 0:ec916055f0dd | 34 | void SerialInputReactionHandler::quit() |
aktk | 0:ec916055f0dd | 35 | { |
aktk | 0:ec916055f0dd | 36 | m_serial_socket->attach(NULL); |
aktk | 1:fd211f137803 | 37 | } |
aktk | 0:ec916055f0dd | 38 | |
aktk | 0:ec916055f0dd | 39 | void SerialInputReactionHandler::commonProcedure() |
aktk | 0:ec916055f0dd | 40 | { |
aktk | 0:ec916055f0dd | 41 | } |
aktk | 0:ec916055f0dd | 42 | |
aktk | 0:ec916055f0dd | 43 | void SerialInputReactionHandler::sig_bind() |
aktk | 0:ec916055f0dd | 44 | { |
aktk | 0:ec916055f0dd | 45 | commonProcedure(); |
aktk | 0:ec916055f0dd | 46 | (this->*funcIfInput[m_input_mode])(); |
aktk | 0:ec916055f0dd | 47 | } |
aktk | 0:ec916055f0dd | 48 | |
aktk | 0:ec916055f0dd | 49 | void SerialInputReactionHandler::changeMode(InputMode arg_mode) |
aktk | 0:ec916055f0dd | 50 | { |
aktk | 0:ec916055f0dd | 51 | discardBuffer(); |
aktk | 0:ec916055f0dd | 52 | m_input_mode = arg_mode; |
aktk | 0:ec916055f0dd | 53 | } |
aktk | 0:ec916055f0dd | 54 | |
aktk | 0:ec916055f0dd | 55 | void SerialInputReactionHandler::listenKBSingleInput() |
aktk | 0:ec916055f0dd | 56 | { |
aktk | 0:ec916055f0dd | 57 | m_buffer_c = m_serial_socket->getc(); |
aktk | 0:ec916055f0dd | 58 | m_buffer_s = m_buffer_c; |
aktk | 1:fd211f137803 | 59 | if(m_buffer_c != 0x1B && isArrowSymbol()) |
aktk | 1:fd211f137803 | 60 | callback_onCommand(m_buffer_s.c_str()); |
aktk | 0:ec916055f0dd | 61 | m_serial_socket->printf("Single key board input: %c (%x)\n", m_buffer_c, m_buffer_c); |
aktk | 0:ec916055f0dd | 62 | discardBuffer(); |
aktk | 0:ec916055f0dd | 63 | } |
aktk | 0:ec916055f0dd | 64 | |
aktk | 0:ec916055f0dd | 65 | bool isUpperCase (const char arg_c) |
aktk | 0:ec916055f0dd | 66 | { |
aktk | 0:ec916055f0dd | 67 | return ('A' <= arg_c && arg_c <= 'Z'); |
aktk | 0:ec916055f0dd | 68 | } |
aktk | 0:ec916055f0dd | 69 | |
aktk | 0:ec916055f0dd | 70 | bool isLowerCase (const char arg_c) |
aktk | 0:ec916055f0dd | 71 | { |
aktk | 0:ec916055f0dd | 72 | return ('a' <= arg_c && arg_c <= 'z'); |
aktk | 0:ec916055f0dd | 73 | } |
aktk | 0:ec916055f0dd | 74 | |
aktk | 0:ec916055f0dd | 75 | bool isSymbol (const char arg_c) |
aktk | 0:ec916055f0dd | 76 | { |
aktk | 0:ec916055f0dd | 77 | return ((0x20 <= arg_c && arg_c <= 0x40) |
aktk | 0:ec916055f0dd | 78 | || (0x5B <= arg_c && arg_c <= 0x60) |
aktk | 0:ec916055f0dd | 79 | || (0x7B <= arg_c && arg_c <= 0x7E)); |
aktk | 0:ec916055f0dd | 80 | } |
aktk | 0:ec916055f0dd | 81 | |
aktk | 1:fd211f137803 | 82 | bool SerialInputReactionHandler::isArrowSymbol() |
aktk | 1:fd211f137803 | 83 | { |
aktk | 1:fd211f137803 | 84 | // If only 0x1B, ESC KEY should be Pressed, else some symbol sequence. |
aktk | 1:fd211f137803 | 85 | if (m_buffer_c == 0x1B && m_serial_socket->readable()) { |
aktk | 1:fd211f137803 | 86 | m_buffer_s = m_buffer_c; |
aktk | 1:fd211f137803 | 87 | m_buffer_c = m_serial_socket->getc(); |
aktk | 1:fd211f137803 | 88 | |
aktk | 1:fd211f137803 | 89 | // If 0x5B follows, it coul be arrow key was pressed |
aktk | 1:fd211f137803 | 90 | if (m_buffer_c == 0x5B && m_serial_socket->readable()) { |
aktk | 1:fd211f137803 | 91 | m_buffer_s += m_buffer_c; |
aktk | 1:fd211f137803 | 92 | m_buffer_c = m_serial_socket->getc(); |
aktk | 1:fd211f137803 | 93 | } else m_buffer_c = 0x1B; |
aktk | 1:fd211f137803 | 94 | |
aktk | 1:fd211f137803 | 95 | m_buffer_s += m_buffer_c; |
aktk | 1:fd211f137803 | 96 | } else m_buffer_c = 0x1B; |
aktk | 1:fd211f137803 | 97 | |
aktk | 1:fd211f137803 | 98 | if (m_buffer_c == 0x1B) return false; |
aktk | 1:fd211f137803 | 99 | else return true; |
aktk | 1:fd211f137803 | 100 | } |
aktk | 1:fd211f137803 | 101 | |
aktk | 1:fd211f137803 | 102 | |
aktk | 0:ec916055f0dd | 103 | void SerialInputReactionHandler::listenKBTillEnter() |
aktk | 0:ec916055f0dd | 104 | { |
aktk | 0:ec916055f0dd | 105 | __disable_irq(); |
aktk | 0:ec916055f0dd | 106 | m_buffer_c = m_serial_socket->getc(); |
aktk | 1:fd211f137803 | 107 | |
aktk | 0:ec916055f0dd | 108 | if ( isUpperCase(m_buffer_c) || isLowerCase(m_buffer_c) || isSymbol( m_buffer_c)) { |
aktk | 0:ec916055f0dd | 109 | |
aktk | 0:ec916055f0dd | 110 | if(m_buffer_s.length() != 0 && m_buffer_s[0] != '\0') |
aktk | 0:ec916055f0dd | 111 | m_buffer_s += m_buffer_c; |
aktk | 0:ec916055f0dd | 112 | else |
aktk | 0:ec916055f0dd | 113 | m_buffer_s = m_buffer_c; |
aktk | 0:ec916055f0dd | 114 | m_serial_socket->putc(m_buffer_c); |
aktk | 0:ec916055f0dd | 115 | |
aktk | 0:ec916055f0dd | 116 | } else if ( m_buffer_c == '\n' || m_buffer_c == '\r') { |
aktk | 0:ec916055f0dd | 117 | |
aktk | 0:ec916055f0dd | 118 | callback_onCommand(m_buffer_s.c_str()); |
aktk | 0:ec916055f0dd | 119 | m_serial_socket->putc(m_buffer_c); |
aktk | 0:ec916055f0dd | 120 | discardBuffer(); |
aktk | 0:ec916055f0dd | 121 | |
aktk | 1:fd211f137803 | 122 | } else if (isArrowSymbol()) { |
aktk | 0:ec916055f0dd | 123 | |
aktk | 1:fd211f137803 | 124 | // Arrows |
aktk | 1:fd211f137803 | 125 | callback_onCommand(m_buffer_s.c_str()); |
aktk | 0:ec916055f0dd | 126 | discardBuffer(); |
aktk | 0:ec916055f0dd | 127 | |
aktk | 0:ec916055f0dd | 128 | } else { |
aktk | 0:ec916055f0dd | 129 | switch (m_buffer_c) { |
aktk | 0:ec916055f0dd | 130 | case 0x08 /*BS */: |
aktk | 0:ec916055f0dd | 131 | case 0x7F /*DEL*/: |
aktk | 0:ec916055f0dd | 132 | m_buffer_s.erase(m_buffer_s.end() - 1); |
aktk | 0:ec916055f0dd | 133 | //m_serial_socket->putc(0x7F); |
aktk | 0:ec916055f0dd | 134 | m_serial_socket->putc(0x08); |
aktk | 0:ec916055f0dd | 135 | m_serial_socket->putc(0x20); |
aktk | 0:ec916055f0dd | 136 | m_serial_socket->putc(0x08); |
aktk | 0:ec916055f0dd | 137 | break; |
aktk | 0:ec916055f0dd | 138 | case 0x1B /*ESC*/: |
aktk | 1:fd211f137803 | 139 | discardBuffer(); |
aktk | 0:ec916055f0dd | 140 | break; |
aktk | 0:ec916055f0dd | 141 | |
aktk | 0:ec916055f0dd | 142 | } |
aktk | 0:ec916055f0dd | 143 | } |
aktk | 0:ec916055f0dd | 144 | __enable_irq(); |
aktk | 0:ec916055f0dd | 145 | } |