Commit Graph

43 Commits

Author SHA1 Message Date
Maxim Prokhorov
a55cf0b847 light: stop the update request from running twice
In case the rgb+white(cct) brightness function was selected by the
configuration, internal calculations will always result in 'changed' being 'true'
and as a result it will continiously re-create the transition object.

fix #2467 (at least for now, the brightness function should be actually fixed)
2021-08-27 23:28:10 +03:00
Maxim Prokhorov
03573b783e webui: apply modules-... style globally
Otherwise, some things are still hidden
(or, depend on the order of incoming messages)

Make thermostat code use those as well, instead of injecting
thermostatVisible in every json payload
2021-08-22 06:42:16 +03:00
Maxim Prokhorov
6087becf86 mqtt: remove unnecessary const_cast of the topic 2021-08-21 02:48:27 +03:00
Maxim Prokhorov
e6a0e505b3 mqtt: payload is not const
Even if std::function compiles with the wrong (but compatible) signature
2021-08-21 02:48:27 +03:00
Maxim Prokhorov
d482baa725 light: refactor to use namespace(s)
Also clamp the value in setters / getters.
2021-08-16 04:31:36 +03:00
Maxim Prokhorov
658648d270 core: naming cleanup
Set -> On, when the handler could be added multiple times
Remove unused functions from the headers, and add correct names for the used ones.
2021-08-06 18:45:14 +03:00
Maxim Prokhorov
27f701773f settings: simplify migrateVersion() checks in modules
Just `if (version < N)` instead of `if (version && (version < N))`
Fix existing functions that were not checking for `version > 0`
2021-08-02 07:50:52 +03:00
Maxim Prokhorov
b0819fb7b2 light: more channel info in the terminal 2021-08-02 07:14:19 +03:00
Maxim Prokhorov
527f911ffb light: provide correct data to the /ws API
RGB is a #RRGGBB hex string, not a R,G,B
Current ltSaveDelay missing from the LIGHTS page
2021-07-23 17:50:21 +03:00
Maxim Prokhorov
b71d384c26 light: allow transition values to be reset during the run()
resolve #2451

Paranoid locking doesn't really matter in the current setup (but, leave some comments about how to solve it when it actually does).
Also copy internal tuple of save+transition+report and give the copy to the callback. Thus, don't assume the consumer is always nice,
and does not declare refs to internal struct variables, making this kind of hard to track.

Refactor internal code so it does not export functions to the built .o
2021-07-23 17:50:21 +03:00
Maxim Prokhorov
b80a2e9ffa terminal: trying to fix Print API usage
- more static flash strings and flash string formats
- println() will write \r\n when we mostly just use the \n
(and avoid mixing it up in the same function)
2021-04-03 20:34:34 +03:00
Maxim Prokhorov
f92116341e system: refactor build configurations
Namespace build configurations of modules, make more things into constexpr
(not fully finished though)

Unify code using ...Count() to parse IDs

Avoid using unsigned char aka uint8_t as index, prefer size_t
as most code already uses it anyway. Making sure we never accidentally
truncate the value or try to read it as 32bit-wide. Also, simplify
access to built in containers, since those use the wide type as well.

Renames led and button types, more consistent initialization and field access.
2021-03-31 09:41:04 +03:00
Maxim Prokhorov
78b4007f01 system: use direct status updates instead of broker
Get rid of status & config brokers, register status callbacks directly with the module.
2021-03-31 09:41:04 +03:00
Maxim Prokhorov
f013e04e22 lights: make sure channel target stays consistent
Make gamma & invert flags only apply to the transition itself.
Revert to previous behaviour, where the modified value is only ever seen
by the provider function.
2021-02-26 23:33:32 +03:00
Maxim Prokhorov
1a5f95c02e lights: more color refactoring
- clamp values via helper functions
- unify channel access as long
  slightly more ram, but we no longer cast values that are 32bit-wide anyway
