Added mode switching and indicator animation.

This commit is contained in:
ThePendulum
2026-04-29 02:48:17 +02:00
parent f6a6723f06
commit 58cb843106
2 changed files with 352 additions and 39 deletions

258
.QuadLightsV2.ino Normal file
View File

@@ -0,0 +1,258 @@
#include <Adafruit_NeoPixel.h>
#include <Button.h>
#include <climits>
constexpr int PIN_STRIP = PA7;
constexpr int PIN_BUTTON_LEFT = PA10;
constexpr int PIN_BUTTON_CENTER = PA9;
constexpr int PIN_BUTTON_RIGHT = PA8;
constexpr int LEDS_COUNT = 143;
constexpr int LEDS_SKIP = 130;
constexpr int LEDS_BRIGHTNESS = 20;
// LEFT HEADLIGHT
constexpr int RING_LEFT_RANGE[] = {130, 143};
constexpr int EDGE_LEFT_RANGE[] = {120, 129};
// RIGHT HEADLIGHT
constexpr int RING_RIGHT_RANGE[] = {91, 104};
constexpr int EDGE_RIGHT_RANGE[] = {105, 114};
constexpr int UPDATE_INTERVAL = 33; // about 30fps
const int stripRange[] = {EDGE_RIGHT_RANGE[0], RING_LEFT_RANGE[1]};
const int ringLength = RING_LEFT_RANGE[1] - RING_LEFT_RANGE[0] + 1; // should be symmetrical
const int edgeLength = EDGE_LEFT_RANGE[1] - EDGE_LEFT_RANGE[0] + 1;
Adafruit_NeoPixel strip(stripRange[1], PIN_STRIP, NEO_GRBW + NEO_KHZ800);
Button buttonLeft(PIN_BUTTON_LEFT);
Button buttonCenter(PIN_BUTTON_CENTER);
Button buttonRight(PIN_BUTTON_RIGHT);
// identical strip definitions, right strip is good as any for color
uint32_t colorOff = strip.Color(0, 0, 0, 0);
uint32_t colorIndicator = strip.Color(255, 40, 0, 0);
uint32_t colorPoliceRed = strip.Color(255, 0, 0, 0);
uint32_t colorPoliceBlue = strip.Color(0, 0, 255, 0);
uint32_t colorWhiteFull = strip.Color(255, 255, 255, 255);
uint32_t colorWhiteNative = strip.Color(0, 0, 0, 255);
uint32_t colorWhiteRgb = strip.Color(255, 150, 75, 0);
enum Direction { LEFT, RIGHT };
unsigned long updateLast = 0;
unsigned long currentFrame = 0;
unsigned long animationFrame = 0;
// relying only on start time would turn on the indicator when uptime < indicator duration
bool indicatorLeftActive = false;
bool indicatorRightActive = false;
unsigned long indicatorLeftStart = 0;
unsigned long indicatorRightStart = 0;
bool policeActive = false;
void render() {
strip.show();
}
void clear() {
Serial.println("CLEAR");
for (int ledIndex = 0; ledIndex < stripRange[1]; ledIndex++) {
strip.setPixelColor(ledIndex, colorOff);
}
render();
}
constexpr int INDICATOR_DURATION = 3000; // total ms
constexpr int INDICATOR_HOLD_FRAMES = 15;
constexpr int INDICATOR_OFF_FRAMES = 20;
constexpr float INDICATOR_SPEED = 2;
void animateHeadlight(Direction direction) {
const int* activeRingRange = direction == LEFT
? RING_LEFT_RANGE
: RING_RIGHT_RANGE;
const int* activeEdgeRange = direction == LEFT
? EDGE_LEFT_RANGE
: EDGE_RIGHT_RANGE;
strip.fill(colorWhiteFull, activeRingRange[0], ringLength);
// strip.fill(colorWhiteFull, activeEdgeRange[0], edgeLength);
}
void animateIndicator(Direction direction) {
const int* ringRange = direction == LEFT
? RING_LEFT_RANGE
: RING_RIGHT_RANGE;
const long indicatorFrame = lround(animationFrame * INDICATOR_SPEED) % (ringLength + INDICATOR_HOLD_FRAMES + INDICATOR_OFF_FRAMES);
const long sweepEnd = ringLength;
const long holdEnd = ringLength + INDICATOR_HOLD_FRAMES;
const long sweepIndex = direction == LEFT
? ringRange[0] + (indicatorFrame % ringLength)
: ringRange[1] - (indicatorFrame % ringLength);
for (int ledIndex = ringRange[0]; ledIndex < ringRange[1]; ledIndex++) {
if (indicatorFrame < sweepEnd) {
// SWEEP
const bool isLit = direction == LEFT
? ledIndex < sweepIndex
: ledIndex >= sweepIndex;
strip.setPixelColor(ledIndex, isLit ? colorIndicator : colorOff);
} else if (indicatorFrame < holdEnd) {
// HOLD
strip.setPixelColor(ledIndex, colorIndicator);
} else {
// OFF
strip.setPixelColor(ledIndex, colorOff);
}
}
}
constexpr float POLICE_SPEED = 0.75;
enum PoliceStates { POLICE_LEFT, POLICE_RIGHT, POLICE_OFF };
const PoliceStates policeBeaconFrames[12] = {
POLICE_LEFT,
POLICE_OFF,
POLICE_LEFT,
POLICE_OFF,
POLICE_OFF,
POLICE_OFF,
POLICE_RIGHT,
POLICE_OFF,
POLICE_RIGHT,
POLICE_OFF,
POLICE_OFF,
POLICE_OFF,
};
void animatePolice() {
const long policeFrame = lround(animationFrame * POLICE_SPEED) % 12;
if (policeBeaconFrames[policeFrame] == POLICE_LEFT) {
strip.fill(colorPoliceRed, RING_LEFT_RANGE[0], ringLength);
strip.fill(colorOff, RING_RIGHT_RANGE[0], ringLength);
} else if (policeBeaconFrames[policeFrame] == POLICE_RIGHT) {
strip.fill(colorOff, RING_LEFT_RANGE[0], ringLength);
strip.fill(colorPoliceBlue, RING_RIGHT_RANGE[0], ringLength);
} else {
strip.fill(colorOff, RING_LEFT_RANGE[0], ringLength);
strip.fill(colorOff, RING_RIGHT_RANGE[0], ringLength);
}
if (policeFrame < 6) {
strip.fill(colorOff, EDGE_LEFT_RANGE[0], edgeLength);
strip.fill(colorWhiteFull, EDGE_RIGHT_RANGE[0], edgeLength);
} else {
strip.fill(colorWhiteFull, EDGE_LEFT_RANGE[0], edgeLength);
strip.fill(colorOff, EDGE_RIGHT_RANGE[0], edgeLength);
}
}
void setup() {
strip.begin();
strip.setBrightness(LEDS_BRIGHTNESS);
clear();
buttonLeft.begin();
buttonCenter.begin();
buttonRight.begin();
Serial.begin(9600);
}
void loop() {
unsigned long now = millis();
if (buttonLeft.pressed()) {
Serial.println("BUTTON LEFT");
animationFrame = 0; // ensures animations start cleanly instead of halfway
indicatorLeftActive = true;
indicatorLeftStart = now;
indicatorRightActive = false;
indicatorRightStart = 0;
Serial.println("INDICATOR LEFT");
}
if (buttonCenter.pressed()) {
Serial.println("BUTTON CENTER");
animationFrame = 0;
/*
if (indicatorLeftActive || indicatorRightActive) {
indicatorLeftActive = false;
indicatorLeftStart = 0;
indicatorRightActive = false;
indicatorRightStart = 0;
Serial.println("INDICATOR OFF");
} else {
policeActive = !policeActive;
Serial.print("POLICE ");
Serial.println(policeActive ? "ON" : "OFF");
}
*/
policeActive = !policeActive;
Serial.print("POLICE ");
Serial.println(policeActive ? "ON" : "OFF");
}
if (buttonRight.pressed()) {
Serial.println("BUTTON RIGHT");
animationFrame = 0;
indicatorLeftActive = false;
indicatorLeftStart = 0;
indicatorRightActive = true;
indicatorRightStart = now;
Serial.println("INDICATOR RIGHT");
}
animateHeadlight(LEFT);
animateHeadlight(RIGHT);
if (policeActive) {
animatePolice();
}
if (indicatorLeftActive && now - indicatorLeftStart < INDICATOR_DURATION) {
animateIndicator(LEFT);
}
if (indicatorRightActive && now - indicatorRightStart < INDICATOR_DURATION) {
animateIndicator(RIGHT);
}
if (now - updateLast >= UPDATE_INTERVAL) {
updateLast += UPDATE_INTERVAL;
render();
currentFrame += 1;
animationFrame += 1;
}
}

