listen serial input and react by callback registered.

Dependents:   Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO

Committer:
aktk
Date:
Thu Nov 14 01:52:06 2019 +0000
Revision:
2:4718a4eaf340
Parent:
1:fd211f137803
Child:
3:599073f82114
make the return type of callback on command (void *) and added pre/post procedure;

Who changed what in which revision?

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