Refactor energy driver detection

Refactor energy driver detection and function call
This commit is contained in:
Theo Arends 2019-09-08 16:57:56 +02:00
parent cea6592945
commit 75abfc5fd9
11 changed files with 217 additions and 247 deletions

View File

@ -696,7 +696,7 @@ void CmndMaxEnergyStart(void)
void EnergyDrvInit(void) void EnergyDrvInit(void)
{ {
energy_flg = ENERGY_NONE; energy_flg = ENERGY_NONE;
XnrgCall(FUNC_PRE_INIT); XnrgCall(FUNC_PRE_INIT); // Find first energy driver
} }
void EnergySnsInit(void) void EnergySnsInit(void)
@ -901,14 +901,14 @@ bool Xdrv03(uint8_t function)
case FUNC_EVERY_250_MSECOND: case FUNC_EVERY_250_MSECOND:
XnrgCall(FUNC_EVERY_250_MSECOND); XnrgCall(FUNC_EVERY_250_MSECOND);
break; break;
case FUNC_SERIAL:
result = XnrgCall(FUNC_SERIAL);
break;
#ifdef USE_ENERGY_MARGIN_DETECTION #ifdef USE_ENERGY_MARGIN_DETECTION
case FUNC_SET_POWER: case FUNC_SET_POWER:
Energy.power_steady_counter = 2; Energy.power_steady_counter = 2;
break; break;
#endif // USE_ENERGY_MARGIN_DETECTION #endif // USE_ENERGY_MARGIN_DETECTION
case FUNC_SERIAL:
result = XnrgCall(FUNC_SERIAL);
break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = DecodeCommand(kEnergyCommands, EnergyCommand); result = DecodeCommand(kEnergyCommands, EnergyCommand);
break; break;
@ -923,9 +923,6 @@ bool Xsns03(uint8_t function)
if (energy_flg) { if (energy_flg) {
switch (function) { switch (function) {
case FUNC_INIT:
EnergySnsInit();
break;
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
#ifdef USE_ENERGY_MARGIN_DETECTION #ifdef USE_ENERGY_MARGIN_DETECTION
EnergyMarginCheck(); EnergyMarginCheck();
@ -943,6 +940,9 @@ bool Xsns03(uint8_t function)
case FUNC_SAVE_BEFORE_RESTART: case FUNC_SAVE_BEFORE_RESTART:
EnergySaveState(); EnergySaveState();
break; break;
case FUNC_INIT:
EnergySnsInit();
break;
} }
} }
return result; return result;

View File

@ -21,7 +21,7 @@
#ifdef USE_TUYA_MCU #ifdef USE_TUYA_MCU
#define XDRV_16 16 #define XDRV_16 16
#define XNRG_08 8 #define XNRG_16 16 // Needs to be the last XNRG_xx
#ifndef TUYA_DIMMER_ID #ifndef TUYA_DIMMER_ID
#define TUYA_DIMMER_ID 0 #define TUYA_DIMMER_ID 0
@ -372,6 +372,7 @@ void TuyaPacketProcess(void)
} }
else if (Tuya.buffer[5] == 8) { // Long value packet else if (Tuya.buffer[5] == 8) { // Long value packet
bool tuya_energy_enabled = (XNRG_16 == energy_flg);
if (fnId == TUYA_MCU_FUNC_DIMMER) { if (fnId == TUYA_MCU_FUNC_DIMMER) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), Tuya.buffer[13]); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), Tuya.buffer[13]);
Tuya.new_dim = changeUIntScale((uint8_t) Tuya.buffer[13], 0, Settings.param[P_TUYA_DIMMER_MAX], 0, 100); Tuya.new_dim = changeUIntScale((uint8_t) Tuya.buffer[13], 0, Settings.param[P_TUYA_DIMMER_MAX], 0, 100);
@ -384,13 +385,13 @@ void TuyaPacketProcess(void)
} }
#ifdef USE_ENERGY_SENSOR #ifdef USE_ENERGY_SENSOR
else if (fnId == TUYA_MCU_FUNC_VOLTAGE) { else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_VOLTAGE) {
Energy.voltage = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 10; Energy.voltage = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 10;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Voltage=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13])); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Voltage=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13]));
} else if (fnId == TUYA_MCU_FUNC_CURRENT) { } else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_CURRENT) {
Energy.current = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 1000; Energy.current = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 1000;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Current=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13])); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Current=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13]));
} else if (fnId == TUYA_MCU_FUNC_POWER) { } else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_POWER) {
Energy.active_power = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 10; Energy.active_power = (float)(Tuya.buffer[12] << 8 | Tuya.buffer[13]) / 10;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Active_Power=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13])); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Rx ID=%d Active_Power=%d"), Tuya.buffer[6], (Tuya.buffer[12] << 8 | Tuya.buffer[13]));
@ -601,22 +602,20 @@ void TuyaSetWifiLed(void)
* Energy Interface * Energy Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg08(uint8_t function) bool Xnrg16(uint8_t function)
{ {
int result = 0; bool result = false;
if (TUYA_DIMMER == my_module_type) { if (TUYA_DIMMER == my_module_type) {
if (FUNC_PRE_INIT == function) { if (FUNC_PRE_INIT == function) {
if (!energy_flg) { if (TuyaGetDpId(TUYA_MCU_FUNC_POWER) != 0) {
if (TuyaGetDpId(TUYA_MCU_FUNC_POWER) != 0) { if (TuyaGetDpId(TUYA_MCU_FUNC_CURRENT) == 0) {
if (TuyaGetDpId(TUYA_MCU_FUNC_CURRENT) == 0) { Energy.current_available = false;
Energy.current_available = false;
}
if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) {
Energy.voltage_available = false;
}
energy_flg = XNRG_08;
} }
if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) {
Energy.voltage_available = false;
}
energy_flg = XNRG_16;
} }
} }
} }

