From efdf3198f7e95105f88c0ca2517661c5225d7367 Mon Sep 17 00:00:00 2001 From: MaciekB <79maciek@gmail.com> Date: Sat, 20 Jan 2024 18:02:58 +0100 Subject: [PATCH] added Manual mode including manual grinding time --- src/Display.cpp | 22 ++++++++++++- src/Display.h | 2 +- src/Dosage.h | 1 + src/RotaryEncoder.cpp | 4 +-- src/main.cpp | 75 ++++++++++++++++++++++++++++++++----------- 5 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/Display.cpp b/src/Display.cpp index 9647089..dede7aa 100644 --- a/src/Display.cpp +++ b/src/Display.cpp @@ -53,6 +53,26 @@ void Display::printDoubleDose() { display->display(); } +void Display::printManualMode(double time){ + display->clearDisplay(); + display->setTextColor(WHITE); + display->setTextSize(2); + display->setCursor(30,10); + display->print("Manual"); + display->println(); + time = time < 0 ? 0.0 : time; + time = time < 100 ? time : 99.9; + display->setCursor(34,34); + display->print(time, time < 10 ? 2 : 1); + + display->setTextSize(2); + display->setCursor(90, 34); + display->print("s"); + + display->println(); + display->display(); +} + void Display::printTime(double time) { display->clearDisplay(); @@ -88,4 +108,4 @@ void Display::printStatistics(int numberSingles, int numberDoubles) { display->println(); display->display(); -} \ No newline at end of file +} diff --git a/src/Display.h b/src/Display.h index 2186f4f..d07d8d5 100644 --- a/src/Display.h +++ b/src/Display.h @@ -1,5 +1,4 @@ #pragma once - class Adafruit_SSD1306; class Display @@ -11,6 +10,7 @@ class Display Display(); void printSingleDose(); void printDoubleDose(); + void printManualMode(double time); void printTime(double time); void printStatistics(int numberSingles, int numberDoubles); }; \ No newline at end of file diff --git a/src/Dosage.h b/src/Dosage.h index 4875868..b180210 100644 --- a/src/Dosage.h +++ b/src/Dosage.h @@ -9,6 +9,7 @@ class Dosage public: Dosage(); bool singleDoseSelected = false; + bool manualModeSelected = false; double singleDoseTime = SINGLEDOSE_DEFAULT_TIME; double doubleDoseTime = DOUBLEDOSE_DEFAULT_TIME; void writeToEEPROM(); diff --git a/src/RotaryEncoder.cpp b/src/RotaryEncoder.cpp index 6e2c14d..feb5fb8 100644 --- a/src/RotaryEncoder.cpp +++ b/src/RotaryEncoder.cpp @@ -10,7 +10,7 @@ RotaryEncoder::RotaryEncoder() { } bool RotaryEncoder::wasTurnedLeft() { - if (encoder->read() < 0 - ENC_TOL) { + if (encoder->read() > 0 + ENC_TOL) { encoder->write(0); return true; } @@ -18,7 +18,7 @@ bool RotaryEncoder::wasTurnedLeft() { } bool RotaryEncoder::wasTurnedRight() { - if (encoder->read() > 0 + ENC_TOL) { + if (encoder->read() < 0 - ENC_TOL) { encoder->write(0); return true; } diff --git a/src/main.cpp b/src/main.cpp index e51aead..8d72b7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,13 @@ Grinder *grinder; // State Machine enum States {NORMAL, SET_DOSE, GRINDING, STATS}; +// Modes +enum Modes {SINGLE, DOUBLE, MANUAL}; + uint8_t state = NORMAL; +uint8_t mode = DOUBLE; +double manualGrindTime =0.0; + void setup() { Serial.begin(9600); @@ -30,6 +36,7 @@ void loop() { switch(state) { case NORMAL: + grinder->off(); // set new dose if (encoder->wasPressed()) { state = SET_DOSE; @@ -47,39 +54,71 @@ void loop() { state = STATS; break; } - - // select dosage - if (encoder->wasTurnedLeft()) { - dosage->singleDoseSelected = true; - } else if (encoder->wasTurnedRight()) { - dosage->singleDoseSelected = false; + + switch(mode){ + case(SINGLE): + if(encoder->wasTurnedRight())mode=DOUBLE; + break; + case(DOUBLE): + if(encoder->wasTurnedLeft())mode=SINGLE; + if(encoder->wasTurnedRight())mode=MANUAL; + break; + case(MANUAL): + if(encoder->wasTurnedLeft())mode=DOUBLE; + break; } + // display dosage - if (dosage->singleDoseSelected) { + if (mode==SINGLE) { + dosage->singleDoseSelected=true; + dosage->manualModeSelected=false; + manualGrindTime=0.0; display->printSingleDose(); - } else { - display->printDoubleDose(); + } else if(mode==DOUBLE) { + dosage->singleDoseSelected=false; + dosage->manualModeSelected=false; + manualGrindTime=0.0; + display->printDoubleDose() ; + } else if(mode==MANUAL) { + dosage->singleDoseSelected=false; + dosage->manualModeSelected=true; + display->printManualMode(manualGrindTime*1.0); } + break; case SET_DOSE: - if (encoder->wasTurnedLeft()) { - dosage->singleDoseTime -= dosage->singleDoseSelected ? 0.1 : 0.0; - dosage->doubleDoseTime -= dosage->singleDoseSelected == false ? 0.1 : 0.0; - } else if (encoder->wasTurnedRight()) { - dosage->singleDoseTime += dosage->singleDoseSelected ? 0.1 : 0.0; - dosage->doubleDoseTime += dosage->singleDoseSelected == false ? 0.1 : 0.0; + if(dosage->manualModeSelected==false){ + if (encoder->wasTurnedLeft()) { + dosage->singleDoseTime -= dosage->singleDoseSelected ? 0.1 : 0.0; + dosage->doubleDoseTime -= dosage->singleDoseSelected==false ? 0.1 : 0.0; + } else if (encoder->wasTurnedRight()) { + dosage->singleDoseTime += dosage->singleDoseSelected ? 0.1 : 0.0; + dosage->doubleDoseTime += dosage->singleDoseSelected==false ? 0.1 : 0.0; + } + dosage->writeToEEPROM(); + display->printTime(dosage->singleDoseSelected ? dosage->singleDoseTime : dosage->doubleDoseTime); } - - dosage->writeToEEPROM(); - display->printTime(dosage->singleDoseSelected ? dosage->singleDoseTime : dosage->doubleDoseTime); state = NORMAL; break; case GRINDING: + if(dosage->manualModeSelected){ + int startTime = millis()/1000.0; + while(grinder->startBtnPressed()){ + grinder->on(1); + display->printTime(millis()/1000.0-startTime); + + } + manualGrindTime+=millis()/1000.0-startTime; + grinder->off(); + state = NORMAL; + break; + }else + grinder->increaseShotCounter(dosage->singleDoseSelected); grinder->on(dosage->singleDoseSelected ? dosage->singleDoseTime : dosage->doubleDoseTime);