listen serial input and react by callback registered.

Dependents:   Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO

Revision:
1:fd211f137803
Parent:
0:ec916055f0dd
Child:
2:4718a4eaf340
diff -r ec916055f0dd -r fd211f137803 SerialInputReactionHandler.cpp
--- a/SerialInputReactionHandler.cpp	Tue Nov 12 01:10:29 2019 +0000
+++ b/SerialInputReactionHandler.cpp	Tue Nov 12 02:41:58 2019 +0000
@@ -34,7 +34,7 @@
 void SerialInputReactionHandler::quit()
 {
     m_serial_socket->attach(NULL);
-}    
+}
 
 void SerialInputReactionHandler::commonProcedure()
 {
@@ -56,7 +56,8 @@
 {
     m_buffer_c = m_serial_socket->getc();
     m_buffer_s = m_buffer_c;
-    callback_onCommand(m_buffer_s.c_str());
+    if(m_buffer_c != 0x1B && isArrowSymbol())
+        callback_onCommand(m_buffer_s.c_str());
     m_serial_socket->printf("Single key board input: %c (%x)\n", m_buffer_c, m_buffer_c);
     discardBuffer();
 }
@@ -78,11 +79,32 @@
             ||  (0x7B <= arg_c && arg_c <= 0x7E));
 }
 
+bool SerialInputReactionHandler::isArrowSymbol()
+{
+    //  If only 0x1B, ESC KEY should be Pressed, else some symbol sequence.
+    if (m_buffer_c == 0x1B && m_serial_socket->readable()) {
+        m_buffer_s = m_buffer_c;
+        m_buffer_c = m_serial_socket->getc();
+
+        //  If 0x5B follows, it coul be arrow key was pressed
+        if (m_buffer_c == 0x5B && m_serial_socket->readable()) {
+            m_buffer_s += m_buffer_c;
+            m_buffer_c = m_serial_socket->getc();
+        } else m_buffer_c = 0x1B;
+
+        m_buffer_s += m_buffer_c;
+    } else m_buffer_c = 0x1B;
+
+    if (m_buffer_c == 0x1B) return false;
+    else return true;
+}
+
+
 void SerialInputReactionHandler::listenKBTillEnter()
 {
     __disable_irq();
     m_buffer_c = m_serial_socket->getc();
-    
+
     if ( isUpperCase(m_buffer_c) || isLowerCase(m_buffer_c) || isSymbol( m_buffer_c)) {
 
         if(m_buffer_s.length() != 0 && m_buffer_s[0] != '\0')
@@ -97,23 +119,10 @@
         m_serial_socket->putc(m_buffer_c);
         discardBuffer();
 
-    } else if (m_buffer_c == 0x1B /*ESC*/) {
-
-        //  If only 0x1B, ESC KEY should be Pressed, else some symbol sequence.
-        if (m_serial_socket->readable()) {
-            m_buffer_s += m_buffer_c;
-            m_buffer_c = m_serial_socket->getc();
+    } else if (isArrowSymbol()) {
 
-            //  If 0x5B follows, it coul be arrow key was pressed 
-            if (m_buffer_c == 0x5B && m_serial_socket->readable()) {
-                m_buffer_s += m_buffer_c;
-                m_buffer_c = m_serial_socket->getc();
-            }
-
-            m_buffer_s += m_buffer_c;
-            //  Arrows
-            callback_onCommand(m_buffer_s.c_str());
-        }
+        //  Arrows
+        callback_onCommand(m_buffer_s.c_str());
         discardBuffer();
 
     } else {
@@ -127,15 +136,10 @@
                 m_serial_socket->putc(0x08);
                 break;
             case 0x1B /*ESC*/:
+                discardBuffer();
                 break;
 
         }
     }
     __enable_irq();
 }
-
-void SerialInputReactionHandler::discardBuffer()
-{
-    m_buffer_c = '\0';
-    m_buffer_s = "";
-}