Redesign GPIO to 16-bit

This commit is contained in:
Theo Arends 2020-09-29 18:10:21 +02:00
parent 3c097ca3d6
commit c00d7035d9
10 changed files with 69 additions and 221 deletions

View File

@ -424,13 +424,13 @@ struct {
SysBitfield3 flag3; // 3A0
uint8_t switchmode[MAX_SWITCHES]; // 3A4 (6.0.0b - moved from 0x4CA)
//#ifdef ESP8266
// char ex_friendlyname[4][33]; // 3AC
// char ex_switch_topic[33]; // 430
//#else // ESP32
myio my_gp; // 3AC - 2 x 18 bytes (ESP8266) / 2 x 40 bytes (ESP32)
#ifdef ESP8266
uint8_t free_esp8266_3D0[44]; // 3D0
uint16_t gpio16_converted; // 3D0
uint8_t free_esp8266_3D2[42]; // 3D2
#endif
mytmplt user_template; // 3FC - 2 x 15 bytes (ESP8266) / 2 x 37 bytes (ESP32)
#ifdef ESP8266
@ -455,12 +455,12 @@ struct {
uint8_t ws_width[3]; // 481
#ifdef ESP8266
myio8 my_gp8; // 484 - 17 bytes (ESP8266)
myio8 ex_my_gp8; // 484 - 17 bytes (ESP8266) - free once gpio16 is active
#else // ESP32
uint8_t free_esp32_484[17]; // 484
#endif // ESP8266 - ESP32
uint8_t ex_my_adc0; // 495 free once gpio16 is active
uint8_t my_adc0; // 495
uint16_t light_pixels; // 496
uint8_t light_color[5]; // 498
uint8_t light_correction; // 49D
@ -514,7 +514,7 @@ struct {
char user_template_name[15]; // 720 15 bytes - Backward compatibility since v8.2.0.3
#ifdef ESP8266
mytmplt8285 user_template8; // 72F 14 bytes (ESP8266)
mytmplt8285 ex_user_template8; // 72F 14 bytes (ESP8266) - free once gpio16 is active
#else // ESP32
uint8_t free_esp32_72f[14]; // 72F
#endif // ESP8266 - ESP32

View File

@ -1193,8 +1193,8 @@ void SettingsDelta(void)
}
}
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
if (Settings.my_gp8.io[i] >= GPI8_SWT5) { // Move up from GPIO_SWT5 to GPIO_KEY1
Settings.my_gp8.io[i] += 4;
if (Settings.ex_my_gp8.io[i] >= GPI8_SWT5) { // Move up from GPI8_SWT5 to GPI8_KEY1
Settings.ex_my_gp8.io[i] += 4;
}
}
}
@ -1259,7 +1259,7 @@ void SettingsDelta(void)
Settings.ledmask = APP_LEDMASK;
}
if (Settings.version < 0x0605000A) {
Settings.my_adc0 = GPIO_NONE;
Settings.ex_my_adc0 = GPIO_NONE;
}
if (Settings.version < 0x0605000D) {
Settings.param[P_IR_UNKNOW_THRESHOLD] = IR_RCV_MIN_UNKNOWN_SIZE;

View File

@ -1143,36 +1143,32 @@ void TemplateConvert(uint8_t template8[], uint16_t template16[]) {
}
template16[(sizeof(mytmplt) / 2) -2] = Adc0Convert(template8[sizeof(mytmplt8285) -1]);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("FNC: TemplateConvert"));
AddLogBuffer(LOG_LEVEL_DEBUG, template8, sizeof(mytmplt8285));
AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t*)template16, sizeof(mytmplt) / 2, 2);
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("FNC: TemplateConvert"));
// AddLogBuffer(LOG_LEVEL_DEBUG, template8, sizeof(mytmplt8285));
// AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t*)template16, sizeof(mytmplt) / 2, 2);
}
void ConvertGpios(void) {
if (Settings.user_template8.flag != 255) {
if (Settings.gpio16_converted != 0xF5A0) {
// Convert 8-bit user template
TemplateConvert((uint8_t*)&Settings.user_template8, (uint16_t*)&Settings.user_template);
Settings.user_template8.flag = 255;
TemplateConvert((uint8_t*)&Settings.ex_user_template8, (uint16_t*)&Settings.user_template);
for (uint32_t i = 0; i < sizeof(Settings.my_gp8.io); i++) {
Settings.my_gp.io[i] = GpioConvert(Settings.my_gp8.io[i]);
for (uint32_t i = 0; i < sizeof(Settings.ex_my_gp8.io); i++) {
Settings.my_gp.io[i] = GpioConvert(Settings.ex_my_gp8.io[i]);
}
Settings.my_gp.io[(sizeof(myio) / 2) -1] = Adc0Convert(Settings.my_adc0);
Settings.my_gp.io[(sizeof(myio) / 2) -1] = Adc0Convert(Settings.ex_my_adc0);
Settings.gpio16_converted = 0xF5A0;
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("FNC: ConvertGpios"));
AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)&Settings.my_gp8.io, sizeof(myio8));
AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t *)&Settings.my_gp.io, sizeof(myio) / 2, 2);
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("FNC: ConvertGpios"));
// AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t *)&Settings.ex_my_gp8.io, sizeof(myio8));
// AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t *)&Settings.my_gp.io, sizeof(myio) / 2, 2);
}
}
#endif // ESP8266
uint32_t ICACHE_RAM_ATTR Pin(uint32_t gpio, uint32_t index = 0);
uint32_t ICACHE_RAM_ATTR Pin(uint32_t gpio, uint32_t index) {
//#ifdef ESP8266
// uint16_t real_gpio = gpio + index;
//#else // ESP32
uint16_t real_gpio = (gpio << 5) + index;
//#endif // ESP8266 - ESP32
for (uint32_t i = 0; i < ARRAY_SIZE(gpio_pin); i++) {
if (gpio_pin[i] == real_gpio) {
return i; // Pin number configured for gpio
@ -1289,20 +1285,15 @@ void GetInternalTemplate(void* ptr, uint32_t module, uint32_t option) {
}
memcpy(ptr, &template16[index], size);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("FNC: GetInternalTemplate option %d"), option);
AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t *)ptr, size / 2, 2);
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("FNC: GetInternalTemplate option %d"), option);
// AddLogBufferSize(LOG_LEVEL_DEBUG, (uint8_t *)ptr, size / 2, 2);
}
#endif // ESP8266
void ModuleGpios(myio *gp)
{
//#ifdef ESP8266
// uint8_t *dest = (uint8_t *)gp;
// uint8_t src[ARRAY_SIZE(Settings.user_template.gp.io)];
//#else // ESP32
uint16_t *dest = (uint16_t *)gp;
uint16_t src[ARRAY_SIZE(Settings.user_template.gp.io)];
//#endif // ESP8266 - ESP32
memset(dest, GPIO_NONE, sizeof(myio));
if (USER_MODULE == Settings.module) {
@ -1396,19 +1387,7 @@ bool ValidGPIO(uint32_t pin, uint32_t gpio)
return (GPIO_USER == ValidPin(pin, BGPIO(gpio))); // Only allow GPIO_USER pins
}
//#ifdef ESP8266
//bool ValidAdc(void) {
// gpio_flag flag = ModuleFlag();
// uint32_t template_adc0 = flag.data &15;
// return (ADC0_USER == template_adc0);
//}
//#endif // ESP8266
//#ifdef ESP8266
//bool GetUsedInModule(uint32_t val, uint8_t *arr)
//#else // ESP32
bool GetUsedInModule(uint32_t val, uint16_t *arr)
//#endif // ESP8266 - ESP32
{
int offset = 0;
@ -1471,8 +1450,8 @@ bool GetUsedInModule(uint32_t val, uint16_t *arr)
bool JsonTemplate(const char* dataBuf)
{
// Old: {"NAME":"Generic","GPIO":[17,254,29,254,7,254,254,254,138,254,139,254,254],"FLAG":1,"BASE":255}
// New: {"NAME":"Generic16","GPIO":[17,254,29,254,7,254,254,254,138,254,139,254,254,254],"FLAG":1,"BASE":255}
// Old: {"NAME":"Shelly 2.5","GPIO":[56,0,17,0,21,83,0,0,6,82,5,22,156],"FLAG":2,"BASE":18}
// New: {"NAME":"Shelly 2.5","GPIO":[320,0,32,0,224,193,0,0,640,192,608,225,3456,4736],"FLAG":0,"BASE":18}
if (strlen(dataBuf) < 9) { return false; } // Workaround exception if empty JSON like {} - Needs checks
@ -1507,15 +1486,11 @@ bool JsonTemplate(const char* dataBuf)
} else {
#endif
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.user_template.gp.io); i++) {
// #ifdef ESP8266
// Settings.user_template.gp.io[i] = obj[D_JSON_GPIO][i] | 0;
// #else // ESP32
uint16_t gpio = obj[D_JSON_GPIO][i] | 0;
if (gpio == (AGPIO(GPIO_NONE) +1)) {
gpio = AGPIO(GPIO_USER);
}
Settings.user_template.gp.io[i] = gpio;
// #endif
}
if (obj[D_JSON_FLAG].success()) {
Settings.user_template.flag.data = obj[D_JSON_FLAG] | 0;
@ -1524,10 +1499,6 @@ bool JsonTemplate(const char* dataBuf)
}
#endif
}
// if (obj[D_JSON_FLAG].success()) {
// uint32_t flag = obj[D_JSON_FLAG] | 0;
// memcpy(&Settings.user_template.flag, &flag, sizeof(gpio_flag));
// }
if (obj[D_JSON_BASE].success()) {
uint32_t base = obj[D_JSON_BASE];
if ((0 == base) || !ValidTemplateModule(base -1)) { base = 18; }
@ -1540,15 +1511,11 @@ void TemplateJson(void)
{
Response_P(PSTR("{\"" D_JSON_NAME "\":\"%s\",\"" D_JSON_GPIO "\":["), SettingsText(SET_TEMPLATE_NAME));
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.user_template.gp.io); i++) {
//#ifdef ESP8266
// ResponseAppend_P(PSTR("%s%d"), (i>0)?",":"", Settings.user_template.gp.io[i]);
//#else // ESP32
uint16_t gpio = Settings.user_template.gp.io[i];
if (gpio == AGPIO(GPIO_USER)) {
gpio = AGPIO(GPIO_NONE) +1;
}
ResponseAppend_P(PSTR("%s%d"), (i>0)?",":"", gpio);
//#endif
}
ResponseAppend_P(PSTR("],\"" D_JSON_FLAG "\":%d,\"" D_JSON_BASE "\":%d}"), Settings.user_template.flag, Settings.user_template_base +1);
}

