Skip to content

Commit

Permalink
refactoring to have a generic base to use multiple count of dimmer co…
Browse files Browse the repository at this point in the history
…ntrols
  • Loading branch information
ohAnd committed Nov 27, 2024
1 parent 1810db9 commit c683100
Show file tree
Hide file tree
Showing 16 changed files with 837 additions and 340 deletions.
14 changes: 12 additions & 2 deletions include/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

#define CONFIG_FILE_PATH "/userconfig.json"

#define LED_DIMMER_COUNT 5 // count of the available dimmer leds

struct ledDimmerConfig
{
uint8_t ledPWMpin = 255; // first init with Pin 4 as base functionality
uint8_t dimValueStep = 1; // in percent 0-100
uint8_t dimValueStepDelay = 10;
uint16_t dimValueRangeLow = 0; // 0-1023
uint16_t dimValueRangeHigh = 1023;// 0-1023
};

struct UserConfig
{
char wifiSsid[64] = "mySSID";
Expand All @@ -23,8 +34,7 @@ struct UserConfig
boolean mqttActive = false;

// led settings
uint8_t dimValueStep = 1;
uint8_t dimValueStepDelay = 10;
ledDimmerConfig ledDimmerConfigs[LED_DIMMER_COUNT];

boolean wifiAPstart = true;
int selectedUpdateChannel = 0; // 0 - release 1 - snapshot
Expand Down
3 changes: 0 additions & 3 deletions include/base/platformData.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ struct baseDataStruct

#if defined(ESP8266)
String chipType = "ESP8266";
#warning "setting chipType for ESP8266"
#elif CONFIG_IDF_TARGET_ESP32
String chipType = "ESP32";
#warning "setting chipType for ESP32"
#elif CONFIG_IDF_TARGET_ESP32S2
String chipType = "ESP32 S2 (LOLIN S2 Mini)";
#warning "setting chipType for ESP32S2"
#endif

const char *fwVersion = VERSION;
Expand Down
6 changes: 4 additions & 2 deletions include/base/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DTUwebserver {

void setWifiScanIsRunning(bool state);

void setLEDdata(ledDimmerStruct ledDimmerDataIn);
void setLEDdata(ledDimmerStruct ledDimmerDataIn[5]);

private:
AsyncWebServer asyncDtuWebServer{80}; // Assuming port 80 for the web server
Expand All @@ -58,10 +58,12 @@ class DTUwebserver {
static void handleDataJson(AsyncWebServerRequest *request);
static void handleInfojson(AsyncWebServerRequest *request);

static String ledStateToJSON(ledDimmerStruct ledDimmerData);
static String ledSettingsToJSON(ledDimmerStruct ledDimmerData);

static void handleUpdateWifiSettings(AsyncWebServerRequest *request);
static void handleUpdateLedSettings(AsyncWebServerRequest *request);
static void handleUpdateMqttSettings(AsyncWebServerRequest *request);
static void handleUpdatePowerLimit(AsyncWebServerRequest *request);
static void handleGetWifiNetworks(AsyncWebServerRequest *request);

static void handleUpdateOTASettings(AsyncWebServerRequest *request);
Expand Down
8 changes: 5 additions & 3 deletions include/dimmerLed.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ struct ledDimmerStruct {
uint8_t dimValueTarget = 0; // in percent 0-100
uint8_t dimValueStep = 1; // in percent 0-100
uint8_t dimValueStepDelay = 3; // in ms
unsigned long dimValueStepTimer = 0; // in ms
unsigned long dimTargetStartTimestamp = 0; // in ms
unsigned long dimValueStepTimerOld = 0; // in ms

boolean inTransition = false;

uint16_t dimValueRangeLow = 0; // 0-1023
uint16_t dimValueRangeHigh = 1023; // 0-1023
uint8_t ledPWMpin = 255; // init with invalid value
};
#endif

// Define the DimmerLed class
class DimmerLed {
public:
DimmerLed();
void setup(uint8_t dimValueStep, uint8_t dimValueStepDelay);
void setDimValue(uint8_t dimValue, uint8_t dimValueStep = 1, uint8_t dimValueStepDelay = 3);
void setup(uint8_t ledPWMpin);
void setDimValue(uint8_t dimValue);
void setConfigValues(uint8_t dimValueStep, uint8_t dimValueStepDelay, uint16_t dimValueRangeLow, uint16_t dimValueRangeHigh);
uint8_t getDimValue();
ledDimmerStruct getDimmerValues();
void loop();
Expand Down
20 changes: 14 additions & 6 deletions include/mqttHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <PubSubClient.h>
#include <WiFiClientSecure.h>

#include <base/platformData.h>

// MQTT_CONNECTION_TIMEOUT (-4): The server didn't respond within the keep-alive time.
// MQTT_CONNECTION_LOST (-3): The network connection was broken.
// MQTT_CONNECT_FAILED (-2): The network connection failed.
Expand All @@ -25,17 +27,20 @@ struct LedDimmerSet {
boolean setValueUpdate = false;
boolean setSwitch = false;
boolean setSwitchUpdate = false;
int8_t setdimValueStep = 0;
boolean setdimValueStepUpdate = false;
int8_t setdimValueStepDelay = 0;
boolean setdimValueStepDelayUpdate = false;
uint8_t ledPWMpin = 255;
};



class MQTTHandler {
public:
MQTTHandler(const char *broker, int port, const char *user, const char *password, bool useTLS);
void setup();
void setup(uint8_t ledPWMpin_0, uint8_t ledPWMpin_1, uint8_t ledPWMpin_2, uint8_t ledPWMpin_3, uint8_t ledPWMpin_4);
void loop();
void publishDiscoveryMessage(const char *entity, const char *entityReadableName, const char *unit, bool deleteMessage, const char *icon=NULL, const char *deviceClass=NULL, const char *commandEntity=NULL);
void publishStandardData(String entity, String value);
void publishStandardData(String subDevice, String entity, String value, String topicClass="light");

// Setters for runtime configuration
void setBroker(const char* broker);
Expand All @@ -50,7 +55,7 @@ class MQTTHandler {

void requestMQTTconnectionReset(boolean autoDiscoveryRemoveRequested);

LedDimmerSet getLedDimmerSet();
LedDimmerSet getLedDimmerSet(uint8_t ledNo);
void stopConnection(boolean full=false);

static void subscribedMessageArrived(char *topic, byte *payload, unsigned int length);
Expand Down Expand Up @@ -79,10 +84,13 @@ class MQTTHandler {
boolean requestMQTTconnectionResetFlag;
unsigned long lastReconnectAttempt = 0;

LedDimmerSet lastDimmerSet;
// LedDimmerSet lastDimmerSet[LED_DIMMER_COUNT];
LedDimmerSet lastDimmerSet[5];

void reconnect();
boolean initiateDiscoveryMessages(bool autoDiscoveryRemove=false);
void subscribingLEDxLight(uint8_t ledNo);
void subscribingLEDxNumber(uint8_t ledNo);
};

extern MQTTHandler mqttHandler;
Expand Down
6 changes: 3 additions & 3 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define VERSION "0.0.291_localDev"
#define BUILDTIME "11.10.2024 - 20:17:45"
#define BUILDTIMESTAMP "1728670665"
#define VERSION "1.1.3_localDev"
#define BUILDTIME "27.11.2024 - 07:15:23"
#define BUILDTIMESTAMP "1732688123"
8 changes: 4 additions & 4 deletions include/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "0.0.291_localDev",
"versiondate": "11.10.2024 - 20:17:45",
"linksnapshot": "https://github.com/ohAnd/LEDdimmerMQTT/releases/download/snapshot/LEDdimmerMQTT_snapshot_0.0.291_localDev.bin",
"link": "https://github.com/ohAnd/LEDdimmerMQTT/releases/latest/download/LEDdimmerMQTT_release_0.0.291_localDev.bin"
"version": "1.1.3_localDev",
"versiondate": "27.11.2024 - 07:15:23",
"linksnapshot": "https://github.com/ohAnd/LEDdimmerMQTT/releases/download/snapshot/LEDdimmerMQTT_snapshot_1.1.3_localDev.bin",
"link": "https://github.com/ohAnd/LEDdimmerMQTT/releases/latest/download/LEDdimmerMQTT_release_1.1.3_localDev.bin"
}
Loading

0 comments on commit c683100

Please sign in to comment.