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

View File

@ -21,7 +21,7 @@
#ifdef USE_TUYA_MCU
#define XDRV_16 16
#define XNRG_08 8
#define XNRG_16 16 // Needs to be the last XNRG_xx
#ifndef TUYA_DIMMER_ID
#define TUYA_DIMMER_ID 0
@ -372,6 +372,7 @@ void TuyaPacketProcess(void)
}
else if (Tuya.buffer[5] == 8) { // Long value packet
bool tuya_energy_enabled = (XNRG_16 == energy_flg);
if (fnId == TUYA_MCU_FUNC_DIMMER) {
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);
@ -384,13 +385,13 @@ void TuyaPacketProcess(void)
}
#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;
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;
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;
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
\*********************************************************************************************/
int Xnrg08(uint8_t function)
bool Xnrg16(uint8_t function)
{
int result = 0;
bool result = false;
if (TUYA_DIMMER == my_module_type) {
if (FUNC_PRE_INIT == function) {
if (!energy_flg) {
if (TuyaGetDpId(TUYA_MCU_FUNC_POWER) != 0) {
if (TuyaGetDpId(TUYA_MCU_FUNC_CURRENT) == 0) {
Energy.current_available = false;
}
if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) {
Energy.voltage_available = false;
}
energy_flg = XNRG_08;
if (TuyaGetDpId(TUYA_MCU_FUNC_POWER) != 0) {
if (TuyaGetDpId(TUYA_MCU_FUNC_CURRENT) == 0) {
Energy.current_available = false;
}
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)
{
if (!energy_flg) {
Hlw.model_type = 0; // HLW8012
if (pin[GPIO_HJL_CF] < 99) {
pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF];
pin[GPIO_HJL_CF] = 99;
Hlw.model_type = 1; // HJL-01/BL0937
Hlw.model_type = 0; // HLW8012
if (pin[GPIO_HJL_CF] < 99) {
pin[GPIO_HLW_CF] = pin[GPIO_HJL_CF];
pin[GPIO_HJL_CF] = 99;
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
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_NRG_CF1] < 99) { // Voltage and/or Current monitor
if (99 == pin[GPIO_NRG_SEL]) { // Voltage and/or Current selector
Energy.current_available = false; // Assume Voltage
}
if (pin[GPIO_NRG_CF1] < 99) { // Voltage and/or Current monitor
if (99 == pin[GPIO_NRG_SEL]) { // Voltage and/or Current selector
Energy.current_available = false; // Assume Voltage
}
} else {
Energy.current_available = false;
Energy.voltage_available = false;
}
energy_flg = XNRG_01;
} else {
Energy.current_available = false;
Energy.voltage_available = false;
}
energy_flg = XNRG_01;
}
}
@ -311,28 +309,26 @@ bool HlwCommand(void)
* Interface
\*********************************************************************************************/
int Xnrg01(uint8_t function)
bool Xnrg01(uint8_t function)
{
int result = 0;
bool result = false;
if (FUNC_PRE_INIT == function) {
HlwDrvInit();
}
else if (XNRG_01 == energy_flg) {
switch (function) {
case FUNC_INIT:
HlwSnsInit();
break;
case FUNC_ENERGY_EVERY_SECOND:
HlwEverySecond();
break;
case FUNC_EVERY_200_MSECOND:
HlwEvery200ms();
break;
case FUNC_COMMAND:
result = HlwCommand();
break;
}
switch (function) {
case FUNC_EVERY_200_MSECOND:
HlwEvery200ms();
break;
case FUNC_ENERGY_EVERY_SECOND:
HlwEverySecond();
break;
case FUNC_COMMAND:
result = HlwCommand();
break;
case FUNC_INIT:
HlwSnsInit();
break;
case FUNC_PRE_INIT:
HlwDrvInit();
break;
}
return result;
}

View File

@ -145,7 +145,7 @@ bool CseSerialInput(void)
Energy.data_valid = 0;
CseReceived();
Cse.received = false;
return 1;
return true;
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: " D_CHECKSUM_FAILURE));
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_byte = 0; // Discard
return 0;
return false;
}
/********************************************************************************************/
@ -208,16 +208,14 @@ void CseEverySecond(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
baudrate = 4800;
serial_config = SERIAL_8E1;
if (0 == Settings.param[P_CSE7766_INVALID_POWER]) {
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;
if ((3 == pin[GPIO_CSE7766_RX]) && (1 == pin[GPIO_CSE7766_TX])) { // As it uses 8E1 currently only hardware serial is supported
baudrate = 4800;
serial_config = SERIAL_8E1;
if (0 == Settings.param[P_CSE7766_INVALID_POWER]) {
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;
}
}
@ -249,25 +247,23 @@ bool CseCommand(void)
* Interface
\*********************************************************************************************/
int Xnrg02(uint8_t function)
bool Xnrg02(uint8_t function)
{
int result = 0;
bool result = false;
if (FUNC_PRE_INIT == function) {
CseDrvInit();
}
else if (XNRG_02 == energy_flg) {
switch (function) {
case FUNC_ENERGY_EVERY_SECOND:
CseEverySecond();
break;
case FUNC_COMMAND:
result = CseCommand();
break;
case FUNC_SERIAL:
result = CseSerialInput();
break;
}
switch (function) {
case FUNC_SERIAL:
result = CseSerialInput();
break;
case FUNC_ENERGY_EVERY_SECOND:
CseEverySecond();
break;
case FUNC_COMMAND:
result = CseCommand();
break;
case FUNC_PRE_INIT:
CseDrvInit();
break;
}
return result;
}

View File

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

View File

@ -583,17 +583,15 @@ void McpSnsInit(void)
void McpDrvInit(void)
{
if (!energy_flg) {
if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) {
if (pin[GPIO_MCP39F5_RST] < 99) {
pinMode(pin[GPIO_MCP39F5_RST], OUTPUT);
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;
if ((pin[GPIO_MCP39F5_RX] < 99) && (pin[GPIO_MCP39F5_TX] < 99)) {
if (pin[GPIO_MCP39F5_RST] < 99) {
pinMode(pin[GPIO_MCP39F5_RST], OUTPUT);
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;
}
}
@ -651,28 +649,26 @@ bool McpCommand(void)
* Interface
\*********************************************************************************************/
int Xnrg04(uint8_t function)
bool Xnrg04(uint8_t function)
{
int result = 0;
bool result = false;
if (FUNC_PRE_INIT == function) {
McpDrvInit();
}
else if (XNRG_04 == energy_flg) {
switch (function) {
case FUNC_LOOP:
if (McpSerial) { McpSerialInput(); }
break;
case FUNC_INIT:
McpSnsInit();
break;
case FUNC_ENERGY_EVERY_SECOND:
if (McpSerial) { McpEverySecond(); }
break;
case FUNC_COMMAND:
result = McpCommand();
break;
}
switch (function) {
case FUNC_LOOP:
if (McpSerial) { McpSerialInput(); }
break;
case FUNC_ENERGY_EVERY_SECOND:
if (McpSerial) { McpEverySecond(); }
break;
case FUNC_COMMAND:
result = McpCommand();
break;
case FUNC_INIT:
McpSnsInit();
break;
case FUNC_PRE_INIT:
McpDrvInit();
break;
}
return result;
}

View File

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

View File

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

View File

@ -169,19 +169,17 @@ void Ade7953EnergyEverySecond()
void Ade7953DrvInit(void)
{
if (!energy_flg) {
if (i2c_flg && (pin[GPIO_ADE7953_IRQ] < 99)) { // Irq on GPIO16 is not supported...
delay(100); // Need 100mS to init ADE7953
if (I2cDevice(ADE7953_ADDR)) {
if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
Settings.energy_power_calibration = ADE7953_PREF;
Settings.energy_voltage_calibration = ADE7953_UREF;
Settings.energy_current_calibration = ADE7953_IREF;
}
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR);
Ade7953.init_step = 2;
energy_flg = XNRG_07;
}
if (i2c_flg && (pin[GPIO_ADE7953_IRQ] < 99)) { // Irq on GPIO16 is not supported...
delay(100); // Need 100mS to init ADE7953
if (I2cDevice(ADE7953_ADDR)) {
if (HLW_PREF_PULSE == Settings.energy_power_calibration) {
Settings.energy_power_calibration = ADE7953_PREF;
Settings.energy_voltage_calibration = ADE7953_UREF;
Settings.energy_current_calibration = ADE7953_IREF;
}
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, "ADE7953", ADE7953_ADDR);
Ade7953.init_step = 2;
energy_flg = XNRG_07;
}
}
}
@ -234,22 +232,20 @@ bool Ade7953Command(void)
* Interface
\*********************************************************************************************/
int Xnrg07(uint8_t function)
bool Xnrg07(uint8_t function)
{
int result = 0;
bool result = false;
if (FUNC_PRE_INIT == function) {
Ade7953DrvInit();
}
else if (XNRG_07 == energy_flg) {
switch (function) {
case FUNC_ENERGY_EVERY_SECOND:
Ade7953EnergyEverySecond();
break;
case FUNC_COMMAND:
result = Ade7953Command();
break;
}
switch (function) {
case FUNC_ENERGY_EVERY_SECOND:
Ade7953EnergyEverySecond();
break;
case FUNC_COMMAND:
result = Ade7953Command();
break;
case FUNC_PRE_INIT:
Ade7953DrvInit();
break;
}
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
@ -25,7 +25,7 @@
* 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
#ifndef SDM120_SPEED
@ -189,10 +189,8 @@ void Sdm120SnsInit(void)
void Sdm120DrvInit(void)
{
if (!energy_flg) {
if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) {
energy_flg = XNRG_09;
}
if ((pin[GPIO_SDM120_RX] < 99) && (pin[GPIO_SDM120_TX] < 99)) {
energy_flg = XNRG_08;
}
}
@ -245,33 +243,31 @@ void Sdm220Show(bool json)
* Interface
\*********************************************************************************************/
int Xnrg09(uint8_t function)
bool Xnrg08(uint8_t function)
{
int result = 0;
bool result = false;
if (FUNC_PRE_INIT == function) {
Sdm120DrvInit();
}
else if (XNRG_09 == energy_flg) {
switch (function) {
case FUNC_INIT:
Sdm120SnsInit();
break;
case FUNC_EVERY_250_MSECOND:
if (uptime > 4) { SDM120Every250ms(); }
break;
case FUNC_ENERGY_RESET:
Sdm220Reset();
break;
case FUNC_JSON_APPEND:
Sdm220Show(1);
break;
switch (function) {
case FUNC_EVERY_250_MSECOND:
if (uptime > 4) { SDM120Every250ms(); }
break;
case FUNC_JSON_APPEND:
Sdm220Show(1);
break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
Sdm220Show(0);
break;
case FUNC_WEB_SENSOR:
Sdm220Show(0);
break;
#endif // USE_WEBSERVER
}
case FUNC_ENERGY_RESET:
Sdm220Reset();
break;
case FUNC_INIT:
Sdm120SnsInit();
break;
case FUNC_PRE_INIT:
Sdm120DrvInit();
break;
}
return result;
}

View File

@ -19,10 +19,12 @@
#ifdef USE_ENERGY_SENSOR
uint8_t xnrg_active_driver_number = 0;
#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
int (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers
bool (* const xnrg_func_ptr[])(uint8_t) = { // Energy driver Function Pointers
#endif
#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
int XnrgCall(uint8_t Function)
bool XnrgCall(uint8_t function)
{
int result = 0;
for (uint32_t x = 0; x < xnrg_present; x++) {
result = xnrg_func_ptr[x](Function);
if (result && ((FUNC_SERIAL == Function) ||
(FUNC_COMMAND == Function)
)) {
break;
if (FUNC_PRE_INIT == function) {
for (uint32_t x = 0; x < xnrg_present; x++) {
xnrg_func_ptr[x](function);
if (energy_flg) {
xnrg_active_driver_number = x;
break; // Stop further driver investigation
}
}
}
return result;
else if (energy_flg) {
return xnrg_func_ptr[xnrg_active_driver_number](function);
}
return false;
}
#endif // USE_ENERGY_SENSOR