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,13 +602,12 @@ 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;
@ -615,8 +615,7 @@ int Xnrg08(uint8_t function)
if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) {
Energy.voltage_available = false;
}
energy_flg = XNRG_08;
}
energy_flg = XNRG_16;
}
}
}

View File

@ -249,7 +249,6 @@ 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];
@ -278,7 +277,6 @@ void HlwDrvInit(void)
energy_flg = XNRG_01;
}
}
}
bool HlwCommand(void)
{
@ -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();
case FUNC_EVERY_200_MSECOND:
HlwEvery200ms();
break;
case FUNC_ENERGY_EVERY_SECOND:
HlwEverySecond();
break;
case FUNC_EVERY_200_MSECOND:
HlwEvery200ms();
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,7 +208,6 @@ 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;
@ -219,7 +218,6 @@ void CseDrvInit(void)
energy_flg = XNRG_02;
}
}
}
bool CseCommand(void)
{
@ -249,26 +247,24 @@ 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_SERIAL:
result = CseSerialInput();
break;
case FUNC_ENERGY_EVERY_SECOND:
CseEverySecond();
break;
case FUNC_COMMAND:
result = CseCommand();
break;
case FUNC_SERIAL:
result = CseSerialInput();
case FUNC_PRE_INIT:
CseDrvInit();
break;
}
}
return result;
}

View File

@ -211,33 +211,29 @@ 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;
}
}
}
/*********************************************************************************************\
* 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;
}
case FUNC_INIT:
PzemSnsInit();
break;
case FUNC_PRE_INIT:
PzemDrvInit();
break;
}
return result;
}

View File

@ -583,7 +583,6 @@ 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);
@ -595,7 +594,6 @@ void McpDrvInit(void)
energy_flg = XNRG_04;
}
}
}
bool McpCommand(void)
{
@ -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;
}
case FUNC_INIT:
McpSnsInit();
break;
case FUNC_PRE_INIT:
McpDrvInit();
break;
}
return result;
}

View File

@ -109,33 +109,29 @@ void PzemAcSnsInit(void)
void PzemAcDrvInit(void)
{
if (!energy_flg) {
if ((pin[GPIO_PZEM016_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) {
energy_flg = XNRG_05;
}
}
}
/*********************************************************************************************\
* 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;
}
case FUNC_INIT:
PzemAcSnsInit();
break;
case FUNC_PRE_INIT:
PzemAcDrvInit();
break;
}
return result;
}

View File

@ -88,33 +88,29 @@ void PzemDcSnsInit(void)
void PzemDcDrvInit(void)
{
if (!energy_flg) {
if ((pin[GPIO_PZEM017_RX] < 99) && (pin[GPIO_PZEM0XX_TX] < 99)) {
energy_flg = XNRG_06;
}
}
}
/*********************************************************************************************\
* 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;
}
case FUNC_INIT:
PzemDcSnsInit();
break;
case FUNC_PRE_INIT:
PzemDcDrvInit();
break;
}
return result;
}

View File

@ -169,7 +169,6 @@ 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)) {
@ -184,7 +183,6 @@ void Ade7953DrvInit(void)
}
}
}
}
bool Ade7953Command(void)
{
@ -234,14 +232,10 @@ 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();
@ -249,7 +243,9 @@ int Xnrg07(uint8_t function)
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;
}
energy_flg = XNRG_08;
}
}
@ -245,24 +243,14 @@ 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;
@ -271,7 +259,15 @@ int Xnrg09(uint8_t function)
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;
if (FUNC_PRE_INIT == function) {
for (uint32_t x = 0; x < xnrg_present; x++) {
result = xnrg_func_ptr[x](Function);
if (result && ((FUNC_SERIAL == Function) ||
(FUNC_COMMAND == Function)
)) {
break;
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