LEDs removed from Gaugecontroller.ino, backup in aptly named directory
This commit is contained in:
@@ -1,228 +1,13 @@
|
||||
#include <Arduino.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <FastLED.h>
|
||||
|
||||
static const uint8_t GAUGE_COUNT = 4;
|
||||
|
||||
// Backlight/status LEDs and indicator LEDs use separate data strips because
|
||||
// their LED chipsets are not compatible on one chain. The command protocol
|
||||
// still exposes one logical LED segment per gauge.
|
||||
static const uint8_t LED_DATA_PIN = 22;
|
||||
static const uint8_t INDICATOR_LED_DATA_PIN = 36;
|
||||
static const uint8_t BREATHE_FRAME_MS = 16;
|
||||
|
||||
// For now, command and debug traffic share the same serial port.
|
||||
#define CMD_PORT Serial1
|
||||
#define DEBUG_PORT Serial1
|
||||
#define CMD_PORT Serial
|
||||
#define DEBUG_PORT Serial
|
||||
static const unsigned long SERIAL_BAUD = 38400;
|
||||
|
||||
namespace vfd {
|
||||
|
||||
constexpr uint8_t kDataPin = 46;
|
||||
constexpr uint8_t kClockPin = 47;
|
||||
constexpr uint8_t kLatchPin = 44;
|
||||
constexpr int8_t kBlankPin = 45; // Set to -1 if BL/OE is not connected
|
||||
constexpr bool kBlankActiveHigh = true;
|
||||
|
||||
constexpr unsigned long kDigitHoldMicros = 2000;
|
||||
constexpr uint8_t kDigitCount = 4;
|
||||
constexpr uint8_t kSegmentCount = 7;
|
||||
constexpr uint8_t kDriverBits = 20;
|
||||
|
||||
constexpr uint8_t kSegmentStartBit = 0; // HVOut1 -> bit 0
|
||||
constexpr uint8_t kPointSegmentBit = 7; // HVOut8 -> bit 7
|
||||
constexpr uint8_t kBellSegmentBit = 8; // HVOut9 -> bit 8
|
||||
constexpr uint8_t kGridStartBit = 9; // HVOut10 -> bit 9
|
||||
constexpr uint8_t kIndicatorGridBit = 13; // HVOut14 -> bit 13
|
||||
|
||||
char displayBuffer[kDigitCount] = {' ', ' ', ' ', ' '};
|
||||
bool pointEnabled = false;
|
||||
bool bellEnabled = false;
|
||||
uint8_t currentPhase = 0;
|
||||
|
||||
uint8_t encodeCharacter(char c) {
|
||||
switch (c) {
|
||||
case '0': return 0b0111111;
|
||||
case '1': return 0b0000110;
|
||||
case '2': return 0b1011011;
|
||||
case '3': return 0b1001111;
|
||||
case '4': return 0b1100110;
|
||||
case '5': return 0b1101101;
|
||||
case '6': return 0b1111101;
|
||||
case '7': return 0b0000111;
|
||||
case '8': return 0b1111111;
|
||||
case '9': return 0b1101111;
|
||||
case 'A':
|
||||
case 'a': return 0b1110111;
|
||||
case 'B':
|
||||
case 'b': return 0b1111100;
|
||||
case 'C':
|
||||
case 'c': return 0b0111001;
|
||||
case 'D':
|
||||
case 'd': return 0b1011110;
|
||||
case 'E':
|
||||
case 'e': return 0b1111001;
|
||||
case 'F':
|
||||
case 'f': return 0b1110001;
|
||||
case '-': return 0b1000000;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void shiftDriverWord(uint32_t word) {
|
||||
digitalWrite(kLatchPin, HIGH);
|
||||
digitalWrite(kClockPin, HIGH);
|
||||
|
||||
for (int8_t bit = kDriverBits - 1; bit >= 0; --bit) {
|
||||
digitalWrite(kDataPin, (word >> bit) & 0x1U ? HIGH : LOW);
|
||||
digitalWrite(kClockPin, LOW);
|
||||
digitalWrite(kClockPin, HIGH);
|
||||
}
|
||||
|
||||
digitalWrite(kLatchPin, LOW);
|
||||
digitalWrite(kLatchPin, HIGH);
|
||||
}
|
||||
|
||||
void setBlanked(bool blanked) {
|
||||
if (kBlankPin < 0) return;
|
||||
|
||||
const bool level = kBlankActiveHigh ? blanked : !blanked;
|
||||
digitalWrite(kBlankPin, level ? HIGH : LOW);
|
||||
}
|
||||
|
||||
void writeText(const char* text) {
|
||||
for (uint8_t i = 0; i < kDigitCount; ++i) {
|
||||
displayBuffer[i] = ' ';
|
||||
}
|
||||
|
||||
size_t len = strlen(text);
|
||||
if (len > kDigitCount) {
|
||||
text += len - kDigitCount;
|
||||
len = kDigitCount;
|
||||
}
|
||||
|
||||
const uint8_t start = kDigitCount - len;
|
||||
for (uint8_t i = 0; i < len; ++i) {
|
||||
displayBuffer[start + i] = text[i];
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
writeText("");
|
||||
pointEnabled = false;
|
||||
bellEnabled = false;
|
||||
}
|
||||
|
||||
bool parseCommand(const String& command) {
|
||||
char displayText[16];
|
||||
size_t inputIndex = 0;
|
||||
size_t displayIndex = 0;
|
||||
|
||||
if (command.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (command[inputIndex] == '-') {
|
||||
if (displayIndex + 1 >= sizeof(displayText)) {
|
||||
return false;
|
||||
}
|
||||
displayText[displayIndex++] = command[inputIndex++];
|
||||
}
|
||||
|
||||
const size_t digitStart = inputIndex;
|
||||
while (inputIndex < static_cast<size_t>(command.length()) &&
|
||||
isxdigit(static_cast<unsigned char>(command[inputIndex]))) {
|
||||
if (displayIndex + 1 >= sizeof(displayText)) {
|
||||
return false;
|
||||
}
|
||||
displayText[displayIndex++] = toupper(static_cast<unsigned char>(command[inputIndex]));
|
||||
++inputIndex;
|
||||
}
|
||||
|
||||
if (inputIndex == digitStart) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool newPointEnabled = false;
|
||||
bool newBellEnabled = false;
|
||||
while (inputIndex < static_cast<size_t>(command.length())) {
|
||||
if (command[inputIndex] == '.') {
|
||||
newPointEnabled = true;
|
||||
} else if (command[inputIndex] == '!') {
|
||||
newBellEnabled = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
++inputIndex;
|
||||
}
|
||||
|
||||
displayText[displayIndex] = '\0';
|
||||
writeText(displayText);
|
||||
pointEnabled = newPointEnabled;
|
||||
bellEnabled = newBellEnabled;
|
||||
return true;
|
||||
}
|
||||
|
||||
void renderDigit(uint8_t digitIndex) {
|
||||
uint32_t word = 0;
|
||||
const uint8_t segments = encodeCharacter(displayBuffer[digitIndex]);
|
||||
|
||||
for (uint8_t segment = 0; segment < kSegmentCount; ++segment) {
|
||||
if ((segments >> segment) & 0x1U) {
|
||||
word |= (1UL << (kSegmentStartBit + segment));
|
||||
}
|
||||
}
|
||||
|
||||
word |= (1UL << (kGridStartBit + digitIndex));
|
||||
shiftDriverWord(word);
|
||||
}
|
||||
|
||||
void renderIndicator() {
|
||||
uint32_t word = 1UL << kIndicatorGridBit;
|
||||
if (pointEnabled) {
|
||||
word |= 1UL << kPointSegmentBit;
|
||||
}
|
||||
if (bellEnabled) {
|
||||
word |= 1UL << kBellSegmentBit;
|
||||
}
|
||||
shiftDriverWord(word);
|
||||
}
|
||||
|
||||
void begin() {
|
||||
pinMode(kDataPin, OUTPUT);
|
||||
pinMode(kClockPin, OUTPUT);
|
||||
pinMode(kLatchPin, OUTPUT);
|
||||
if (kBlankPin >= 0) {
|
||||
pinMode(kBlankPin, OUTPUT);
|
||||
}
|
||||
|
||||
digitalWrite(kDataPin, LOW);
|
||||
digitalWrite(kClockPin, HIGH);
|
||||
digitalWrite(kLatchPin, HIGH);
|
||||
setBlanked(true);
|
||||
writeText("0");
|
||||
shiftDriverWord(0);
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
setBlanked(true);
|
||||
if (currentPhase < kDigitCount) {
|
||||
renderDigit(currentPhase);
|
||||
} else if (pointEnabled || bellEnabled) {
|
||||
renderIndicator();
|
||||
} else {
|
||||
shiftDriverWord(0);
|
||||
}
|
||||
setBlanked(false);
|
||||
delayMicroseconds(kDigitHoldMicros);
|
||||
setBlanked(true);
|
||||
|
||||
currentPhase = (currentPhase + 1) % (kDigitCount + 1);
|
||||
}
|
||||
|
||||
} // namespace vfd
|
||||
|
||||
struct GaugePins {
|
||||
uint8_t dirPin;
|
||||
uint8_t stepPin;
|
||||
@@ -230,41 +15,16 @@ struct GaugePins {
|
||||
bool dirInverted;
|
||||
bool stepActiveHigh;
|
||||
bool enableActiveLow;
|
||||
const char* ledOrder; // one char per LED: 'G' = GRB, 'R' = RGB; length defines ledCount
|
||||
};
|
||||
|
||||
constexpr GaugePins gaugePins[GAUGE_COUNT] = {
|
||||
// dir, step, en, dirInv, stepHigh, enActiveLow, ledOrder
|
||||
{48, 49, -1, false, true, true, "RRRGGRR"}, // Gauge 0
|
||||
{8, 9, -1, true, true, true, "GGGRRRR"}, // Gauge 1
|
||||
{52, 53, -1, false, true, true, "GGGRRRR"}, // Gauge 2
|
||||
{50, 51, -1, false, true, true, "GGGRRRR"}, // Gauge 3
|
||||
// dir, step, en, dirInv, stepHigh, enActiveLow
|
||||
{48, 49, -1, false, true, true}, // Gauge 0
|
||||
{8, 9, -1, true, true, true}, // Gauge 1
|
||||
{52, 53, -1, false, true, true}, // Gauge 2
|
||||
{50, 51, -1, false, true, true}, // Gauge 3
|
||||
};
|
||||
|
||||
constexpr uint8_t cstrLen(const char* s) {
|
||||
return *s ? uint8_t(1 + cstrLen(s + 1)) : uint8_t(0);
|
||||
}
|
||||
|
||||
constexpr uint8_t sumLedCounts(uint8_t i = 0) {
|
||||
return i >= GAUGE_COUNT ? 0 : cstrLen(gaugePins[i].ledOrder) + sumLedCounts(i + 1);
|
||||
}
|
||||
static const uint8_t TOTAL_LEDS = sumLedCounts();
|
||||
|
||||
constexpr bool isIndicatorLedIndex(uint8_t localIdx) {
|
||||
return localIdx == 3 || localIdx == 4;
|
||||
}
|
||||
|
||||
constexpr uint8_t countIndicatorLedsForGauge(uint8_t gaugeIdx) {
|
||||
return (cstrLen(gaugePins[gaugeIdx].ledOrder) > 3 ? 1 : 0) +
|
||||
(cstrLen(gaugePins[gaugeIdx].ledOrder) > 4 ? 1 : 0);
|
||||
}
|
||||
|
||||
constexpr uint8_t sumIndicatorLedCounts(uint8_t i = 0) {
|
||||
return i >= GAUGE_COUNT ? 0 : countIndicatorLedsForGauge(i) + sumIndicatorLedCounts(i + 1);
|
||||
}
|
||||
static const uint8_t TOTAL_INDICATOR_LEDS = sumIndicatorLedCounts();
|
||||
static const uint8_t TOTAL_MAIN_LEDS = TOTAL_LEDS - TOTAL_INDICATOR_LEDS;
|
||||
|
||||
enum HomingState : uint8_t {
|
||||
HS_IDLE,
|
||||
HS_START,
|
||||
@@ -301,107 +61,9 @@ struct Gauge {
|
||||
bool sweepTowardMax = true;
|
||||
};
|
||||
|
||||
enum LedFx : uint8_t { FX_BLINK = 0, FX_BREATHE = 1, FX_DFLASH = 2 };
|
||||
|
||||
struct BlinkState {
|
||||
bool active = false;
|
||||
LedFx fx = FX_BLINK;
|
||||
CRGB onColor;
|
||||
unsigned long lastMs = 0;
|
||||
uint16_t onMs = 500;
|
||||
uint16_t offMs = 500;
|
||||
bool currentlyOn = false;
|
||||
uint16_t periodMs = 2000;
|
||||
uint16_t cyclePos = 0;
|
||||
uint8_t dphase = 0;
|
||||
};
|
||||
|
||||
Gauge gauges[GAUGE_COUNT];
|
||||
String rxLine;
|
||||
|
||||
CRGB logicalLeds[TOTAL_LEDS];
|
||||
CRGB mainLeds[TOTAL_MAIN_LEDS];
|
||||
CRGB indicatorLeds[TOTAL_INDICATOR_LEDS];
|
||||
CLEDController* mainLedController = nullptr;
|
||||
CLEDController* indicatorLedController = nullptr;
|
||||
uint8_t gaugeLedOffset[GAUGE_COUNT];
|
||||
uint8_t gaugeLedCount[GAUGE_COUNT];
|
||||
uint8_t gaugeMainLedOffset[GAUGE_COUNT];
|
||||
uint8_t gaugeIndicatorLedOffset[GAUGE_COUNT];
|
||||
BlinkState blinkState[TOTAL_LEDS];
|
||||
bool mainLedsDirty = false;
|
||||
bool indicatorLedsDirty = false;
|
||||
|
||||
// FastLED drives the shared strip as RGB. Each gauge's ledOrder string marks per-LED
|
||||
// type ('R' = RGB, 'G' = GRB); writes to GRB-ordered LEDs pre-swap R and G to compensate.
|
||||
inline bool ledNeedsRgSwap(uint8_t globalIdx) {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
uint8_t off = gaugeLedOffset[i];
|
||||
if (globalIdx >= off && globalIdx < off + gaugeLedCount[i]) {
|
||||
char c = gaugePins[i].ledOrder[globalIdx - off];
|
||||
return c == 'G' || c == 'g';
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline CRGB encodeForStrip(uint8_t globalIdx, CRGB color) {
|
||||
if (ledNeedsRgSwap(globalIdx)) {
|
||||
uint8_t tmp = color.r;
|
||||
color.r = color.g;
|
||||
color.g = tmp;
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
bool ledPhysicalIndex(uint8_t globalIdx, bool& indicatorStrip, uint8_t& physicalIdx) {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
uint8_t off = gaugeLedOffset[i];
|
||||
if (globalIdx < off || globalIdx >= off + gaugeLedCount[i]) continue;
|
||||
|
||||
uint8_t localIdx = globalIdx - off;
|
||||
indicatorStrip = isIndicatorLedIndex(localIdx);
|
||||
if (indicatorStrip) {
|
||||
physicalIdx = gaugeIndicatorLedOffset[i] + (localIdx - 3);
|
||||
} else {
|
||||
physicalIdx = gaugeMainLedOffset[i] + localIdx - (localIdx > 4 ? 2 : 0);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void writeLed(uint8_t globalIdx, CRGB color) {
|
||||
logicalLeds[globalIdx] = color;
|
||||
|
||||
bool indicatorStrip = false;
|
||||
uint8_t physicalIdx = 0;
|
||||
if (!ledPhysicalIndex(globalIdx, indicatorStrip, physicalIdx)) return;
|
||||
|
||||
if (indicatorStrip) {
|
||||
indicatorLeds[physicalIdx] = encodeForStrip(globalIdx, color);
|
||||
indicatorLedsDirty = true;
|
||||
} else {
|
||||
mainLeds[physicalIdx] = encodeForStrip(globalIdx, color);
|
||||
mainLedsDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
inline CRGB readLed(uint8_t globalIdx) {
|
||||
return logicalLeds[globalIdx];
|
||||
}
|
||||
|
||||
void showDirtyLeds() {
|
||||
if (mainLedsDirty && mainLedController != nullptr) {
|
||||
mainLedController->showLeds(255);
|
||||
mainLedsDirty = false;
|
||||
}
|
||||
if (indicatorLedsDirty && indicatorLedController != nullptr) {
|
||||
indicatorLedController->showLeds(255);
|
||||
indicatorLedsDirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Sends one-line command replies back over the control port.
|
||||
//
|
||||
// Serial protocol summary.
|
||||
@@ -416,19 +78,14 @@ void showDirtyLeds() {
|
||||
// HOMEALL
|
||||
// SWEEP <id> <accel> <speed>
|
||||
// POS?
|
||||
// LED?
|
||||
// LED <id> <idx|a-b> <r> <g> <b>
|
||||
// BLINK <id> <idx|a-b> <on_ms> <off_ms> [<r> <g> <b>]
|
||||
// BREATHE <id> <idx|a-b> <period_ms> <r> <g> <b>
|
||||
// DFLASH <id> <idx|a-b> <r> <ig> <b>
|
||||
// VFD <text[.!]>
|
||||
// CFG?
|
||||
// PING
|
||||
//
|
||||
// Controller -> host replies / events:
|
||||
// READY
|
||||
// Sent once from setup() after boot completes.
|
||||
// OK
|
||||
// Sent after a valid mutating command, and after POS?/LED? once all data lines
|
||||
// Sent after a valid mutating command, and after POS?/CFG? once all data lines
|
||||
// for that query have been emitted.
|
||||
// PONG
|
||||
// Sent in response to PING.
|
||||
@@ -442,16 +99,10 @@ void showDirtyLeds() {
|
||||
// Sent by SPEED when the requested speed is <= 0.
|
||||
// ERR BAD_ACCEL
|
||||
// Sent by ACCEL when the requested acceleration is <= 0.
|
||||
// ERR BAD_IDX
|
||||
// Sent by LED/BLINK/BREATHE/DFLASH when an LED index or range is invalid.
|
||||
// ERR BAD_TIME
|
||||
// Sent by BLINK/BREATHE when the timing parameter is invalid.
|
||||
// ERR BAD_VFD
|
||||
// Sent by VFD when the text payload is malformed.
|
||||
// POS <id> <currentPos> <targetPos> <homed> <homingState> <sweepEnabled>
|
||||
// Emitted once per gauge before the trailing OK reply to POS?.
|
||||
// LED <id> <idx> <r> <g> <b>
|
||||
// Emitted once per configured LED before the trailing OK reply to LED?.
|
||||
// CFG <id> <maxSpeed> <accel>
|
||||
// Emitted once per gauge before the trailing OK reply to CFG?.
|
||||
// HOMED <id>
|
||||
// Debug event printed on DEBUG_PORT when a homing sequence settles successfully.
|
||||
void sendReply(const String& s) {
|
||||
@@ -924,241 +575,6 @@ bool parsePing(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `VFD <text>` where <text> is up to four hex characters with optional `.` and `!` suffixes.
|
||||
// Replies: `OK`, `ERR BAD_VFD`.
|
||||
bool parseVfd(const String& line) {
|
||||
if (line == "VFD") {
|
||||
vfd::clear();
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!line.startsWith("VFD ")) return false;
|
||||
|
||||
const String payload = line.substring(4);
|
||||
if (payload.length() == 0) {
|
||||
vfd::clear();
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vfd::parseCommand(payload)) {
|
||||
sendReply("OK");
|
||||
} else {
|
||||
sendReply("ERR BAD_VFD");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Answers `LED?` with the current RGB values for every configured LED.
|
||||
// Emits one `LED <id> <idx> <r> <g> <b>` line per configured LED, then replies `OK`.
|
||||
bool parseLedQuery(const String& line) {
|
||||
if (line == "LED?") {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
for (uint8_t j = 0; j < gaugeLedCount[i]; j++) {
|
||||
CRGB c = readLed(gaugeLedOffset[i] + j);
|
||||
CMD_PORT.print("LED ");
|
||||
CMD_PORT.print(i);
|
||||
CMD_PORT.print(' ');
|
||||
CMD_PORT.print(j);
|
||||
CMD_PORT.print(' ');
|
||||
CMD_PORT.print(c.r);
|
||||
CMD_PORT.print(' ');
|
||||
CMD_PORT.print(c.g);
|
||||
CMD_PORT.print(' ');
|
||||
CMD_PORT.println(c.b);
|
||||
}
|
||||
}
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `LED <id> <idx|a-b> <r> <g> <b>` and writes static colours.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`.
|
||||
bool parseLed(const String& line) {
|
||||
int id, r, g, b;
|
||||
char idxToken[16];
|
||||
if (sscanf(line.c_str(), "LED %d %15s %d %d %d", &id, idxToken, &r, &g, &b) == 5) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
int idxFirst = atoi(idxToken);
|
||||
int idxLast = dash ? atoi(dash + 1) : idxFirst;
|
||||
if (idxFirst < 0 || idxLast >= gaugeLedCount[id] || idxFirst > idxLast) {
|
||||
sendReply("ERR BAD_IDX"); return true;
|
||||
}
|
||||
CRGB color(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255));
|
||||
for (int i = idxFirst; i <= idxLast; i++) {
|
||||
blinkState[gaugeLedOffset[id] + i].active = false;
|
||||
writeLed(gaugeLedOffset[id] + i, color);
|
||||
}
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parses `BLINK ...` and assigns a simple on/off effect to one LED or a range.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`, `ERR BAD_TIME`.
|
||||
bool parseBlink(const String& line) {
|
||||
int id, onMs, offMs, r, g, b;
|
||||
char idxToken[16];
|
||||
// Optional RGB values let BLINK either reuse or replace the current colour.
|
||||
int count = sscanf(line.c_str(), "BLINK %d %15s %d %d %d %d %d",
|
||||
&id, idxToken, &onMs, &offMs, &r, &g, &b);
|
||||
if (count != 4 && count != 7) return false;
|
||||
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
int idxFirst = atoi(idxToken);
|
||||
int idxLast = dash ? atoi(dash + 1) : idxFirst;
|
||||
if (idxFirst < 0 || idxLast >= gaugeLedCount[id] || idxFirst > idxLast) {
|
||||
sendReply("ERR BAD_IDX"); return true;
|
||||
}
|
||||
|
||||
if (onMs == 0 && offMs == 0) {
|
||||
for (int i = idxFirst; i <= idxLast; i++)
|
||||
blinkState[gaugeLedOffset[id] + i].active = false;
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
if (onMs <= 0 || offMs <= 0) { sendReply("ERR BAD_TIME"); return true; }
|
||||
|
||||
CRGB color = (count == 7)
|
||||
? CRGB(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255))
|
||||
: CRGB(0, 0, 0); // Placeholder; replaced with the live LED colour below.
|
||||
|
||||
unsigned long nowMs = millis();
|
||||
for (int i = idxFirst; i <= idxLast; i++) {
|
||||
uint8_t globalIdx = gaugeLedOffset[id] + i;
|
||||
BlinkState& bs = blinkState[globalIdx];
|
||||
bs.fx = FX_BLINK;
|
||||
bs.onColor = (count == 7) ? color : readLed(globalIdx);
|
||||
bs.onMs = (uint16_t)onMs;
|
||||
bs.offMs = (uint16_t)offMs;
|
||||
bs.currentlyOn = true;
|
||||
bs.lastMs = nowMs;
|
||||
bs.active = true;
|
||||
writeLed(globalIdx, bs.onColor);
|
||||
}
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses `BREATHE ...` and assigns a triangle-wave fade effect.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`, `ERR BAD_TIME`.
|
||||
bool parseBreathe(const String& line) {
|
||||
int id, periodMs, r, g, b;
|
||||
char idxToken[16];
|
||||
if (sscanf(line.c_str(), "BREATHE %d %15s %d %d %d %d",
|
||||
&id, idxToken, &periodMs, &r, &g, &b) != 6) return false;
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
int idxFirst = atoi(idxToken);
|
||||
int idxLast = dash ? atoi(dash + 1) : idxFirst;
|
||||
if (idxFirst < 0 || idxLast >= gaugeLedCount[id] || idxFirst > idxLast) {
|
||||
sendReply("ERR BAD_IDX"); return true;
|
||||
}
|
||||
if (periodMs <= 0) { sendReply("ERR BAD_TIME"); return true; }
|
||||
CRGB color(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255));
|
||||
unsigned long nowMs = millis();
|
||||
for (int i = idxFirst; i <= idxLast; i++) {
|
||||
uint8_t gi = gaugeLedOffset[id] + i;
|
||||
BlinkState& bs = blinkState[gi];
|
||||
bs.fx = FX_BREATHE;
|
||||
bs.onColor = color;
|
||||
bs.periodMs = (uint16_t)constrain(periodMs, 100, 30000);
|
||||
bs.cyclePos = 0;
|
||||
bs.lastMs = nowMs;
|
||||
bs.active = true;
|
||||
writeLed(gi, CRGB::Black);
|
||||
}
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parses `DFLASH ...` and assigns the double-flash pattern.
|
||||
// Replies: `OK`, `ERR BAD_ID`, `ERR BAD_IDX`.
|
||||
bool parseDflash(const String& line) {
|
||||
int id, r, g, b;
|
||||
char idxToken[16];
|
||||
if (sscanf(line.c_str(), "DFLASH %d %15s %d %d %d",
|
||||
&id, idxToken, &r, &g, &b) != 5) return false;
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
char* dash = strchr(idxToken, '-');
|
||||
int idxFirst = atoi(idxToken);
|
||||
int idxLast = dash ? atoi(dash + 1) : idxFirst;
|
||||
if (idxFirst < 0 || idxLast >= gaugeLedCount[id] || idxFirst > idxLast) {
|
||||
sendReply("ERR BAD_IDX"); return true;
|
||||
}
|
||||
CRGB color(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255));
|
||||
unsigned long nowMs = millis();
|
||||
for (int i = idxFirst; i <= idxLast; i++) {
|
||||
uint8_t gi = gaugeLedOffset[id] + i;
|
||||
BlinkState& bs = blinkState[gi];
|
||||
bs.fx = FX_DFLASH;
|
||||
bs.onColor = color;
|
||||
bs.dphase = 0;
|
||||
bs.lastMs = nowMs;
|
||||
bs.active = true;
|
||||
writeLed(gi, color); // phase 0 = on
|
||||
}
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Advances all active LED effects. writeLed() marks the affected physical strip dirty.
|
||||
void updateBlink() {
|
||||
unsigned long nowMs = millis();
|
||||
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
for (uint8_t j = 0; j < gaugeLedCount[i]; j++) {
|
||||
uint8_t gi = gaugeLedOffset[i] + j;
|
||||
BlinkState& bs = blinkState[gi];
|
||||
if (!bs.active) continue;
|
||||
|
||||
switch (bs.fx) {
|
||||
case FX_BLINK: {
|
||||
uint32_t period = bs.currentlyOn ? bs.onMs : bs.offMs;
|
||||
if ((nowMs - bs.lastMs) >= period) {
|
||||
bs.currentlyOn = !bs.currentlyOn;
|
||||
bs.lastMs = nowMs;
|
||||
writeLed(gi, bs.currentlyOn ? bs.onColor : CRGB::Black);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FX_BREATHE: {
|
||||
unsigned long dt = nowMs - bs.lastMs;
|
||||
if (dt < BREATHE_FRAME_MS) break;
|
||||
uint32_t newPos = (uint32_t)bs.cyclePos + dt;
|
||||
bs.cyclePos = (uint16_t)(newPos % bs.periodMs);
|
||||
bs.lastMs = nowMs;
|
||||
// Triangle wave brightness; frame-limited so breathe remains smooth
|
||||
// without refreshing the LED strips on every service-loop pass.
|
||||
uint16_t half = bs.periodMs >> 1;
|
||||
uint8_t bri = (bs.cyclePos < half)
|
||||
? (uint8_t)((uint32_t)bs.cyclePos * 255 / half)
|
||||
: (uint8_t)((uint32_t)(bs.periodMs - bs.cyclePos) * 255 / half);
|
||||
CRGB scaled = bs.onColor;
|
||||
scaled.nscale8(bri ? bri : 1);
|
||||
writeLed(gi, scaled);
|
||||
break;
|
||||
}
|
||||
case FX_DFLASH: {
|
||||
static const uint16_t dur[4] = {100, 100, 100, 700}; // on, off, on, longer off
|
||||
if ((nowMs - bs.lastMs) >= dur[bs.dphase]) {
|
||||
bs.lastMs = nowMs;
|
||||
bs.dphase = (bs.dphase + 1) & 3;
|
||||
writeLed(gi, (bs.dphase == 0 || bs.dphase == 2) ? bs.onColor : CRGB::Black);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Runs the command parsers in order until one claims the line.
|
||||
// Reply: `ERR BAD_CMD` when no parser accepts the line.
|
||||
void processLine(const String& line) {
|
||||
@@ -1171,12 +587,6 @@ void processLine(const String& line) {
|
||||
if (parseSweep(line)) return;
|
||||
if (parsePosQuery(line)) return;
|
||||
if (parseCfgQuery(line)) return;
|
||||
if (parseLedQuery(line)) return;
|
||||
if (parseLed(line)) return;
|
||||
if (parseBlink(line)) return;
|
||||
if (parseBreathe(line)) return;
|
||||
if (parseDflash(line)) return;
|
||||
if (parseVfd(line)) return;
|
||||
if (parsePing(line)) return;
|
||||
|
||||
sendReply("ERR BAD_CMD");
|
||||
@@ -1204,7 +614,7 @@ void readCommands() {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialises pins, LED bookkeeping and the initial homing cycle.
|
||||
// Initialises stepper pins and the initial homing cycle.
|
||||
// Reply/event: emits `READY` on CMD_PORT once boot is complete.
|
||||
void setup() {
|
||||
DEBUG_PORT.begin(SERIAL_BAUD);
|
||||
@@ -1225,28 +635,6 @@ void setup() {
|
||||
gauges[i].lastUpdateMicros = micros();
|
||||
}
|
||||
|
||||
// Flatten the per-gauge LED counts into logical offsets and separate
|
||||
// physical offsets for the main and indicator strips.
|
||||
uint8_t ledOff = 0;
|
||||
uint8_t mainLedOff = 0;
|
||||
uint8_t indicatorLedOff = 0;
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
gaugeLedCount[i] = cstrLen(gaugePins[i].ledOrder);
|
||||
gaugeLedOffset[i] = ledOff;
|
||||
gaugeMainLedOffset[i] = mainLedOff;
|
||||
gaugeIndicatorLedOffset[i] = indicatorLedOff;
|
||||
ledOff += gaugeLedCount[i];
|
||||
indicatorLedOff += countIndicatorLedsForGauge(i);
|
||||
mainLedOff += gaugeLedCount[i] - countIndicatorLedsForGauge(i);
|
||||
}
|
||||
mainLedController = &FastLED.addLeds<WS2812, LED_DATA_PIN, RGB>(mainLeds, TOTAL_MAIN_LEDS);
|
||||
indicatorLedController = &FastLED.addLeds<WS2812B, INDICATOR_LED_DATA_PIN, RGB>(indicatorLeds, TOTAL_INDICATOR_LEDS);
|
||||
FastLED.setBrightness(255);
|
||||
mainLedController->showLeds(255);
|
||||
indicatorLedController->showLeds(255);
|
||||
|
||||
vfd::begin();
|
||||
|
||||
requestHomeAll();
|
||||
|
||||
DEBUG_PORT.println("READY");
|
||||
@@ -1254,17 +642,11 @@ void setup() {
|
||||
sendReply("READY");
|
||||
}
|
||||
|
||||
// Main service loop: ingest commands, advance effects, move gauges, flush LEDs.
|
||||
// Main service loop: ingest commands and move gauges.
|
||||
void loop() {
|
||||
readCommands();
|
||||
vfd::refresh();
|
||||
updateBlink();
|
||||
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
updateGauge(i);
|
||||
}
|
||||
|
||||
showDirtyLeds();
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user