countdown colors + different formats (mm:ss / ss:cc)

This commit is contained in:
Patrick Haßel 2025-03-12 22:12:19 +01:00
parent ef87855b78
commit 1561f48d1a
4 changed files with 78 additions and 31 deletions

View File

@ -1,13 +1,14 @@
#include "Color.h" #include "Color.h"
#define ___ 0 #define ___ 0
#define QQQ 64
#define TTT 85 #define TTT 85
#define HHH 127 #define HHH 127
#define XXX 255 #define XXX 255
// @formatter:off // @formatter:off
Color RED (XXX, ___, ___); Color RED (XXX, ___, ___);
Color YELLOW (HHH, HHH, ___); Color YELLOW (HHH, QQQ, ___);
Color GREEN (___, XXX, ___); Color GREEN (___, XXX, ___);
Color TURQUOISE (___, HHH, HHH); Color TURQUOISE (___, HHH, HHH);
Color BLUE (___, ___, XXX); Color BLUE (___, ___, XXX);

View File

@ -20,6 +20,7 @@ uint8_t CHARS[] = {
0b11100110, // P 0b11100110, // P
0b11110110, // A 0b11110110, // A
0b10111100, // U 0b10111100, // U
0b00000010, // -
}; };
int toIndex(char c) { int toIndex(char c) {
@ -29,6 +30,14 @@ int toIndex(char c) {
switch (c) { switch (c) {
case 'P': case 'P':
return 10; return 10;
case 'A':
return 11;
case 'U':
return 12;
case 'S':
return 5;
case '-':
return 13;
} }
return -1; return -1;
} }
@ -41,17 +50,19 @@ uint8_t mapChar(char c) {
return 0; return 0;
} }
void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, bool showIfZero, Color& color) { void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, const Color& color) {
drawChar(pixels, digit, c, true, color);
}
void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, bool showIfZero, const Color& color) {
if (c == '0' && !showIfZero) { if (c == '0' && !showIfZero) {
return; return;
} }
auto pixel = digit * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT + (digit > DOTS_AFTER_DIGIT ? DOT_COUNT * LEDS_PER_DOT : 0); auto pixel = digit * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT + (digit > DOTS_AFTER_DIGIT ? DOT_COUNT * LEDS_PER_DOT : 0);
const auto symbol = mapChar(c); const auto symbol = mapChar(c);
Serial.printf(" character '%c'\n", c);
uint8_t symbolMask = 128; uint8_t symbolMask = 128;
for (int segment = 0; segment < SEGMENTS_PER_DIGIT; ++segment) { for (int segment = 0; segment < SEGMENTS_PER_DIGIT; ++segment) {
const auto on = (symbol & symbolMask) != 0; const auto on = (symbol & symbolMask) != 0;
Serial.printf(" segment %d = %s\n", segment, on ? "ON" : "__");
if (on) { if (on) {
for (int led = 0; led < LEDS_PER_SEGMENT; ++led) { for (int led = 0; led < LEDS_PER_SEGMENT; ++led) {
pixels.setPixelColor(pixel, color.r, color.g, color.b); pixels.setPixelColor(pixel, color.r, color.g, color.b);
@ -64,7 +75,7 @@ void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, bool showIfZero, Col
} }
} }
void drawNumber(Adafruit_NeoPixel& pixels, int digit, int number, bool zero, Color& color) { void drawNumber(Adafruit_NeoPixel& pixels, int digit, int number, bool zero, const Color& color) {
const char ten = '0' + (number / 10 % 10); const char ten = '0' + (number / 10 % 10);
const char one = '0' + (number % 10); const char one = '0' + (number % 10);
drawChar(pixels, digit + 0, ten, zero, color); drawChar(pixels, digit + 0, ten, zero, color);
@ -73,13 +84,46 @@ void drawNumber(Adafruit_NeoPixel& pixels, int digit, int number, bool zero, Col
void drawDot(Adafruit_NeoPixel& pixels, int dot, const Color& c) { void drawDot(Adafruit_NeoPixel& pixels, int dot, const Color& c) {
int index = 2 * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT + dot * LEDS_PER_DOT; int index = 2 * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT + dot * LEDS_PER_DOT;
pixels.setPixelColor(index++, c.r, c.g, c.b); pixels.setPixelColor(index + 0, c.r, c.g, c.b);
pixels.setPixelColor(index++, c.r, c.g, c.b); pixels.setPixelColor(index + 1, c.r, c.g, c.b);
} }
void drawDots(Adafruit_NeoPixel& pixels, Color& bottom, Color& middleBottom, Color& middleTop, Color& top) { void drawDots(Adafruit_NeoPixel& pixels, const Color& bottom, const Color& middleBottom, const Color& middleTop, const Color& top) {
drawDot(pixels, 0, bottom); drawDot(pixels, 0, bottom);
drawDot(pixels, 1, middleBottom); drawDot(pixels, 1, middleBottom);
drawDot(pixels, 2, middleTop); drawDot(pixels, 2, middleTop);
drawDot(pixels, 3, top); drawDot(pixels, 3, top);
} }
void drawMillis(Adafruit_NeoPixel& pixels, const unsigned long millisTotal, const Color& color) {
const auto secondsTotal = millisTotal / 1000;
const auto minutes = (int) secondsTotal / 60;
const auto seconds = (int) secondsTotal % 60;
if (minutes > 0) {
static auto lastSeconds = -1;
if (lastSeconds == seconds) {
return;
}
lastSeconds = seconds;
Serial.printf("%2d:%02d\n", minutes, seconds);
pixels.clear();
drawNumber(pixels, 0, minutes, false, color);
drawDots(pixels, BLACK, color, color, BLACK);
drawNumber(pixels, 2, seconds, true, color);
pixels.show();
} else {
const auto deci = (int) millisTotal / 100 % 10;
Serial.printf("%2d.%02d\n", seconds, deci);
pixels.clear();
if (seconds > 0) {
drawNumber(pixels, 0, seconds, false, color);
drawDots(pixels, color, BLACK, BLACK, BLACK);
}
drawChar(pixels, 2, '0' + deci, true, color);
pixels.show();
}
}

View File

@ -5,10 +5,14 @@
#include "Color.h" #include "Color.h"
void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, bool showIfZero, Color& color); void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, const Color& color);
void drawNumber(Adafruit_NeoPixel& pixels, int digit, int number, bool zero, Color& color); void drawChar(Adafruit_NeoPixel& pixels, int digit, char c, bool showIfZero, const Color& color);
void drawDots(Adafruit_NeoPixel& pixels, Color& bottom, Color& middleBottom, Color& middleTop, Color& top); void drawNumber(Adafruit_NeoPixel& pixels, int digit, int number, bool zero, const Color& color);
void drawDots(Adafruit_NeoPixel& pixels, const Color& bottom, const Color& middleBottom, const Color& middleTop, const Color& top);
void drawMillis(Adafruit_NeoPixel& pixels, unsigned long millisTotal, const Color& color);
#endif #endif

View File

@ -16,9 +16,9 @@ void setup() {
pixels.show(); pixels.show();
} }
long configCountdownSeconds = 65; long configCountdownMillis = 6 * 60 * 1000;
long countdownRestMillis = configCountdownSeconds * 1000; long rest = configCountdownMillis;
unsigned long last = 0; unsigned long last = 0;
@ -26,32 +26,30 @@ void updateTime() {
const auto now = max(1UL, millis()); const auto now = max(1UL, millis());
if (last != 0) { if (last != 0) {
const auto diff = now - last; const auto diff = now - last;
countdownRestMillis -= (long) diff; rest -= (long) diff;
if (countdownRestMillis < 0) { if (rest < 0) {
countdownRestMillis = 0; rest = 0;
} }
} }
last = now; last = now;
} }
void showTime() { bool running = true;
const auto secondsTotal = (unsigned long) ceil(countdownRestMillis / 1000.0);
const auto minutes = (int) secondsTotal / 60;
const auto seconds = (int) secondsTotal % 60;
static auto lastSeconds = -1; void showTime() {
if (lastSeconds == seconds) { if (!running) {
return; return;
} }
lastSeconds = seconds; if (rest > 0) {
const auto color = rest >= 60000 ? WHITE : (rest >= 10000 ? YELLOW : RED);
Serial.printf("%2d:%02d\n", minutes, seconds); drawMillis(pixels, rest, color);
} else {
pixels.clear(); running = false;
drawNumber(pixels, 0, minutes, false, WHITE); pixels.clear();
drawDots(pixels, BLACK, WHITE, WHITE, BLACK); drawNumber(pixels, 3, 0, true, RED);
drawNumber(pixels, 2, seconds, true, WHITE); pixels.show();
pixels.show(); Serial.println("END");
}
} }
void loop() { void loop() {