listen serial input and react by callback registered.

Dependents:   Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO

Files at this revision

API Documentation at this revision

Comitter:
aktk
Date:
Wed Nov 27 23:35:41 2019 +0000
Parent:
2:4718a4eaf340
Commit message:
nyaan nyaan

Changed in this revision

SerialInputReactionHandler.cpp Show annotated file Show diff for this revision Revisions of this file
SerialInputReactionHandler.h Show annotated file Show diff for this revision Revisions of this file
diff -r 4718a4eaf340 -r 599073f82114 SerialInputReactionHandler.cpp
--- a/SerialInputReactionHandler.cpp	Thu Nov 14 01:52:06 2019 +0000
+++ b/SerialInputReactionHandler.cpp	Wed Nov 27 23:35:41 2019 +0000
@@ -1,49 +1,47 @@
 #include "SerialInputReactionHandler.h"
 
-//void SerialInputReactionHandler::doNothing(char const * const){}
-//void SerialInputReactionHandler::doNothing(void *){}
-//template <typename T>
-//void SerialInputReactionHandler::doNothing(T arg) {}
-
-char const * const SerialInputReactionHandler::ARROW_UP   = "\x1b\x5b\x41";
-char const * const SerialInputReactionHandler::ARROW_DOWN = "\x1b\x5b\x42";
-char const * const SerialInputReactionHandler::ARROW_RIGHT= "\x1b\x5b\x43";
-char const * const SerialInputReactionHandler::ARROW_LEFT = "\x1b\x5b\x44";
-
 SerialInputReactionHandler::SerialInputReactionHandler(
     Callback<void *(char const * const)> arg_callback_onCommand,
-    Callback<void (char const * const)> arg_commonPre_callback_onCommand,
-    Callback<void (void *)> arg_commonPost_callback_onCommand
+    Callback<void (char const * const)> arg_Pre_callback_onCommand,
+    Callback<void (char const * const, void *)> arg_Post_callback_onCommand
 )
 {
     funcIfInput[KB_SINGLE_INPUT] = &SerialInputReactionHandler::listenKBSingleInput;
     funcIfInput[KB_TILL_ENTER] = &SerialInputReactionHandler::listenKBTillEnter;
-    callback_onCommand = arg_callback_onCommand;
-    commonPre_callback_onCommand = arg_commonPre_callback_onCommand;
-    commonPost_callback_onCommand = arg_commonPost_callback_onCommand;
+    m_callback_onCommand = arg_callback_onCommand;
+    m_Pre_callback_onCommand = arg_Pre_callback_onCommand;
+    m_Post_callback_onCommand = arg_Post_callback_onCommand;
 }
 
+
+
 void SerialInputReactionHandler::attach(
     Callback<void * (char const * const)> arg_callback_onCommand
 )
 {
-    callback_onCommand = arg_callback_onCommand;
+    m_callback_onCommand = arg_callback_onCommand;
 }
 
+
+
 void SerialInputReactionHandler::attach_PreProc(
-    Callback<void (char const * const)> arg_commonPre_callback_onCommand
+    Callback<void (char const * const)> arg_Pre_callback_onCommand
 )
 {
-    commonPre_callback_onCommand = arg_commonPre_callback_onCommand;
+    m_Pre_callback_onCommand = arg_Pre_callback_onCommand;
 }
 
+
+
 void SerialInputReactionHandler::attach_PostProc(
-    Callback<void (void *)> arg_commonPost_callback_onCommand
+    Callback<void (char const * const, void *)> arg_Post_callback_onCommand
 )
 {
-    commonPost_callback_onCommand = arg_commonPost_callback_onCommand;
+    m_Post_callback_onCommand = arg_Post_callback_onCommand;
 }
 
+
+
 void SerialInputReactionHandler::startReception(
     Serial * arg_serial_socket,
     InputMode arg_mode
@@ -54,32 +52,51 @@
     m_serial_socket->attach(callback(this, &SerialInputReactionHandler::sig_bind));
 }
 
+
+
 void SerialInputReactionHandler::quit()
 {
     m_serial_socket->attach(NULL);
 }
 
+
+
 void SerialInputReactionHandler::sig_bind()
 {
     (this->*funcIfInput[m_input_mode])();
 }
 
+
+
 void SerialInputReactionHandler::changeMode(InputMode arg_mode)
 {
     discardBuffer();
     m_input_mode = arg_mode;
 }
 
