Fix 32 device issue

This commit is contained in:
Theo Arends 2022-02-18 16:13:55 +01:00
parent e6cafcfa42
commit 202f83b4ff
4 changed files with 48 additions and 54 deletions

View File

@ -255,27 +255,25 @@ void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len)
} }
} }
AddLog(LOG_LEVEL_DEBUG, PSTR("CMD: Grp %d, Cmnd '%s', Idx %d, Len %d, Data '%s'"), int32_t payload = -99;
grpflg, type, index, data_len, (binary_data) ? HexToString((uint8_t*)dataBuf, data_len).c_str() : dataBuf); if (!binary_data) {
if (!strcmp(dataBuf,"?")) { data_len = 0; }
char *p;
payload = strtol(dataBuf, &p, 0); // decimal, octal (0) or hex (0x)
if (p == dataBuf) { payload = -99; }
int temp_payload = GetStateNumber(dataBuf);
if (temp_payload > -1) { payload = temp_payload; }
}
AddLog(LOG_LEVEL_DEBUG, PSTR("CMD: Grp %d, Cmd '%s', Idx %d, Len %d, Pld %d, Data '%s'"),
grpflg, type, index, data_len, payload, (binary_data) ? HexToString((uint8_t*)dataBuf, data_len).c_str() : dataBuf);
if (type != nullptr) { if (type != nullptr) {
Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_ERROR "\"}")); Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_ERROR "\"}"));
if (Settings->ledstate &0x02) { TasmotaGlobal.blinks++; } if (Settings->ledstate &0x02) { TasmotaGlobal.blinks++; }
int32_t payload = -99;
if (!binary_data) {
if (!strcmp(dataBuf,"?")) { data_len = 0; }
char *p;
payload = strtol(dataBuf, &p, 0); // decimal, octal (0) or hex (0x)
if (p == dataBuf) { payload = -99; }
int temp_payload = GetStateNumber(dataBuf);
if (temp_payload > -1) { payload = temp_payload; }
}
DEBUG_CORE_LOG(PSTR("CMD: Payload %d"), payload);
// TasmotaGlobal.backlog_timer = millis() + (100 * MIN_BACKLOG_DELAY); // TasmotaGlobal.backlog_timer = millis() + (100 * MIN_BACKLOG_DELAY);
TasmotaGlobal.backlog_timer = millis() + Settings->param[P_BACKLOG_DELAY]; // SetOption34 TasmotaGlobal.backlog_timer = millis() + Settings->param[P_BACKLOG_DELAY]; // SetOption34

View File

