Refactor string code

This commit is contained in:
Theo Arends 2019-10-22 17:29:21 +02:00
parent bd260a2f40
commit 9868033654
4 changed files with 21 additions and 78 deletions

View File

@ -256,7 +256,6 @@
#define D_CMND_PASSWORD "Password"
#define D_CMND_HOSTNAME "Hostname"
#define D_CMND_WIFICONFIG "WifiConfig"
#define WCFG_MAX_STRING_LENGTH 12
#define D_WCFG_0_RESTART "Restart"
#define D_WCFG_2_WIFIMANAGER "WifiManager"
#define D_WCFG_4_RETRY "Retry"
@ -303,7 +302,6 @@
#define D_CMND_TLSKEY "TLSKey"
#define D_CMND_FULLTOPIC "FullTopic"
#define D_CMND_PREFIX "Prefix"
#define PRFX_MAX_STRING_LENGTH 5
#define D_CMND "cmnd"
#define D_STAT "stat"
#define D_TELE "tele"
@ -505,49 +503,13 @@
// Common
enum UnitNames {
UNIT_AMPERE,
UNIT_HOUR,
UNIT_KILOOHM,
UNIT_KILOWATTHOUR,
UNIT_LUX,
UNIT_MICROSECOND,
UNIT_MILLIAMPERE,
UNIT_MILLIMETER_MERCURY,
UNIT_MILLISECOND,
UNIT_MINUTE,
UNIT_PPB,
UNIT_PPD,
UNIT_PPM,
UNIT_PERCENTAGE,
UNIT_PRESSURE,
UNIT_SECOND,
UNIT_SECTORS,
UNIT_VOLT,
UNIT_WATT,
UNIT_WATTHOUR,
UNIT_HERTZ };
UNIT_AMPERE, UNIT_HOUR, UNIT_KILOOHM, UNIT_KILOWATTHOUR, UNIT_LUX, UNIT_MICROSECOND, UNIT_MILLIAMPERE, UNIT_MILLIMETER_MERCURY,
UNIT_MILLISECOND, UNIT_MINUTE, UNIT_PPB, UNIT_PPD, UNIT_PPM, UNIT_PERCENTAGE, UNIT_PRESSURE, UNIT_SECOND,
UNIT_SECTORS, UNIT_VOLT, UNIT_WATT, UNIT_WATTHOUR, UNIT_HERTZ };
const char kUnitNames[] PROGMEM =
D_UNIT_AMPERE "|"
D_UNIT_HOUR "|"
D_UNIT_KILOOHM "|"
D_UNIT_KILOWATTHOUR "|"
D_UNIT_LUX "|"
D_UNIT_MICROSECOND "|"
D_UNIT_MILLIAMPERE "|"
D_UNIT_MILLIMETER_MERCURY "|"
D_UNIT_MILLISECOND "|"
D_UNIT_MINUTE "|"
D_UNIT_PARTS_PER_BILLION "|"
D_UNIT_PARTS_PER_DECILITER "|"
D_UNIT_PARTS_PER_MILLION "|"
"%|"
D_UNIT_PRESSURE "|"
D_UNIT_SECOND "|"
D_UNIT_SECTORS "|"
D_UNIT_VOLT "|"
D_UNIT_WATT "|"
D_UNIT_WATTHOUR "|"
D_UNIT_HERTZ ;
D_UNIT_AMPERE "|" D_UNIT_HOUR "|" D_UNIT_KILOOHM "|" D_UNIT_KILOWATTHOUR "|" D_UNIT_LUX "|" D_UNIT_MICROSECOND "|" D_UNIT_MILLIAMPERE "|" D_UNIT_MILLIMETER_MERCURY "|"
D_UNIT_MILLISECOND "|" D_UNIT_MINUTE "|" D_UNIT_PARTS_PER_BILLION "|" D_UNIT_PARTS_PER_DECILITER "|" D_UNIT_PARTS_PER_MILLION "|%|" D_UNIT_PRESSURE "|" D_UNIT_SECOND "|"
D_UNIT_SECTORS "|" D_UNIT_VOLT "|" D_UNIT_WATT "|" D_UNIT_WATTHOUR "|" D_UNIT_HERTZ ;
const char S_JSON_COMMAND_NVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%d %s\"}";
const char S_JSON_COMMAND_LVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%lu %s\"}";
@ -594,30 +556,6 @@ const char S_RSLT_WARNING[] PROGMEM = D_RSLT_WARNING;
const char S_LWT[] PROGMEM = D_LWT;
const char S_OFFLINE[] PROGMEM = D_OFFLINE;
// sonoff.ino
#define MAX_BUTTON_COMMANDS 5 // Max number of button commands supported
const char kCommands[MAX_BUTTON_COMMANDS][14] PROGMEM = {
D_CMND_WIFICONFIG " 2", // Press button three times
D_CMND_WIFICONFIG " 2", // Press button four times
D_CMND_WIFICONFIG " 2", // Press button five times
D_CMND_RESTART " 1", // Press button six times
D_CMND_UPGRADE " 1" }; // Press button seven times
const char kWifiConfig[MAX_WIFI_OPTION][WCFG_MAX_STRING_LENGTH] PROGMEM = {
D_WCFG_0_RESTART,
D_WCFG_2_WIFIMANAGER,
D_WCFG_2_WIFIMANAGER,
D_WCFG_2_WIFIMANAGER,
D_WCFG_4_RETRY,
D_WCFG_5_WAIT,
D_WCFG_6_SERIAL,
D_WCFG_7_WIFIMANAGER_RESET_ONLY };
const char kPrefixes[3][PRFX_MAX_STRING_LENGTH] PROGMEM = {
D_CMND,
D_STAT,
D_TELE };
const char kCodeImage[] PROGMEM = "sonoff|minimal|sensors|knx|basic|display|ir";
// support.ino
static const char kMonthNames[] = D_MONTH3LIST;

