From f412bc60da31863bbd76ecc508e5751249a292a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 13 Mar 2025 08:41:39 +0100 Subject: [PATCH] code clean --- platformio.ini | 2 +- src/Color.cpp | 2 +- src/Color.h | 6 ++-- src/button.h | 4 +-- src/countdown.cpp | 64 +++++++++++------------------------ src/countdown.h | 4 +-- src/display.cpp | 85 +++++++++++++++++++++++++++++++---------------- src/display.h | 12 ++++--- src/main.cpp | 2 +- 9 files changed, 94 insertions(+), 87 deletions(-) diff --git a/platformio.ini b/platformio.ini index 12d1fe8..e81ba0d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -5,4 +5,4 @@ framework = arduino lib_deps = https://github.com/adafruit/Adafruit_NeoPixel upload_port = /dev/ttyUSB0 upload_speed = 460800 -monitor_speed = 115200 \ No newline at end of file +monitor_speed = 115200 diff --git a/src/Color.cpp b/src/Color.cpp index d683d47..ad47a4b 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -1,6 +1,6 @@ #include "Color.h" -#define ___ 0 +#define ___ 0 // NOLINT(*-reserved-identifier) #define QQQ 64 #define TTT 85 #define HHH 127 diff --git a/src/Color.h b/src/Color.h index 5963caa..0dbeca1 100644 --- a/src/Color.h +++ b/src/Color.h @@ -1,12 +1,12 @@ -#ifndef WS2812B_COLOR_H -#define WS2812B_COLOR_H +#ifndef SPORTTAFEL_COLOR_H +#define SPORTTAFEL_COLOR_H #include struct Color { uint8_t r, g, b; - Color(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) { + Color(const uint8_t r, const uint8_t g, const uint8_t b) : r(r), g(g), b(b) { // } diff --git a/src/button.h b/src/button.h index 01c97b3..8d70417 100644 --- a/src/button.h +++ b/src/button.h @@ -1,5 +1,5 @@ -#ifndef WS2812B_BUTTON_H -#define WS2812B_BUTTON_H +#ifndef SPORTTAFEL_BUTTON_H +#define SPORTTAFEL_BUTTON_H void buttonSetup(); diff --git a/src/countdown.cpp b/src/countdown.cpp index 7f9471e..3502abb 100644 --- a/src/countdown.cpp +++ b/src/countdown.cpp @@ -8,9 +8,9 @@ enum State { State state = CONFIG; -long configMillis = 6 * 60 * 1000; +long countdownConfig = 6 * 60 * 1000; -long rest = configMillis; +long countdownRest = countdownConfig; unsigned long last = 0; @@ -21,42 +21,18 @@ void updateTime() { const auto now = max(1UL, millis()); if (last != 0) { const auto diff = now - last; - rest -= (long) diff; - if (rest < 0) { - rest = 0; + countdownRest -= static_cast(diff); + if (countdownRest < 0) { + countdownRest = 0; } } last = now; } -void drawDashes() { - displayClear(); - drawChar(0, '-', true, RED); - drawChar(1, '-', true, RED); - drawChar(2, '-', true, RED); - drawChar(3, '-', true, RED); - displayShow(); -} - -void drawWhite() { - displayClear(); - drawChar(0, '8', true, WHITE); - drawChar(1, '8', true, WHITE); - drawDots(WHITE, WHITE, WHITE, WHITE); - drawChar(2, '8', true, WHITE); - drawChar(3, '8', true, WHITE); - displayShow(); -} - -void drawBlack() { - displayClear(); - displayShow(); -} - void drawSequence() { for (int x = 0; x < 3; ++x) { for (int i = 0; i < 2; ++i) { - drawWhite(); + drawAll(WHITE); delay(100); drawBlack(); delay(100); @@ -66,20 +42,20 @@ void drawSequence() { } } -void setState(State newState) { +void setState(const State newState) { if (state == newState) { return; } state = newState; - const char *name; + const char* name; switch (state) { case CONFIG: - rest = configMillis; + countdownRest = countdownConfig; name = "CONFIG"; break; case READY: - rest = configMillis; + countdownRest = countdownConfig; name = "READY"; break; case RUNNING: @@ -103,15 +79,15 @@ void setState(State newState) { void countdownUpdate() { switch (state) { case CONFIG: - drawMillis(rest, MAGENTA); + drawMillis(countdownRest, MAGENTA); break; case READY: - drawMillis(rest, GREEN); + drawMillis(countdownRest, GREEN); break; case RUNNING: { - if (rest > 0) { - const auto color = rest >= 60000 ? WHITE : (rest >= 10000 ? YELLOW : RED); - drawMillis(rest, color); + if (countdownRest > 0) { + const auto color = countdownRest >= 60000 ? WHITE : (countdownRest >= 10000 ? YELLOW : RED); + drawMillis(countdownRest, color); } else { setState(END); setState(READY); @@ -119,7 +95,7 @@ void countdownUpdate() { break; } case PAUSED: - drawMillis(rest, BLUE); + drawMillis(countdownRest, BLUE); break; case END: drawDashes(); @@ -155,9 +131,9 @@ void buttonShortPressed() { void buttonLongPressed() { switch (state) { case CONFIG: { - const auto seconds = (configMillis / 30000) * 30 % (15 * 60) + 30; - configMillis = seconds * 1000; - rest = configMillis; + const auto seconds = (countdownConfig / 30000) * 30 % (15 * 60) + 30; + countdownConfig = seconds * 1000; + countdownRest = countdownConfig; break; } case RUNNING: @@ -171,4 +147,4 @@ void buttonLongPressed() { setState(READY); break; } -} \ No newline at end of file +} diff --git a/src/countdown.h b/src/countdown.h index de0d172..6250864 100644 --- a/src/countdown.h +++ b/src/countdown.h @@ -1,5 +1,5 @@ -#ifndef WS2812B_COUNTDOWN_H -#define WS2812B_COUNTDOWN_H +#ifndef SPORTTAFEL_COUNTDOWN_H +#define SPORTTAFEL_COUNTDOWN_H void countdownSetup(); diff --git a/src/display.cpp b/src/display.cpp index a93fccf..1f3eba8 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1,5 +1,7 @@ #include "display.h" +#include + #define DIGIT_COUNT 4 #define SEGMENTS_PER_DIGIT 7 #define LEDS_PER_SEGMENT 6 @@ -14,23 +16,23 @@ Adafruit_NeoPixel pixels(PIXELS, GPIO, NEO_GRB + NEO_KHZ800); uint8_t CHARS[] = { - 0b11111100, // 0 - 0b00110000, // 1 - 0b01101110, // 2 - 0b01111010, // 3 - 0b10110010, // 4 - 0b11011010, // 5, S - 0b11011110, // 6 - 0b01110000, // 7 - 0b11111110, // 8 - 0b11111010, // 9 - 0b11100110, // P - 0b11110110, // A - 0b10111100, // U - 0b00000010, // - + 0b11111100, // 0 + 0b00110000, // 1 + 0b01101110, // 2 + 0b01111010, // 3 + 0b10110010, // 4 + 0b11011010, // 5, S + 0b11011110, // 6 + 0b01110000, // 7 + 0b11111110, // 8 + 0b11111010, // 9 + 0b11100110, // P + 0b11110110, // A + 0b10111100, // U + 0b00000010, // - }; -int toIndex(char c) { +int toIndex(const char c) { if (c >= '0' && c <= '9') { return c - '0'; } @@ -45,11 +47,12 @@ int toIndex(char c) { return 5; case '-': return 13; + default: + return -1; } - return -1; } -uint8_t mapChar(char c) { +uint8_t mapChar(const char c) { const int index = toIndex(c); if (index >= 0) { return CHARS[index]; @@ -72,11 +75,11 @@ void displayShow() { pixels.show(); } -void drawChar(int digit, char c, const Color& color) { +void drawChar(const int digit, const char c, const Color& color) { drawChar(digit, c, true, color); } -void drawChar(int digit, char c, bool showIfZero, const Color& color) { +void drawChar(const int digit, const char c, const bool showIfZero, const Color& color) { if (c == '0' && !showIfZero) { return; } @@ -97,14 +100,14 @@ void drawChar(int digit, char c, bool showIfZero, const Color& color) { } } -void drawNumber(int digit, int number, bool zero, const Color& color) { +void drawNumber(const int digit, const int number, const bool zero, const Color& color) { const char ten = '0' + (number / 10 % 10); const char one = '0' + (number % 10); drawChar(digit + 0, ten, zero, color); drawChar(digit + 1, one, true, color); } -void drawDot(int dot, const Color& c) { +void drawDot(const int dot, const Color& c) { int index = 2 * SEGMENTS_PER_DIGIT * LEDS_PER_SEGMENT + dot * LEDS_PER_DOT; pixels.setPixelColor(index + 0, c.r, c.g, c.b); pixels.setPixelColor(index + 1, c.r, c.g, c.b); @@ -117,11 +120,11 @@ void drawDots(const Color& bottom, const Color& middleBottom, const Color& middl drawDot(3, top); } -void drawMillisDeci(unsigned long millisTotal, const Color& color) { +void drawMillisDeci(const unsigned long millisTotal, const Color& color) { const auto secondsTotal = millisTotal / 1000; - const auto minutes = (int) secondsTotal / 60; - const auto seconds = (int) secondsTotal % 60; - const auto deci = (int) millisTotal / 100 % 10; + const auto minutes = (int)secondsTotal / 60; + const auto seconds = (int)secondsTotal % 60; + const auto deci = (int)millisTotal / 100 % 10; pixels.clear(); if (minutes > 0) { @@ -138,10 +141,10 @@ void drawMillisDeci(unsigned long millisTotal, const Color& color) { pixels.show(); } -void drawMillis(unsigned long millisTotal, const Color& color) { - const auto secondsTotal = (unsigned long) ceil(millisTotal / 1000.0); - const auto minutes = (int) secondsTotal / 60; - const auto seconds = (int) secondsTotal % 60; +void drawMillis(const unsigned long millisTotal, const Color& color) { + const auto secondsTotal = (unsigned long)ceil(millisTotal / 1000.0); + const auto minutes = (int)secondsTotal / 60; + const auto seconds = (int)secondsTotal % 60; pixels.clear(); if (minutes > 0) { @@ -151,3 +154,27 @@ void drawMillis(unsigned long millisTotal, const Color& color) { drawNumber(2, seconds, minutes > 0, color); pixels.show(); } + +void drawDashes() { + displayClear(); + drawChar(0, '-', true, RED); + drawChar(1, '-', true, RED); + drawChar(2, '-', true, RED); + drawChar(3, '-', true, RED); + displayShow(); +} + +void drawAll(const Color& color) { + displayClear(); + drawChar(0, '8', true, color); + drawChar(1, '8', true, color); + drawDots(color, color, color, color); + drawChar(2, '8', true, color); + drawChar(3, '8', true, color); + displayShow(); +} + +void drawBlack() { + displayClear(); + displayShow(); +} diff --git a/src/display.h b/src/display.h index 713233b..663c172 100644 --- a/src/display.h +++ b/src/display.h @@ -1,7 +1,5 @@ -#ifndef WS2812B_DISPLAY_H -#define WS2812B_DISPLAY_H - -#include +#ifndef SPORTTAFEL_DISPLAY_H +#define SPORTTAFEL_DISPLAY_H #include "Color.h" @@ -23,4 +21,10 @@ void drawMillisDeci(unsigned long millisTotal, const Color& color); void drawMillis(unsigned long millisTotal, const Color& color); +void drawDashes(); + +void drawAll(const Color&); + +void drawBlack(); + #endif diff --git a/src/main.cpp b/src/main.cpp index 64376c9..4787c89 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ -#include "display.h" #include "button.h" #include "countdown.h" +#include "display.h" void setup() { delay(500);