providers: relays, lights and buttons refactoring (#2414)
- gpio module now tracks the known providers (right now, hardware and mcp expander)
- refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions
- refactored button module to use gpio provider instead of referencing types itself
- removed dual & stm code from buttons, migrate both to relay module
- added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did)
- relays runtime configuration keys
- relay command now shows configured relays and current & target statuses
- refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead
- remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT
- allow to bind rf codes to real relays
- drop tuya-specific lights provider, remove tuya code from relays and lights modules
- integrate tuya via relay listeners and providers, use lights custom provider
- implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle)
- lights custom provider (global, not per-pin) and state listeners
- remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT
- lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT
- refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing
- transition time + step parameter for the lightUpdate
- report mask parameter for the lightUpdate
- minor fixes across the board
resolve #2222 3 years ago providers: relays, lights and buttons refactoring (#2414)
- gpio module now tracks the known providers (right now, hardware and mcp expander)
- refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions
- refactored button module to use gpio provider instead of referencing types itself
- removed dual & stm code from buttons, migrate both to relay module
- added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did)
- relays runtime configuration keys
- relay command now shows configured relays and current & target statuses
- refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead
- remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT
- allow to bind rf codes to real relays
- drop tuya-specific lights provider, remove tuya code from relays and lights modules
- integrate tuya via relay listeners and providers, use lights custom provider
- implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle)
- lights custom provider (global, not per-pin) and state listeners
- remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT
- lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT
- refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing
- transition time + step parameter for the lightUpdate
- report mask parameter for the lightUpdate
- minor fixes across the board
resolve #2222 3 years ago providers: relays, lights and buttons refactoring (#2414)
- gpio module now tracks the known providers (right now, hardware and mcp expander)
- refactored relay struct to use 'Provider' implementing setup,notify,change,boot instead of just BasePin actions
- refactored button module to use gpio provider instead of referencing types itself
- removed dual & stm code from buttons, migrate both to relay module
- added status notify and change callbacks for relayStatus (i.e. 'notify' when relay status was called, but not changed. and 'changed' when it did)
- relays runtime configuration keys
- relay command now shows configured relays and current & target statuses
- refactor the code using relayStatus(0, blah) under LIGHT_PROVIDER check to use lightState instead
- remove rfbridge code form relay module. implement through a basic state listener in the rfbridge module, depend on RELAY_SUPPORT
- allow to bind rf codes to real relays
- drop tuya-specific lights provider, remove tuya code from relays and lights modules
- integrate tuya via relay listeners and providers, use lights custom provider
- implement channel transitions for tuya. disabled by default, and transition time and step are overridden to 2000 + 100. needs to be set to some value below the total time (i.e. `total transition time / step time == number of steps`, we need to figure out a correct time that serial comms could handle)
- lights custom provider (global, not per-pin) and state listeners
- remove lights code from relay module. implement through providers & listeners in the lights module, depend on RELAY_SUPPORT
- lights per-channel relay provider (unused atm), depend on RELAY_SUPPORT
- refactored channel transition - calculate step only once, make sure time + step values are sane, generate quick transitions with very small delay (10ms hardcoded) for transitions during OFF state i.e. we no longer waste 500ms (or whatever transition time is set to) on boot doing nothing
- transition time + step parameter for the lightUpdate
- report mask parameter for the lightUpdate
- minor fixes across the board
resolve #2222 3 years ago sys: clean-up system-specific functions
- shrink utils source file, move heartbeat and boot management into system
- improvise with 'heartbeat' functionality. include scheduler implementation that will
manage the per-module heartbeat callbacks with individual 'mode' and
'interval' settings. current ones are mqtt (including relays, lights, thermostat), debug and
influxdb. preserve heartbeat NONE, ONCE and REPEAT, REPEAT_STATUS is effectively a hbReport & status bit.
- mqtt heartbeat is managed through mqttHeartbeat() callbacks
- tweak mqtt callbacks to use lists instead of the vector, slighly reducing the size of the .bin
- update WebUI, include report setting and update hbMode values
- make sure general.h settings include new heartbeat,
move constant definitions outside of the header
- correctly include dependencies through the .cpp, avoid leaking
internal details.
- as a side-effect, base headers are no longer included recursively
3 years ago |
|
- // -----------------------------------------------------------------------------
- // GUVA-S12SD UV Sensor
- // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
- // by Mustafa Tufan
- // -----------------------------------------------------------------------------
-
- #if SENSOR_SUPPORT && GUVAS12SD_SUPPORT
-
- #pragma once
-
-
- #include "BaseSensor.h"
- #include "../utils.h"
-
- // http://www.eoc-inc.com/genicom/GUVA-S12SD.pdf
- //
- // GUVA-S12D has a wide spectral range of 200nm-400nm
- // The output voltage and the UV index is linear, illumination intensity = 307 * Vsig where: Vsig is the value of voltage measured from the SIG pin of the interface, unit V.
- // illumination intensity unit: mW/m2 for the combination strength of UV light with wavelength range: 200nm-400nm
- // UV Index = illumination intensity / 200
- //
- // UV Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 10+
- // -----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+--------
- // mV | <50 | 227 | 318 | 408 | 503 | 606 | 696 | 795 | 881 | 976 | 1079 | 1170+
- // analog val | <10 | 46 | 65 | 83 | 103 | 124 | 142 | 162 | 180 | 200 | 221 | 240+
- //
-
- #define UV_SAMPLE_RATE 1
-
- class GUVAS12SDSensor : public BaseSensor {
-
- public:
-
- // ---------------------------------------------------------------------
- // Public
- // ---------------------------------------------------------------------
-
- GUVAS12SDSensor() {
- _count = 1;
- _sensor_id = SENSOR_GUVAS12SD_ID;
- }
-
- ~GUVAS12SDSensor() {
- gpioUnlock(_gpio);
- }
-
- // ---------------------------------------------------------------------
-
- void setGPIO(unsigned char gpio) {
- _gpio = gpio;
- }
-
- // ---------------------------------------------------------------------
-
- unsigned char getGPIO() {
- return _gpio;
- }
-
- // ---------------------------------------------------------------------
- // Sensor API
- // ---------------------------------------------------------------------
-
- // Initialization method, must be idempotent
- void begin() {
-
- // Manage GPIO lock
- if (_previous != GPIO_NONE) {
- gpioUnlock(_previous);
- }
-
- _previous = GPIO_NONE;
- if (!gpioLock(_gpio)) {
- _error = SENSOR_ERROR_GPIO_USED;
- return;
- }
- _previous = _gpio;
-
- _ready = true;
-
- }
-
- // Pre-read hook (usually to populate registers with up-to-date data)
- void pre() {
- _error = SENSOR_ERROR_OK;
- _read();
- }
-
- // Descriptive name of the sensor
- String description() {
- char buffer[18];
- snprintf(buffer, sizeof(buffer), "GUVAS12SD @ GPIO%hhu", _gpio);
- return String(buffer);
- }
-
- // Descriptive name of the slot # index
- String description(unsigned char index) {
- return description();
- };
-
- // Address of the sensor (it could be the GPIO or I2C address)
- String address(unsigned char index) {
- return String(_gpio);
- }
-
- // Type for slot # index
- unsigned char type(unsigned char index) {
- if (index == 0) return MAGNITUDE_UVI;
- return MAGNITUDE_NONE;
- }
-
- // Current value for slot # index
- double value(unsigned char index) {
- if (index == 0) return _uvindex;
- return 0;
- }
-
-
- protected:
-
- // ---------------------------------------------------------------------
- // Protected
- // ---------------------------------------------------------------------
-
- void _read() {
-
- int _average = 0;
-
- #if UV_SAMPLE_RATE == 1
- _average = analogRead(0);
- #else
- for (unsigned int i=0; i < UV_SAMPLE_RATE; i++) {
- _average += analogRead(0);
- nice_delay(2);
- }
- _average = (_average / UV_SAMPLE_RATE);
- #endif
- // _sensormV = _average / 1023*3.3;
-
- if (_average < 10) {
- _uvindex = 0;
- } else if (_average < 46) {
- _uvindex = (_average - 10) / (46-10);
- } else if (_average < 65) {
- _uvindex = 1 + ((_average - 46) / (65-46));
- } else if (_average < 83) {
- _uvindex = 2 + ((_average - 65) / (83-65));
- } else if (_average < 103) {
- _uvindex = 3 + ((_average - 83) / (103- 83));
- } else if (_average < 124) {
- _uvindex = 4 + ((_average - 103) / (124-103));
- } else if (_average < 142) {
- _uvindex = 5 + ((_average - 124) / (142-124));
- } else if (_average < 162) {
- _uvindex = 6 + ((_average - 142) / (162-142));
- } else if (_average < 180) {
- _uvindex = 7 + ((_average - 162) / (180-162));
- } else if (_average < 200) {
- _uvindex = 8 + ((_average - 180) / (200-180));
- } else if (_average < 221) {
- _uvindex = 9 + ((_average - 200) / (221-200));
- } else {
- _uvindex = 10;
- }
-
- }
-
- unsigned char _gpio = GPIO_NONE;
- unsigned char _previous = GPIO_NONE;
-
- double _uvindex = 0;
-
- };
-
- #endif // SENSOR_SUPPORT && GUVAS12SD_SUPPORT
|