Olivier Smeesters / Mbed 2 deprecated DtmfKit

Dependencies:   mbed ExtTextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers system.cpp Source File

system.cpp

00001 #include "system.hpp"
00002 #include "state.hpp"
00003 
00004 #include "mbed.h"
00005 
00006 #include <cstring>
00007 #include <algorithm>
00008 #include <iostream>
00009 
00010 BusOut stateLeds(LED1, LED2);
00011 
00012 System::System(DisplayManager *display, KeyboardManager *keyboard, DtmfGenerator *dtmf) : 
00013     display_(display), keyboard_(keyboard), dtmf_(dtmf), state_(NULL), text_(), len_(0), pos_(0)
00014 {
00015     std::cout << "Init System" << "\r" << std::endl;     
00016 
00017     std::memset(this->states_, 0, sizeof(this->states_));
00018 }
00019 
00020 void System::registerState(StateId id, State *state)
00021 {
00022     std::cout << "Register System State " << id << "\r" << std::endl;     
00023     this->states_[id] = state;
00024     if (this->state_ == NULL) {
00025         setState(id);
00026     }
00027 }
00028 
00029 void System::unregisterState(State *state)
00030 {
00031     State **begin = &this->states_[0];
00032     State **end = begin + NumStates;
00033     State **p = std::find(begin, end, state);
00034     if (p != end) {
00035         *p = NULL;
00036     }
00037     
00038     if (this->state_ == state) {
00039         this->state_ = NULL;
00040     }
00041 }
00042 
00043 void System::setState(StateId newState)
00044 {
00045     std::cout << "Set System state " << newState << "\r" << std::endl;     
00046     State *s = this->states_[newState];
00047     if (!s) {
00048         std::cout << "Target state not found !\r" << std::endl;
00049         return;
00050     }
00051     
00052     if (s != this->state_) {
00053         if (this->state_ != NULL) {
00054             std::cout << "Exit current state\r" << std::endl;
00055             this->state_->exitState();
00056         }
00057         
00058         stateLeds = 0;
00059         
00060         if (s != NULL) {
00061             std::cout << "Enter new state\r" << std::endl;
00062             s->enterState();
00063         }
00064         
00065         stateLeds = newState+1;
00066         
00067         this->state_ = s;
00068         std::cout << "State change complete\r" << std::endl;
00069     }
00070 }
00071 
00072 void System::insertSymbol(char ch)
00073 {
00074     std::cout << "System Insert '" << ch << "'\r" << std::endl;     
00075 
00076     if (this->len_ == 19) {
00077         return;
00078     }
00079     
00080     if (this->pos_ == this->len_) {
00081         this->text_.append(1, ch);
00082     }
00083     else {
00084         this->text_.insert(this->pos_, 1, ch);
00085     }
00086     this->pos_++;
00087     this->len_++;
00088 }
00089 
00090 void System::deleteCurrentSymbol()
00091 {
00092     std::cout << "System Delete" << "\r" << std::endl;     
00093     if (this->pos_ < this->len_) {
00094         this->text_.erase(this->pos_, 1);
00095         this->len_--;
00096         if (this->pos_ > this->len_) {
00097             this->pos_ = len_;
00098         }
00099     }
00100 }
00101 
00102 void System::clearText()
00103 {
00104     std::cout << "System Clear" << "\r" << std::endl;     
00105     this->text_.clear();
00106     this->len_ = 0;
00107     this->pos_ = 0;
00108 }
00109 
00110 void System::moveCursorTo(int pos)
00111 {
00112     std::cout << "System MoveTo " << pos << "/" << this->len_ << "\r" << std::endl;     
00113     if (pos < 0) {
00114         pos = this->len_+1 + pos;
00115     }
00116     setCursorPosition(pos);
00117 }
00118 
00119 void System::moveCursorBy(int delta)
00120 {
00121     std::cout << "System MoveBy " << delta << "\r" << std::endl;     
00122     int pos = this->pos_ + delta;
00123     setCursorPosition(pos);
00124 }
00125 
00126 void System::setCursorPosition(int pos) 
00127 {    
00128     std::cout << "System SetPos " << pos << "/" << this->len_ << "\r" << std::endl;     
00129     if (pos < 0) {
00130         pos = 0;
00131     }
00132     else if (pos > this->len_) {
00133         pos = this->len_;
00134     }
00135     this->pos_ = pos;
00136 }
00137 
00138 void System::handleKey(char ch)
00139 {
00140     this->state()->handleKey(ch);
00141 }