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_PULSE "Pulse"
#define D_JSON_RF_REPEAT "Repeat" #define D_JSON_RF_REPEAT "Repeat"
const char kRfSendCommands[] PROGMEM = D_CMND_RFSEND;
void (* const RfSendCommand[])(void) PROGMEM = { &CmndRfSend };
#include <RCSwitch.h> #include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch(); RCSwitch mySwitch = RCSwitch();
@ -87,78 +91,72 @@ void RfInit(void)
* Commands * Commands
\*********************************************************************************************/ \*********************************************************************************************/
bool RfSendCommand(void) void CmndRfSend(void)
{ {
bool serviced = true;
bool error = false; bool error = false;
if (!strcasecmp_P(XdrvMailbox.topic, PSTR(D_CMND_RFSEND))) { if (XdrvMailbox.data_len) {
if (XdrvMailbox.data_len) { unsigned long data = 0;
unsigned long data = 0; unsigned int bits = 24;
unsigned int bits = 24; int protocol = 1;
int protocol = 1; int repeat = 10;
int repeat = 10; int pulse = 350;
int pulse = 350;
char dataBufUc[XdrvMailbox.data_len]; char dataBufUc[XdrvMailbox.data_len];
UpperCase(dataBufUc, XdrvMailbox.data); UpperCase(dataBufUc, XdrvMailbox.data);
StaticJsonBuffer<150> jsonBuf; // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(5) + 40 = 134 StaticJsonBuffer<150> jsonBuf; // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(5) + 40 = 134
JsonObject &root = jsonBuf.parseObject(dataBufUc); JsonObject &root = jsonBuf.parseObject(dataBufUc);
if (root.success()) { if (root.success()) {
// RFsend {"data":0x501014,"bits":24,"protocol":1,"repeat":10,"pulse":350} // RFsend {"data":0x501014,"bits":24,"protocol":1,"repeat":10,"pulse":350}
char parm_uc[10]; char parm_uc[10];
data = strtoul(root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_DATA))], nullptr, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input data = strtoul(root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_DATA))], nullptr, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input
bits = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_BITS))]; bits = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_BITS))];
protocol = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PROTOCOL))]; protocol = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PROTOCOL))];
repeat = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_REPEAT))]; repeat = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_REPEAT))];
pulse = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PULSE))]; pulse = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PULSE))];
} else { } else {
// RFsend data, bits, protocol, repeat, pulse // RFsend data, bits, protocol, repeat, pulse
char *p; char *p;
uint8_t i = 0; uint8_t i = 0;
for (char *str = strtok_r(XdrvMailbox.data, ", ", &p); str && i < 5; str = strtok_r(nullptr, ", ", &p)) { for (char *str = strtok_r(XdrvMailbox.data, ", ", &p); str && i < 5; str = strtok_r(nullptr, ", ", &p)) {
switch (i++) { switch (i++) {
case 0: case 0:
data = strtoul(str, nullptr, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input data = strtoul(str, nullptr, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input
break; break;
case 1: case 1:
bits = atoi(str); bits = atoi(str);
break; break;
case 2: case 2:
protocol = atoi(str); protocol = atoi(str);
break; break;
case 3: case 3:
repeat = atoi(str); repeat = atoi(str);
break; break;
case 4: case 4:
pulse = atoi(str); pulse = atoi(str);
}
} }
} }
}
if (!protocol) { protocol = 1; } if (!protocol) { protocol = 1; }
mySwitch.setProtocol(protocol); mySwitch.setProtocol(protocol);
if (!pulse) { pulse = 350; } // Default pulse length for protocol 1 if (!pulse) { pulse = 350; } // Default pulse length for protocol 1
mySwitch.setPulseLength(pulse); mySwitch.setPulseLength(pulse);
if (!repeat) { repeat = 10; } // Default at init if (!repeat) { repeat = 10; } // Default at init
mySwitch.setRepeatTransmit(repeat); mySwitch.setRepeatTransmit(repeat);
if (!bits) { bits = 24; } // Default 24 bits if (!bits) { bits = 24; } // Default 24 bits
if (data) { if (data) {
mySwitch.send(data, bits); mySwitch.send(data, bits);
Response_P(PSTR("{\"" D_CMND_RFSEND "\":\"" D_JSON_DONE "\"}")); ResponseCmndDone();
} else {
error = true;
}
} else { } else {
error = true; error = true;
} }
if (error) { } else {
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 "\"}")); error = true;
} }
if (error) {
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;
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -171,9 +169,6 @@ bool Xdrv17(uint8_t function)
if ((pin[GPIO_RFSEND] < 99) || (pin[GPIO_RFRECV] < 99)) { if ((pin[GPIO_RFSEND] < 99) || (pin[GPIO_RFRECV] < 99)) {
switch (function) { switch (function) {
case FUNC_INIT:
RfInit();
break;
case FUNC_EVERY_50_MSECOND: case FUNC_EVERY_50_MSECOND:
if (pin[GPIO_RFRECV] < 99) { if (pin[GPIO_RFRECV] < 99) {
RfReceiveCheck(); RfReceiveCheck();
@ -181,9 +176,12 @@ bool Xdrv17(uint8_t function)
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
if (pin[GPIO_RFSEND] < 99) { if (pin[GPIO_RFSEND] < 99) {
result = RfSendCommand(); result = DecodeCommand(kRfSendCommands, RfSendCommand);
} }
break; break;
case FUNC_INIT:
RfInit();
break;
} }
} }
return result; 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 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 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_timer = 0;
uint8_t ifan_fanspeed_goal = 0; uint8_t ifan_fanspeed_goal = 0;
bool ifan_receive_flag = false; bool ifan_receive_flag = false;
@ -184,35 +188,22 @@ bool SonoffIfanSerialInput(void)
* Commands * Commands
\*********************************************************************************************/ \*********************************************************************************************/
enum SonoffIfanCommands { CMND_FANSPEED }; void CmndFanspeed(void)
const char kSonoffIfanCommands[] PROGMEM = D_CMND_FANSPEED;
bool SonoffIfanCommand(void)
{ {
bool serviced = true; if (XdrvMailbox.data_len > 0) {
if ('-' == XdrvMailbox.data[0]) {
int command_code = GetCommandCode(XdrvMailbox.command, CMDSZ, XdrvMailbox.topic, kSonoffIfanCommands); XdrvMailbox.payload = (int16_t)GetFanspeed() -1;
if (-1 == command_code) { if (XdrvMailbox.payload < 0) { XdrvMailbox.payload = MAX_FAN_SPEED -1; }
serviced = false; // Unknown command }
else if ('+' == XdrvMailbox.data[0]) {
XdrvMailbox.payload = GetFanspeed() +1;
if (XdrvMailbox.payload > MAX_FAN_SPEED -1) { XdrvMailbox.payload = 0; }
}
} }
else if (CMND_FANSPEED == command_code) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_FAN_SPEED)) {
if (XdrvMailbox.data_len > 0) { SonoffIFanSetFanspeed(XdrvMailbox.payload, true);
if ('-' == XdrvMailbox.data[0]) { }
XdrvMailbox.payload = (int16_t)GetFanspeed() -1; ResponseCmndNumber(GetFanspeed());
if (XdrvMailbox.payload < 0) { XdrvMailbox.payload = MAX_FAN_SPEED -1; }
}
else if ('+' == XdrvMailbox.data[0]) {
XdrvMailbox.payload = GetFanspeed() +1;
if (XdrvMailbox.payload > MAX_FAN_SPEED -1) { XdrvMailbox.payload = 0; }
}
}
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < MAX_FAN_SPEED)) {
SonoffIFanSetFanspeed(XdrvMailbox.payload, true);
}
ResponseCmndNumber(GetFanspeed());
} else serviced = false; // Unknown command
return serviced;
} }
/*********************************************************************************************/ /*********************************************************************************************/
@ -262,7 +253,7 @@ bool Xdrv22(uint8_t function)
result = SonoffIfanSerialInput(); result = SonoffIfanSerialInput();
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = SonoffIfanCommand(); result = DecodeCommand(kSonoffIfanCommands, SonoffIfanCommand);
break; break;
case FUNC_MODULE_INIT: case FUNC_MODULE_INIT:
result = SonoffIfanInit(); result = SonoffIfanInit();