View File

@ -249,34 +249,32 @@ void HlwSnsInit(void)
void HlwDrvInit(void) void HlwDrvInit(void)
{ {
if (!energy_flg) { Hlw.model_type = 0; // HLW8012
Hlw.model_type = 0; // HLW8012 if (pin[GPIO_HJL_CF] < 99) {
if (pin[GPIO_HJL_CF] < 99) { pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF];
pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF]; pin[GPIO_HJL_CF] = 99;
pin[GPIO_HJL_CF] = 99; Hlw.model_type = 1; // HJL-01/BL0937
Hlw.model_type = 1; // HJL-01/BL0937 }
if (pin[GPIO_HLW_CF] < 99) { // HLW8012 or HJL-01 based device Power monitor
Hlw.ui_flag = true; // Voltage on high
if (pin[GPIO_NRG_SEL_INV] < 99) {
pin[GPIO_NRG_SEL] = pin[GPIO_NRG_SEL_INV];
pin[GPIO_NRG_SEL_INV] = 99;
Hlw.ui_flag = false; // Voltage on low
} }
if (pin[GPIO_HLW_CF] < 99) { // HLW8012 or HJL-01 based device Power monitor if (pin[GPIO_NRG_CF1] < 99) { // Voltage and/or Current monitor
if (99 == pin[GPIO_NRG_SEL]) { // Voltage and/or Current selector
Hlw.ui_flag = true; // Voltage on high Energy.current_available = false; // Assume Voltage
if (pin[GPIO_NRG_SEL_INV] < 99) {
pin[GPIO_NRG_SEL] = pin[GPIO_NRG_SEL_INV];
pin[GPIO_NRG_SEL_INV] = 99;
Hlw.ui_flag = false; // Voltage on low
} }
} else {
if (pin[GPIO_NRG_CF1] < 99) { // Voltage and/or Current monitor Energy.current_available = false;
if (99 == pin[GPIO_NRG_SEL]) { // Voltage and/or Current selector Energy.voltage_available = false;
Energy.current_available = false; // Assume Voltage
}
} else {
Energy.current_available = false;
Energy.voltage_available = false;
}
energy_flg = XNRG_01;
} }
energy_flg = XNRG_01;
} }
} }
@ -311,28 +309,26 @@ bool HlwCommand(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg01(uint8_t function) bool Xnrg01(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
HlwDrvInit(); case FUNC_EVERY_200_MSECOND:
} HlwEvery200ms();
else if (XNRG_01 == energy_flg) { break;
switch (function) { case FUNC_ENERGY_EVERY_SECOND:
case FUNC_INIT: HlwEverySecond();
HlwSnsInit(); break;
break; case FUNC_COMMAND:
case FUNC_ENERGY_EVERY_SECOND: result = HlwCommand();
HlwEverySecond(); break;
break; case FUNC_INIT:
case FUNC_EVERY_200_MSECOND: HlwSnsInit();
HlwEvery200ms(); break;
break; case FUNC_PRE_INIT:
case FUNC_COMMAND: HlwDrvInit();
result = HlwCommand(); break;
break;
}
} }
return result; return result;
} }

View File

