From 380363af4b896180de76eecfc97196f7a4e62ca6 Mon Sep 17 00:00:00 2001 From: boerge1 Date: Mon, 8 Apr 2024 17:12:28 +0200 Subject: [PATCH] Issue_190: Neo Pixel Ring: Add the possibility to have 2 rings --- README.md | 3 +++ TonUINO-TNG.ino | 2 +- src/constants.hpp | 15 ++++++++++++ src/ring.cpp | 27 ++++++++++++++-------- src/ring.hpp | 59 ++++++++++++++++++++++++++++++++++++++++------- src/tonuino.cpp | 6 ++++- 6 files changed, 92 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c8f0f38..0e82b02 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,9 @@ Die SD Karte (Ordner mp3 und advert) hat sich gegenüber der Version 3.1.6 geän # Change Log +## Version 3.1.7 (08.04.2024) +- [Issue 190](https://github.com/tonuino/TonUINO-TNG/issues/190): Neo Pixel Ring: Add the possibility to have 2 rings remains + ## Version 3.1.7 (29.03.2024) - [Issue 184](https://github.com/tonuino/TonUINO-TNG/issues/184): #define DONT_ACCEPT_SAME_RFID_TWICE makes the error: 'class Tonuino' has no member named 'getCard' - [Issue 181](https://github.com/tonuino/TonUINO-TNG/issues/181): Implement battery voltage measurement diff --git a/TonUINO-TNG.ino b/TonUINO-TNG.ino index 1e5f779..6a366a1 100644 --- a/TonUINO-TNG.ino +++ b/TonUINO-TNG.ino @@ -35,7 +35,7 @@ void setup() LOG(init_log, s_error, F("TonUINO Version 3.1 - refactored by Boerge1\n")); LOG(init_log, s_error, F("created by Thorsten Voß and licensed under GNU/GPL.")); LOG(init_log, s_error, F("Information and contribution at https://tonuino.de.\n")); - LOG(init_log, s_error, F("V3.1.7 29.03.24\n")); + LOG(init_log, s_error, F("V3.1.7 08.04.24\n")); Tonuino::getTonuino().setup(); } diff --git a/src/constants.hpp b/src/constants.hpp index 52468fb..9742677 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -104,6 +104,11 @@ /* uncomment one of the below lines to support a special chip on the DfMiniMp3 player * um einen speziellen Chip auf dem DfMiniMp3 Player zu ünterstützen bitte in eine der nächste Zeilen den Kommentar entfernen + * + * GD3200B: bad behavior of getFolderTrackCount() - ignores the parameter folder + * MH2024K16SS: no checksums + * LISP3: bad behavior of callback OnPlayFinished - it is also called on advertise tracks (also on some MH2024K24SS) + * LKP Player: no ACK for requests (use Mp3ChipIncongruousNoAck for them) */ //#define DFMiniMp3_T_CHIP_GD3200B //#define DFMiniMp3_T_CHIP_MH2024K16SS @@ -162,12 +167,22 @@ inline constexpr uint8_t potiPin = A3 ; // AiO/Classic A3 */ //#define NEO_RING //#define NEO_RING_EXT +//#define NEO_RING_2 + #ifdef ALLinONE_Plus inline constexpr uint8_t neoPixelRingPin = 10; // PB2 on AiOplus (Erweiterungsleiste (Female)) #else inline constexpr uint8_t neoPixelRingPin = 5; // D5 on AiO/Classic #endif // ALLinONE_Plus inline constexpr uint8_t neoPixelNumber = 24; // Total Number of Pixels +#ifdef NEO_RING_2 +#ifdef ALLinONE_Plus +inline constexpr uint8_t neoPixelRingPin2= 14; // PC0 on AiOplus (Erweiterungsleiste (Female)) +#else +inline constexpr uint8_t neoPixelRingPin2= 2; // D2 on AiO/Classic (only Every) +#endif // ALLinONE_Plus +inline constexpr uint8_t neoPixelNumber2 = 24; // Total Number of Pixels +#endif // NEO_RING_2 // ###################################################################### diff --git a/src/ring.cpp b/src/ring.cpp index 101eae0..475f016 100644 --- a/src/ring.cpp +++ b/src/ring.cpp @@ -4,38 +4,45 @@ #include -Ring::Ring() -: strip(neoPixelNumber, neoPixelRingPin, NEO_GRB + NEO_KHZ800) -{} +OneRing::OneRing(uint8_t pin, direction dir) +: brightness_pulse{(dir==direction::cw) ? brightness_pulse_min : brightness_pulse_max} +, dir{dir} +, strip{neoPixelNumber, pin, NEO_GRB + NEO_KHZ800} +{ + if (dir==direction::ccw) { + brightness_pulse = brightness_pulse_max; + brightness_inc *= -1; + } +} -void Ring::init() { +void OneRing::init() { strip.begin(); strip.setBrightness(brightness); } -void Ring::pulse(const color_t color) { +void OneRing::pulse(const color_t color) { brightness_pulse += brightness_inc; - if (brightness_pulse >= 200 or brightness_pulse <= 50) + if (brightness_pulse >= brightness_pulse_max or brightness_pulse <= brightness_pulse_min) brightness_inc *= -1; setAll(color*brightness_pulse); } // Rainbow cycle along whole strip. -void Ring::rainbow(uint8_t incr){ +void OneRing::rainbow(uint8_t incr){ setAll([this](uint8_t i){ return wheel((255/neoPixelNumber * (neoPixelNumber-1-i) + pixelCycle) & 255); }); pixelCycle += incr; } -void Ring::setAll(const color_t color) { +void OneRing::setAll(const color_t color) { for (uint8_t i = 0; i < neoPixelNumber; i++) { setPixel(i, color); } showStrip(); } -void Ring::level(uint8_t l) { +void OneRing::level(uint8_t l) { const uint8_t last = static_cast(l) * neoPixelNumber / 0xff; setAll([last, this](uint8_t i) { return (i <= last) ? wheel(static_cast(i)*100/neoPixelNumber) : black;} @@ -45,7 +52,7 @@ void Ring::level(uint8_t l) { // Input a value 0 to 255 to get a color value. // The colors are a transition r - g - b - back to r. -Ring::color_t Ring::wheel(byte WheelPos) const { +OneRing::color_t OneRing::wheel(byte WheelPos) const { color_t color; if (WheelPos == 255) WheelPos = 254; const uint16_t WheelPos_times_3 = (WheelPos % 85) * 3; diff --git a/src/ring.hpp b/src/ring.hpp index 4b84ddc..f1cf096 100644 --- a/src/ring.hpp +++ b/src/ring.hpp @@ -9,12 +9,20 @@ #include inline constexpr uint8_t pulse_per_second = 1; +inline constexpr uint8_t brightness_pulse_max = 200; +inline constexpr uint8_t brightness_pulse_min = 50; + inline constexpr uint8_t brightness_max = 32; inline constexpr uint8_t brightness_init = 16; -class Ring { +enum class direction: uint8_t { + cw, + ccw, +}; + +class OneRing { public: - Ring(); + OneRing(uint8_t pin = neoPixelRingPin, direction dir = direction::cw); void init(); @@ -39,7 +47,7 @@ class Ring { void call_on_idle () { pulse (green); } void call_on_startPlay() { pulse (red ); } void call_on_play () { rainbow (5 ); } - void call_on_quiz () { rainbow (10 ); } + void call_on_game () { rainbow (10 ); } void call_on_pause () { rainbow (0 ); } void call_on_admin () { pulse (blue ); } void call_on_sleep () { setAll (black); } @@ -66,19 +74,54 @@ class Ring { uint8_t brightness { brightness_init }; // for pulse() - uint8_t brightness_pulse { 50 }; - int8_t brightness_inc { cycleTime*255/pulse_per_second/1000 }; - + uint8_t brightness_pulse{ brightness_pulse_min }; + int8_t brightness_inc { cycleTime*255/pulse_per_second/1000 }; // for rainbow() uint8_t pixelCycle { 0 }; // Pattern Pixel Cycle + direction dir; + Adafruit_NeoPixel strip; }; -void Ring::setAll(auto&& f) { +#ifdef NEO_RING_2 +class TwoRings { +public: + TwoRings() + : ring1(neoPixelRingPin) + , ring2(neoPixelRingPin2, direction::ccw) + {} + + void init () { ring1.init ( ); ring2.init ( ); } + + void call_on_startup () { ring1.call_on_startup ( ); ring2.call_on_startup ( ); } + void call_on_idle () { ring1.call_on_idle ( ); ring2.call_on_idle ( ); } + void call_on_startPlay () { ring1.call_on_startPlay ( ); ring2.call_on_startPlay ( ); } + void call_on_play () { ring1.call_on_play ( ); ring2.call_on_play ( ); } + void call_on_game () { ring1.call_on_game ( ); ring2.call_on_game ( ); } + void call_on_pause () { ring1.call_on_pause ( ); ring2.call_on_pause ( ); } + void call_on_admin () { ring1.call_on_admin ( ); ring2.call_on_admin ( ); } + void call_on_sleep () { ring1.call_on_sleep ( ); ring2.call_on_sleep ( ); } + void call_on_volume(uint8_t v) { ring1.call_on_volume (v); ring2.call_on_volume (v); } + void call_on_sleep_timer () { ring1.call_on_sleep_timer( ); ring2.call_on_sleep_timer( ); } + void call_before_sleep(uint8_t r) { ring1.call_before_sleep (r); ring2.call_before_sleep (r); } + void brightness_up () { ring1.brightness_up ( ); ring2.brightness_up ( ); } + void brightness_down () { ring1.brightness_down ( ); ring2.brightness_down ( ); } + +private: + OneRing ring1; + OneRing ring2; +}; +using Ring = TwoRings; +#else +using Ring = OneRing; +#endif // NEO_RING_2 + +void OneRing::setAll(auto&& f) { for (uint8_t i = 0; i < neoPixelNumber; ++i) { - setPixel(i, f(i)); + uint8_t fi = (dir == direction::cw) ? i : neoPixelNumber - i - 1; + setPixel(i, f(fi)); } showStrip(); } diff --git a/src/tonuino.cpp b/src/tonuino.cpp index 8952ece..00879e4 100644 --- a/src/tonuino.cpp +++ b/src/tonuino.cpp @@ -157,8 +157,12 @@ void Tonuino::loop() { ring.call_on_pause(); #ifdef QUIZ_GAME else if (SM_tonuino::is_in_state()) - ring.call_on_quiz(); + ring.call_on_game(); #endif // QUIZ_GAME +#ifdef MEMORY_GAME + else if (SM_tonuino::is_in_state()) + ring.call_on_game(); +#endif // MEMORY_GAME else // admin menu ring.call_on_admin(); #endif // NEO_RING