+
+
+void SerialInputReactionHandler::callback_onCommand(char const * const arg_command)
+{
+    void * l_rescallback;
+    m_Pre_callback_onCommand(arg_command);
+    l_rescallback = m_callback_onCommand(arg_command);
+    m_Post_callback_onCommand(arg_command, l_rescallback);
+}
+
+
+
 void SerialInputReactionHandler::listenKBSingleInput()
 {
     m_buffer_c = m_serial_socket->getc();
     m_buffer_s = m_buffer_c;
-    if(m_buffer_c != 0x1B && isArrowSymbol())
+    if(m_buffer_c != 0x1B || 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();
 }
 
+
+
 bool SerialInputReactionHandler::isArrowSymbol()
 {
     //  If only 0x1B, ESC KEY should be Pressed, else some symbol sequence.
@@ -101,6 +118,7 @@
 }
 
 
+
 void SerialInputReactionHandler::listenKBTillEnter()
 {
     __disable_irq();
@@ -116,8 +134,8 @@
 
     } else if ( m_buffer_c == '\n' || m_buffer_c == '\r') {
 
+        m_serial_socket->putc(m_buffer_c);
         callback_onCommand(m_buffer_s.c_str());
-        m_serial_socket->putc(m_buffer_c);
         discardBuffer();
 
     } else if (isArrowSymbol()) {
diff -r 4718a4eaf340 -r 599073f82114 SerialInputReactionHandler.h
--- a/SerialInputReactionHandler.h	Thu Nov 14 01:52:06 2019 +0000
+++ b/SerialInputReactionHandler.h	Wed Nov 27 23:35:41 2019 +0000
@@ -4,27 +4,45 @@
 #include "mbed.h"
 #include <string>
 
+/** \Class SerialInputReactionHandler
+ *
+ *  Char string signal handler. 
+ *  This require a serial object,
+ *  receiving signal from the serial object,
+ *  executing callback which can receive serial as the argument
+ */
 class SerialInputReactionHandler
 {
 public:
+    /// Constant representing input mode. 
     enum InputMode {KB_SINGLE_INPUT = 0, KB_TILL_ENTER, NUMBEROFMODES};
-    static char const * const ARROW_UP;
-    static char const * const ARROW_DOWN;
-    static char const * const ARROW_RIGHT;
-    static char const * const ARROW_LEFT;
 
+    /// Constructor
     SerialInputReactionHandler(
+        /// Register a callback supposed called when a command is ordered
         Callback<void * (char const * const)> arg_callback_onCommand = &echoCommand,
+        /// Register a callback supposed called just before a command be conducted
         Callback<void (char const * const)> arg_commonPre_callback_onCommand = &doNothing,
-        Callback<void (void *)> arg_commonPost_callback_onCommand = &doNothing
+        /// Register a callback supposed called just after a command be conducted
+        Callback<void (char const * const, void *)> arg_commonPost_callback_onCommand = &doNothing
     );
     
+    /// Register a callback supposed called when a command is ordered 
     void attach(Callback<void * (char const * const)> arg_callback_onCommand);
+    
+    /// Register a callback supposed called just before a command be conducted
     void attach_PreProc(Callback<void (char const * const)> arg_commonPre_callback_onCommand);
-    void attach_PostProc(Callback<void (void *)> arg_commonPost_callback_onCommand);
+    
+    /// Register a callback supposed called just after a command be conducted
+    void attach_PostProc(Callback<void (char const * const, void *)> arg_commonPost_callback_onCommand);
+    
+    /// Register a serial object and start listening signal from it
     void startReception(Serial * arg_serial_socket, InputMode arg_mode = KB_SINGLE_INPUT);
+    
+    /// stop listening
     void quit();
 
+    /// Change input mode
     void changeMode(InputMode arg_mode);
 
 private:
@@ -43,12 +61,15 @@
      */
     void sig_bind();
     
-    Callback<void * (char const * const)> callback_onCommand;
+    void callback_onCommand(char const * const);
+    Callback<void * (char const * const)>       m_callback_onCommand;
+    Callback<void (char const * const)>         m_Pre_callback_onCommand;
+    Callback<void (char const * const, void *)> m_Post_callback_onCommand;
     static void * echoCommand (char const * const);
-    Callback<void (char const * const)> commonPre_callback_onCommand;
-    Callback<void (void *)>commonPost_callback_onCommand;
     template <typename T>
     static void doNothing(T arg){}
+    template <typename T1, typename T2>
+    static void doNothing(T1 arg1, T2 arg){}
     //static void doNothing(char const * const);
     //static void doNothing(void *);