From ec564736311d798e2e539eb9473c61025c0dc135 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 12 Feb 2023 14:05:33 +0100 Subject: [PATCH] Refactor XxxSet and XxxCal energy commands --- .../tasmota_xdrv_driver/xdrv_03_energy.ino | 64 +++++---------- .../xdrv_03_esp32_energy.ino | 78 ++++++++----------- 2 files changed, 54 insertions(+), 88 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino index 67d891fe7..45613230e 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_energy.ino @@ -911,69 +911,45 @@ void EnergyCommandCalSetResponse(uint32_t cal_type) { } void EnergyCommandCalResponse(uint32_t cal_type) { - Response_P(PSTR("{\"%s\":"), XdrvMailbox.command); - EnergyCommandCalSetResponse(cal_type); -} - -void EnergyCommandSetCalResponse(uint32_t cal_type) { - Response_P(PSTR("{\"%sCal\":"), XdrvMailbox.command); - EnergyCommandCalSetResponse(cal_type); + Energy->command_code = cal_type; // Is XxxCal command too + if (XnrgCall(FUNC_COMMAND)) { // XxxCal + Response_P(PSTR("{\"%s\":"), XdrvMailbox.command); + EnergyCommandCalSetResponse(cal_type); + } } void CmndPowerCal(void) { - Energy->command_code = CMND_POWERCAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_POWER_CALIBRATION); - } + EnergyCommandCalResponse(ENERGY_POWER_CALIBRATION); } - void CmndVoltageCal(void) { - Energy->command_code = CMND_VOLTAGECAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_VOLTAGE_CALIBRATION); - } + EnergyCommandCalResponse(ENERGY_VOLTAGE_CALIBRATION); } - void CmndCurrentCal(void) { - Energy->command_code = CMND_CURRENTCAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_CURRENT_CALIBRATION); - } + EnergyCommandCalResponse(ENERGY_CURRENT_CALIBRATION); +} +void CmndFrequencyCal(void) { + EnergyCommandCalResponse(ENERGY_FREQUENCY_CALIBRATION); } -void CmndFrequencyCal(void) { - Energy->command_code = CMND_FREQUENCYCAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_FREQUENCY_CALIBRATION); +void EnergyCommandSetCalResponse(uint32_t cal_type) { + Energy->command_code = CMND_POWERSET + cal_type; // Adjust for XxxSet command + if (XnrgCall(FUNC_COMMAND)) { // XxxSet + Response_P(PSTR("{\"%sCal\":"), XdrvMailbox.command); + EnergyCommandCalSetResponse(cal_type); } } void CmndPowerSet(void) { - Energy->command_code = CMND_POWERSET; - if (XnrgCall(FUNC_COMMAND)) { // Watt - EnergyCommandSetCalResponse(ENERGY_POWER_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_POWER_CALIBRATION); } - void CmndVoltageSet(void) { - Energy->command_code = CMND_VOLTAGESET; - if (XnrgCall(FUNC_COMMAND)) { // Volt - EnergyCommandSetCalResponse(ENERGY_VOLTAGE_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_VOLTAGE_CALIBRATION); } - void CmndCurrentSet(void) { - Energy->command_code = CMND_CURRENTSET; - if (XnrgCall(FUNC_COMMAND)) { // milliAmpere - EnergyCommandSetCalResponse(ENERGY_CURRENT_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_CURRENT_CALIBRATION); } - void CmndFrequencySet(void) { - Energy->command_code = CMND_FREQUENCYSET; - if (XnrgCall(FUNC_COMMAND)) { // Hz - EnergyCommandSetCalResponse(ENERGY_FREQUENCY_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_FREQUENCY_CALIBRATION); } void CmndModuleAddress(void) { diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino index 85b146bb7..603a6fa2e 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino @@ -1125,69 +1125,59 @@ void EnergyCommandCalSetResponse(uint32_t cal_type) { } void EnergyCommandCalResponse(uint32_t cal_type) { - Response_P(PSTR("{\"%s\":"), XdrvMailbox.command); - EnergyCommandCalSetResponse(cal_type); -} - -void EnergyCommandSetCalResponse(uint32_t cal_type) { - Response_P(PSTR("{\"%sCal\":"), XdrvMailbox.command); - EnergyCommandCalSetResponse(cal_type); + Energy->command_code = cal_type; // Is XxxCal command too + if (XnrgCall(FUNC_COMMAND)) { // XxxCal + Response_P(PSTR("{\"%s\":"), XdrvMailbox.command); + EnergyCommandCalSetResponse(cal_type); + } } void CmndPowerCal(void) { - Energy->command_code = CMND_POWERCAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_POWER_CALIBRATION); - } + EnergyCommandCalResponse(ENERGY_POWER_CALIBRATION); } - void CmndVoltageCal(void) { - Energy->command_code = CMND_VOLTAGECAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_VOLTAGE_CALIBRATION); - } + EnergyCommandCalResponse(ENERGY_VOLTAGE_CALIBRATION); } - void CmndCurrentCal(void) { - Energy->command_code = CMND_CURRENTCAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_CURRENT_CALIBRATION); - } + EnergyCommandCalResponse(ENERGY_CURRENT_CALIBRATION); +} +void CmndFrequencyCal(void) { + EnergyCommandCalResponse(ENERGY_FREQUENCY_CALIBRATION); } -void CmndFrequencyCal(void) { - Energy->command_code = CMND_FREQUENCYCAL; - if (XnrgCall(FUNC_COMMAND)) { // microseconds - EnergyCommandCalResponse(ENERGY_FREQUENCY_CALIBRATION); +void EnergyCommandSetCalResponse(uint32_t cal_type) { + bool serviced = false; + Energy->command_code = CMND_POWERSET + cal_type; // Adjust for XxxSet command + if (0 == XdrvMailbox.index) { // XxxSet0 + for (XdrvMailbox.index = 1; XdrvMailbox.index <= Energy->phase_count; XdrvMailbox.index++) { + if (XnrgCall(FUNC_COMMAND)) { + if (XdrvMailbox.payload > 99) { + EnergySetCalibration(cal_type, XdrvMailbox.payload, XdrvMailbox.index -1); + } + } + } + XdrvMailbox.payload = 2; // No more updates but response only + serviced = true; + } else { + serviced = (XnrgCall(FUNC_COMMAND)); // XxxSet + } + if (serviced) { + Response_P(PSTR("{\"%sCal\":"), XdrvMailbox.command); + EnergyCommandCalSetResponse(cal_type); } } void CmndPowerSet(void) { - Energy->command_code = CMND_POWERSET; - if (XnrgCall(FUNC_COMMAND)) { // Watt - EnergyCommandSetCalResponse(ENERGY_POWER_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_POWER_CALIBRATION); } - void CmndVoltageSet(void) { - Energy->command_code = CMND_VOLTAGESET; - if (XnrgCall(FUNC_COMMAND)) { // Volt - EnergyCommandSetCalResponse(ENERGY_VOLTAGE_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_VOLTAGE_CALIBRATION); } - void CmndCurrentSet(void) { - Energy->command_code = CMND_CURRENTSET; - if (XnrgCall(FUNC_COMMAND)) { // milliAmpere - EnergyCommandSetCalResponse(ENERGY_CURRENT_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_CURRENT_CALIBRATION); } - void CmndFrequencySet(void) { - Energy->command_code = CMND_FREQUENCYSET; - if (XnrgCall(FUNC_COMMAND)) { // Hz - EnergyCommandSetCalResponse(ENERGY_FREQUENCY_CALIBRATION); - } + EnergyCommandSetCalResponse(ENERGY_FREQUENCY_CALIBRATION); } void CmndModuleAddress(void) {