mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-28 05:06:32 +00:00
Refactor energy driver detection
Refactor energy driver detection and function call
This commit is contained in:
parent
cea6592945
commit
75abfc5fd9
@ -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;
|
||||||
|
@ -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,13 +602,12 @@ 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;
|
||||||
@ -615,8 +615,7 @@ int Xnrg08(uint8_t function)
|
|||||||
if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) {
|
if (TuyaGetDpId(TUYA_MCU_FUNC_VOLTAGE) == 0) {
|
||||||
Energy.voltage_available = false;
|
Energy.voltage_available = false;
|
||||||
}
|
}
|
||||||
energy_flg = XNRG_08;
|
energy_flg = XNRG_16;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,6 @@ 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];
|
||||||
@ -278,7 +277,6 @@ void HlwDrvInit(void)
|
|||||||
energy_flg = XNRG_01;
|
energy_flg = XNRG_01;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool HlwCommand(void)
|
bool HlwCommand(void)
|
||||||
{
|
{
|
||||||
@ -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) {
|
|
||||||
HlwDrvInit();
|
|
||||||
}
|
|
||||||
else if (XNRG_01 == energy_flg) {
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_INIT:
|
case FUNC_EVERY_200_MSECOND:
|
||||||
HlwSnsInit();
|
HlwEvery200ms();
|
||||||
break;
|
break;
|
||||||
case FUNC_ENERGY_EVERY_SECOND:
|
case FUNC_ENERGY_EVERY_SECOND:
|
||||||
HlwEverySecond();
|
HlwEverySecond();
|
||||||
break;
|
break;
|
||||||
case FUNC_EVERY_200_MSECOND:
|
|
||||||
HlwEvery200ms();
|
|
||||||
break;
|
|
||||||
case FUNC_COMMAND:
|
case FUNC_COMMAND:
|
||||||
result = HlwCommand();
|
result = HlwCommand();
|
||||||
break;
|
break;
|
||||||
}
|
case FUNC_INIT:
|
||||||
|
HlwSnsInit();
|
||||||
|
break;
|
||||||
|
case FUNC_PRE_INIT:
|
||||||
|
HlwDrvInit();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -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,7 +208,6 @@ 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;
|
||||||
@ -219,7 +218,6 @@ void CseDrvInit(void)
|
|||||||
energy_flg = XNRG_02;
|
energy_flg = XNRG_02;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool CseCommand(void)
|
bool CseCommand(void)
|
||||||
{
|
{
|
||||||
@ -249,26 +247,24 @@ 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) {
|
|
||||||
CseDrvInit();
|
|
||||||
}
|
|
||||||
else if (XNRG_02 == energy_flg) {
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
|
case FUNC_SERIAL:
|
||||||
|
result = CseSerialInput();
|
||||||
|
break;
|
||||||
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_SERIAL:
|
case FUNC_PRE_INIT:
|
||||||
result = CseSerialInput();
|
CseDrvInit();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,33 +211,29 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* Interface
|
* 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) {
|
switch (function) {
|
||||||
case FUNC_INIT:
|
|
||||||
PzemSnsInit();
|
|
||||||
break;
|
|
||||||
case FUNC_EVERY_200_MSECOND:
|
case FUNC_EVERY_200_MSECOND:
|
||||||
if (PzemSerial) { PzemEvery200ms(); }
|
if (PzemSerial) { PzemEvery200ms(); }
|
||||||
break;
|
break;
|
||||||
}
|
case FUNC_INIT:
|
||||||
|
PzemSnsInit();
|
||||||
|
break;
|
||||||
|
case FUNC_PRE_INIT:
|
||||||
|
PzemDrvInit();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -583,7 +583,6 @@ 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);
|
||||||
@ -595,7 +594,6 @@ void McpDrvInit(void)
|
|||||||
energy_flg = XNRG_04;
|
energy_flg = XNRG_04;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool McpCommand(void)
|
bool McpCommand(void)
|
||||||
{
|
{
|
||||||
@ -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) {
|
|
||||||
McpDrvInit();
|
|
||||||
}
|
|
||||||
else if (XNRG_04 == energy_flg) {
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_LOOP:
|
case FUNC_LOOP:
|
||||||
if (McpSerial) { McpSerialInput(); }
|
if (McpSerial) { McpSerialInput(); }
|
||||||
break;
|
break;
|
||||||
case FUNC_INIT:
|
|
||||||
McpSnsInit();
|
|
||||||
break;
|
|
||||||
case FUNC_ENERGY_EVERY_SECOND:
|
case FUNC_ENERGY_EVERY_SECOND:
|
||||||
if (McpSerial) { McpEverySecond(); }
|
if (McpSerial) { McpEverySecond(); }
|
||||||
break;
|
break;
|
||||||
case FUNC_COMMAND:
|
case FUNC_COMMAND:
|
||||||
result = McpCommand();
|
result = McpCommand();
|
||||||
break;
|
break;
|
||||||
}
|
case FUNC_INIT:
|
||||||
|
McpSnsInit();
|
||||||
|
break;
|
||||||
|
case FUNC_PRE_INIT:
|
||||||
|
McpDrvInit();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -109,33 +109,29 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* Interface
|
* 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) {
|
switch (function) {
|
||||||
case FUNC_INIT:
|
|
||||||
PzemAcSnsInit();
|
|
||||||
break;
|
|
||||||
case FUNC_ENERGY_EVERY_SECOND:
|
case FUNC_ENERGY_EVERY_SECOND:
|
||||||
if (uptime > 4) { PzemAcEverySecond(); } // Fix start up issue #5875
|
if (uptime > 4) { PzemAcEverySecond(); } // Fix start up issue #5875
|
||||||
break;
|
break;
|
||||||
}
|
case FUNC_INIT:
|
||||||
|
PzemAcSnsInit();
|
||||||
|
break;
|
||||||
|
case FUNC_PRE_INIT:
|
||||||
|
PzemAcDrvInit();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -88,33 +88,29 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* Interface
|
* 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) {
|
switch (function) {
|
||||||
case FUNC_INIT:
|
|
||||||
PzemDcSnsInit();
|
|
||||||
break;
|
|
||||||
case FUNC_ENERGY_EVERY_SECOND:
|
case FUNC_ENERGY_EVERY_SECOND:
|
||||||
if (uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875
|
if (uptime > 4) { PzemDcEverySecond(); } // Fix start up issue #5875
|
||||||
break;
|
break;
|
||||||
}
|
case FUNC_INIT:
|
||||||
|
PzemDcSnsInit();
|
||||||
|
break;
|
||||||
|
case FUNC_PRE_INIT:
|
||||||
|
PzemDcDrvInit();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,6 @@ 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)) {
|
||||||
@ -184,7 +183,6 @@ void Ade7953DrvInit(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool Ade7953Command(void)
|
bool Ade7953Command(void)
|
||||||
{
|
{
|
||||||
@ -234,14 +232,10 @@ 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) {
|
|
||||||
Ade7953DrvInit();
|
|
||||||
}
|
|
||||||
else if (XNRG_07 == energy_flg) {
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_ENERGY_EVERY_SECOND:
|
case FUNC_ENERGY_EVERY_SECOND:
|
||||||
Ade7953EnergyEverySecond();
|
Ade7953EnergyEverySecond();
|
||||||
@ -249,7 +243,9 @@ int Xnrg07(uint8_t function)
|
|||||||
case FUNC_COMMAND:
|
case FUNC_COMMAND:
|
||||||
result = Ade7953Command();
|
result = Ade7953Command();
|
||||||
break;
|
break;
|
||||||
}
|
case FUNC_PRE_INIT:
|
||||||
|
Ade7953DrvInit();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -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_09;
|
energy_flg = XNRG_08;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,24 +243,14 @@ 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) {
|
|
||||||
Sdm120DrvInit();
|
|
||||||
}
|
|
||||||
else if (XNRG_09 == energy_flg) {
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_INIT:
|
|
||||||
Sdm120SnsInit();
|
|
||||||
break;
|
|
||||||
case FUNC_EVERY_250_MSECOND:
|
case FUNC_EVERY_250_MSECOND:
|
||||||
if (uptime > 4) { SDM120Every250ms(); }
|
if (uptime > 4) { SDM120Every250ms(); }
|
||||||
break;
|
break;
|
||||||
case FUNC_ENERGY_RESET:
|
|
||||||
Sdm220Reset();
|
|
||||||
break;
|
|
||||||
case FUNC_JSON_APPEND:
|
case FUNC_JSON_APPEND:
|
||||||
Sdm220Show(1);
|
Sdm220Show(1);
|
||||||
break;
|
break;
|
||||||
@ -271,7 +259,15 @@ int Xnrg09(uint8_t function)
|
|||||||
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;
|
||||||
}
|
}
|
@ -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++) {
|
||||||
result = xnrg_func_ptr[x](Function);
|
xnrg_func_ptr[x](function);
|
||||||
|
if (energy_flg) {
|
||||||
if (result && ((FUNC_SERIAL == Function) ||
|
xnrg_active_driver_number = x;
|
||||||
(FUNC_COMMAND == Function)
|
break; // Stop further driver investigation
|
||||||
)) {
|
|
||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user