Bugfixes regarding issue #8

and some minor changes
This commit is contained in:
bpohvoodoo
2019-03-20 17:10:56 +01:00
parent 9f482877c3
commit 4c88233a6f
9 changed files with 1315 additions and 1312 deletions
+57 -53
View File
@@ -301,9 +301,9 @@ bool checkPin(uint8_t pin) {
neoPixelType checkRGBOrder(char rgbOrder[5]) {
for( int i=0 ; i < sizeof(rgbOrder) ; ++i ) rgbOrder[i] = toupper(rgbOrder[i]) ;
for( uint8_t i=0 ; i < sizeof(rgbOrder) ; ++i ) rgbOrder[i] = toupper(rgbOrder[i]) ;
DBG_OUTPUT_PORT.printf("Checking RGB Order: %s ...", rgbOrder);
neoPixelType returnOrder;
neoPixelType returnOrder = 0;
if (strcmp(rgbOrder, "GRB") == 0) {
returnOrder = NEO_GRB;
} else if (strcmp(rgbOrder, "GBR") == 0) {
@@ -366,7 +366,14 @@ neoPixelType checkRGBOrder(char rgbOrder[5]) {
returnOrder = NEO_BGRW;
} else {
DBG_OUTPUT_PORT.print("invalid input!");
returnOrder = static_cast<neoPixelType>(checkRGBOrder(WS2812FXStripSettings.RGBOrder));
uint16_t check = checkRGBOrder(WS2812FXStripSettings.RGBOrder);
if (check != 0) {
returnOrder = static_cast<neoPixelType>(check);
strcpy(rgbOrder, WS2812FXStripSettings.RGBOrder);
} else {
returnOrder = static_cast<neoPixelType>(checkRGBOrder(RGBORDER));
strcpy(rgbOrder, RGBORDER);
}
}
DBG_OUTPUT_PORT.println("success!");
strcpy(WS2812FXStripSettings.RGBOrder, rgbOrder);
@@ -383,12 +390,12 @@ bool setConfByConfString(String saved_conf_string) {
getValue(saved_conf_string, '|', 4).toCharArray(mqtt_user, 32);
getValue(saved_conf_string, '|', 5).toCharArray(mqtt_pass, 32);
#endif
WS2812FXStripSettings.stripSize = constrain(getValue(saved_conf_string, '|', 6).toInt(), 0, MAXLEDS);
WS2812FXStripSettings.stripSize = constrain(getValue(saved_conf_string, '|', 6).toInt(), 1, MAXLEDS);
checkPin(getValue(saved_conf_string, '|', 7).toInt());
char tmp_rgbOrder[5];
getValue(saved_conf_string, '|', 8).toCharArray(tmp_rgbOrder, 4);
checkRGBOrder(tmp_rgbOrder);
WS2812FXStripSettings.fxoptions = getValue(saved_conf_string, '|', 9).toInt();
WS2812FXStripSettings.fxoptions = constrain(((getValue(saved_conf_string, '|', 9).toInt()>>1)<<1), 0, 255);
return true;
} else {
DBG_OUTPUT_PORT.println("Saved conf not found!");
@@ -680,7 +687,7 @@ void handleNotFound() {
// Functions and variables for automatic cycling
// ***************************************************************************
Ticker autoTicker;
int autoCount = 0;
uint8_t autoCount = 0;
void autoTick() {
uint32_t setcolors[] = {autoParams[autoCount][0],autoParams[autoCount][1],autoParams[autoCount][2]};
@@ -808,10 +815,10 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
DBG_OUTPUT_PORT.print("MQTT: ");
#if defined(ENABLE_MQTT)
#if ENABLE_MQTT == 0
mqtt_client->publish(mqtt_outtopic, json.c_str());
mqtt_client.publish(mqtt_outtopic, json.c_str());
#endif
#if ENABLE_MQTT == 1
mqtt_client->publish(mqtt_outtopic, qospub, false, json.c_str());
mqtt_client.publish(mqtt_outtopic, qospub, false, json.c_str());
#endif
#endif
} else {
@@ -832,7 +839,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
char tmp_count[6];
snprintf(tmp_count, sizeof(tmp_count), "%s", &payload[3]);
tmp_count[5] = 0x00;
WS2812FXStripSettings.stripSize = constrain(atoi(tmp_count), 0, MAXLEDS);
WS2812FXStripSettings.stripSize = constrain(atoi(tmp_count), 1, MAXLEDS);
updateStrip = true;
}
if (payload[2] == 'r') {
@@ -856,7 +863,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
char tmp_fxoptions[4];
snprintf(tmp_fxoptions, sizeof(tmp_fxoptions), "%s", &payload[3]);
tmp_fxoptions[3] = 0x00;
WS2812FXStripSettings.fxoptions = constrain(atoi(tmp_fxoptions), 0, 255);
WS2812FXStripSettings.fxoptions = ((constrain(atoi(tmp_fxoptions), 0, 255)>>1)<<1);
updateStrip = true;
}
}
@@ -910,16 +917,16 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
EEPROM.commit();
}
#endif
#endif
#endif
String json = listConfigJSON();
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
#if defined(ENABLE_MQTT)
#if ENABLE_MQTT == 0
mqtt_client->publish(mqtt_outtopic, json.c_str());
mqtt_client.publish(mqtt_outtopic, json.c_str());
#endif
#if ENABLE_MQTT == 1
mqtt_client->publish(mqtt_outtopic, qospub, false, json.c_str());
mqtt_client.publish(mqtt_outtopic, qospub, false, json.c_str());
#endif
#endif
} else {
@@ -938,17 +945,14 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
if (mqtt == true) {
DBG_OUTPUT_PORT.print("MQTT: ");
#if defined(ENABLE_MQTT)
#if ENABLE_MQTT == 0
String json = listModesJSON();
unsigned int msg_len = measureJson(json) + 1;
char buffer[msg_len];
serializeJson(json, buffer, sizeof(buffer));
mqtt_client->beginPublish(mqtt_outtopic, msg_len, true);
mqtt_client->write((const uint8_t*)buffer, msg_len);
mqtt_client->endPublish();
#if ENABLE_MQTT == 2
uint16_t msg_len = strlen(json.c_str()) + 1;
mqtt_client.beginPublish(mqtt_outtopic, msg_len, true);
mqtt_client.write((const uint8_t*)json.c_str(), msg_len);
mqtt_client.endPublish();
#endif
#if ENABLE_MQTT == 1
mqtt_client->publish(mqtt_outtopic, qospub, false, json.c_str());
mqtt_client.publish(mqtt_outtopic, qospub, false, json.c_str());
#endif
#endif
} else {
@@ -1016,8 +1020,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
new_ha_mqtt_msg = true;
}
LEDState temp2rgb(unsigned int kelvin) {
int tmp_internal = kelvin / 100.0;
LEDState temp2rgb(uint16_t kelvin) {
uint16_t tmp_internal = kelvin / 100.0;
LEDState tmp_color;
// red
@@ -1122,11 +1126,11 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
serializeJson(root, buffer, sizeof(buffer));
jsonBuffer.clear();
#if ENABLE_MQTT == 0
mqtt_client->publish(mqtt_ha_state_out, buffer, true);
mqtt_client.publish(mqtt_ha_state_out, buffer, true);
DBG_OUTPUT_PORT.printf("MQTT: Send [%s]: %s\r\n", mqtt_ha_state_out, buffer);
#endif
#if ENABLE_MQTT == 1
mqtt_client->publish(mqtt_ha_state_out, 1, true, buffer);
mqtt_client.publish(mqtt_ha_state_out, 1, true, buffer);
DBG_OUTPUT_PORT.printf("MQTT: Send [%s]: %s\r\n", mqtt_ha_state_out, buffer);
#endif
new_ha_mqtt_msg = false;
@@ -1195,7 +1199,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
if (root.containsKey("color_temp")) {
//temp comes in as mireds, need to convert to kelvin then to RGB
color_temp = (uint16_t) root["color_temp"];
unsigned int kelvin = 1000000 / color_temp;
uint16_t kelvin = 1000000 / color_temp;
main_color = temp2rgb(kelvin);
mode = SET_COLOR;
}
@@ -1252,7 +1256,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#endif
#if ENABLE_MQTT == 0
void mqtt_callback(char* topic, byte* payload_in, unsigned int length) {
void mqtt_callback(char* topic, byte* payload_in, uint16_t length) {
uint8_t * payload = (uint8_t *)malloc(length + 1);
memcpy(payload, payload_in, length);
payload[length] = 0;
@@ -1279,26 +1283,26 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#if ENABLE_MQTT == 0
void mqtt_reconnect() {
// Loop until we're reconnected
while (!mqtt_client->connected() && mqtt_reconnect_retries < MQTT_MAX_RECONNECT_TRIES) {
while (!mqtt_client.connected() && mqtt_reconnect_retries < MQTT_MAX_RECONNECT_TRIES) {
mqtt_reconnect_retries++;
DBG_OUTPUT_PORT.printf("Attempting MQTT connection %d / %d ...\r\n", mqtt_reconnect_retries, MQTT_MAX_RECONNECT_TRIES);
// Attempt to connect
if (mqtt_client->connect(mqtt_clientid, mqtt_user, mqtt_pass, mqtt_will_topic, 2, true, mqtt_will_payload, true)) {
if (mqtt_client.connect(mqtt_clientid, mqtt_user, mqtt_pass, mqtt_will_topic, 2, true, mqtt_will_payload, true)) {
DBG_OUTPUT_PORT.println("MQTT connected!");
// Once connected, publish an announcement...
char message[18 + strlen(HOSTNAME) + 1];
strcpy(message, "McLighting ready: ");
strcat(message, HOSTNAME);
mqtt_client->publish(mqtt_outtopic, message);
mqtt_client.publish(mqtt_outtopic, message);
// ... and resubscribe
mqtt_client->subscribe(mqtt_intopic, qossub);
mqtt_client.subscribe(mqtt_intopic, qossub);
if(mqtt_lwt_boot_flag) {
mqtt_client.publish(mqtt_will_topic, "ONLINE");
//mqtt_lwt_boot_flag = false;
}
#if defined(ENABLE_HOMEASSISTANT)
ha_send_data.detach();
mqtt_client->subscribe(mqtt_ha_state_in, qossub);
mqtt_client.subscribe(mqtt_ha_state_in, qossub);
ha_send_data.once(5, tickerSendState);
#if defined(MQTT_HOME_ASSISTANT_SUPPORT)
const size_t bufferSize = JSON_ARRAY_SIZE(strip->getModeCount()+ 4) + JSON_OBJECT_SIZE(11) + 1500;
@@ -1338,16 +1342,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
// Following will never work for PubSubClient as message size > 1.6kB
// char buffer[measureJson(json) + 1];
// serializeJson(json, buffer, sizeof(buffer));
// mqtt_client->publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true);
// mqtt_client.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true);
// Alternate way to publish large messages using PubSubClient
unsigned int msg_len = measureJson(json) + 1;
uint16_t msg_len = measureJson(json) + 1;
char buffer[msg_len];
serializeJson(json, buffer, sizeof(buffer));
DBG_OUTPUT_PORT.println(buffer);
mqtt_client->beginPublish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), msg_len-1, true);
mqtt_client->write((const uint8_t*)buffer, msg_len-1);
mqtt_client->endPublish();
mqtt_client.beginPublish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), msg_len-1, true);
mqtt_client.write((const uint8_t*)buffer, msg_len-1);
mqtt_client.endPublish();
#endif
#endif
@@ -1355,7 +1359,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
DBG_OUTPUT_PORT.printf("MQTT topic out: %s\r\n", mqtt_outtopic);
} else {
DBG_OUTPUT_PORT.print("failed, rc=");
DBG_OUTPUT_PORT.print(mqtt_client->state());
DBG_OUTPUT_PORT.print(mqtt_client.state());
DBG_OUTPUT_PORT.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
@@ -1376,7 +1380,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
void connectToMqtt() {
DBG_OUTPUT_PORT.println("Connecting to MQTT...");
mqtt_client->connect();
mqtt_client.connect();
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
@@ -1400,17 +1404,17 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
char message[18 + strlen(HOSTNAME) + 1];
strcpy(message, "McLighting ready: ");
strcat(message, HOSTNAME);
mqtt_client->publish(mqtt_outtopic, qospub, false, message);
mqtt_client.publish(mqtt_outtopic, qospub, false, message);
//Subscribe
uint16_t packetIdSub1 = mqtt_client->subscribe(mqtt_intopic, qossub);
uint16_t packetIdSub1 = mqtt_client.subscribe(mqtt_intopic, qossub);
DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub1);
if(mqtt_lwt_boot_flag) {
mqtt_client->publish(mqtt_will_topic, qospub, false, "ONLINE");
mqtt_client.publish(mqtt_will_topic, qospub, false, "ONLINE");
mqtt_lwt_boot_flag = false;
}
#if defined(ENABLE_HOMEASSISTANT)
ha_send_data.detach();
uint16_t packetIdSub2 = mqtt_client->subscribe((char *)mqtt_ha_state_in, qossub);
uint16_t packetIdSub2 = mqtt_client.subscribe((char *)mqtt_ha_state_in, qossub);
DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2);
#if defined(MQTT_HOME_ASSISTANT_SUPPORT)
const size_t bufferSize = JSON_ARRAY_SIZE(strip->getModeCount()+ 4) + JSON_OBJECT_SIZE(11) + 1500;
@@ -1450,7 +1454,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
char buffer[measureJson(json) + 1];
serializeJson(json, buffer, sizeof(buffer));
jsonBuffer.clear();
mqtt_client->publish(mqtt_ha_config, qospub, true, buffer);
mqtt_client.publish(mqtt_ha_config, qospub, true, buffer);
#endif
#endif
}
@@ -1656,13 +1660,13 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
strcpy(mqtt_user, json["mqtt_user"]);
strcpy(mqtt_pass, json["mqtt_pass"]);
#endif
WS2812FXStripSettings.stripSize = constrain ((uint8_t) json["ws_cnt"],0, MAXLEDS);
WS2812FXStripSettings.stripSize = constrain ((uint8_t) json["ws_cnt"], 1, MAXLEDS);
char tmp_rgbOrder[5];
strcpy(tmp_rgbOrder, json["ws_rgbo"]);
checkRGBOrder(tmp_rgbOrder);
uint8_t temp_pin;
WS2812FXStripSettings.pin = (uint8_t) json["ws_pin"];
WS2812FXStripSettings.fxoptions = (uint8_t) json["ws_fxopt"];
WS2812FXStripSettings.pin = checkPin((uint8_t) json["ws_pin"]);
WS2812FXStripSettings.fxoptions = ((constrain((uint8_t) json["ws_fxopt"], 0, 255)>>1)<<1);
jsonBuffer.clear();
return true;
} else {
@@ -1769,9 +1773,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
// ***************************************************************************
// EEPROM helper
// ***************************************************************************
String readEEPROM(int offset, int len) {
String readEEPROM(uint16_t offset, uint16_t len) {
String res = "";
for (int i = 0; i < len; ++i)
for (uint16_t i = 0; i < len; ++i)
{
res += char(EEPROM.read(i + offset));
//DBG_OUTPUT_PORT.println(char(EEPROM.read(i + offset)));
@@ -1780,9 +1784,9 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
return res;
}
void writeEEPROM(int offset, int len, String value) {
void writeEEPROM(uint16_t offset, uint16_t len, String value) {
DBG_OUTPUT_PORT.printf("writeEEPROM(): %s\r\n", value.c_str());
for (int i = 0; i < len; ++i)
for (uint16_t i = 0; i < len; ++i)
{
if (i < value.length()) {
EEPROM.write(i + offset, value[i]);
@@ -1802,7 +1806,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
// Request handler for IR remote support
// ***************************************************************************
void handleRemote() {
int chng = 1;
uint8_t chng = 1;
if (irrecv.decode(&results)) {
DBG_OUTPUT_PORT.print("IR Code: 0x");
DBG_OUTPUT_PORT.print(uint64ToString(results.value, HEX));