listen serial input and react by callback registered.

Dependents:   Interference_Simple StrCommandHandler_Demo SerialInputReactionHandler_DEMO

Committer:
aktk
Date:
Wed Nov 27 23:35:41 2019 +0000
Revision:
3:599073f82114
Parent:
2:4718a4eaf340
nyaan nyaan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:ec916055f0dd 1 #ifndef SERIAL_INPUT_REACTION_HANDLER_H
aktk 0:ec916055f0dd 2 #define SERIAL_INPUT_REACTION_HANDLER_H
aktk 0:ec916055f0dd 3
aktk 0:ec916055f0dd 4 #include "mbed.h"
aktk 0:ec916055f0dd 5 #include <string>
aktk 0:ec916055f0dd 6
aktk 3:599073f82114 7 /** \Class SerialInputReactionHandler
aktk 3:599073f82114 8 *
aktk 3:599073f82114 9 * Char string signal handler.
aktk 3:599073f82114 10 * This require a serial object,
aktk 3:599073f82114 11 * receiving signal from the serial object,
aktk 3:599073f82114 12 * executing callback which can receive serial as the argument
aktk 3:599073f82114 13 */
aktk 0:ec916055f0dd 14 class SerialInputReactionHandler
aktk 0:ec916055f0dd 15 {
aktk 0:ec916055f0dd 16 public:
aktk 3:599073f82114 17 /// Constant representing input mode.
aktk 0:ec916055f0dd 18 enum InputMode {KB_SINGLE_INPUT = 0, KB_TILL_ENTER, NUMBEROFMODES};
aktk 0:ec916055f0dd 19
aktk 3:599073f82114 20 /// Constructor
aktk 0:ec916055f0dd 21 SerialInputReactionHandler(
aktk 3:599073f82114 22 /// Register a callback supposed called when a command is ordered
aktk 2:4718a4eaf340 23 Callback<void * (char const * const)> arg_callback_onCommand = &echoCommand,
aktk 3:599073f82114 24 /// Register a callback supposed called just before a command be conducted
aktk 2:4718a4eaf340 25 Callback<void (char const * const)> arg_commonPre_callback_onCommand = &doNothing,
aktk 3:599073f82114 26 /// Register a callback supposed called just after a command be conducted
aktk 3:599073f82114 27 Callback<void (char const * const, void *)> arg_commonPost_callback_onCommand = &doNothing
aktk 0:ec916055f0dd 28 );
aktk 0:ec916055f0dd 29
aktk 3:599073f82114 30 /// Register a callback supposed called when a command is ordered
aktk 2:4718a4eaf340 31 void attach(Callback<void * (char const * const)> arg_callback_onCommand);
aktk 3:599073f82114 32
aktk 3:599073f82114 33 /// Register a callback supposed called just before a command be conducted
aktk 2:4718a4eaf340 34 void attach_PreProc(Callback<void (char const * const)> arg_commonPre_callback_onCommand);
aktk 3:599073f82114 35
aktk 3:599073f82114 36 /// Register a callback supposed called just after a command be conducted
aktk 3:599073f82114 37 void attach_PostProc(Callback<void (char const * const, void *)> arg_commonPost_callback_onCommand);
aktk 3:599073f82114 38
aktk 3:599073f82114 39 /// Register a serial object and start listening signal from it
aktk 0:ec916055f0dd 40 void startReception(Serial * arg_serial_socket, InputMode arg_mode = KB_SINGLE_INPUT);
aktk 3:599073f82114 41
aktk 3:599073f82114 42 /// stop listening
aktk 0:ec916055f0dd 43 void quit();
aktk 0:ec916055f0dd 44
aktk 3:599073f82114 45 /// Change input mode
aktk 0:ec916055f0dd 46 void changeMode(InputMode arg_mode);
aktk 0:ec916055f0dd 47
aktk 0:ec916055f0dd 48 private:
aktk 0:ec916055f0dd 49 void listenKBSingleInput();
aktk 0:ec916055f0dd 50 void listenKBTillEnter();
aktk 0:ec916055f0dd 51 void discardBuffer();
aktk 0:ec916055f0dd 52
aktk 0:ec916055f0dd 53 void (SerialInputReactionHandler::*funcIfInput[NUMBEROFMODES])();
aktk 0:ec916055f0dd 54
aktk 0:ec916055f0dd 55 /** Wrapper function that executes commonProcedure() and a function
aktk 0:ec916055f0dd 56 * listed in funcIfInput[]()
aktk 0:ec916055f0dd 57 *
aktk 0:ec916055f0dd 58 * This function is supposed to be used as callback attached to such like
aktk 0:ec916055f0dd 59 * Serial.attach(), so that it is called evrey a key input thry keyboard
aktk 0:ec916055f0dd 60 * or a byte written thru communication with like PCs.
aktk 0:ec916055f0dd 61 */
aktk 0:ec916055f0dd 62 void sig_bind();
aktk 1:fd211f137803 63
aktk 3:599073f82114 64 void callback_onCommand(char const * const);
aktk 3:599073f82114 65 Callback<void * (char const * const)> m_callback_onCommand;
aktk 3:599073f82114 66 Callback<void (char const * const)> m_Pre_callback_onCommand;
aktk 3:599073f82114 67 Callback<void (char const * const, void *)> m_Post_callback_onCommand;
aktk 2:4718a4eaf340 68 static void * echoCommand (char const * const);
aktk 2:4718a4eaf340 69 template <typename T>
aktk 2:4718a4eaf340 70 static void doNothing(T arg){}
aktk 3:599073f82114 71 template <typename T1, typename T2>
aktk 3:599073f82114 72 static void doNothing(T1 arg1, T2 arg){}
aktk 2:4718a4eaf340 73 //static void doNothing(char const * const);
aktk 2:4718a4eaf340 74 //static void doNothing(void *);
aktk 0:ec916055f0dd 75
aktk 0:ec916055f0dd 76 Serial * m_serial_socket;
aktk 0:ec916055f0dd 77
aktk 0:ec916055f0dd 78 InputMode m_input_mode;
aktk 0:ec916055f0dd 79 uint8_t m_buffer_c;
aktk 0:ec916055f0dd 80 string m_buffer_s;
aktk 1:fd211f137803 81
aktk 1:fd211f137803 82 bool isArrowSymbol();
aktk 0:ec916055f0dd 83 };
aktk 0:ec916055f0dd 84
aktk 2:4718a4eaf340 85 inline void * SerialInputReactionHandler::echoCommand (char const * const arg_str)
aktk 0:ec916055f0dd 86 {
aktk 0:ec916055f0dd 87 printf("%s", arg_str);
aktk 2:4718a4eaf340 88 return NULL;
aktk 0:ec916055f0dd 89 }
aktk 1:fd211f137803 90 inline void SerialInputReactionHandler::discardBuffer()
aktk 1:fd211f137803 91 {
aktk 1:fd211f137803 92 m_buffer_c = '\0';
aktk 1:fd211f137803 93 m_buffer_s = "";
aktk 1:fd211f137803 94 }
aktk 0:ec916055f0dd 95
aktk 2:4718a4eaf340 96
aktk 2:4718a4eaf340 97 inline bool isUpperCase (const char arg_c)
aktk 2:4718a4eaf340 98 {
aktk 2:4718a4eaf340 99 return ('A' <= arg_c && arg_c <= 'Z');
aktk 2:4718a4eaf340 100 }
aktk 2:4718a4eaf340 101
aktk 2:4718a4eaf340 102 inline bool isLowerCase (const char arg_c)
aktk 2:4718a4eaf340 103 {
aktk 2:4718a4eaf340 104 return ('a' <= arg_c && arg_c <= 'z');
aktk 2:4718a4eaf340 105 }
aktk 2:4718a4eaf340 106
aktk 2:4718a4eaf340 107 inline bool isSymbol (const char arg_c)
aktk 2:4718a4eaf340 108 {
aktk 2:4718a4eaf340 109 return ((0x20 <= arg_c && arg_c <= 0x40)
aktk 2:4718a4eaf340 110 || (0x5B <= arg_c && arg_c <= 0x60)
aktk 2:4718a4eaf340 111 || (0x7B <= arg_c && arg_c <= 0x7E));
aktk 2:4718a4eaf340 112 }
aktk 2:4718a4eaf340 113
aktk 0:ec916055f0dd 114 #endif