- remove css option, send unconditionally as /hex
- reimplement hsv conversion functions (based on HA / python's colorsys)
  simplify calculations and allow to return standalone value structs for
  RGB and HSV
- named channel accessors
- implement HA color inputs / outputs
2021-02-26 18:44:12 +03:00
Maxim Prokhorov
65c8db72fb lights: fix rtcmem implementation shifts
ref. https://github.com/xoseperez/espurna/issues/2422#issuecomment-786171934

stop including mireds into the brightness when unpacking uint64 value
clamp brightness via the helper function
2021-02-26 18:34:06 +03:00
Maxim Prokhorov
7434ea3878 lights: finish up provider settings
get rid of LIGHT_CHANNELS, but keep channel setting for the my92xx
also clean up recent changes, channel command did not actually work
2021-02-10 20:05:13 +03:00
Maxim Prokhorov
00b470bc83 lights: always check for transformation flags
amend bdbd127bfb
2021-02-09 23:50:10 +03:00
Maxim Prokhorov
009021118b lights: fix my92xx build 2021-02-09 22:17:41 +03:00
Maxim Prokhorov
bdbd127bfb lights: handle value transformations in the transition 2021-02-09 22:06:41 +03:00
Maxim Prokhorov
ec5311c850 lights: terminal output specific values 2021-02-09 21:36:01 +03:00
Maxim Prokhorov
f6527e4325 lights: fix channel description, remove tags table from globals 2021-02-09 13:49:34 +03:00
Maxim Prokhorov
489813864e lights: match original rtcmem union layout 2021-02-09 13:48:47 +03:00
Maxim Prokhorov
ecaa5fa457 lights: explain the brightness stub and fix the gcc-4.8.2 build 2021-02-09 12:26:20 +03:00
Maxim Prokhorov
5c620723dd lights: one more try at fixing the boot sequence
Make sure 'custom provider' `lightAdd()` scheduled function locks further channel modifications,
and take care of light sanity checks before the light update is even scheduled.

Reworked rtcmem reader / writer to use default values, instead of accidentaly using
the unitialized mem of the stack. Also, reworked const names used by the lights module
to distinguish them from the build flags.
2021-02-08 21:39:46 +03:00
Maxim Prokhorov
af4477999d lights: allow to trigger an update when brightness is 0 2021-02-06 08:37:09 +03:00
Maxim Prokhorov
b35a565126 lights: fix changing channel value while state is the same 2021-02-06 05:53:31 +03:00
Maxim Prokhorov
0f723b0248 settings: handle old keys in the respective modules
fixes #2419
2021-02-06 04:44:26 +03:00
Maxim Prokhorov
12747bf8ce lights: fix inconsistent code generation with gcc-4.8.2
Make sure transition is always pushed into the vector the same way

Only happens with ESPURNA_BUILD_SINGLE_SOURCE=1:

/home/runner/.platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/bits/vector.tcc:405:31: error: __c causes a section type conflict with __c
    _M_check_len(size_type(1), __EXCSTR("vector::_M_emplace_back_aux"));
                               ^
/home/runner/.platformio/packages/toolchain-xtensa/xtensa-lx106-elf/include/c++/4.8.2/bits/vector.tcc:71:23: note: '__c' was declared here
  __throw_length_error(__EXCSTR(__N("vector::reserve")));
2021-02-06 04:44:25 +03:00
Maxim Prokhorov
34d63c155d lights: refactor my92xx mapping build flags 2021-02-06 04:44:25 +03:00
Maxim Prokhorov
1fa10cd7cf lights: fix my92xx channel setup 2021-02-06 04:44:25 +03:00
Maxim Prokhorov
b4093418f4 lights: no need for a generic function when we only map for pwm 2021-02-06 04:44:25 +03:00
Maxim Prokhorov
2f39d0db8a lights: refactor providers
- make sure we have custom provider before calling the update,
replace old check in the update function
- fix race condition in the provider function, causing complete lock-up of
lights controls (triggered by sending light updates very fast. for example,
using arrow keys with the webui sliders; ref. #2424)
- rework state change functions to work using loop() func instead of a
pair of scheduled functions. we should not go out-of-sync now, and it
also avoids forcibly creating std::func object each invocation
- rework other providers to be more in line with custom callbacks -
introduce state, value and update calls as a common way to handle lights
- correctly handle state change when turning OFF and ON
- update tuya to store the last state, never send channel values when OFF (ref. #2424)
2021-02-06 04:43:35 +03:00
Maxim Prokhorov
4486f82f09 lights: terminal commands clean-up
- fix argc checks in state
- use ctx as output for commands
- show state when calling without args
2021-02-06 01:20:09 +03:00
Maxim Prokhorov
4d157ccd5b ha: discovery refactoring
- retain setting
- generate topic and message in the discovery object instead of
creating them all at once
- re-implement relays, lights and sensors discovery
- rework name normalization, use relative magnitude indexes
- replace original lights code with schema: json, subscribe to a special
topic to handle lights instead of using the common API
(still wip - colors, hs, temperature settings)

resolve #630
resolve #1564
resolve #2403
2021-01-26 01:02:49 +03:00
Maxim Prokhorov
021f0afb86 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
2021-01-24 03:38:02 +03:00
Maxim Prokhorov
808981ca39 webui: update colorpicker
ref. https://github.com/xoseperez/espurna/issues/1132#issuecomment-431868924
it is slightly bigger, but better looking and no jquery dependency

rework colorpicker style as well, hide to prevent sending garbage when
lights are off. update lights module to send `lightState()` and separate
state from status payloads

yes, input[type="color"] is an option, but it does not look good
2021-01-15 22:46:55 +03:00
Max Prokhorov
8ceeebdb24 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
2021-01-14 10:39:18 +03:00
Max Prokhorov
8e80a7786c api: rework plain and JSON implementations (#2405)
- match paths through a custom AsyncWebHandler instead of using generic not-found fallback handler
- allow MQTT-like patterns when registering paths (`simple/path`, `path/+/something`, `path/#`)
Replaces `relay/0`, `relay/1` etc. with `relay/+`. Magnitudes are plain paths, but using `/+` in case there's more than 1 magnitude of the same type.
- restore `std::function` as callback container (no more single-byte arg nonsense). Still, limit to 1 type per handler type
- adds JSON handlers which will receive JsonObject root as both input and output. Same logic as plain - GET returns resource data, PUT updates it.
- breaking change to `apiAuthenticate(request)`, it no longer will do `request->send(403)` and expect this to be handled externally.
- allow `Api-Key` header containing the key, works for both GET & PUT plain requests. The only way to set apikey for JSON.
- add `ApiRequest::param` to retrieve both GET and PUT params (aka args), remove ApiBuffer
- remove `API_BUFFER_SIZE`. Allow custom form-data key=value pairs for requests, allow to send basic `String`.
- add `API_JSON_BUFFER_SIZE` for the JSON buffer (both input and output)
- `/apis` replaced with `/api/list`, no longer uses custom handler and is an `apiRegister` callback
- `/api/rpc` custom handler replaced with an `apiRegister` callback

WIP further down:
- no more `webLog` for API requests, unless `webAccessLog` / `WEB_ACCESS_LOG` is set to `1`. This also needs to happen to the other handlers. 
- migrate to ArduinoJson v6, since it become apparent it is actually a good upgrade :)
- actually make use of JSON endpoints more, right now it's just existing GET for sensors and relays
- fork ESPAsyncWebServer to cleanup path parsing and temporary objects attached to the request (also, fix things a lot of things based on PRs there...)
2020-12-05 14:14:38 +03:00
Maxim Prokhorov
f3c185cc73 api: rework storage & json calls, drop std::function
- reduce overall size of the structure, store a single required entity
  inside of Api object itself.
- tweak internal functions to expect a certain path size
- (kind of a hack) add manual calls to vector<Api>::reserve() as we go over
  the current capacity and vector increases it's size times 2.
  e.g. for 9 relays, we would allocate space for 32 Api objects
  when vector size goes from 16 to 32, after we add 18 Api objects with relay + pulse
- (breaking) json calls on a separate path, don't waste time encoding
  a single entity as json object when we can encode more things
2020-08-13 16:54:36 +03:00
Max Prokhorov
b8fc8cd1fd Terminal: change command-line parser (#2247)
Change the underlying command line handling:
- switch to a custom parser, inspired by redis / sds
- update terminalRegisterCommand signature, pass only bare minimum
- clean-up `help` & `commands`. update settings `set`, `get` and `del`
- allow our custom test suite to run command-line tests
- clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`)
- send parsing errors to the debug log

As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT`
- MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API.
- Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS.

Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :)
Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
2020-05-25 23:41:37 +03:00
Max Prokhorov
84b51cf680 broker: declare and define per module (#2253)
* broker: declare and define per module

We no longer need to specify each Broker type in broker.h
But, we exchange that bit for explicit initialization of the instance

Define helper macros to generate boilerplate code
- namespace with Instance, Register(), Publish()
- forward Register(...) -> Instance.Register(...),
  Publish(...) -> Instance.Publish(...)

* don't check for broker when deps enable it
2020-05-18 02:29:17 +03:00
Max Prokhorov
edb23dbfc4 Convert .ino -> .cpp (#2228)
- general conversion from .ino modules into a separate .cpp files
- clean-up internal headers, place libraries into .h. guard .cpp with _SUPPORT flags 
- fix some instances of shared variables instead of public methods
- tweak build system to still build a single source file via os environment variable ESPURNA_BUILD_SINGLE_SOURCE
2020-04-30 13:55:07 +03:00