259 lines
6.6 KiB
C++
259 lines
6.6 KiB
C++
#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;
|
|
}
|
|
}
|