Skip to content

Commit

Permalink
Convert to state machine (#17)
Browse files Browse the repository at this point in the history
* Convert to state machine
  • Loading branch information
matthewturner authored Apr 30, 2022
1 parent 5100580 commit 1b2a560
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 84 deletions.
92 changes: 25 additions & 67 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,22 @@ void setup()
setupTimezones();
setupTest();

commandListener = new EvtTimeListener(0, true, (EvtAction)processCommands);
mgr.addListener(commandListener);
stateMachine.when(IDLE, (EvtAction)idle, UPDATING);
stateMachine.when(SHOWING, (EvtAction)showing, UPDATING, STATE_FAILED, SHOW_TEMPORARILY_DURATION);
stateMachine.when(UPDATING, (EvtAction)updating, PROCESSING);
stateMachine.when(PROCESSING, (EvtAction)processingCommands, IDLE);

updateListener = new EvtTimeListener(0, true, (EvtAction)update);
mgr.addListener(updateListener);
stateMachine.whenInterrupted(IDLE, SHOWING);
stateMachine.transition(UPDATING);

sleepListener = new EvtByteListener(pState, IDLE, (EvtAction)sleep);
mgr.addListener(sleepListener);
mgr.addListener(&stateMachine);

showTemporarilyListener = new EvtByteListener(pState, PENDING, (EvtAction)showTemporarily);
mgr.addListener(showTemporarilyListener);

returnToNormalListener = new EvtTimeListener(SHOW_TEMPORARILY_DURATION, true, (EvtAction)returnToNormal);
returnToNormalListener->disable();
mgr.addListener(returnToNormalListener);

attachInterrupt(digitalPinToInterrupt(SHOW_PIN), wakeup, LOW);
attachInterrupt(digitalPinToInterrupt(SHOW_PIN), onInterrupt, FALLING);

Serial.println(F("Setup complete. Continuing..."));
}

void loop()
{
mgr.loopIteration();
}

void wakeup()
{
if (state == IDLE)
{
setState(PENDING);
}
}

bool sleep()
bool idle()
{
Serial.println(F("Sleeping..."));
Serial.flush();
Expand All @@ -66,7 +47,7 @@ bool sleep()
return true;
}

bool processCommands()
bool processingCommands()
{
commandReader.tryReadCommand(&command);
switch (command.Value)
Expand All @@ -77,13 +58,13 @@ bool processCommands()
break;
case Commands::SHOW:
Serial.println(F("Command: SHOW"));
showTemporarily();
stateMachine.transition(SHOWING);
break;
case Commands::SET:
Serial.print(F("Command: SET "));
Serial.println(command.Data);
clock.adjust(DateTime(command.Data));
showTemporarily();
stateMachine.transition(SHOWING);
break;
case Commands::STATUS:
Serial.println(F("Command: STATUS"));
Expand All @@ -97,7 +78,7 @@ bool processCommands()
return true;
}

bool update()
bool updating()
{
Serial.println(F("Updating..."));

Expand Down Expand Up @@ -132,31 +113,9 @@ bool update()
return true;
}

void setState(byte newState)
{
Serial.print(F("Setting state: "));
switch (newState)
{
case PENDING:
Serial.println(F("PENDING"));
break;
case IN_PROGRESS:
Serial.println(F("IN_PROGRESS"));
break;
case IDLE:
Serial.println(F("IDLE"));
break;
}
state = newState;
}

bool showTemporarily()
bool showing()
{
Serial.println(F("Showing temporarily..."));
setState(IN_PROGRESS);

updateListener->disable();
returnToNormalListener->enable();

DateTime now = DateTime();
if (CLOCK_IS_ENABLED)
Expand All @@ -178,17 +137,6 @@ bool showTemporarily()
return true;
}

bool returnToNormal()
{
Serial.println(F("Returning to normal..."));
returnToNormalListener->disable();
updateListener->enable();

setState(IDLE);

return true;
}

void render()
{
FastLED.setBrightness(display.getBrightness());
Expand Down Expand Up @@ -219,7 +167,7 @@ void reportStatus()
{
bluetoothSerial.println(F("{"));
bluetoothSerial.print(F(" \"time\": "));
if(CLOCK_IS_ENABLED)
if (CLOCK_IS_ENABLED)
{
bluetoothSerial.println(clock.now().unixtime());
}
Expand Down Expand Up @@ -342,4 +290,14 @@ void setupTimezones()
timezone = new Timezone(
TimeChangeRule("BST", Last, Sun, Mar, 1, 60),
TimeChangeRule("GMT", Last, Sun, Oct, 2, 0));
}

void loop()
{
mgr.loopIteration();
}

void onInterrupt()
{
stateMachine.onInterrupt();
}
26 changes: 9 additions & 17 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ const byte SHOW_PIN = 2;
const bool CLOCK_IS_ENABLED = true;

const byte IDLE = 0;
const byte PENDING = 1;
const byte IN_PROGRESS = 2;
const byte UPDATING = 1;
const byte PROCESSING = 2;
const byte SHOWING = 3;

volatile byte state = IDLE;
volatile byte *pState = &state;
CRGB physicalLeds[NUM_LEDS];
Display display;
Display pendingDisplay;
Expand All @@ -46,22 +45,15 @@ RuntimeStreamReader streamReader(&bluetoothSerial);
Command command;
CommandReader commandReader(&streamReader);

EvtTimeListener *updateListener;
EvtTimeListener *commandListener;
EvtTimeListener *renderListener;
EvtByteListener *showTemporarilyListener;
EvtTimeListener *returnToNormalListener;
EvtByteListener *sleepListener;
StateMachineListener stateMachine;

void setState(byte state);
void wakeup();
void onInterrupt();
void render();
void render(CRGB::HTMLColorCode colorCode, byte brightness);
bool update();
bool sleep();
bool showTemporarily();
bool returnToNormal();
bool processCommands();
bool updating();
bool idle();
bool showing();
bool processingCommands();
void reportStatus();
DateTime toLocal(DateTime utc);

Expand Down

0 comments on commit 1b2a560

Please sign in to comment.