MachineIntelligenceCore:Toolchain
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KeyHandlerRegistry.cpp
Go to the documentation of this file.
1 
24 
25 namespace mic {
26  namespace application {
27 
29  //key_map.insert(std::make_pair((char)27, std::make_pair( "ESC - exit the program", boost::bind(&KeyHandler::functionQuit, this ) ) ) );
30  extendedInputMode = false;
31 
32  // Register default key handlers.
33  registerKeyhandler(27, "ESC - exits the program", &KeyHandlerRegistry::keyhandlerQuit, this);
34  registerKeyhandler('h', "h - displays this list of registered key handlers", &KeyHandlerRegistry::keyhandlerDisplayOptions, this);
35  registerKeyhandler('s', "s - display application status", &KeyHandlerRegistry::keyhandlerDisplayAppState, this);
36 
37  // Logger.
38  registerKeyhandler(';', "; - increments the logger severity level", &KeyHandlerRegistry::keyhandlerIncrementLoggerLevel, this);
39  registerKeyhandler('\'', "\' - decrements the logger severity level", &KeyHandlerRegistry::keyhandlerDecrementLoggerLevel, this);
40 
41  // Time keyhandlers.
42  registerKeyhandler('-', "- - slows down the processing (multiplies the sleep interval by 1.5)", &KeyHandlerRegistry::keyhandlerSlowDown, this);
43  registerKeyhandler('+', "+ - speeds up the processing (divides the sleep interval by 1.5)", &KeyHandlerRegistry::keyhandlerFastenUp, this);
44  registerKeyhandler('=', "= - resets the processing time (sets the sleep interval to 1s)", &KeyHandlerRegistry::keyhandlerResetProcessingTime, this);
45 
46  // Application modes.
47  registerKeyhandler('l', "l - toggles learning mode on/off", &KeyHandlerRegistry::keyhandlerToggleLearning, this);
48  registerKeyhandler(' ', "PAUSE - stops/starts the continuous execution of the program", &KeyHandlerRegistry::keyhandlerPause, this);
49  registerKeyhandler('\\', "\\ - performs a single step", &KeyHandlerRegistry::keyhandlerSingleStep, this);
50 
51  LOG(LSTATUS) << "keyHandlerRegistry established ok...";
52  }
53 
55  // TODO Auto-generated destructor stub
56  }
57 
58  void KeyHandlerRegistry::keyboardHandler(unsigned char key_) {
59  LOG(LTRACE) << "KeyHandlerRegistry::keyboardHandler";
60 
61  bool processKeystroke = true;
62  if (extendedInputMode) {
63  if (key_ == 13) {
64  key_ = finishKey;
65  }
66  else if (key_ == 27) {
67  payload = "-1"; // must be interpreted as an abort entry
68  key_ = finishKey;
69  }
70  else {
71  payload.append((const char *) &key_, 1);
72  LOG(LSTATUS) << payload ;
73  key_ = 0;
74  processKeystroke = false;
75  }
76  }
77  if (processKeystroke) {
78  // Try to find the pressed key.
79  KeyHandlerMap::const_iterator iter = key_handler_map.find(key_);
80  if (iter != key_handler_map.end())
81  // If found - execute the associated function.
82  ((iter->second).second)();
83  else
84  LOG(LWARNING) << "Handler for given key not found. Please press h for help.";
85  }
86  }
87 
89  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerDisplayOptions";
90 
91  LOG(LSTATUS) << "----------------------------------------------------------------";
92  LOG(LSTATUS) << "List of registered key handlers:";
93  LOG(LSTATUS) << "----------------------------------------------------------------";
94  // Iterate through handlers and display their descriptions.
95  for (KeyHandlerMap::const_iterator iter = key_handler_map.begin(); iter != key_handler_map.end(); iter++) {
96  LOG(LSTATUS) << (iter->second).first;
97  }//: each option
98  LOG(LSTATUS) << "----------------------------------------------------------------";
99  }
100 
102  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerQuit";
103  // Set quit flag to true.
104  APP_STATE->setQuit();
105 
106  }
107 
108  void keyhandlerDisplayAppState(void);
109 
111  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerIncrementLoggerLevel";
112  LOGGER->incrementSeverityLevel();
113  }
114 
116  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerDecrementLoggerLevel";
117  LOGGER->decrementSeverityLevel();
118  }
119 
121  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerToggleLearning";
122  // Switch state of the learning mode.
123  APP_STATE->pressLearning();
124  }
125 
127  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerPause";
128  // Switch state of the pause mode.
129  APP_STATE->pressPause();
130  }
131 
133  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerSingleStep";
134  // Switch state of the single step mode.
135  APP_STATE->pressSingleStep();
136  }
137 
139  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerSlowDown";
140  APP_STATE->multiplySleepInterval();
141  }
142 
144  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerFastenUp";
145  APP_STATE->divideSleepInterval();
146  }
147 
149  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerResetProcessingTime";
150  APP_STATE->setSleepIntervalS(1);
151  }
152 
154  LOG(LTRACE) << "KeyHandlerRegistry::keyhandlerDisplayAppState";
155  APP_STATE->displayStatus();
156  }
157 
158  std::string KeyHandlerRegistry::changeInputMode(unsigned char finishKey_) {
159  if (extendedInputMode) {
160  extendedInputMode = false;
161  }
162  else {
163  extendedInputMode = true;
164  finishKey = finishKey_;
165  payload = "";
166  }
167  return (payload);
168  }
169 
170  } /* namespace application */
171 } /* namespace mic */
std::string changeInputMode(unsigned char finishKey_)
#define APP_STATE
Macro returning application state instance.
#define LTRACE
Definition: LoggerAux.hpp:52
#define LWARNING
Definition: LoggerAux.hpp:57
#define LSTATUS
Definition: LoggerAux.hpp:56
virtual void keyboardHandler(unsigned char key)
Contains declaration of the abstract KeyHandlerRegistry class responsible for handling the keypressed...
void keyhandlerDisplayAppState(void)
#define LOG(level)
Macro for message printing.
Definition: Log.hpp:39
void registerKeyhandler(char key_, std::string description_, void(SlotClass::*function_)(void), SlotClass *obj_)
#define LOGGER
Macro returning logger instance.
Definition: Log.hpp:34