View File

@ -1119,19 +1119,11 @@ void CmndGpio(void)
if (ValidGPIO(XdrvMailbox.index, cmodule.io[XdrvMailbox.index]) && (XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < AGPIO(GPIO_SENSOR_END))) {
bool present = false;
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) {
//#ifdef ESP8266
// uint32_t midx = pgm_read_byte(kGpioNiceList + i);
// if (midx == XdrvMailbox.payload) {
// present = true;
// break;
// }
//#else // ESP32
uint32_t midx = pgm_read_word(kGpioNiceList + i);
if ((XdrvMailbox.payload >= (midx & 0xFFE0)) && (XdrvMailbox.payload < midx)) {
present = true;
break;
}
//#endif // ESP8266 - ESP32
}
if (present) {
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
@ -1146,7 +1138,7 @@ void CmndGpio(void)
Response_P(PSTR("{"));
bool jsflg = false;
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
if (ValidGPIO(i, cmodule.io[i]) || ((AGPIO(GPIO_USER) == XdrvMailbox.payload) && !FlashPin(i))) {
if (ValidGPIO(i, cmodule.io[i]) || ((255 == XdrvMailbox.payload) && !FlashPin(i))) {
if (jsflg) { ResponseAppend_P(PSTR(",")); }
jsflg = true;
uint32_t sensor_type = Settings.my_gp.io[i];
@ -1158,7 +1150,6 @@ void CmndGpio(void)
}
char sindex[4] = { 0 };
uint32_t sensor_name_idx = BGPIO(sensor_type);
//#ifdef ESP32
uint32_t nice_list_search = sensor_type & 0xFFE0;
for (uint32_t j = 0; j < ARRAY_SIZE(kGpioNiceList); j++) {
uint32_t nls_idx = pgm_read_word(kGpioNiceList + j);
@ -1167,7 +1158,6 @@ void CmndGpio(void)
break;
}
}
//#endif // ESP32
const char *sensor_names = kSensorNames;
if (sensor_name_idx > GPIO_FIX_START) {
sensor_name_idx = sensor_name_idx - GPIO_FIX_START -1;
@ -1186,20 +1176,13 @@ void CmndGpio(void)
}
}
void CmndGpios(void)
{
void ShowGpios(const uint16_t *NiceList, uint32_t size, uint32_t offset, uint32_t &lines) {
myio cmodule;
ModuleGpios(&cmodule);
uint32_t lines = 1;
bool jsflg = false;
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) {
//#ifdef ESP8266
// uint32_t midx = pgm_read_byte(kGpioNiceList + i);
// uint32_t ridx = midx;
//#else // ESP32
uint32_t ridx = pgm_read_word(kGpioNiceList + i) & 0xFFE0;
for (uint32_t i = offset; i < size; i++) { // Skip ADC_NONE
uint32_t ridx = pgm_read_word(NiceList + i) & 0xFFE0;
uint32_t midx = BGPIO(ridx);
//#endif // ESP8266 - ESP32
if ((XdrvMailbox.payload != 255) && GetUsedInModule(midx, cmodule.io)) { continue; }
if (!jsflg) {
Response_P(PSTR("{\"" D_CMND_GPIOS "%d\":{"), lines);
@ -1208,19 +1191,31 @@ void CmndGpios(void)
}
jsflg = true;
char stemp1[TOPSZ];
if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), ridx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (LOGSZ - TOPSZ)) || (i == ARRAY_SIZE(kGpioNiceList) -1)) {
if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), ridx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (LOGSZ - TOPSZ)) || (i == size -1)) {
ResponseJsonEndEnd();
MqttPublishPrefixTopic_P(RESULT_OR_STAT, XdrvMailbox.command);
jsflg = false;
lines++;
}
}
}
void CmndGpios(void)
{
uint32_t lines = 1;
ShowGpios(kGpioNiceList, ARRAY_SIZE(kGpioNiceList), 0, lines);
#ifdef ESP8266
#ifndef USE_ADC_VCC
ShowGpios(kAdcNiceList, ARRAY_SIZE(kAdcNiceList), 1, lines);
#endif // USE_ADC_VCC
#endif // ESP8266
mqtt_data[0] = '\0';
}
void CmndTemplate(void)
{
// {"NAME":"Generic","GPIO":[17,254,29,254,7,254,254,254,138,254,139,254,254],"FLAG":1,"BASE":255}
// {"NAME":"Shelly 2.5","GPIO":[320,0,32,0,224,193,0,0,640,192,608,225,3456,4736],"FLAG":0,"BASE":18}
bool error = false;
if (strstr(XdrvMailbox.data, "{") == nullptr) { // If no JSON it must be parameter

View File

@ -1499,19 +1499,6 @@ void GpioInit(void)
my_module.io[i] = def_gp.io[i]; // Force Template override
}
}
//#ifdef ESP8266
// if ((Settings.my_adc0 >= ADC0_END) && (Settings.my_adc0 < ADC0_USER)) {
// Settings.my_adc0 = ADC0_NONE; // Fix not supported sensor ids in module
// }
// else if (Settings.my_adc0 > ADC0_NONE) {
// my_adc0 = Settings.my_adc0; // Set User selected Module sensors
// }
// my_module_flag = ModuleFlag();
// uint32_t template_adc0 = my_module_flag.data &15;
// if ((template_adc0 > ADC0_NONE) && (template_adc0 < ADC0_USER)) {
// my_adc0 = template_adc0; // Force Template override
// }
//#endif
for (uint32_t i = 0; i < ARRAY_SIZE(my_module.io); i++) {
uint32_t mpin = ValidPin(i, my_module.io[i]);

View File

@ -83,7 +83,7 @@ const uint8_t MAX_BUTTON_TEXT = 16; // Max number of GUI button labels
const uint8_t MAX_GROUP_TOPICS = 4; // Max number of Group Topics
const uint8_t MAX_DEV_GROUP_NAMES = 4; // Max number of Device Group names
#ifdef ESP8266
const uint8_t MAX_ADCS = 1; // Max number of ADC pins
const uint8_t MAX_ADCS = 1; // Max number of ESP8266 ADC pins
#else
const uint8_t MAX_ADCS = 8; // Max number of ESP32 ADC pins (ADC2 pins are unusable with Wifi enabled)
#endif

View File

@ -115,11 +115,7 @@ uint16_t tele_period = 9999; // Tele period timer
uint16_t blink_counter = 0; // Number of blink cycles
uint16_t seriallog_timer = 0; // Timer to disable Seriallog
uint16_t syslog_timer = 0; // Timer to re-enable syslog_level
//#ifdef ESP32
uint16_t gpio_pin[MAX_GPIO_PIN] = { 0 }; // GPIO functions indexed by pin number
//#endif // ESP32
int16_t save_data_counter; // Counter and flag for config save to Flash
RulesBitfield rules_flag; // Rule state flags (16 bits)
uint8_t mqtt_cmnd_blocked = 0; // Ignore flag for publish command
@ -128,11 +124,6 @@ uint8_t state_250mS = 0; // State 250msecond per second flag
uint8_t latching_relay_pulse = 0; // Latching relay pulse timer
uint8_t ssleep; // Current copy of Settings.sleep
uint8_t blinkspeed = 1; // LED blink rate
//#ifdef ESP8266
//uint8_t gpio_pin[MAX_GPIO_PIN] = { 0 }; // GPIO functions indexed by pin number
//#endif // ESP8266 - ESP32
uint8_t active_device = 1; // Active device in ExecuteCommandPower
uint8_t leds_present = 0; // Max number of LED supported
uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off))
@ -149,7 +140,6 @@ uint8_t masterlog_level = 0; // Master log level used to override
uint8_t seriallog_level; // Current copy of Settings.seriallog_level
uint8_t syslog_level; // Current copy of Settings.syslog_level
uint8_t my_module_type; // Current copy of Settings.module or user template type
uint8_t my_adc0 = 0; // Active copy of Module ADC0
uint8_t last_source = 0; // Last command source
uint8_t shutters_present = 0; // Number of actual define shutters
uint8_t prepped_loglevel = 0; // Delayed log level message
@ -171,7 +161,6 @@ bool is_8285 = false; // Hardware device ESP8266EX (0) or
bool skip_light_fade; // Temporarily skip light fading
bool restart_halt = false; // Do not restart but stay in wait loop
myio my_module; // Active copy of Module GPIOs (17 x 8 bits)
//gpio_flag my_module_flag; // Active copy of Template GPIO flags
StateBitfield global_state; // Global states (currently Wifi and Mqtt) (8 bits)
char my_version[33]; // Composed version string
char my_image[33]; // Code image and/or commit
@ -276,9 +265,6 @@ void setup(void) {
for (uint32_t i = 0; i < ARRAY_SIZE(Settings.my_gp.io); i++) {
Settings.my_gp.io[i] = GPIO_NONE; // Reset user defined GPIO disabling sensors
}
//#ifdef ESP8266
// Settings.my_adc0 = ADC0_NONE; // Reset user defined ADC0 disabling sensors
//#endif
}
if (RtcReboot.fast_reboot_count > Settings.param[P_BOOT_LOOP_OFFSET] +4) { // Restarted 6 times
Settings.module = Settings.fallback_module; // Reset module to fallback module

View File

@ -590,12 +590,12 @@ const uint16_t kGpioNiceList[] PROGMEM = {
AGPIO(GPIO_WEBCAM_PSCLK),
AGPIO(GPIO_WEBCAM_HSD) + MAX_WEBCAM_HSD,
AGPIO(GPIO_WEBCAM_PSRCS),
#endif
#endif // USE_WEBCAM
#ifdef USE_ETHERNET
AGPIO(GPIO_ETH_PHY_POWER),
AGPIO(GPIO_ETH_PHY_MDC),
AGPIO(GPIO_ETH_PHY_MDIO), // Ethernet
#endif
#endif // USE_ETHERNET
AGPIO(GPIO_ADC_INPUT) + MAX_ADCS, // Analog inputs
AGPIO(GPIO_ADC_TEMP) + MAX_ADCS, // Thermistor
AGPIO(GPIO_ADC_LIGHT) + MAX_ADCS, // Light sensor
@ -621,10 +621,6 @@ const uint16_t kAdcNiceList[] PROGMEM = {
};
#endif // ESP8266
/*********************************************************************************************\
* ATTENTION: No user changeable features beyond this point - do not add templates !!!
\*********************************************************************************************/
// User selectable ADC functionality
enum UserSelectableAdc {
ADC_NONE, // Not used
@ -640,13 +636,17 @@ enum UserSelectableAdc {
// ADC_SWITCH_INV,
ADC_END };
/*********************************************************************************************\
* ATTENTION: No user changeable features beyond this point - do not add templates !!!
\*********************************************************************************************/
#ifdef ESP8266
#define MAX_GPI8_PIN 17 // Number of supported GPIO (0..16)
#define FLASH_PINS 6 // Number of flash chip pins
#define ADC0_PIN 17 // Pin number of ADC0
#define MAX_GPIO_PIN 18 // Number of supported GPIO (0..16 + ADC0)
#define ADC0_PIN 17 // Pin number of ADC0
#define MIN_FLASH_PINS 4 // Number of flash chip pins unusable for configuration (GPIO6, 7, 8 and 11)
#define MAX_USER_PINS 14 // MAX_GPIO_PIN - MIN_FLASH_PINS
#define WEMOS_MODULE 17 // Wemos module
@ -731,8 +731,7 @@ typedef struct MYTMPLT {
#ifdef ESP8266
// User selectable GPIO functionality
// ATTENTION: Only add at the end of this list just before GPI8_SENSOR_END
// Then add the same name(s) in a nice location in array kGpioNiceList
// ATTENTION: No additions are supported
enum LegacyUserSelectablePins {
GPI8_NONE, // Not used
GPI8_DHT11, // DHT11

View File

@ -368,16 +368,12 @@ const char HTTP_SCRIPT_CONSOL[] PROGMEM =
"wl(h);"; // Add console command key eventlistener after name has been synced with id (= wl(jd))
#endif //USE_UNISHOX_COMPRESSION
const char HTTP_MODULE_TEMPLATE_REPLACE[] PROGMEM =
"}2%d'>%s (%d}3"; // }2 and }3 are used in below os.replace
const char HTTP_MODULE_TEMPLATE_REPLACE_INDEX[] PROGMEM =
"}2%d'>%s (%d)}3"; // }2 and }3 are used in below os.replace
const char HTTP_MODULE_TEMPLATE_REPLACE_NO_INDEX[] PROGMEM =
"}2%d'>%s}3"; // }2 and }3 are used in below os.replace
#if defined(USE_UNISHOX_COMPRESSION) && defined(ESP32)
// no compression on ESP8266, we would lose 16 bytes
#if defined(USE_UNISHOX_COMPRESSION)
const size_t HTTP_SCRIPT_MODULE_TEMPLATE_SIZE = 602;
const char HTTP_SCRIPT_MODULE_TEMPLATE_COMPRESSED[] PROGMEM = "\x33\xBF\xAC\xF1\xD4\x2B\xC7\x83\x02\xF8\x3A\xDC\xE4\x1B\x3B\xBA\x75\x1A\x8E\xF1"
"\xED\x33\xBF\xAC\x3E\x09\x81\x8B\x1A\xFA\x8E\x81\xFD\xDD\x32\x61\x31\xAF\xA8\xEE"
@ -405,14 +401,6 @@ const char HTTP_SCRIPT_MODULE_TEMPLATE_COMPRESSED[] PROGMEM = "\x33\xBF\xAC\xF1\
#define HTTP_SCRIPT_MODULE_TEMPLATE Decompress(HTTP_SCRIPT_MODULE_TEMPLATE_COMPRESSED,HTTP_SCRIPT_MODULE_TEMPLATE_SIZE).c_str()
#else
const char HTTP_SCRIPT_MODULE_TEMPLATE[] PROGMEM =
//#ifdef ESP8266
// "var os;"
// "function sk(s,g){" // s = value, g = id and name
// "var o=os.replace(/}2/g,\"<option value='\").replace(/}3/g,\")</option>\");"
// "eb('g'+g).innerHTML=o;"
// "eb('g'+g).value=s;"
// "}";
//#else // ESP32
"var os,hs;"
"function ce(i,q){" // Create index select
"var o=document.createElement('option');"
@ -436,7 +424,6 @@ const char HTTP_SCRIPT_MODULE_TEMPLATE[] PROGMEM =
"eb('g'+g).value=(g<99)?s&0xffe0:s;"
"if(g<99){ot(g,s);}"
"}";
//#endif // ESP8266 - ESP32
#endif //USE_UNISHOX_COMPRESSION
#ifdef USE_UNISHOX_COMPRESSION
@ -487,11 +474,9 @@ const char HTTP_SCRIPT_TEMPLATE2[] PROGMEM =
"sk(g[i],j);" // Set GPIO
"j++;"
"}";
// "g=o.shift();"; // FLAG
const char HTTP_SCRIPT_TEMPLATE3[] PROGMEM =
"\";"
"sk(g[13]," STR(ADC0_PIN) ");"; // Set ADC0
// "g>>=4;";
const char HTTP_SCRIPT_TEMPLATE4[] PROGMEM =
"g=o.shift();" // FLAG
"for(i=0;i<" STR(GPIO_FLAG_USED) ";i++){"
@ -1727,25 +1712,15 @@ void HandleTemplateConfiguration(void)
WSContentSend_P(HTTP_SCRIPT_TEMPLATE);
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) { // GPIO: }2'0'>None (0)}3}2'17'>Button1 (17)}3...
//#ifdef ESP8266
// if (1 == i) {
// WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, AGPIO(GPIO_USER), D_SENSOR_USER, AGPIO(GPIO_USER)); // }2'255'>User (255)}3
// }
// uint32_t midx = pgm_read_byte(kGpioNiceList + i);
// uint32_t ridx = midx;
// WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, ridx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), ridx);
//#else // ESP32
if (1 == i) {
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_NO_INDEX, AGPIO(GPIO_USER), D_SENSOR_USER); // }2'255'>User}3
}
uint32_t ridx = pgm_read_word(kGpioNiceList + i) & 0xFFE0;
uint32_t midx = BGPIO(ridx);
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_NO_INDEX, ridx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames));
//#endif // ESP8266 - ESP32
}
WSContentSend_P(PSTR("\";"));
//#ifdef ESP32
WSContentSend_P(PSTR("hs=["));
bool first_done = false;
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) { // hs=[36,68,100,132,168,200,232,264,292,324,356,388,421,453];
@ -1757,7 +1732,6 @@ void HandleTemplateConfiguration(void)
}
}
WSContentSend_P(PSTR("];"));
//#endif // ESP32
WSContentSend_P(HTTP_SCRIPT_TEMPLATE2);
@ -1767,7 +1741,6 @@ void HandleTemplateConfiguration(void)
if (1 == i) {
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_NO_INDEX, AGPIO(GPIO_USER), D_SENSOR_USER); // }2'15'>User}3
}
// WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, i, GetTextIndexed(stemp, sizeof(stemp), i, kAdc0Names), i);
uint32_t ridx = pgm_read_word(kAdcNiceList + i) & 0xFFE0;
uint32_t midx = BGPIO(ridx);
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_NO_INDEX, ridx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames));
@ -1778,11 +1751,7 @@ void HandleTemplateConfiguration(void)
WSContentSend_P(HTTP_SCRIPT_TEMPLATE4);
for (uint32_t i = 0; i < sizeof(kModuleNiceList); i++) { // "}2'%d'>%s (%d)}3" - "}2'0'>Sonoff Basic (1)}3"
uint32_t midx = pgm_read_byte(kModuleNiceList + i);
//#ifdef ESP8266
// WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), midx +1);
//#else // ESP32
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_INDEX, midx, AnyModuleName(midx).c_str(), midx +1);
//#endif // ESP8266 - ESP32
}
WSContentSend_P(HTTP_SCRIPT_TEMPLATE5);
@ -1796,11 +1765,6 @@ void HandleTemplateConfiguration(void)
WSContentSend_P(HTTP_TABLE100);
for (uint32_t i = 0; i < MAX_GPIO_PIN; i++) {
if (!FlashPin(i)) {
//#ifdef ESP8266
// WSContentSend_P(PSTR("<tr><td><b><font color='#%06x'>" D_GPIO "%d</font></b></td><td%s><select id='g%d'></select></td></tr>"),
// ((9==i)||(10==i)) ? WebColor(COL_TEXT_WARNING) : WebColor(COL_TEXT), i, (0==i) ? " style='width:200px'" : "", i);
//#else // ESP32
//#ifdef ESP8266
// WSContentSend_P(PSTR("<tr><td><b><font color='#%06x'>%s%d</font></b></td><td%s><select id='g%d' onchange='ot(%d,this.value)'></select></td>"),
// ((9==i)||(10==i)) ? WebColor(COL_TEXT_WARNING) : WebColor(COL_TEXT),
@ -1813,17 +1777,10 @@ void HandleTemplateConfiguration(void)
WSContentSend_P(PSTR("<td style='width:50px'><select id='h%d'></select></td></tr>"), i);
}
}
//#ifdef ESP8266
// WSContentSend_P(PSTR("<tr><td><b><font color='#%06x'>" D_ADC "0</font></b></td><td><select id='g17'></select></td></tr>"), WebColor(COL_TEXT));
//#endif
WSContentSend_P(PSTR("</table>"));
gpio_flag flag = ModuleFlag();
//#ifdef ESP8266
// if (flag.data > ADC0_USER) {
//#else // ESP32
if (flag.data) {
//#endif // ESP32
WSContentSend_P(HTTP_FORM_TEMPLATE_FLAG);
}
@ -1848,24 +1805,17 @@ void TemplateSaveSettings(void)
snprintf_P(webindex, sizeof(webindex), PSTR("g%d"), j);
WebGetArg(webindex, tmp, sizeof(tmp)); // GPIO
uint32_t gpio = atoi(tmp);
//#ifdef ESP32
char tmp2[8]; // WebGetArg numbers only
char webindex2[5]; // WebGetArg name
snprintf_P(webindex2, sizeof(webindex2), PSTR("h%d"), j);
WebGetArg(webindex2, tmp2, sizeof(tmp2));
uint32_t value2 = (!strlen(tmp2)) ? 0 : atoi(tmp2) -1;
gpio += value2;
//#endif // ESP32
snprintf_P(svalue, sizeof(svalue), PSTR("%s%s%d"), svalue, (i>0)?",":"", gpio);
j++;
}
//#ifdef ESP8266
// WebGetArg("g" STR(ADC0_PIN), tmp, sizeof(tmp)); // FLAG - ADC0
// uint32_t flag = atoi(tmp);
//#else // ESP32
uint32_t flag = 0;
//#endif // ESP32
for (uint32_t i = 0; i < GPIO_FLAG_USED; i++) {
snprintf_P(webindex, sizeof(webindex), PSTR("c%d"), i);
uint32_t state = Webserver->hasArg(webindex) << i; // FLAG
@ -1910,31 +1860,18 @@ void HandleModuleConfiguration(void)
midx = pgm_read_byte(kModuleNiceList + i -1);
vidx = midx +1;
}
//#ifdef ESP8266
// WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), vidx);
//#else // ESP32
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_INDEX, midx, AnyModuleName(midx).c_str(), vidx);
//#endif // ESP8266 - ESP32
}
WSContentSend_P(PSTR("\";sk(%d,99);os=\""), Settings.module);
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) {
//#ifdef ESP8266
// midx = pgm_read_byte(kGpioNiceList + i);
// uint32_t ridx = midx;
// if (!GetUsedInModule(midx, cmodule.io)) {
// WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, ridx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), ridx);
// }
//#else // ESP32
uint32_t ridx = pgm_read_word(kGpioNiceList + i) & 0xFFE0;
midx = BGPIO(ridx);
if (!GetUsedInModule(midx, cmodule.io)) {
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE_NO_INDEX, ridx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames));
}
//#endif // ESP8266 - ESP32
}
WSContentSend_P(PSTR("\";"));
//#ifdef ESP32
WSContentSend_P(PSTR("hs=["));
bool first_done = false;
for (uint32_t i = 0; i < ARRAY_SIZE(kGpioNiceList); i++) { // hs=[36,68,100,132,168,200,232,264,292,324,356,388,421,453];
@ -1946,7 +1883,6 @@ void HandleModuleConfiguration(void)
}
}
WSContentSend_P(PSTR("];"));
//#endif // ESP32
for (uint32_t i = 0; i < ARRAY_SIZE(cmodule.io); i++) {
if (ValidGPIO(i, cmodule.io[i])) {
@ -1957,12 +1893,6 @@ void HandleModuleConfiguration(void)
#ifdef ESP8266
#ifndef USE_ADC_VCC
WSContentSend_P(PSTR("os=\""));
/*
for (uint32_t j = 0; j < ADC0_END; j++) {
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, j, GetTextIndexed(stemp, sizeof(stemp), j, kAdc0Names), j);
}
WSContentSend_P(PSTR("\";sk(%d," STR(ADC0_PIN) ");"), Settings.my_adc0);
*/
for (uint32_t i = 0; i < ARRAY_SIZE(kAdcNiceList); i++) { // FLAG: }2'0'>None}3}2'17'>Analog}3...
uint32_t ridx = pgm_read_word(kAdcNiceList + i) & 0xFFE0;
uint32_t midx = BGPIO(ridx);
@ -1979,13 +1909,6 @@ void HandleModuleConfiguration(void)
for (uint32_t i = 0; i < ARRAY_SIZE(cmodule.io); i++) {
if (ValidGPIO(i, cmodule.io[i])) {
snprintf_P(stemp, 3, PINS_WEMOS +i*2);
//#ifdef ESP8266
// char sesp8285[40];
// snprintf_P(sesp8285, sizeof(sesp8285), PSTR("<font color='#%06x'>ESP8285</font>"), WebColor(COL_TEXT_WARNING));
// WSContentSend_P(PSTR("<tr><td style='width:190px'>%s <b>" D_GPIO "%d</b> %s</td><td style='width:176px'><select id='g%d'></select></td></tr>"),
// (WEMOS==my_module_type)?stemp:"", i, (0==i)? D_SENSOR_BUTTON "1":(1==i)? D_SERIAL_OUT :(3==i)? D_SERIAL_IN :((9==i)||(10==i))? sesp8285 :(12==i)? D_SENSOR_RELAY "1":(13==i)? D_SENSOR_LED "1i":(14==i)? D_SENSOR :"", i);
//#else // ESP32
//#ifdef ESP8266
// WSContentSend_P(PSTR("<tr><td style='width:116px'>%s <b>%s%d</b></td><td style='width:150px'><select id='g%d' onchange='ot(%d,this.value)'></select></td>"),
// (WEMOS==my_module_type)?stemp:"",
@ -1996,16 +1919,8 @@ void HandleModuleConfiguration(void)
(WEMOS==my_module_type)?stemp:"", i, i, i);
//#endif
WSContentSend_P(PSTR("<td style='width:50px'><select id='h%d'></select></td></tr>"), i);
//#endif // ESP8266
}
}
//#ifdef ESP8266
//#ifndef USE_ADC_VCC
// if (ValidAdc()) {
// WSContentSend_P(PSTR("<tr><td>%s <b>" D_ADC "0</b></td><td style='width:176px'><select id='g17'></select></td></tr>"), (WEMOS==my_module_type)?"A0":"");
// }
//#endif // USE_ADC_VCC
//#endif // ESP8266
WSContentSend_P(PSTR("</table>"));
WSContentSend_P(HTTP_FORM_END);
WSContentSpaceButton(BUTTON_CONFIGURATION);
@ -2033,26 +1948,17 @@ void ModuleSaveSettings(void)
snprintf_P(webindex, sizeof(webindex), PSTR("g%d"), i);
WebGetArg(webindex, tmp, sizeof(tmp));
uint32_t value = (!strlen(tmp)) ? 0 : atoi(tmp);
//#ifdef ESP32
char tmp2[8]; // WebGetArg numbers only
char webindex2[5]; // WebGetArg name
snprintf_P(webindex2, sizeof(webindex2), PSTR("h%d"), i);
WebGetArg(webindex2, tmp2, sizeof(tmp2));
uint32_t value2 = (!strlen(tmp2)) ? 0 : atoi(tmp2) -1;
value += value2;
//#endif // ESP8266 - ESP32
Settings.my_gp.io[i] = value;
gpios += F(", " D_GPIO ); gpios += String(i); gpios += F(" "); gpios += String(value);
}
}
}
//#ifdef ESP8266
//#ifndef USE_ADC_VCC
// WebGetArg("g" STR(ADC0_PIN), tmp, sizeof(tmp));
// Settings.my_adc0 = (!strlen(tmp)) ? 0 : atoi(tmp);
// gpios += F(", " D_ADC "0 "); gpios += String(Settings.my_adc0);
//#endif // USE_ADC_VCC
//#endif // ESP8266
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MODULE "%s " D_CMND_MODULE "%s"), ModuleName().c_str(), gpios.c_str());
}

