Attempted to fix stuck on in reverse. Cleaned up some excessive serial logging and unused declarations. Reading mode switch on release.

This commit is contained in:
ThePendulum
2026-10-20 23:36:50 +02:00
parent b9a6fecae7
commit d14795e216
+37 -36
View File
@@ -9,16 +9,14 @@ constexpr int LEDS_TAIL_LENGTH = 10;
constexpr int LEDS_TAIL_BRIGHTNESS = 255; constexpr int LEDS_TAIL_BRIGHTNESS = 255;
constexpr int LEDS_TAIL_SIDE_LENGTH = 2; constexpr int LEDS_TAIL_SIDE_LENGTH = 2;
constexpr int LEDS_TAIL_SIDE_MARGIN = 2; constexpr int LEDS_TAIL_SIDE_MARGIN = 2;
constexpr int LEDS_TAIL_RIGHT_RANGE[] = {0, LEDS_TAIL_SIDE_LENGTH};
constexpr int LEDS_TAIL_LEFT_RANGE[] = {LEDS_TAIL_LENGTH - LEDS_TAIL_SIDE_LENGTH, LEDS_TAIL_SIDE_LENGTH};
constexpr int LEDS_LEFT_PIN = PA5; constexpr int LEDS_LEFT_PIN = PA5;
constexpr int LEDS_RIGHT_PIN = PA7; constexpr int LEDS_RIGHT_PIN = PA7;
/* default configuration right lamp (left from the front) /* default configuration right lamp (left from the front)
RING EDGE RING EDGE
29 30 31 32 33 34 35 37 0 1 2 3 4 5 6 7 8 9 10 31 32 33 34 35 36 37 38 0 1 2 3 4 5 6 7 8 9 10 11
28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12
*/ */
constexpr int LEDS_TOP_LENGTH = 12; constexpr int LEDS_TOP_LENGTH = 12;
constexpr int LEDS_BOTTOM_LENGTH = 11; constexpr int LEDS_BOTTOM_LENGTH = 11;
@@ -91,9 +89,7 @@ uint32_t colorHSV(float hue, float sat = 1.0, float val = 1.0) {
const uint32_t COLOR_OFF = ledsTail.Color(0, 0, 0, 0); const uint32_t COLOR_OFF = ledsTail.Color(0, 0, 0, 0);
const uint32_t COLOR_WHITE = ledsTail.Color(0, 0, 0, 255); const uint32_t COLOR_WHITE = ledsTail.Color(0, 0, 0, 255);
const uint32_t COLOR_WHITE_FULL = ledsTail.Color(255, 255, 255, 255);
const uint32_t COLOR_WHITE_DIM = ledsTail.Color(0, 0, 0, 100); const uint32_t COLOR_WHITE_DIM = ledsTail.Color(0, 0, 0, 100);
const uint32_t COLOR_RED_DIM = ledsTail.Color(150, 0, 0, 0);
const uint32_t COLOR_RED = ledsTail.Color(255, 0, 0, 0); const uint32_t COLOR_RED = ledsTail.Color(255, 0, 0, 0);
const uint32_t COLOR_AMBER = colorHSV(10); // easier value fade const uint32_t COLOR_AMBER = colorHSV(10); // easier value fade
const uint32_t COLOR_POLICE_BLUE = ledsTail.Color(0, 0, 255, 0); const uint32_t COLOR_POLICE_BLUE = ledsTail.Color(0, 0, 255, 0);
@@ -116,8 +112,6 @@ unsigned long indicatorRightStart = 0;
bool indicatorLeftActive = false; bool indicatorLeftActive = false;
bool indicatorRightActive = false; bool indicatorRightActive = false;
bool policeActive = false;
void setup() { void setup() {
// initialize digital pin LED_BUILTIN as an output. // initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
@@ -315,11 +309,11 @@ void animatePolice() {
} }
if (policeFrame < 6) { if (policeFrame < 6) {
ledsHeadLeft.fill(COLOR_WHITE_DIM, LEDS_EDGE_LENGTH, LEDS_EDGE_LENGTH + LEDS_RING_LENGTH); ledsHeadLeft.fill(COLOR_WHITE_DIM, LEDS_EDGE_LENGTH, LEDS_RING_LENGTH);
ledsHeadRight.fill(COLOR_WHITE, LEDS_EDGE_LENGTH, LEDS_EDGE_LENGTH + LEDS_RING_LENGTH); ledsHeadRight.fill(COLOR_WHITE, LEDS_EDGE_LENGTH, LEDS_RING_LENGTH);
} else { } else {
ledsHeadLeft.fill(COLOR_WHITE, LEDS_EDGE_LENGTH, LEDS_EDGE_LENGTH + LEDS_RING_LENGTH); ledsHeadLeft.fill(COLOR_WHITE, LEDS_EDGE_LENGTH, LEDS_RING_LENGTH);
ledsHeadRight.fill(COLOR_WHITE_DIM, LEDS_EDGE_LENGTH, LEDS_EDGE_LENGTH + LEDS_RING_LENGTH); ledsHeadRight.fill(COLOR_WHITE_DIM, LEDS_EDGE_LENGTH, LEDS_RING_LENGTH);
} }
} }
@@ -469,7 +463,8 @@ void animate(long now) {
animateKitt(); animateKitt();
} }
if (now - lastPedalRelease < 1500) { // lastPedalRelease is 0 until the first release, prevents brake lights right after boot
if (lastPedalRelease > 0 && now - lastPedalRelease < 1500) {
animateBrake(); animateBrake();
} }
@@ -497,11 +492,15 @@ void render() {
bool isOff = false; bool isOff = false;
void shutdown() { void shutdown() {
// digitalWrite(PIN_BUCK, BUCK_OFF); // when STLink or USB prevents full power cut, loop keeps running, only shut down once
if (isOff) {
return;
}
Serial.println("SHUTTING DOWN"); Serial.println("SHUTTING DOWN");
Serial.println("BUCK OFF"); Serial.println("BUCK OFF");
digitalWrite(PIN_BUCK, LOW); digitalWrite(PIN_BUCK, BUCK_OFF);
delay(100); delay(100);
@@ -532,16 +531,19 @@ void loop() {
modePressed = true; modePressed = true;
modePressStart = now; modePressStart = now;
activeMode = (Mode)((activeMode + 1) % MODE_COUNT);
animationFrame = 0; // ensures animations start cleanly instead of halfway
lastButtonPress = now; lastButtonPress = now;
Serial.print("BUTTON MODE ");
Serial.println(activeMode);
} }
// switch mode on release, so a long press for shutdown doesn't switch mode first
if (buttonMode.released()) { if (buttonMode.released()) {
if (modePressed && !isOff) {
activeMode = (Mode)((activeMode + 1) % MODE_COUNT);
animationFrame = 0; // ensures animations start cleanly instead of halfway
Serial.print("BUTTON MODE ");
Serial.println(activeMode);
}
modePressed = false; modePressed = false;
} }
@@ -577,31 +579,30 @@ void loop() {
Serial.println("BUTTON RIGHT"); Serial.println("BUTTON RIGHT");
} }
if (pedalReverse.read() == Button::PRESSED && pedalForward.read() != Button::PRESSED) { // only the pedal counts as activity, forward and reverse inputs can read active while parked
if (pedal.read() == Button::PRESSED) {
lastPedalPress = now; lastPedalPress = now;
isReversing = true;
if (isOff) { if (isOff) {
restart(); restart();
} }
Serial.println("PEDAL REVERSE");
} else {
isReversing = false;
} }
if (pedalForward.read() == Button::PRESSED) { if (pedal.pressed()) {
lastPedalPress = now; Serial.println("PEDAL PRESSED");
}
if (isOff) {
restart();
}
Serial.println("PEDAL FORWARD");
}`3
if (pedal.released()) { if (pedal.released()) {
lastPedalRelease = now; lastPedalRelease = now;
Serial.println("PEDAL RELEASED");
}
const bool wasReversing = isReversing;
isReversing = pedalReverse.read() == Button::PRESSED && pedalForward.read() != Button::PRESSED;
if (isReversing != wasReversing) {
Serial.println(isReversing ? "REVERSE ON" : "REVERSE OFF");
} }
if (!isOff && now - lastFrame >= ANIMATION_INTERVAL) { if (!isOff && now - lastFrame >= ANIMATION_INTERVAL) {