Olivier Smeesters / Mbed 2 deprecated DtmfKit

Dependencies:   mbed ExtTextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers command_state.cpp Source File

command_state.cpp

00001 #include "system_states.hpp"
00002 #include "system.hpp"
00003 #include "display_manager.hpp"
00004 
00005 const char *statusMsg[] = {
00006     //234567890123456
00007     "Cmd: [5]Help",
00008     "Cmd: [0]Send",
00009     "Cmd: 1<-4-+-6->3",
00010     "Cmd: [7]BS[8]Del",
00011     "Cmd: [9]Clear",
00012     "Cmd: <*>Edit",
00013     NULL
00014 };
00015 
00016 enum {
00017     HelpTimeout = 2
00018 };
00019 
00020 void CommandState::enterState() {
00021     DisplayManager *display = system()->display();
00022     display->hideCursor();
00023     showStatusMsg(0);
00024     updateText();
00025 }
00026 
00027 void CommandState::exitState() {
00028     this->statusMsgTimeout.detach();
00029 }
00030 
00031 void CommandState::handleKey(char key)
00032 {
00033     switch(key) {
00034     case '5':
00035         handleHelp(); break;
00036     case '1':
00037         handleHome(); break;
00038     case '3':
00039         handleEnd(); break;
00040     case '4':
00041         handleLeft(); break;
00042     case '6':
00043         handleRight(); break;
00044     case '7':
00045         handleBackSpace(); break;
00046     case '8':
00047         handleDelete(); break;
00048     case '9':
00049         handleClear(); break;
00050     case '0':
00051         handleSend(); break;
00052     case '@':
00053         system()->setState(System::Edit);
00054         break;
00055     default:
00056         break;
00057     }
00058 }
00059 
00060 void CommandState::handleHome() const {
00061     system()->moveCursorTo(0);
00062     updateCursor();
00063 }
00064 
00065 void CommandState::handleEnd() const {
00066     system()->moveCursorTo(-1);
00067     updateCursor();
00068 }
00069 
00070 void CommandState::handleLeft() const {
00071     system()->moveCursorBy(-1);
00072     updateCursor();
00073 }
00074 
00075 void CommandState::handleRight() const {
00076     system()->moveCursorBy(+1);
00077     updateCursor();
00078 }
00079 
00080 void CommandState::handleDelete() const {
00081     system()->deleteCurrentSymbol();
00082     updateText();
00083 }
00084 
00085 void CommandState::handleBackSpace() const {
00086     system()->moveCursorBy(-1);
00087     system()->deleteCurrentSymbol();
00088     updateText();
00089 }
00090 
00091 void CommandState::handleClear() const {
00092     system()->clearText();
00093     updateText();
00094 }
00095 
00096 void CommandState::handleSend() const {
00097     if (system()->text_size() > 0) {
00098         system()->setState(System::Sending);
00099     }
00100 }
00101 
00102 void CommandState::handleHelp() {
00103     this->statusMsgTimeout.detach();
00104     showNextStatusMsg();
00105 }
00106 
00107 void CommandState::showNextStatusMsg() {
00108     int msg = this->statusMsgIndex+1;
00109     if (statusMsg[msg] == NULL) {
00110         msg = 0;
00111     }
00112     showStatusMsg(msg);
00113 }
00114 
00115 void CommandState::showStatusMsg(int index) {
00116     this->statusMsgIndex = index;
00117     DisplayManager *display = system()->display();
00118     display->writeStatus(statusMsg[index]);
00119     this->statusMsgTimeout.attach(this, &CommandState::showNextStatusMsg, HelpTimeout * 1.0);    
00120 }