@ -145,7 +145,7 @@ bool CseSerialInput(void)
Energy.data_valid = 0; Energy.data_valid = 0;
CseReceived(); CseReceived();
Cse.received = false; Cse.received = false;
return 1; return true;
} else { } else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: " D_CHECKSUM_FAILURE)); AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: " D_CHECKSUM_FAILURE));
do { // Sync buffer with data (issue #1907 and #3425) do { // Sync buffer with data (issue #1907 and #3425)
@ -167,7 +167,7 @@ bool CseSerialInput(void)
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte; serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
} }
serial_in_byte = 0; // Discard serial_in_byte = 0; // Discard
return 0; return false;
} }
/********************************************************************************************/ /********************************************************************************************/
@ -208,16 +208,14 @@ void CseEverySecond(void)
void CseDrvInit(void) void CseDrvInit(void)
{ {
if (!energy_flg) { if ((3 == pin[GPIO_CSE7766_RX]) && (1 == pin[GPIO_CSE7766_TX])) { // As it uses 8E1 currently only hardware serial is supported
if ((3 == pin[GPIO_CSE7766_RX]) && (1 == pin[GPIO_CSE7766_TX])) { // As it uses 8E1 currently only hardware serial is supported baudrate = 4800;
baudrate = 4800; serial_config = SERIAL_8E1;
serial_config = SERIAL_8E1; if (0 == Settings.param[P_CSE7766_INVALID_POWER]) {
if (0 == Settings.param[P_CSE7766_INVALID_POWER]) { Settings.param[P_CSE7766_INVALID_POWER] = CSE_MAX_INVALID_POWER; // SetOption39 1..255
Settings.param[P_CSE7766_INVALID_POWER] = CSE_MAX_INVALID_POWER; // SetOption39 1..255
}
Cse.power_invalid = Settings.param[P_CSE7766_INVALID_POWER];
energy_flg = XNRG_02;
} }
Cse.power_invalid = Settings.param[P_CSE7766_INVALID_POWER];
energy_flg = XNRG_02;
} }
} }
@ -249,25 +247,23 @@ bool CseCommand(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg02(uint8_t function) bool Xnrg02(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
CseDrvInit(); case FUNC_SERIAL:
} result = CseSerialInput();
else if (XNRG_02 == energy_flg) { break;
switch (function) { case FUNC_ENERGY_EVERY_SECOND:
case FUNC_ENERGY_EVERY_SECOND: CseEverySecond();
CseEverySecond(); break;
break; case FUNC_COMMAND:
case FUNC_COMMAND: result = CseCommand();
result = CseCommand(); break;
break; case FUNC_PRE_INIT:
case FUNC_SERIAL: CseDrvInit();
result = CseSerialInput(); break;
break;
}
} }
return result; return result;
} }

View File

@ -211,10 +211,8 @@ void PzemSnsInit(void)
void PzemDrvInit(void) void PzemDrvInit(void)
{ {
if (!energy_flg) { if ((pin[GPIO_PZEM004_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { // Any device with a Pzem004T
if ((pin[GPIO_PZEM004_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { // Any device with a Pzem004T energy_flg = XNRG_03;
energy_flg = XNRG_03;
}
} }
} }
@ -222,22 +220,20 @@ void PzemDrvInit(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg03(uint8_t function) bool Xnrg03(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
PzemDrvInit(); case FUNC_EVERY_200_MSECOND:
} if (PzemSerial) { PzemEvery200ms(); }
else if (XNRG_03 == energy_flg) { break;
switch (function) { case FUNC_INIT:
case FUNC_INIT: PzemSnsInit();
PzemSnsInit(); break;
break; case FUNC_PRE_INIT:
case FUNC_EVERY_200_MSECOND: PzemDrvInit();
if (PzemSerial) { PzemEvery200ms(); } break;
break;
}
} }
return result; return result;
} }

View File

@ -583,17 +583,15 @@ void McpSnsInit(void)
void McpDrvInit(void) void McpDrvInit(void)
{ {
if (!energy_flg) { if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) {
if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) { if (pin[GPIO_MCP39F5_RST] < 99) {
if (pin[GPIO_MCP39F5_RST] < 99) { pinMode(pin[GPIO_MCP39F5_RST], OUTPUT);
pinMode(pin[GPIO_MCP39F5_RST], OUTPUT); digitalWrite(pin[GPIO_MCP39F5_RST], 0); // MCP disable - Reset Delta Sigma ADC's
digitalWrite(pin[GPIO_MCP39F5_RST], 0); // MCP disable - Reset Delta Sigma ADC's
}
mcp_calibrate = 0;
mcp_timeout = 2; // Initial wait
mcp_init = 2; // Initial setup steps
energy_flg = XNRG_04;
} }
mcp_calibrate = 0;
mcp_timeout = 2; // Initial wait
mcp_init = 2; // Initial setup steps
energy_flg = XNRG_04;
} }
} }
@ -651,28 +649,26 @@ bool McpCommand(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg04(uint8_t function) bool Xnrg04(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
McpDrvInit(); case FUNC_LOOP:
} if (McpSerial) { McpSerialInput(); }
else if (XNRG_04 == energy_flg) { break;
switch (function) { case FUNC_ENERGY_EVERY_SECOND:
case FUNC_LOOP: if (McpSerial) { McpEverySecond(); }
if (McpSerial) { McpSerialInput(); } break;
break; case FUNC_COMMAND:
case FUNC_INIT: result = McpCommand();
McpSnsInit(); break;
break; case FUNC_INIT:
case FUNC_ENERGY_EVERY_SECOND: McpSnsInit();
if (McpSerial) { McpEverySecond(); } break;
break; case FUNC_PRE_INIT:
case FUNC_COMMAND: McpDrvInit();
result = McpCommand(); break;
break;
}
} }
return result; return result;
} }

