-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
301 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "modes/mode_espresso.h" | ||
#include "data/localization.h" | ||
|
||
void ModeEspresso::enter() { buttons.resetEncoderTicks(); } | ||
|
||
void ModeEspresso::update() | ||
{ | ||
// start stopwatch and tare on click | ||
if (buttons.getEncoderClick() == ClickType::SINGLE) | ||
{ | ||
stopwatch.toggle(); | ||
|
||
if (stopwatch.isRunning()) | ||
{ | ||
weightSensor.tare(); | ||
approximator.reset(); | ||
} | ||
} | ||
|
||
// clamp target weight to min and max | ||
targetWeightMg += (buttons.getEncoderTicks() * ENCODER_MG_PER_TICK); | ||
if (targetWeightMg < MIN_TARGET_WEIGHT_MG) | ||
{ | ||
targetWeightMg = MIN_TARGET_WEIGHT_MG; | ||
} | ||
else if (targetWeightMg > MAX_TARGET_WEIGHT_MG) | ||
{ | ||
targetWeightMg = MAX_TARGET_WEIGHT_MG; | ||
} | ||
|
||
if (buttons.getEncoderTicks() >= 1 || buttons.getEncoderTicks() <= -1) | ||
{ | ||
buttons.resetEncoderTicks(); | ||
} | ||
|
||
// new weight must be handled for regression | ||
if (weightSensor.isNewWeight()) | ||
{ | ||
handleNewWeight(); | ||
} | ||
|
||
// display | ||
int32_t remainingTime = lastEstimatedTime - stopwatch.getTime(); | ||
bool waiting = | ||
!stopwatch.isRunning() || remainingTime < 0 || remainingTime > REGRESSION_MAX_TIME || stopwatch.getTime() < REGRESSION_GRACE_PERIOD; | ||
|
||
display.espressoShot(stopwatch.getTime(), remainingTime, weightSensor.getWeight() * 1000, targetWeightMg, waiting); | ||
} | ||
|
||
void ModeEspresso::handleNewWeight() | ||
{ | ||
if (stopwatch.isRunning()) | ||
{ | ||
int32_t lastWeightMg = weightSensor.getLastWeight() * 1000; | ||
approximator.addPoint({(long)stopwatch.getTime(), (float)lastWeightMg}); | ||
lastEstimatedTime = approximator.getXAtY(targetWeightMg); | ||
|
||
if (lastWeightMg >= targetWeightMg) | ||
{ | ||
stopwatch.stop(); | ||
} | ||
} | ||
} | ||
|
||
bool ModeEspresso::canSwitchMode() { return true; } | ||
|
||
const char *ModeEspresso::getName() { return MODE_NAME_ESPRESSO; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "loadcell.h" | ||
#include "mode.h" | ||
#include "stopwatch.h" | ||
#include "user_interface.h" | ||
#include "regression.h" | ||
|
||
#define ENCODER_MG_PER_TICK 100 | ||
|
||
#define MIN_TARGET_WEIGHT_MG 1000 | ||
#define MAX_TARGET_WEIGHT_MG 100000 | ||
|
||
#define REGRESSION_BUFFER_SIZE 50 | ||
#define REGRESSION_MAX_TIME 3 * 60 * 1000 | ||
#define REGRESSION_GRACE_PERIOD 1000 | ||
|
||
class ModeEspresso : public Mode | ||
{ | ||
public: | ||
ModeEspresso(WeightSensor &weightSensor, UserInput &buttons, Display &display, Stopwatch &stopwatch) | ||
: weightSensor(weightSensor), buttons(buttons), display(display), stopwatch(stopwatch), | ||
targetWeightMg(36 * 1000), approximator(REGRESSION_BUFFER_SIZE), lastEstimatedTime(0){}; | ||
~ModeEspresso(){}; | ||
void update() override; | ||
void enter() override; | ||
bool canSwitchMode() override; | ||
const char *getName() override; | ||
|
||
private: | ||
WeightSensor &weightSensor; | ||
UserInput &buttons; | ||
Display &display; | ||
Stopwatch &stopwatch; | ||
|
||
Regression::Approximator approximator; | ||
int32_t targetWeightMg; | ||
long lastEstimatedTime; | ||
|
||
void handleNewWeight(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.