better support for shared config, rgb implementation correct

This commit is contained in:
Daan Meijer 2025-04-23 21:20:41 +02:00
parent 067f76f71f
commit 8e410e616e
12 changed files with 142 additions and 46 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "config.h"
#include <Arduino.h> #include <Arduino.h>
#define PIN_A 1 #define PIN_A 1

View File

@ -3,6 +3,7 @@
CRGB colorTable[] = { CRGB colorTable[] = {
CRGB(255, 142, 32), // "warm wit"
// CRGB(255, 100, 4), // CRGB(255, 100, 4),
// CRGB(255, 128, 4), // CRGB(255, 128, 4),
// CRGB(255, 100, 32), // CRGB(255, 100, 32),
@ -14,20 +15,94 @@ CRGB colorTable[] = {
CRGB::Yellow, CRGB::Yellow,
CRGB::Turquoise, CRGB::Turquoise,
CRGB::Purple, CRGB::Purple,
CRGB(255, 142, 32), // "warm wit"
}; };
Hanglamp::Hanglamp(){ Hanglamp::Hanglamp(){
turnOn();
} }
void parseHexColor(const String& hexColor, uint8_t& r, uint8_t& g, uint8_t& b) {
String cleanHex = hexColor;
// Remove leading '#' if present
if (cleanHex.startsWith("#")) {
cleanHex = cleanHex.substring(1);
}
// Make sure it's exactly 6 characters
if (cleanHex.length() != 6) {
r = g = b = 0; // default/fallback
return;
}
// Parse the color components
r = strtoul(cleanHex.substring(0, 2).c_str(), NULL, 16);
g = strtoul(cleanHex.substring(2, 4).c_str(), NULL, 16);
b = strtoul(cleanHex.substring(4, 6).c_str(), NULL, 16);
}
void Hanglamp::setup(){ void Hanglamp::setup(){
#ifdef ENABLE_MQTT
MQTT_subscribe("hanglamp/CMD/brightness", [&](unsigned char* payload, unsigned int len){
String input = String((const char*)payload).substring(0, len);
int brightness = constrain(input.toInt(), 0, 255);
setBrightness(brightness);
});
MQTT_subscribe("hanglamp/CMD/color", [&](unsigned char* payload, unsigned int len){
String input = String((const char*)payload).substring(0, len);
uint8_t r, g, b;
parseHexColor(input, r, g, b);
Serial.printf("input: %s, r: %d, g: %d, b: %d\n", input, r, g, b);
CRGB color(r, g, b);
setColor(color);
});
MQTT_subscribe("hanglamp/CMD/power", [&](unsigned char* payload, unsigned int len){
String input = String((const char*)payload).substring(0, len);
Serial.printf("Have power command: [%s]\n", input.c_str());
if(input == "true" || input == "1" || input == "on" || input == "ON"){
Serial.printf("Setting power to true\n");
turnOn();
}else{
Serial.printf("Setting power to false\n");
turnOff();
}
});
#endif
setColor(colorTable[this->colorIndex]);
turnOn();
} }
void Hanglamp::setColor(CRGB color){ void Hanglamp::setColor(CRGB color){
jumpTo(color); currentColor = color;
jumpTo(currentColor);
changed();
}
void Hanglamp::changed(){
#ifdef ENABLE_MQTT
char buff[128];
snprintf(buff, sizeof(buff),
"{\"color\":\"%02X%02X%02X\",\"brightness\":%d,\"power\":%s}",
currentColor.r,
currentColor.g,
currentColor.b,
brightness,
on ? "true" : "false"
);
MQTT_publish("hanglamp/status", buff);
#endif
} }
void Hanglamp::nextColor(){ void Hanglamp::nextColor(){
@ -41,17 +116,15 @@ void Hanglamp::nextColor(){
} }
void Hanglamp::setBrightness(int brightness){ void Hanglamp::setBrightness(int brightness){
uint8_t newBrightness = uint8_t newBrightness = constrain(brightness, 0, 255);
brightness < 0 ? 0 : if(!isOn()){
brightness > 255 ? 255 : turnOn();
brightness; }
this->brightness = newBrightness; this->brightness = newBrightness;
Serial.printf("new brightness: %d\n", newBrightness); Serial.printf("new brightness: %d\n", newBrightness);
FastLED.setBrightness(newBrightness); FastLED.setBrightness(newBrightness);
FastLED.show(); FastLED.show();
#ifdef ENABLE_MQTT changed();
MQTT_publish("hanglamp/brightness", String(newBrightness));
#endif
} }
void Hanglamp::adjustBrightness(int add){ void Hanglamp::adjustBrightness(int add){
@ -94,6 +167,10 @@ void Hanglamp::status(CRGB color){
void Hanglamp::setOn(bool on){ void Hanglamp::setOn(bool on){
Serial.printf("Hanglamp::setOn(%d -> %d)\n", this->on, on); Serial.printf("Hanglamp::setOn(%d -> %d)\n", this->on, on);
this->on = on; this->on = on;
if(currentColor == CRGB::Black){
nextColor();
}
changed();
} }
bool Hanglamp::isOn(){ bool Hanglamp::isOn(){

View File

@ -1,9 +1,13 @@
#pragma once #pragma once
#include "config.h"
#include <FastLED.h> #include <FastLED.h>
#include "Led.h" #include "Led.h"
#include "Controls.h" #include "Controls.h"
#include "MQTT.h"
class Hanglamp { class Hanglamp {
public: public:
@ -20,9 +24,13 @@ class Hanglamp {
void turnOn(); void turnOn();
void turnOff(); void turnOff();
// void receiveBrightnessFromMQTT(uint8_t * payload, unsigned int len);
private: private:
void changed();
char colorIndex = 0; char colorIndex = 0;
CRGB currentColor;
int brightness = 128; int brightness = 128;
bool on = false; bool on = false;
void setOn(bool on); void setOn(bool on);

View File

@ -123,8 +123,8 @@ void Led_setup(){
// analogWriteRange(255); // analogWriteRange(255);
#ifdef LED_FASTLED #ifdef LED_FASTLED
FastLED.addLeds<WS2812B, PIN_STRIP1, BRG>(strip2, NUM_LEDS).setRgbw(RgbwDefault()); FastLED.addLeds<WS2812B, PIN_STRIP1, GRB>(strip2, NUM_LEDS).setRgbw(RgbwDefault());
FastLED.addLeds<WS2812B, PIN_STRIP2, BRG>(strip2, NUM_LEDS).setRgbw(RgbwDefault()); FastLED.addLeds<WS2812B, PIN_STRIP2, GRB>(strip2, NUM_LEDS).setRgbw(RgbwDefault());
strip1[0] = CRGB::Black; strip1[0] = CRGB::Black;
strip2[0] = CRGB::Black; strip2[0] = CRGB::Black;

View File

@ -1,7 +1,10 @@
#pragma once #pragma once
#include "stdint.h" #include "stdint.h"
#include <cstdio> #include <cstdio>
#include "config.h"
#include <Arduino.h> #include <Arduino.h>
// #define LED_NEOPIXEL_BUS // #define LED_NEOPIXEL_BUS

View File

@ -46,20 +46,18 @@ void connect() {
} }
void MQTT_publish(const char * topic, String str){ void MQTT_publish(const char * topic, String str){
char * buff = new char[str.length() + 1]; // char * buff = new char[str.length() + 1];
str.toCharArray(buff, str.length() + 1); // str.toCharArray(buff, str.length() + 1);
MQTT_publish(topic, buff); MQTT_publish(topic, str.c_str());
delete buff; // delete buff;
} }
void MQTT_publish(const char * topic, const char * msg){ void MQTT_publish(const char * topic, const char * msg){
Serial.printf("MQTT_publish(%s, %s)\n", topic, msg);
client.publish(topic, msg); client.publish(topic, msg);
} }
void (*pCallback)(uint8_t *, unsigned int) = NULL; void MQTT_subscribe(char * topic, mqtt_callback callback){
void MQTT_subscribe(char * topic, void (*callback)(uint8_t *, unsigned int)){
Serial.println("MQTT_subscribe"); Serial.println("MQTT_subscribe");
Serial.printf("Topic: [%s]\n", topic); Serial.printf("Topic: [%s]\n", topic);
@ -88,7 +86,7 @@ void MQTT_callback(char* topic, uint8_t * payload, unsigned int length){
void MQTT_setup(){ void MQTT_setup(){
for (int i = 0; i < 17; i = i + 8) { for (int i = 0; i < 17; i = i + 8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
} }
@ -96,14 +94,9 @@ void MQTT_setup(){
client.setServer(mqtt_server, String(mqtt_port).toInt()); client.setServer(mqtt_server, String(mqtt_port).toInt());
client.setCallback(MQTT_callback); client.setCallback(MQTT_callback);
// Serial.println("MQTT_init after callback");
// char buff[256];
// sprintf(buff, "%08x", _pCallback);
// Serial.println(buff);
TaskHandle_t taskMqtt = NULL; TaskHandle_t taskMqtt = NULL;
xTaskCreate(MQTT_loop, "Wifi_loop", 10000, NULL, tskIDLE_PRIORITY, &taskMqtt); xTaskCreate(MQTT_loop, "MQTT_loop", 10000, NULL, tskIDLE_PRIORITY, &taskMqtt);
} }
void MQTT_loop(void* params){ void MQTT_loop(void* params){
@ -119,7 +112,10 @@ void MQTT_loop(void* params){
connect(); connect();
if(client.connected()){ if(client.connected()){
Serial.println("MQTT: succes!"); Serial.println("MQTT: success!");
for (auto channel = channels->begin(); channel != channels->end(); ++channel){
client.subscribe(channel->topic->c_str());
}
break; break;
}else{ }else{
Serial.println("MQTT: connection failed, try again in 5 seconds"); Serial.println("MQTT: connection failed, try again in 5 seconds");

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "settings.h" #include "config.h"
#include "Arduino.h" #include "Arduino.h"
@ -11,14 +11,20 @@
using namespace std; using namespace std;
extern char mqtt_server[40];
extern char mqtt_port[6];
typedef std::function<void(unsigned char *, unsigned int)> mqtt_callback;
struct SubscribedChannel { struct SubscribedChannel {
String * topic; String * topic;
void (*callback)(uint8_t *, unsigned int); mqtt_callback callback;
}; };
void MQTT_subscribe(char * topic, mqtt_callback callback);
void MQTT_publish(const char * topic, String str); void MQTT_publish(const char * topic, String str);
void MQTT_publish(const char * topic, const char * msg); void MQTT_publish(const char * topic, const char * msg);

View File

@ -1,2 +1,5 @@
#include "config.h"
void PinFinder_setup(); void PinFinder_setup();
void PinFinder_loop(); void PinFinder_loop();

View File

@ -1,3 +1,3 @@
#include "config.h"
void Wifi_setup(); void Wifi_setup();

12
src/config.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#define INCLUDE_LED
#define ENABLE_MQTT
// #define ENABLE_OTA
#define ENABLE_WIFI
// #define ENABLE_FS
// #define ENABLE_PINFINDER
#define ENABLE_COLORS
// #define ENABLE_PIN_SENSING
#define ENABLE_CONTROLS

View File

@ -1,4 +1,6 @@
#define INCLUDE_LED
#include "config.h"
#ifdef INCLUDE_LED #ifdef INCLUDE_LED
#include "Led.h" #include "Led.h"
#endif #endif
@ -8,13 +10,7 @@
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass #define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only #define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
#define ENABLE_MQTT
#define ENABLE_WIFI
// #define ENABLE_FS
// #define ENABLE_PINFINDER
#define ENABLE_COLORS
// #define ENABLE_PIN_SENSING
#define ENABLE_CONTROLS
#ifdef ENABLE_WIFI #ifdef ENABLE_WIFI
#include "Wifi.h" #include "Wifi.h"

View File

@ -1,7 +0,0 @@
#ifndef SETTINGS_H
#define SETTINGS_H
extern char mqtt_server[40];
extern char mqtt_port[6];
#endif