From 72baffdf818b61fa883e793bc91db0acc5991447 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 13 Aug 2019 14:52:46 +0200 Subject: [PATCH] Refactor Counter and Dht code Refactor Counter and Dht code --- sonoff/sonoff.ino | 21 +++---------- sonoff/xdrv_24_buzzer.ino | 10 ++----- sonoff/xsns_01_counter.ino | 24 +++++++++++---- sonoff/xsns_06_dht.ino | 61 ++++++++++++++++++++++---------------- 4 files changed, 62 insertions(+), 54 deletions(-) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 172fe3bf5..c1bbba867 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -133,7 +133,6 @@ uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 uint8_t led_power = 0; // LED power state uint8_t ledlnk_inverted = 0; // Link LED inverted flag (1 = (0 = On, 1 = Off)) uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted) -uint8_t counter_no_pullup = 0; // Counter input pullup flag (1 = No pullup) uint8_t energy_flg = 0; // Energy monitor configured uint8_t light_type = 0; // Light types uint8_t serial_in_byte; // Received byte @@ -152,7 +151,6 @@ bool stop_flash_rotate = false; // Allow flash configuration rotatio bool blinkstate = false; // LED state //bool latest_uptime_flag = true; // Signal latest uptime bool pwm_present = false; // Any PWM channel configured with SetOption15 0 -bool dht_flg = false; // DHT configured bool i2c_flg = false; // I2C configured bool spi_flg = false; // SPI configured bool soft_spi_flg = false; // Software SPI configured @@ -1245,6 +1243,7 @@ void GpioInit(void) if (mpin) { XdrvMailbox.index = mpin; + XdrvMailbox.payload = i; if ((mpin >= GPIO_SWT1_NP) && (mpin < (GPIO_SWT1_NP + MAX_SWITCHES))) { SwitchPullupFlag(mpin - GPIO_SWT1_NP); @@ -1279,20 +1278,6 @@ void GpioInit(void) bitSet(pwm_inverted, mpin - GPIO_PWM1_INV); mpin -= (GPIO_PWM1_INV - GPIO_PWM1); } - else if ((mpin >= GPIO_CNTR1_NP) && (mpin < (GPIO_CNTR1_NP + MAX_COUNTERS))) { - bitSet(counter_no_pullup, mpin - GPIO_CNTR1_NP); - mpin -= (GPIO_CNTR1_NP - GPIO_CNTR1); - } -#ifdef USE_DHT - else if ((mpin >= GPIO_DHT11) && (mpin <= GPIO_SI7021)) { - if (DhtSetup(i, mpin)) { - dht_flg = true; - mpin = GPIO_DHT11; - } else { - mpin = 0; - } - } -#endif // USE_DHT else if (XdrvCall(FUNC_PIN_STATE)) { mpin = XdrvMailbox.index; } @@ -1326,7 +1311,9 @@ void GpioInit(void) #ifdef USE_I2C i2c_flg = ((pin[GPIO_I2C_SCL] < 99) && (pin[GPIO_I2C_SDA] < 99)); - if (i2c_flg) { Wire.begin(pin[GPIO_I2C_SDA], pin[GPIO_I2C_SCL]); } + if (i2c_flg) { + Wire.begin(pin[GPIO_I2C_SDA], pin[GPIO_I2C_SCL]); + } #endif // USE_I2C devices_present = 1; diff --git a/sonoff/xdrv_24_buzzer.ino b/sonoff/xdrv_24_buzzer.ino index 52bcab4d4..c7b6fac68 100644 --- a/sonoff/xdrv_24_buzzer.ino +++ b/sonoff/xdrv_24_buzzer.ino @@ -134,7 +134,7 @@ void (* const BuzzerCommand[])(void) PROGMEM = { void CmndBuzzer(void) { - // Buzzer ,, + // Buzzer ,,, // All parameters are optional // // Buzzer = Buzzer 1,1,1 = Beep once with both duration and pause set to 100mS @@ -151,12 +151,8 @@ void CmndBuzzer(void) parm[i] = strtoul(str, nullptr, 0); i++; } - for (uint32_t i = 0; i < 4; i++) { - if (i < 3) { - if (parm[i] < 1) { parm[i] = 1; } // Default Count, On time, Off time - } else { - if (parm[3] < 0) { parm[3] = 0; } // Default Tune bitmap - } + for (uint32_t i = 0; i < 3; i++) { + if (parm[i] < 1) { parm[i] = 1; } // Default Count, On time, Off time } BuzzerBeep(parm[0], parm[1], parm[2], parm[3]); } else { diff --git a/sonoff/xsns_01_counter.ino b/sonoff/xsns_01_counter.ino index 61245e2e6..eaac77282 100644 --- a/sonoff/xsns_01_counter.ino +++ b/sonoff/xsns_01_counter.ino @@ -35,6 +35,7 @@ void (* const CounterCommand[])(void) PROGMEM = { &CmndCounter, &CmndCounterType, &CmndCounterDebounce }; unsigned long last_counter_timer[MAX_COUNTERS]; // Last counter time in micro seconds +uint8_t counter_no_pullup = 0; // Counter input pullup flag (1 = No pullup) #ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception void CounterUpdate(uint8_t index) ICACHE_RAM_ATTR; @@ -81,13 +82,14 @@ void CounterUpdate4(void) /********************************************************************************************/ -void CounterSaveState(void) +bool CounterPinState() { - for (uint32_t i = 0; i < MAX_COUNTERS; i++) { - if (pin[GPIO_CNTR1 +i] < 99) { - Settings.pulse_counter[i] = RtcSettings.pulse_counter[i]; - } + if ((XdrvMailbox.index >= GPIO_CNTR1_NP) && (XdrvMailbox.index < (GPIO_CNTR1_NP + MAX_COUNTERS))) { + bitSet(counter_no_pullup, XdrvMailbox.index - GPIO_CNTR1_NP); + XdrvMailbox.index -= (GPIO_CNTR1_NP - GPIO_CNTR1); + return true; } + return false; } void CounterInit(void) @@ -103,6 +105,15 @@ void CounterInit(void) } } +void CounterSaveState(void) +{ + for (uint32_t i = 0; i < MAX_COUNTERS; i++) { + if (pin[GPIO_CNTR1 +i] < 99) { + Settings.pulse_counter[i] = RtcSettings.pulse_counter[i]; + } + } +} + void CounterShow(bool json) { bool header = false; @@ -212,6 +223,9 @@ bool Xsns01(uint8_t function) case FUNC_INIT: CounterInit(); break; + case FUNC_PIN_STATE: + result = CounterPinState(); + break; } return result; } diff --git a/sonoff/xsns_06_dht.ino b/sonoff/xsns_06_dht.ino index 69e543ab8..c0062edb1 100644 --- a/sonoff/xsns_06_dht.ino +++ b/sonoff/xsns_06_dht.ino @@ -34,6 +34,7 @@ uint32_t dht_max_cycles; uint8_t dht_data[5]; uint8_t dht_sensors = 0; +bool dht_active = true; // DHT configured struct DHTSTRUCT { uint8_t pin; @@ -163,33 +164,40 @@ void DhtReadTempHum(uint8_t sensor) } } -bool DhtSetup(uint8_t pin, uint8_t type) -{ - bool success = false; - - if (dht_sensors < DHT_MAX_SENSORS) { - Dht[dht_sensors].pin = pin; - Dht[dht_sensors].type = type; - dht_sensors++; - success = true; - } - return success; -} - /********************************************************************************************/ +bool DhtPinState() +{ + if ((XdrvMailbox.index >= GPIO_DHT11) && (XdrvMailbox.index <= GPIO_SI7021)) { + if (dht_sensors < DHT_MAX_SENSORS) { + Dht[dht_sensors].pin = XdrvMailbox.payload; + Dht[dht_sensors].type = XdrvMailbox.index; + dht_sensors++; + XdrvMailbox.index = GPIO_DHT11; + } else { + XdrvMailbox.index = 0; + } + return true; + } + return false; +} + void DhtInit(void) { - dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor. + if (dht_sensors) { + dht_max_cycles = microsecondsToClockCycles(1000); // 1 millisecond timeout for reading pulses from DHT sensor. - for (uint32_t i = 0; i < dht_sensors; i++) { - pinMode(Dht[i].pin, INPUT_PULLUP); - Dht[i].lastreadtime = 0; - Dht[i].lastresult = 0; - GetTextIndexed(Dht[i].stype, sizeof(Dht[i].stype), Dht[i].type, kSensorNames); - if (dht_sensors > 1) { - snprintf_P(Dht[i].stype, sizeof(Dht[i].stype), PSTR("%s%c%02d"), Dht[i].stype, IndexSeparator(), Dht[i].pin); + for (uint32_t i = 0; i < dht_sensors; i++) { + pinMode(Dht[i].pin, INPUT_PULLUP); + Dht[i].lastreadtime = 0; + Dht[i].lastresult = 0; + GetTextIndexed(Dht[i].stype, sizeof(Dht[i].stype), Dht[i].type, kSensorNames); + if (dht_sensors > 1) { + snprintf_P(Dht[i].stype, sizeof(Dht[i].stype), PSTR("%s%c%02d"), Dht[i].stype, IndexSeparator(), Dht[i].pin); + } } + } else { + dht_active = false; } } @@ -244,11 +252,8 @@ bool Xsns06(uint8_t function) { bool result = false; - if (dht_flg) { + if (dht_active) { switch (function) { - case FUNC_INIT: - DhtInit(); - break; case FUNC_EVERY_SECOND: DhtEverySecond(); break; @@ -260,6 +265,12 @@ bool Xsns06(uint8_t function) DhtShow(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + DhtInit(); + break; + case FUNC_PIN_STATE: + result = DhtPinState(); + break; } } return result;