Skip to content

Commit

Permalink
add manual flush as own function
Browse files Browse the repository at this point in the history
take manual flush out of checkbrewswitch
manual flush reset standby time and ends standby
  • Loading branch information
LoQue90 committed Oct 9, 2024
1 parent 9bb37f3 commit cca508b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 89 deletions.
210 changes: 124 additions & 86 deletions src/brewHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
*
*/
// TODO: Clean up brew()
// move flush out of checkbrewswitch
// flush should trigger standby timer
// flush should stop standby
// move flush out of checkbrewswitch -> Clean up Brewswitch
// move brewPIDDisabled to kBrew?
// add flush to display templates, SHOTTIMER 0
// add flush to display templates, SHOTTIMER 0 still to do
// check all Scale stuff
// add shot time log info output
// add shot time log info output ???
// check params website
// ...
// check MQTT/HASSIO for all brew stuff
// is brewSwitchWasOff needed?

#pragma once

Expand All @@ -39,6 +38,11 @@ enum BrewState {
kWaitBrewOff = 43
};

enum ManualFlushState {
kManualFlushIdle = 10,
kManualFlushRunning = 20,
};

enum BackflushState {
kBackflushWaitBrewswitchOn = 10,
kBackflushFillingStart = 20,
Expand All @@ -50,13 +54,15 @@ enum BackflushState {

// Normal Brew
BrewState currBrewState = kBrewIdle;
ManualFlushState currManualFlushState = kManualFlushIdle;
BrewSwitchState brewSwitchState = kBrewSwitchIdle;

uint8_t currStateBrewSwitch = LOW;
uint8_t currBrewSwitchStateMomentary = LOW;
int brewSwitchState = kBrewSwitchIdle;
boolean brewSwitchWasOff = false;

int brewOn = 0; // flag is set if brew was detected
int manualFlushOn = 0; // flag is set if manual flush is detected
double totalBrewTime = 0; // total brewtime set in software
double timeBrewed = 0; // total brewed time
double lastBrewTimeMillis = 0; // for shottimer delay after brew is finished
Expand Down Expand Up @@ -110,22 +116,19 @@ void checkbrewswitch() {
if (currBrewSwitchStateMomentary == LOW && currStateBrewSwitch == LOW) {
currStateBrewSwitch = HIGH;
brewSwitchState = kBrewSwitchActive;
LOG(DEBUG, "brewSwitchState = kBrewSwitchBrew; brew switch short pressed - start Brew");
LOG(DEBUG, "brewSwitchState = kBrewSwitchBrew; brew switch short pressed");
}
else if (currBrewSwitchStateMomentary == HIGH && brewSwitch->longPressDetected() && machineState != kWaterEmpty) { // Brew switch more than brewSwitchMomentaryLongPress pressed - start flushing
valveRelay.on();
pumpRelay.on();
startingTime = millis();
else if (currBrewSwitchStateMomentary == HIGH && brewSwitch->longPressDetected()) { // Brew switch more than brewSwitchMomentaryLongPress pressed - start flushing
brewSwitchState = kBrewSwitchFlush;
LOG(DEBUG, "brewSwitchState = kBrewSwitchBrew: brew switch long pressed - start flushing");
LOG(DEBUG, "brewSwitchState = kBrewSwitchBrew: brew switch long pressed");
}
break;

case kBrewSwitchActive:
if (currBrewSwitchStateMomentary == HIGH && currStateBrewSwitch == HIGH) { // Brew switch got short pressed while brew is running - abort brew
currStateBrewSwitch = LOW;
brewSwitchState = kBrewSwitchWaitForRelease;
LOG(DEBUG, "brewSwitchState = kBrewSwitchActive: brew switch short pressed - stop brew");
LOG(DEBUG, "brewSwitchState = kBrewSwitchActive: brew switch short pressed");
}
else if ((currBrewState == kBrewFinished) || (backflushState == kBackflushWaitBrewswitchOff)) { // Brew reached target and stopped or blackflush cycle done
currStateBrewSwitch = LOW;
Expand All @@ -136,10 +139,8 @@ void checkbrewswitch() {

case kBrewSwitchFlush: // Brew switch got released after long press detected - stop flushing
if (currBrewSwitchStateMomentary == LOW) {
valveRelay.off();
pumpRelay.off();
brewSwitchState = kBrewSwitchWaitForRelease;
LOG(DEBUG, "brewswitchState = kBrewSwitchFlush: brew switch long press released - stop flushing");
LOG(DEBUG, "brewswitchState = kBrewSwitchFlush: brew switch long press released");
}
break;

Expand Down Expand Up @@ -187,6 +188,90 @@ void brewTimer() {
#endif

#if (FEATURE_BREWCONTROL == 1)
/**
* @brief Backflush
*/
void backflush() {
if (backflushState != kBackflushWaitBrewswitchOn && backflushOn == 0) {
backflushState = kBackflushWaitBrewswitchOff; // Force reset in case backflushOn is reset during backflush!
LOG(INFO, "Backflush: Disabled via Webinterface");
}
else if (offlineMode == 1 || currBrewState > kBrewIdle || maxflushCycles <= 0 || backflushOn == 0) {
return;
}

if (bPID.GetMode() == 1) { // Deactivate PID
bPID.SetMode(0);
pidOutput = 0;
}

heaterRelay.off(); // Stop heating

checkbrewswitch();

if (currStateBrewSwitch == LOW && backflushState != kBackflushWaitBrewswitchOn) { // Abort function for state machine from every state
backflushState = kBackflushWaitBrewswitchOff;
}

// State machine for backflush
switch (backflushState) {
case kBackflushWaitBrewswitchOn:
if (currStateBrewSwitch == HIGH && backflushOn) {
startingTime = millis();
backflushState = kBackflushFillingStart;
}

break;

case kBackflushFillingStart:
LOG(INFO, "Backflush: Portafilter filling...");
valveRelay.on();
pumpRelay.on();
backflushState = kBackflushFilling;

break;

case kBackflushFilling:
if (millis() - startingTime > FILLTIME) {
startingTime = millis();
backflushState = kBackflushFlushingStart;
}

break;

case kBackflushFlushingStart:
LOG(INFO, "Backflush: Flushing to drip tray...");
valveRelay.off();
pumpRelay.off();
flushCycles++;
backflushState = kBackflushFlushing;

break;

case kBackflushFlushing:
if (millis() - startingTime > flushTime && flushCycles < maxflushCycles) {
startingTime = millis();
backflushState = kBackflushFillingStart;
}
else if (flushCycles >= maxflushCycles) {
backflushState = kBackflushWaitBrewswitchOff;
}

break;

case kBackflushWaitBrewswitchOff:
if (currStateBrewSwitch == LOW) {
LOG(INFO, "Backflush: Finished!");
valveRelay.off();
pumpRelay.off();
flushCycles = 0;
backflushState = kBackflushWaitBrewswitchOn;
}

break;
}
}

/**
* @brief Time or weight based brew mode
*/
Expand All @@ -201,7 +286,7 @@ void brew() {
currBrewState = kWaitBrewOff;
}

if (currBrewState > kBrewIdle && currBrewState < kWaitBrewOff || brewSwitchState == kBrewSwitchFlush) {
if (currBrewState > kBrewIdle && currBrewState < kWaitBrewOff) {
timeBrewed = currentMillisTemp - startingTime;
}

Expand Down Expand Up @@ -317,86 +402,39 @@ void brew() {
break;
}
}

/**
* @brief Backflush
* @brief manual grouphead flush
*/
void backflush() {
if (backflushState != kBackflushWaitBrewswitchOn && backflushOn == 0) {
backflushState = kBackflushWaitBrewswitchOff; // Force reset in case backflushOn is reset during backflush!
LOG(INFO, "Backflush: Disabled via Webinterface");
}
else if (offlineMode == 1 || currBrewState > kBrewIdle || maxflushCycles <= 0 || backflushOn == 0) {
return;
}

if (bPID.GetMode() == 1) { // Deactivate PID
bPID.SetMode(0);
pidOutput = 0;
}

heaterRelay.off(); // Stop heating

void manualFlush() {
unsigned long currentMillisTemp = millis();
checkbrewswitch();

if (currStateBrewSwitch == LOW && backflushState != kBackflushWaitBrewswitchOn) { // Abort function for state machine from every state
backflushState = kBackflushWaitBrewswitchOff;
if (currManualFlushState == kManualFlushRunning) {
timeBrewed = currentMillisTemp - startingTime;
}

// State machine for backflush
switch (backflushState) {
case kBackflushWaitBrewswitchOn:
if (currStateBrewSwitch == HIGH && backflushOn) {
startingTime = millis();
backflushState = kBackflushFillingStart;
}

break;

case kBackflushFillingStart:
LOG(INFO, "Backflush: Portafilter filling...");
valveRelay.on();
pumpRelay.on();
backflushState = kBackflushFilling;

break;

case kBackflushFilling:
if (millis() - startingTime > FILLTIME) {
switch (currManualFlushState) {
case kManualFlushIdle:
if (brewSwitchState == kBrewSwitchFlush && machineState != kWaterEmpty) {
LOG(INFO, "Manual flush started");
startingTime = millis();
backflushState = kBackflushFlushingStart;
manualFlushOn = 1;
valveRelay.on();
pumpRelay.on();
resetStandbyTimer();
currManualFlushState = kManualFlushRunning;
}

break;

case kBackflushFlushingStart:
LOG(INFO, "Backflush: Flushing to drip tray...");
valveRelay.off();
pumpRelay.off();
flushCycles++;
backflushState = kBackflushFlushing;

break;

case kBackflushFlushing:
if (millis() - startingTime > flushTime && flushCycles < maxflushCycles) {
startingTime = millis();
backflushState = kBackflushFillingStart;
}
else if (flushCycles >= maxflushCycles) {
backflushState = kBackflushWaitBrewswitchOff;
}

break;

case kBackflushWaitBrewswitchOff:
if (currStateBrewSwitch == LOW) {
LOG(INFO, "Backflush: Finished!");
case kManualFlushRunning:
if (brewSwitchState != kBrewSwitchFlush) {
LOG(INFO, "Manual flush stopped");
manualFlushOn = 0;
valveRelay.off();
pumpRelay.off();
flushCycles = 0;
backflushState = kBackflushWaitBrewswitchOn;
LOGF(INFO, "Manual flush time: %4.1f s", timeBrewed / 1000);
currManualFlushState = kManualFlushIdle;
}

break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/display/displayCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ bool displayShottimer() {
return false;
}

if (machineState == kBrew || (millis() - lastBrewTimeMillis) < SHOTTIMERDISPLAYDELAY || brewSwitchState == kBrewSwitchFlush) {
if (machineState == kBrew || (millis() - lastBrewTimeMillis) < SHOTTIMERDISPLAYDELAY || currManualFlushState == kManualFlushRunning) {
u8g2.clearBuffer();

if (brewSwitchState != kBrewSwitchFlush) {
if (currManualFlushState != kManualFlushRunning) {
u8g2.drawXBMP(-1, 11, Brew_Cup_Logo_width, Brew_Cup_Logo_height, Brew_Cup_Logo);
}
else {
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ void handleMachineState() {
#endif
}

if (pidON || steamON || brewOn) {
if (pidON || steamON || brewOn || manualFlushOn) {
pidON = 1;
resetStandbyTimer();
#if OLED_DISPLAY != 0
Expand Down Expand Up @@ -1515,6 +1515,7 @@ void looppid() {

#if (FEATURE_BREWSWITCH == 1 && FEATURE_BREWCONTROL == 1)
brew();
manualFlush();
#endif

#if (FEATURE_PRESSURESENSOR == 1)
Expand Down

0 comments on commit cca508b

Please sign in to comment.