From 8edae04556c46aa0645792c6b3c021636bb454ff Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 20 Oct 2023 20:51:54 +0200 Subject: [PATCH] feat: led-lib (#3) --- config.ini | 3 + include/Program.h | 3 + lib/LedLib/include/LedLib.h | 30 ++++++++ lib/LedLib/scr/LedLib.cpp | 77 +++++++++++++++++++ .../include/SwitchableEncodeur.h | 5 ++ .../src/SwitchableEncodeur.cpp | 4 +- platformio.ini | 1 + src/Program.cpp | 4 + 8 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 lib/LedLib/include/LedLib.h create mode 100644 lib/LedLib/scr/LedLib.cpp diff --git a/config.ini b/config.ini index ae6344a..c8e382c 100644 --- a/config.ini +++ b/config.ini @@ -23,3 +23,6 @@ build_flags = -D ENCODER_SWITCH=D5 -D ENCODER_DT=D6 -D ENCODER_CLK=D7 + + -D PIXEL_PIN=4 + -D PIXEL_COUNT=24 diff --git a/include/Program.h b/include/Program.h index 205afc3..3da93f5 100644 --- a/include/Program.h +++ b/include/Program.h @@ -6,6 +6,7 @@ #include "DiscordAPI.h" #include "SwitchableEncodeur.h" +#include "LedLib.h" @@ -32,6 +33,8 @@ private: long oldPosition; DiscordAPI* discord; + + LedLib* ledLib; }; #endif diff --git a/lib/LedLib/include/LedLib.h b/lib/LedLib/include/LedLib.h new file mode 100644 index 0000000..3d13b94 --- /dev/null +++ b/lib/LedLib/include/LedLib.h @@ -0,0 +1,30 @@ +#ifndef NEOLED_H +#define NEOLED_H 0 + +#include + +// Classe LedLib : Gestion de l'allumage des LEDs en fonction du Rotary +// Class LedLib : Manages LEDs colors according to the Rotary +class LedLib{ +public : + LedLib(int pixelCount, int pixelPin, int bright); + + // Fonction pour allumer correctement les LEDs en fonction du Rotary + // Function to manage LEDs colors according to the Rotary + void actLed(int nb); + + // Fonction qui renvoie le nombre de LEDs allumées + // Function that returns the number of LEDs lit + int getledNB(); + + void okBlink(); + +private : + // Le nombre de LEDs allumées + // Amount of LEDs lit + int ledNb; + + Adafruit_NeoPixel* strip; +}; + +#endif diff --git a/lib/LedLib/scr/LedLib.cpp b/lib/LedLib/scr/LedLib.cpp new file mode 100644 index 0000000..05185b6 --- /dev/null +++ b/lib/LedLib/scr/LedLib.cpp @@ -0,0 +1,77 @@ +#include "../include/LedLib.h" + +// Initialisation +// Initialization +LedLib::LedLib(int pixelCount, int pixelPin, int bright){ + this->ledNb = 0; + this->strip = new Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); + + this->strip->begin(); + this->strip->show(); + this->strip->setBrightness(bright); +} + +// Fonction pour allumer correctement les LEDs en fonction du Rotary +// Function to manage LEDs colors according to the Rotary +void LedLib::actLed(int nb){ + this->ledNb = nb; + + // On éteint tout + // We shut everything up + this->strip->clear(); + + // Allumer 0 LED ? Inutile + // Useless to light up 0 LED + if (nb != 0) { + + // Boucle dans les LEDs + // Loop in LEDs + for (int i = 0; i < nb+1; i++) { + + // Une LED sur deux est blanche : heure entière, l'autre rouge pour 30m + // One LED out of two is white: whole hour, the other red for 30m + if (i%2 == 0) { + this->strip->setPixelColor(i-1, 255, 255, 255); + }else{ + this->strip->setPixelColor(i-1, 255, 0, 0); + } + } + } + + // Maintenant on allume + // Then light everything up + this->strip->show(); +} + +// Fonction qui renvoie le nombre de LEDs allumées +// Function that returns the number of LEDs lit +int LedLib::getledNB(){ + return this->ledNb; +} + +void LedLib::okBlink(){ + int tempR; + int tempV; + int tempB; + + for (int i = 0; i < 3*2; i++) { + if(i%2 == 0){ + tempR = 0; + tempV = 0; + tempB = 0; + + }else{ + tempR = 75; + tempV = 181; + tempB = 67; + } + + for (int j = 0; j < 24; j++) { + // On fait clignoter les LEDs pour confirmer + // We make the LEDs blink to confirm + this->strip->setPixelColor(j-1, tempR, tempV, tempB); + } + + this->strip->show(); + } +} diff --git a/lib/SwitchableEncodeur/include/SwitchableEncodeur.h b/lib/SwitchableEncodeur/include/SwitchableEncodeur.h index a938568..bfa57f5 100644 --- a/lib/SwitchableEncodeur/include/SwitchableEncodeur.h +++ b/lib/SwitchableEncodeur/include/SwitchableEncodeur.h @@ -68,6 +68,11 @@ private: */ int oldPosition = -999; + /** + * @brief old menu number + */ + int oldMenu = -999; + /** * @brief nombre de menu */ diff --git a/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp b/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp index 2723d59..8f23269 100644 --- a/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp +++ b/lib/SwitchableEncodeur/src/SwitchableEncodeur.cpp @@ -13,6 +13,7 @@ SwitchableEncodeur::SwitchableEncodeur(uint8_t pin1, uint8_t pin2, uint8_t pinSW pinMode(pinSW, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(pinSW), switchEncoder, RISING);//FIXME: maybe change to FALLING this->oldPosition = -999; + this->oldMenu = -999; } @@ -38,7 +39,8 @@ SwitchableEncodeur* SwitchableEncodeur::getInstance() { bool SwitchableEncodeur::update() { bool sortie = false; long newPosition = this->read()/4; - if (newPosition != this->oldPosition) { + if (newPosition != this->oldPosition || this->menu != this->oldMenu) { + this->oldMenu = this->menu; this->oldPosition = newPosition; sortie = true; } diff --git a/platformio.ini b/platformio.ini index 329b7c7..a4c6637 100644 --- a/platformio.ini +++ b/platformio.ini @@ -52,6 +52,7 @@ upload_speed = 921600 lib_deps = ; example: ; erropix/ESP32 AnalogWrite@0.2 + adafruit/Adafruit NeoPixel@^1.11.0 ; Checker settings check_tool = clangtidy, cppcheck diff --git a/src/Program.cpp b/src/Program.cpp index 9e58303..9f4d975 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -31,11 +31,15 @@ Program::Program() { // Startup Encoder this->encoder = new SwitchableEncodeur(ENCODER_DT, ENCODER_CLK, ENCODER_SWITCH, 3); + + // Startup LEDs + this->ledLib = new LedLib(PIXEL_COUNT, PIXEL_PIN, 255); } void Program::loop() { if(this->encoder->update()){ Serial.print(this->encoder->getValue()); + this->ledLib->actLed(this->encoder->getValue()); Serial.print(" "); Serial.println(this->encoder->getMenu()); }