LEDs per gauge added
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
#include <Arduino.h>
|
||||
#include <math.h>
|
||||
#include <FastLED.h>
|
||||
|
||||
static const uint8_t GAUGE_COUNT = 2;
|
||||
|
||||
// LED strip — one shared WS2812B strip, segmented per gauge.
|
||||
// Set LED_DATA_PIN to the digital pin driving the strip data line.
|
||||
// TOTAL_LEDS is computed automatically from gaugePins[].ledCount.
|
||||
static const uint8_t LED_DATA_PIN = 22;
|
||||
|
||||
// For now: commands come over USB serial
|
||||
#define CMD_PORT Serial
|
||||
#define DEBUG_PORT Serial
|
||||
@@ -14,14 +20,20 @@ struct GaugePins {
|
||||
bool dirInverted;
|
||||
bool stepActiveHigh;
|
||||
bool enableActiveLow;
|
||||
uint8_t ledCount; // WS2812B LEDs on this gauge's strip segment (0 if none)
|
||||
};
|
||||
|
||||
GaugePins gaugePins[GAUGE_COUNT] = {
|
||||
// dir, step, en, dirInv, stepHigh, enActiveLow
|
||||
{50, 51, -1, false, true, true}, // Gauge 0
|
||||
{8, 9, -1, true, true, true}, // Gauge 1
|
||||
constexpr GaugePins gaugePins[GAUGE_COUNT] = {
|
||||
// dir, step, en, dirInv, stepHigh, enActiveLow, leds
|
||||
{50, 51, -1, false, true, true, 6}, // Gauge 0
|
||||
{8, 9, -1, true, true, true, 6}, // Gauge 1
|
||||
};
|
||||
|
||||
constexpr uint8_t sumLedCounts(uint8_t i = 0) {
|
||||
return i >= GAUGE_COUNT ? 0 : gaugePins[i].ledCount + sumLedCounts(i + 1);
|
||||
}
|
||||
static const uint8_t TOTAL_LEDS = sumLedCounts();
|
||||
|
||||
enum HomingState : uint8_t {
|
||||
HS_IDLE,
|
||||
HS_START,
|
||||
@@ -39,8 +51,8 @@ struct Gauge {
|
||||
long homingBackoffSteps = 3700; // should exceed reverse travel slightly
|
||||
|
||||
float velocity = 0.0f;
|
||||
float maxSpeed = 8000.0f;
|
||||
float accel = 9000.0f;
|
||||
float maxSpeed = 5000.0f;
|
||||
float accel = 6000.0f;
|
||||
float homingSpeed = 500.0f;
|
||||
|
||||
float stepAccumulator = 0.0f;
|
||||
@@ -61,6 +73,9 @@ struct Gauge {
|
||||
Gauge gauges[GAUGE_COUNT];
|
||||
String rxLine;
|
||||
|
||||
CRGB leds[TOTAL_LEDS];
|
||||
uint8_t gaugeLedOffset[GAUGE_COUNT];
|
||||
|
||||
void sendReply(const String& s) {
|
||||
CMD_PORT.println(s);
|
||||
}
|
||||
@@ -481,6 +496,44 @@ bool parsePing(const String& line) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool parseLedQuery(const String& line) {
|
||||
if (line == "LED?") {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
for (uint8_t j = 0; j < gaugePins[i].ledCount; j++) {
|
||||
const CRGB& c = leds[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;
|
||||
}
|
||||
|
||||
bool parseLed(const String& line) {
|
||||
int id, idx, r, g, b;
|
||||
if (sscanf(line.c_str(), "LED %d %d %d %d %d", &id, &idx, &r, &g, &b) == 5) {
|
||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||
if (idx < 0 || idx >= gaugePins[id].ledCount) { sendReply("ERR BAD_IDX"); return true; }
|
||||
leds[gaugeLedOffset[id] + idx] = CRGB(constrain(r, 0, 255),
|
||||
constrain(g, 0, 255),
|
||||
constrain(b, 0, 255));
|
||||
FastLED.show();
|
||||
sendReply("OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void processLine(const String& line) {
|
||||
if (parseSet(line)) return;
|
||||
if (parseSpeed(line)) return;
|
||||
@@ -490,6 +543,8 @@ void processLine(const String& line) {
|
||||
if (parseHome(line)) return;
|
||||
if (parseSweep(line)) return;
|
||||
if (parsePosQuery(line)) return;
|
||||
if (parseLedQuery(line)) return;
|
||||
if (parseLed(line)) return;
|
||||
if (parsePing(line)) return;
|
||||
|
||||
sendReply("ERR BAD_CMD");
|
||||
@@ -534,6 +589,16 @@ void setup() {
|
||||
gauges[i].lastUpdateMicros = micros();
|
||||
}
|
||||
|
||||
// Compute per-gauge LED offsets and initialise the strip.
|
||||
uint8_t ledOff = 0;
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
gaugeLedOffset[i] = ledOff;
|
||||
ledOff += gaugePins[i].ledCount;
|
||||
}
|
||||
FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, TOTAL_LEDS);
|
||||
FastLED.setBrightness(255);
|
||||
FastLED.show();
|
||||
|
||||
requestHomeAll();
|
||||
|
||||
DEBUG_PORT.println("READY");
|
||||
@@ -546,4 +611,4 @@ void loop() {
|
||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||
updateGauge(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user