View File

@ -24,6 +24,10 @@
#define XSNS_01 1 #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 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 #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) void CounterShow(bool json)
{ {
char stemp[10]; bool header = false;
uint8_t dsxflg = 0; uint8_t dsxflg = 0;
uint8_t header = 0;
for (uint32_t i = 0; i < MAX_COUNTERS; i++) { for (uint32_t i = 0; i < MAX_COUNTERS; i++) {
if (pin[GPIO_CNTR1 +i] < 99) { if (pin[GPIO_CNTR1 +i] < 99) {
char counter[33]; char counter[33];
@ -117,11 +114,9 @@ void CounterShow(bool json)
if (json) { if (json) {
if (!header) { if (!header) {
ResponseAppend_P(PSTR(",\"COUNTER\":{")); ResponseAppend_P(PSTR(",\"COUNTER\":{"));
stemp[0] = '\0';
} }
header++; ResponseAppend_P(PSTR("%s\"C%d\":%s"), (header)?",":"", i +1, counter);
ResponseAppend_P(PSTR("%s\"C%d\":%s"), stemp, i +1, counter); header = true;
strlcpy(stemp, ",", sizeof(stemp));
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if ((0 == tele_period) && (1 == dsxflg)) { if ((0 == tele_period) && (1 == dsxflg)) {
DomoticzSensor(DZ_COUNT, RtcSettings.pulse_counter[i]); DomoticzSensor(DZ_COUNT, RtcSettings.pulse_counter[i]);
@ -130,7 +125,8 @@ void CounterShow(bool json)
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } 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 #endif // USE_WEBSERVER
} }
} }
@ -138,10 +134,8 @@ 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 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) {
if (header) { ResponseJsonEnd();
ResponseJsonEnd();
}
} }
} }
@ -149,47 +143,40 @@ void CounterShow(bool json)
* Commands * Commands
\*********************************************************************************************/ \*********************************************************************************************/
enum CounterCommands { CMND_COUNTER, CMND_COUNTERTYPE, CMND_COUNTERDEBOUNCE }; void CmndCounter(void)
const char kCounterCommands[] PROGMEM = D_CMND_COUNTER "|" D_CMND_COUNTERTYPE "|" D_CMND_COUNTERDEBOUNCE ;
bool CounterCommand(void)
{ {
bool serviced = true; if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_COUNTERS)) {
if ((XdrvMailbox.data_len > 0) && (pin[GPIO_CNTR1 + XdrvMailbox.index -1] < 99)) {
int command_code = GetCommandCode(XdrvMailbox.command, CMDSZ, XdrvMailbox.topic, kCounterCommands); if ((XdrvMailbox.data[0] == '-') || (XdrvMailbox.data[0] == '+')) {
if (CMND_COUNTER == command_code) { RtcSettings.pulse_counter[XdrvMailbox.index -1] += XdrvMailbox.payload;
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_COUNTERS)) { Settings.pulse_counter[XdrvMailbox.index -1] += XdrvMailbox.payload;
if ((XdrvMailbox.data_len > 0) && (pin[GPIO_CNTR1 + XdrvMailbox.index -1] < 99)) { } else {
if ((XdrvMailbox.data[0] == '-') || (XdrvMailbox.data[0] == '+')) { RtcSettings.pulse_counter[XdrvMailbox.index -1] = XdrvMailbox.payload;
RtcSettings.pulse_counter[XdrvMailbox.index -1] += XdrvMailbox.payload; Settings.pulse_counter[XdrvMailbox.index -1] = XdrvMailbox.payload;
Settings.pulse_counter[XdrvMailbox.index -1] += XdrvMailbox.payload;
} else {
RtcSettings.pulse_counter[XdrvMailbox.index -1] = XdrvMailbox.payload;
Settings.pulse_counter[XdrvMailbox.index -1] = XdrvMailbox.payload;
}
} }
Response_P(S_JSON_COMMAND_INDEX_LVALUE, XdrvMailbox.command, XdrvMailbox.index, RtcSettings.pulse_counter[XdrvMailbox.index -1]);
} }
Response_P(S_JSON_COMMAND_INDEX_LVALUE, XdrvMailbox.command, XdrvMailbox.index, RtcSettings.pulse_counter[XdrvMailbox.index -1]);
} }
else if (CMND_COUNTERTYPE == command_code) { }
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);
RtcSettings.pulse_counter[XdrvMailbox.index -1] = 0;
Settings.pulse_counter[XdrvMailbox.index -1] = 0;
}
ResponseCmndIdxNumber(bitRead(Settings.pulse_counter_type, XdrvMailbox.index -1));
}
}
else if (CMND_COUNTERDEBOUNCE == command_code) {
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; 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);
RtcSettings.pulse_counter[XdrvMailbox.index -1] = 0;
Settings.pulse_counter[XdrvMailbox.index -1] = 0;
}
ResponseCmndIdxNumber(bitRead(Settings.pulse_counter_type, XdrvMailbox.index -1));
}
}
void CmndCounterDebounce(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 32001)) {
Settings.pulse_counter_debounce = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings.pulse_counter_debounce);
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -201,9 +188,6 @@ bool Xsns01(uint8_t function)
bool result = false; bool result = false;
switch (function) { switch (function) {
case FUNC_INIT:
CounterInit();
break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
CounterShow(1); CounterShow(1);
break; break;
@ -217,7 +201,10 @@ bool Xsns01(uint8_t function)
CounterSaveState(); CounterSaveState();
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = CounterCommand(); result = DecodeCommand(kCounterCommands, CounterCommand);
break;
case FUNC_INIT:
CounterInit();
break; break;
} }
return result; return result;

