Skip to content

Instantly share code, notes, and snippets.

@loginov-rocks
Created January 8, 2022 19:39
Show Gist options
  • Save loginov-rocks/c7e9d3b286da0336fb337f38f9aad5bc to your computer and use it in GitHub Desktop.
Save loginov-rocks/c7e9d3b286da0336fb337f38f9aad5bc to your computer and use it in GitHub Desktop.
DIY Connected Espresso Machine: Main Class and Indicators (Part 5) - EspressoMachine Implementation, Work Part
#include "EspressoMachine.h"
void EspressoMachine::operateDone()
{
isDone = false;
if (getBoilerTargetTemp() != BoilerTemp::Cold && getBoilerTemp() == getBoilerTargetTemp())
{
isDone = true;
}
digitalWrite(donePin, isDone);
}
void EspressoMachine::work()
{
// Get toggle state before checking whether it was toggled to have the flag up to date.
ToggleState toggleState = getToggleState();
// Set the command if the toggle has been toggled.
if (toggle.getIsToggled())
{
switch (toggleState)
{
case ToggleState::Boil:
setCommand(EspressoMachineCommand::ToggleBoil);
break;
case ToggleState::MakeSteam:
setCommand(EspressoMachineCommand::ToggleMakeSteam);
break;
case ToggleState::PourWater:
setCommand(EspressoMachineCommand::TogglePourWater);
break;
// Off by default.
default:
setCommand(EspressoMachineCommand::Off);
break;
}
}
// Operate components based on the command value.
switch (lastCommand)
{
// External commands.
case EspressoMachineCommand::PourWater:
pump.on();
break;
case EspressoMachineCommand::StopPouringWater:
pump.off();
break;
case EspressoMachineCommand::Boil:
boiler.setTargetTemp(BoilerTemp::Boiling);
break;
case EspressoMachineCommand::MakeSteam:
boiler.setTargetTemp(BoilerTemp::Steam);
break;
case EspressoMachineCommand::CoolDown:
boiler.setTargetTemp(BoilerTemp::Cold);
break;
// Internal commands.
case EspressoMachineCommand::ToggleBoil:
pump.off();
boiler.setTargetTemp(BoilerTemp::Boiling);
break;
case EspressoMachineCommand::ToggleMakeSteam:
pump.off();
boiler.setTargetTemp(BoilerTemp::Steam);
break;
case EspressoMachineCommand::TogglePourWater:
pump.on();
boiler.setTargetTemp(BoilerTemp::Cold);
break;
// Off by default.
default:
pump.off();
boiler.setTargetTemp(BoilerTemp::Cold);
break;
}
// Operate boiler to achieve the target state.
boiler.work();
// Operate the "Done" pin to reflect the command state.
operateDone();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment