Skip to content

Commit

Permalink
draw three bars and get settings from struct
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ-Koenig committed May 3, 2024
1 parent 7526403 commit 1c975d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
47 changes: 39 additions & 8 deletions Software/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,30 @@
OSSM *ossm;

// TODO: Move this to a service
bool handlePress = false;
int counter = 0;
bool isDouble = false;
long lastPressed = 0;
static int lastState = HIGH;
static unsigned long fallTime = millis();
static unsigned long riseTime = millis();
static bool handlePress = false;
static bool watchForLongPress = false;
static unsigned long lastPressed = millis();

void IRAM_ATTR encoderPressed() { handlePress = true; }
void IRAM_ATTR handleEncoder() {
int currentState = digitalRead(Pins::Remote::encoderSwitch);

if (currentState == HIGH && lastState == LOW) {
// Pressing down.
riseTime = millis();
fallTime = millis();
watchForLongPress = true;
} else if (currentState == LOW && lastState == HIGH) {
// Releasing Press
riseTime = millis();
handlePress = true;
watchForLongPress = false;
}

lastState = currentState; // Update lastState to the new state
}

void setup() {
/** Board setup */
Expand All @@ -47,13 +65,28 @@ void setup() {
ossm = new OSSM(display, encoder);

attachInterrupt(digitalPinToInterrupt(Pins::Remote::encoderSwitch),
encoderPressed, RISING);
handleEncoder, CHANGE);
};

void loop() {
// TODO: Relocate this code.

// if the encoder is down and has been for 1000ms, don't wait for the rise,
// just trigger a long press.
if (watchForLongPress) {
int currentState = digitalRead(Pins::Remote::encoderSwitch);
if (currentState == HIGH && millis() - fallTime > 1000 &&
millis() - lastPressed > 1000) {
ossm->sm->process_event(LongPress{});
fallTime = millis();
lastPressed = millis();
}
}

if (handlePress) {
handlePress = false;
unsigned long pressTime = riseTime - fallTime;
ESP_LOGD("Encoder", "Press time: %d, %d", pressTime, millis() - lastPressed);

// detect if a double click occurred
if (millis() - lastPressed < 300) {
Expand All @@ -62,7 +95,5 @@ void loop() {
ossm->sm->process_event(ButtonPress{});
}
lastPressed = millis();

ESP_LOGD("Loop", "%sButton Press", isDouble ? "Double " : "");
}
};
2 changes: 2 additions & 0 deletions Software/src/ossm/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace sml = boost::sml;
* They just happen to be defined inside of the OSSM State Machine class.
*/
struct ButtonPress {};
struct LongPress {};

struct DoublePress {};

Expand All @@ -26,6 +27,7 @@ struct Error {};

// Definitions to make the table easier to read.
static auto buttonPress = sml::event<ButtonPress>;
static auto longPress = sml::event<LongPress>;
static auto doublePress = sml::event<DoublePress>;
static auto done = sml::event<Done>;
static auto error = sml::event<Error>;
Expand Down
7 changes: 2 additions & 5 deletions Software/src/ossm/OSSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ class OSSM {
auto drawPlayControls = [](OSSM &o) { o.drawPlayControls(); };
auto drawPreflight = [](OSSM &o) { o.drawPreflight(); };
auto resetSettings = [](OSSM &o) {

o.setting.speed = 0;
o.setting.stroke = 0;
o.setting.depth = 0;
o.setting.sensation = 0;
o.playControl = PlayControls::STROKE;


};

auto incrementControl = [](OSSM &o) {
Expand Down Expand Up @@ -171,13 +168,13 @@ class OSSM {
"simplePenetration"_s [isPreflightSafe] / (resetSettings, drawPlayControls, startSimplePenetration) = "simplePenetration.idle"_s,
"simplePenetration"_s / drawPreflight = "simplePenetration.preflight"_s,
"simplePenetration.preflight"_s + done / (resetSettings, drawPlayControls, startSimplePenetration) = "simplePenetration.idle"_s,
"simplePenetration.idle"_s + doublePress / emergencyStop = "menu"_s,
"simplePenetration.idle"_s + longPress / emergencyStop = "menu"_s,

"strokeEngine"_s [isPreflightSafe] / (resetSettings, drawPlayControls, startStrokeEngine) = "strokeEngine.idle"_s,
"strokeEngine"_s / drawPreflight = "strokeEngine.preflight"_s,
"strokeEngine.preflight"_s + done / (resetSettings, drawPlayControls, startStrokeEngine) = "strokeEngine.idle"_s,
"strokeEngine.idle"_s + buttonPress / incrementControl = "strokeEngine.idle"_s,
"strokeEngine.idle"_s + doublePress / emergencyStop = "menu"_s,
"strokeEngine.idle"_s + longPress / emergencyStop = "menu"_s,

"update"_s [isOnline] / drawUpdate = "update.checking"_s,
"update"_s = "wifi"_s,
Expand Down

0 comments on commit 1c975d0

Please sign in to comment.