View File

@ -183,73 +183,69 @@ void AdcShow(bool json)
\*********************************************************************************************/ \*********************************************************************************************/
#define D_CMND_ADCPARAM "AdcParam" #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; const char kAdcCommands[] PROGMEM = D_CMND_ADC "|" D_CMND_ADCS "|" D_CMND_ADCPARAM;
bool AdcCommand(void) void (* const AdcCommand[])(void) PROGMEM = { &CmndAdc, &CmndAdcs, &CmndAdcParam };
void CmndAdc(void)
{ {
char command[CMDSZ]; if (ValidAdc() && (XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < ADC0_END)) {
bool serviced = true; Settings.my_adc0 = XdrvMailbox.payload;
restart_flag = 2;
}
char stemp1[TOPSZ];
Response_P(PSTR("{\"" D_CMND_ADC "0\":\"%d (%s)\"}"), Settings.my_adc0, GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_adc0, kAdc0Names));
}
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kAdcCommands); void CmndAdcs(void)
if (CMND_ADC == command_code) { {
if (ValidAdc() && (XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < ADC0_END)) { Response_P(PSTR("{\"" D_CMND_ADCS "\":["));
Settings.my_adc0 = XdrvMailbox.payload; bool jsflg = false;
restart_flag = 2; char stemp1[TOPSZ];
for (uint32_t i = 0; i < ADC0_END; i++) {
if (jsflg) {
ResponseAppend_P(PSTR(","));
} }
char stemp1[TOPSZ]; jsflg = true;
Response_P(PSTR("{\"" D_CMND_ADC "0\":\"%d (%s)\"}"), Settings.my_adc0, GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_adc0, kAdc0Names)); ResponseAppend_P(PSTR("\"%d (%s)\""), i, GetTextIndexed(stemp1, sizeof(stemp1), i, kAdc0Names));
} }
else if (CMND_ADCS == command_code) { ResponseAppend_P(PSTR("]}"));
Response_P(PSTR("{\"" D_CMND_ADCS "\":[")); }
bool jsflg = false;
char stemp1[TOPSZ]; void CmndAdcParam(void)
for (uint32_t i = 0; i < ADC0_END; i++) { {
if (jsflg) { if (XdrvMailbox.data_len) {
ResponseAppend_P(PSTR(",")); if ((ADC0_TEMP == XdrvMailbox.payload) || (ADC0_LIGHT == XdrvMailbox.payload)) {
}
jsflg = true;
ResponseAppend_P(PSTR("\"%d (%s)\""), i, GetTextIndexed(stemp1, sizeof(stemp1), i, kAdc0Names));
}
ResponseAppend_P(PSTR("]}"));
}
else if (CMND_ADCPARAM == command_code) {
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))) { // if ((XdrvMailbox.payload == my_adc0) && ((ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0))) {
if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry
char sub_string[XdrvMailbox.data_len +1]; char sub_string[XdrvMailbox.data_len +1];
// AdcParam 2, 32000, 10000, 3350 // AdcParam 2, 32000, 10000, 3350
// AdcParam 3, 10000, 12518931, -1.405 // AdcParam 3, 10000, 12518931, -1.405
Settings.adc_param_type = XdrvMailbox.payload; Settings.adc_param_type = XdrvMailbox.payload;
// Settings.adc_param_type = my_adc0; // Settings.adc_param_type = my_adc0;
Settings.adc_param1 = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); Settings.adc_param1 = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
Settings.adc_param2 = strtol(subStr(sub_string, XdrvMailbox.data, ",", 3), nullptr, 10); Settings.adc_param2 = strtol(subStr(sub_string, XdrvMailbox.data, ",", 3), nullptr, 10);
Settings.adc_param3 = (int)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 4)) * 10000); Settings.adc_param3 = (int)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 4)) * 10000);
} else { // Set default values based on current adc type } else { // Set default values based on current adc type
// AdcParam 2 // AdcParam 2
// AdcParam 3 // AdcParam 3
Settings.adc_param_type = 0; Settings.adc_param_type = 0;
AdcInit(); AdcInit();
}
} }
} }
// AdcParam
int value = Settings.adc_param3;
uint8_t precision;
for (precision = 4; precision > 0; precision--) {
if (value % 10) { break; }
value /= 10;
}
char param3[33];
dtostrfd(((double)Settings.adc_param3)/10000, precision, param3);
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; // AdcParam
int value = Settings.adc_param3;
uint8_t precision;
for (precision = 4; precision > 0; precision--) {
if (value % 10) { break; }
value /= 10;
}
char param3[33];
dtostrfd(((double)Settings.adc_param3)/10000, precision, param3);
Response_P(PSTR("{\"" D_CMND_ADCPARAM "\":[%d,%d,%d,%s]}"),
Settings.adc_param_type, Settings.adc_param1, Settings.adc_param2, param3);
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -262,7 +258,7 @@ bool Xsns02(uint8_t function)
switch (function) { switch (function) {
case FUNC_COMMAND: case FUNC_COMMAND:
result = AdcCommand(); result = DecodeCommand(kAdcCommands, AdcCommand);
break; break;
default: default:
if ((ADC0_INPUT == my_adc0) || (ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0)) { if ((ADC0_INPUT == my_adc0) || (ADC0_TEMP == my_adc0) || (ADC0_LIGHT == my_adc0)) {