Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO
SerialInputReactionHandler.h
00001 #ifndef SERIAL_INPUT_REACTION_HANDLER_H 00002 #define SERIAL_INPUT_REACTION_HANDLER_H 00003 00004 #include "mbed.h" 00005 #include <string> 00006 00007 /** \Class SerialInputReactionHandler 00008 * 00009 * Char string signal handler. 00010 * This require a serial object, 00011 * receiving signal from the serial object, 00012 * executing callback which can receive serial as the argument 00013 */ 00014 class SerialInputReactionHandler 00015 { 00016 public: 00017 /// Constant representing input mode. 00018 enum InputMode {KB_SINGLE_INPUT = 0, KB_TILL_ENTER, NUMBEROFMODES}; 00019 00020 /// Constructor 00021 SerialInputReactionHandler( 00022 /// Register a callback supposed called when a command is ordered 00023 Callback<void * (char const * const)> arg_callback_onCommand = &echoCommand, 00024 /// Register a callback supposed called just before a command be conducted 00025 Callback<void (char const * const)> arg_commonPre_callback_onCommand = &doNothing, 00026 /// Register a callback supposed called just after a command be conducted 00027 Callback<void (char const * const, void *)> arg_commonPost_callback_onCommand = &doNothing 00028 ); 00029 00030 /// Register a callback supposed called when a command is ordered 00031 void attach(Callback<void * (char const * const)> arg_callback_onCommand); 00032 00033 /// Register a callback supposed called just before a command be conducted 00034 void attach_PreProc(Callback<void (char const * const)> arg_commonPre_callback_onCommand); 00035 00036 /// Register a callback supposed called just after a command be conducted 00037 void attach_PostProc(Callback<void (char const * const, void *)> arg_commonPost_callback_onCommand); 00038 00039 /// Register a serial object and start listening signal from it 00040 void startReception(Serial * arg_serial_socket, InputMode arg_mode = KB_SINGLE_INPUT); 00041 00042 /// stop listening 00043 void quit(); 00044 00045 /// Change input mode 00046 void changeMode(InputMode arg_mode); 00047 00048 private: 00049 void listenKBSingleInput(); 00050 void listenKBTillEnter(); 00051 void discardBuffer(); 00052 00053 void (SerialInputReactionHandler::*funcIfInput[NUMBEROFMODES])(); 00054 00055 /** Wrapper function that executes commonProcedure() and a function 00056 * listed in funcIfInput[]() 00057 * 00058 * This function is supposed to be used as callback attached to such like 00059 * Serial.attach(), so that it is called evrey a key input thry keyboard 00060 * or a byte written thru communication with like PCs. 00061 */ 00062 void sig_bind(); 00063 00064 void callback_onCommand(char const * const); 00065 Callback<void * (char const * const)> m_callback_onCommand; 00066 Callback<void (char const * const)> m_Pre_callback_onCommand; 00067 Callback<void (char const * const, void *)> m_Post_callback_onCommand; 00068 static void * echoCommand (char const * const); 00069 template <typename T> 00070 static void doNothing(T arg){} 00071 template <typename T1, typename T2> 00072 static void doNothing(T1 arg1, T2 arg){} 00073 //static void doNothing(char const * const); 00074 //static void doNothing(void *); 00075 00076 Serial * m_serial_socket; 00077 00078 InputMode m_input_mode; 00079 uint8_t m_buffer_c; 00080 string m_buffer_s; 00081 00082 bool isArrowSymbol(); 00083 }; 00084 00085 inline void * SerialInputReactionHandler::echoCommand (char const * const arg_str) 00086 { 00087 printf("%s", arg_str); 00088 return NULL; 00089 } 00090 inline void SerialInputReactionHandler::discardBuffer() 00091 { 00092 m_buffer_c = '\0'; 00093 m_buffer_s = ""; 00094 } 00095 00096 00097 inline bool isUpperCase (const char arg_c) 00098 { 00099 return ('A' <= arg_c && arg_c <= 'Z'); 00100 } 00101 00102 inline bool isLowerCase (const char arg_c) 00103 { 00104 return ('a' <= arg_c && arg_c <= 'z'); 00105 } 00106 00107 inline bool isSymbol (const char arg_c) 00108 { 00109 return ((0x20 <= arg_c && arg_c <= 0x40) 00110 || (0x5B <= arg_c && arg_c <= 0x60) 00111 || (0x7B <= arg_c && arg_c <= 0x7E)); 00112 } 00113 00114 #endif
Generated on Tue Jul 12 2022 23:51:24 by
1.7.2