Refactor command line arguments

This commit is contained in:
Theo Arends 2021-01-30 12:48:44 +01:00
parent 1e0631d7e1
commit aea856ca2c
4 changed files with 57 additions and 58 deletions

View File

@ -119,20 +119,20 @@ bool PCA9685_Command(void)
serviced = false;
return serviced;
}
char sub_string[XdrvMailbox.data_len];
char argument[XdrvMailbox.data_len];
for (uint32_t ca=0;ca<XdrvMailbox.data_len;ca++) {
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
if (',' == XdrvMailbox.data[ca]) { paramcount++; }
}
UpperCase(XdrvMailbox.data,XdrvMailbox.data);
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET")) { PCA9685_Reset(); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET")) { PCA9685_Reset(); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"STATUS")) { PCA9685_OutputTelemetry(false); return serviced; }
if (!strcmp(ArgV(argument, 1),"STATUS")) { PCA9685_OutputTelemetry(false); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"PWMF")) {
if (!strcmp(ArgV(argument, 1),"PWMF")) {
if (paramcount > 1) {
uint16_t new_freq = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint16_t new_freq = atoi(ArgV(argument, 2));
if ((new_freq >= 24) && (new_freq <= 1526)) {
PCA9685_SetPWMfreq(new_freq);
Response_P(PSTR("{\"PCA9685\":{\"PWMF\":%i, \"Result\":\"OK\"}}"),new_freq);
@ -143,23 +143,23 @@ bool PCA9685_Command(void)
return serviced;
}
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"PWM")) {
if (!strcmp(ArgV(argument, 1),"PWM")) {
if (paramcount > 1) {
uint8_t pin = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t pin = atoi(ArgV(argument, 2));
if (paramcount > 2) {
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 3), "ON")) {
if (!strcmp(ArgV(argument, 3), "ON")) {
PCA9685_SetPWM(pin, 4096, false);
Response_P(PSTR("{\"PCA9685\":{\"PIN\":%i,\"PWM\":%i}}"),pin,4096);
serviced = true;
return serviced;
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 3), "OFF")) {
if (!strcmp(ArgV(argument, 3), "OFF")) {
PCA9685_SetPWM(pin, 0, false);
Response_P(PSTR("{\"PCA9685\":{\"PIN\":%i,\"PWM\":%i}}"),pin,0);
serviced = true;
return serviced;
}
uint16_t pwm = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
uint16_t pwm = atoi(ArgV(argument, 3));
if ((pin >= 0 && pin <= 15) && (pwm >= 0 && pwm <= 4096)) {
PCA9685_SetPWM(pin, pwm, false);
Response_P(PSTR("{\"PCA9685\":{\"PIN\":%i,\"PWM\":%i}}"),pin,pwm);

View File

@ -451,25 +451,25 @@ bool MCP230xx_Command(void)
serviced = false;
return serviced;
}
char sub_string[XdrvMailbox.data_len];
char argument[XdrvMailbox.data_len];
for (uint32_t ca=0;ca<XdrvMailbox.data_len;ca++) {
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
if (',' == XdrvMailbox.data[ca]) { paramcount++; }
}
UpperCase(XdrvMailbox.data,XdrvMailbox.data);
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET")) { MCP230xx_Reset(1); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET1")) { MCP230xx_Reset(1); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET2")) { MCP230xx_Reset(2); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET3")) { MCP230xx_Reset(3); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET4")) { MCP230xx_Reset(4); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET")) { MCP230xx_Reset(1); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET1")) { MCP230xx_Reset(1); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET2")) { MCP230xx_Reset(2); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET3")) { MCP230xx_Reset(3); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET4")) { MCP230xx_Reset(4); return serviced; }
#ifdef USE_MCP230xx_OUTPUT
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET5")) { MCP230xx_Reset(5); return serviced; }
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"RESET6")) { MCP230xx_Reset(6); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET5")) { MCP230xx_Reset(5); return serviced; }
if (!strcmp(ArgV(argument, 1),"RESET6")) { MCP230xx_Reset(6); return serviced; }
#endif // USE_MCP230xx_OUTPUT
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"INTPRI")) {
if (!strcmp(ArgV(argument, 1),"INTPRI")) {
if (paramcount > 1) {
uint8_t intpri = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t intpri = atoi(ArgV(argument, 2));
if ((intpri >= 0) && (intpri <= 20)) {
Settings.mcp230xx_int_prio = intpri;
Response_P(MCP230XX_INTCFG_RESPONSE,"PRI",99,Settings.mcp230xx_int_prio); // "{\"MCP230xx_INT%s\":{\"D_%i\":%i}}";
@ -481,9 +481,9 @@ bool MCP230xx_Command(void)
}
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"INTTIMER")) {
if (!strcmp(ArgV(argument, 1),"INTTIMER")) {
if (paramcount > 1) {
uint8_t inttim = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t inttim = atoi(ArgV(argument, 2));
if ((inttim >= 0) && (inttim <= 3600)) {
Settings.mcp230xx_int_timer = inttim;
MCP230xx_CheckForIntCounter(); // update register on whether or not we should be counting interrupts
@ -496,19 +496,19 @@ bool MCP230xx_Command(void)
}
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"INTDEF")) {
if (!strcmp(ArgV(argument, 1),"INTDEF")) {
if (paramcount > 1) {
uint8_t pin = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t pin = atoi(ArgV(argument, 2));
if (pin < mcp230xx_pincount) {
if (pin == 0) {
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "0")) validpin=true;
if (!strcmp(ArgV(argument, 2), "0")) validpin=true;
} else {
validpin = true;
}
}
if (validpin) {
if (paramcount > 2) {
uint8_t intdef = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
uint8_t intdef = atoi(ArgV(argument, 3));
if ((intdef >= 0) && (intdef <= 15)) {
Settings.mcp230xx_config[pin].int_report_defer=intdef;
if (Settings.mcp230xx_config[pin].int_count_en) {
@ -535,19 +535,19 @@ bool MCP230xx_Command(void)
}
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"INTCNT")) {
if (!strcmp(ArgV(argument, 1),"INTCNT")) {
if (paramcount > 1) {
uint8_t pin = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t pin = atoi(ArgV(argument, 2));
if (pin < mcp230xx_pincount) {
if (pin == 0) {
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "0")) validpin=true;
if (!strcmp(ArgV(argument, 2), "0")) validpin=true;
} else {
validpin = true;
}
}
if (validpin) {
if (paramcount > 2) {
uint8_t intcnt = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
uint8_t intcnt = atoi(ArgV(argument, 3));
if ((intcnt >= 0) && (intcnt <= 1)) {
Settings.mcp230xx_config[pin].int_count_en=intcnt;
if (Settings.mcp230xx_config[pin].int_report_defer) {
@ -581,19 +581,19 @@ bool MCP230xx_Command(void)
}
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"INTRETAIN")) {
if (!strcmp(ArgV(argument, 1),"INTRETAIN")) {
if (paramcount > 1) {
uint8_t pin = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t pin = atoi(ArgV(argument, 2));
if (pin < mcp230xx_pincount) {
if (pin == 0) {
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "0")) validpin=true;
if (!strcmp(ArgV(argument, 2), "0")) validpin=true;
} else {
validpin = true;
}
}
if (validpin) {
if (paramcount > 2) {
uint8_t int_retain = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
uint8_t int_retain = atoi(ArgV(argument, 3));
if ((int_retain >= 0) && (int_retain <= 1)) {
Settings.mcp230xx_config[pin].int_retain_flag=int_retain;
Response_P(MCP230XX_INTCFG_RESPONSE,"INT_RETAIN",pin,Settings.mcp230xx_config[pin].int_retain_flag);
@ -616,17 +616,17 @@ bool MCP230xx_Command(void)
}
}
uint8_t pin = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1));
uint8_t pin = atoi(ArgV(argument, 1));
if (pin < mcp230xx_pincount) {
if (0 == pin) {
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1), "0")) validpin=true;
if (!strcmp(ArgV(argument, 1), "0")) validpin=true;
} else {
validpin=true;
}
}
if (validpin && (paramcount > 1)) {
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "?")) {
if (!strcmp(ArgV(argument, 2), "?")) {
uint8_t port = 0;
if (pin > 7) { port = 1; }
uint8_t portdata = MCP230xx_readGPIO(port);
@ -652,15 +652,15 @@ bool MCP230xx_Command(void)
if ( mcp230xx_outpinmapping[relay_no] == pin) break;
}
relay_no = TasmotaGlobal.devices_present - mcp230xx_oldoutpincount + relay_no +1;
if ((!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "ON")) || (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "1"))) {
if ((!strcmp(ArgV(argument, 2), "ON")) || (!strcmp(ArgV(argument, 2), "1"))) {
ExecuteCommandPower(relay_no, 1, SRC_IGNORE);
return serviced;
}
if ((!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "OFF")) || (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "0"))) {
if ((!strcmp(ArgV(argument, 2), "OFF")) || (!strcmp(ArgV(argument, 2), "0"))) {
ExecuteCommandPower(relay_no, 0, SRC_IGNORE);
return serviced;
}
if ((!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "T")) || (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 2), "2"))) {
if ((!strcmp(ArgV(argument, 2), "T")) || (!strcmp(ArgV(argument, 2), "2"))) {
ExecuteCommandPower(relay_no, 2, SRC_IGNORE);
return serviced;
}
@ -670,13 +670,13 @@ bool MCP230xx_Command(void)
uint8_t pullup = 0;
uint8_t intmode = 0;
if (paramcount > 1) {
pinmode = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
pinmode = atoi(ArgV(argument, 2));
}
if (paramcount > 2) {
pullup = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
pullup = atoi(ArgV(argument, 3));
}
if (paramcount > 3) {
intmode = atoi(subStr(sub_string, XdrvMailbox.data, ",", 4));
intmode = atoi(ArgV(argument, 4));
}
#ifdef USE_MCP230xx_OUTPUT
if ((pin < mcp230xx_pincount) && (pinmode > 0) && (pinmode < 7) && (pullup < 2) && (paramcount > 2)) {

View File

@ -193,7 +193,7 @@ bool HxCommand(void)
{
bool serviced = true;
bool show_parms = false;
char sub_string[XdrvMailbox.data_len +1];
char argument[XdrvMailbox.data_len];
for (uint32_t ca = 0; ca < XdrvMailbox.data_len; ca++) {
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
@ -206,7 +206,7 @@ bool HxCommand(void)
break;
case 2: // Calibrate
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.weight_reference = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
Settings.weight_reference = strtol(ArgV(argument, 2), nullptr, 10);
}
Hx.scale = 1;
HxReset();
@ -216,26 +216,26 @@ bool HxCommand(void)
break;
case 3: // WeightRef to user reference
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.weight_reference = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
Settings.weight_reference = strtol(ArgV(argument, 2), nullptr, 10);
}
show_parms = true;
break;
case 4: // WeightCal to user calculated value
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.weight_calibration = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
Settings.weight_calibration = strtol(ArgV(argument, 2), nullptr, 10);
Hx.scale = Settings.weight_calibration;
}
show_parms = true;
break;
case 5: // WeightMax
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.weight_max = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10) / 1000;
Settings.weight_max = strtol(ArgV(argument, 2), nullptr, 10) / 1000;
}
show_parms = true;
break;
case 6: // WeightItem
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.weight_item = (unsigned long)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 2)) * 10);
Settings.weight_item = (unsigned long)(CharToFloat(ArgV(argument, 2)) * 10);
}
show_parms = true;
break;
@ -245,13 +245,13 @@ bool HxCommand(void)
break;
case 8: // Json on weight change
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.SensorBits1.hx711_json_weight_change = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10) & 1;
Settings.SensorBits1.hx711_json_weight_change = strtol(ArgV(argument, 2), nullptr, 10) & 1;
}
show_parms = true;
break;
case 9: // WeightDelta
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.weight_change = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
Settings.weight_change = strtol(ArgV(argument, 2), nullptr, 10);
SetWeightDelta();
}
show_parms = true;

