Refactor commands

Refactor commands
This commit is contained in:
Theo Arends 2019-08-11 15:18:11 +02:00
parent 01ca5f5cfb
commit 305cb8fd7e
4 changed files with 178 additions and 206 deletions

View File

@ -32,6 +32,10 @@
#define D_JSON_RF_PULSE "Pulse"
#define D_JSON_RF_REPEAT "Repeat"
const char kRfSendCommands[] PROGMEM = D_CMND_RFSEND;
void (* const RfSendCommand[])(void) PROGMEM = { &CmndRfSend };
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
@ -87,12 +91,10 @@ void RfInit(void)
* Commands
\*********************************************************************************************/
bool RfSendCommand(void)
void CmndRfSend(void)
{
bool serviced = true;
bool error = false;
if (!strcasecmp_P(XdrvMailbox.topic, PSTR(D_CMND_RFSEND))) {
if (XdrvMailbox.data_len) {
unsigned long data = 0;
unsigned int bits = 24;
@ -145,7 +147,7 @@ bool RfSendCommand(void)
if (!bits) { bits = 24; } // Default 24 bits
if (data) {
mySwitch.send(data, bits);
Response_P(PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_DONE "\"}"));
ResponseCmndDone();
} else {
error = true;
}
@ -156,10 +158,6 @@ bool RfSendCommand(void)
Response_P(PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_NO " " D_JSON_RF_DATA ", " D_JSON_RF_BITS ", " D_JSON_RF_PROTOCOL ", " D_JSON_RF_REPEAT " " D_JSON_OR " " D_JSON_RF_PULSE "\"}"));
}
}
else serviced = false; // Unknown command
return serviced;
}
/*********************************************************************************************\
* Interface
@ -171,9 +169,6 @@ bool Xdrv17(uint8_t function)
if ((pin[GPIO_RFSEND] < 99) || (pin[GPIO_RFRECV] < 99)) {
switch (function) {
case FUNC_INIT:
RfInit();
break;
case FUNC_EVERY_50_MSECOND:
if (pin[GPIO_RFRECV] < 99) {
RfReceiveCheck();
@ -181,9 +176,12 @@ bool Xdrv17(uint8_t function)
break;
case FUNC_COMMAND:
if (pin[GPIO_RFSEND] < 99) {
result = RfSendCommand();
result = DecodeCommand(kRfSendCommands, RfSendCommand);
}
break;
case FUNC_INIT:
RfInit();
break;
}
}
return result;

View File

@ -30,6 +30,10 @@ const uint8_t kIFan02Speed[MAX_FAN_SPEED] = { 0x00, 0x01, 0x03, 0x05 };
const uint8_t kIFan03Speed[MAX_FAN_SPEED +2] = { 0x00, 0x01, 0x03, 0x04, 0x05, 0x06 };
const uint8_t kIFan03Sequence[MAX_FAN_SPEED][MAX_FAN_SPEED] = {{0, 2, 2, 2}, {0, 1, 2, 4}, {1, 1, 2, 5}, {4, 4, 5, 3}};
const char kSonoffIfanCommands[] PROGMEM = D_CMND_FANSPEED;
void (* const SonoffIfanCommand[])(void) PROGMEM = { &CmndFanspeed };
uint8_t ifan_fanspeed_timer = 0;
uint8_t ifan_fanspeed_goal = 0;
bool ifan_receive_flag = false;
@ -184,18 +188,8 @@ bool SonoffIfanSerialInput(void)
* Commands
\*********************************************************************************************/
enum SonoffIfanCommands { CMND_FANSPEED };
const char kSonoffIfanCommands[] PROGMEM = D_CMND_FANSPEED;
bool SonoffIfanCommand(void)
void CmndFanspeed(void)
{
bool serviced = true;
int command_code = GetCommandCode(XdrvMailbox.command, CMDSZ, XdrvMailbox.topic, kSonoffIfanCommands);
if (-1 == command_code) {
serviced = false; // Unknown command
}
else if (CMND_FANSPEED == command_code) {
if (XdrvMailbox.data_len > 0) {
if ('-' == XdrvMailbox.data[0]) {
XdrvMailbox.payload = (int16_t)GetFanspeed() -1;
@ -210,9 +204,6 @@ bool SonoffIfanCommand(void)
SonoffIFanSetFanspeed(XdrvMailbox.payload, true);
}
ResponseCmndNumber(GetFanspeed());
} else serviced = false; // Unknown command
return serviced;
}
/*********************************************************************************************/
@ -262,7 +253,7 @@ bool Xdrv22(uint8_t function)
result = SonoffIfanSerialInput();
break;
case FUNC_COMMAND:
result = SonoffIfanCommand();
result = DecodeCommand(kSonoffIfanCommands, SonoffIfanCommand);
break;
case FUNC_MODULE_INIT:
result = SonoffIfanInit();

View File

@ -24,6 +24,10 @@
#define XSNS_01 1
const char kCounterCommands[] PROGMEM = D_CMND_COUNTER "|" D_CMND_COUNTERTYPE "|" D_CMND_COUNTERDEBOUNCE ;
void (* const CounterCommand[])(void) PROGMEM = { &CmndCounter, &CmndCounterType, &CmndCounterDebounce };
unsigned long last_counter_timer[MAX_COUNTERS]; // Last counter time in micro seconds
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception
@ -93,17 +97,10 @@ void CounterInit(void)
}
}
#ifdef USE_WEBSERVER
const char HTTP_SNS_COUNTER[] PROGMEM =
"{s}" D_COUNTER "%d{m}%s%s{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
#endif // USE_WEBSERVER
void CounterShow(bool json)
{
char stemp[10];
bool header = false;
uint8_t dsxflg = 0;
uint8_t header = 0;
for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
if (pin[GPIO_CNTR1 +i] < 99) {
char counter[33];
@ -117,11 +114,9 @@ void CounterShow(bool json)
if (json) {
if (!header) {
ResponseAppend_P(PSTR(",\"COUNTER\":{"));
stemp[0] = '\0';
}
header++;
ResponseAppend_P(PSTR("%s\"C%d\":%s"), stemp, i +1, counter);
strlcpy(stemp, ",", sizeof(stemp));
ResponseAppend_P(PSTR("%s\"C%d\":%s"), (header)?",":"", i +1, counter);
header = true;
#ifdef USE_DOMOTICZ
if ((0 == tele_period) && (1 == dsxflg)) {
DomoticzSensor(DZ_COUNT, RtcSettings.pulse_counter[i]);
@ -130,7 +125,8 @@ void CounterShow(bool json)
#endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_COUNTER, i +1, counter, (bitRead(Settings.pulse_counter_type, i)) ? " " D_UNIT_SECOND : "");
WSContentSend_PD(PSTR("{s}" D_COUNTER "%d{m}%s%s{e}"),
i +1, counter, (bitRead(Settings.pulse_counter_type, i)) ? " " D_UNIT_SECOND : "");
#endif // USE_WEBSERVER
}
}
@ -138,26 +134,17 @@ void CounterShow(bool json)
RtcSettings.pulse_counter[i] = 0xFFFFFFFF; // Set Timer to max in case of no more interrupts due to stall of measured device
}
}
if (json) {
if (header) {
ResponseJsonEnd();
}
}
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
enum CounterCommands { CMND_COUNTER, CMND_COUNTERTYPE, CMND_COUNTERDEBOUNCE };
const char kCounterCommands[] PROGMEM = D_CMND_COUNTER "|" D_CMND_COUNTERTYPE "|" D_CMND_COUNTERDEBOUNCE ;
bool CounterCommand(void)
void CmndCounter(void)
{
bool serviced = true;
int command_code = GetCommandCode(XdrvMailbox.command, CMDSZ, XdrvMailbox.topic, kCounterCommands);
if (CMND_COUNTER == command_code) {
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_COUNTERS)) {
if ((XdrvMailbox.data_len > 0) && (pin[GPIO_CNTR1 + XdrvMailbox.index -1] < 99)) {
if ((XdrvMailbox.data[0] == '-') || (XdrvMailbox.data[0] == '+')) {
@ -171,7 +158,9 @@ bool CounterCommand(void)
Response_P(S_JSON_COMMAND_INDEX_LVALUE, XdrvMailbox.command, XdrvMailbox.index, RtcSettings.pulse_counter[XdrvMailbox.index -1]);
}
}
else if (CMND_COUNTERTYPE == command_code) {
void CmndCounterType(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_COUNTERS)) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1) && (pin[GPIO_CNTR1 + XdrvMailbox.index -1] < 99)) {
bitWrite(Settings.pulse_counter_type, XdrvMailbox.index -1, XdrvMailbox.payload &1);
@ -181,16 +170,14 @@ bool CounterCommand(void)
ResponseCmndIdxNumber(bitRead(Settings.pulse_counter_type, XdrvMailbox.index -1));
}
}
else if (CMND_COUNTERDEBOUNCE == command_code) {
void CmndCounterDebounce(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 32001)) {
Settings.pulse_counter_debounce = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings.pulse_counter_debounce);
}
else serviced = false; // Unknown command
return serviced;
}
/*********************************************************************************************\
* Interface
@ -201,9 +188,6 @@ bool Xsns01(uint8_t function)
bool result = false;
switch (function) {
case FUNC_INIT:
CounterInit();
break;
case FUNC_JSON_APPEND:
CounterShow(1);
break;
@ -217,7 +201,10 @@ bool Xsns01(uint8_t function)
CounterSaveState();
break;
case FUNC_COMMAND:
result = CounterCommand();
result = DecodeCommand(kCounterCommands, CounterCommand);
break;
case FUNC_INIT:
CounterInit();
break;
}
return result;

View File

@ -183,16 +183,12 @@ void AdcShow(bool json)
\*********************************************************************************************/
#define D_CMND_ADCPARAM "AdcParam"
enum AdcCommands { CMND_ADC, CMND_ADCS, CMND_ADCPARAM };
const char kAdcCommands[] PROGMEM = D_CMND_ADC "|" D_CMND_ADCS "|" D_CMND_ADCPARAM;
bool AdcCommand(void)
{
char command[CMDSZ];
bool serviced = true;
void (* const AdcCommand[])(void) PROGMEM = { &CmndAdc, &CmndAdcs, &CmndAdcParam };
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kAdcCommands);
if (CMND_ADC == command_code) {
void CmndAdc(void)
{
if (ValidAdc() && (XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < ADC0_END)) {
Settings.my_adc0 = XdrvMailbox.payload;
restart_flag = 2;
@ -200,7 +196,9 @@ bool AdcCommand(void)
char stemp1[TOPSZ];
Response_P(PSTR("{\"" D_CMND_ADC "0\":\"%d (%s)\"}"), Settings.my_adc0, GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_adc0, kAdc0Names));
}
else if (CMND_ADCS == command_code) {
void CmndAdcs(void)
{
Response_P(PSTR("{\"" D_CMND_ADCS "\":["));
bool jsflg = false;
char stemp1[TOPSZ];
@ -213,7 +211,9 @@ bool AdcCommand(void)
}
ResponseAppend_P(PSTR("]}"));
}
else if (CMND_ADCPARAM == command_code) {
void CmndAdcParam(void)
{
if (XdrvMailbox.data_len) {
if ((ADC0_TEMP == XdrvMailbox.payload) || (ADC0_LIGHT == XdrvMailbox.payload)) {
// if ((XdrvMailbox.payload == my_adc0) && ((ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0))) {
@ -247,10 +247,6 @@ bool AdcCommand(void)
Response_P(PSTR("{\"" D_CMND_ADCPARAM "\":[%d,%d,%d,%s]}"),
Settings.adc_param_type, Settings.adc_param1, Settings.adc_param2, param3);
}
else serviced = false; // Unknown command
return serviced;
}
/*********************************************************************************************\
* Interface
@ -262,7 +258,7 @@ bool Xsns02(uint8_t function)
switch (function) {
case FUNC_COMMAND:
result = AdcCommand();
result = DecodeCommand(kAdcCommands, AdcCommand);
break;
default:
if ((ADC0_INPUT == my_adc0) || (ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0)) {