LedComponent: Added operator, LedColors namespace with multiple constexpres, Added fading function. ServerHandler: Blinking led when connecting, and status led after.

This commit is contained in:
Yohan Boujon 2023-12-16 00:31:58 +01:00
parent d4de1f6efb
commit 8b610a9dcc
6 changed files with 67 additions and 9 deletions

View file

@ -1,8 +1,17 @@
#include "LedComponent.hpp"
Color Color::operator-(byte value)
{
byte nullbyte(0);
return {(this->red - value) < 0 ? nullbyte : static_cast<byte>(this->red - value),
(this->blue - value) < 0 ? nullbyte : static_cast<byte>(this->blue - value),
(this->green - value) < 0 ? nullbyte : static_cast<byte>(this->green - value)};
}
LedComponent::LedComponent(byte pin, byte pin_clock, uint8_t led_number)
: _pin(pin), _pinClock(pin_clock), _ledNumber(led_number), _led(nullptr)
{}
{
}
LedComponent::~LedComponent()
{
@ -11,12 +20,27 @@ LedComponent::~LedComponent()
void LedComponent::setup()
{
_led = new ChainableLED(_pinClock,_pin,_ledNumber);
_led = new ChainableLED(_pinClock, _pin, _ledNumber);
}
void LedComponent::setColor(uint8_t led_number, Color color)
{
if(led_number >= _ledNumber)
if (led_number >= _ledNumber)
return;
_led->setColorRGB(led_number,color.red,color.green,color.blue);
_led->setColorRGB(led_number, color.red, color.green, color.blue);
}
void LedComponent::setColor(uint8_t led_number, Color color, uint16_t fading_time)
{
if (led_number >= _ledNumber)
return;
const auto redFade = color.red / (static_cast<float>(fading_time));
const auto greenFade = color.green / (static_cast<float>(fading_time));
const auto blueFade = color.blue / (static_cast<float>(fading_time));
for (uint16_t time(0); time < fading_time; time++)
{
_led->setColorRGB(led_number, static_cast<byte>(redFade * time), static_cast<byte>(greenFade * time), static_cast<byte>(blueFade * time));
delay(1);
}
}

View file

@ -8,8 +8,17 @@ struct Color
byte red;
byte blue;
byte green;
Color operator-(byte value);
};
namespace LedColors
{
constexpr Color LED_OFF = {0,0,0};
constexpr Color WIFI_ON = {0x18,0x28,0x36};
constexpr Color NO_WIFI = {0x64,0x04,0x0B};
}
class LedComponent
{
public:
@ -17,6 +26,7 @@ public:
~LedComponent();
void setup();
void setColor(uint8_t led_number, Color color);
void setColor(uint8_t led_number, Color color, uint16_t fading_time);
private:
byte _pin;

View file

@ -12,6 +12,9 @@ void MainComponent::setup()
{
_led.setup();
_dht.setup();
// Lights are off when powered
_led.setColor(0,{0,0,0});
_led.setColor(1,{0,0,0});
}
Component& MainComponent::getHumidity() { return _humidity; }

View file

@ -1,4 +1,6 @@
#include "Screen.hpp"
#include "MainComponent.hpp"
#include <vector>
#include <memory>
@ -126,6 +128,10 @@ void Screen::boot()
_bootFrame++;
bootWindow.Update(0,CLOVER_FRAMES[(_bootFrame >= 10 ? 10 : _bootFrame)]);
_screen->sendBuffer();
// Shutting down led when finished booting
if(_bootFrame == MAX_BOOT_FRAMES)
MainComponent::GetInstance().getLed().setColor(0,LedColors::LED_OFF);
}
void Screen::loop(const float plantHumidity, const float airTemperature, const float airHumidity)

View file

@ -1,6 +1,15 @@
#include "ServerHandler.hpp"
#include "MainComponent.hpp"
#include "../Display/Screen.hpp"
inline void led_blink(LedComponent& led)
{
led.setColor(0,LedColors::WIFI_ON);
delay(50);
led.setColor(0,LedColors::LED_OFF);
}
ServerHandler::ServerHandler() : server(80), display_time(0), _connected(false)
{
}
@ -11,13 +20,17 @@ void ServerHandler::setup(const char *ssid, const char *password)
{ // On utilise les scope resolution operator pour définir les méthodes la classe ServerHandle qui elle est dans hpp
uint8_t state(0);
uint16_t tryConnection(0);
auto& led = MainComponent::GetInstance().getLed();
Serial.begin(9600);
WiFi.begin(ssid, password);
// Testing connection
while ((WiFi.status() != WL_CONNECTED) && (tryConnection < MAX_CONNECT_TRIES))
{
delay(500);
led_blink(led);
delay(50);
led_blink(led);
delay(350);
Display::Screen::GetInstance().connecting(state);
state >= 3 ? state = 0 : state++;
tryConnection++;
@ -26,10 +39,15 @@ void ServerHandler::setup(const char *ssid, const char *password)
if (tryConnection < MAX_CONNECT_TRIES)
{
_connected = true;
auto color = LedColors::WIFI_ON;
led.setColor(0,color-15,200);
server.begin();
server.on("/", [this]()
{ this->handleRoot(); }); // fonction lamda pour gérer les requettes get
}
else {
led.setColor(0,LedColors::NO_WIFI,200);
}
}
void ServerHandler::loop()

View file

@ -17,15 +17,12 @@ void setup()
{
// Sensors/Acuators setup
MainComponent::GetInstance().setup();
// Lights are off when powered
auto& led = MainComponent::GetInstance().getLed();
led.setColor(0,{0,0,0});
led.setColor(1,{0,0,0});
// Setup for screen and server
Serial.begin(9600);
Display::Screen::GetInstance().Setup(const_cast<uint8_t*>(u8g2_font_busdisplay8x5_tr));
ServerHandler::GetInstance().setup(ssid, pswd);
// Printing server data
Serial.print("Connected to WiFi. IP address: ");
Serial.println(WiFi.localIP());