listen serial input and react by callback registered.

Dependents:   Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO

Committer:
aktk
Date:
Tue Nov 12 01:10:29 2019 +0000
Revision:
0:ec916055f0dd
Child:
1:fd211f137803
First Implement;

Who changed what in which revision?

UserRevisionLine numberNew 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 0:ec916055f0dd 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 0:ec916055f0dd 59 callback_onCommand(m_buffer_s.c_str());
aktk 0:ec916055f0dd 60 m_serial_socket->printf("Single key board input: %c (%x)\n", m_buffer_c, m_buffer_c);
aktk 0:ec916055f0dd 61 discardBuffer();
aktk 0:ec916055f0dd 62 }
aktk 0:ec916055f0dd 63
aktk 0:ec916055f0dd 64 bool isUpperCase (const char arg_c)
aktk 0:ec916055f0dd 65 {
aktk 0:ec916055f0dd 66 return ('A' <= arg_c && arg_c <= 'Z');
aktk 0:ec916055f0dd 67 }
aktk 0:ec916055f0dd 68
aktk 0:ec916055f0dd 69 bool isLowerCase (const char arg_c)
aktk 0:ec916055f0dd 70 {
aktk 0:ec916055f0dd 71 return ('a' <= arg_c && arg_c <= 'z');
aktk 0:ec916055f0dd 72 }
aktk 0:ec916055f0dd 73
aktk 0:ec916055f0dd 74 bool isSymbol (const char arg_c)
aktk 0:ec916055f0dd 75 {
aktk 0:ec916055f0dd 76 return ((0x20 <= arg_c && arg_c <= 0x40)
aktk 0:ec916055f0dd 77 || (0x5B <= arg_c && arg_c <= 0x60)
aktk 0:ec916055f0dd 78 || (0x7B <= arg_c && arg_c <= 0x7E));
aktk 0:ec916055f0dd 79 }
aktk 0:ec916055f0dd 80
aktk 0:ec916055f0dd 81 void SerialInputReactionHandler::listenKBTillEnter()
aktk 0:ec916055f0dd 82 {
aktk 0:ec916055f0dd 83 __disable_irq();
aktk 0:ec916055f0dd 84 m_buffer_c = m_serial_socket->getc();
aktk 0:ec916055f0dd 85
aktk 0:ec916055f0dd 86 if ( isUpperCase(m_buffer_c) || isLowerCase(m_buffer_c) || isSymbol( m_buffer_c)) {
aktk 0:ec916055f0dd 87
aktk 0:ec916055f0dd 88 if(m_buffer_s.length() != 0 && m_buffer_s[0] != '\0')
aktk 0:ec916055f0dd 89 m_buffer_s += m_buffer_c;
aktk 0:ec916055f0dd 90 else
aktk 0:ec916055f0dd 91 m_buffer_s = m_buffer_c;
aktk 0:ec916055f0dd 92 m_serial_socket->putc(m_buffer_c);
aktk 0:ec916055f0dd 93
aktk 0:ec916055f0dd 94 } else if ( m_buffer_c == '\n' || m_buffer_c == '\r') {
aktk 0:ec916055f0dd 95
aktk 0:ec916055f0dd 96 callback_onCommand(m_buffer_s.c_str());
aktk 0:ec916055f0dd 97 m_serial_socket->putc(m_buffer_c);
aktk 0:ec916055f0dd 98 discardBuffer();
aktk 0:ec916055f0dd 99
aktk 0:ec916055f0dd 100 } else if (m_buffer_c == 0x1B /*ESC*/) {
aktk 0:ec916055f0dd 101
aktk 0:ec916055f0dd 102 // If only 0x1B, ESC KEY should be Pressed, else some symbol sequence.
aktk 0:ec916055f0dd 103 if (m_serial_socket->readable()) {
aktk 0:ec916055f0dd 104 m_buffer_s += m_buffer_c;
aktk 0:ec916055f0dd 105 m_buffer_c = m_serial_socket->getc();
aktk 0:ec916055f0dd 106
aktk 0:ec916055f0dd 107 // If 0x5B follows, it coul be arrow key was pressed
aktk 0:ec916055f0dd 108 if (m_buffer_c == 0x5B && m_serial_socket->readable()) {
aktk 0:ec916055f0dd 109 m_buffer_s += m_buffer_c;
aktk 0:ec916055f0dd 110 m_buffer_c = m_serial_socket->getc();
aktk 0:ec916055f0dd 111 }
aktk 0:ec916055f0dd 112
aktk 0:ec916055f0dd 113 m_buffer_s += m_buffer_c;
aktk 0:ec916055f0dd 114 // Arrows
aktk 0:ec916055f0dd 115 callback_onCommand(m_buffer_s.c_str());
aktk 0:ec916055f0dd 116 }
aktk 0:ec916055f0dd 117 discardBuffer();
aktk 0:ec916055f0dd 118
aktk 0:ec916055f0dd 119 } else {
aktk 0:ec916055f0dd 120 switch (m_buffer_c) {
aktk 0:ec916055f0dd 121 case 0x08 /*BS */:
aktk 0:ec916055f0dd 122 case 0x7F /*DEL*/:
aktk 0:ec916055f0dd 123 m_buffer_s.erase(m_buffer_s.end() - 1);
aktk 0:ec916055f0dd 124 //m_serial_socket->putc(0x7F);
aktk 0:ec916055f0dd 125 m_serial_socket->putc(0x08);
aktk 0:ec916055f0dd 126 m_serial_socket->putc(0x20);
aktk 0:ec916055f0dd 127 m_serial_socket->putc(0x08);
aktk 0:ec916055f0dd 128 break;
aktk 0:ec916055f0dd 129 case 0x1B /*ESC*/:
aktk 0:ec916055f0dd 130 break;
aktk 0:ec916055f0dd 131
aktk 0:ec916055f0dd 132 }
aktk 0:ec916055f0dd 133 }
aktk 0:ec916055f0dd 134 __enable_irq();
aktk 0:ec916055f0dd 135 }
aktk 0:ec916055f0dd 136
aktk 0:ec916055f0dd 137 void SerialInputReactionHandler::discardBuffer()
aktk 0:ec916055f0dd 138 {
aktk 0:ec916055f0dd 139 m_buffer_c = '\0';
aktk 0:ec916055f0dd 140 m_buffer_s = "";
aktk 0:ec916055f0dd 141 }