View File

@ -222,13 +222,19 @@ uint16_t AdcRead(uint32_t pin, uint32_t factor) {
#ifdef USE_RULES
void AdcEvery250ms(void) {
char adc_idx[3] = { 0 };
uint32_t offset = 0;
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
#ifdef ESP32
snprintf_P(adc_idx, sizeof(adc_idx), PSTR("%d"), idx +1);
offset = 1;
#endif
if (ADC_INPUT == Adc[idx].type) {
uint16_t new_value = AdcRead(Adc[idx].pin, 5);
if ((new_value < Adc[idx].last_value -10) || (new_value > Adc[idx].last_value +10)) {
Adc[idx].last_value = new_value;
uint16_t value = Adc[idx].last_value / 10;
Response_P(PSTR("{\"ANALOG\":{\"A%ddiv10\":%d}}"), idx +1, (value > 99) ? 100 : value);
Response_P(PSTR("{\"ANALOG\":{\"A%ddiv10\":%d}}"), idx + offset, (value > 99) ? 100 : value);
XdrvRulesProcess();
}
}
@ -237,7 +243,7 @@ void AdcEvery250ms(void) {
if (new_value && (new_value != Adc[idx].last_value)) {
Adc[idx].last_value = new_value;
uint16_t value = new_value / Adc[idx].param1;
Response_P(PSTR("{\"ANALOG\":{\"Joy%d\":%d}}"), idx +1, value);
Response_P(PSTR("{\"ANALOG\":{\"Joy%s\":%d}}"), adc_idx, value);
XdrvRulesProcess();
} else {
Adc[idx].last_value = 0;
@ -348,12 +354,14 @@ void AdcShowContinuation(bool *jsonflg) {
void AdcShow(bool json) {
bool domo_flag[ADC_END] = { false };
char adc_name[10] = { 0 }; // ANALOG8
char adc_idx[3] = { 0 };
uint32_t offset = 0;
bool jsonflg = false;
for (uint32_t idx = 0; idx < Adcs.present; idx++) {
#ifdef ESP32
snprintf_P(adc_name, sizeof(adc_name), PSTR("Analog%d"), idx +1);
snprintf_P(adc_idx, sizeof(adc_idx), PSTR("%d"), idx +1);
offset = 1;
#endif
@ -377,7 +385,7 @@ void AdcShow(bool json) {
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "%d\":%s"), idx + offset, temperature);
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "%s\":%s"), adc_idx, temperature);
if ((0 == tele_period) && (!domo_flag[ADC_TEMP])) {
#ifdef USE_DOMOTICZ
DomoticzSensor(DZ_TEMP, temperature);
@ -399,7 +407,7 @@ void AdcShow(bool json) {
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"" D_JSON_ILLUMINANCE "%d\":%d"), idx + offset, adc_light);
ResponseAppend_P(PSTR("\"" D_JSON_ILLUMINANCE "%s\":%d"), adc_idx, adc_light);
#ifdef USE_DOMOTICZ
if ((0 == tele_period) && (!domo_flag[ADC_LIGHT])) {
DomoticzSensor(DZ_ILLUMINANCE, adc_light);
@ -418,7 +426,7 @@ void AdcShow(bool json) {
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"" D_JSON_RANGE "%d\":%d"), idx + offset, adc_range);
ResponseAppend_P(PSTR("\"" D_JSON_RANGE "%s\":%d"), adc_idx, adc_range);
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_RANGE, adc_name, adc_range);
@ -441,8 +449,8 @@ void AdcShow(bool json) {
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"CTEnergy%d\":{\"" D_JSON_ENERGY "\":%s,\"" D_JSON_POWERUSAGE "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s}"),
idx +1, energy_chr, power_chr, voltage_chr, current_chr);
ResponseAppend_P(PSTR("\"CTEnergy%s\":{\"" D_JSON_ENERGY "\":%s,\"" D_JSON_POWERUSAGE "\":%s,\"" D_JSON_VOLTAGE "\":%s,\"" D_JSON_CURRENT "\":%s}"),
adc_idx, energy_chr, power_chr, voltage_chr, current_chr);
#ifdef USE_DOMOTICZ
if ((0 == tele_period) && (!domo_flag[ADC_CT_POWER])) {
DomoticzSensor(DZ_POWER_ENERGY, power_chr);
@ -466,7 +474,7 @@ void AdcShow(bool json) {
uint16_t value = new_value / Adc[idx].param1;
if (json) {
AdcShowContinuation(&jsonflg);
ResponseAppend_P(PSTR("\"Joy%d\":%d"), idx + offset, value);
ResponseAppend_P(PSTR("\"Joy%s\":%d"), adc_idx, value);
}
break;
}