View File

@ -500,29 +500,28 @@ bool PN532_Command(void) {
serviced = false;
return serviced;
}
char sub_string[XdrvMailbox.data_len];
char sub_string_tmp[XdrvMailbox.data_len];
char argument[XdrvMailbox.data_len];
for (uint32_t ca=0;ca<XdrvMailbox.data_len;ca++) {
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
if (',' == XdrvMailbox.data[ca]) { paramcount++; }
}
UpperCase(XdrvMailbox.data,XdrvMailbox.data);
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"E")) {
if (!strcmp(ArgV(argument, 1),"E")) {
Pn532.function = 1; // Block 1 of next card/tag will be reset to 0x00...
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 NFC - Next scanned tag data block 1 will be erased"));
ResponseTime_P(PSTR(",\"PN532\":{\"COMMAND\":\"E\"}}"));
return serviced;
}
if (!strcmp(subStr(sub_string, XdrvMailbox.data, ",", 1),"S")) {
if (!strcmp(ArgV(argument, 1),"S")) {
if (paramcount > 1) {
if (XdrvMailbox.data[XdrvMailbox.data_len-1] == ',') {
serviced = false;
return serviced;
}
sprintf(sub_string_tmp,subStr(sub_string, XdrvMailbox.data, ",", 2));
Pn532.newdata_len = strlen(sub_string_tmp);
ArgV(argument, 2);
Pn532.newdata_len = strlen(argument);
if (Pn532.newdata_len > 15) { Pn532.newdata_len = 15; }
memcpy(&Pn532.newdata,&sub_string_tmp,Pn532.newdata_len);
memcpy(&Pn532.newdata,&argument,Pn532.newdata_len);
Pn532.newdata[Pn532.newdata_len] = 0x00; // Null terminate the string
Pn532.function = 2;
AddLog(LOG_LEVEL_INFO, PSTR("NFC: PN532 NFC - Next scanned tag data block 1 will be set to '%s'"), Pn532.newdata);