View File

@@ -59,12 +59,35 @@ Adafruit_NeoPixel ledsHeadLeft(LEDS_HEAD_LENGTH, LEDS_LEFT_PIN, NEO_GRBW + NEO_K
Adafruit_NeoPixel ledsHeadRight(LEDS_HEAD_LENGTH, LEDS_RIGHT_PIN, NEO_GRBW + NEO_KHZ800); Adafruit_NeoPixel ledsHeadRight(LEDS_HEAD_LENGTH, LEDS_RIGHT_PIN, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel ledsDash(LEDS_DASH_LENGTH, LEDS_DASH_PIN, NEO_GRBW + NEO_KHZ800); Adafruit_NeoPixel ledsDash(LEDS_DASH_LENGTH, LEDS_DASH_PIN, NEO_GRBW + NEO_KHZ800);
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_RED = ledsTail.Color(255, 0, 0, 0); const uint32_t COLOR_RED = ledsTail.Color(255, 0, 0, 0);
const uint32_t COLOR_AMBER = ledsTail.Color(255, 50, 0, 0); const uint32_t COLOR_AMBER = ledsTail.Color(255, 50, 0, 0);
const uint32_t COLOR_POLICE_BLUE = ledsTail.Color(0, 0, 255, 0); const uint32_t COLOR_POLICE_BLUE = ledsTail.Color(0, 0, 255, 0);
const uint32_t COLOR_POLICE_RED = ledsTail.Color(255, 0, 0, 0); const uint32_t COLOR_POLICE_RED = ledsTail.Color(255, 0, 0, 0);
enum Direction { LEFT, RIGHT };
enum Mode {
MODE_HEAD,
MODE_POLICE,
MODE_HAZARD,
MODE_RAINBOW,
MODE_COUNT,
};
Mode activeMode = MODE_HEAD;
unsigned long animationFrame = 0;
unsigned long lastFrame = 0;
unsigned long lastPedalPress = millis();
unsigned long indicatorLeftStart = 0;
unsigned long indicatorRightStart = 0;
bool indicatorLeftActive = false;
bool indicatorRightActive = 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);
@@ -110,40 +133,59 @@ void setup() {
ledsDash.clear(); ledsDash.clear();
} }
enum Mode {
MODE_HEAD,
MODE_POLICE,
MODE_HAZARD,
MODE_RAINBOW,
MODE_COUNT,
};
Mode activeMode = MODE_HEAD;
unsigned long animationFrame = 0;
unsigned long lastFrame = 0;
void animateHeadlights() { void animateHeadlights() {
ledsHeadLeft.fill(COLOR_WHITE); ledsHeadLeft.fill(COLOR_WHITE);
ledsHeadRight.fill(COLOR_WHITE); ledsHeadRight.fill(COLOR_WHITE);
ledsDash.fill(ledsDash.Color(0, 255, 0, 0));
} }
void animateIndicator(Adafruit_NeoPixel& ledsTarget) { constexpr int INDICATOR_DURATION = 3000; // total ms
Serial.println("ANIMATING INDICATOR"); constexpr int INDICATOR_HOLD_FRAMES = 15;
constexpr int INDICATOR_OFF_FRAMES = 20;
constexpr float INDICATOR_SPEED = 2;
for (int ledIndex = LEDS_EDGE_LENGTH; ledIndex < LEDS_EDGE_LENGTH + LEDS_RING_LENGTH; ledIndex += 1) { void animateIndicator(Direction direction) {
Adafruit_NeoPixel& ledsTarget = direction == LEFT // reference, not pointer
? ledsHeadLeft
: ledsHeadRight;
const int ringStart = LEDS_EDGE_LENGTH;
const int ringEnd = LEDS_EDGE_LENGTH + LEDS_RING_LENGTH;
const long indicatorFrame =
lround(animationFrame * INDICATOR_SPEED)
% (LEDS_RING_LENGTH + INDICATOR_HOLD_FRAMES + INDICATOR_OFF_FRAMES);
const long sweepEnd = LEDS_RING_LENGTH;
const long holdEnd = LEDS_RING_LENGTH + INDICATOR_HOLD_FRAMES;
const long sweepIndex = direction == LEFT
? ringStart + (indicatorFrame % LEDS_RING_LENGTH)
: ringEnd - (indicatorFrame % LEDS_RING_LENGTH);
for (int ledIndex = ringStart; ledIndex < ringEnd; ledIndex++) {
if (indicatorFrame < sweepEnd) {
// SWEEP
const bool isLit = direction == LEFT
? ledIndex < sweepIndex
: ledIndex >= sweepIndex;
ledsTarget.setPixelColor(ledIndex, isLit ? COLOR_AMBER : COLOR_OFF);
ledsDash.fill(COLOR_AMBER);
} else if (indicatorFrame < holdEnd) {
// HOLD
ledsTarget.setPixelColor(ledIndex, COLOR_AMBER); ledsTarget.setPixelColor(ledIndex, COLOR_AMBER);
} else {
// OFF
ledsTarget.setPixelColor(ledIndex, COLOR_OFF);
} }
} }
unsigned long lastPedalPress = millis(); if (indicatorFrame < holdEnd) {
ledsDash.fill(COLOR_AMBER);
unsigned long lastIndicatingLeft = 0; } else {
unsigned long lastIndicatingRight = 0; ledsDash.fill(COLOR_OFF);
bool isIndicatingLeft = false; }
bool isIndicatingRight = false; }
void animate(long now) { void animate(long now) {
if (pedalForward.read() == Button::PRESSED) { if (pedalForward.read() == Button::PRESSED) {
@@ -164,15 +206,22 @@ void animate(long now) {
animateHeadlights(); animateHeadlights();
// we need the boolean to prevent the indicators from turning on right after boot // we need the boolean to prevent the indicators from turning on right after boot
if (isIndicatingLeft && animationFrame - lastIndicatingLeft < 100) { if (indicatorLeftActive && now - indicatorLeftStart < INDICATOR_DURATION) {
animateIndicator(ledsHeadLeft); animateIndicator(LEFT);
} }
if (isIndicatingRight && animationFrame - lastIndicatingRight < 100) { if (indicatorRightActive && now - indicatorRightStart < INDICATOR_DURATION) {
animateIndicator(ledsHeadRight); animateIndicator(RIGHT);
} }
} }
void render() {
ledsTail.show();
ledsHeadLeft.show();
ledsHeadRight.show();
ledsDash.show();
}
// the loop function runs over and over again forever // the loop function runs over and over again forever
void loop() { void loop() {
const unsigned long now = millis(); const unsigned long now = millis();
@@ -187,15 +236,25 @@ void loop() {
} }
if (buttonLeft.pressed()) { if (buttonLeft.pressed()) {
isIndicatingLeft = true; animationFrame = 0; // ensures animations start cleanly instead of halfway
lastIndicatingLeft = animationFrame;
indicatorLeftActive = true;
indicatorLeftStart = now;
indicatorRightActive = false;
indicatorRightStart = 0;
Serial.println("BUTTON LEFT"); Serial.println("BUTTON LEFT");
} }
if (buttonRight.pressed()) { if (buttonRight.pressed()) {
isIndicatingRight = true; animationFrame = 0; // ensures animations start cleanly instead of halfway
lastIndicatingRight = animationFrame;
indicatorRightActive = true;
indicatorRightStart = now;
indicatorLeftActive = false;
indicatorLeftStart = 0;
Serial.println("BUTTON RIGHT"); Serial.println("BUTTON RIGHT");
} }
@@ -205,11 +264,7 @@ void loop() {
animationFrame += 1; animationFrame += 1;
animate(now); animate(now);
render();
ledsTail.show();
ledsHeadLeft.show();
ledsHeadRight.show();
ledsDash.show();
} }
if (KEEPALIVE_ENABLE && now - lastPedalPress > KEEPALIVE_TIMEOUT) { if (KEEPALIVE_ENABLE && now - lastPedalPress > KEEPALIVE_TIMEOUT) {