View File

@ -70,6 +70,8 @@
#include "settings.h"
const char kSleepMode[] PROGMEM = "Dynamic|Normal";
const char kPrefixes[] PROGMEM = D_CMND "|" D_STAT "|" D_TELE;
const char kCodeImage[] PROGMEM = "sonoff|minimal|sensors|knx|basic|display|ir";
// Global variables
SerialConfig serial_config = SERIAL_8N1; // Serial interface configuration 8 data bits, No parity, 1 stop bit
@ -248,7 +250,8 @@ char* GetTopic_P(char *stopic, uint32_t prefix, char *topic, const char* subtopi
snprintf_P(romram, sizeof(romram), subtopic);
if (fallback_topic_flag || (prefix > 3)) {
prefix &= 3;
fulltopic = FPSTR(kPrefixes[prefix]);
char stemp[11];
fulltopic = GetTextIndexed(stemp, sizeof(stemp), prefix, kPrefixes);
fulltopic += F("/");
fulltopic += mqtt_client;
fulltopic += F("_fb"); // cmnd/<mqttclient>_fb
@ -260,7 +263,7 @@ char* GetTopic_P(char *stopic, uint32_t prefix, char *topic, const char* subtopi
}
for (uint32_t i = 0; i < 3; i++) {
if ('\0' == Settings.mqtt_prefix[i][0]) {
snprintf_P(Settings.mqtt_prefix[i], sizeof(Settings.mqtt_prefix[i]), kPrefixes[i]);
GetTextIndexed(Settings.mqtt_prefix[i], sizeof(Settings.mqtt_prefix[i]), i, kPrefixes);
}
}
fulltopic.replace(FPSTR(MQTT_TOKEN_PREFIX), Settings.mqtt_prefix[prefix]);

View File

@ -23,6 +23,10 @@
* Button support
\*********************************************************************************************/
#define MAX_BUTTON_COMMANDS 5 // Max number of button commands supported
const char kCommands[] PROGMEM =
D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_WIFICONFIG " 2|" D_CMND_RESTART " 1|" D_CMND_UPGRADE " 1";
struct BUTTON {
unsigned long debounce = 0; // Button debounce timer
uint16_t hold_timer[MAX_KEYS] = { 0 }; // Timer for button hold
@ -252,7 +256,7 @@ void ButtonHandler(void)
}
} else { // 3 - 7 press
if (!Settings.flag.button_restrict) { // SetOption1 (0)
snprintf_P(scmnd, sizeof(scmnd), kCommands[Button.press_counter[button_index] -3]);
GetTextIndexed(scmnd, sizeof(scmnd), Button.press_counter[button_index] -3, kCommands);
ExecuteCommand(scmnd, SRC_BUTTON);
}
}

View File

@ -47,6 +47,9 @@ void (* const TasmotaCommand[])(void) PROGMEM = {
#endif
&CmndSensor, &CmndDriver };
const char kWifiConfig[] PROGMEM =
D_WCFG_0_RESTART "||" D_WCFG_2_WIFIMANAGER "||" D_WCFG_4_RETRY "|" D_WCFG_5_WAIT "|" D_WCFG_6_SERIAL "|" D_WCFG_7_WIFIMANAGER_RESET_ONLY;
/********************************************************************************************/
void ResponseCmndNumber(int value)
@ -1184,23 +1187,18 @@ void CmndHostname(void)
void CmndWifiConfig(void)
{
char stemp1[TOPSZ];
if ((XdrvMailbox.payload >= WIFI_RESTART) && (XdrvMailbox.payload < MAX_WIFI_OPTION)) {
if ((EX_WIFI_SMARTCONFIG == XdrvMailbox.payload) || (EX_WIFI_WPSCONFIG == XdrvMailbox.payload)) {
XdrvMailbox.payload = WIFI_MANAGER;
}
Settings.sta_config = XdrvMailbox.payload;
wifi_state_flag = Settings.sta_config;
snprintf_P(stemp1, sizeof(stemp1), kWifiConfig[Settings.sta_config]);
Response_P(PSTR("{\"" D_CMND_WIFICONFIG "\":\"%s " D_JSON_SELECTED "\"}"), stemp1);
if (WifiState() > WIFI_RESTART) {
// ResponseAppend_P(PSTR(" after restart"));
restart_flag = 2;
}
} else {
snprintf_P(stemp1, sizeof(stemp1), kWifiConfig[Settings.sta_config]);
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, Settings.sta_config, stemp1);
}
char stemp1[TOPSZ];
Response_P(S_JSON_COMMAND_NVALUE_SVALUE, XdrvMailbox.command, Settings.sta_config, GetTextIndexed(stemp1, sizeof(stemp1), Settings.sta_config, kWifiConfig));
}
void CmndFriendlyname(void)