Skip to content
Snippets Groups Projects

feat: add state machine phone network

Merged Imported Clubelek Asso requested to merge feature/state_machine into master
8 files
+ 415
0
Compare changes
  • Side-by-side
  • Inline
Files
8
+ 83
0
#include "PhoneNetwork.hh"
PhoneNetwork::PhoneNetwork() :m_state(WAITING), m_switch(), m_communication({-1, -1}) {
}
void PhoneNetwork::changeState(State targetState) {
switch(m_state) {
case(WAITING):
break;
case(DIALING):
break;
case(RINGING):
m_switch.stop_ring();
break;
case(COMMUNICATING):
break;
}
switch(targetState) {
case(WAITING):
Serial.println("Move to state WAITING\n");
m_switch.resetLines();
break;
case(DIALING):
Serial.println("Move to state DIALING\n");
m_communication.caller = m_switch.getActiveLine();
m_switch.setLineMode(DIALING, m_communication.caller);
break;
case(RINGING):
Serial.println("Move to state RINGING\n");
m_switch.setLineMode(RINGING, m_communication.called);
m_switch.start_ring();
break;
case(COMMUNICATING):
Serial.println("Move to state COMMUNICATING\n");
m_switch.setLineMode(COMMUNICATING, m_communication.called, m_communication.caller);
break;
}
m_state = targetState;
}
void PhoneNetwork::run() {
m_switch.refreshLinesState();
Serial.println(String(m_switch.m_linesState[0]) +
String(m_switch.m_linesState[1]) +
String(m_switch.m_linesState[2]));
switch(m_state) {
case(WAITING):
if(m_switch.phoneUnhooked()) {
changeState(DIALING);
}
break;
case(DIALING):
m_communication.called = m_switch.getDialedLine(m_communication.caller);
if(m_switch.dialedComplete(m_communication.called)){
changeState(RINGING);
}
else if (!m_switch.phoneUnhooked()) {
changeState(WAITING);
}
break;
case(RINGING):
m_switch.ring();
if(m_switch.phoneUnhooked(m_communication.called)) {
changeState(COMMUNICATING);
}
else if (!m_switch.phoneUnhooked()) {
changeState(WAITING);
}
break;
case(COMMUNICATING):
if(!m_switch.phoneUnhooked(m_communication.caller)){
changeState(WAITING);
}
break;
}
}