View File

@ -109,10 +109,8 @@ void PzemAcSnsInit(void)
void PzemAcDrvInit(void) void PzemAcDrvInit(void)
{ {
if (!energy_flg) { if ((pin[GPIO_PZEM016_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) {
if ((pin[GPIO_PZEM016_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { energy_flg = XNRG_05;
energy_flg = XNRG_05;
}
} }
} }
@ -120,22 +118,20 @@ void PzemAcDrvInit(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg05(uint8_t function) bool Xnrg05(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
PzemAcDrvInit(); case FUNC_ENERGY_EVERY_SECOND:
} if (uptime > 4) { PzemAcEverySecond(); } // Fix start up issue #5875
else if (XNRG_05 == energy_flg) { break;
switch (function) { case FUNC_INIT:
case FUNC_INIT: PzemAcSnsInit();
PzemAcSnsInit(); break;
break; case FUNC_PRE_INIT:
case FUNC_ENERGY_EVERY_SECOND: PzemAcDrvInit();
if (uptime > 4) { PzemAcEverySecond(); } // Fix start up issue #5875 break;
break;
}
} }
return result; return result;
} }

View File

@ -88,10 +88,8 @@ void PzemDcSnsInit(void)
void PzemDcDrvInit(void) void PzemDcDrvInit(void)
{ {
if (!energy_flg) { if ((pin[GPIO_PZEM017_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) {
if ((pin[GPIO_PZEM017_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) { energy_flg = XNRG_06;
energy_flg = XNRG_06;
}
} }
} }
@ -99,22 +97,20 @@ void PzemDcDrvInit(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg06(uint8_t function) bool Xnrg06(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
PzemDcDrvInit(); case FUNC_ENERGY_EVERY_SECOND:
} if (uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875
else if (XNRG_06 == energy_flg) { break;
switch (function) { case FUNC_INIT:
case FUNC_INIT: PzemDcSnsInit();
PzemDcSnsInit(); break;
break; case FUNC_PRE_INIT:
case FUNC_ENERGY_EVERY_SECOND: PzemDcDrvInit();
if (uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875 break;
break;
}
} }
return result; return result;
} }

View File

@ -169,19 +169,17 @@ void Ade7953EnergyEverySecond()
void Ade7953DrvInit(void) void Ade7953DrvInit(void)
{ {
if (!energy_flg) { if (i2c_flg && (pin[GPIO_ADE7953_IRQ] < 99)) { // Irq on GPIO16 is not supported...
if (i2c_flg && (pin[GPIO_ADE7953_IRQ] < 99)) { // Irq on GPIO16 is not supported... delay(100); // Need 100mS to init ADE7953
delay(100); // Need 100mS to init ADE7953 if (I2cDevice(ADE7953_ADDR)) {
if (I2cDevice(ADE7953_ADDR)) { if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
if (HLW_PREF_PULSE == Settings.energy_power_calibration) { Settings.energy_power_calibration = ADE7953_PREF;
Settings.energy_power_calibration = ADE7953_PREF; Settings.energy_voltage_calibration = ADE7953_UREF;
Settings.energy_voltage_calibration = ADE7953_UREF; Settings.energy_current_calibration = ADE7953_IREF;
Settings.energy_current_calibration = ADE7953_IREF; }
} AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR);
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR); Ade7953.init_step = 2;
Ade7953.init_step = 2; energy_flg = XNRG_07;
energy_flg = XNRG_07;
}
} }
} }
} }
@ -234,22 +232,20 @@ bool Ade7953Command(void)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg07(uint8_t function) bool Xnrg07(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
Ade7953DrvInit(); case FUNC_ENERGY_EVERY_SECOND:
} Ade7953EnergyEverySecond();
else if (XNRG_07 == energy_flg) { break;
switch (function) { case FUNC_COMMAND:
case FUNC_ENERGY_EVERY_SECOND: result = Ade7953Command();
Ade7953EnergyEverySecond(); break;
break; case FUNC_PRE_INIT:
case FUNC_COMMAND: Ade7953DrvInit();
result = Ade7953Command(); break;
break;
}
} }
return result; return result;
} }