@ -246,7 +246,7 @@ void SetDevicePower(power_t rpower, uint32_t source)
TasmotaGlobal.last_source = source; TasmotaGlobal.last_source = source;
if (POWER_ALL_ALWAYS_ON == Settings->poweronstate) { // All on and stay on if (POWER_ALL_ALWAYS_ON == Settings->poweronstate) { // All on and stay on
TasmotaGlobal.power = (1 << TasmotaGlobal.devices_present) -1; TasmotaGlobal.power = POWER_MASK >> (POWER_SIZE - TasmotaGlobal.devices_present);
rpower = TasmotaGlobal.power; rpower = TasmotaGlobal.power;
} }
@ -337,7 +337,7 @@ void SetAllPower(uint32_t state, uint32_t source)
publish_power = false; publish_power = false;
} }
if ((state >= POWER_OFF) && (state <= POWER_TOGGLE)) { if ((state >= POWER_OFF) && (state <= POWER_TOGGLE)) {
power_t all_on = (1 << TasmotaGlobal.devices_present) -1; power_t all_on = POWER_MASK >> (POWER_SIZE - TasmotaGlobal.devices_present);
switch (state) { switch (state) {
case POWER_OFF: case POWER_OFF:
TasmotaGlobal.power = 0; TasmotaGlobal.power = 0;
@ -365,6 +365,7 @@ void SetPowerOnState(void)
if (POWER_ALL_ALWAYS_ON == Settings->poweronstate) { if (POWER_ALL_ALWAYS_ON == Settings->poweronstate) {
SetDevicePower(1, SRC_RESTART); SetDevicePower(1, SRC_RESTART);
} else { } else {
power_t devices_mask = POWER_MASK >> (POWER_SIZE - TasmotaGlobal.devices_present);
if ((ResetReason() == REASON_DEFAULT_RST) || (ResetReason() == REASON_EXT_SYS_RST)) { if ((ResetReason() == REASON_DEFAULT_RST) || (ResetReason() == REASON_EXT_SYS_RST)) {
switch (Settings->poweronstate) { switch (Settings->poweronstate) {
case POWER_ALL_OFF: case POWER_ALL_OFF:
@ -373,24 +374,24 @@ void SetPowerOnState(void)
SetDevicePower(TasmotaGlobal.power, SRC_RESTART); SetDevicePower(TasmotaGlobal.power, SRC_RESTART);
break; break;
case POWER_ALL_ON: // All on case POWER_ALL_ON: // All on
TasmotaGlobal.power = (1 << TasmotaGlobal.devices_present) -1; TasmotaGlobal.power = devices_mask;
SetDevicePower(TasmotaGlobal.power, SRC_RESTART); SetDevicePower(TasmotaGlobal.power, SRC_RESTART);
break; break;
case POWER_ALL_SAVED_TOGGLE: case POWER_ALL_SAVED_TOGGLE:
TasmotaGlobal.power = (Settings->power & ((1 << TasmotaGlobal.devices_present) -1)) ^ POWER_MASK; TasmotaGlobal.power = (Settings->power & devices_mask) ^ POWER_MASK;
if (Settings->flag.save_state) { // SetOption0 - Save power state and use after restart if (Settings->flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(TasmotaGlobal.power, SRC_RESTART); SetDevicePower(TasmotaGlobal.power, SRC_RESTART);
} }
break; break;
case POWER_ALL_SAVED: case POWER_ALL_SAVED:
TasmotaGlobal.power = Settings->power & ((1 << TasmotaGlobal.devices_present) -1); TasmotaGlobal.power = Settings->power & devices_mask;
if (Settings->flag.save_state) { // SetOption0 - Save power state and use after restart if (Settings->flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(TasmotaGlobal.power, SRC_RESTART); SetDevicePower(TasmotaGlobal.power, SRC_RESTART);
} }
break; break;
} }
} else { } else {
TasmotaGlobal.power = Settings->power & ((1 << TasmotaGlobal.devices_present) -1); TasmotaGlobal.power = Settings->power & devices_mask;
if (Settings->flag.save_state) { // SetOption0 - Save power state and use after restart if (Settings->flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(TasmotaGlobal.power, SRC_RESTART); SetDevicePower(TasmotaGlobal.power, SRC_RESTART);
} }

View File

@ -37,8 +37,9 @@
* Power Type * Power Type
\*********************************************************************************************/ \*********************************************************************************************/
typedef unsigned long power_t; // Power (Relay) type typedef uint32_t power_t; // Power (Relay) type
const uint32_t POWER_MASK = 0xffffffffUL; // Power (Relay) full mask const uint32_t POWER_MASK = 0xFFFFFFFFUL; // Power (Relay) full mask
const uint32_t POWER_SIZE = 32; // Power (relay) bit count
/*********************************************************************************************\ /*********************************************************************************************\
* Constants * Constants

View File

@ -257,8 +257,8 @@ typedef struct {
uint8_t history_day[SSPM_MAX_MODULES][4]; uint8_t history_day[SSPM_MAX_MODULES][4];
#ifdef SSPM_SIMULATE #ifdef SSPM_SIMULATE
uint8_t emulate; uint8_t simulate;
uint8_t emulated_module; uint8_t simulated_module;
#endif #endif
uint8_t allow_updates; uint8_t allow_updates;
uint8_t get_energy_relay; uint8_t get_energy_relay;
@ -398,8 +398,8 @@ uint32_t SSPMGetMappedModuleId(uint32_t module) {
module_nr = module; // input module number if not found used as fallback module_nr = module; // input module number if not found used as fallback
} }
#ifdef SSPM_SIMULATE #ifdef SSPM_SIMULATE
Sspm->emulated_module = module_nr; Sspm->simulated_module = module_nr;
if (Sspm->emulate && (module_nr > 0) && (module_nr <= Sspm->emulate)) { if (Sspm->simulate && (module_nr > 0) && (module_nr <= Sspm->simulate)) {
module_nr = 0; // Emulate modules by 0 module_nr = 0; // Emulate modules by 0
} }
#endif #endif
@ -1067,18 +1067,15 @@ void SSPMHandleReceivedData(void) {
*/ */
if (!status && (0x06 == expected_bytes)) { if (!status && (0x06 == expected_bytes)) {
// SspmBuffer[20] & 0x0F // Relays operational // SspmBuffer[20] & 0x0F // Relays operational
power_t current_state = SspmBuffer[20] >> 4; // Relays state uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[3] << 8 | SspmBuffer[4]);
power_t mask = 0x0000000F; #ifdef SSPM_SIMULATE
for (uint32_t i = 0; i < Sspm->module_max; i++) { module = Sspm->simulated_module;
uint32_t module = SSPMGetMappedModuleId(i); #endif
if ((SspmBuffer[3] == Sspm->module[module][0]) && (SspmBuffer[4] == Sspm->module[module][1])) { power_t current_state = (SspmBuffer[20] >> 4) << (module * 4); // Relays state
current_state <<= (i * 4); power_t mask = 0x0000000F << (module * 4);
mask <<= (i * 4); TasmotaGlobal.power &= (POWER_MASK ^ mask);
TasmotaGlobal.power &= (POWER_MASK ^ mask); TasmotaGlobal.power |= current_state;
TasmotaGlobal.power |= current_state;
break;
}
}
Sspm->old_power = TasmotaGlobal.power; Sspm->old_power = TasmotaGlobal.power;
TasmotaGlobal.devices_present += 4; TasmotaGlobal.devices_present += 4;
} }
@ -1161,7 +1158,7 @@ void SSPMHandleReceivedData(void) {
uint32_t channel = SspmBuffer[32]; uint32_t channel = SspmBuffer[32];
uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[20] << 8 | SspmBuffer[21]); uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[20] << 8 | SspmBuffer[21]);
#ifdef SSPM_SIMULATE #ifdef SSPM_SIMULATE
module = Sspm->emulated_module; module = Sspm->simulated_module;
#endif #endif
if (Sspm->history_relay < 255) { if (Sspm->history_relay < 255) {
uint32_t history_module = Sspm->history_relay >> 2; uint32_t history_module = Sspm->history_relay >> 2;
@ -1344,7 +1341,7 @@ void SSPMHandleReceivedData(void) {
} }
uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[19] << 8 | SspmBuffer[20]); uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[19] << 8 | SspmBuffer[20]);
#ifdef SSPM_SIMULATE #ifdef SSPM_SIMULATE
module = Sspm->emulated_module; module = Sspm->simulated_module;
#endif #endif
Sspm->current[module][channel] = SspmBuffer[32] + (float)SspmBuffer[33] / 100; // x.xxA Sspm->current[module][channel] = SspmBuffer[32] + (float)SspmBuffer[33] / 100; // x.xxA
Sspm->voltage[module][channel] = SSPMGetValue(&SspmBuffer[34]); // x.xxV Sspm->voltage[module][channel] = SSPMGetValue(&SspmBuffer[34]); // x.xxV
@ -1368,16 +1365,13 @@ void SSPMHandleReceivedData(void) {
aa 55 01 00 00 00 00 00 00 00 00 00 00 00 00 00 07 00 0d 8b 34 32 37 39 37 34 13 4b 35 36 37 02 2d bb 08 - L2 Overload triggered power off aa 55 01 00 00 00 00 00 00 00 00 00 00 00 00 00 07 00 0d 8b 34 32 37 39 37 34 13 4b 35 36 37 02 2d bb 08 - L2 Overload triggered power off
*/ */
if (!Sspm->no_send_key) { if (!Sspm->no_send_key) {
power_t relay = SspmBuffer[31] & 0x0F; // Relays active uint32_t module = SSPMGetModuleNumberFromMap(SspmBuffer[19] << 8 | SspmBuffer[20]);
power_t relay_state = SspmBuffer[31] >> 4; // Relays state #ifdef SSPM_SIMULATE
for (uint32_t i = 0; i < Sspm->module_max; i++) { // module = Sspm->simulated_module; // Won't work as this is initiated from device
uint32_t module = SSPMGetMappedModuleId(i); #endif
if ((SspmBuffer[19] == Sspm->module[module][0]) && (SspmBuffer[20] == Sspm->module[module][1])) { power_t relay = (SspmBuffer[31] & 0x0F) << (module * 4); // Relays active
relay <<= (i * 4); power_t relay_state = (SspmBuffer[31] >> 4) << (module * 4); // Relays state
relay_state <<= (i * 4);
break;
}
}
for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) { for (uint32_t i = 1; i <= TasmotaGlobal.devices_present; i++) {
if (relay &1) { if (relay &1) {
ExecuteCommandPower(i, relay_state &1, SRC_BUTTON); ExecuteCommandPower(i, relay_state &1, SRC_BUTTON);
@ -1458,10 +1452,10 @@ void SSPMHandleReceivedData(void) {
if (0x24 == expected_bytes) { if (0x24 == expected_bytes) {
SSPMAddModule(); SSPMAddModule();
#ifdef SSPM_SIMULATE #ifdef SSPM_SIMULATE
if (0 == Sspm->emulate) { if (0 == Sspm->simulate) {
uint8_t current_idl = SspmBuffer[20]; uint8_t current_idl = SspmBuffer[20];
for (Sspm->emulate = 0; Sspm->emulate < SSPM_SIMULATE; Sspm->emulate++) { for (Sspm->simulate = 0; Sspm->simulate < SSPM_SIMULATE; Sspm->simulate++) {
SspmBuffer[20] = Sspm->emulate +1; SspmBuffer[20] = Sspm->simulate +1;
SSPMAddModule(); SSPMAddModule();
} }
SspmBuffer[20] = current_idl; SspmBuffer[20] = current_idl;