View File

@ -1,5 +1,5 @@
/* /*
xnrg_09_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota xnrg_08_sdm120.ino - Eastron SDM120-Modbus energy meter support for Sonoff-Tasmota
Copyright (C) 2019 Gennaro Tortone and Theo Arends Copyright (C) 2019 Gennaro Tortone and Theo Arends
@ -25,7 +25,7 @@
* Based on: https://github.com/reaper7/SDM_Energy_Meter * Based on: https://github.com/reaper7/SDM_Energy_Meter
\*********************************************************************************************/ \*********************************************************************************************/
#define XNRG_09 9 #define XNRG_08 8
// can be user defined in my_user_config.h // can be user defined in my_user_config.h
#ifndef SDM120_SPEED #ifndef SDM120_SPEED
@ -189,10 +189,8 @@ void Sdm120SnsInit(void)
void Sdm120DrvInit(void) void Sdm120DrvInit(void)
{ {
if (!energy_flg) { if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) {
if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) { energy_flg = XNRG_08;
energy_flg = XNRG_09;
}
} }
} }
@ -245,33 +243,31 @@ void Sdm220Show(bool json)
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
int Xnrg09(uint8_t function) bool Xnrg08(uint8_t function)
{ {
int result = 0; bool result = false;
if (FUNC_PRE_INIT == function) { switch (function) {
Sdm120DrvInit(); case FUNC_EVERY_250_MSECOND:
} if (uptime > 4) { SDM120Every250ms(); }
else if (XNRG_09 == energy_flg) { break;
switch (function) { case FUNC_JSON_APPEND:
case FUNC_INIT: Sdm220Show(1);
Sdm120SnsInit(); break;
break;
case FUNC_EVERY_250_MSECOND:
if (uptime > 4) { SDM120Every250ms(); }
break;
case FUNC_ENERGY_RESET:
Sdm220Reset();
break;
case FUNC_JSON_APPEND:
Sdm220Show(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
Sdm220Show(0); Sdm220Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} case FUNC_ENERGY_RESET:
Sdm220Reset();
break;
case FUNC_INIT:
Sdm120SnsInit();
break;
case FUNC_PRE_INIT:
Sdm120DrvInit();
break;
} }
return result; return result;
} }

View File

@ -19,10 +19,12 @@
#ifdef USE_ENERGY_SENSOR #ifdef USE_ENERGY_SENSOR
uint8_t xnrg_active_driver_number = 0;
#ifdef XFUNC_PTR_IN_ROM #ifdef XFUNC_PTR_IN_ROM
int (* const xnrg_func_ptr[])(uint8_t) PROGMEM = { // Energy driver Function Pointers bool (* const xnrg_func_ptr[])(uint8_t) PROGMEM = { // Energy driver Function Pointers
#else #else
int (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers bool (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers
#endif #endif
#ifdef XNRG_01 #ifdef XNRG_01
@ -92,20 +94,21 @@ int (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers
const uint8_t xnrg_present = sizeof(xnrg_func_ptr) / sizeof(xnrg_func_ptr[0]); // Number of drivers found const uint8_t xnrg_present = sizeof(xnrg_func_ptr) / sizeof(xnrg_func_ptr[0]); // Number of drivers found
int XnrgCall(uint8_t Function) bool XnrgCall(uint8_t function)
{ {
int result = 0; if (FUNC_PRE_INIT == function) {
for (uint32_t x = 0; x < xnrg_present; x++) {
for (uint32_t x = 0; x < xnrg_present; x++) { xnrg_func_ptr[x](function);
result = xnrg_func_ptr[x](Function); if (energy_flg) {
xnrg_active_driver_number = x;
if (result && ((FUNC_SERIAL == Function) || break; // Stop further driver investigation
(FUNC_COMMAND == Function) }
)) {
break;
} }
} }
return result; else if (energy_flg) {
return xnrg_func_ptr[xnrg_active_driver_number](function);
}
return false;
} }
#endif // USE_ENERGY_SENSOR #endif // USE_ENERGY_SENSOR