diff --git a/.gitignore b/.gitignore index 75ff90814..16fb10282 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.pyc ## Project files ###### +.platformio .pioenvs .piolibdeps .clang_complete diff --git a/.gitpod.Dockerfile b/.gitpod.Dockerfile new file mode 100644 index 000000000..909bcf681 --- /dev/null +++ b/.gitpod.Dockerfile @@ -0,0 +1,5 @@ +FROM gitpod/workspace-full + +USER gitpod + +RUN pip3 install -U platformio && brew install uncrustify diff --git a/.gitpod.yml b/.gitpod.yml index 8ac16a8ad..228c1dbf3 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -1,3 +1,13 @@ tasks: - - before: pip3 install -U platformio - command: platformio run -e tasmota + - command: platformio run -e tasmota + +image: + file: .gitpod.Dockerfile + +vscode: + extensions: + - ms-vscode.cpptools@0.26.3:u3GsZ5PK12Ddr79vh4TWgQ== + - eamodio.gitlens@10.2.1:e0IYyp0efFqVsrZwsIe8CA== + - LaurentTreguier.uncrustify@2.18.0:/k8Osjj/XSuz09F+pEu7wg== + - Atishay-Jain.All-Autocomplete@0.0.23:fbZNfSpnd8XkAHGfAPS2rA== + - 2gua.rainbow-brackets@0.0.6:Tbu8dTz0i+/bgcKQTQ5b8g== diff --git a/I2CDEVICES.md b/I2CDEVICES.md index 457116347..71ceba8ac 100644 --- a/I2CDEVICES.md +++ b/I2CDEVICES.md @@ -74,4 +74,6 @@ Index | Define | Driver | Device | Address(es) | Description 49 | USE_VEML6075 | xsns_70 | VEML6075 | 0x10 | UVA/UVB/UVINDEX Sensor 50 | USE_VEML7700 | xsns_71 | VEML7700 | 0x10 | Ambient light intensity sensor 51 | USE_MCP9808 | xsns_72 | MCP9808 | 0x18 - 0x1F | Temperature sensor - 52 | USE_HP303B | xsns_73 | HP303B | 0x76 - 0x77 | Pressure and temperature sensor \ No newline at end of file + 52 | USE_HP303B | xsns_73 | HP303B | 0x76 - 0x77 | Pressure and temperature sensor + 53 | USE_MLX90640 | xdrv_84 | MLX90640 | 0x33 | IR array temperature sensor + 54 | USE_VL53L1X | xsns_77 | VL53L1X | 0x29 | Time-of-flight (ToF) distance sensor diff --git a/README.md b/README.md index 63b90933f..e9925c262 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ _Written for PlatformIO with limited support for Arduino IDE._ [![Chat](https://img.shields.io/discord/479389167382691863.svg)](https://discord.gg/Ks2Kzd4) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/arendst/Tasmota.svg)](http://isitmaintained.com/project/arendst/Tasmota "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/arendst/Tasmota.svg)](http://isitmaintained.com/project/arendst/Tasmota "Percentage of issues still open") +[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/arendst/Tasmota) If you like **Tasmota**, give it a star, or fork it and contribute! diff --git a/RELEASENOTES.md b/RELEASENOTES.md index d33297668..29ad008f8 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -55,30 +55,23 @@ The attached binaries can also be downloaded from http://ota.tasmota.com/tasmota ## Changelog -### Version 8.5.0 Hannah +### Version 8.5.0.1 -- Remove support for direct upgrade from versions before 6.6.0.11 to versions after 8.4.0.1 -- Change references from http://thehackbox.org/tasmota/ to http://ota.tasmota.com/tasmota/ -- Change triple-mode TLS via configuration in a single firmware (TLS AWS IoT, Letsencrypt and No-TLS) -- Change White blend mode to using command ``SetOption 105`` instead of ``RGBWWTable`` -- Fix ESP32 PWM range -- Fix display power control (#9114) - Fix energy total counters (#9263, #9266) -- Add command ``SetOption102 0/1`` to set Baud rate for Teleinfo communication (0 = 1200 or 1 = 9600) -- Add command ``SetOption103 0/1`` to set TLS mode when TLS is selected -- Add command ``SetOption104 1`` to disable all MQTT retained messages -- Add command ``SetOption106 1`` to create a virtual White ColorTemp for RGBW lights -- Add command ``SetOption107 0/1`` to select virtual White as (0) Warm or (1) Cold -- Add command ``SetOption108 0/1`` to enable Teleinfo telemetry into Tasmota Energy MQTT (0) or Teleinfo only (1) -- Add command ``SetOption109 1`` to force gen1 Alexa mode, for Echo Dot 2nd gen devices only -- Add command ``Restart 2`` to halt system. Needs hardware reset or power cycle to restart (#9046) -- Add command ``PowerDelta1`` to ``PowerDelta3`` to trigger on up to three phases (#9134) -- Add Zigbee options to ``ZbSend`` ``Config`` and ``ReadCondig`` -- Add Zigbee better support for IKEA Motion Sensor -- Add Zigbee web gui widget for Battery and Temp/Humidity/Pressure sensors -- Add Zigbee web ui for power metering plugs -- Add better configuration corruption recovery (#9046) -- Add virtual CT for 4 channels lights, emulating a 5th channel -- Add support for DYP ME007 ultrasonic distance sensor by Janusz Kostorz (#9113) -- Add ESP32 Analog input support for GPIO32 to GPIO39 -- Add experimental support for ESP32 TTGO Watch and I2S Audio by Gerhard Mutz +- Fix crash in ``ZbRestore`` +- Fix reset BMP sensors when executing command ``SaveData`` and define USE_DEEPSLEEP enabled (#9300) +- Fix ``status 0`` message when using define USE_MQTT_TLS due to small log buffer (#9305) +- Fix ``status 13`` exception 9 when more than one shutter is configured +- Fix ``status 13`` json message +- Fix Shelly 2.5 higher temperature regression from 8.2.0.1 (#7991) +- Change replace ArduinoJson with JSMN for JSON parsing +- Change ``WakeUp`` uses 256 steps instead of 100 (#9241) +- Add command ``SetOption110 1`` to disable Zigbee auto-config when pairing new devices +- Add command ``SetOption111 1`` to enable frequency output for buzzer GPIO (#8994) +- Add command ``SetOption112 1`` to enable friendly name in zigbee topic (use with SetOption89) +- Add ``#define USE_MQTT_AWS_IOT_LIGHT`` for password based AWS IoT authentication +- Add ``#define MQTT_LWT_OFFLINE`` and ``#define MQTT_LWT_ONLINE`` to user_config.h (#9395) +- Add new shutter modes (#9244) +- Add Zigbee auto-config when pairing +- Add support for MLX90640 IR array temperature sensor by Christian Baars +- Add support for VL53L1X time of flight sensor by Johann Obermeier diff --git a/lib/ArduinoJson-5.13.4/ArduinoJson.h b/lib/ArduinoJson-5.13.4/ArduinoJson.h deleted file mode 100644 index 9f78b9f18..000000000 --- a/lib/ArduinoJson-5.13.4/ArduinoJson.h +++ /dev/null @@ -1,5 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#include "src/ArduinoJson.h" diff --git a/lib/ArduinoJson-5.13.4/CHANGELOG.md b/lib/ArduinoJson-5.13.4/CHANGELOG.md deleted file mode 100644 index 3616b176a..000000000 --- a/lib/ArduinoJson-5.13.4/CHANGELOG.md +++ /dev/null @@ -1,483 +0,0 @@ -ArduinoJson: change log -======================= - -v5.13.4 -------- - -* Removed spurious files in the Particle library - -v5.13.3 -------- - -* Improved float serialization when `-fsingle-precision-constant` is used -* Fixed `JsonVariant::is()` that returned true for empty strings -* Fixed `JsonVariant::is()` (closes #763) - -v5.13.2 -------- - -* Fixed `JsonBuffer::parse()` not respecting nesting limit correctly (issue #693) -* Fixed inconsistencies in nesting level counting (PR #695 from Zhenyu Wu) -* Fixed null values that could be pass to `strcmp()` (PR #745 from Mike Karlesky) -* Added macros `ARDUINOJSON_VERSION`, `ARDUINOJSON_VERSION_MAJOR`... - -v5.13.1 -------- - -* Fixed `JsonVariant::operator|(int)` that returned the default value if the variant contained a double (issue #675) -* Allowed non-quoted key to contain underscores (issue #665) - -v5.13.0 -------- - -* Changed the rules of string duplication (issue #658) -* `RawJson()` accepts any kind of string and obeys to the same rules for duplication -* Changed the return type of `strdup()` to `const char*` to prevent double duplication -* Marked `strdup()` as deprecated - -> ### New rules for string duplication -> -> | type | duplication | -> |:---------------------------|:------------| -> | const char* | no | -> | char* | ~~no~~ yes | -> | String | yes | -> | std::string | yes | -> | const __FlashStringHelper* | yes | -> -> These new rules make `JsonBuffer::strdup()` useless. - -v5.12.0 -------- - -* Added `JsonVariant::operator|` to return a default value (see below) -* Added a clear error message when compiled as C instead of C++ (issue #629) -* Added detection of MPLAB XC compiler (issue #629) -* Added detection of Keil ARM Compiler (issue #629) -* Added an example that shows how to save and load a configuration file -* Reworked all other examples - -> ### How to use the new feature? -> -> If you have a block like this: -> -> ```c++ -> const char* ssid = root["ssid"]; -> if (!ssid) -> ssid = "default ssid"; -> ``` -> -> You can simplify like that: -> -> ```c++ -> const char* ssid = root["ssid"] | "default ssid"; -> ``` - -v5.11.2 -------- - -* Fixed `DynamicJsonBuffer::clear()` not resetting allocation size (issue #561) -* Fixed incorrect rounding for float values (issue #588) - -v5.11.1 -------- - -* Removed dependency on `PGM_P` as Particle 0.6.2 doesn't define it (issue #546) -* Fixed warning "dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]" -* Fixed warning "floating constant exceeds range of 'float' [-Woverflow]" (issue #544) -* Fixed warning "this statement may fall through" [-Wimplicit-fallthrough=] (issue #539) -* Removed `ARDUINOJSON_DOUBLE_IS_64BITS` as it became useless. -* Fixed too many decimals places in float serialization (issue #543) - -v5.11.0 -------- - -* Made `JsonBuffer` non-copyable (PR #524 by @luisrayas3) -* Added `StaticJsonBuffer::clear()` -* Added `DynamicJsonBuffer::clear()` - -v5.10.1 -------- - -* Fixed IntelliSense errors in Visual Micro (issue #483) -* Fixed compilation in IAR Embedded Workbench (issue #515) -* Fixed reading "true" as a float (issue #516) -* Added `ARDUINOJSON_DOUBLE_IS_64BITS` -* Added `ARDUINOJSON_EMBEDDED_MODE` - -v5.10.0 -------- - -* Removed configurable number of decimal places (issues #288, #427 and #506) -* Changed exponentiation thresholds to `1e7` and `1e-5` (issues #288, #427 and #506) -* `JsonVariant::is()` now returns `true` for integers -* Fixed error `IsBaseOf is not a member of ArduinoJson::TypeTraits` (issue #495) -* Fixed error `forming reference to reference` (issue #495) - -> ### BREAKING CHANGES :warning: -> -> | Old syntax | New syntax | -> |:--------------------------------|:--------------------| -> | `double_with_n_digits(3.14, 2)` | `3.14` | -> | `float_with_n_digits(3.14, 2)` | `3.14f` | -> | `obj.set("key", 3.14, 2)` | `obj["key"] = 3.14` | -> | `arr.add(3.14, 2)` | `arr.add(3.14)` | -> -> | Input | Old output | New output | -> |:----------|:-----------|:-----------| -> | `3.14159` | `3.14` | `3.14159` | -> | `42.0` | `42.00` | `42` | -> | `0.0` | `0.00` | `0` | -> -> | Expression | Old result | New result | -> |:-------------------------------|:-----------|:-----------| -> | `JsonVariant(42).is()` | `true` | `true` | -> | `JsonVariant(42).is()` | `false` | `true` | -> | `JsonVariant(42).is()` | `false` | `true` | - -v5.9.0 ------- - -* Added `JsonArray::remove(iterator)` (issue #479) -* Added `JsonObject::remove(iterator)` -* Renamed `JsonArray::removeAt(size_t)` into `remove(size_t)` -* Renamed folder `include/` to `src/` -* Fixed warnings `floating constant exceeds range of float`and `floating constant truncated to zero` (issue #483) -* Removed `Print` class and converted `printTo()` to a template method (issue #276) -* Removed example `IndentedPrintExample.ino` -* Now compatible with Particle 0.6.1, thanks to Jacob Nite (issue #294 and PR #461 by @foodbag) - -v5.8.4 ------- - -* Added custom implementation of `strtod()` (issue #453) -* Added custom implementation of `strtol()` (issue #465) -* `char` is now treated as an integral type (issue #337, #370) - -v5.8.3 ------- - -* Fixed an access violation in `DynamicJsonBuffer` when memory allocation fails (issue #433) -* Added operators `==` and `!=` for two `JsonVariant`s (issue #436) -* Fixed `JsonVariant::operator[const FlashStringHelper*]` (issue #441) - -v5.8.2 ------- - -* Fixed parsing of comments (issue #421) -* Fixed ignored `Stream` timeout (issue #422) -* Made sure we don't read more that necessary (issue #422) -* Fixed error when the key of a `JsonObject` is a `char[]` (issue #423) -* Reduced code size when using `const` references -* Fixed error with string of type `unsigned char*` (issue #428) -* Added `deprecated` attribute on `asArray()`, `asObject()` and `asString()` (issue #420) - -v5.8.1 ------- - -* Fixed error when assigning a `volatile int` to a `JsonVariant` (issue #415) -* Fixed errors with Variable Length Arrays (issue #416) -* Fixed error when both `ARDUINOJSON_ENABLE_STD_STREAM` and `ARDUINOJSON_ENABLE_ARDUINO_STREAM` are set to `1` -* Fixed error "Stream does not name a type" (issue #412) - -v5.8.0 ------- - -* Added operator `==` to compare `JsonVariant` and strings (issue #402) -* Added support for `Stream` (issue #300) -* Reduced memory consumption by not duplicating spaces and comments - -> ### BREAKING CHANGES :warning: -> -> `JsonBuffer::parseObject()` and `JsonBuffer::parseArray()` have been pulled down to the derived classes `DynamicJsonBuffer` and `StaticJsonBufferBase`. -> -> This means that if you have code like: -> -> ```c++ -> void myFunction(JsonBuffer& jsonBuffer); -> ``` -> -> you need to replace it with one of the following: -> -> ```c++ -> void myFunction(DynamicJsonBuffer& jsonBuffer); -> void myFunction(StaticJsonBufferBase& jsonBuffer); -> template void myFunction(TJsonBuffer& jsonBuffer); -> ``` - -v5.7.3 ------- - -* Added an `printTo(char[N])` and `prettyPrintTo(char[N])` (issue #292) -* Added ability to set a nested value like this: `root["A"]["B"] = "C"` (issue #352) -* Renamed `*.ipp` to `*Impl.hpp` because they were ignored by Arduino IDE (issue #396) - -v5.7.2 ------- - -* Made PROGMEM available on more platforms (issue #381) -* Fixed PROGMEM causing an exception on ESP8266 (issue #383) - -v5.7.1 ------- - -* Added support for PROGMEM (issue #76) -* Fixed compilation error when index is not an `int` (issue #381) - -v5.7.0 ------- - -* Templatized all functions using `String` or `std::string` -* Removed `ArduinoJson::String` -* Removed `JsonVariant::defaultValue()` -* Removed non-template `JsonObject::get()` and `JsonArray.get()` -* Fixed support for `StringSumHelper` (issue #184) -* Replaced `ARDUINOJSON_USE_ARDUINO_STRING` by `ARDUINOJSON_ENABLE_STD_STRING` and `ARDUINOJSON_ENABLE_ARDUINO_STRING` (issue #378) -* Added example `StringExample.ino` to show where `String` can be used -* Increased default nesting limit to 50 when compiled for a computer (issue #349) - -> ### BREAKING CHANGES :warning: -> -> The non-template functions `JsonObject::get()` and `JsonArray.get()` have been removed. This means that you need to explicitely tell the type you expect in return. -> -> Old code: -> -> ```c++ -> #define ARDUINOJSON_USE_ARDUINO_STRING 0 -> JsonVariant value1 = myObject.get("myKey"); -> JsonVariant value2 = myArray.get(0); -> ``` -> -> New code: -> -> ```c++ -> #define ARDUINOJSON_ENABLE_ARDUINO_STRING 0 -> #define ARDUINOJSON_ENABLE_STD_STRING 1 -> JsonVariant value1 = myObject.get("myKey"); -> JsonVariant value2 = myArray.get(0); -> ``` - -v5.6.7 ------- - -* Fixed `array[idx].as()` and `object[key].as()` -* Fixed return value of `JsonObject::set()` (issue #350) -* Fixed undefined behavior in `Prettyfier` and `Print` (issue #354) -* Fixed parser that incorrectly rejected floats containing a `+` (issue #349) - -v5.6.6 ------- - -* Fixed `-Wparentheses` warning introduced in v5.6.5 (PR #335 by @nuket) -* Added `.mbedignore` for ARM mbdeb (PR #334 by @nuket) -* Fixed `JsonVariant::success()` which didn't propagate `JsonArray::success()` nor `JsonObject::success()` (issue #342). - -v5.6.5 ------- - -* `as()` now returns `true` when input is `null` (issue #330) - -v5.6.4 ------- - -* Fixed error in float serialization (issue #324) - -v5.6.3 ------- - -* Improved speed of float serialization (about twice faster) -* Added `as()` as a synonym for `as()`... (issue #291) -* Fixed `call of overloaded isinf(double&) is ambiguous` (issue #284) - -v5.6.2 ------- - -* Fixed build when another lib does `#undef isnan` (issue #284) - -v5.6.1 ------- - -* Added missing `#pragma once` (issue #310) - -v5.6.0 ------- - -* ArduinoJson is now a header-only library (issue #199) - -v5.5.1 ------- - -* Fixed compilation error with Intel Galileo (issue #299) - -v5.5.0 ------- - -* Added `JsonVariant::success()` (issue #279) -* Renamed `JsonVariant::invalid()` to `JsonVariant::defaultValue()` - -v5.4.0 ------- - -* Changed `::String` to `ArduinoJson::String` (issue #275) -* Changed `::Print` to `ArduinoJson::Print` too - -v5.3.0 ------- - -* Added custom implementation of `ftoa` (issues #266, #267, #269 and #270) -* Added `JsonVariant JsonBuffer::parse()` (issue #265) -* Fixed `unsigned long` printed as `signed long` (issue #170) - -v5.2.0 ------- - -* Added `JsonVariant::as()` as a synonym for `JsonVariant::as()` (issue #257) -* Added example `JsonHttpClient` (issue #256) -* Added `JsonArray::copyTo()` and `JsonArray::copyFrom()` (issue #254) -* Added `RawJson()` to insert pregenerated JSON portions (issue #259) - -v5.1.1 ------- - -* Removed `String` duplication when one replaces a value in a `JsonObject` (PR #232 by @ulion) - -v5.1.0 ------- - -* Added support of `long long` (issue #171) -* Moved all build settings to `ArduinoJson/Configuration.hpp` - -> ### BREAKING CHANGE :warning: -> -> If you defined `ARDUINOJSON_ENABLE_STD_STREAM`, you now need to define it to `1`. - -v5.0.8 ------- - -* Made the library compatible with [PlatformIO](http://platformio.org/) (issue #181) -* Fixed `JsonVariant::is()` that was incorrectly returning false (issue #214) - -v5.0.7 ------- - -* Made library easier to use from a CMake project: simply `add_subdirectory(ArduinoJson/src)` -* Changed `String` to be a `typedef` of `std::string` (issues #142 and #161) - -> ### BREAKING CHANGES :warning: -> -> - `JsonVariant(true).as()` now returns `"true"` instead of `"1"` -> - `JsonVariant(false).as()` now returns `"false"` instead of `"0"` - -v5.0.6 ------- - -* Added parameter to `DynamicJsonBuffer` constructor to set initial size (issue #152) -* Fixed warning about library category in Arduino 1.6.6 (issue #147) -* Examples: Added a loop to wait for serial port to be ready (issue #156) - -v5.0.5 ------- - -* Added overload `JsonObjectSuscript::set(value, decimals)` (issue #143) -* Use `float` instead of `double` to reduce the size of `JsonVariant` (issue #134) - -v5.0.4 ------- - -* Fixed ambiguous overload with `JsonArraySubscript` and `JsonObjectSubscript` (issue #122) - -v5.0.3 ------- - -* Fixed `printTo(String)` which wrote numbers instead of strings (issue #120) -* Fixed return type of `JsonArray::is()` and some others (issue #121) - -v5.0.2 ------- - -* Fixed segmentation fault in `parseObject(String)` and `parseArray(String)`, when the - `StaticJsonBuffer` is too small to hold a copy of the string -* Fixed Clang warning "register specifier is deprecated" (issue #102) -* Fixed GCC warning "declaration shadows a member" (issue #103) -* Fixed memory alignment, which made ESP8266 crash (issue #104) -* Fixed compilation on Visual Studio 2010 and 2012 (issue #107) - -v5.0.1 ------- - -* Fixed compilation with Arduino 1.0.6 (issue #99) - -v5.0.0 ------- - -* Added support of `String` class (issues #55, #56, #70, #77) -* Added `JsonBuffer::strdup()` to make a copy of a string (issues #10, #57) -* Implicitly call `strdup()` for `String` but not for `char*` (issues #84, #87) -* Added support of non standard JSON input (issue #44) -* Added support of comments in JSON input (issue #88) -* Added implicit cast between numerical types (issues #64, #69, #93) -* Added ability to read number values as string (issue #90) -* Redesigned `JsonVariant` to leverage converting constructors instead of assignment operators (issue #66) -* Switched to new the library layout (requires Arduino 1.0.6 or above) - -> ### BREAKING CHANGES :warning: -> -> - `JsonObject::add()` was renamed to `set()` -> - `JsonArray::at()` and `JsonObject::at()` were renamed to `get()` -> - Number of digits of floating point value are now set with `double_with_n_digits()` - -**Personal note about the `String` class**: -Support of the `String` class has been added to the library because many people use it in their programs. -However, you should not see this as an invitation to use the `String` class. -The `String` class is **bad** because it uses dynamic memory allocation. -Compared to static allocation, it compiles to a bigger, slower program, and is less predictable. -You certainly don't want that in an embedded environment! - -v4.6 ----- - -* Fixed segmentation fault in `DynamicJsonBuffer` when memory allocation fails (issue #92) - -v4.5 ----- - -* Fixed buffer overflow when input contains a backslash followed by a terminator (issue #81) - -**Upgrading is recommended** since previous versions contain a potential security risk. - -Special thanks to [Giancarlo Canales Barreto](https://github.com/gcanalesb) for finding this nasty bug. - -v4.4 ----- - -* Added `JsonArray::measureLength()` and `JsonObject::measureLength()` (issue #75) - -v4.3 ----- - -* Added `JsonArray::removeAt()` to remove an element of an array (issue #58) -* Fixed stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65) -* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68) - -v4.2 ----- - -* Switched back to old library layout (issues #39, #43 and #45) -* Removed global new operator overload (issue #40, #45 and #46) -* Added an example with EthernetServer - -v4.1 ----- - -* Added DynamicJsonBuffer (issue #19) - -v4.0 ----- - -* Unified parser and generator API (issue #23) -* Updated library layout, now requires Arduino 1.0.6 or newer - -> ### BREAKING CHANGES :warning: -> -> API changed significantly since v3, see [Migrating code to the new API](https://arduinojson.org/doc/migration/). - diff --git a/lib/ArduinoJson-5.13.4/LICENSE.md b/lib/ArduinoJson-5.13.4/LICENSE.md deleted file mode 100644 index f0c4b5ae7..000000000 --- a/lib/ArduinoJson-5.13.4/LICENSE.md +++ /dev/null @@ -1,10 +0,0 @@ -The MIT License (MIT) ---------------------- - -Copyright © 2014-2018 Benoit BLANCHON - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/lib/ArduinoJson-5.13.4/README.md b/lib/ArduinoJson-5.13.4/README.md deleted file mode 100644 index 8ddc698fc..000000000 --- a/lib/ArduinoJson-5.13.4/README.md +++ /dev/null @@ -1,110 +0,0 @@ -![ArduinoJson](banner.svg) - ---- - -[![Build status](https://ci.appveyor.com/api/projects/status/m7s53wav1l0abssg/branch/master?svg=true)](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/master) [![Build Status](https://travis-ci.org/bblanchon/ArduinoJson.svg?branch=master)](https://travis-ci.org/bblanchon/ArduinoJson) [![Coverage Status](https://img.shields.io/coveralls/bblanchon/ArduinoJson.svg)](https://coveralls.io/r/bblanchon/ArduinoJson?branch=master) [![Star this project](http://githubbadges.com/star.svg?user=bblanchon&repo=ArduinoJson&style=flat&color=fff&background=007ec6)](https://github.com/bblanchon/ArduinoJson) - -ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things). - -## Features - -* JSON decoding (comments are supported) -* JSON encoding (with optional indentation) -* Elegant API, easy to use -* Fixed memory allocation (zero malloc) -* No data duplication (zero copy) -* Portable (written in C++98, can be used in any C++ project) -* Self-contained (no external dependency) -* Small footprint -* Input and output streams -* [100% code coverage](https://coveralls.io/github/bblanchon/ArduinoJson) -* [Header-only library](https://en.wikipedia.org/wiki/Header-only) -* [MIT License](https://en.wikipedia.org/wiki/MIT_License) -* [Comprehensive documentation](https://arduinojson.org?utm_source=github&utm_medium=readme) - -## Compatibility - -ArduinoJson works on the following hardware: - -* Arduino boards: [Uno](https://www.arduino.cc/en/Main/ArduinoBoardUno), [Due](https://www.arduino.cc/en/Main/ArduinoBoardDue), [Mini](https://www.arduino.cc/en/Main/ArduinoBoardMini), [Micro](https://www.arduino.cc/en/Main/ArduinoBoardMicro), [Yun](https://www.arduino.cc/en/Main/ArduinoBoardYun)... -* Espressif chips: [ESP8266](https://en.wikipedia.org/wiki/ESP8266), [ESP32](https://en.wikipedia.org/wiki/ESP32) -* WeMos boards: [D1](https://wiki.wemos.cc/products:d1:d1), [D1 mini](https://wiki.wemos.cc/products:d1:d1_mini), ... -* RedBearLab boards: [BLE Nano](http://redbearlab.com/blenano/), [BLE Mini](http://redbearlab.com/blemini/), [WiFi Micro](https://redbear.cc/product/wifi/wifi-micro.html), [LOLIN32](https://wiki.wemos.cc/products:lolin32:lolin32)... -* [Teensy](https://www.pjrc.com/teensy/) boards -* Intel boards: Edison, Galileo... -* Particle boards: [Photon](https://www.particle.io/products/hardware/photon-wifi-dev-kit), [Electron](https://www.particle.io/products/hardware/electron-cellular-dev-kit)... -* Texas Instruments boards: [MSP430](http://www.ti.com/microcontrollers/msp430-ultra-low-power-mcus/overview/overview.html)... - -ArduinoJson compiles with zero warning on the following compilers, IDEs, and platforms: - -* [Arduino IDE](https://www.arduino.cc/en/Main/Software) -* [PlatformIO](http://platformio.org/) -* [Energia](http://energia.nu/) -* [Visual Micro](http://www.visualmicro.com/) -* [Atmel Studio](http://www.atmel.com/microsite/atmel-studio/) -* [IAR Embedded Workbench](https://www.iar.com/iar-embedded-workbench/) -* [Atollic TrueSTUDIO](https://atollic.com/truestudio/) -* [Keil uVision](http://www.keil.com/) -* [MPLAB X IDE](http://www.microchip.com/mplab/mplab-x-ide) -* [GCC](https://gcc.gnu.org/) -* [Clang](https://clang.llvm.org/) -* [Visual Studio](https://www.visualstudio.com/) - -## Quickstart - -### Deserialization - -Here is a program that parses a JSON document with ArduinoJson. - -```c++ -char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; - -StaticJsonBuffer<200> jsonBuffer; - -JsonObject& root = jsonBuffer.parseObject(json); - -const char* sensor = root["sensor"]; -long time = root["time"]; -double latitude = root["data"][0]; -double longitude = root["data"][1]; -``` - -See the [tutorial on arduinojson.org](https://arduinojson.org/doc/decoding/?utm_source=github&utm_medium=readme) - -### Serialization - -Here is a program that generates a JSON document with ArduinoJson: - -```c++ -StaticJsonBuffer<200> jsonBuffer; - -JsonObject& root = jsonBuffer.createObject(); -root["sensor"] = "gps"; -root["time"] = 1351824120; - -JsonArray& data = root.createNestedArray("data"); -data.add(48.756080); -data.add(2.302038); - -root.printTo(Serial); -// This prints: -// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} -``` - -See the [tutorial on arduinojson.org](https://arduinojson.org/doc/encoding/?utm_source=github&utm_medium=readme) - -## Documentation - -The documentation is available on [arduinojson.org](https://arduinojson.org/?utm_source=github&utm_medium=readme), here are some shortcuts: - -* The [Examples](https://arduinojson.org/example/?utm_source=github&utm_medium=readme) show how to use the library in various situations. -* The [API Reference](https://arduinojson.org/api/?utm_source=github&utm_medium=readme) contains the description of each class and function. -* The [FAQ](https://arduinojson.org/faq/?utm_source=github&utm_medium=readme) has the answer to virtually every question. -* The [ArduinoJson Assistant](https://arduinojson.org/assistant/?utm_source=github&utm_medium=readme) writes programs for you! - ---- - -Do you like this library? Please [star this project on GitHub](https://github.com/bblanchon/ArduinoJson/stargazers)! - -What? You don't like it but you *love* it? -We don't take donations anymore, but [we sell a book](https://arduinojson.org/book/?utm_source=github&utm_medium=readme), so you can help and learn at the same time! \ No newline at end of file diff --git a/lib/ArduinoJson-5.13.4/examples/JsonConfigFile/JsonConfigFile.ino b/lib/ArduinoJson-5.13.4/examples/JsonConfigFile/JsonConfigFile.ino deleted file mode 100644 index 2ccf7d673..000000000 --- a/lib/ArduinoJson-5.13.4/examples/JsonConfigFile/JsonConfigFile.ino +++ /dev/null @@ -1,144 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows how to store your project configuration in a file. -// It uses the SD library but can be easily modified for any other file-system. -// -// The file contains a JSON document with the following content: -// { -// "hostname": "examples.com", -// "port": 2731 -// } - -#include -#include -#include - -// Configuration that we'll store on disk -struct Config { - char hostname[64]; - int port; -}; - -const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames -Config config; // <- global configuration object - -// Loads the configuration from a file -void loadConfiguration(const char *filename, Config &config) { - // Open file for reading - File file = SD.open(filename); - - // Allocate the memory pool on the stack. - // Don't forget to change the capacity to match your JSON document. - // Use arduinojson.org/assistant to compute the capacity. - StaticJsonBuffer<512> jsonBuffer; - - // Parse the root object - JsonObject &root = jsonBuffer.parseObject(file); - - if (!root.success()) - Serial.println(F("Failed to read file, using default configuration")); - - // Copy values from the JsonObject to the Config - config.port = root["port"] | 2731; - strlcpy(config.hostname, // <- destination - root["hostname"] | "example.com", // <- source - sizeof(config.hostname)); // <- destination's capacity - - // Close the file (File's destructor doesn't close the file) - file.close(); -} - -// Saves the configuration to a file -void saveConfiguration(const char *filename, const Config &config) { - // Delete existing file, otherwise the configuration is appended to the file - SD.remove(filename); - - // Open file for writing - File file = SD.open(filename, FILE_WRITE); - if (!file) { - Serial.println(F("Failed to create file")); - return; - } - - // Allocate the memory pool on the stack - // Don't forget to change the capacity to match your JSON document. - // Use https://arduinojson.org/assistant/ to compute the capacity. - StaticJsonBuffer<256> jsonBuffer; - - // Parse the root object - JsonObject &root = jsonBuffer.createObject(); - - // Set the values - root["hostname"] = config.hostname; - root["port"] = config.port; - - // Serialize JSON to file - if (root.printTo(file) == 0) { - Serial.println(F("Failed to write to file")); - } - - // Close the file (File's destructor doesn't close the file) - file.close(); -} - -// Prints the content of a file to the Serial -void printFile(const char *filename) { - // Open file for reading - File file = SD.open(filename); - if (!file) { - Serial.println(F("Failed to read file")); - return; - } - - // Extract each characters by one by one - while (file.available()) { - Serial.print((char)file.read()); - } - Serial.println(); - - // Close the file (File's destructor doesn't close the file) - file.close(); -} - -void setup() { - // Initialize serial port - Serial.begin(9600); - while (!Serial) continue; - - // Initialize SD library - while (!SD.begin()) { - Serial.println(F("Failed to initialize SD library")); - delay(1000); - } - - // Should load default config if run for the first time - Serial.println(F("Loading configuration...")); - loadConfiguration(filename, config); - - // Create configuration file - Serial.println(F("Saving configuration...")); - saveConfiguration(filename, config); - - // Dump config file - Serial.println(F("Print config file...")); - printFile(filename); -} - -void loop() { - // not used in this example -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization or deserialization problem. -// -// The book "Mastering ArduinoJson" contains a case study of a project that has -// a complex configuration with nested members. -// Contrary to this example, the project in the book uses the SPIFFS filesystem. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/lib/ArduinoJson-5.13.4/examples/JsonGeneratorExample/JsonGeneratorExample.ino deleted file mode 100644 index 7b38227b3..000000000 --- a/lib/ArduinoJson-5.13.4/examples/JsonGeneratorExample/JsonGeneratorExample.ino +++ /dev/null @@ -1,81 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows how to generate a JSON document with ArduinoJson. - -#include - -void setup() { - // Initialize Serial port - Serial.begin(9600); - while (!Serial) continue; - - // Memory pool for JSON object tree. - // - // Inside the brackets, 200 is the size of the pool in bytes. - // Don't forget to change this value to match your JSON document. - // Use arduinojson.org/assistant to compute the capacity. - StaticJsonBuffer<200> jsonBuffer; - - // StaticJsonBuffer allocates memory on the stack, it can be - // replaced by DynamicJsonBuffer which allocates in the heap. - // - // DynamicJsonBuffer jsonBuffer(200); - - // Create the root of the object tree. - // - // It's a reference to the JsonObject, the actual bytes are inside the - // JsonBuffer with all the other nodes of the object tree. - // Memory is freed when jsonBuffer goes out of scope. - JsonObject& root = jsonBuffer.createObject(); - - // Add values in the object - // - // Most of the time, you can rely on the implicit casts. - // In other case, you can do root.set("time", 1351824120); - root["sensor"] = "gps"; - root["time"] = 1351824120; - - // Add a nested array. - // - // It's also possible to create the array separately and add it to the - // JsonObject but it's less efficient. - JsonArray& data = root.createNestedArray("data"); - data.add(48.756080); - data.add(2.302038); - - root.printTo(Serial); - // This prints: - // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} - - Serial.println(); - - root.prettyPrintTo(Serial); - // This prints: - // { - // "sensor": "gps", - // "time": 1351824120, - // "data": [ - // 48.756080, - // 2.302038 - // ] - // } -} - -void loop() { - // not used in this example -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// -// The book "Mastering ArduinoJson" contains a tutorial on serialization. -// It begins with a simple example, like the one above, and then adds more -// features like serializing directly to a file or an HTTP request. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/JsonHttpClient/JsonHttpClient.ino b/lib/ArduinoJson-5.13.4/examples/JsonHttpClient/JsonHttpClient.ino deleted file mode 100644 index 4ce1c20d1..000000000 --- a/lib/ArduinoJson-5.13.4/examples/JsonHttpClient/JsonHttpClient.ino +++ /dev/null @@ -1,112 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows how to parse a JSON document in an HTTP response. -// It uses the Ethernet library, but can be easily adapted for Wifi. -// -// It performs a GET resquest on arduinojson.org/example.json -// Here is the expected response: -// { -// "sensor": "gps", -// "time": 1351824120, -// "data": [ -// 48.756080, -// 2.302038 -// ] -// } - -#include -#include -#include - -void setup() { - // Initialize Serial port - Serial.begin(9600); - while (!Serial) continue; - - // Initialize Ethernet library - byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; - if (!Ethernet.begin(mac)) { - Serial.println(F("Failed to configure Ethernet")); - return; - } - delay(1000); - - Serial.println(F("Connecting...")); - - // Connect to HTTP server - EthernetClient client; - client.setTimeout(10000); - if (!client.connect("arduinojson.org", 80)) { - Serial.println(F("Connection failed")); - return; - } - - Serial.println(F("Connected!")); - - // Send HTTP request - client.println(F("GET /example.json HTTP/1.0")); - client.println(F("Host: arduinojson.org")); - client.println(F("Connection: close")); - if (client.println() == 0) { - Serial.println(F("Failed to send request")); - return; - } - - // Check HTTP status - char status[32] = {0}; - client.readBytesUntil('\r', status, sizeof(status)); - if (strcmp(status, "HTTP/1.1 200 OK") != 0) { - Serial.print(F("Unexpected response: ")); - Serial.println(status); - return; - } - - // Skip HTTP headers - char endOfHeaders[] = "\r\n\r\n"; - if (!client.find(endOfHeaders)) { - Serial.println(F("Invalid response")); - return; - } - - // Allocate JsonBuffer - // Use arduinojson.org/assistant to compute the capacity. - const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60; - DynamicJsonBuffer jsonBuffer(capacity); - - // Parse JSON object - JsonObject& root = jsonBuffer.parseObject(client); - if (!root.success()) { - Serial.println(F("Parsing failed!")); - return; - } - - // Extract values - Serial.println(F("Response:")); - Serial.println(root["sensor"].as()); - Serial.println(root["time"].as()); - Serial.println(root["data"][0].as()); - Serial.println(root["data"][1].as()); - - // Disconnect - client.stop(); -} - -void loop() { - // not used in this example -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// -// The book "Mastering ArduinoJson" contains a tutorial on deserialization -// showing how to parse the response from Yahoo Weather. In the last chapter, -// it shows how to parse the huge documents from OpenWeatherMap -// and Weather Underground. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/JsonParserExample/JsonParserExample.ino b/lib/ArduinoJson-5.13.4/examples/JsonParserExample/JsonParserExample.ino deleted file mode 100644 index 6c16211b5..000000000 --- a/lib/ArduinoJson-5.13.4/examples/JsonParserExample/JsonParserExample.ino +++ /dev/null @@ -1,78 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows how to deserialize a JSON document with ArduinoJson. - -#include - -void setup() { - // Initialize serial port - Serial.begin(9600); - while (!Serial) continue; - - // Memory pool for JSON object tree. - // - // Inside the brackets, 200 is the size of the pool in bytes. - // Don't forget to change this value to match your JSON document. - // Use arduinojson.org/assistant to compute the capacity. - StaticJsonBuffer<200> jsonBuffer; - - // StaticJsonBuffer allocates memory on the stack, it can be - // replaced by DynamicJsonBuffer which allocates in the heap. - // - // DynamicJsonBuffer jsonBuffer(200); - - // JSON input string. - // - // It's better to use a char[] as shown here. - // If you use a const char* or a String, ArduinoJson will - // have to make a copy of the input in the JsonBuffer. - char json[] = - "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; - - // Root of the object tree. - // - // It's a reference to the JsonObject, the actual bytes are inside the - // JsonBuffer with all the other nodes of the object tree. - // Memory is freed when jsonBuffer goes out of scope. - JsonObject& root = jsonBuffer.parseObject(json); - - // Test if parsing succeeds. - if (!root.success()) { - Serial.println("parseObject() failed"); - return; - } - - // Fetch values. - // - // Most of the time, you can rely on the implicit casts. - // In other case, you can do root["time"].as(); - const char* sensor = root["sensor"]; - long time = root["time"]; - double latitude = root["data"][0]; - double longitude = root["data"][1]; - - // Print values. - Serial.println(sensor); - Serial.println(time); - Serial.println(latitude, 6); - Serial.println(longitude, 6); -} - -void loop() { - // not used in this example -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// deserialization problem. -// -// The book "Mastering ArduinoJson" contains a tutorial on deserialization. -// It begins with a simple example, like the one above, and then adds more -// features like deserializing directly from a file or an HTTP request. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/JsonServer/JsonServer.ino b/lib/ArduinoJson-5.13.4/examples/JsonServer/JsonServer.ino deleted file mode 100644 index e693ae176..000000000 --- a/lib/ArduinoJson-5.13.4/examples/JsonServer/JsonServer.ino +++ /dev/null @@ -1,109 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows how to implement an HTTP server that sends JSON document -// in the responses. -// It uses the Ethernet library but can be easily adapted for Wifi. -// -// It sends the value of the analog and digital pins. -// The JSON document looks like the following: -// { -// "analog": [ 0, 1, 2, 3, 4, 5 ], -// "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ] -// } - -#include -#include -#include - -byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; -EthernetServer server(80); - -void setup() { - // Initialize serial port - Serial.begin(9600); - while (!Serial) continue; - - // Initialize Ethernet libary - if (!Ethernet.begin(mac)) { - Serial.println(F("Failed to initialize Ethernet library")); - return; - } - - // Start to listen - server.begin(); - - Serial.println(F("Server is ready.")); - Serial.print(F("Please connect to http://")); - Serial.println(Ethernet.localIP()); -} - -void loop() { - // Wait for an incomming connection - EthernetClient client = server.available(); - - // Do we have a client? - if (!client) return; - - Serial.println(F("New client")); - - // Read the request (we ignore the content in this example) - while (client.available()) client.read(); - - // Allocate JsonBuffer - // Use arduinojson.org/assistant to compute the capacity. - StaticJsonBuffer<500> jsonBuffer; - - // Create the root object - JsonObject& root = jsonBuffer.createObject(); - - // Create the "analog" array - JsonArray& analogValues = root.createNestedArray("analog"); - for (int pin = 0; pin < 6; pin++) { - // Read the analog input - int value = analogRead(pin); - - // Add the value at the end of the array - analogValues.add(value); - } - - // Create the "digital" array - JsonArray& digitalValues = root.createNestedArray("digital"); - for (int pin = 0; pin < 14; pin++) { - // Read the digital input - int value = digitalRead(pin); - - // Add the value at the end of the array - digitalValues.add(value); - } - - Serial.print(F("Sending: ")); - root.printTo(Serial); - Serial.println(); - - // Write response headers - client.println("HTTP/1.0 200 OK"); - client.println("Content-Type: application/json"); - client.println("Connection: close"); - client.println(); - - // Write JSON document - root.prettyPrintTo(client); - - // Disconnect - client.stop(); -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// -// The book "Mastering ArduinoJson" contains a tutorial on serialization. -// It begins with a simple example, then adds more features like serializing -// directly to a file or an HTTP client. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/JsonUdpBeacon/JsonUdpBeacon.ino b/lib/ArduinoJson-5.13.4/examples/JsonUdpBeacon/JsonUdpBeacon.ino deleted file mode 100644 index b2328a62d..000000000 --- a/lib/ArduinoJson-5.13.4/examples/JsonUdpBeacon/JsonUdpBeacon.ino +++ /dev/null @@ -1,101 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows how to send a JSON document to a UDP socket. -// At regular interval, it sends a UDP packet that contains the status of -// analog and digital pins. -// The JSON document looks like the following: -// { -// "analog": [ 0, 1, 2, 3, 4, 5 ], -// "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ] -// } -// -// If you want to test this program, you need to be able to receive the UDP -// packets. -// For example, you can run netcat on your computer -// $ ncat -ulp 8888 -// See https://nmap.org/ncat/ - -#include -#include -#include - -byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; -IPAddress remoteIp(192, 168, 0, 108); // <- EDIT!!!! -unsigned short remotePort = 8888; -unsigned short localPort = 8888; -EthernetUDP udp; - -void setup() { - // Initialize serial port - Serial.begin(9600); - while (!Serial) continue; - - // Initialize Ethernet libary - if (!Ethernet.begin(mac)) { - Serial.println(F("Failed to initialize Ethernet library")); - return; - } - - // Enable UDP - udp.begin(localPort); -} - -void loop() { - // Allocate JsonBuffer - // Use arduinojson.org/assistant to compute the capacity. - StaticJsonBuffer<500> jsonBuffer; - - // Create the root object - JsonObject& root = jsonBuffer.createObject(); - - // Create the "analog" array - JsonArray& analogValues = root.createNestedArray("analog"); - for (int pin = 0; pin < 6; pin++) { - // Read the analog input - int value = analogRead(pin); - - // Add the value at the end of the array - analogValues.add(value); - } - - // Create the "digital" array - JsonArray& digitalValues = root.createNestedArray("digital"); - for (int pin = 0; pin < 14; pin++) { - // Read the digital input - int value = digitalRead(pin); - - // Add the value at the end of the array - digitalValues.add(value); - } - - // Log - Serial.print(F("Sending to ")); - Serial.print(remoteIp); - Serial.print(F(" on port ")); - Serial.println(remotePort); - root.printTo(Serial); - - // Send UDP packet - udp.beginPacket(remoteIp, remotePort); - root.printTo(udp); - udp.println(); - udp.endPacket(); - - // Wait - delay(10000); -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any -// serialization problem. -// -// The book "Mastering ArduinoJson" contains a tutorial on serialization. -// It begins with a simple example, then adds more features like serializing -// directly to a file or any stream. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/ProgmemExample/ProgmemExample.ino b/lib/ArduinoJson-5.13.4/examples/ProgmemExample/ProgmemExample.ino deleted file mode 100644 index ddde8fd1d..000000000 --- a/lib/ArduinoJson-5.13.4/examples/ProgmemExample/ProgmemExample.ino +++ /dev/null @@ -1,70 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows the different ways you can use Flash strings with -// ArduinoJson. -// -// Use Flash strings sparingly, because ArduinoJson duplicates them in the -// JsonBuffer. Prefer plain old char*, as they are more efficient in term of -// code size, speed, and memory usage. - -#include - -void setup() { -#ifdef PROGMEM // <- check that Flash strings are supported - - DynamicJsonBuffer jsonBuffer; - - // You can use a Flash String as your JSON input. - // WARNING: the content of the Flash String will be duplicated in the - // JsonBuffer. - JsonObject& root = - jsonBuffer.parseObject(F("{\"sensor\":\"gps\",\"time\":1351824120," - "\"data\":[48.756080,2.302038]}")); - - // You can use a Flash String to get an element of a JsonObject - // No duplication is done. - long time = root[F("time")]; - - // You can use a Flash String to set an element of a JsonObject - // WARNING: the content of the Flash String will be duplicated in the - // JsonBuffer. - root[F("time")] = time; - - // You can set a Flash String to a JsonObject or JsonArray: - // WARNING: the content of the Flash String will be duplicated in the - // JsonBuffer. - root["sensor"] = F("gps"); - - // It works with RawJson too: - root["sensor"] = RawJson(F("\"gps\"")); - - // You can compare the content of a JsonVariant to a Flash String - if (root["sensor"] == F("gps")) { - // ... - } - -#else - -#warning PROGMEM is not supported on this platform - -#endif -} - -void loop() { - // not used in this example -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any memory -// problem. -// -// The book "Mastering ArduinoJson" contains a quick C++ course that explains -// how your microcontroller stores strings in memory. It also tells why you -// should not abuse Flash strings with ArduinoJson. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/examples/StringExample/StringExample.ino b/lib/ArduinoJson-5.13.4/examples/StringExample/StringExample.ino deleted file mode 100644 index fc7503d0e..000000000 --- a/lib/ArduinoJson-5.13.4/examples/StringExample/StringExample.ino +++ /dev/null @@ -1,74 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License -// -// This example shows the different ways you can use String with ArduinoJson. -// -// Use String objects sparingly, because ArduinoJson duplicates them in the -// JsonBuffer. Prefer plain old char[], as they are more efficient in term of -// code size, speed, and memory usage. - -#include - -void setup() { - DynamicJsonBuffer jsonBuffer; - - // You can use a String as your JSON input. - // WARNING: the content of the String will be duplicated in the JsonBuffer. - String input = - "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; - JsonObject& root = jsonBuffer.parseObject(input); - - // You can use a String to get an element of a JsonObject - // No duplication is done. - long time = root[String("time")]; - - // You can use a String to set an element of a JsonObject - // WARNING: the content of the String will be duplicated in the JsonBuffer. - root[String("time")] = time; - - // You can get a String from a JsonObject or JsonArray: - // No duplication is done, at least not in the JsonBuffer. - String sensor = root["sensor"]; - - // Unfortunately, the following doesn't work (issue #118): - // sensor = root["sensor"]; // <- error "ambiguous overload for 'operator='" - // As a workaround, you need to replace by: - sensor = root["sensor"].as(); - - // You can set a String to a JsonObject or JsonArray: - // WARNING: the content of the String will be duplicated in the JsonBuffer. - root["sensor"] = sensor; - - // It works with RawJson too: - root["sensor"] = RawJson(sensor); - - // You can also concatenate strings - // WARNING: the content of the String will be duplicated in the JsonBuffer. - root[String("sen") + "sor"] = String("gp") + "s"; - - // You can compare the content of a JsonObject with a String - if (root["sensor"] == sensor) { - // ... - } - - // Lastly, you can print the resulting JSON to a String - String output; - root.printTo(output); -} - -void loop() { - // not used in this example -} - -// See also -// -------- -// -// https://arduinojson.org/ contains the documentation for all the functions -// used above. It also includes an FAQ that will help you solve any problem. -// -// The book "Mastering ArduinoJson" contains a quick C++ course that explains -// how your microcontroller stores strings in memory. On several occasions, it -// shows how you can avoid String in your program. -// Learn more at https://arduinojson.org/book/ -// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤ diff --git a/lib/ArduinoJson-5.13.4/keywords.txt b/lib/ArduinoJson-5.13.4/keywords.txt deleted file mode 100644 index 833cddb73..000000000 --- a/lib/ArduinoJson-5.13.4/keywords.txt +++ /dev/null @@ -1,15 +0,0 @@ -JsonArray KEYWORD1 -JsonObject KEYWORD1 -JsonVariant KEYWORD1 -StaticJsonBuffer KEYWORD1 -DynamicJsonBuffer KEYWORD1 -add KEYWORD2 -createArray KEYWORD2 -createNestedArray KEYWORD2 -createNestedObject KEYWORD2 -createObject KEYWORD2 -parseArray KEYWORD2 -parseObject KEYWORD2 -prettyPrintTo KEYWORD2 -printTo KEYWORD2 -success KEYWORD2 diff --git a/lib/ArduinoJson-5.13.4/library.properties b/lib/ArduinoJson-5.13.4/library.properties deleted file mode 100644 index 0829de12e..000000000 --- a/lib/ArduinoJson-5.13.4/library.properties +++ /dev/null @@ -1,11 +0,0 @@ -name=ArduinoJson -version=5.13.4 -author=Benoit Blanchon -maintainer=Benoit Blanchon -sentence=An efficient and elegant JSON library for Arduino. -paragraph=ArduinoJson supports serialization, deserialization, fixed allocation, zero-copy, streams, and more. It is the most popular Arduino library on GitHub. Check out arduinojson.org for a comprehensive documentation. -category=Data Processing -url=https://arduinojson.org/?utm_source=meta&utm_medium=library.properties -architectures=* -repository=https://github.com/bblanchon/ArduinoJson.git -license=MIT diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson.h b/lib/ArduinoJson-5.13.4/src/ArduinoJson.h deleted file mode 100644 index 3782aeabc..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson.h +++ /dev/null @@ -1,17 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#ifdef __cplusplus - -#include "ArduinoJson.hpp" - -using namespace ArduinoJson; - -#else - -#error ArduinoJson requires a C++ compiler, please change file extension to .cc or .cpp - -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson.hpp deleted file mode 100644 index c493c06a9..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "ArduinoJson/version.hpp" - -#include "ArduinoJson/DynamicJsonBuffer.hpp" -#include "ArduinoJson/JsonArray.hpp" -#include "ArduinoJson/JsonObject.hpp" -#include "ArduinoJson/StaticJsonBuffer.hpp" - -#include "ArduinoJson/Deserialization/JsonParserImpl.hpp" -#include "ArduinoJson/JsonArrayImpl.hpp" -#include "ArduinoJson/JsonBufferImpl.hpp" -#include "ArduinoJson/JsonObjectImpl.hpp" -#include "ArduinoJson/JsonVariantImpl.hpp" -#include "ArduinoJson/Serialization/JsonSerializerImpl.hpp" diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Configuration.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Configuration.hpp deleted file mode 100644 index 82483adfa..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Configuration.hpp +++ /dev/null @@ -1,151 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -// Small or big machine? -#ifndef ARDUINOJSON_EMBEDDED_MODE -#if defined(ARDUINO) || defined(__IAR_SYSTEMS_ICC__) || defined(__XC) || \ - defined(__ARMCC_VERSION) -#define ARDUINOJSON_EMBEDDED_MODE 1 -#else -#define ARDUINOJSON_EMBEDDED_MODE 0 -#endif -#endif - -#if ARDUINOJSON_EMBEDDED_MODE - -// Store floats by default to reduce the memory usage (issue #134) -#ifndef ARDUINOJSON_USE_DOUBLE -#define ARDUINOJSON_USE_DOUBLE 0 -#endif - -// Store longs by default, because they usually match the size of a float. -#ifndef ARDUINOJSON_USE_LONG_LONG -#define ARDUINOJSON_USE_LONG_LONG 0 -#endif -#ifndef ARDUINOJSON_USE_INT64 -#define ARDUINOJSON_USE_INT64 0 -#endif - -// Embedded systems usually don't have std::string -#ifndef ARDUINOJSON_ENABLE_STD_STRING -#define ARDUINOJSON_ENABLE_STD_STRING 0 -#endif - -// Embedded systems usually don't have std::stream -#ifndef ARDUINOJSON_ENABLE_STD_STREAM -#define ARDUINOJSON_ENABLE_STD_STREAM 0 -#endif - -// Limit nesting as the stack is likely to be small -#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT -#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 10 -#endif - -#else // ARDUINOJSON_EMBEDDED_MODE - -// On a computer we have plenty of memory so we can use doubles -#ifndef ARDUINOJSON_USE_DOUBLE -#define ARDUINOJSON_USE_DOUBLE 1 -#endif - -// Use long long when available -#ifndef ARDUINOJSON_USE_LONG_LONG -#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800) -#define ARDUINOJSON_USE_LONG_LONG 1 -#else -#define ARDUINOJSON_USE_LONG_LONG 0 -#endif -#endif - -// Use _int64 on old versions of Visual Studio -#ifndef ARDUINOJSON_USE_INT64 -#if defined(_MSC_VER) && _MSC_VER <= 1700 -#define ARDUINOJSON_USE_INT64 1 -#else -#define ARDUINOJSON_USE_INT64 0 -#endif -#endif - -// On a computer, we can use std::string -#ifndef ARDUINOJSON_ENABLE_STD_STRING -#define ARDUINOJSON_ENABLE_STD_STRING 1 -#endif - -// On a computer, we can assume std::stream -#ifndef ARDUINOJSON_ENABLE_STD_STREAM -#define ARDUINOJSON_ENABLE_STD_STREAM 1 -#endif - -// On a computer, the stack is large so we can increase nesting limit -#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT -#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 50 -#endif - -#endif // ARDUINOJSON_EMBEDDED_MODE - -#ifdef ARDUINO - -// Enable support for Arduino String -#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING -#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 -#endif - -// Enable support for Arduino Stream -#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM -#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1 -#endif - -#else // ARDUINO - -// Disable support for Arduino String -#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING -#define ARDUINOJSON_ENABLE_ARDUINO_STRING 0 -#endif - -// Disable support for Arduino Stream -#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM -#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 0 -#endif - -#endif // ARDUINO - -#ifndef ARDUINOJSON_ENABLE_PROGMEM -#ifdef PROGMEM -#define ARDUINOJSON_ENABLE_PROGMEM 1 -#else -#define ARDUINOJSON_ENABLE_PROGMEM 0 -#endif -#endif - -#ifndef ARDUINOJSON_ENABLE_ALIGNMENT -#ifdef ARDUINO_ARCH_AVR -// alignment isn't needed for 8-bit AVR -#define ARDUINOJSON_ENABLE_ALIGNMENT 0 -#else -// but most processors need pointers to be align on word size -#define ARDUINOJSON_ENABLE_ALIGNMENT 1 -#endif -#endif - -// Enable deprecated functions by default -#ifndef ARDUINOJSON_ENABLE_DEPRECATED -#define ARDUINOJSON_ENABLE_DEPRECATED 1 -#endif - -// Control the exponentiation threshold for big numbers -// CAUTION: cannot be more that 1e9 !!!! -#ifndef ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD -#define ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD 1e7 -#endif - -// Control the exponentiation threshold for small numbers -#ifndef ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD -#define ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD 1e-5 -#endif - -#if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64 -#error ARDUINOJSON_USE_LONG_LONG and ARDUINOJSON_USE_INT64 cannot be set together -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/Encoding.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/Encoding.hpp deleted file mode 100644 index a0efa2c74..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/Encoding.hpp +++ /dev/null @@ -1,37 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -class Encoding { - public: - // Optimized for code size on a 8-bit AVR - static char escapeChar(char c) { - const char *p = escapeTable(false); - while (p[0] && p[1] != c) { - p += 2; - } - return p[0]; - } - - // Optimized for code size on a 8-bit AVR - static char unescapeChar(char c) { - const char *p = escapeTable(true); - for (;;) { - if (p[0] == '\0') return c; - if (p[0] == c) return p[1]; - p += 2; - } - } - - private: - static const char *escapeTable(bool excludeIdenticals) { - return &"\"\"\\\\b\bf\fn\nr\rt\t"[excludeIdenticals ? 4 : 0]; - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonBufferAllocated.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonBufferAllocated.hpp deleted file mode 100644 index 443aae4df..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonBufferAllocated.hpp +++ /dev/null @@ -1,22 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../JsonBuffer.hpp" - -namespace ArduinoJson { -namespace Internals { - -class JsonBufferAllocated { - public: - void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() { - if (!jsonBuffer) return NULL; - return jsonBuffer->alloc(n); - } - - void operator delete(void *, JsonBuffer *)throw(); -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonFloat.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonFloat.hpp deleted file mode 100644 index 0ed42140f..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonFloat.hpp +++ /dev/null @@ -1,18 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" - -namespace ArduinoJson { -namespace Internals { - -#if ARDUINOJSON_USE_DOUBLE -typedef double JsonFloat; -#else -typedef float JsonFloat; -#endif -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonInteger.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonInteger.hpp deleted file mode 100644 index c8ddd00b4..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonInteger.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" - -namespace ArduinoJson { -namespace Internals { - -#if ARDUINOJSON_USE_LONG_LONG -typedef long long JsonInteger; -typedef unsigned long long JsonUInt; -#elif ARDUINOJSON_USE_INT64 -typedef __int64 JsonInteger; -typedef unsigned _int64 JsonUInt; -#else -typedef long JsonInteger; -typedef unsigned long JsonUInt; -#endif -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantAs.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantAs.hpp deleted file mode 100644 index 8f202c5eb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantAs.hpp +++ /dev/null @@ -1,42 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A metafunction that returns the type of the value returned by -// JsonVariant::as() -template -struct JsonVariantAs { - typedef T type; -}; - -template <> -struct JsonVariantAs { - typedef const char* type; -}; - -template <> -struct JsonVariantAs { - typedef JsonArray& type; -}; - -template <> -struct JsonVariantAs { - typedef const JsonArray& type; -}; - -template <> -struct JsonVariantAs { - typedef JsonObject& type; -}; - -template <> -struct JsonVariantAs { - typedef const JsonObject& type; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantContent.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantContent.hpp deleted file mode 100644 index c525a6060..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantContent.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonFloat.hpp" -#include "JsonInteger.hpp" - -namespace ArduinoJson { - -// Forward declarations -class JsonArray; -class JsonObject; - -namespace Internals { -// A union that defines the actual content of a JsonVariant. -// The enum JsonVariantType determines which member is in use. -union JsonVariantContent { - JsonFloat asFloat; // used for double and float - JsonUInt asInteger; // used for bool, char, short, int and longs - const char* asString; // asString can be null - JsonArray* asArray; // asArray cannot be null - JsonObject* asObject; // asObject cannot be null -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantDefault.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantDefault.hpp deleted file mode 100644 index 57ecc83ee..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantDefault.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -template -struct JsonVariantDefault { - static T get() { - return T(); - } -}; - -template -struct JsonVariantDefault : JsonVariantDefault {}; - -template -struct JsonVariantDefault : JsonVariantDefault {}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantType.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantType.hpp deleted file mode 100644 index 21f890e52..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/JsonVariantType.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -class JsonArray; -class JsonObject; - -namespace Internals { - -// Enumerated type to know the current type of a JsonVariant. -// The value determines which member of JsonVariantContent is used. -enum JsonVariantType { - JSON_UNDEFINED, // JsonVariant has not been initialized - JSON_UNPARSED, // JsonVariant contains an unparsed string - JSON_STRING, // JsonVariant stores a const char* - JSON_BOOLEAN, // JsonVariant stores a bool - JSON_POSITIVE_INTEGER, // JsonVariant stores an JsonUInt - JSON_NEGATIVE_INTEGER, // JsonVariant stores an JsonUInt that must be negated - JSON_ARRAY, // JsonVariant stores a pointer to a JsonArray - JSON_OBJECT, // JsonVariant stores a pointer to a JsonObject - JSON_FLOAT // JsonVariant stores a JsonFloat -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/List.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/List.hpp deleted file mode 100644 index 506308cc3..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/List.hpp +++ /dev/null @@ -1,94 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../JsonBuffer.hpp" -#include "ListConstIterator.hpp" -#include "ListIterator.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A singly linked list of T. -// The linked list is composed of ListNode. -// It is derived by JsonArray and JsonObject -template -class List { - public: - typedef T value_type; - typedef ListNode node_type; - typedef ListIterator iterator; - typedef ListConstIterator const_iterator; - - // Creates an empty List attached to a JsonBuffer. - // The JsonBuffer allows to allocate new nodes. - // When buffer is NULL, the List is not able to grow and success() returns - // false. This is used to identify bad memory allocations and parsing - // failures. - explicit List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} - - // Returns true if the object is valid - // Would return false in the following situation: - // - the memory allocation failed (StaticJsonBuffer was too small) - // - the JSON parsing failed - bool success() const { - return _buffer != NULL; - } - - // Returns the numbers of elements in the list. - // For a JsonObject, it would return the number of key-value pairs - size_t size() const { - size_t nodeCount = 0; - for (node_type *node = _firstNode; node; node = node->next) nodeCount++; - return nodeCount; - } - - iterator add() { - node_type *newNode = new (_buffer) node_type(); - - if (_firstNode) { - node_type *lastNode = _firstNode; - while (lastNode->next) lastNode = lastNode->next; - lastNode->next = newNode; - } else { - _firstNode = newNode; - } - - return iterator(newNode); - } - - iterator begin() { - return iterator(_firstNode); - } - iterator end() { - return iterator(NULL); - } - - const_iterator begin() const { - return const_iterator(_firstNode); - } - const_iterator end() const { - return const_iterator(NULL); - } - - void remove(iterator it) { - node_type *nodeToRemove = it._node; - if (!nodeToRemove) return; - if (nodeToRemove == _firstNode) { - _firstNode = nodeToRemove->next; - } else { - for (node_type *node = _firstNode; node; node = node->next) - if (node->next == nodeToRemove) node->next = nodeToRemove->next; - } - } - - protected: - JsonBuffer *_buffer; - - private: - node_type *_firstNode; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListConstIterator.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListConstIterator.hpp deleted file mode 100644 index a6af685e5..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListConstIterator.hpp +++ /dev/null @@ -1,50 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "ListNode.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A read-only forward itertor for List -template -class ListConstIterator { - public: - explicit ListConstIterator(const ListNode *node = NULL) : _node(node) {} - - const T &operator*() const { - return _node->content; - } - const T *operator->() { - return &_node->content; - } - - bool operator==(const ListConstIterator &other) const { - return _node == other._node; - } - - bool operator!=(const ListConstIterator &other) const { - return _node != other._node; - } - - ListConstIterator &operator++() { - if (_node) _node = _node->next; - return *this; - } - - ListConstIterator &operator+=(size_t distance) { - while (_node && distance) { - _node = _node->next; - --distance; - } - return *this; - } - - private: - const ListNode *_node; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListIterator.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListIterator.hpp deleted file mode 100644 index 01fa287f7..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListIterator.hpp +++ /dev/null @@ -1,60 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "ListConstIterator.hpp" -#include "ListNode.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -class List; - -// A read-write forward iterator for List -template -class ListIterator { - friend class List; - - public: - explicit ListIterator(ListNode *node = NULL) : _node(node) {} - - T &operator*() const { - return _node->content; - } - T *operator->() { - return &_node->content; - } - - bool operator==(const ListIterator &other) const { - return _node == other._node; - } - - bool operator!=(const ListIterator &other) const { - return _node != other._node; - } - - ListIterator &operator++() { - if (_node) _node = _node->next; - return *this; - } - - ListIterator &operator+=(size_t distance) { - while (_node && distance) { - _node = _node->next; - --distance; - } - return *this; - } - - operator ListConstIterator() const { - return ListConstIterator(_node); - } - - private: - ListNode *_node; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListNode.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListNode.hpp deleted file mode 100644 index c0907120e..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ListNode.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include // for NULL - -#include "JsonBufferAllocated.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A node for a singly-linked list. -// Used by List and its iterators. -template -struct ListNode : public Internals::JsonBufferAllocated { - ListNode() throw() : next(NULL) {} - - ListNode *next; - T content; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/NonCopyable.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/NonCopyable.hpp deleted file mode 100644 index 73f3d8edb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/NonCopyable.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A type that cannot be copied -class NonCopyable { - protected: - NonCopyable() {} - - private: - // copy constructor is private - NonCopyable(const NonCopyable&); - - // copy operator is private - NonCopyable& operator=(const NonCopyable&); -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ReferenceType.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ReferenceType.hpp deleted file mode 100644 index 1e491172f..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ReferenceType.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A type that is meant to be used by reference only (JsonArray and JsonObject) -class ReferenceType { - public: - bool operator==(const ReferenceType& other) const { - // two JsonArray are equal if they are the same instance - // (we don't compare the content) - return this == &other; - } - - bool operator!=(const ReferenceType& other) const { - return this != &other; - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ValueSaver.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ValueSaver.hpp deleted file mode 100644 index 9750f1ac5..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Data/ValueSaver.hpp +++ /dev/null @@ -1,52 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../JsonBuffer.hpp" -#include "../JsonVariant.hpp" -#include "../StringTraits/StringTraits.hpp" -#include "../TypeTraits/EnableIf.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -struct ValueSaver { - template - static bool save(JsonBuffer*, Destination& destination, Source source) { - destination = source; - return true; - } -}; - -template -struct ValueSaver< - Source, typename EnableIf::should_duplicate>::type> { - template - static bool save(JsonBuffer* buffer, Destination& dest, Source source) { - if (!StringTraits::is_null(source)) { - typename StringTraits::duplicate_t dup = - StringTraits::duplicate(source, buffer); - if (!dup) return false; - dest = dup; - } else { - dest = reinterpret_cast(0); - } - return true; - } -}; - -// const char*, const signed char*, const unsigned char* -template -struct ValueSaver< - Char*, typename EnableIf::should_duplicate>::type> { - template - static bool save(JsonBuffer*, Destination& dest, Char* source) { - dest = reinterpret_cast(source); - return true; - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/Comments.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/Comments.hpp deleted file mode 100644 index c2c48ebcc..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/Comments.hpp +++ /dev/null @@ -1,61 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { -template -void skipSpacesAndComments(TInput& input) { - for (;;) { - switch (input.current()) { - // spaces - case ' ': - case '\t': - case '\r': - case '\n': - input.move(); - continue; - - // comments - case '/': - switch (input.next()) { - // C-style block comment - case '*': - input.move(); // skip '/' - // no need to skip '*' - for (;;) { - input.move(); - if (input.current() == '\0') return; - if (input.current() == '*' && input.next() == '/') { - input.move(); // skip '*' - input.move(); // skip '/' - break; - } - } - break; - - // C++-style line comment - case '/': - // not need to skip "//" - for (;;) { - input.move(); - if (input.current() == '\0') return; - if (input.current() == '\n') break; - } - break; - - // not a comment, just a '/' - default: - return; - } - break; - - default: - return; - } - } -} -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/JsonParser.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/JsonParser.hpp deleted file mode 100644 index 4cbaf454c..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/JsonParser.hpp +++ /dev/null @@ -1,102 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../JsonBuffer.hpp" -#include "../JsonVariant.hpp" -#include "../TypeTraits/IsConst.hpp" -#include "StringWriter.hpp" - -namespace ArduinoJson { -namespace Internals { - -// Parse JSON string to create JsonArrays and JsonObjects -// This internal class is not indended to be used directly. -// Instead, use JsonBuffer.parseArray() or .parseObject() -template -class JsonParser { - public: - JsonParser(JsonBuffer *buffer, TReader reader, TWriter writer, - uint8_t nestingLimit) - : _buffer(buffer), - _reader(reader), - _writer(writer), - _nestingLimit(nestingLimit) {} - - JsonArray &parseArray(); - JsonObject &parseObject(); - - JsonVariant parseVariant() { - JsonVariant result; - parseAnythingTo(&result); - return result; - } - - private: - JsonParser &operator=(const JsonParser &); // non-copiable - - static bool eat(TReader &, char charToSkip); - FORCE_INLINE bool eat(char charToSkip) { - return eat(_reader, charToSkip); - } - - const char *parseString(); - bool parseAnythingTo(JsonVariant *destination); - - inline bool parseArrayTo(JsonVariant *destination); - inline bool parseObjectTo(JsonVariant *destination); - inline bool parseStringTo(JsonVariant *destination); - - static inline bool isBetween(char c, char min, char max) { - return min <= c && c <= max; - } - - static inline bool canBeInNonQuotedString(char c) { - return isBetween(c, '0', '9') || isBetween(c, '_', 'z') || - isBetween(c, 'A', 'Z') || c == '+' || c == '-' || c == '.'; - } - - static inline bool isQuote(char c) { - return c == '\'' || c == '\"'; - } - - JsonBuffer *_buffer; - TReader _reader; - TWriter _writer; - uint8_t _nestingLimit; -}; - -template -struct JsonParserBuilder { - typedef typename StringTraits::Reader InputReader; - typedef JsonParser TParser; - - static TParser makeParser(TJsonBuffer *buffer, TString &json, - uint8_t nestingLimit) { - return TParser(buffer, InputReader(json), *buffer, nestingLimit); - } -}; - -template -struct JsonParserBuilder::value>::type> { - typedef typename StringTraits::Reader TReader; - typedef StringWriter TWriter; - typedef JsonParser TParser; - - static TParser makeParser(TJsonBuffer *buffer, TChar *json, - uint8_t nestingLimit) { - return TParser(buffer, TReader(json), TWriter(json), nestingLimit); - } -}; - -template -inline typename JsonParserBuilder::TParser makeParser( - TJsonBuffer *buffer, TString &json, uint8_t nestingLimit) { - return JsonParserBuilder::makeParser(buffer, json, - nestingLimit); -} -} // namespace Internals -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/JsonParserImpl.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/JsonParserImpl.hpp deleted file mode 100644 index 504267355..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/JsonParserImpl.hpp +++ /dev/null @@ -1,189 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Comments.hpp" -#include "JsonParser.hpp" - -template -inline bool ArduinoJson::Internals::JsonParser::eat( - TReader &reader, char charToSkip) { - skipSpacesAndComments(reader); - if (reader.current() != charToSkip) return false; - reader.move(); - return true; -} - -template -inline bool -ArduinoJson::Internals::JsonParser::parseAnythingTo( - JsonVariant *destination) { - skipSpacesAndComments(_reader); - - switch (_reader.current()) { - case '[': - return parseArrayTo(destination); - - case '{': - return parseObjectTo(destination); - - default: - return parseStringTo(destination); - } -} - -template -inline ArduinoJson::JsonArray & -ArduinoJson::Internals::JsonParser::parseArray() { - if (_nestingLimit == 0) return JsonArray::invalid(); - _nestingLimit--; - - // Create an empty array - JsonArray &array = _buffer->createArray(); - - // Check opening braket - if (!eat('[')) goto ERROR_MISSING_BRACKET; - if (eat(']')) goto SUCCESS_EMPTY_ARRAY; - - // Read each value - for (;;) { - // 1 - Parse value - JsonVariant value; - if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE; - if (!array.add(value)) goto ERROR_NO_MEMORY; - - // 2 - More values? - if (eat(']')) goto SUCCES_NON_EMPTY_ARRAY; - if (!eat(',')) goto ERROR_MISSING_COMMA; - } - -SUCCESS_EMPTY_ARRAY: -SUCCES_NON_EMPTY_ARRAY: - _nestingLimit++; - return array; - -ERROR_INVALID_VALUE: -ERROR_MISSING_BRACKET: -ERROR_MISSING_COMMA: -ERROR_NO_MEMORY: - return JsonArray::invalid(); -} - -template -inline bool ArduinoJson::Internals::JsonParser::parseArrayTo( - JsonVariant *destination) { - JsonArray &array = parseArray(); - if (!array.success()) return false; - - *destination = array; - return true; -} - -template -inline ArduinoJson::JsonObject & -ArduinoJson::Internals::JsonParser::parseObject() { - if (_nestingLimit == 0) return JsonObject::invalid(); - _nestingLimit--; - - // Create an empty object - JsonObject &object = _buffer->createObject(); - - // Check opening brace - if (!eat('{')) goto ERROR_MISSING_BRACE; - if (eat('}')) goto SUCCESS_EMPTY_OBJECT; - - // Read each key value pair - for (;;) { - // 1 - Parse key - const char *key = parseString(); - if (!key) goto ERROR_INVALID_KEY; - if (!eat(':')) goto ERROR_MISSING_COLON; - - // 2 - Parse value - JsonVariant value; - if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE; - if (!object.set(key, value)) goto ERROR_NO_MEMORY; - - // 3 - More keys/values? - if (eat('}')) goto SUCCESS_NON_EMPTY_OBJECT; - if (!eat(',')) goto ERROR_MISSING_COMMA; - } - -SUCCESS_EMPTY_OBJECT: -SUCCESS_NON_EMPTY_OBJECT: - _nestingLimit++; - return object; - -ERROR_INVALID_KEY: -ERROR_INVALID_VALUE: -ERROR_MISSING_BRACE: -ERROR_MISSING_COLON: -ERROR_MISSING_COMMA: -ERROR_NO_MEMORY: - return JsonObject::invalid(); -} - -template -inline bool ArduinoJson::Internals::JsonParser::parseObjectTo( - JsonVariant *destination) { - JsonObject &object = parseObject(); - if (!object.success()) return false; - - *destination = object; - return true; -} - -template -inline const char * -ArduinoJson::Internals::JsonParser::parseString() { - typename RemoveReference::type::String str = _writer.startString(); - - skipSpacesAndComments(_reader); - char c = _reader.current(); - - if (isQuote(c)) { // quotes - _reader.move(); - char stopChar = c; - for (;;) { - c = _reader.current(); - if (c == '\0') break; - _reader.move(); - - if (c == stopChar) break; - - if (c == '\\') { - // replace char - c = Encoding::unescapeChar(_reader.current()); - if (c == '\0') break; - _reader.move(); - } - - str.append(c); - } - } else { // no quotes - for (;;) { - if (!canBeInNonQuotedString(c)) break; - _reader.move(); - str.append(c); - c = _reader.current(); - } - } - - return str.c_str(); -} - -template -inline bool ArduinoJson::Internals::JsonParser::parseStringTo( - JsonVariant *destination) { - bool hasQuotes = isQuote(_reader.current()); - const char *value = parseString(); - if (value == NULL) return false; - if (hasQuotes) { - *destination = value; - } else { - *destination = RawJson(value); - } - return true; -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/StringWriter.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/StringWriter.hpp deleted file mode 100644 index fd5507ea5..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Deserialization/StringWriter.hpp +++ /dev/null @@ -1,41 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -template -class StringWriter { - public: - class String { - public: - String(TChar** ptr) : _writePtr(ptr), _startPtr(*ptr) {} - - void append(char c) { - *(*_writePtr)++ = TChar(c); - } - - const char* c_str() const { - *(*_writePtr)++ = 0; - return reinterpret_cast(_startPtr); - } - - private: - TChar** _writePtr; - TChar* _startPtr; - }; - - StringWriter(TChar* buffer) : _ptr(buffer) {} - - String startString() { - return String(&_ptr); - } - - private: - TChar* _ptr; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/DynamicJsonBuffer.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/DynamicJsonBuffer.hpp deleted file mode 100644 index bdbd5dd90..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/DynamicJsonBuffer.hpp +++ /dev/null @@ -1,170 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonBufferBase.hpp" - -#include - -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wnon-virtual-dtor" -#elif defined(__GNUC__) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -#pragma GCC diagnostic push -#endif -#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" -#endif - -namespace ArduinoJson { -namespace Internals { -class DefaultAllocator { - public: - void* allocate(size_t size) { - return malloc(size); - } - void deallocate(void* pointer) { - free(pointer); - } -}; - -template -class DynamicJsonBufferBase - : public JsonBufferBase > { - struct Block; - struct EmptyBlock { - Block* next; - size_t capacity; - size_t size; - }; - struct Block : EmptyBlock { - uint8_t data[1]; - }; - - public: - enum { EmptyBlockSize = sizeof(EmptyBlock) }; - - DynamicJsonBufferBase(size_t initialSize = 256) - : _head(NULL), _nextBlockCapacity(initialSize) {} - - ~DynamicJsonBufferBase() { - clear(); - } - - // Gets the number of bytes occupied in the buffer - size_t size() const { - size_t total = 0; - for (const Block* b = _head; b; b = b->next) total += b->size; - return total; - } - - // Allocates the specified amount of bytes in the buffer - virtual void* alloc(size_t bytes) { - alignNextAlloc(); - return canAllocInHead(bytes) ? allocInHead(bytes) : allocInNewBlock(bytes); - } - - // Resets the buffer. - // USE WITH CAUTION: this invalidates all previously allocated data - void clear() { - Block* currentBlock = _head; - while (currentBlock != NULL) { - _nextBlockCapacity = currentBlock->capacity; - Block* nextBlock = currentBlock->next; - _allocator.deallocate(currentBlock); - currentBlock = nextBlock; - } - _head = 0; - } - - class String { - public: - String(DynamicJsonBufferBase* parent) - : _parent(parent), _start(NULL), _length(0) {} - - void append(char c) { - if (_parent->canAllocInHead(1)) { - char* end = static_cast(_parent->allocInHead(1)); - *end = c; - if (_length == 0) _start = end; - } else { - char* newStart = - static_cast(_parent->allocInNewBlock(_length + 1)); - if (_start && newStart) memcpy(newStart, _start, _length); - if (newStart) newStart[_length] = c; - _start = newStart; - } - _length++; - } - - const char* c_str() { - append(0); - return _start; - } - - private: - DynamicJsonBufferBase* _parent; - char* _start; - size_t _length; - }; - - String startString() { - return String(this); - } - - private: - void alignNextAlloc() { - if (_head) _head->size = this->round_size_up(_head->size); - } - - bool canAllocInHead(size_t bytes) const { - return _head != NULL && _head->size + bytes <= _head->capacity; - } - - void* allocInHead(size_t bytes) { - void* p = _head->data + _head->size; - _head->size += bytes; - return p; - } - - void* allocInNewBlock(size_t bytes) { - size_t capacity = _nextBlockCapacity; - if (bytes > capacity) capacity = bytes; - if (!addNewBlock(capacity)) return NULL; - _nextBlockCapacity *= 2; - return allocInHead(bytes); - } - - bool addNewBlock(size_t capacity) { - size_t bytes = EmptyBlockSize + capacity; - Block* block = static_cast(_allocator.allocate(bytes)); - if (block == NULL) return false; - block->capacity = capacity; - block->size = 0; - block->next = _head; - _head = block; - return true; - } - - TAllocator _allocator; - Block* _head; - size_t _nextBlockCapacity; -}; -} - -#if defined(__clang__) -#pragma clang diagnostic pop -#elif defined(__GNUC__) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -#pragma GCC diagnostic pop -#endif -#endif - -// Implements a JsonBuffer with dynamic memory allocation. -// You are strongly encouraged to consider using StaticJsonBuffer which is much -// more suitable for embedded systems. -typedef Internals::DynamicJsonBufferBase - DynamicJsonBuffer; -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArray.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArray.hpp deleted file mode 100644 index 2acd2a1a5..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArray.hpp +++ /dev/null @@ -1,227 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Data/JsonBufferAllocated.hpp" -#include "Data/List.hpp" -#include "Data/ReferenceType.hpp" -#include "Data/ValueSaver.hpp" -#include "JsonVariant.hpp" -#include "Serialization/JsonPrintable.hpp" -#include "StringTraits/StringTraits.hpp" -#include "TypeTraits/EnableIf.hpp" -#include "TypeTraits/IsArray.hpp" -#include "TypeTraits/IsFloatingPoint.hpp" -#include "TypeTraits/IsSame.hpp" - -// Returns the size (in bytes) of an array with n elements. -// Can be very handy to determine the size of a StaticJsonBuffer. -#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ - (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type)) - -namespace ArduinoJson { - -// Forward declarations -class JsonObject; -class JsonBuffer; -namespace Internals { -class JsonArraySubscript; -} - -// An array of JsonVariant. -// -// The constructor is private, instances must be created via -// JsonBuffer::createArray() or JsonBuffer::parseArray(). -// A JsonArray can be serialized to a JSON string via JsonArray::printTo(). -// It can also be deserialized from a JSON string via JsonBuffer::parseArray(). -class JsonArray : public Internals::JsonPrintable, - public Internals::ReferenceType, - public Internals::NonCopyable, - public Internals::List, - public Internals::JsonBufferAllocated { - public: - // Create an empty JsonArray attached to the specified JsonBuffer. - // You should not call this constructor directly. - // Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray(). - explicit JsonArray(JsonBuffer *buffer) throw() - : Internals::List(buffer) {} - - // Gets the value at the specified index - const Internals::JsonArraySubscript operator[](size_t index) const; - - // Gets or sets the value at specified index - Internals::JsonArraySubscript operator[](size_t index); - - // Adds the specified value at the end of the array. - // - // bool add(TValue); - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool add(const T &value) { - return add_impl(value); - } - // - // bool add(TValue); - // TValue = char*, const char*, const FlashStringHelper* - template - bool add(T *value) { - return add_impl(value); - } - // - // bool add(TValue value, uint8_t decimals); - // TValue = float, double - template - DEPRECATED("Second argument is not supported anymore") - bool add(T value, uint8_t) { - return add_impl(JsonVariant(value)); - } - - // Sets the value at specified index. - // - // bool add(size_t index, const TValue&); - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool set(size_t index, const T &value) { - return set_impl(index, value); - } - // - // bool add(size_t index, TValue); - // TValue = char*, const char*, const FlashStringHelper* - template - bool set(size_t index, T *value) { - return set_impl(index, value); - } - // - // bool set(size_t index, TValue value, uint8_t decimals); - // TValue = float, double - template - typename Internals::EnableIf::value, bool>::type - set(size_t index, T value, uint8_t decimals) { - return set_impl(index, JsonVariant(value, decimals)); - } - - // Gets the value at the specified index. - template - typename Internals::JsonVariantAs::type get(size_t index) const { - const_iterator it = begin() += index; - return it != end() ? it->as() : Internals::JsonVariantDefault::get(); - } - - // Check the type of the value at specified index. - template - bool is(size_t index) const { - const_iterator it = begin() += index; - return it != end() ? it->is() : false; - } - - // Creates a JsonArray and adds a reference at the end of the array. - // It's a shortcut for JsonBuffer::createArray() and JsonArray::add() - JsonArray &createNestedArray(); - - // Creates a JsonObject and adds a reference at the end of the array. - // It's a shortcut for JsonBuffer::createObject() and JsonArray::add() - JsonObject &createNestedObject(); - - // Removes element at specified index. - void remove(size_t index) { - remove(begin() += index); - } - using Internals::List::remove; - - // Returns a reference an invalid JsonArray. - // This object is meant to replace a NULL pointer. - // This is used when memory allocation or JSON parsing fail. - static JsonArray &invalid() { - static JsonArray instance(NULL); - return instance; - } - - // Imports a 1D array - template - bool copyFrom(T (&array)[N]) { - return copyFrom(array, N); - } - - // Imports a 1D array - template - bool copyFrom(T *array, size_t len) { - bool ok = true; - for (size_t i = 0; i < len; i++) { - ok &= add(array[i]); - } - return ok; - } - - // Imports a 2D array - template - bool copyFrom(T (&array)[N1][N2]) { - bool ok = true; - for (size_t i = 0; i < N1; i++) { - JsonArray &nestedArray = createNestedArray(); - for (size_t j = 0; j < N2; j++) { - ok &= nestedArray.add(array[i][j]); - } - } - return ok; - } - - // Exports a 1D array - template - size_t copyTo(T (&array)[N]) const { - return copyTo(array, N); - } - - // Exports a 1D array - template - size_t copyTo(T *array, size_t len) const { - size_t i = 0; - for (const_iterator it = begin(); it != end() && i < len; ++it) - array[i++] = *it; - return i; - } - - // Exports a 2D array - template - void copyTo(T (&array)[N1][N2]) const { - size_t i = 0; - for (const_iterator it = begin(); it != end() && i < N1; ++it) { - it->as().copyTo(array[i++]); - } - } - -#if ARDUINOJSON_ENABLE_DEPRECATED - DEPRECATED("use remove() instead") - FORCE_INLINE void removeAt(size_t index) { - return remove(index); - } -#endif - - private: - template - bool set_impl(size_t index, TValueRef value) { - iterator it = begin() += index; - if (it == end()) return false; - return Internals::ValueSaver::save(_buffer, *it, value); - } - - template - bool add_impl(TValueRef value) { - iterator it = Internals::List::add(); - if (it == end()) return false; - return Internals::ValueSaver::save(_buffer, *it, value); - } -}; - -namespace Internals { -template <> -struct JsonVariantDefault { - static JsonArray &get() { - return JsonArray::invalid(); - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArrayImpl.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArrayImpl.hpp deleted file mode 100644 index 924b7ea7a..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArrayImpl.hpp +++ /dev/null @@ -1,26 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonArray.hpp" -#include "JsonArraySubscript.hpp" -#include "JsonObject.hpp" - -namespace ArduinoJson { - -inline JsonArray &JsonArray::createNestedArray() { - if (!_buffer) return JsonArray::invalid(); - JsonArray &array = _buffer->createArray(); - add(array); - return array; -} - -inline JsonObject &JsonArray::createNestedObject() { - if (!_buffer) return JsonObject::invalid(); - JsonObject &object = _buffer->createObject(); - add(object); - return object; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArraySubscript.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArraySubscript.hpp deleted file mode 100644 index afb4dc1ec..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonArraySubscript.hpp +++ /dev/null @@ -1,122 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Configuration.hpp" -#include "JsonVariantBase.hpp" - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable : 4522) -#endif - -namespace ArduinoJson { -namespace Internals { -class JsonArraySubscript : public JsonVariantBase { - public: - FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index) - : _array(array), _index(index) {} - - FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) { - _array.set(_index, src); - return *this; - } - - // Replaces the value - // - // operator=(const TValue&) - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - FORCE_INLINE JsonArraySubscript& operator=(const T& src) { - _array.set(_index, src); - return *this; - } - // - // operator=(TValue) - // TValue = char*, const char*, const FlashStringHelper* - template - FORCE_INLINE JsonArraySubscript& operator=(T* src) { - _array.set(_index, src); - return *this; - } - - FORCE_INLINE bool success() const { - return _index < _array.size(); - } - - template - FORCE_INLINE typename JsonVariantAs::type as() const { - return _array.get(_index); - } - - template - FORCE_INLINE bool is() const { - return _array.is(_index); - } - - // Replaces the value - // - // bool set(const TValue&) - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - FORCE_INLINE bool set(const TValue& value) { - return _array.set(_index, value); - } - // - // bool set(TValue) - // TValue = char*, const char*, const FlashStringHelper* - template - FORCE_INLINE bool set(TValue* value) { - return _array.set(_index, value); - } - // - // bool set(TValue, uint8_t decimals); - // TValue = float, double - template - DEPRECATED("Second argument is not supported anymore") - FORCE_INLINE bool set(const TValue& value, uint8_t) { - return _array.set(_index, value); - } - - private: - JsonArray& _array; - const size_t _index; -}; - -template -inline JsonArraySubscript JsonVariantSubscripts::operator[]( - size_t index) { - return impl()->template as()[index]; -} - -template -inline const JsonArraySubscript JsonVariantSubscripts::operator[]( - size_t index) const { - return impl()->template as()[index]; -} - -#if ARDUINOJSON_ENABLE_STD_STREAM -inline std::ostream& operator<<(std::ostream& os, - const JsonArraySubscript& source) { - return source.printTo(os); -} -#endif -} - -inline Internals::JsonArraySubscript JsonArray::operator[](size_t index) { - return Internals::JsonArraySubscript(*this, index); -} - -inline const Internals::JsonArraySubscript JsonArray::operator[]( - size_t index) const { - return Internals::JsonArraySubscript(*const_cast(this), index); -} -} - -#ifdef _MSC_VER -#pragma warning(pop) -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBuffer.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBuffer.hpp deleted file mode 100644 index 26101e086..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBuffer.hpp +++ /dev/null @@ -1,78 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include // for size_t -#include // for uint8_t -#include - -#include "Data/NonCopyable.hpp" -#include "JsonVariant.hpp" -#include "TypeTraits/EnableIf.hpp" -#include "TypeTraits/IsArray.hpp" - -namespace ArduinoJson { -class JsonArray; -class JsonObject; - -// Entry point for using the library. -// -// Handle the memory management (done in derived classes) and calls the parser. -// This abstract class is implemented by StaticJsonBuffer which implements a -// fixed memory allocation. -class JsonBuffer : Internals::NonCopyable { - public: - // Allocates an empty JsonArray. - // - // Returns a reference to the new JsonArray or JsonArray::invalid() if the - // allocation fails. - JsonArray &createArray(); - - // Allocates an empty JsonObject. - // - // Returns a reference to the new JsonObject or JsonObject::invalid() if the - // allocation fails. - JsonObject &createObject(); - - // Duplicates a string - // - // const char* strdup(TValue); - // TValue = const std::string&, const String&, - template - DEPRECATED("char* are duplicated, you don't need strdup() anymore") - typename Internals::EnableIf::value, - const char *>::type strdup(const TString &src) { - return Internals::StringTraits::duplicate(src, this); - } - // - // const char* strdup(TValue); - // TValue = char*, const char*, const FlashStringHelper* - template - DEPRECATED("char* are duplicated, you don't need strdup() anymore") - const char *strdup(TString *src) { - return Internals::StringTraits::duplicate(src, this); - } - - // Allocates n bytes in the JsonBuffer. - // Return a pointer to the allocated memory or NULL if allocation fails. - virtual void *alloc(size_t size) = 0; - - protected: - // CAUTION: NO VIRTUAL DESTRUCTOR! - // If we add a virtual constructor the Arduino compiler will add malloc() - // and free() to the binary, adding 706 useless bytes. - ~JsonBuffer() {} - - // Preserve aligment if necessary - static FORCE_INLINE size_t round_size_up(size_t bytes) { -#if ARDUINOJSON_ENABLE_ALIGNMENT - const size_t x = sizeof(void *) - 1; - return (bytes + x) & ~x; -#else - return bytes; -#endif - } -}; -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBufferBase.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBufferBase.hpp deleted file mode 100644 index 1e771bfdb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBufferBase.hpp +++ /dev/null @@ -1,127 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Deserialization/JsonParser.hpp" - -namespace ArduinoJson { -namespace Internals { -template -class JsonBufferBase : public JsonBuffer { - public: - // Allocates and populate a JsonArray from a JSON string. - // - // The First argument is a pointer to the JSON string, the memory must be - // writable - // because the parser will insert null-terminators and replace escaped chars. - // - // The second argument set the nesting limit - // - // Returns a reference to the new JsonObject or JsonObject::invalid() if the - // allocation fails. - // With this overload, the JsonBuffer will make a copy of the string - // - // JsonArray& parseArray(TString); - // TString = const std::string&, const String& - template - typename Internals::EnableIf::value, - JsonArray &>::type - parseArray(const TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseArray(); - } - // - // JsonArray& parseArray(TString); - // TString = const char*, const char[N], const FlashStringHelper* - template - JsonArray &parseArray( - TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseArray(); - } - // - // JsonArray& parseArray(TString); - // TString = std::istream&, Stream& - template - JsonArray &parseArray( - TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseArray(); - } - - // Allocates and populate a JsonObject from a JSON string. - // - // The First argument is a pointer to the JSON string, the memory must be - // writable - // because the parser will insert null-terminators and replace escaped chars. - // - // The second argument set the nesting limit - // - // Returns a reference to the new JsonObject or JsonObject::invalid() if the - // allocation fails. - // - // JsonObject& parseObject(TString); - // TString = const std::string&, const String& - template - typename Internals::EnableIf::value, - JsonObject &>::type - parseObject(const TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseObject(); - } - // - // JsonObject& parseObject(TString); - // TString = const char*, const char[N], const FlashStringHelper* - template - JsonObject &parseObject( - TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseObject(); - } - // - // JsonObject& parseObject(TString); - // TString = std::istream&, Stream& - template - JsonObject &parseObject( - TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseObject(); - } - - // Generalized version of parseArray() and parseObject(), also works for - // integral types. - // - // JsonVariant parse(TString); - // TString = const std::string&, const String& - template - typename Internals::EnableIf::value, - JsonVariant>::type - parse(const TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseVariant(); - } - // - // JsonVariant parse(TString); - // TString = const char*, const char[N], const FlashStringHelper* - template - JsonVariant parse(TString *json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseVariant(); - } - // - // JsonVariant parse(TString); - // TString = std::istream&, Stream& - template - JsonVariant parse(TString &json, - uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) { - return Internals::makeParser(that(), json, nestingLimit).parseVariant(); - } - - protected: - ~JsonBufferBase() {} - - private: - TDerived *that() { - return static_cast(this); - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBufferImpl.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBufferImpl.hpp deleted file mode 100644 index cdea374bb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonBufferImpl.hpp +++ /dev/null @@ -1,17 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Deserialization/JsonParser.hpp" - -inline ArduinoJson::JsonArray &ArduinoJson::JsonBuffer::createArray() { - JsonArray *ptr = new (this) JsonArray(this); - return ptr ? *ptr : JsonArray::invalid(); -} - -inline ArduinoJson::JsonObject &ArduinoJson::JsonBuffer::createObject() { - JsonObject *ptr = new (this) JsonObject(this); - return ptr ? *ptr : JsonObject::invalid(); -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObject.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObject.hpp deleted file mode 100644 index caf698a3e..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObject.hpp +++ /dev/null @@ -1,328 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Data/JsonBufferAllocated.hpp" -#include "Data/List.hpp" -#include "Data/ReferenceType.hpp" -#include "Data/ValueSaver.hpp" -#include "JsonPair.hpp" -#include "Serialization/JsonPrintable.hpp" -#include "StringTraits/StringTraits.hpp" -#include "TypeTraits/EnableIf.hpp" -#include "TypeTraits/IsArray.hpp" -#include "TypeTraits/IsFloatingPoint.hpp" -#include "TypeTraits/IsSame.hpp" - -// Returns the size (in bytes) of an object with n elements. -// Can be very handy to determine the size of a StaticJsonBuffer. -#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \ - (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type)) - -namespace ArduinoJson { - -// Forward declarations -class JsonArray; -class JsonBuffer; -namespace Internals { -template -class JsonObjectSubscript; -} - -// A dictionary of JsonVariant indexed by string (char*) -// -// The constructor is private, instances must be created via -// JsonBuffer::createObject() or JsonBuffer::parseObject(). -// A JsonObject can be serialized to a JSON string via JsonObject::printTo(). -// It can also be deserialized from a JSON string via JsonBuffer::parseObject(). -class JsonObject : public Internals::JsonPrintable, - public Internals::ReferenceType, - public Internals::NonCopyable, - public Internals::List, - public Internals::JsonBufferAllocated { - public: - // Create an empty JsonArray attached to the specified JsonBuffer. - // You should not use this constructor directly. - // Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject(). - explicit JsonObject(JsonBuffer* buffer) throw() - : Internals::List(buffer) {} - - // Gets or sets the value associated with the specified key. - // - // JsonObjectSubscript operator[](TKey) - // TKey = const std::string&, const String& - template - Internals::JsonObjectSubscript operator[]( - const TString& key) { - return Internals::JsonObjectSubscript(*this, key); - } - // - // JsonObjectSubscript operator[](TKey) - // TKey = char*, const char*, char[], const char[N], const FlashStringHelper* - template - Internals::JsonObjectSubscript operator[](TString* key) { - return Internals::JsonObjectSubscript(*this, key); - } - - // Gets the value associated with the specified key. - // - // const JsonObjectSubscript operator[](TKey) const; - // TKey = const std::string&, const String& - template - const Internals::JsonObjectSubscript operator[]( - const TString& key) const { - return Internals::JsonObjectSubscript( - *const_cast(this), key); - } - // - // const JsonObjectSubscript operator[](TKey) const; - // TKey = const char*, const char[N], const FlashStringHelper* - template - const Internals::JsonObjectSubscript operator[]( - TString* key) const { - return Internals::JsonObjectSubscript( - *const_cast(this), key); - } - - // Sets the specified key with the specified value. - // - // bool set(TKey, TValue); - // TKey = const std::string&, const String& - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool set(const TString& key, const TValue& value) { - return set_impl(key, value); - } - // - // bool set(TKey, TValue); - // TKey = const std::string&, const String& - // TValue = char*, const char*, const FlashStringHelper* - template - bool set(const TString& key, TValue* value) { - return set_impl(key, value); - } - // - // bool set(TKey, const TValue&); - // TKey = char*, const char*, const FlashStringHelper* - // TValue = bool, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - bool set(TString* key, const TValue& value) { - return set_impl(key, value); - } - // - // bool set(TKey, TValue); - // TKey = char*, const char*, const FlashStringHelper* - // TValue = char*, const char*, const FlashStringHelper* - template - bool set(TString* key, TValue* value) { - return set_impl(key, value); - } - // - // bool set(TKey, TValue, uint8_t decimals); - // TKey = const std::string&, const String& - // TValue = float, double - template - DEPRECATED("Second argument is not supported anymore") - typename Internals::EnableIf::value, - bool>::type - set(const TString& key, TValue value, uint8_t) { - return set_impl(key, - JsonVariant(value)); - } - // - // bool set(TKey, TValue, uint8_t decimals); - // TKey = char*, const char*, const FlashStringHelper* - // TValue = float, double - template - DEPRECATED("Second argument is not supported anymore") - typename Internals::EnableIf::value, - bool>::type - set(TString* key, TValue value, uint8_t) { - return set_impl(key, JsonVariant(value)); - } - - // Gets the value associated with the specified key. - // - // TValue get(TKey) const; - // TKey = const std::string&, const String& - // TValue = bool, char, long, int, short, float, double, - // std::string, String, JsonArray, JsonObject - template - typename Internals::JsonVariantAs::type get( - const TString& key) const { - return get_impl(key); - } - // - // TValue get(TKey) const; - // TKey = char*, const char*, const FlashStringHelper* - // TValue = bool, char, long, int, short, float, double, - // std::string, String, JsonArray, JsonObject - template - typename Internals::JsonVariantAs::type get(TString* key) const { - return get_impl(key); - } - - // Checks the type of the value associated with the specified key. - // - // - // bool is(TKey) const; - // TKey = const std::string&, const String& - // TValue = bool, char, long, int, short, float, double, - // std::string, String, JsonArray, JsonObject - template - bool is(const TString& key) const { - return is_impl(key); - } - // - // bool is(TKey) const; - // TKey = char*, const char*, const FlashStringHelper* - // TValue = bool, char, long, int, short, float, double, - // std::string, String, JsonArray, JsonObject - template - bool is(TString* key) const { - return is_impl(key); - } - - // Creates and adds a JsonArray. - // - // JsonArray& createNestedArray(TKey); - // TKey = const std::string&, const String& - template - JsonArray& createNestedArray(const TString& key) { - return createNestedArray_impl(key); - } - // JsonArray& createNestedArray(TKey); - // TKey = char*, const char*, char[], const char[], const FlashStringHelper* - template - JsonArray& createNestedArray(TString* key) { - return createNestedArray_impl(key); - } - - // Creates and adds a JsonObject. - // - // JsonObject& createNestedObject(TKey); - // TKey = const std::string&, const String& - template - JsonObject& createNestedObject(const TString& key) { - return createNestedObject_impl(key); - } - // - // JsonObject& createNestedObject(TKey); - // TKey = char*, const char*, char[], const char[], const FlashStringHelper* - template - JsonObject& createNestedObject(TString* key) { - return createNestedObject_impl(key); - } - - // Tells weither the specified key is present and associated with a value. - // - // bool containsKey(TKey); - // TKey = const std::string&, const String& - template - bool containsKey(const TString& key) const { - return findKey(key) != end(); - } - // - // bool containsKey(TKey); - // TKey = char*, const char*, char[], const char[], const FlashStringHelper* - template - bool containsKey(TString* key) const { - return findKey(key) != end(); - } - - // Removes the specified key and the associated value. - // - // void remove(TKey); - // TKey = const std::string&, const String& - template - void remove(const TString& key) { - remove(findKey(key)); - } - // - // void remove(TKey); - // TKey = char*, const char*, char[], const char[], const FlashStringHelper* - template - void remove(TString* key) { - remove(findKey(key)); - } - // - // void remove(iterator) - using Internals::List::remove; - - // Returns a reference an invalid JsonObject. - // This object is meant to replace a NULL pointer. - // This is used when memory allocation or JSON parsing fail. - static JsonObject& invalid() { - static JsonObject instance(NULL); - return instance; - } - - private: - // Returns the list node that matches the specified key. - template - iterator findKey(TStringRef key) { - iterator it; - for (it = begin(); it != end(); ++it) { - if (Internals::StringTraits::equals(key, it->key)) break; - } - return it; - } - template - const_iterator findKey(TStringRef key) const { - return const_cast(this)->findKey(key); - } - - template - typename Internals::JsonVariantAs::type get_impl( - TStringRef key) const { - const_iterator it = findKey(key); - return it != end() ? it->value.as() - : Internals::JsonVariantDefault::get(); - } - - template - bool set_impl(TStringRef key, TValueRef value) { - // ignore null key - if (Internals::StringTraits::is_null(key)) return false; - - // search a matching key - iterator it = findKey(key); - if (it == end()) { - // add the key - it = Internals::List::add(); - if (it == end()) return false; - bool key_ok = - Internals::ValueSaver::save(_buffer, it->key, key); - if (!key_ok) return false; - } - - // save the value - return Internals::ValueSaver::save(_buffer, it->value, value); - } - - template - bool is_impl(TStringRef key) const { - const_iterator it = findKey(key); - return it != end() ? it->value.is() : false; - } - - template - JsonArray& createNestedArray_impl(TStringRef key); - - template - JsonObject& createNestedObject_impl(TStringRef key); -}; - -namespace Internals { -template <> -struct JsonVariantDefault { - static JsonObject& get() { - return JsonObject::invalid(); - } -}; -} // namespace Internals -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObjectImpl.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObjectImpl.hpp deleted file mode 100644 index e7689b507..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObjectImpl.hpp +++ /dev/null @@ -1,28 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonArray.hpp" -#include "JsonObject.hpp" -#include "JsonObjectSubscript.hpp" - -namespace ArduinoJson { - -template -inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) { - if (!_buffer) return JsonArray::invalid(); - JsonArray &array = _buffer->createArray(); - set(key, array); - return array; -} - -template -inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) { - if (!_buffer) return JsonObject::invalid(); - JsonObject &object = _buffer->createObject(); - set(key, object); - return object; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObjectSubscript.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObjectSubscript.hpp deleted file mode 100644 index 6ac476370..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonObjectSubscript.hpp +++ /dev/null @@ -1,110 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Configuration.hpp" -#include "JsonVariantBase.hpp" -#include "TypeTraits/EnableIf.hpp" - -#ifdef _MSC_VER -#pragma warning(push) -#pragma warning(disable : 4522) -#endif - -namespace ArduinoJson { -namespace Internals { - -template -class JsonObjectSubscript - : public JsonVariantBase > { - typedef JsonObjectSubscript this_type; - - public: - FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key) - : _object(object), _key(key) {} - - FORCE_INLINE this_type& operator=(const this_type& src) { - _object.set(_key, src); - return *this; - } - - // Set the specified value - // - // operator=(const TValue&); - // TValue = bool, char, long, int, short, float, double, - // std::string, String, JsonArray, JsonObject - template - FORCE_INLINE typename EnableIf::value, this_type&>::type - operator=(const TValue& src) { - _object.set(_key, src); - return *this; - } - // - // operator=(TValue); - // TValue = char*, const char*, const FlashStringHelper* - template - FORCE_INLINE this_type& operator=(TValue* src) { - _object.set(_key, src); - return *this; - } - - FORCE_INLINE bool success() const { - return _object.containsKey(_key); - } - - template - FORCE_INLINE typename JsonVariantAs::type as() const { - return _object.get(_key); - } - - template - FORCE_INLINE bool is() const { - return _object.is(_key); - } - - // Sets the specified value. - // - // bool set(const TValue&); - // TValue = bool, char, long, int, short, float, double, RawJson, JsonVariant, - // std::string, String, JsonArray, JsonObject - template - FORCE_INLINE typename EnableIf::value, bool>::type set( - const TValue& value) { - return _object.set(_key, value); - } - // - // bool set(TValue); - // TValue = char*, const char, const FlashStringHelper* - template - FORCE_INLINE bool set(const TValue* value) { - return _object.set(_key, value); - } - // - // bool set(TValue, uint8_t decimals); - // TValue = float, double - template - DEPRECATED("Second argument is not supported anymore") - FORCE_INLINE bool set(const TValue& value, uint8_t) { - return _object.set(_key, value); - } - - private: - JsonObject& _object; - TStringRef _key; -}; - -#if ARDUINOJSON_ENABLE_STD_STREAM -template -inline std::ostream& operator<<(std::ostream& os, - const JsonObjectSubscript& source) { - return source.printTo(os); -} -#endif -} -} - -#ifdef _MSC_VER -#pragma warning(pop) -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonPair.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonPair.hpp deleted file mode 100644 index 417243045..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonPair.hpp +++ /dev/null @@ -1,16 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonVariant.hpp" - -namespace ArduinoJson { - -// A key value pair for JsonObject. -struct JsonPair { - const char* key; - JsonVariant value; -}; -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariant.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariant.hpp deleted file mode 100644 index 43c51b770..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariant.hpp +++ /dev/null @@ -1,357 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include -#include // for uint8_t - -#include "Data/JsonVariantContent.hpp" -#include "Data/JsonVariantDefault.hpp" -#include "Data/JsonVariantType.hpp" -#include "JsonVariantBase.hpp" -#include "RawJson.hpp" -#include "Serialization/JsonPrintable.hpp" -#include "TypeTraits/EnableIf.hpp" -#include "TypeTraits/IsChar.hpp" -#include "TypeTraits/IsFloatingPoint.hpp" -#include "TypeTraits/IsIntegral.hpp" -#include "TypeTraits/IsSame.hpp" -#include "TypeTraits/IsSignedIntegral.hpp" -#include "TypeTraits/IsUnsignedIntegral.hpp" -#include "TypeTraits/RemoveConst.hpp" -#include "TypeTraits/RemoveReference.hpp" - -namespace ArduinoJson { - -// Forward declarations. -class JsonArray; -class JsonObject; - -// A variant that can be a any value serializable to a JSON value. -// -// It can be set to: -// - a boolean -// - a char, short, int or a long (signed or unsigned) -// - a string (const char*) -// - a reference to a JsonArray or JsonObject -class JsonVariant : public Internals::JsonVariantBase { - template - friend class Internals::JsonSerializer; - - public: - // Creates an uninitialized JsonVariant - JsonVariant() : _type(Internals::JSON_UNDEFINED) {} - - // Create a JsonVariant containing a boolean value. - // It will be serialized as "true" or "false" in JSON. - JsonVariant(bool value) { - using namespace Internals; - _type = JSON_BOOLEAN; - _content.asInteger = static_cast(value); - } - - // Create a JsonVariant containing a floating point value. - // JsonVariant(double value); - // JsonVariant(float value); - template - JsonVariant(T value, typename Internals::EnableIf< - Internals::IsFloatingPoint::value>::type * = 0) { - using namespace Internals; - _type = JSON_FLOAT; - _content.asFloat = static_cast(value); - } - template - DEPRECATED("Second argument is not supported anymore") - JsonVariant(T value, uint8_t, - typename Internals::EnableIf< - Internals::IsFloatingPoint::value>::type * = 0) { - using namespace Internals; - _type = JSON_FLOAT; - _content.asFloat = static_cast(value); - } - - // Create a JsonVariant containing an integer value. - // JsonVariant(char) - // JsonVariant(signed short) - // JsonVariant(signed int) - // JsonVariant(signed long) - // JsonVariant(signed char) - template - JsonVariant( - T value, - typename Internals::EnableIf::value || - Internals::IsSame::value>::type * = - 0) { - using namespace Internals; - if (value >= 0) { - _type = JSON_POSITIVE_INTEGER; - _content.asInteger = static_cast(value); - } else { - _type = JSON_NEGATIVE_INTEGER; - _content.asInteger = static_cast(-value); - } - } - // JsonVariant(unsigned short) - // JsonVariant(unsigned int) - // JsonVariant(unsigned long) - template - JsonVariant(T value, - typename Internals::EnableIf< - Internals::IsUnsignedIntegral::value>::type * = 0) { - using namespace Internals; - _type = JSON_POSITIVE_INTEGER; - _content.asInteger = static_cast(value); - } - - // Create a JsonVariant containing a string. - // JsonVariant(const char*); - // JsonVariant(const signed char*); - // JsonVariant(const unsigned char*); - template - JsonVariant( - const TChar *value, - typename Internals::EnableIf::value>::type * = - 0) { - _type = Internals::JSON_STRING; - _content.asString = reinterpret_cast(value); - } - - // Create a JsonVariant containing an unparsed string - JsonVariant(Internals::RawJsonString value) { - _type = Internals::JSON_UNPARSED; - _content.asString = value; - } - - // Create a JsonVariant containing a reference to an array. - // CAUTION: we are lying about constness, because the array can be modified if - // the variant is converted back to a JsonArray& - JsonVariant(const JsonArray &array); - - // Create a JsonVariant containing a reference to an object. - // CAUTION: we are lying about constness, because the object can be modified - // if the variant is converted back to a JsonObject& - JsonVariant(const JsonObject &object); - - // Get the variant as the specified type. - // - // char as() const; - // signed char as() const; - // signed short as() const; - // signed int as() const; - // signed long as() const; - // unsigned char as() const; - // unsigned short as() const; - // unsigned int as() const; - // unsigned long as() const; - template - const typename Internals::EnableIf::value, T>::type - as() const { - return variantAsInteger(); - } - // bool as() const - template - const typename Internals::EnableIf::value, T>::type - as() const { - return variantAsInteger() != 0; - } - // - // double as() const; - // float as() const; - template - const typename Internals::EnableIf::value, - T>::type - as() const { - return variantAsFloat(); - } - // - // const char* as() const; - // const char* as() const; - template - typename Internals::EnableIf::value || - Internals::IsSame::value, - const char *>::type - as() const { - return variantAsString(); - } - // - // std::string as() const; - // String as() const; - template - typename Internals::EnableIf::has_append, T>::type - as() const { - const char *cstr = variantAsString(); - if (cstr) return T(cstr); - T s; - printTo(s); - return s; - } - // - // JsonArray& as const; - // JsonArray& as const; - template - typename Internals::EnableIf< - Internals::IsSame::type, - JsonArray>::value, - JsonArray &>::type - as() const { - return variantAsArray(); - } - // - // const JsonArray& as const; - template - typename Internals::EnableIf< - Internals::IsSame::type, - const JsonArray>::value, - const JsonArray &>::type - as() const { - return variantAsArray(); - } - // - // JsonObject& as const; - // JsonObject& as const; - template - typename Internals::EnableIf< - Internals::IsSame::type, - JsonObject>::value, - JsonObject &>::type - as() const { - return variantAsObject(); - } - // - // JsonObject& as const; - // JsonObject& as const; - template - typename Internals::EnableIf< - Internals::IsSame::type, - const JsonObject>::value, - const JsonObject &>::type - as() const { - return variantAsObject(); - } - // - // JsonVariant as const; - template - typename Internals::EnableIf::value, - T>::type - as() const { - return *this; - } - - // Tells weither the variant has the specified type. - // Returns true if the variant has type type T, false otherwise. - // - // bool is() const; - // bool is() const; - // bool is() const; - // bool is() const; - // bool is() const; - // bool is() const; - // bool is() const; - // bool is() const; - // bool is() const; - template - typename Internals::EnableIf::value, bool>::type is() - const { - return variantIsInteger(); - } - // - // bool is() const; - // bool is() const; - template - typename Internals::EnableIf::value, bool>::type - is() const { - return variantIsFloat(); - } - // - // bool is() const - template - typename Internals::EnableIf::value, bool>::type - is() const { - return variantIsBoolean(); - } - // - // bool is() const; - // bool is() const; - // bool is() const; - template - typename Internals::EnableIf::value || - Internals::IsSame::value || - Internals::StringTraits::has_append, - bool>::type - is() const { - return variantIsString(); - } - // - // bool is const; - // bool is const; - // bool is const; - template - typename Internals::EnableIf< - Internals::IsSame::type>::type, - JsonArray>::value, - bool>::type - is() const { - return variantIsArray(); - } - // - // bool is const; - // bool is const; - // bool is const; - template - typename Internals::EnableIf< - Internals::IsSame::type>::type, - JsonObject>::value, - bool>::type - is() const { - return variantIsObject(); - } - - // Returns true if the variant has a value - bool success() const { - return _type != Internals::JSON_UNDEFINED; - } - - private: - JsonArray &variantAsArray() const; - JsonObject &variantAsObject() const; - const char *variantAsString() const; - template - T variantAsFloat() const; - template - T variantAsInteger() const; - bool variantIsBoolean() const; - bool variantIsFloat() const; - bool variantIsInteger() const; - bool variantIsArray() const { - return _type == Internals::JSON_ARRAY; - } - bool variantIsObject() const { - return _type == Internals::JSON_OBJECT; - } - bool variantIsString() const { - return _type == Internals::JSON_STRING || - (_type == Internals::JSON_UNPARSED && _content.asString && - !strcmp("null", _content.asString)); - } - - // The current type of the variant - Internals::JsonVariantType _type; - - // The various alternatives for the value of the variant. - Internals::JsonVariantContent _content; -}; - -DEPRECATED("Decimal places are ignored, use the float value instead") -inline JsonVariant float_with_n_digits(float value, uint8_t) { - return JsonVariant(value); -} - -DEPRECATED("Decimal places are ignored, use the double value instead") -inline JsonVariant double_with_n_digits(double value, uint8_t) { - return JsonVariant(value); -} -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantBase.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantBase.hpp deleted file mode 100644 index 44acf2e14..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantBase.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonVariantCasts.hpp" -#include "JsonVariantComparisons.hpp" -#include "JsonVariantOr.hpp" -#include "JsonVariantSubscripts.hpp" -#include "Serialization/JsonPrintable.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -class JsonVariantBase : public JsonPrintable, - public JsonVariantCasts, - public JsonVariantComparisons, - public JsonVariantOr, - public JsonVariantSubscripts, - public JsonVariantTag {}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantCasts.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantCasts.hpp deleted file mode 100644 index 68f5bd7dd..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantCasts.hpp +++ /dev/null @@ -1,59 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Data/JsonVariantAs.hpp" -#include "Polyfills/attributes.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -class JsonVariantCasts { - public: -#if ARDUINOJSON_ENABLE_DEPRECATED - DEPRECATED("use as() instead") - FORCE_INLINE JsonArray &asArray() const { - return impl()->template as(); - } - - DEPRECATED("use as() instead") - FORCE_INLINE JsonObject &asObject() const { - return impl()->template as(); - } - - DEPRECATED("use as() instead") - FORCE_INLINE const char *asString() const { - return impl()->template as(); - } -#endif - - // Gets the variant as an array. - // Returns a reference to the JsonArray or JsonArray::invalid() if the - // variant - // is not an array. - FORCE_INLINE operator JsonArray &() const { - return impl()->template as(); - } - - // Gets the variant as an object. - // Returns a reference to the JsonObject or JsonObject::invalid() if the - // variant is not an object. - FORCE_INLINE operator JsonObject &() const { - return impl()->template as(); - } - - template - FORCE_INLINE operator T() const { - return impl()->template as(); - } - - private: - const TImpl *impl() const { - return static_cast(this); - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantComparisons.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantComparisons.hpp deleted file mode 100644 index 47f9d6322..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantComparisons.hpp +++ /dev/null @@ -1,139 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "StringTraits/StringTraits.hpp" -#include "TypeTraits/EnableIf.hpp" -#include "TypeTraits/IsVariant.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -class JsonVariantComparisons { - public: - template - friend bool operator==(const JsonVariantComparisons &variant, - TComparand comparand) { - return variant.equals(comparand); - } - - template - friend typename EnableIf::value, bool>::type - operator==(TComparand comparand, const JsonVariantComparisons &variant) { - return variant.equals(comparand); - } - - template - friend bool operator!=(const JsonVariantComparisons &variant, - TComparand comparand) { - return !variant.equals(comparand); - } - - template - friend typename EnableIf::value, bool>::type - operator!=(TComparand comparand, const JsonVariantComparisons &variant) { - return !variant.equals(comparand); - } - - template - friend bool operator<=(const JsonVariantComparisons &left, TComparand right) { - return left.as() <= right; - } - - template - friend bool operator<=(TComparand comparand, - const JsonVariantComparisons &variant) { - return comparand <= variant.as(); - } - - template - friend bool operator>=(const JsonVariantComparisons &variant, - TComparand comparand) { - return variant.as() >= comparand; - } - - template - friend bool operator>=(TComparand comparand, - const JsonVariantComparisons &variant) { - return comparand >= variant.as(); - } - - template - friend bool operator<(const JsonVariantComparisons &varian, - TComparand comparand) { - return varian.as() < comparand; - } - - template - friend bool operator<(TComparand comparand, - const JsonVariantComparisons &variant) { - return comparand < variant.as(); - } - - template - friend bool operator>(const JsonVariantComparisons &variant, - TComparand comparand) { - return variant.as() > comparand; - } - - template - friend bool operator>(TComparand comparand, - const JsonVariantComparisons &variant) { - return comparand > variant.as(); - } - - private: - const TImpl *impl() const { - return static_cast(this); - } - - template - const typename JsonVariantAs::type as() const { - return impl()->template as(); - } - - template - bool is() const { - return impl()->template is(); - } - - template - typename EnableIf::has_equals, bool>::type equals( - const TString &comparand) const { - const char *value = as(); - return StringTraits::equals(comparand, value); - } - - template - typename EnableIf::value && - !StringTraits::has_equals, - bool>::type - equals(const TComparand &comparand) const { - return as() == comparand; - } - - template - bool equals(const JsonVariantComparisons &right) const { - using namespace Internals; - if (is() && right.template is()) - return as() == right.template as(); - if (is() && right.template is()) - return as() == right.template as(); - if (is() && right.template is()) - return as() == right.template as(); - if (is() && right.template is()) - return as() == right.template as(); - if (is() && right.template is()) - return as() == right.template as(); - if (is() && right.template is()) - return StringTraits::equals(as(), - right.template as()); - - return false; - } -}; -} // namespace Internals -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantImpl.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantImpl.hpp deleted file mode 100644 index 31f96ce1a..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantImpl.hpp +++ /dev/null @@ -1,126 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Configuration.hpp" -#include "JsonArray.hpp" -#include "JsonObject.hpp" -#include "JsonVariant.hpp" -#include "Polyfills/isFloat.hpp" -#include "Polyfills/isInteger.hpp" -#include "Polyfills/parseFloat.hpp" -#include "Polyfills/parseInteger.hpp" - -#include // for strcmp - -namespace ArduinoJson { - -inline JsonVariant::JsonVariant(const JsonArray &array) { - if (array.success()) { - _type = Internals::JSON_ARRAY; - _content.asArray = const_cast(&array); - } else { - _type = Internals::JSON_UNDEFINED; - } -} - -inline JsonVariant::JsonVariant(const JsonObject &object) { - if (object.success()) { - _type = Internals::JSON_OBJECT; - _content.asObject = const_cast(&object); - } else { - _type = Internals::JSON_UNDEFINED; - } -} - -inline JsonArray &JsonVariant::variantAsArray() const { - if (_type == Internals::JSON_ARRAY) return *_content.asArray; - return JsonArray::invalid(); -} - -inline JsonObject &JsonVariant::variantAsObject() const { - if (_type == Internals::JSON_OBJECT) return *_content.asObject; - return JsonObject::invalid(); -} - -template -inline T JsonVariant::variantAsInteger() const { - using namespace Internals; - switch (_type) { - case JSON_UNDEFINED: - return 0; - case JSON_POSITIVE_INTEGER: - case JSON_BOOLEAN: - return T(_content.asInteger); - case JSON_NEGATIVE_INTEGER: - return T(~_content.asInteger + 1); - case JSON_STRING: - case JSON_UNPARSED: - return parseInteger(_content.asString); - default: - return T(_content.asFloat); - } -} - -inline const char *JsonVariant::variantAsString() const { - using namespace Internals; - if (_type == JSON_UNPARSED && _content.asString && - !strcmp("null", _content.asString)) - return NULL; - if (_type == JSON_STRING || _type == JSON_UNPARSED) return _content.asString; - return NULL; -} - -template -inline T JsonVariant::variantAsFloat() const { - using namespace Internals; - switch (_type) { - case JSON_UNDEFINED: - return 0; - case JSON_POSITIVE_INTEGER: - case JSON_BOOLEAN: - return static_cast(_content.asInteger); - case JSON_NEGATIVE_INTEGER: - return -static_cast(_content.asInteger); - case JSON_STRING: - case JSON_UNPARSED: - return parseFloat(_content.asString); - default: - return static_cast(_content.asFloat); - } -} - -inline bool JsonVariant::variantIsBoolean() const { - using namespace Internals; - if (_type == JSON_BOOLEAN) return true; - - if (_type != JSON_UNPARSED || _content.asString == NULL) return false; - - return !strcmp(_content.asString, "true") || - !strcmp(_content.asString, "false"); -} - -inline bool JsonVariant::variantIsInteger() const { - using namespace Internals; - - return _type == JSON_POSITIVE_INTEGER || _type == JSON_NEGATIVE_INTEGER || - (_type == JSON_UNPARSED && isInteger(_content.asString)); -} - -inline bool JsonVariant::variantIsFloat() const { - using namespace Internals; - - return _type == JSON_FLOAT || _type == JSON_POSITIVE_INTEGER || - _type == JSON_NEGATIVE_INTEGER || - (_type == JSON_UNPARSED && isFloat(_content.asString)); -} - -#if ARDUINOJSON_ENABLE_STD_STREAM -inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) { - return source.printTo(os); -} -#endif - -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantOr.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantOr.hpp deleted file mode 100644 index d8022fcb2..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantOr.hpp +++ /dev/null @@ -1,52 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Data/JsonVariantAs.hpp" -#include "Polyfills/attributes.hpp" -#include "TypeTraits/EnableIf.hpp" -#include "TypeTraits/IsIntegral.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -class JsonVariantOr { - public: - // Returns the default value if the JsonVariant is undefined of incompatible - template - typename EnableIf::value, T>::type operator|( - const T &defaultValue) const { - if (impl()->template is()) - return impl()->template as(); - else - return defaultValue; - } - - // Returns the default value if the JsonVariant is undefined of incompatible - // Special case for string: null is treated as undefined - const char *operator|(const char *defaultValue) const { - const char *value = impl()->template as(); - return value ? value : defaultValue; - } - - // Returns the default value if the JsonVariant is undefined of incompatible - // Special case for integers: we also accept double - template - typename EnableIf::value, Integer>::type operator|( - const Integer &defaultValue) const { - if (impl()->template is()) - return impl()->template as(); - else - return defaultValue; - } - - private: - const TImpl *impl() const { - return static_cast(this); - } -}; -} // namespace Internals -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantSubscripts.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantSubscripts.hpp deleted file mode 100644 index 279ee019f..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/JsonVariantSubscripts.hpp +++ /dev/null @@ -1,86 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "Data/JsonVariantAs.hpp" -#include "Polyfills/attributes.hpp" -#include "StringTraits/StringTraits.hpp" -#include "TypeTraits/EnableIf.hpp" - -namespace ArduinoJson { -namespace Internals { - -// Forward declarations. -class JsonArraySubscript; -template -class JsonObjectSubscript; - -template -class JsonVariantSubscripts { - public: - // Mimics an array or an object. - // Returns the size of the array or object if the variant has that type. - // Returns 0 if the variant is neither an array nor an object - size_t size() const { - return impl()->template as().size() + - impl()->template as().size(); - } - - // Mimics an array. - // Returns the element at specified index if the variant is an array. - // Returns JsonVariant::invalid() if the variant is not an array. - FORCE_INLINE const JsonArraySubscript operator[](size_t index) const; - FORCE_INLINE JsonArraySubscript operator[](size_t index); - - // Mimics an object. - // Returns the value associated with the specified key if the variant is - // an object. - // Return JsonVariant::invalid() if the variant is not an object. - // - // const JsonObjectSubscript operator[](TKey) const; - // TKey = const std::string&, const String& - template - FORCE_INLINE - typename EnableIf::has_equals, - const JsonObjectSubscript >::type - operator[](const TString &key) const { - return impl()->template as()[key]; - } - // - // const JsonObjectSubscript operator[](TKey) const; - // TKey = const std::string&, const String& - template - FORCE_INLINE typename EnableIf::has_equals, - JsonObjectSubscript >::type - operator[](const TString &key) { - return impl()->template as()[key]; - } - // - // JsonObjectSubscript operator[](TKey); - // TKey = const char*, const char[N], const FlashStringHelper* - template - FORCE_INLINE typename EnableIf::has_equals, - JsonObjectSubscript >::type - operator[](const TString *key) { - return impl()->template as()[key]; - } - // - // JsonObjectSubscript operator[](TKey); - // TKey = const char*, const char[N], const FlashStringHelper* - template - FORCE_INLINE - typename EnableIf::has_equals, - const JsonObjectSubscript >::type - operator[](const TString *key) const { - return impl()->template as()[key]; - } - - private: - const TImpl *impl() const { - return static_cast(this); - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/attributes.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/attributes.hpp deleted file mode 100644 index b49091ddc..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/attributes.hpp +++ /dev/null @@ -1,29 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#ifdef _MSC_VER // Visual Studio - -#define FORCE_INLINE // __forceinline causes C4714 when returning std::string -#define NO_INLINE __declspec(noinline) -#define DEPRECATED(msg) __declspec(deprecated(msg)) - -#elif defined(__GNUC__) // GCC or Clang - -#define FORCE_INLINE __attribute__((always_inline)) -#define NO_INLINE __attribute__((noinline)) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) -#define DEPRECATED(msg) __attribute__((deprecated(msg))) -#else -#define DEPRECATED(msg) __attribute__((deprecated)) -#endif - -#else // Other compilers - -#define FORCE_INLINE -#define NO_INLINE -#define DEPRECATED(msg) - -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/ctype.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/ctype.hpp deleted file mode 100644 index 2d52703cd..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/ctype.hpp +++ /dev/null @@ -1,18 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -inline bool isdigit(char c) { - return '0' <= c && c <= '9'; -} - -inline bool issign(char c) { - return '-' == c || c == '+'; -} -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/isFloat.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/isFloat.hpp deleted file mode 100644 index 973b89fe9..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/isFloat.hpp +++ /dev/null @@ -1,38 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include // for strcmp -#include "./ctype.hpp" - -namespace ArduinoJson { -namespace Internals { - -inline bool isFloat(const char* s) { - if (!s) return false; - - if (!strcmp(s, "NaN")) return true; - if (issign(*s)) s++; - if (!strcmp(s, "Infinity")) return true; - if (*s == '\0') return false; - - while (isdigit(*s)) s++; - - if (*s == '.') { - s++; - while (isdigit(*s)) s++; - } - - if (*s == 'e' || *s == 'E') { - s++; - if (issign(*s)) s++; - if (!isdigit(*s)) return false; - while (isdigit(*s)) s++; - } - - return *s == '\0'; -} -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/isInteger.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/isInteger.hpp deleted file mode 100644 index 8049079a7..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/isInteger.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "./ctype.hpp" - -namespace ArduinoJson { -namespace Internals { - -inline bool isInteger(const char* s) { - if (!s || !*s) return false; - if (issign(*s)) s++; - while (isdigit(*s)) s++; - return *s == '\0'; -} -} // namespace Internals -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/math.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/math.hpp deleted file mode 100644 index 48773edd2..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/math.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { -template -bool isNaN(T x) { - return x != x; -} - -template -bool isInfinity(T x) { - return x != 0.0 && x * 2 == x; -} -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/parseFloat.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/parseFloat.hpp deleted file mode 100644 index 49b0f6fcd..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/parseFloat.hpp +++ /dev/null @@ -1,90 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../TypeTraits/FloatTraits.hpp" -#include "./ctype.hpp" -#include "./math.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -inline T parseFloat(const char* s) { - typedef FloatTraits traits; - typedef typename traits::mantissa_type mantissa_t; - typedef typename traits::exponent_type exponent_t; - - if (!s) return 0; // NULL - - bool negative_result = false; - switch (*s) { - case '-': - negative_result = true; - s++; - break; - case '+': - s++; - break; - } - - if (*s == 't') return 1; // true - if (*s == 'n' || *s == 'N') return traits::nan(); - if (*s == 'i' || *s == 'I') - return negative_result ? -traits::inf() : traits::inf(); - - mantissa_t mantissa = 0; - exponent_t exponent_offset = 0; - - while (isdigit(*s)) { - if (mantissa < traits::mantissa_max / 10) - mantissa = mantissa * 10 + (*s - '0'); - else - exponent_offset++; - s++; - } - - if (*s == '.') { - s++; - while (isdigit(*s)) { - if (mantissa < traits::mantissa_max / 10) { - mantissa = mantissa * 10 + (*s - '0'); - exponent_offset--; - } - s++; - } - } - - int exponent = 0; - if (*s == 'e' || *s == 'E') { - s++; - bool negative_exponent = false; - if (*s == '-') { - negative_exponent = true; - s++; - } else if (*s == '+') { - s++; - } - - while (isdigit(*s)) { - exponent = exponent * 10 + (*s - '0'); - if (exponent + exponent_offset > traits::exponent_max) { - if (negative_exponent) - return negative_result ? -0.0f : 0.0f; - else - return negative_result ? -traits::inf() : traits::inf(); - } - s++; - } - if (negative_exponent) exponent = -exponent; - } - exponent += exponent_offset; - - T result = traits::make_float(static_cast(mantissa), exponent); - - return negative_result ? -result : result; -} -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/parseInteger.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/parseInteger.hpp deleted file mode 100644 index e8f197494..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Polyfills/parseInteger.hpp +++ /dev/null @@ -1,41 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include - -#include "../Configuration.hpp" -#include "./ctype.hpp" - -namespace ArduinoJson { -namespace Internals { -template -T parseInteger(const char *s) { - if (!s) return 0; // NULL - - if (*s == 't') return 1; // "true" - - T result = 0; - bool negative_result = false; - - switch (*s) { - case '-': - negative_result = true; - s++; - break; - case '+': - s++; - break; - } - - while (isdigit(*s)) { - result = T(result * 10 + T(*s - '0')); - s++; - } - - return negative_result ? T(~result + 1) : result; -} -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/RawJson.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/RawJson.hpp deleted file mode 100644 index 4beb980ee..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/RawJson.hpp +++ /dev/null @@ -1,46 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { - -namespace Internals { -// A special type of data that can be used to insert pregenerated JSON portions. -template -class RawJsonString { - public: - explicit RawJsonString(T str) : _str(str) {} - operator T() const { - return _str; - } - - private: - T _str; -}; - -template -struct StringTraits, void> { - static bool is_null(RawJsonString source) { - return StringTraits::is_null(static_cast(source)); - } - - typedef RawJsonString duplicate_t; - - template - static duplicate_t duplicate(RawJsonString source, Buffer* buffer) { - return duplicate_t(StringTraits::duplicate(source, buffer)); - } - - static const bool has_append = false; - static const bool has_equals = false; - static const bool should_duplicate = StringTraits::should_duplicate; -}; -} - -template -inline Internals::RawJsonString RawJson(T str) { - return Internals::RawJsonString(str); -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/DummyPrint.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/DummyPrint.hpp deleted file mode 100644 index 9fdf2d6a0..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/DummyPrint.hpp +++ /dev/null @@ -1,22 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A dummy Print implementation used in JsonPrintable::measureLength() -class DummyPrint { - public: - size_t print(char) { - return 1; - } - - size_t print(const char* s) { - return strlen(s); - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/DynamicStringBuilder.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/DynamicStringBuilder.hpp deleted file mode 100644 index 41be6392c..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/DynamicStringBuilder.hpp +++ /dev/null @@ -1,35 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../StringTraits/StringTraits.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A Print implementation that allows to write in a String -template -class DynamicStringBuilder { - public: - DynamicStringBuilder(TString &str) : _str(str) {} - - size_t print(char c) { - StringTraits::append(_str, c); - return 1; - } - - size_t print(const char *s) { - size_t initialLen = _str.length(); - StringTraits::append(_str, s); - return _str.length() - initialLen; - } - - private: - DynamicStringBuilder &operator=(const DynamicStringBuilder &); - - TString &_str; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/FloatParts.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/FloatParts.hpp deleted file mode 100644 index c14e3b553..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/FloatParts.hpp +++ /dev/null @@ -1,89 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" -#include "../Polyfills/math.hpp" -#include "../TypeTraits/FloatTraits.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -struct FloatParts { - uint32_t integral; - uint32_t decimal; - int16_t exponent; - int8_t decimalPlaces; - - FloatParts(TFloat value) { - uint32_t maxDecimalPart = sizeof(TFloat) >= 8 ? 1000000000 : 1000000; - decimalPlaces = sizeof(TFloat) >= 8 ? 9 : 6; - - exponent = normalize(value); - - integral = uint32_t(value); - // reduce number of decimal places by the number of integral places - for (uint32_t tmp = integral; tmp >= 10; tmp /= 10) { - maxDecimalPart /= 10; - decimalPlaces--; - } - - TFloat remainder = (value - TFloat(integral)) * TFloat(maxDecimalPart); - - decimal = uint32_t(remainder); - remainder = remainder - TFloat(decimal); - - // rounding: - // increment by 1 if remainder >= 0.5 - decimal += uint32_t(remainder * 2); - if (decimal >= maxDecimalPart) { - decimal = 0; - integral++; - if (exponent && integral >= 10) { - exponent++; - integral = 1; - } - } - - // remove trailing zeros - while (decimal % 10 == 0 && decimalPlaces > 0) { - decimal /= 10; - decimalPlaces--; - } - } - - static int16_t normalize(TFloat& value) { - typedef FloatTraits traits; - int16_t powersOf10 = 0; - - int8_t index = sizeof(TFloat) == 8 ? 8 : 5; - int bit = 1 << index; - - if (value >= ARDUINOJSON_POSITIVE_EXPONENTIATION_THRESHOLD) { - for (; index >= 0; index--) { - if (value >= traits::positiveBinaryPowerOfTen(index)) { - value *= traits::negativeBinaryPowerOfTen(index); - powersOf10 = int16_t(powersOf10 + bit); - } - bit >>= 1; - } - } - - if (value > 0 && value <= ARDUINOJSON_NEGATIVE_EXPONENTIATION_THRESHOLD) { - for (; index >= 0; index--) { - if (value < traits::negativeBinaryPowerOfTenPlusOne(index)) { - value *= traits::positiveBinaryPowerOfTen(index); - powersOf10 = int16_t(powersOf10 - bit); - } - bit >>= 1; - } - } - - return powersOf10; - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/IndentedPrint.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/IndentedPrint.hpp deleted file mode 100644 index 864f9aaa4..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/IndentedPrint.hpp +++ /dev/null @@ -1,68 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// Decorator on top of Print to allow indented output. -// This class is used by JsonPrintable::prettyPrintTo() but can also be used -// for your own purpose, like logging. -template -class IndentedPrint { - public: - explicit IndentedPrint(Print &p) : sink(&p) { - level = 0; - tabSize = 2; - isNewLine = true; - } - - size_t print(char c) { - size_t n = 0; - if (isNewLine) n += writeTabs(); - n += sink->print(c); - isNewLine = c == '\n'; - return n; - } - - size_t print(const char *s) { - // TODO: optimize - size_t n = 0; - while (*s) n += print(*s++); - return n; - } - - // Adds one level of indentation - void indent() { - if (level < MAX_LEVEL) level++; - } - - // Removes one level of indentation - void unindent() { - if (level > 0) level--; - } - - // Set the number of space printed for each level of indentation - void setTabSize(uint8_t n) { - if (n < MAX_TAB_SIZE) tabSize = n & MAX_TAB_SIZE; - } - - private: - Print *sink; - uint8_t level : 4; - uint8_t tabSize : 3; - bool isNewLine : 1; - - size_t writeTabs() { - size_t n = 0; - for (int i = 0; i < level * tabSize; i++) n += sink->print(' '); - return n; - } - - static const int MAX_LEVEL = 15; // because it's only 4 bits - static const int MAX_TAB_SIZE = 7; // because it's only 3 bits -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonPrintable.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonPrintable.hpp deleted file mode 100644 index 43d413a85..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonPrintable.hpp +++ /dev/null @@ -1,117 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" -#include "../TypeTraits/EnableIf.hpp" -#include "DummyPrint.hpp" -#include "DynamicStringBuilder.hpp" -#include "IndentedPrint.hpp" -#include "JsonSerializer.hpp" -#include "JsonWriter.hpp" -#include "Prettyfier.hpp" -#include "StaticStringBuilder.hpp" - -#if ARDUINOJSON_ENABLE_STD_STREAM -#include "StreamPrintAdapter.hpp" -#endif - -namespace ArduinoJson { -namespace Internals { - -// Implements all the overloads of printTo() and prettyPrintTo() -// Caution: this class use a template parameter to avoid virtual methods. -// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray -// and JsonObject. -template -class JsonPrintable { - public: - template - typename EnableIf::has_append, size_t>::type printTo( - Print &print) const { - JsonWriter writer(print); - JsonSerializer >::serialize(downcast(), writer); - return writer.bytesWritten(); - } - -#if ARDUINOJSON_ENABLE_STD_STREAM - std::ostream &printTo(std::ostream &os) const { - StreamPrintAdapter adapter(os); - printTo(adapter); - return os; - } -#endif - - size_t printTo(char *buffer, size_t bufferSize) const { - StaticStringBuilder sb(buffer, bufferSize); - return printTo(sb); - } - - template - size_t printTo(char (&buffer)[N]) const { - return printTo(buffer, N); - } - - template - typename EnableIf::has_append, size_t>::type printTo( - TString &str) const { - DynamicStringBuilder sb(str); - return printTo(sb); - } - - template - size_t prettyPrintTo(IndentedPrint &print) const { - Prettyfier p(print); - return printTo(p); - } - - size_t prettyPrintTo(char *buffer, size_t bufferSize) const { - StaticStringBuilder sb(buffer, bufferSize); - return prettyPrintTo(sb); - } - - template - size_t prettyPrintTo(char (&buffer)[N]) const { - return prettyPrintTo(buffer, N); - } - - template - typename EnableIf::has_append, size_t>::type - prettyPrintTo(Print &print) const { - IndentedPrint indentedPrint(print); - return prettyPrintTo(indentedPrint); - } - - template - typename EnableIf::has_append, size_t>::type - prettyPrintTo(TString &str) const { - DynamicStringBuilder sb(str); - return prettyPrintTo(sb); - } - - size_t measureLength() const { - DummyPrint dp; - return printTo(dp); - } - - size_t measurePrettyLength() const { - DummyPrint dp; - return prettyPrintTo(dp); - } - - private: - const T &downcast() const { - return *static_cast(this); - } -}; - -#if ARDUINOJSON_ENABLE_STD_STREAM -template -inline std::ostream &operator<<(std::ostream &os, const JsonPrintable &v) { - return v.printTo(os); -} -#endif -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonSerializer.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonSerializer.hpp deleted file mode 100644 index 0cb537f7d..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonSerializer.hpp +++ /dev/null @@ -1,32 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonWriter.hpp" - -namespace ArduinoJson { - -class JsonArray; -class JsonObject; -class JsonVariant; - -namespace Internals { - -class JsonArraySubscript; -template -class JsonObjectSubscript; - -template -class JsonSerializer { - public: - static void serialize(const JsonArray &, Writer &); - static void serialize(const JsonArraySubscript &, Writer &); - static void serialize(const JsonObject &, Writer &); - template - static void serialize(const JsonObjectSubscript &, Writer &); - static void serialize(const JsonVariant &, Writer &); -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonSerializerImpl.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonSerializerImpl.hpp deleted file mode 100644 index 0faae2769..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonSerializerImpl.hpp +++ /dev/null @@ -1,103 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../JsonArray.hpp" -#include "../JsonArraySubscript.hpp" -#include "../JsonObject.hpp" -#include "../JsonObjectSubscript.hpp" -#include "../JsonVariant.hpp" -#include "JsonSerializer.hpp" - -template -inline void ArduinoJson::Internals::JsonSerializer::serialize( - const JsonArray& array, Writer& writer) { - writer.beginArray(); - - JsonArray::const_iterator it = array.begin(); - while (it != array.end()) { - serialize(*it, writer); - - ++it; - if (it == array.end()) break; - - writer.writeComma(); - } - - writer.endArray(); -} - -template -inline void ArduinoJson::Internals::JsonSerializer::serialize( - const JsonArraySubscript& arraySubscript, Writer& writer) { - serialize(arraySubscript.as(), writer); -} - -template -inline void ArduinoJson::Internals::JsonSerializer::serialize( - const JsonObject& object, Writer& writer) { - writer.beginObject(); - - JsonObject::const_iterator it = object.begin(); - while (it != object.end()) { - writer.writeString(it->key); - writer.writeColon(); - serialize(it->value, writer); - - ++it; - if (it == object.end()) break; - - writer.writeComma(); - } - - writer.endObject(); -} - -template -template -inline void ArduinoJson::Internals::JsonSerializer::serialize( - const JsonObjectSubscript& objectSubscript, Writer& writer) { - serialize(objectSubscript.template as(), writer); -} - -template -inline void ArduinoJson::Internals::JsonSerializer::serialize( - const JsonVariant& variant, Writer& writer) { - switch (variant._type) { - case JSON_FLOAT: - writer.writeFloat(variant._content.asFloat); - return; - - case JSON_ARRAY: - serialize(*variant._content.asArray, writer); - return; - - case JSON_OBJECT: - serialize(*variant._content.asObject, writer); - return; - - case JSON_STRING: - writer.writeString(variant._content.asString); - return; - - case JSON_UNPARSED: - writer.writeRaw(variant._content.asString); - return; - - case JSON_NEGATIVE_INTEGER: - writer.writeRaw('-'); // Falls through. - - case JSON_POSITIVE_INTEGER: - writer.writeInteger(variant._content.asInteger); - return; - - case JSON_BOOLEAN: - writer.writeBoolean(variant._content.asInteger != 0); - return; - - default: // JSON_UNDEFINED - return; - } -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonWriter.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonWriter.hpp deleted file mode 100644 index 146d51dcb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/JsonWriter.hpp +++ /dev/null @@ -1,155 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include -#include "../Data/Encoding.hpp" -#include "../Data/JsonInteger.hpp" -#include "../Polyfills/attributes.hpp" -#include "../Serialization/FloatParts.hpp" - -namespace ArduinoJson { -namespace Internals { - -// Writes the JSON tokens to a Print implementation -// This class is used by: -// - JsonArray::writeTo() -// - JsonObject::writeTo() -// - JsonVariant::writeTo() -// Its derived by PrettyJsonWriter that overrides some members to add -// indentation. -template -class JsonWriter { - public: - explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {} - - // Returns the number of bytes sent to the Print implementation. - // This is very handy for implementations of printTo() that must return the - // number of bytes written. - size_t bytesWritten() const { - return _length; - } - - void beginArray() { - writeRaw('['); - } - void endArray() { - writeRaw(']'); - } - - void beginObject() { - writeRaw('{'); - } - void endObject() { - writeRaw('}'); - } - - void writeColon() { - writeRaw(':'); - } - void writeComma() { - writeRaw(','); - } - - void writeBoolean(bool value) { - writeRaw(value ? "true" : "false"); - } - - void writeString(const char *value) { - if (!value) { - writeRaw("null"); - } else { - writeRaw('\"'); - while (*value) writeChar(*value++); - writeRaw('\"'); - } - } - - void writeChar(char c) { - char specialChar = Encoding::escapeChar(c); - if (specialChar) { - writeRaw('\\'); - writeRaw(specialChar); - } else { - writeRaw(c); - } - } - - template - void writeFloat(TFloat value) { - if (isNaN(value)) return writeRaw("NaN"); - - if (value < 0.0) { - writeRaw('-'); - value = -value; - } - - if (isInfinity(value)) return writeRaw("Infinity"); - - FloatParts parts(value); - - writeInteger(parts.integral); - if (parts.decimalPlaces) writeDecimals(parts.decimal, parts.decimalPlaces); - - if (parts.exponent < 0) { - writeRaw("e-"); - writeInteger(-parts.exponent); - } - - if (parts.exponent > 0) { - writeRaw('e'); - writeInteger(parts.exponent); - } - } - - template - void writeInteger(UInt value) { - char buffer[22]; - char *end = buffer + sizeof(buffer) - 1; - char *ptr = end; - - *ptr = 0; - do { - *--ptr = char(value % 10 + '0'); - value = UInt(value / 10); - } while (value); - - writeRaw(ptr); - } - - void writeDecimals(uint32_t value, int8_t width) { - // buffer should be big enough for all digits, the dot and the null - // terminator - char buffer[16]; - char *ptr = buffer + sizeof(buffer) - 1; - - // write the string in reverse order - *ptr = 0; - while (width--) { - *--ptr = char(value % 10 + '0'); - value /= 10; - } - *--ptr = '.'; - - // and dump it in the right order - writeRaw(ptr); - } - - void writeRaw(const char *s) { - _length += _sink.print(s); - } - void writeRaw(char c) { - _length += _sink.print(c); - } - - protected: - Print &_sink; - size_t _length; - - private: - JsonWriter &operator=(const JsonWriter &); // cannot be assigned -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/Prettyfier.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/Prettyfier.hpp deleted file mode 100644 index 8b4f0d2eb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/Prettyfier.hpp +++ /dev/null @@ -1,133 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "IndentedPrint.hpp" - -namespace ArduinoJson { -namespace Internals { - -// Converts a compact JSON string into an indented one. -template -class Prettyfier { - public: - explicit Prettyfier(IndentedPrint& p) : _sink(p) { - _previousChar = 0; - _inString = false; - } - - size_t print(char c) { - size_t n = _inString ? handleStringChar(c) : handleMarkupChar(c); - _previousChar = c; - return n; - } - - size_t print(const char* s) { - // TODO: optimize - size_t n = 0; - while (*s) n += print(*s++); - return n; - } - - private: - Prettyfier& operator=(const Prettyfier&); // cannot be assigned - - bool inEmptyBlock() { - return _previousChar == '{' || _previousChar == '['; - } - - size_t handleStringChar(char c) { - bool isQuote = c == '"' && _previousChar != '\\'; - - if (isQuote) _inString = false; - - return _sink.print(c); - } - - size_t handleMarkupChar(char c) { - switch (c) { - case '{': - case '[': - return writeBlockOpen(c); - - case '}': - case ']': - return writeBlockClose(c); - - case ':': - return writeColon(); - - case ',': - return writeComma(); - - case '"': - return writeQuoteOpen(); - - default: - return writeNormalChar(c); - } - } - - size_t writeBlockClose(char c) { - size_t n = 0; - n += unindentIfNeeded(); - n += _sink.print(c); - return n; - } - - size_t writeBlockOpen(char c) { - size_t n = 0; - n += indentIfNeeded(); - n += _sink.print(c); - return n; - } - - size_t writeColon() { - size_t n = 0; - n += _sink.print(": "); - return n; - } - - size_t writeComma() { - size_t n = 0; - n += _sink.print(",\r\n"); - return n; - } - - size_t writeQuoteOpen() { - _inString = true; - size_t n = 0; - n += indentIfNeeded(); - n += _sink.print('"'); - return n; - } - - size_t writeNormalChar(char c) { - size_t n = 0; - n += indentIfNeeded(); - n += _sink.print(c); - return n; - } - - size_t indentIfNeeded() { - if (!inEmptyBlock()) return 0; - - _sink.indent(); - return _sink.print("\r\n"); - } - - size_t unindentIfNeeded() { - if (inEmptyBlock()) return 0; - - _sink.unindent(); - return _sink.print("\r\n"); - } - - char _previousChar; - IndentedPrint& _sink; - bool _inString; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/StaticStringBuilder.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/StaticStringBuilder.hpp deleted file mode 100644 index 9617bbd97..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/StaticStringBuilder.hpp +++ /dev/null @@ -1,36 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A Print implementation that allows to write in a char[] -class StaticStringBuilder { - public: - StaticStringBuilder(char *buf, size_t size) : end(buf + size - 1), p(buf) { - *p = '\0'; - } - - size_t print(char c) { - if (p >= end) return 0; - *p++ = c; - *p = '\0'; - return 1; - } - - size_t print(const char *s) { - char *begin = p; - while (p < end && *s) *p++ = *s++; - *p = '\0'; - return size_t(p - begin); - } - - private: - char *end; - char *p; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/StreamPrintAdapter.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/StreamPrintAdapter.hpp deleted file mode 100644 index 60f0af4a3..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/Serialization/StreamPrintAdapter.hpp +++ /dev/null @@ -1,39 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" - -#if ARDUINOJSON_ENABLE_STD_STREAM - -#include - -namespace ArduinoJson { -namespace Internals { - -class StreamPrintAdapter { - public: - explicit StreamPrintAdapter(std::ostream& os) : _os(os) {} - - size_t print(char c) { - _os << c; - return 1; - } - - size_t print(const char* s) { - _os << s; - return strlen(s); - } - - private: - // cannot be assigned - StreamPrintAdapter& operator=(const StreamPrintAdapter&); - - std::ostream& _os; -}; -} -} - -#endif // ARDUINOJSON_ENABLE_STD_STREAM diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StaticJsonBuffer.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StaticJsonBuffer.hpp deleted file mode 100644 index 267d9d018..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StaticJsonBuffer.hpp +++ /dev/null @@ -1,126 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "JsonBufferBase.hpp" - -namespace ArduinoJson { -namespace Internals { - -class StaticJsonBufferBase : public JsonBufferBase { - public: - class String { - public: - String(StaticJsonBufferBase* parent) : _parent(parent) { - _start = parent->_buffer + parent->_size; - } - - void append(char c) { - if (_parent->canAlloc(1)) { - char* last = static_cast(_parent->doAlloc(1)); - *last = c; - } - } - - const char* c_str() const { - if (_parent->canAlloc(1)) { - char* last = static_cast(_parent->doAlloc(1)); - *last = '\0'; - return _start; - } else { - return NULL; - } - } - - private: - StaticJsonBufferBase* _parent; - char* _start; - }; - - StaticJsonBufferBase(char* buffer, size_t capa) - : _buffer(buffer), _capacity(capa), _size(0) {} - - // Gets the capacity of the buffer in bytes - size_t capacity() const { - return _capacity; - } - - // Gets the current usage of the buffer in bytes - size_t size() const { - return _size; - } - - // Allocates the specified amount of bytes in the buffer - virtual void* alloc(size_t bytes) { - alignNextAlloc(); - if (!canAlloc(bytes)) return NULL; - return doAlloc(bytes); - } - - // Resets the buffer. - // USE WITH CAUTION: this invalidates all previously allocated data - void clear() { - _size = 0; - } - - String startString() { - return String(this); - } - - protected: - ~StaticJsonBufferBase() {} - - private: - void alignNextAlloc() { - _size = round_size_up(_size); - } - - bool canAlloc(size_t bytes) const { - return _size + bytes <= _capacity; - } - - void* doAlloc(size_t bytes) { - void* p = &_buffer[_size]; - _size += bytes; - return p; - } - - char* _buffer; - size_t _capacity; - size_t _size; -}; -} - -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wnon-virtual-dtor" -#elif defined(__GNUC__) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -#pragma GCC diagnostic push -#endif -#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" -#endif - -// Implements a JsonBuffer with fixed memory allocation. -// The template paramenter CAPACITY specifies the capacity of the buffer in -// bytes. -template -class StaticJsonBuffer : public Internals::StaticJsonBufferBase { - public: - explicit StaticJsonBuffer() - : Internals::StaticJsonBufferBase(_buffer, CAPACITY) {} - - private: - char _buffer[CAPACITY]; -}; -} - -#if defined(__clang__) -#pragma clang diagnostic pop -#elif defined(__GNUC__) -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) -#pragma GCC diagnostic pop -#endif -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/ArduinoStream.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/ArduinoStream.hpp deleted file mode 100644 index 5db0852b8..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/ArduinoStream.hpp +++ /dev/null @@ -1,61 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#if ARDUINOJSON_ENABLE_ARDUINO_STREAM - -#include - -namespace ArduinoJson { -namespace Internals { - -struct ArduinoStreamTraits { - class Reader { - Stream& _stream; - char _current, _next; - - public: - Reader(Stream& stream) : _stream(stream), _current(0), _next(0) {} - - void move() { - _current = _next; - _next = 0; - } - - char current() { - if (!_current) _current = read(); - return _current; - } - - char next() { - // assumes that current() has been called - if (!_next) _next = read(); - return _next; - } - - private: - char read() { - // don't use _stream.read() as it ignores the timeout - char c = 0; - _stream.readBytes(&c, 1); - return c; - } - }; - - static const bool has_append = false; - static const bool has_equals = false; -}; - -template -struct StringTraits< - TStream, - // match any type that is derived from Stream: - typename EnableIf< - IsBaseOf::type>::value>::type> - : ArduinoStreamTraits {}; -} -} - -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/CharPointer.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/CharPointer.hpp deleted file mode 100644 index 98896ccfb..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/CharPointer.hpp +++ /dev/null @@ -1,64 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -template -struct CharPointerTraits { - class Reader { - const TChar* _ptr; - - public: - Reader(const TChar* ptr) - : _ptr(ptr ? ptr : reinterpret_cast("")) {} - - void move() { - ++_ptr; - } - - char current() const { - return char(_ptr[0]); - } - - char next() const { - return char(_ptr[1]); - } - }; - - static bool equals(const TChar* str, const char* expected) { - const char* actual = reinterpret_cast(str); - if (!actual || !expected) return actual == expected; - return strcmp(actual, expected) == 0; - } - - static bool is_null(const TChar* str) { - return !str; - } - - typedef const char* duplicate_t; - - template - static duplicate_t duplicate(const TChar* str, Buffer* buffer) { - if (!str) return NULL; - size_t size = strlen(reinterpret_cast(str)) + 1; - void* dup = buffer->alloc(size); - if (dup != NULL) memcpy(dup, str, size); - return static_cast(dup); - } - - static const bool has_append = false; - static const bool has_equals = true; - static const bool should_duplicate = !IsConst::value; -}; - -// char*, unsigned char*, signed char* -// const char*, const unsigned char*, const signed char* -template -struct StringTraits::value>::type> - : CharPointerTraits {}; -} // namespace Internals -} // namespace ArduinoJson diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/FlashString.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/FlashString.hpp deleted file mode 100644 index 0701b9ba2..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/FlashString.hpp +++ /dev/null @@ -1,61 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#if ARDUINOJSON_ENABLE_PROGMEM - -namespace ArduinoJson { -namespace Internals { -template <> -struct StringTraits { - class Reader { - const char* _ptr; - - public: - Reader(const __FlashStringHelper* ptr) - : _ptr(reinterpret_cast(ptr)) {} - - void move() { - _ptr++; - } - - char current() const { - return pgm_read_byte_near(_ptr); - } - - char next() const { - return pgm_read_byte_near(_ptr + 1); - } - }; - - static bool equals(const __FlashStringHelper* str, const char* expected) { - const char* actual = reinterpret_cast(str); - if (!actual || !expected) return actual == expected; - return strcmp_P(expected, actual) == 0; - } - - static bool is_null(const __FlashStringHelper* str) { - return !str; - } - - typedef const char* duplicate_t; - - template - static duplicate_t duplicate(const __FlashStringHelper* str, Buffer* buffer) { - if (!str) return NULL; - size_t size = strlen_P((const char*)str) + 1; - void* dup = buffer->alloc(size); - if (dup != NULL) memcpy_P(dup, (const char*)str, size); - return static_cast(dup); - } - - static const bool has_append = false; - static const bool has_equals = true; - static const bool should_duplicate = true; -}; -} // namespace Internals -} // namespace ArduinoJson - -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StdStream.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StdStream.hpp deleted file mode 100644 index 227c74406..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StdStream.hpp +++ /dev/null @@ -1,60 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#if ARDUINOJSON_ENABLE_STD_STREAM - -#include - -namespace ArduinoJson { -namespace Internals { - -struct StdStreamTraits { - class Reader { - std::istream& _stream; - char _current, _next; - - public: - Reader(std::istream& stream) : _stream(stream), _current(0), _next(0) {} - - void move() { - _current = _next; - _next = 0; - } - - char current() { - if (!_current) _current = read(); - return _current; - } - - char next() { - // assumes that current() has been called - if (!_next) _next = read(); - return _next; - } - - private: - Reader& operator=(const Reader&); // Visual Studio C4512 - - char read() { - return _stream.eof() ? '\0' : static_cast(_stream.get()); - } - }; - - static const bool has_append = false; - static const bool has_equals = false; -}; - -template -struct StringTraits< - TStream, - // match any type that is derived from std::istream: - typename EnableIf::type>::value>::type> - : StdStreamTraits {}; -} -} - -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StdString.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StdString.hpp deleted file mode 100644 index 35f4461d8..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StdString.hpp +++ /dev/null @@ -1,77 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#if ARDUINOJSON_ENABLE_STD_STRING || ARDUINOJSON_ENABLE_ARDUINO_STRING - -#if ARDUINOJSON_ENABLE_ARDUINO_STRING -#include -#endif - -#if ARDUINOJSON_ENABLE_STD_STRING -#include -#endif - -namespace ArduinoJson { -namespace Internals { - -template -struct StdStringTraits { - typedef const char* duplicate_t; - - template - static duplicate_t duplicate(const TString& str, Buffer* buffer) { - if (!str.c_str()) return NULL; // <- Arduino string can return NULL - size_t size = str.length() + 1; - void* dup = buffer->alloc(size); - if (dup != NULL) memcpy(dup, str.c_str(), size); - return static_cast(dup); - } - - static bool is_null(const TString& str) { - // Arduino's String::c_str() can return NULL - return !str.c_str(); - } - - struct Reader : CharPointerTraits::Reader { - Reader(const TString& str) : CharPointerTraits::Reader(str.c_str()) {} - }; - - static bool equals(const TString& str, const char* expected) { - // Arduino's String::c_str() can return NULL - const char* actual = str.c_str(); - if (!actual || !expected) return actual == expected; - return 0 == strcmp(actual, expected); - } - - static void append(TString& str, char c) { - str += c; - } - - static void append(TString& str, const char* s) { - str += s; - } - - static const bool has_append = true; - static const bool has_equals = true; - static const bool should_duplicate = true; -}; - -#if ARDUINOJSON_ENABLE_ARDUINO_STRING -template <> -struct StringTraits : StdStringTraits {}; -template <> -struct StringTraits : StdStringTraits { -}; -#endif - -#if ARDUINOJSON_ENABLE_STD_STRING -template <> -struct StringTraits : StdStringTraits {}; -#endif -} // namespace Internals -} // namespace ArduinoJson - -#endif diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StringTraits.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StringTraits.hpp deleted file mode 100644 index dd5694b2e..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/StringTraits/StringTraits.hpp +++ /dev/null @@ -1,36 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include -#include "../Configuration.hpp" -#include "../TypeTraits/EnableIf.hpp" -#include "../TypeTraits/IsBaseOf.hpp" -#include "../TypeTraits/IsChar.hpp" -#include "../TypeTraits/IsConst.hpp" -#include "../TypeTraits/RemoveReference.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -struct StringTraits { - static const bool has_append = false; - static const bool has_equals = false; -}; - -template -struct StringTraits : StringTraits {}; - -template -struct StringTraits : StringTraits {}; -} -} - -#include "ArduinoStream.hpp" -#include "CharPointer.hpp" -#include "FlashString.hpp" -#include "StdStream.hpp" -#include "StdString.hpp" diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/EnableIf.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/EnableIf.hpp deleted file mode 100644 index 83fc5e07f..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/EnableIf.hpp +++ /dev/null @@ -1,19 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that return the type T if Condition is true. -template -struct EnableIf {}; - -template -struct EnableIf { - typedef T type; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/FloatTraits.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/FloatTraits.hpp deleted file mode 100644 index 648cc82fd..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/FloatTraits.hpp +++ /dev/null @@ -1,171 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include -#include // for size_t -#include "../Configuration.hpp" -#include "../Polyfills/math.hpp" - -namespace ArduinoJson { -namespace Internals { - -template -struct FloatTraits {}; - -template -struct FloatTraits { - typedef int64_t mantissa_type; - static const short mantissa_bits = 52; - static const mantissa_type mantissa_max = - (static_cast(1) << mantissa_bits) - 1; - - typedef int16_t exponent_type; - static const exponent_type exponent_max = 308; - - template - static T make_float(T m, TExponent e) { - if (e > 0) { - for (uint8_t index = 0; e != 0; index++) { - if (e & 1) m *= positiveBinaryPowerOfTen(index); - e >>= 1; - } - } else { - e = TExponent(-e); - for (uint8_t index = 0; e != 0; index++) { - if (e & 1) m *= negativeBinaryPowerOfTen(index); - e >>= 1; - } - } - return m; - } - - static T positiveBinaryPowerOfTen(int index) { - static T factors[] = { - 1e1, - 1e2, - 1e4, - 1e8, - 1e16, - forge(0x4693B8B5, 0xB5056E17), // 1e32 - forge(0x4D384F03, 0xE93FF9F5), // 1e64 - forge(0x5A827748, 0xF9301D32), // 1e128 - forge(0x75154FDD, 0x7F73BF3C) // 1e256 - }; - return factors[index]; - } - - static T negativeBinaryPowerOfTen(int index) { - static T factors[] = { - forge(0x3FB99999, 0x9999999A), // 1e-1 - forge(0x3F847AE1, 0x47AE147B), // 1e-2 - forge(0x3F1A36E2, 0xEB1C432D), // 1e-4 - forge(0x3E45798E, 0xE2308C3A), // 1e-8 - forge(0x3C9CD2B2, 0x97D889BC), // 1e-16 - forge(0x3949F623, 0xD5A8A733), // 1e-32 - forge(0x32A50FFD, 0x44F4A73D), // 1e-64 - forge(0x255BBA08, 0xCF8C979D), // 1e-128 - forge(0x0AC80628, 0x64AC6F43) // 1e-256 - }; - return factors[index]; - } - - static T negativeBinaryPowerOfTenPlusOne(int index) { - static T factors[] = { - 1e0, - forge(0x3FB99999, 0x9999999A), // 1e-1 - forge(0x3F50624D, 0xD2F1A9FC), // 1e-3 - forge(0x3E7AD7F2, 0x9ABCAF48), // 1e-7 - forge(0x3CD203AF, 0x9EE75616), // 1e-15 - forge(0x398039D6, 0x65896880), // 1e-31 - forge(0x32DA53FC, 0x9631D10D), // 1e-63 - forge(0x25915445, 0x81B7DEC2), // 1e-127 - forge(0x0AFE07B2, 0x7DD78B14) // 1e-255 - }; - return factors[index]; - } - - static T nan() { - return forge(0x7ff80000, 0x00000000); - } - - static T inf() { - return forge(0x7ff00000, 0x00000000); - } - - // constructs a double floating point values from its binary representation - // we use this function to workaround platforms with single precision literals - // (for example, when -fsingle-precision-constant is passed to GCC) - static T forge(uint32_t msb, uint32_t lsb) { - union { - uint64_t integerBits; - T floatBits; - }; - integerBits = (uint64_t(msb) << 32) | lsb; - return floatBits; - } -}; - -template -struct FloatTraits { - typedef int32_t mantissa_type; - static const short mantissa_bits = 23; - static const mantissa_type mantissa_max = - (static_cast(1) << mantissa_bits) - 1; - - typedef int8_t exponent_type; - static const exponent_type exponent_max = 38; - - template - static T make_float(T m, TExponent e) { - if (e > 0) { - for (uint8_t index = 0; e != 0; index++) { - if (e & 1) m *= positiveBinaryPowerOfTen(index); - e >>= 1; - } - } else { - e = -e; - for (uint8_t index = 0; e != 0; index++) { - if (e & 1) m *= negativeBinaryPowerOfTen(index); - e >>= 1; - } - } - return m; - } - - static T positiveBinaryPowerOfTen(int index) { - static T factors[] = {1e1f, 1e2f, 1e4f, 1e8f, 1e16f, 1e32f}; - return factors[index]; - } - - static T negativeBinaryPowerOfTen(int index) { - static T factors[] = {1e-1f, 1e-2f, 1e-4f, 1e-8f, 1e-16f, 1e-32f}; - return factors[index]; - } - - static T negativeBinaryPowerOfTenPlusOne(int index) { - static T factors[] = {1e0f, 1e-1f, 1e-3f, 1e-7f, 1e-15f, 1e-31f}; - return factors[index]; - } - - static T forge(uint32_t bits) { - union { - uint32_t integerBits; - T floatBits; - }; - integerBits = bits; - return floatBits; - } - - static T nan() { - return forge(0x7fc00000); - } - - static T inf() { - return forge(0x7f800000); - } -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsArray.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsArray.hpp deleted file mode 100644 index 259923115..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsArray.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that return the type T without the const modifier -template -struct IsArray { - static const bool value = false; -}; -template -struct IsArray { - static const bool value = true; -}; -template -struct IsArray { - static const bool value = true; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsBaseOf.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsBaseOf.hpp deleted file mode 100644 index bf24e965e..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsBaseOf.hpp +++ /dev/null @@ -1,27 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if Derived inherits from TBase is an -// integral type. -template -class IsBaseOf { - protected: // <- to avoid GCC's "all member functions in class are private" - typedef char Yes[1]; - typedef char No[2]; - - static Yes &probe(const TBase *); - static No &probe(...); - - public: - enum { - value = sizeof(probe(reinterpret_cast(0))) == sizeof(Yes) - }; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsChar.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsChar.hpp deleted file mode 100644 index d97cec213..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsChar.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "IsSame.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if T is a charater -template -struct IsChar { - static const bool value = IsSame::value || - IsSame::value || - IsSame::value; -}; - -template -struct IsChar : IsChar {}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsConst.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsConst.hpp deleted file mode 100644 index 512ee5ca0..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsConst.hpp +++ /dev/null @@ -1,21 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that return the type T without the const modifier -template -struct IsConst { - static const bool value = false; -}; - -template -struct IsConst { - static const bool value = true; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsFloatingPoint.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsFloatingPoint.hpp deleted file mode 100644 index e41a6824c..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsFloatingPoint.hpp +++ /dev/null @@ -1,18 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "IsSame.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if T is a floating point type -template -struct IsFloatingPoint { - static const bool value = IsSame::value || IsSame::value; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsIntegral.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsIntegral.hpp deleted file mode 100644 index 17ae5f284..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsIntegral.hpp +++ /dev/null @@ -1,26 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "IsSame.hpp" -#include "IsSignedIntegral.hpp" -#include "IsUnsignedIntegral.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if T is an integral type. -template -struct IsIntegral { - static const bool value = IsSignedIntegral::value || - IsUnsignedIntegral::value || - IsSame::value; - // CAUTION: differs from std::is_integral as it doesn't include bool -}; - -template -struct IsIntegral : IsIntegral {}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsSame.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsSame.hpp deleted file mode 100644 index 06567c93b..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsSame.hpp +++ /dev/null @@ -1,21 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if types T and U are the same. -template -struct IsSame { - static const bool value = false; -}; - -template -struct IsSame { - static const bool value = true; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsSignedIntegral.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsSignedIntegral.hpp deleted file mode 100644 index 7334eb9c7..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsSignedIntegral.hpp +++ /dev/null @@ -1,28 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" -#include "IsSame.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if T is an integral type. -template -struct IsSignedIntegral { - static const bool value = - IsSame::value || IsSame::value || - IsSame::value || IsSame::value || -#if ARDUINOJSON_USE_LONG_LONG - IsSame::value || -#endif -#if ARDUINOJSON_USE_INT64 - IsSame::value || -#endif - false; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp deleted file mode 100644 index 938423f5c..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsUnsignedIntegral.hpp +++ /dev/null @@ -1,28 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "../Configuration.hpp" -#include "IsSame.hpp" - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that returns true if T is an integral type. -template -struct IsUnsignedIntegral { - static const bool value = - IsSame::value || IsSame::value || - IsSame::value || IsSame::value || -#if ARDUINOJSON_USE_LONG_LONG - IsSame::value || -#endif -#if ARDUINOJSON_USE_INT64 - IsSame::value || -#endif - false; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsVariant.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsVariant.hpp deleted file mode 100644 index f8b299f7a..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/IsVariant.hpp +++ /dev/null @@ -1,17 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#include "IsBaseOf.hpp" - -namespace ArduinoJson { -namespace Internals { - -class JsonVariantTag {}; - -template -struct IsVariant : IsBaseOf {}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/RemoveConst.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/RemoveConst.hpp deleted file mode 100644 index 39d4cb5a5..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/RemoveConst.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that return the type T without the const modifier -template -struct RemoveConst { - typedef T type; -}; -template -struct RemoveConst { - typedef T type; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/RemoveReference.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/RemoveReference.hpp deleted file mode 100644 index 395a12889..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/TypeTraits/RemoveReference.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -namespace ArduinoJson { -namespace Internals { - -// A meta-function that return the type T without the reference modifier. -template -struct RemoveReference { - typedef T type; -}; -template -struct RemoveReference { - typedef T type; -}; -} -} diff --git a/lib/ArduinoJson-5.13.4/src/ArduinoJson/version.hpp b/lib/ArduinoJson-5.13.4/src/ArduinoJson/version.hpp deleted file mode 100644 index 34c78461d..000000000 --- a/lib/ArduinoJson-5.13.4/src/ArduinoJson/version.hpp +++ /dev/null @@ -1,10 +0,0 @@ -// ArduinoJson - arduinojson.org -// Copyright Benoit Blanchon 2014-2018 -// MIT License - -#pragma once - -#define ARDUINOJSON_VERSION "5.13.4" -#define ARDUINOJSON_VERSION_MAJOR 5 -#define ARDUINOJSON_VERSION_MINOR 13 -#define ARDUINOJSON_VERSION_REVISION 4 diff --git a/lib/C2Programmer-1.0.0/src/c2.cpp b/lib/C2Programmer-1.0.0/src/c2.cpp index 22bfbaedd..fd7e41af0 100644 --- a/lib/C2Programmer-1.0.0/src/c2.cpp +++ b/lib/C2Programmer-1.0.0/src/c2.cpp @@ -204,13 +204,33 @@ uint8_t c2_reset() { return C2_SUCCESS; } -uint8_t c2_programming_init() { +uint8_t c2_programming_init(uint8_t devid) { c2_reset(); c2_address_write(C2FPCTL); C2_DATA_WRITE_AND_CHECK(C2FPCTL_ENABLE0, 1); C2_DATA_WRITE_AND_CHECK(C2FPCTL_CORE_HALT, 1); C2_DATA_WRITE_AND_CHECK(C2FPCTL_ENABLE1, 1) C2_DELAY_MS(21); + + // device specific initialization, see https://www.silabs.com/documents/public/application-notes/AN127.pdf + switch (devid) { + case C2_DEVID_UNKNOWN: + break; + case C2_DEVID_EFM8BB1: + case C2_DEVID_EFM8BB2: + case C2_DEVID_EFM8BB3: // C2_DEVID_EFM8LB1 is the same + c2_address_write(0xFF); + C2_DATA_WRITE_AND_CHECK(0x80, 1); + C2_DELAY_US(5); + c2_address_write(0xEF); + C2_DATA_WRITE_AND_CHECK(0x02, 1); + c2_address_write(0xA9); + C2_DATA_WRITE_AND_CHECK(0x00, 1); + break; + default: + return C2_BROKEN_LINK; + } + return C2_SUCCESS; } diff --git a/lib/C2Programmer-1.0.0/src/c2.h b/lib/C2Programmer-1.0.0/src/c2.h index bc744d02f..0b80c95f3 100644 --- a/lib/C2Programmer-1.0.0/src/c2.h +++ b/lib/C2Programmer-1.0.0/src/c2.h @@ -103,6 +103,13 @@ inline void C2D_enable(bool oe) { #define C2_INBUSY 0x02 #define C2_OUTREADY 0x01 +// Device families (https://www.silabs.com/documents/public/application-notes/AN127.pdf) +#define C2_DEVID_UNKNOWN 0x00 +#define C2_DEVID_EFM8BB1 0x30 +#define C2_DEVID_EFM8BB2 0x32 +#define C2_DEVID_EFM8BB3 0x34 +#define C2_DEVID_EFM8LB1 0x34 + // Layer 1: C2 Programmig Interface (PI) Register access void c2_address_write(uint8_t address); uint8_t c2_address_read(); @@ -125,7 +132,7 @@ inline uint8_t c2_data_read(uint8_t &d, uint8_t bytes=1) { // Layer 2: Operations uint8_t c2_reset(); -uint8_t c2_programming_init(); +uint8_t c2_programming_init(uint8_t devid); uint8_t c2_block_write(uint32_t address, uint8_t *data, uint8_t len); uint8_t c2_block_read(uint32_t address, uint8_t *data, uint8_t len); uint8_t c2_eeprom_read(uint32_t address, uint8_t *data, uint8_t len); diff --git a/lib/OneWire-2.3.3.06/OneWire.cpp b/lib/OneWire-Stickbreaker-20190506-1.1/OneWire.cpp similarity index 86% rename from lib/OneWire-2.3.3.06/OneWire.cpp rename to lib/OneWire-Stickbreaker-20190506-1.1/OneWire.cpp index 5c9945d51..4476ff53a 100644 --- a/lib/OneWire-2.3.3.06/OneWire.cpp +++ b/lib/OneWire-Stickbreaker-20190506-1.1/OneWire.cpp @@ -32,6 +32,17 @@ private email about OneWire). OneWire is now very mature code. No changes other than adding definitions for newer hardware support are anticipated. +======= +Version 2.3.3 ESP32 Stickbreaker 06MAY2019 + Add a #ifdef to isolate ESP32 mods +Version 2.3.1 ESP32 everslick 30APR2018 + add IRAM_ATTR attribute to write_bit/read_bit to fix icache miss delay + https://github.com/espressif/arduino-esp32/issues/1335 + +Version 2.3 ESP32 stickbreaker 28DEC2017 + adjust to use portENTER_CRITICAL(&mux) instead of noInterrupts(); + adjust to use portEXIT_CRITICAL(&mux) instead of Interrupts(); + Version 2.3: Unknown chip fallback mode, Roger Clark Teensy-LC compatibility, Paul Stoffregen @@ -141,14 +152,18 @@ sample code bearing this copyright. #include "OneWire.h" +#ifdef ARDUINO_ARCH_ESP32 +#define noInterrupts() {portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;portENTER_CRITICAL(&mux) +#define interrupts() portEXIT_CRITICAL(&mux);} +#endif OneWire::OneWire(uint8_t pin) { - pinMode(pin, INPUT); - bitmask = PIN_TO_BITMASK(pin); - baseReg = PIN_TO_BASEREG(pin); + pinMode(pin, INPUT); + bitmask = PIN_TO_BITMASK(pin); + baseReg = PIN_TO_BASEREG(pin); #if ONEWIRE_SEARCH - reset_search(); + reset_search(); #endif } @@ -159,60 +174,65 @@ OneWire::OneWire(uint8_t pin) // // Returns 1 if a device asserted a presence pulse, 0 otherwise. // +#ifdef ARDUINO_ARCH_ESP32 +uint8_t IRAM_ATTR OneWire::reset(void) +#else uint8_t OneWire::reset(void) +#endif { - IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; - volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; - uint8_t r; - uint8_t retries = 125; - - noInterrupts(); - DIRECT_MODE_INPUT(reg, mask); - interrupts(); - // wait until the wire is high... just in case - do { - if (--retries == 0) return 0; - delayMicroseconds(2); - } while ( !DIRECT_READ(reg, mask)); - - noInterrupts(); - DIRECT_WRITE_LOW(reg, mask); - DIRECT_MODE_OUTPUT(reg, mask); // drive output low - interrupts(); - delayMicroseconds(480); - noInterrupts(); - DIRECT_MODE_INPUT(reg, mask); // allow it to float - delayMicroseconds(70); - r = !DIRECT_READ(reg, mask); - interrupts(); - delayMicroseconds(410); - return r; + IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; + volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; + uint8_t r; + uint8_t retries = 125; + noInterrupts(); + DIRECT_MODE_INPUT(reg, mask); + interrupts(); + // wait until the wire is high... just in case + do { + if (--retries == 0) return 0; + delayMicroseconds(2); + } while ( !DIRECT_READ(reg, mask)); + + noInterrupts(); + DIRECT_WRITE_LOW(reg, mask); + DIRECT_MODE_OUTPUT(reg, mask); // drive output low + delayMicroseconds(480); + DIRECT_MODE_INPUT(reg, mask); // allow it to float + delayMicroseconds(70); + r = !DIRECT_READ(reg, mask); + interrupts(); + delayMicroseconds(410); + return r; } // // Write a bit. Port and bit is used to cut lookup time and provide // more certain timing. // +#ifdef ARDUINO_ARCH_ESP32 +void IRAM_ATTR OneWire::write_bit(uint8_t v) +#else void OneWire::write_bit(uint8_t v) +#endif { IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; if (v & 1) { - noInterrupts(); + noInterrupts(); DIRECT_WRITE_LOW(reg, mask); DIRECT_MODE_OUTPUT(reg, mask); // drive output low delayMicroseconds(10); DIRECT_WRITE_HIGH(reg, mask); // drive output high - interrupts(); + interrupts(); delayMicroseconds(55); } else { - noInterrupts(); + noInterrupts(); DIRECT_WRITE_LOW(reg, mask); DIRECT_MODE_OUTPUT(reg, mask); // drive output low delayMicroseconds(65); DIRECT_WRITE_HIGH(reg, mask); // drive output high - interrupts(); + interrupts(); delayMicroseconds(5); } } @@ -221,20 +241,24 @@ void OneWire::write_bit(uint8_t v) // Read a bit. Port and bit is used to cut lookup time and provide // more certain timing. // +#ifdef ARDUINO_ARCH_ESP32 +uint8_t IRAM_ATTR OneWire::read_bit(void) +#else uint8_t OneWire::read_bit(void) +#endif { IO_REG_TYPE mask IO_REG_MASK_ATTR = bitmask; volatile IO_REG_TYPE *reg IO_REG_BASE_ATTR = baseReg; uint8_t r; - noInterrupts(); + noInterrupts(); DIRECT_MODE_OUTPUT(reg, mask); DIRECT_WRITE_LOW(reg, mask); delayMicroseconds(3); DIRECT_MODE_INPUT(reg, mask); // let pin float, pull up will raise delayMicroseconds(10); r = DIRECT_READ(reg, mask); - interrupts(); + interrupts(); delayMicroseconds(53); return r; } @@ -247,17 +271,17 @@ uint8_t OneWire::read_bit(void) // other mishap. // void OneWire::write(uint8_t v, uint8_t power /* = 0 */) { - uint8_t bitMask; + uint8_t bitMask; - for (bitMask = 0x01; bitMask; bitMask <<= 1) { - OneWire::write_bit( (bitMask & v)?1:0); - } - if ( !power) { - noInterrupts(); - DIRECT_MODE_INPUT(baseReg, bitmask); - DIRECT_WRITE_LOW(baseReg, bitmask); - interrupts(); - } + for (bitMask = 0x01; bitMask; bitMask <<= 1) { + OneWire::write_bit( (bitMask & v)?1:0); + } + if ( !power) { + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + DIRECT_WRITE_LOW(baseReg, bitmask); + interrupts(); + } } void OneWire::write_bytes(const uint8_t *buf, uint16_t count, bool power /* = 0 */) { @@ -279,7 +303,7 @@ uint8_t OneWire::read() { uint8_t r = 0; for (bitMask = 0x01; bitMask; bitMask <<= 1) { - if ( OneWire::read_bit()) r |= bitMask; + if ( OneWire::read_bit()) r |= bitMask; } return r; } @@ -311,9 +335,9 @@ void OneWire::skip() void OneWire::depower() { - noInterrupts(); - DIRECT_MODE_INPUT(baseReg, bitmask); - interrupts(); + noInterrupts(); + DIRECT_MODE_INPUT(baseReg, bitmask); + interrupts(); } #if ONEWIRE_SEARCH @@ -391,13 +415,12 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) LastFamilyDiscrepancy = 0; return FALSE; } - // issue the search command if (search_mode == true) { write(0xF0); // NORMAL SEARCH } else { write(0xEC); // CONDITIONAL SEARCH - } + } // loop to do the search do @@ -405,7 +428,7 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) // read a bit and its complement id_bit = read_bit(); cmp_id_bit = read_bit(); - + // check for no devices on 1-wire if ((id_bit == 1) && (cmp_id_bit == 1)) break; @@ -459,7 +482,6 @@ uint8_t OneWire::search(uint8_t *newAddr, bool search_mode /* = true */) } } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 - // if the search was successful then if (!(id_bit_number < 65)) { @@ -524,12 +546,12 @@ static const uint8_t PROGMEM dscrc_table[] = { // uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) { - uint8_t crc = 0; + uint8_t crc = 0; - while (len--) { - crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); - } - return crc; + while (len--) { + crc = pgm_read_byte(dscrc_table + (crc ^ *addr++)); + } + return crc; } #else // @@ -538,22 +560,22 @@ uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) // uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len) { - uint8_t crc = 0; + uint8_t crc = 0; - while (len--) { + while (len--) { #if defined(__AVR__) - crc = _crc_ibutton_update(crc, *addr++); + crc = _crc_ibutton_update(crc, *addr++); #else - uint8_t inbyte = *addr++; - for (uint8_t i = 8; i; i--) { - uint8_t mix = (crc ^ inbyte) & 0x01; - crc >>= 1; - if (mix) crc ^= 0x8C; - inbyte >>= 1; - } + uint8_t inbyte = *addr++; + for (uint8_t i = 8; i; i--) { + uint8_t mix = (crc ^ inbyte) & 0x01; + crc >>= 1; + if (mix) crc ^= 0x8C; + inbyte >>= 1; + } #endif - } - return crc; + } + return crc; } #endif @@ -594,4 +616,10 @@ uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc) } #endif + +#ifdef ARDUINO_ARCH_ESP32 +#undef noInterrupts() +#undef interrupts() +#endif + #endif diff --git a/lib/OneWire-2.3.3.06/OneWire.h b/lib/OneWire-Stickbreaker-20190506-1.1/OneWire.h similarity index 95% rename from lib/OneWire-2.3.3.06/OneWire.h rename to lib/OneWire-Stickbreaker-20190506-1.1/OneWire.h index 47bf7c1cb..119ac5413 100644 --- a/lib/OneWire-2.3.3.06/OneWire.h +++ b/lib/OneWire-Stickbreaker-20190506-1.1/OneWire.h @@ -275,18 +275,18 @@ void directModeOutput(IO_REG_TYPE pin) #include "portable.h" #include "avr/pgmspace.h" -#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) -#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) -#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) -#define DIR_OFFSET_SS 0x01 -#define DIR_OFFSET_SOC 0x04 -#define EXT_PORT_OFFSET_SS 0x0A -#define EXT_PORT_OFFSET_SOC 0x50 +#define GPIO_ID(pin) (g_APinDescription[pin].ulGPIOId) +#define GPIO_TYPE(pin) (g_APinDescription[pin].ulGPIOType) +#define GPIO_BASE(pin) (g_APinDescription[pin].ulGPIOBase) +#define DIR_OFFSET_SS 0x01 +#define DIR_OFFSET_SOC 0x04 +#define EXT_PORT_OFFSET_SS 0x0A +#define EXT_PORT_OFFSET_SOC 0x50 /* GPIO registers base address */ -#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) -#define PIN_TO_BITMASK(pin) pin -#define IO_REG_TYPE uint32_t +#define PIN_TO_BASEREG(pin) ((volatile uint32_t *)g_APinDescription[pin].ulGPIOBase) +#define PIN_TO_BITMASK(pin) pin +#define IO_REG_TYPE uint32_t #define IO_REG_BASE_ATTR #define IO_REG_MASK_ATTR @@ -307,7 +307,7 @@ void directModeInput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG((((IO_REG_TYPE)base) + DIR_OFFSET_SS)) & ~(0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); } else { MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) &= ~(0x01 << GPIO_ID(pin)); } @@ -318,7 +318,7 @@ void directModeOutput(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) { if (SS_GPIO == GPIO_TYPE(pin)) { WRITE_ARC_REG(READ_ARC_REG(((IO_REG_TYPE)(base) + DIR_OFFSET_SS)) | (0x01 << GPIO_ID(pin)), - ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); + ((IO_REG_TYPE)(base) + DIR_OFFSET_SS)); } else { MMIO_REG_VAL_FROM_BASE((IO_REG_TYPE)base, DIR_OFFSET_SOC) |= (0x01 << GPIO_ID(pin)); } @@ -344,11 +344,11 @@ void directWriteHigh(volatile IO_REG_TYPE *base, IO_REG_TYPE pin) } } -#define DIRECT_READ(base, pin) directRead(base, pin) -#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) -#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) -#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) -#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) +#define DIRECT_READ(base, pin) directRead(base, pin) +#define DIRECT_MODE_INPUT(base, pin) directModeInput(base, pin) +#define DIRECT_MODE_OUTPUT(base, pin) directModeOutput(base, pin) +#define DIRECT_WRITE_LOW(base, pin) directWriteLow(base, pin) +#define DIRECT_WRITE_HIGH(base, pin) directWriteHigh(base, pin) #elif defined(__riscv) diff --git a/lib/OneWire-Stickbreaker-20190506-1.1/README.md b/lib/OneWire-Stickbreaker-20190506-1.1/README.md new file mode 100644 index 000000000..fcc566fbf --- /dev/null +++ b/lib/OneWire-Stickbreaker-20190506-1.1/README.md @@ -0,0 +1,11 @@ +# OneWire library + A modification of the Arduino OneWire library maintained by @PaulStoffregen. This modifications supports the ESP32 under the Arduino-esp32 Environment. + + No changes are required for compatibility with Arduino coding. + +Original Source is Paul's 2.3 version. Forked 28DEC2017 + +@stickbreaker +V2.3.1 30APR2018 add IRAM_ATTR to read_bit() write_bit() to solve ICache miss timing failure. + thanks @everslick re: https://github.com/espressif/arduino-esp32/issues/1335 +V2.3 28DEC2017 original mods to support ESP32 diff --git a/lib/OneWire-2.3.3.06/examples/DS18x20_Temperature/DS18x20_Temperature.pde b/lib/OneWire-Stickbreaker-20190506-1.1/examples/DS18x20_Temperature/DS18x20_Temperature.pde similarity index 100% rename from lib/OneWire-2.3.3.06/examples/DS18x20_Temperature/DS18x20_Temperature.pde rename to lib/OneWire-Stickbreaker-20190506-1.1/examples/DS18x20_Temperature/DS18x20_Temperature.pde diff --git a/lib/OneWire-2.3.3.06/examples/DS2408_Switch/DS2408_Switch.pde b/lib/OneWire-Stickbreaker-20190506-1.1/examples/DS2408_Switch/DS2408_Switch.pde similarity index 100% rename from lib/OneWire-2.3.3.06/examples/DS2408_Switch/DS2408_Switch.pde rename to lib/OneWire-Stickbreaker-20190506-1.1/examples/DS2408_Switch/DS2408_Switch.pde diff --git a/lib/OneWire-2.3.3.06/examples/DS250x_PROM/DS250x_PROM.pde b/lib/OneWire-Stickbreaker-20190506-1.1/examples/DS250x_PROM/DS250x_PROM.pde similarity index 100% rename from lib/OneWire-2.3.3.06/examples/DS250x_PROM/DS250x_PROM.pde rename to lib/OneWire-Stickbreaker-20190506-1.1/examples/DS250x_PROM/DS250x_PROM.pde diff --git a/lib/OneWire-2.3.3.06/keywords.txt b/lib/OneWire-Stickbreaker-20190506-1.1/keywords.txt similarity index 100% rename from lib/OneWire-2.3.3.06/keywords.txt rename to lib/OneWire-Stickbreaker-20190506-1.1/keywords.txt diff --git a/lib/OneWire-2.3.3.06/library.json b/lib/OneWire-Stickbreaker-20190506-1.1/library.json similarity index 100% rename from lib/OneWire-2.3.3.06/library.json rename to lib/OneWire-Stickbreaker-20190506-1.1/library.json diff --git a/lib/OneWire-2.3.3.06/library.properties b/lib/OneWire-Stickbreaker-20190506-1.1/library.properties similarity index 83% rename from lib/OneWire-2.3.3.06/library.properties rename to lib/OneWire-Stickbreaker-20190506-1.1/library.properties index cd8fc05f7..2a8b08e53 100644 --- a/lib/OneWire-2.3.3.06/library.properties +++ b/lib/OneWire-Stickbreaker-20190506-1.1/library.properties @@ -3,8 +3,8 @@ version=2.3.3 author=Jim Studt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangel, Guillermo Lovato, Paul Stoffregen, Scott Roberts, Bertrik Sikken, Mark Tillotson, Ken Butcher, Roger Clark, Love Nystrom maintainer=Paul Stoffregen sentence=Access 1-wire temperature sensors, memory and other chips. -paragraph= +paragraph= Mod of Paul Stoffregen code to support ESP32 category=Communication url=http://www.pjrc.com/teensy/td_libs_OneWire.html -architectures=* +architectures=esp32 diff --git a/lib/jsmn-shadinger-1.0/README.md b/lib/jsmn-shadinger-1.0/README.md new file mode 100644 index 000000000..bf5b59565 --- /dev/null +++ b/lib/jsmn-shadinger-1.0/README.md @@ -0,0 +1,195 @@ +# JSMN lightweight JSON parser for Tasmota + +Intro: this library uses the JSMN in-place JSON parser. +See https://github.com/zserge/jsmn and https://zserge.com/jsmn/ + +It is proposed as a replacement for ArduinoJson. It has less features, does only parsing but does it in a very efficient way. + +## Benefits + +First, the memory impact is very low: 4 bytes per token and no need to add an extra buffer for values. +Second, the code is much smaller than ArduinoJson by 5-7KB. + +## How to use + +`{"Device":"0x1234","Power":true,"Temperature":25.5}` + +The JSON above is split into 8 tokens, and requires 32 bytes of dynamic memory. + +- Token 0: `OBJECT`, size=3: `{"Device":"0x1234","Power":true,"Temperature":25.5}` +- Token 1: `KEY`: `Device` +- Token 2: `STRING`: `0x1234` +- Token 3: `KEY`: `Power` +- Token 4: `BOOL_TRUE`: `true` +- Token 5: `KEY`: `Temperature` +- Token 6: `FLOAT`: `25.5` +- Token 7: `INVALID` + +If what you need is to parse a JSON Object for values with default values: +``` +#include "JsonParser.h" + +char json_buffer[] = "{\"Device\":\"0x1234\",\"Power\":true,\"Temperature\":25.6}"; +JsonParser parser(json_buffer); +JsonParserObject root = parser.getRootObject(); +if (!root) { ResponseCmndChar_P(PSTR("Invalid JSON")); return; } + +uint16_t d = root.getUInt(PSTR("DEVICE"), 0xFFFF); // case insensitive +bool b = root.getBool(PSTR("Power"), false); +float f = root.getFloat(PSTR("Temperature), -100); +``` + +Alternative pattern, if you want to test the existence of the attribute first: +``` +#include "JsonParser.h" + +char json_buffer[] = "{\"Device\":\"0x1234\",\"Power\":true,\"Temperature\":25.6}"; +JsonParser parser(json_buffer); +JsonParserObject root = parser.getRootObject(); +if (!root) { ResponseCmndChar_P(PSTR("Invalid JSON")); return; } + +JsonParserToken val = root[PSTR("DEVICE")]; +if (val) { + d = val.getUInt(); +} +val = root[PSTR("Power")]; +if (val) { + b = val.getBool(); +} +val = root[PSTR("Temperature)]; +if (val) { + f = val.getFloat(); +} +``` + +WARNING: never use the following form: +``` +JsonParserObject root = JsonParser(json_buffer).getRootObject(); +``` +In this case, the `JsonParser` object is temporary and destroyed at the end of the expression. Setting the JsonParser to a local variable ensures that the lifetime of the object is extended to the end of the scope. + +## Types and conversion + +JSMN relies on the concept of JSON Tokens `JsonParserToken`. Tokens do not hold any data, but point to token boundaries in JSON string instead. Every jsmn token has a type, which indicates the type of corresponding JSON token. JSMN for Tasmota extends the type from JSMN to ease further parsing. + +Types are: +- `INVALID` invalid token or end of stream, see Error Handling below +- `OBJECT` a JSON sub-object, `size()` contains the number of key/values of the object +- `ARRAY` a JSON array, `size()` contains the number of sub values +- `STRING` a JSON string, return the sub-string, unescaped, without surrounding quotes. UTF-8 is supported. +- `PRIMITIVE` an unrecognized JSON token, consider as an error +- `KEY` a JSON key in an object as a string +- `NULL` a JSON `null` value, automatically converted to `0` or `""` +- `BOOL_FALSE` a JSON `false` value, automatically converted to `0` or `""` +- `BOOL_TRUE` a JSON `true` value, automatically converted to `1` or `""` +- `UINT` a JSON unsigned int +- `INT` a JSON negative int +- `FLOAT` a JSON floating point value, i.e. a numerical value containing a decimal ot `.` + +Note: values are converted in this priority: 1/ `UINT`, 2/ `INT`, 3/ `FLOAT`. + +`JsonParserToken` support the following getters: +- `getBool()`: returns true if 'true' or non-zero int (default false) +- `getUInt()`: converts to unsigned int (default 0), boolean true is 1 +- `getInt()`: converts to signed int (default 0), boolean true is 1 +- `getULong()`: converts to unsigned 64 bits (default 0), boolean is 1 +- `getStr()`: converts to string (default "") + +There are variants of the same function for which you can choose the default values. Remember that using a getter if the token type is INVALID returns the default value. + +Conversion to `OBJECT` or `ARRAY`: +- `getObject()`: converts token to `OBJECT` or `INVALID` +- `getArray()`: converts token to `ARRAY` or `INVALID` + +For `JsonParserKey`: +- `getValue()`: returns the value token associated to the key + +## Checking Token types + +Type checking for `JsonParserToken`: +- `isSingleToken()` returns `true` for a single level token, `false` for `OBJECT`, `ARRAY` and `INVALID` +- `isKey()` returns `true` for a `KEY` within an `OBJECT` +- `isStr()` returns `true` for `STRING` (note: other types can still be read as strings with `getStr()`) +- `isNull()` returns `true` for `NULL` +- `isBool()` returns `true` for `BOOL_FALSE` or `BOOL_TRUE` +- `isUInt()` returns `true` for `UINT` (see below for number conversions) +- `isInt()` returns `true` for `INT` (see below for number conversions) +- `isFloat()` returns `true` for `FLOAT` (see below for number conversions) +- `isNum()` returns `true` for any numerical value, i.e. `UINT`, `INT` or `FLOAT` +- `isObject()` returns `true` for `OBJECT` +- `isArray()` returns `true` for `ARRAY` +- `isValid()` returns `true`for any type except `INVALID` + +JSMN for Tasmota provides sub-classes: +- `JsonParserKey` of type `KEY` or `INVALID`, used as a return value for Object iterators +- `JsonParserObject` of type `OBJECT` or `INVALID`, providing iterators +- `JsonParserArray` of type `ARRAY` or `INVALID`, providing iterators + +Converting from Token to Object or Array is done with `token.getObject()` or `token.getArray()`. If the conversion is invalid, the resulting object has type `INVALID` (see Error Handling). + +## Iterators and accessors for Objects and Arrays + +The `JsonParserObject` provides an easy to use iterator: +``` +JsonParserToken obj = <...> +for (auto key : obj) { + // key is of type JsonParserKey + JsonParserToken valie = key.getValue(); // retrieve the value associated to key +} +``` + +If the object contains only one element, you can retrieve it with `obj.getFirstElement()`. + +You can access any key with `obj["Key"]`. Note: the search is on purpose **case insensitive** as it is the norm in Tasmota. The search string can be in PROGMEM. If the token is not found, it is of type `INVALID`. + +The `JsonParserArray` provides an easy to use iterator: +``` +JsonParserArray arr = <...> +for (auto elt : arr) { + // elt is of type JsonParserToken +} +``` + +You can access any element in the array with the `[]` operator. Ex: `arr[0]` fof first element. If the index is invalid, the token has type `INVALID`. + +## Memory + +The `JsonParserToken` fits in 32 bits, so it can be freely returned or copied without any penalty of object copying. Hence it doesn't need the `const` modifier either, since it is always passed by value. + +## Error handling + +This library uses a `zero error` pattern. This means that calls never report any error nor throw exceptions. If something wrong happens (bad JSON, token not found...), function return an **Invalid Token**. You can call any function on an Invalid Token, they will always return the same Invalid Token (aka fixed point). + +You can easily test if the current token is invalid with the following: + +Short version: +``` +if (token) { /* token is valid */ } +``` + +Long version: +``` +if (token->isValiid()) { /* token is valid */ } +``` + +This pattern allows to cascade calls and check only the final result: +``` +char json_buffer[] = ""; +JsonParserObject json = JsonParser(json_buffer).getRootObject(); +JsonParserToken zbstatus_tok = json["ZbStatus"]; +JsonParserObject zbstatus = zbstatus_tok.getObject(); +if (zbstatus) { /* do some stuff */ + // reaching this point means: JSON is valid, there is a root object, there is a `ZbStatus` key and it contains a sub-object +} +``` + +Warning: there is an explicit convert to `bool` to allow the short version. Be careful, `(bool)token` is equivalent to `token->isValid()`, it is **NOT** equivalent to `token->getBool()`. + +## Limits + +Please keep in mind the current limits for this library: +- Maximum JSON buffer size 2047 bytes +- Maximum 63 JSON tokens +- No support for exponent in floats (i.e. `1.0e3` is invalid) + +These limits shouldn't be a problem since buffers in Tasmota are limited to 1.2KB. The support for exponent in floats is commented out and can be added if needed (slight increase in Flash size) \ No newline at end of file diff --git a/lib/jsmn-shadinger-1.0/library.properties b/lib/jsmn-shadinger-1.0/library.properties new file mode 100644 index 000000000..674aa76e7 --- /dev/null +++ b/lib/jsmn-shadinger-1.0/library.properties @@ -0,0 +1,8 @@ +name=JSMN JSON parser customized and optimized for ESP8266 and Tasmota +version=1.0 +author=Serge Zaitsev, Stephan Hadinger +maintainer=Stephan +sentence=Lightweight in-place JSON parser +paragraph= +url=https://github.com/zserge/jsmn +architectures=esp8266 diff --git a/lib/jsmn-shadinger-1.0/src/JsonGenerator.cpp b/lib/jsmn-shadinger-1.0/src/JsonGenerator.cpp new file mode 100644 index 000000000..a9554a55a --- /dev/null +++ b/lib/jsmn-shadinger-1.0/src/JsonGenerator.cpp @@ -0,0 +1,185 @@ +/* + JsonGenerator.cpp - lightweight JSON parser + + Copyright (C) 2020 Stephan Hadinger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "JsonGenerator.h" + +/*********************************************************************************************\ + * JSON Generator for Arrays +\*********************************************************************************************/ +void JsonGeneratorArray::pre(void) { + // remove trailing ']' + val.remove(val.length()-1); + if (val.length() > 1) { // if not empty, prefix with comma + val += ','; + } +} + +// void JsonGeneratorArray::post(void) { +// val += ']'; +// } + +// Add signed int (32 bits) +void JsonGeneratorArray::add(int32_t uval32) { + pre(); + val += uval32; + post(); +} + +// Add unsigned int (32 bits) +void JsonGeneratorArray::add(uint32_t uval32) { + pre(); + val += uval32; + post(); +} + +// Add a raw string, that will not be escaped. +// Can be used to add a sub-array, sub-object or 'null', 'true', 'false' values +void JsonGeneratorArray::addStrRaw(const char * sval) { + pre(); + val += sval; + post(); +} + +// Add a JSON String (will be escaped) +void JsonGeneratorArray::addStr(const char * sval) { + pre(); + val += '"'; + val += EscapeJSONString(sval).c_str(); + val += '"'; + post(); +} + +/*********************************************************************************************\ + * JSON Generator for Objects +\*********************************************************************************************/ +void JsonGeneratorObject::pre(const char * key) { + // remove trailing '}' + val.remove(val.length()-1); + if (val.length() > 1) { // if not empty, prefix with comma + val += ','; + } + val += '"'; + val += EscapeJSONString(key); + val += '"'; + val += ':'; +} + +// void JsonGeneratorObject::post(void) { +// val += '}'; +// } + +// Add signed int (32 bits) +void JsonGeneratorObject::add(const char* key, int32_t uval32) { + pre(key); + val += uval32; + post(); +} + +// Add unsigned int (32 bits) +void JsonGeneratorObject::add(const char* key, uint32_t uval32) { + pre(key); + val += uval32; + post(); +} + +void JsonGeneratorObject::add(const char* key, const String & str) { + pre(key); + val += '"'; + val += EscapeJSONString(str.c_str()).c_str(); + val += '"'; + post(); +} + +// Add a raw string, that will not be escaped. +// Can be used to add a sub-array, sub-object or 'null', 'true', 'false' values +void JsonGeneratorObject::addStrRaw(const char* key, const char * sval) { + pre(key); + val += sval; + post(); +} + +// Add a JSON String (will be escaped) +void JsonGeneratorObject::addStr(const char* key, const char * sval) { + pre(key); + val += '"'; + val += EscapeJSONString(sval).c_str(); + val += '"'; + post(); +} + +/*********************************************************************************************\ + * JSON Generator for Arrays +\*********************************************************************************************/ +// does the character needs to be escaped, and if so with which character +static char EscapeJSONChar(char c) { + if ((c == '\"') || (c == '\\')) { + return c; + } + if (c == '\n') { return 'n'; } + if (c == '\t') { return 't'; } + if (c == '\r') { return 'r'; } + if (c == '\f') { return 'f'; } + if (c == '\b') { return 'b'; } + return 0; +} + +String EscapeJSONString(const char *str) { + // As this function is used in ResponseCmndChar() and ResponseCmndIdxChar() + // it needs to be PROGMEM safe! + String r(""); + if (nullptr == str) { return r; } + + bool needs_escape = false; + size_t len_out = 1; + const char* c = str; + char ch = '.'; + while (ch != '\0') { + ch = pgm_read_byte(c++); + if (EscapeJSONChar(ch)) { + len_out++; + needs_escape = true; + } + len_out++; + } + + if (needs_escape) { + // we need to escape some chars + // allocate target buffer + r.reserve(len_out); + c = str; + char *d = r.begin(); + char ch = '.'; + while (ch != '\0') { + ch = pgm_read_byte(c++); + char c2 = EscapeJSONChar(ch); + if (c2) { + *d++ = '\\'; + *d++ = c2; + } else { + *d++ = ch; + } + } + *d = 0; // add NULL terminator + r = (char*) r.begin(); // assign the buffer to the string + } else { + r = FPSTR(str); + } + + return r; +} \ No newline at end of file diff --git a/lib/jsmn-shadinger-1.0/src/JsonGenerator.h b/lib/jsmn-shadinger-1.0/src/JsonGenerator.h new file mode 100644 index 000000000..8670aabbd --- /dev/null +++ b/lib/jsmn-shadinger-1.0/src/JsonGenerator.h @@ -0,0 +1,72 @@ +/* + JsonGenerator.h - lightweight JSON generator + + Copyright (C) 2020 Stephan Hadinger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef __JSON_GENERATOR__ +#define __JSON_GENERATOR__ + +#include +#include +#include + +extern String EscapeJSONString(const char *str); + +/*********************************************************************************************\ + * JSON Generator for Arrays +\*********************************************************************************************/ +class JsonGeneratorArray { +public: + + JsonGeneratorArray(): val("[]") {} // start with empty array + + void add(uint32_t uval32); + void add(int32_t uval32); + void addStrRaw(const char * sval); + void addStr(const char * sval); + + inline String &toString(void) { return val; } + +protected: + void pre(void); + void post(void) { val += ']'; } + String val; +}; + +/*********************************************************************************************\ + * JSON Generator for Objects +\*********************************************************************************************/ +class JsonGeneratorObject { +public: + + JsonGeneratorObject(): val("{}") {} // start with empty object + + void add(const char* key, uint32_t uval32); + void add(const char* key, int32_t uval32); + void add(const char* key, const String & str); + void addStrRaw(const char* key, const char * sval); + void addStr(const char* key, const char * sval); + + inline String &toString(void) { return val; } + +protected: + void pre(const char * key); + void post(void) { val += '}'; } + String val; +}; + +#endif // __JSON_PARSER__ \ No newline at end of file diff --git a/lib/jsmn-shadinger-1.0/src/JsonParser.cpp b/lib/jsmn-shadinger-1.0/src/JsonParser.cpp new file mode 100644 index 000000000..22d89424e --- /dev/null +++ b/lib/jsmn-shadinger-1.0/src/JsonParser.cpp @@ -0,0 +1,533 @@ +/* + JsonParser.cpp - lightweight JSON parser + + Copyright (C) 2020 Stephan Hadinger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "JsonParser.h" +#include + +/*********************************************************************************************\ + * Utilities +\*********************************************************************************************/ + +const char * k_current_json_buffer = ""; + +/*********************************************************************************************\ + * Lightweight String to Float, because atof() or strtof() takes 10KB + * + * To remove code, exponents are not parsed + * (commented out below, just in case we need them after all) +\*********************************************************************************************/ +// Inspired from https://searchcode.com/codesearch/view/22115068/ +float json_strtof(const char* s) { + const char* p = s; + float value = 0.; + int32_t sign = +1; + float factor; + // unsigned int expo; + + while (isspace(*p)){ // skip any leading white-spaces + p++; + } + + switch (*p) { + case '-': sign = -1; + case '+': p++; + default : break; + } + + while ((unsigned int)(*p - '0') < 10u) { + value = value*10 + (*p++ - '0'); + } + + if (*p == '.' ) { + factor = 1.0f; + + p++; + while ((unsigned int)(*p - '0') < 10u) { + factor *= 0.1f; + value += (*p++ - '0') * factor; + } + } + +// if ( (*p | 32) == 'e' ) { +// expo = 0; +// factor = 10.L; + +// switch (*++p) { // ja hier weiß ich nicht, was mindestens nach einem 'E' folgenden MUSS. +// case '-': factor = 0.1; +// case '+': p++; +// break; +// case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': +// break; +// default : value = 0.L; +// p = s; +// goto done; +// } + +// while ( (unsigned int)(*p - '0') < 10u ) +// expo = 10 * expo + (*p++ - '0'); + +// while ( 1 ) { +// if ( expo & 1 ) +// value *= factor; +// if ( (expo >>= 1) == 0 ) +// break; +// factor *= factor; +// } +// } + +// done: + // if ( endptr != NULL ) + // *endptr = (char*)p; + + return value * sign; +} + +/*********************************************************************************************\ + * Read-only JSON token object, fits in 32 bits +\*********************************************************************************************/ + +void JsonParserToken::skipToken(void) { + // printf("skipToken type = %d %s\n", t->type, json_string + t->start); + switch (t->type) { + case JSMN_OBJECT: + skipObject(); + break; + case JSMN_ARRAY: + skipArray(); + break; + case JSMN_STRING: + case JSMN_PRIMITIVE: + case JSMN_KEY: + case JSMN_NULL: + case JSMN_BOOL_FALSE: + case JSMN_BOOL_TRUE: + case JSMN_FLOAT: + case JSMN_INT: + case JSMN_UINT: + t++; // skip 1 token + break; + case JSMN_INVALID: + default: + break; // end of stream, stop advancing + } +} + +void JsonParserToken::skipArray(void) { + if (t->type == JSMN_ARRAY) { + size_t obj_size = t->size; + t++; // array root + if (t->type == JSMN_INVALID) { return; } + for (uint32_t i=0; itype == JSMN_OBJECT) { + size_t obj_size = t->size; + t++; // object root + if (t->type == JSMN_INVALID) { return; } + for (uint32_t i=0; itype == JSMN_INVALID) { return; } + skipToken(); + } + } +} + +/*********************************************************************************************\ + * JsonParserArray +\*********************************************************************************************/ + +JsonParserArray::JsonParserArray(const jsmntok_t * token) : JsonParserToken(token) { + if (t->type != JSMN_ARRAY) { + t = &token_bad; + } +} +JsonParserArray::JsonParserArray(const JsonParserToken token) : JsonParserToken(token.t) { + if (t->type != JSMN_ARRAY) { + t = &token_bad; + } +} + +JsonParserArray::const_iterator::const_iterator(const JsonParserArray t): tok(t), remaining(0) { + if (tok.t == &token_bad) { tok.t = nullptr; } + if (nullptr != tok.t) { + // ASSERT type == JSMN_ARRAY by constructor + remaining = tok.t->size; + tok.nextOne(); // skip array root token + } +} + +JsonParserArray::const_iterator JsonParserArray::const_iterator::const_iterator::operator++() { + if (remaining <= 1) { tok.t = nullptr; } + else { + remaining--; + tok.skipToken(); // munch value + if (tok.t->type == JSMN_INVALID) { tok.t = nullptr; } // unexpected end of stream + } + return *this; +} + +JsonParserToken JsonParserArray::operator[](int32_t i) const { + if ((i >= 0) && (i < t->size)) { + uint32_t index = 0; + for (const auto elt : *this) { + if (i == index) { + return elt; + } + index++; + } + } + // fallback + return JsonParserToken(&token_bad); +} + +/*********************************************************************************************\ + * JsonParserObject +\*********************************************************************************************/ + +JsonParserObject::JsonParserObject(const jsmntok_t * token) : JsonParserToken(token) { + if (t->type != JSMN_OBJECT) { + t = &token_bad; + } +} +JsonParserObject::JsonParserObject(const JsonParserToken token) : JsonParserToken(token.t) { + if (t->type != JSMN_OBJECT) { + t = &token_bad; + } +} + +JsonParserKey JsonParserObject::getFirstElement(void) const { + if (t->size > 0) { + return JsonParserKey(t+1); // return next element and cast to Key + } else { + return JsonParserKey(&token_bad); + } +} + +JsonParserObject::const_iterator::const_iterator(const JsonParserObject t): tok(t), remaining(0) { + if (tok.t == &token_bad) { tok.t = nullptr; } + if (nullptr != tok.t) { + // ASSERT type == JSMN_OBJECT by constructor + remaining = tok.t->size; + tok.nextOne(); + } +} + +JsonParserObject::const_iterator JsonParserObject::const_iterator::operator++() { + if (remaining <= 1) { tok.t = nullptr; } + else { + remaining--; + tok.nextOne(); // munch key + if (tok.t->type == JSMN_INVALID) { tok.t = nullptr; } // unexpected end of stream + tok.skipToken(); // munch value + if (tok.t->type == JSMN_INVALID) { tok.t = nullptr; } // unexpected end of stream + } + return *this; +} + +/*********************************************************************************************\ + * JsonParserKey +\*********************************************************************************************/ + + +JsonParserKey::JsonParserKey(const jsmntok_t * token) : JsonParserToken(token) { + if (t->type != JSMN_KEY) { + t = &token_bad; + } +} +JsonParserKey::JsonParserKey(const JsonParserToken token) : JsonParserToken(token.t) { + if (t->type != JSMN_KEY) { + t = &token_bad; + } +} + +JsonParserToken JsonParserKey::getValue(void) const { + return JsonParserToken(t+1); +} + +/*********************************************************************************************\ + * Implementation for JSON Parser +\*********************************************************************************************/ + +// fall-back token object when parsing failed +const jsmntok_t token_bad = { JSMN_INVALID, 0, 0, 0 }; + +JsonParser::JsonParser(char * json_in) : + _size(0), + _token_len(0), + _tokens(nullptr), + _json(nullptr) +{ + parse(json_in); +} + +JsonParser::~JsonParser() { + this->free(); +} + +const JsonParserObject JsonParser::getRootObject(void) const { + return JsonParserObject(&_tokens[0]); +} + +const JsonParserToken JsonParser::operator[](int32_t i) const { +if ((_token_len > 0) && (i < _token_len)) { + return JsonParserToken(&_tokens[i]); + } else { + return JsonParserToken(&token_bad); + } +} + +// pointer arithmetic +// ptrdiff_t JsonParser::index(JsonParserToken token) const { +// return token.t - _tokens; +// } + +bool JsonParserToken::getBool(bool val) const { + if (t->type == JSMN_INVALID) { return val; } + if (t->type == JSMN_BOOL_TRUE) return true; + if (t->type == JSMN_BOOL_FALSE) return false; + if (isSingleToken()) return strtol(&k_current_json_buffer[t->start], nullptr, 0) != 0; + return false; +} +int32_t JsonParserToken::getInt(int32_t val) const { + if (t->type == JSMN_INVALID) { return val; } + if (t->type == JSMN_BOOL_TRUE) return 1; + if (isSingleToken()) return strtol(&k_current_json_buffer[t->start], nullptr, 0); + return 0; +} +uint32_t JsonParserToken::getUInt(uint32_t val) const { + if (t->type == JSMN_INVALID) { return val; } + if (t->type == JSMN_BOOL_TRUE) return 1; + if (isSingleToken()) return strtoul(&k_current_json_buffer[t->start], nullptr, 0); + return 0; +} +uint64_t JsonParserToken::getULong(uint64_t val) const { + if (t->type == JSMN_INVALID) { return val; } + if (t->type == JSMN_BOOL_TRUE) return 1; + if (isSingleToken()) return strtoull(&k_current_json_buffer[t->start], nullptr, 0); + return 0; +} +float JsonParserToken::getFloat(float val) const { + if (t->type == JSMN_INVALID) { return val; } + if (t->type == JSMN_BOOL_TRUE) return 1; + if (isSingleToken()) return json_strtof(&k_current_json_buffer[t->start]); + return 0; +} +const char * JsonParserToken::getStr(const char * val) const { + if (t->type == JSMN_INVALID) { return val; } + if (t->type == JSMN_NULL) return ""; + return (t->type >= JSMN_STRING) ? &k_current_json_buffer[t->start] : val; +} + + +JsonParserObject JsonParserToken::getObject(void) const { return JsonParserObject(*this); } +JsonParserArray JsonParserToken::getArray(void) const { return JsonParserArray(*this); } + + +bool JsonParserToken::getBool(void) const { return getBool(false); } +int32_t JsonParserToken::getInt(void) const { return getInt(0); } +uint32_t JsonParserToken::getUInt(void) const { return getUInt(0); } +uint64_t JsonParserToken::getULong(void) const { return getULong(0); } +float JsonParserToken::getFloat(void) const { return getFloat(0); } +const char * JsonParserToken::getStr(void) const { return getStr(""); } + +int32_t JsonParserObject::getInt(const char * needle, int32_t val) const { + return (*this)[needle].getInt(val); +} +uint32_t JsonParserObject::getUInt(const char * needle, uint32_t val) const { + return (*this)[needle].getUInt(val); +} +uint64_t JsonParserObject::getULong(const char * needle, uint64_t val) const { + return (*this)[needle].getULong(val); +} +float JsonParserObject::getFloat(const char * needle, float val) const { + return (*this)[needle].getFloat(val); +} +const char * JsonParserObject::getStr(const char * needle, const char * val) const { + return (*this)[needle].getStr(val); +} +const char * JsonParserObject::getStr(const char * needle) const { + return getStr(needle, ""); +} + +void JsonParser::parse(char * json_in) { + k_current_json_buffer = ""; + if (nullptr == json_in) { return; } + _json = json_in; + k_current_json_buffer = _json; + size_t json_len = strlen(json_in); + if (_size == 0) { + // first run is used to count tokens before allocation + jsmn_init(&this->_parser); + int32_t _token_len = jsmn_parse(&this->_parser, json_in, json_len, nullptr, 0); + if (_token_len <= 0) { return; } + _size = _token_len + 1; + } + allocate(); + jsmn_init(&this->_parser); + _token_len = jsmn_parse(&this->_parser, json_in, json_len, _tokens, _size); + // TODO error checking + if (_token_len >= 0) { + postProcess(json_len); + } +} + +// post process the parsing by pre-munching extended types +void JsonParser::postProcess(size_t json_len) { + // add an end marker + if (_size > _token_len) { + _tokens[_token_len].type = JSMN_INVALID; + _tokens[_token_len].start = json_len; + _tokens[_token_len].len = 0; + _tokens[_token_len].size = 0; + } + for (uint32_t i=0; i<_token_len; i++) { + jsmntok_t & tok = _tokens[i]; + + if (tok.type >= JSMN_STRING) { + // we modify to null-terminate the primitive + _json[tok.start + tok.len] = 0; + } + + if (tok.type == JSMN_STRING) { + if (tok.size == 1) { tok.type = JSMN_KEY; } + else { json_unescape(&_json[tok.start]); } + } else if (tok.type == JSMN_PRIMITIVE) { + if (tok.len >= 0) { + // non-null string + char c0 = _json[tok.start]; + switch (c0) { + case 'n': + case 'N': + tok.type = JSMN_NULL; + break; + case 't': + case 'T': + tok.type = JSMN_BOOL_TRUE; + break; + case 'f': + case 'F': + tok.type = JSMN_BOOL_FALSE; + break; + case '-': + case '0'...'9': + // look if there is a '.' in the string + if (nullptr != memchr(&_json[tok.start], '.', tok.len)) { + tok.type = JSMN_FLOAT; + } else if (c0 == '-') { + tok.type = JSMN_INT; + } else { + tok.type = JSMN_UINT; + } + break; + default: + tok.type = JSMN_PRIMITIVE; + break; + } + } else { + tok.type = JSMN_PRIMITIVE; + } + } + } +} + +JsonParserToken JsonParserObject::operator[](const char * needle) const { + // key can be in PROGMEM + if ((!this->isValid()) || (nullptr == needle) || (0 == pgm_read_byte(needle))) { + return JsonParserToken(&token_bad); + } + // if needle == "?" then we return the first valid key + bool wildcard = (strcmp_P("?", needle) == 0); + + for (const auto key : *this) { + if (wildcard) { return key.getValue(); } + if (0 == strcasecmp_P(key.getStr(), needle)) { return key.getValue(); } + } + // if not found + return JsonParserToken(&token_bad); +} + +JsonParserToken JsonParserObject::operator[](const String & needle) const { + return (*this)[needle.c_str()]; +} + +JsonParserToken JsonParserObject::findStartsWith(const char * needle) const { + // key can be in PROGMEM + if ((!this->isValid()) || (nullptr == needle) || (0 == pgm_read_byte(needle))) { + return JsonParserToken(&token_bad); + } + + String needle_s((const __FlashStringHelper *)needle); + needle_s.toLowerCase(); + + for (const auto key : *this) { + String key_s(key.getStr()); + key_s.toLowerCase(); + + if (key_s.startsWith(needle_s)) { + return key.getValue(); + } + } + // if not found + return JsonParserToken(&token_bad); +} + +const char * JsonParserObject::findConstCharNull(const char * needle) const { + const char * r = (*this)[needle].getStr(); + if (*r == 0) { r = nullptr; } // if empty string + return r; +} + +// JsonParserToken JsonParser::find(JsonParserObject obj, const char *needle, bool case_sensitive) const { +// // key can be in PROGMEM +// if ((!obj.isValid()) || (nullptr == needle) || (0 == pgm_read_byte(needle))) { +// return JsonParserToken(&token_bad); +// } +// // if needle == "?" then we return the first valid key +// bool wildcard = (strcmp_P("?", needle) == 0); + +// for (const auto key : obj) { +// if (wildcard) { return key.getValue(); } +// if (case_sensitive) { +// if (0 == strcmp_P(this->getStr(key), needle)) { return key.getValue(); } +// } else { +// if (0 == strcasecmp_P(this->getStr(key), needle)) { return key.getValue(); } +// } +// } +// // if not found +// return JsonParserToken(&token_bad); +// } + +void JsonParser::free(void) { + if (nullptr != _tokens) { + delete[] _tokens; // TODO + _tokens = nullptr; + } +} + +void JsonParser::allocate(void) { + this->free(); + if (_size != 0) { + _tokens = new jsmntok_t[_size]; + } +} diff --git a/lib/jsmn-shadinger-1.0/src/JsonParser.h b/lib/jsmn-shadinger-1.0/src/JsonParser.h new file mode 100644 index 000000000..85d401407 --- /dev/null +++ b/lib/jsmn-shadinger-1.0/src/JsonParser.h @@ -0,0 +1,266 @@ +/* + JsonParser.h - lightweight JSON parser + + Copyright (C) 2020 Stephan Hadinger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef __JSON_PARSER__ +#define __JSON_PARSER__ + +#include "jsmn.h" +#include +#include +#include + +// #define strcmp_P(x, y) strcmp(x,y) +// #define strcasecmp_P(x,y) strcasecmp(x,y) +// #define pgm_read_byte(x) (*(uint8_t*)(x)) +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +/*********************************************************************************************\ + * Utilities +\*********************************************************************************************/ + +// The code uses a zero-error approach. Functions never return an error code nor an exception. +// In case an operation fails, it returns an "Invalid Token". +// To know if a token is valid, use the `isValid()` method or just use it in an if statement. +// +// Internally, the bad token is a pointer to a constant token of type JSMN_INVALID +// fall-back token object when parsing failed +const extern jsmntok_t token_bad; + +// To reduce code size, the current buffer is stored in a global variable. +// This prevents all calls to add this parameter on the stack and reduces code size. +// The caveat is that code is not re-entrant. +// If you ever intermix two or more JSON parsers, use `parser->setCurrent()` before further calls +// +// the current json buffer being used, for convenience +// Warning: this makes code non-reentrant. +extern const char * k_current_json_buffer; + +/*********************************************************************************************\ + * Read-only JSON token object, fits in 32 bits +\*********************************************************************************************/ +// forward class declarations +class JsonParserObject; +class JsonParserArray; + +class JsonParserToken { +public: + + // constructor + // If parameter is null, we use the Invalid Token instead. + JsonParserToken(const jsmntok_t * token) : t(token) { + if (nullptr == t) { t = &token_bad; } + } + JsonParserToken() : t(&token_bad) { } + // no explicit destructor (not needed) + + inline bool isValid(void) const { return t->type != JSMN_INVALID; } + inline size_t size(void) const { return t->size; } + + inline bool isSingleToken(void) const { return (t->type >= JSMN_STRING); } + inline bool isKey(void) const { return (t->type == JSMN_KEY); } + inline bool isStr(void) const { return (t->type == JSMN_STRING); } + inline bool isNull(void) const { return (t->type == JSMN_NULL); } + inline bool isBool(void) const { return (t->type == JSMN_BOOL_TRUE) || (t->type == JSMN_BOOL_FALSE); } + inline bool isFloat(void) const { return (t->type == JSMN_FLOAT); } + inline bool isInt(void) const { return (t->type == JSMN_INT); } + inline bool isUint(void) const { return (t->type == JSMN_UINT); } + inline bool isNum(void) const { return (t->type >= JSMN_FLOAT) && (t->type <= JSMN_UINT); } + inline bool isObject(void) const { return (t->type == JSMN_OBJECT); } + inline bool isArray(void) const { return (t->type == JSMN_ARRAY); } + + // move to token immediately after in the buffer + void nextOne(void) { if (t->type != JSMN_INVALID) { t++; } } + + // conversion operators + // Warning - bool does not test for Boolean value but for validity, i.e. equivalent to token.valid() + inline explicit operator bool() const { return t->type != JSMN_INVALID; }; + + // all the following conversion will try to give a meaninful value + // if the content is not of the right type or the token is invalid, returns the 'default' + bool getBool(void) const; // true if 'true' or non-zero int (default false) + int32_t getInt(void) const; // convert to int (default 0) + uint32_t getUInt(void) const; // convert to unsigned int (default 0) + uint64_t getULong(void) const; // convert to unsigned 64 bits (default 0) + float getFloat(void) const; // convert to float (default 0), does not support exponent + const char * getStr(void) const; // convert to string (default "") + + // same as above, but you can choose the default value + bool getBool(bool val) const; + int32_t getInt(int32_t val) const; + uint32_t getUInt(uint32_t val) const; + uint64_t getULong(uint64_t val) const; + float getFloat(float val) const; + const char * getStr(const char * val) const; + + // convert to JsonParserObject or JsonParserArray, or Invalid Token if not allowed + JsonParserObject getObject(void) const; + JsonParserArray getArray(void) const; + +public: + // the following should be 'protected' but then it can't be accessed by iterators + const jsmntok_t * t; + // skip the next Token as a whole (i.e. skip an entire array) + void skipToken(void); + +protected: + + // skip the next token knowing it's an array + void skipArray(void); + + // skip the next token knowing it's an object + void skipObject(void); +}; + +/*********************************************************************************************\ + * Subclass for Key +\*********************************************************************************************/ +class JsonParserKey : public JsonParserToken { +public: + JsonParserKey(const jsmntok_t * token); + explicit JsonParserKey(const JsonParserToken token); + + // get the value token associated to the key + JsonParserToken getValue(void) const; +}; + +/*********************************************************************************************\ + * Subclass for Object +\*********************************************************************************************/ +class JsonParserObject : public JsonParserToken { +public: + JsonParserObject(const jsmntok_t * token); + JsonParserObject(const JsonParserToken token); + JsonParserObject() : JsonParserToken() { } + + // find key with name, case-insensitive, '?' matches any key. Returns Invalid Token if not found + JsonParserToken operator[](const char * needle) const; + JsonParserToken operator[](const String & needle) const; + // find a key starting with `needle`, case insensitive + JsonParserToken findStartsWith(const char * needle) const; + // find a key, case-insensitive, return nullptr if not found (instead of "") + const char * findConstCharNull(const char * needle) const; + + // all-in-one methods: search for key (case insensitive), convert value and set default + int32_t getInt(const char *, int32_t) const; + uint32_t getUInt(const char *, uint32_t) const; + uint64_t getULong(const char *, uint64_t) const; + float getFloat(const char *, float) const; + const char * getStr(const char *, const char *) const; + const char * getStr(const char *) const; + + // get first element (key) + JsonParserKey getFirstElement(void) const; + + // + // const iterator + // + class const_iterator { + public: + const_iterator(const JsonParserObject t); + const_iterator operator++(); + bool operator!=(const_iterator & other) const { return tok.t != other.tok.t; } + const JsonParserKey operator*() const { return JsonParserKey(tok); } + private: + JsonParserToken tok; + size_t remaining; + }; + const_iterator begin() const { return const_iterator(*this); } // start with 'head' + const_iterator end() const { return const_iterator(JsonParserObject(&token_bad)); } // end with null pointer +}; + +/*********************************************************************************************\ + * Subclass for Array +\*********************************************************************************************/ +class JsonParserArray : public JsonParserToken { +public: + JsonParserArray(const jsmntok_t * token); + JsonParserArray(const JsonParserToken token); + JsonParserArray() : JsonParserToken() { } + + // get the element if index `i` from 0 to `size() - 1` + JsonParserToken operator[](int32_t i) const; + + // + // const iterator + // + class const_iterator { + public: + const_iterator(const JsonParserArray t); + const_iterator operator++(); + bool operator!=(const_iterator & other) const { return tok.t != other.tok.t; } + const JsonParserToken operator*() const { return tok; } + private: + JsonParserToken tok; + size_t remaining; + }; + const_iterator begin() const { return const_iterator(*this); } // start with 'head' + const_iterator end() const { return const_iterator(JsonParserArray(&token_bad)); } // end with null pointer +}; + +/*********************************************************************************************\ + * JSON Parser +\*********************************************************************************************/ + +class JsonParser { +public: + // constructor, parse the json buffer + // Warning: the buffer is modified in the process (in-place parsing) + // Input: `json_in` can be nullptr, but CANNOT be in PROGMEM (remember we need to change characters in-place) + JsonParser(char * json_in); + + // destructor + ~JsonParser(); + + // set the current buffer for attribute access (i.e. set the global) + void setCurrent(void) { k_current_json_buffer = _json; } + + // test if the parsing was successful + inline explicit operator bool() const { return _token_len > 0; } + + const JsonParserToken getRoot(void) { return JsonParserToken(&_tokens[0]); } + // const JsonParserObject getRootObject(void) { return JsonParserObject(&_tokens[0]); } + const JsonParserObject getRootObject(void) const; + + // pointer arithmetic + // ptrdiff_t index(JsonParserToken token) const; + +protected: + uint16_t _size; // size of tokens buffer + int16_t _token_len; // how many tokens have been parsed + jsmntok_t * _tokens; // pointer to token buffer + jsmn_parser _parser; // jmsn_parser structure + char * _json; // json buffer + + // disallocate token buffer + void free(void); + + // allocate token buffer of size _size + void allocate(void); + + // access tokens by index + const JsonParserToken operator[](int32_t i) const; + // parse + void parse(char * json_in); + // post-process parsing: insert NULL chars to split strings, compute a more precise token type + void postProcess(size_t json_len); +}; + +#endif // __JSON_PARSER__ \ No newline at end of file diff --git a/lib/jsmn-shadinger-1.0/src/jsmn.cpp b/lib/jsmn-shadinger-1.0/src/jsmn.cpp new file mode 100644 index 000000000..905d102f4 --- /dev/null +++ b/lib/jsmn-shadinger-1.0/src/jsmn.cpp @@ -0,0 +1,453 @@ +/* + * MIT License + * + * Copyright (c) 2010 Serge Zaitsev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "jsmn.h" + +#define JSMN_STRICT // force strict mode + +const uint32_t JSMN_START_MAX = (1U << JSMN_START_B) - 1; +const uint32_t JSMN_LEN_MAX = (1U << JSMN_LEN_B) - 1; + +/** + * Allocates a fresh unused token from the token pool. + */ +static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens, + const size_t num_tokens) { + jsmntok_t *tok; + if (parser->toknext >= num_tokens) { + return NULL; + } + tok = &tokens[parser->toknext++]; + tok->start = JSMN_START_MAX; + tok->len = JSMN_LEN_MAX; + tok->size = 0; + return tok; +} + +/** + * Fills token type and boundaries. + */ +static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type, + const int start, const int len) { + token->type = type; + token->start = start; + token->len = len; + token->size = 0; +} + +/** + * Fills next available token with JSON primitive. + */ +static int jsmn_parse_primitive(jsmn_parser *parser, const char *js, + const size_t len, jsmntok_t *tokens, + const size_t num_tokens) { + jsmntok_t *token; + int start; + + start = parser->pos; + + for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + switch (js[parser->pos]) { +#ifndef JSMN_STRICT + /* In strict mode primitive must be followed by "," or "}" or "]" */ + case ':': +#endif + case '\t': + case '\r': + case '\n': + case ' ': + case ',': + case ']': + case '}': + goto found; + default: + /* to quiet a warning from gcc*/ + break; + } + if (js[parser->pos] < 32 || js[parser->pos] >= 127) { + parser->pos = start; + return JSMN_ERROR_INVAL; + } + } +#ifdef JSMN_STRICT + /* In strict mode primitive must be followed by a comma/object/array */ + parser->pos = start; + return JSMN_ERROR_PART; +#endif + +found: + if (tokens == NULL) { + parser->pos--; + return 0; + } + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) { + parser->pos = start; + return JSMN_ERROR_NOMEM; + } + jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos - start); + parser->pos--; + return 0; +} + +/** + * Fills next token with JSON string. + */ +static int jsmn_parse_string(jsmn_parser *parser, const char *js, + const size_t len, jsmntok_t *tokens, + const size_t num_tokens) { + jsmntok_t *token; + + int start = parser->pos; + + parser->pos++; + + /* Skip starting quote */ + for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + char c = js[parser->pos]; + + /* Quote: end of string */ + if (c == '\"') { + if (tokens == NULL) { + return 0; + } + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) { + parser->pos = start; + return JSMN_ERROR_NOMEM; + } + jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos - start - 1); + return 0; + } + + /* Backslash: Quoted symbol expected */ + if (c == '\\' && parser->pos + 1 < len) { + int i; + parser->pos++; + switch (js[parser->pos]) { + /* Allowed escaped symbols */ + case '\"': + case '/': + case '\\': + case 'b': + case 'f': + case 'r': + case 'n': + case 't': + break; + /* Allows escaped symbol \uXXXX */ + case 'u': + parser->pos++; + for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; + i++) { + /* If it isn't a hex character we have an error */ + if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ + (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ + (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ + parser->pos = start; + return JSMN_ERROR_INVAL; + } + parser->pos++; + } + parser->pos--; + break; + /* Unexpected symbol */ + default: + parser->pos = start; + return JSMN_ERROR_INVAL; + } + } + } + parser->pos = start; + return JSMN_ERROR_PART; +} + +/** + * Parse JSON string and fill tokens. + */ +JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, + jsmntok_t *tokens, const unsigned int num_tokens) { + int r; + int i; + jsmntok_t *token; + int count = parser->toknext; + + for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + char c; + jsmntype_t type; + + c = js[parser->pos]; + switch (c) { + case '{': + case '[': + count++; + if (tokens == NULL) { + break; + } + token = jsmn_alloc_token(parser, tokens, num_tokens); + if (token == NULL) { + return JSMN_ERROR_NOMEM; + } + if (parser->toksuper != -1) { + jsmntok_t *t = &tokens[parser->toksuper]; +#ifdef JSMN_STRICT + /* In strict mode an object or array can't become a key */ + if (t->type == JSMN_OBJECT) { + return JSMN_ERROR_INVAL; + } +#endif + t->size++; + } + token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); + token->start = parser->pos; + parser->toksuper = parser->toknext - 1; + break; + case '}': + case ']': + if (tokens == NULL) { + break; + } + type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); + for (i = parser->toknext - 1; i >= 0; i--) { + token = &tokens[i]; + if ((token->start != JSMN_START_MAX) && (token->len == JSMN_LEN_MAX)) { + if (token->type != type) { + return JSMN_ERROR_INVAL; + } + parser->toksuper = -1; + token->len = parser->pos + 1 - token->start; + break; + } + } + /* Error if unmatched closing bracket */ + if (i == -1) { + return JSMN_ERROR_INVAL; + } + for (; i >= 0; i--) { + token = &tokens[i]; + if ((token->start != JSMN_START_MAX) && (token->len == JSMN_LEN_MAX)) { + parser->toksuper = i; + break; + } + } + break; + case '\"': + r = jsmn_parse_string(parser, js, len, tokens, num_tokens); + if (r < 0) { + return r; + } + count++; + if (parser->toksuper != -1 && tokens != NULL) { + tokens[parser->toksuper].size++; + } + break; + case '\t': + case '\r': + case '\n': + case ' ': + break; + case ':': + parser->toksuper = parser->toknext - 1; + break; + case ',': + if (tokens != NULL && parser->toksuper != -1 && + tokens[parser->toksuper].type != JSMN_ARRAY && + tokens[parser->toksuper].type != JSMN_OBJECT) { + for (i = parser->toknext - 1; i >= 0; i--) { + if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) { + if ((tokens[i].start != JSMN_START_MAX) && (tokens[i].len == JSMN_LEN_MAX)) { + parser->toksuper = i; + break; + } + } + } + } + break; +#ifdef JSMN_STRICT + /* In strict mode primitives are: numbers and booleans */ + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 't': + case 'f': + case 'n': +// Add uppercase variants + case 'T': + case 'F': + case 'N': + /* And they must not be keys of the object */ + if (tokens != NULL && parser->toksuper != -1) { + const jsmntok_t *t = &tokens[parser->toksuper]; + if (t->type == JSMN_OBJECT || + (t->type == JSMN_STRING && t->size != 0)) { + return JSMN_ERROR_INVAL; + } + } +#else + /* In non-strict mode every unquoted value is a primitive */ + default: +#endif + r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); + if (r < 0) { + return r; + } + count++; + if (parser->toksuper != -1 && tokens != NULL) { + tokens[parser->toksuper].size++; + } + break; + +#ifdef JSMN_STRICT + /* Unexpected char in strict mode */ + default: + return JSMN_ERROR_INVAL; +#endif + } + } + + if (tokens != NULL) { + for (i = parser->toknext - 1; i >= 0; i--) { + /* Unmatched opened object or array */ + if ((tokens[i].start != JSMN_START_MAX) && (tokens[i].len == JSMN_LEN_MAX)) { + return JSMN_ERROR_PART; + } + } + } + + return count; +} + +/** + * Creates a new parser based over a given buffer with an array of tokens + * available. + */ +JSMN_API void jsmn_init(jsmn_parser *parser) { + parser->pos = 0; + parser->toknext = 0; + parser->toksuper = -1; +} + +// +// Json in-place string unescape +// inpired from https://github.com/mjansson/json/blob/master/json.h +// +//! Define a bitmask with the given number of bits set to 1 +#define JSON_BITMASK(numbits) ((1U << (numbits)) - 1) + +static uint32_t json_get_num_bytes_as_utf8(uint32_t val) { + if (val >= 0x04000000) return 6; + else if (val >= 0x00200000) return 5; + else if (val >= 0x00010000) return 4; + else if (val >= 0x00000800) return 3; + else if (val >= 0x00000080) return 2; + return 1; +} + +static uint32_t json_encode_utf8(char* str, uint32_t val) { + if (val < 0x80) { + *str = (char)val; + return 1; + } + + //Get number of _extra_ bytes + uint32_t num = json_get_num_bytes_as_utf8(val) - 1; + + *str++ = (char)((0x80U | (JSON_BITMASK(num) << (7U - num))) | + ((val >> (6U * num)) & JSON_BITMASK(6U - num))); + for (uint32_t j = 1; j <= num; ++j) + *str++ = (char)(0x80U | ((val >> (6U * (num - j))) & 0x3F)); + + return num + 1; +} + +void json_unescape(char* string) { + size_t outlength = 0; + uint32_t hexval, numbytes; + + char c; + for (uint32_t i = 0; (c = string[i]) != 0; i++) { + if ('\\' == c) { + c = string[++i]; + switch (c) { + case 0: + return; // end of stream + case '\"': + case '/': + case '\\': + string[outlength++] = c; + break; + + case 'b': + string[outlength++] = '\b'; + break; + case 'f': + string[outlength++] = '\f'; + break; + case 'r': + string[outlength++] = '\r'; + break; + case 'n': + string[outlength++] = '\n'; + break; + case 't': + string[outlength++] = '\t'; + break; + + case 'u': + { + uint32_t hexval = 0; + for (uint32_t j = 0; j < 4; ++j) { + char val = string[++i]; + if (0 == val) { return; } // we reached end of string + uint32_t uival = 0; + if ((val >= 'a') && (val <= 'f')) + uival = 10 + (val - 'a'); + else if ((val >= 'A') && (val <= 'F')) + uival = 10 + (val - 'A'); + else if ((val >= '0') && (val <= '9')) + uival = val - '0'; + hexval |= uival << (3 - j); + } + numbytes = json_get_num_bytes_as_utf8(hexval); + outlength += json_encode_utf8(string + outlength, hexval); + } + break; + + default: + break; + } + } + else { + string[outlength++] = c; + } + } +} \ No newline at end of file diff --git a/lib/jsmn-shadinger-1.0/src/jsmn.h b/lib/jsmn-shadinger-1.0/src/jsmn.h new file mode 100644 index 000000000..36fd11db8 --- /dev/null +++ b/lib/jsmn-shadinger-1.0/src/jsmn.h @@ -0,0 +1,118 @@ +/* + * MIT License + * + * Copyright (c) 2010 Serge Zaitsev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef JSMN_H +#define JSMN_H + +#include +#include + +// #ifdef JSMN_STATIC +// #define JSMN_API static +// #else +#define JSMN_API extern +// #endif + +/** + * JSON type identifier. Basic types are: + * o Object + * o Array + * o String + * o Other primitive: number, boolean (true/false) or null + */ +typedef enum { + // end market + JSMN_INVALID = 0, // type == 0 is invalid + JSMN_OBJECT = 1, + JSMN_ARRAY = 2, + JSMN_STRING = 3, + JSMN_PRIMITIVE = 4, + // new types created during post-processing + JSMN_KEY = 5, // JSMN_STRING with size 1 + JSMN_NULL = 6, // JSMN_PRIMITIVE starting with 'n' + JSMN_BOOL_FALSE = 7, // JSMN_PRIMITIVE starting with 'f' or 'F' + JSMN_BOOL_TRUE = 8, // JSMN_PRIMITIVE starting with 't' or 'T' + JSMN_FLOAT = 9, // JSMN_PRIMITIVE starting with '.', '-', '0-9' and containing a '.' + JSMN_INT = 10, // JSMN_PRIMITIVE starting with '-', '0-9' and not containing a '.' + JSMN_UINT = 11, // JSMN_PRIMITIVE starting with '0-9' and not containing a '.' +} jsmntype_t; + +enum jsmnerr { + /* Not enough tokens were provided */ + JSMN_ERROR_NOMEM = -1, + /* Invalid character inside JSON string */ + JSMN_ERROR_INVAL = -2, + /* The string is not a full JSON packet, more bytes expected */ + JSMN_ERROR_PART = -3 +}; + +/** + * JSON token description. + * type type (object, array, string etc.) + * start start position in JSON data string + * end end position in JSON data string + */ +// size of bitfield, sum is 32 +#define JSMN_TYPE_B 4 +#define JSMN_SIZE_B 6 // max 63 items per level (ex: max 63 keys per object) +#define JSMN_START_B 11 // max 2KB input buffer +#define JSMN_LEN_B 11 // max 2KB per item + +typedef struct jsmntok { + jsmntype_t type : JSMN_TYPE_B; + unsigned int size : JSMN_SIZE_B; + unsigned int start : JSMN_START_B; + unsigned int len : JSMN_LEN_B; +} jsmntok_t; + +/** + * JSON parser. Contains an array of token blocks available. Also stores + * the string being parsed now and current position in that string. + */ +typedef struct jsmn_parser { + unsigned int pos; /* offset in the JSON string */ + unsigned int toknext; /* next token to allocate */ + int toksuper; /* superior token node, e.g. parent object or array */ +} jsmn_parser; + +/** + * Create JSON parser over an array of tokens + */ +JSMN_API void jsmn_init(jsmn_parser *parser); + +/** + * Run JSON parser. It parses a JSON data string into and array of tokens, each + * describing + * a single JSON object. + */ +JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, + jsmntok_t *tokens, const unsigned int num_tokens); + +/** + * + * In-place json unescape + * + */ +void json_unescape(char* string); + +#endif /* JSMN_H */ diff --git a/lib/jsmn-shadinger-1.0/test/test-json.cpp b/lib/jsmn-shadinger-1.0/test/test-json.cpp new file mode 100644 index 000000000..ccfbf45fd --- /dev/null +++ b/lib/jsmn-shadinger-1.0/test/test-json.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include "../src/JsonParser.h" + + +static char test_simple[] = "{\"Device\":\"0x9C33\",\"Illuminance\":42,\"Occupancy\":1,\"Endpoint\":1,\"LinkQuality\":59}"; +static char test_moderate[] = "{\"ZbReceived\":{\"Prez\":{\"Device\":\"0x9C33\",\"Illuminance\":42,\"Occupancy\":1,\"Endpoint\":1,\"LinkQuality\":59}}}"; +static char test_complex[] = "{\"ZbStatus3\":[{\"Device\":\"0x7869\",\"INT\":-3,\"Name\":\"Tilt\",\"IEEEAddr\":\"0x00158D00031310F4\",\"ModelId\":\"lumi.vibration.aq1\",\"Manufacturer\":\"LUMI\",\"Endpoints\":{\"0x01\":{\"ProfileId\":\"0x0104\",\"ClustersIn\":[\"0x0000\",\"0x0003\",\"0x0019\",\"0x0101\"],\"ClustersOut\":[\"0x0000\",\"0x0004\",\"0x0003\",\"0x0005\",\"0x0019\",\"0x0101\"]},\"0x02\":{\"ProfileId\":\"0x0000\\ta\",\"ClustersIn\":[2],\"ClustersOut\":[-3,0.4,5.8]}}}]}"; + +int main(int argc, char* argv[]) { + printf("Starting... sizeof = %lu / %lu\n", sizeof(jsmntok_t), sizeof(JsonParserToken)); + + // char * json_str = test_complex; + char * json_str = test_simple; + + // JsonParser parser(64); // size for 64 tokens + + int r = parser.parse(json_str); + + printf("r = %d\n", r); + + for (uint32_t i=0; istart; + uint32_t len = token.t->len; + printf("Tok[%2d]= type=%s, start=%d, len=%d, size=%d, str ='%s'\n", i, JSMNTypeName(token.t->type), start, len, token.t->size, (token.t->type >= JSMN_STRING || 1) ? &json_str[start] : ""); + } + printf("==================\n"); + JsonParserObject root = parser.getRootObject(); + + for (const auto key : root) { + // printf("Index = %ld\n", parser.index(key)); + JsonParserToken value = key.getValue(); + printf("Key = %s, Val type = %s\n", parser.getStr(key), JSMNTypeName(value.t->type)); + if (value.isArray()) { + for (const auto arr_val : JsonParserArray(value)) { + printf("Array = %s, type = %s\n", parser.getStr(arr_val), JSMNTypeName(arr_val.t->type)); + } + } else { + printf("Value = %s\n", parser.getStr(value)); + } + } + + // root.nextOne(); + // printf("Index = %ld\n", parser.index(root)); + // root.skipObject(); + // printf("Index = %ld\n", parser.index(root)); + + JsonParserToken oc = parser.GetCaseInsensitive(root, "occupancy"); + printf("Looking for 'Occupancy': %s, %d\n", parser.getStr(oc), parser.getInt(oc)); + JsonParserToken oc2 = parser.GetCaseInsensitive(root, "occupanc"); + printf("Looking for 'Occupanc': %s, %d\n", parser.getStr(oc2), parser.getInt(oc2)); +} diff --git a/lib/mlx90640-library/MLX90640_API.cpp b/lib/mlx90640-library/MLX90640_API.cpp new file mode 100644 index 000000000..87d871530 --- /dev/null +++ b/lib/mlx90640-library/MLX90640_API.cpp @@ -0,0 +1,1640 @@ +/** + * @copyright (C) 2017 Melexis N.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include + +void ExtractVDDParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractPTATParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractGainParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractTgcParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractResolutionParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKsTaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +void ExtractCILCParameters(uint16_t *eeData, paramsMLX90640 *mlx90640); +int ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90640 *mlx90640); +int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2); +float GetMedian(float *values, int n); +int IsPixelBad(uint16_t pixel,paramsMLX90640 *params); +int ValidateFrameData(uint16_t *frameData); +int ValidateAuxData(uint16_t *auxData); +int MLX90640_I2CRead(uint8_t addr, uint32_t reg, uint16_t len, uint16_t *reg_data); +int MLX90640_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data); + +// I2C +#define I2C_BUFFER_LENGTH 128 + +int MLX90640_I2CRead(uint8_t addr, uint32_t reg, uint16_t len, uint16_t *reg_data){ + int bytesRemaining = len * 2; + int dataSpot = 0; //Start at beginning of array + while (bytesRemaining > 0) + { + Wire.beginTransmission(addr); + Wire.write(reg >> 8); //MSB + Wire.write(reg & 0xFF); //LSB + if (Wire.endTransmission(false) != 0) //Do not release bus + { + return (0); //Sensor did not ACK + } + int numberOfBytesToRead = bytesRemaining; + if (numberOfBytesToRead > I2C_BUFFER_LENGTH) numberOfBytesToRead = I2C_BUFFER_LENGTH; + Wire.requestFrom((int)addr, numberOfBytesToRead); + if (Wire.available()) + { + for (uint32_t x = 0 ; x < numberOfBytesToRead / 2; x++) + { + reg_data[dataSpot] = Wire.read() << 8; //MSB + reg_data[dataSpot] |= Wire.read(); //LSB + dataSpot++; + } + } + bytesRemaining -= numberOfBytesToRead; + reg += numberOfBytesToRead / 2; + } + return (0); //Success +} + +int MLX90640_I2CWrite(uint8_t _deviceAddress, unsigned int writeAddress, uint16_t data) +{ + Wire.beginTransmission((uint8_t)_deviceAddress); + Wire.write(writeAddress >> 8); //MSB + Wire.write(writeAddress & 0xFF); //LSB + Wire.write(data >> 8); //MSB + Wire.write(data & 0xFF); //LSB + if (Wire.endTransmission() != 0) + { + //Sensor did not ACK + return (-1); + } + uint16_t dataCheck; + MLX90640_I2CRead(_deviceAddress, writeAddress, 1, &dataCheck); + if (dataCheck != data) + { + return -2; + } + return (0); //Success +} + +int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t *eeData) +{ + return MLX90640_I2CRead(slaveAddr, 0x2400, 832, eeData); +} + +int MLX90640_SynchFrame(uint8_t slaveAddr) +{ + uint16_t dataReady = 0; + uint16_t statusRegister; + int error = 1; + + error = MLX90640_I2CWrite(slaveAddr, 0x8000, 0x0030); + if(error == -1) + { + return error; + } + + while(dataReady == 0) + { + error = MLX90640_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); + if(error != 0) + { + return error; + } + dataReady = statusRegister & 0x0008; + } + + return 0; +} + +// int MLX90640_TriggerMeasurement(uint8_t slaveAddr) // ATM not used in Tasmota +// { +// int error = 1; +// uint16_t ctrlReg; + +// error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &ctrlReg); + +// if ( error != 0) +// { +// return error; +// } + +// ctrlReg |= 0x8000; +// error = MLX90640_I2CWrite(slaveAddr, 0x800D, ctrlReg); + +// if ( error != 0) +// { +// return error; +// } + +// // error = MLX90640_I2CGeneralReset(); + +// // if ( error != 0) +// // { +// // return error; +// // } + +// error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &ctrlReg); + +// if ( error != 0) +// { +// return error; +// } + +// if ((ctrlReg & 0x8000) != 0) +// { +// return -9; +// } + +// return 0; +// } + +int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t *frameData) +{ + uint16_t dataReady = 0; + uint16_t controlRegister1; + uint16_t statusRegister; + int error = 1; + uint16_t data[64]; + uint8_t cnt = 0; + + while(dataReady == 0) + { + error = MLX90640_I2CRead(slaveAddr, 0x8000, 1, &statusRegister); + if(error != 0) + { + return error; + } + dataReady = statusRegister & 0x0008; + } + + error = MLX90640_I2CWrite(slaveAddr, 0x8000, 0x0030); + if(error == -1) + { + return error; + } + + error = MLX90640_I2CRead(slaveAddr, 0x0400, 768, frameData); + if(error != 0) + { + return error; + } + + error = MLX90640_I2CRead(slaveAddr, 0x0700, 64, data); + if(error != 0) + { + return error; + } + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + frameData[832] = controlRegister1; + frameData[833] = statusRegister & 0x0001; + + if(error != 0) + { + return error; + } + + error = ValidateAuxData(data); + if(error == 0) + { + for(cnt=0; cnt<64; cnt++) + { + frameData[cnt+768] = data[cnt]; + } + } + + error = ValidateFrameData(frameData); + if (error != 0) + { + return error; + } + + return frameData[833]; +} + +int ValidateFrameData(uint16_t *frameData) +{ + uint8_t line = 0; + + for(int i=0; i<768; i+=32) + { + if((frameData[i] == 0x7FFF) && (line%2 == frameData[833])) return -8; + line = line + 1; + } + + return 0; +} + +int ValidateAuxData(uint16_t *auxData) +{ + + if(auxData[0] == 0x7FFF) return -8; + + for(int i=8; i<19; i++) + { + if(auxData[i] == 0x7FFF) return -8; + } + + for(int i=20; i<23; i++) + { + if(auxData[i] == 0x7FFF) return -8; + } + + for(int i=24; i<33; i++) + { + if(auxData[i] == 0x7FFF) return -8; + } + + for(int i=40; i<51; i++) + { + if(auxData[i] == 0x7FFF) return -8; + } + + for(int i=52; i<55; i++) + { + if(auxData[i] == 0x7FFF) return -8; + } + + for(int i=56; i<64; i++) + { + if(auxData[i] == 0x7FFF) return -8; + } + + return 0; + +} + +int MLX90640_ExtractParameters(uint16_t *eeData, paramsMLX90640 *mlx90640, int _chunk) // Tasmota +{ + int error = 0; + switch(_chunk){ + case 0: + ExtractVDDParameters(eeData, mlx90640); + ExtractPTATParameters(eeData, mlx90640); + ExtractGainParameters(eeData, mlx90640); + ExtractTgcParameters(eeData, mlx90640); + ExtractResolutionParameters(eeData, mlx90640); + ExtractKsTaParameters(eeData, mlx90640); + ExtractKsToParameters(eeData, mlx90640); + break; + case 1: + ExtractCPParameters(eeData, mlx90640); + ExtractAlphaParameters(eeData, mlx90640); + break; + case 2: + ExtractOffsetParameters(eeData, mlx90640); + break; + case 3: + ExtractKtaPixelParameters(eeData, mlx90640); + break; + case 4: + ExtractKvPixelParameters(eeData, mlx90640); + break; + case 5: + ExtractCILCParameters(eeData, mlx90640); + error = ExtractDeviatingPixels(eeData, mlx90640); + break; + } + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution) +{ + uint16_t controlRegister1; + int value; + int error; + + value = (resolution & 0x03) << 10; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + + if(error == 0) + { + value = (controlRegister1 & 0xF3FF) | value; + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetCurResolution(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int resolutionRAM; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error != 0) + { + return error; + } + resolutionRAM = (controlRegister1 & 0x0C00) >> 10; + + return resolutionRAM; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate) +{ + uint16_t controlRegister1; + int value; + int error; + + value = (refreshRate & 0x07)<<7; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error == 0) + { + value = (controlRegister1 & 0xFC7F) | value; + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetRefreshRate(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int refreshRate; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error != 0) + { + return error; + } + refreshRate = (controlRegister1 & 0x0380) >> 7; + + return refreshRate; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetInterleavedMode(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int value; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + + if(error == 0) + { + value = (controlRegister1 & 0xEFFF); + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_SetChessMode(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int value; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + + if(error == 0) + { + value = (controlRegister1 | 0x1000); + error = MLX90640_I2CWrite(slaveAddr, 0x800D, value); + } + + return error; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetCurMode(uint8_t slaveAddr) +{ + uint16_t controlRegister1; + int modeRAM; + int error; + + error = MLX90640_I2CRead(slaveAddr, 0x800D, 1, &controlRegister1); + if(error != 0) + { + return error; + } + modeRAM = (controlRegister1 & 0x1000) >> 12; + + return modeRAM; +} + +//------------------------------------------------------------------------------ +void MLX90640_CalculateTo(uint16_t *frameData, const paramsMLX90640 *params, float emissivity, float tr, float *result, uint8_t _part) +{ + float vdd; + float ta; + float ta4; + float tr4; + float taTr; + float gain; + float irDataCP[2]; + float irData; + float alphaCompensated; + uint8_t mode; + int8_t ilPattern; + int8_t chessPattern; + int8_t pattern; + int8_t conversionPattern; + float Sx; + float To; + float alphaCorrR[4]; + int8_t range; + uint16_t subPage; + float ktaScale; + float kvScale; + float alphaScale; + float kta; + float kv; + + subPage = frameData[833]; + vdd = MLX90640_GetVdd(frameData, params); + ta = MLX90640_GetTa(frameData, params); + + ta4 = (ta + 273.15); + ta4 = ta4 * ta4; + ta4 = ta4 * ta4; + tr4 = (tr + 273.15); + tr4 = tr4 * tr4; + tr4 = tr4 * tr4; + taTr = tr4 - (tr4-ta4)/emissivity; + + ktaScale = pow(2,(double)params->ktaScale); + kvScale = pow(2,(double)params->kvScale); + alphaScale = pow(2,(double)params->alphaScale); + + alphaCorrR[0] = 1 / (1 + params->ksTo[0] * 40); + alphaCorrR[1] = 1 ; + alphaCorrR[2] = (1 + params->ksTo[1] * params->ct[2]); + alphaCorrR[3] = alphaCorrR[2] * (1 + params->ksTo[2] * (params->ct[3] - params->ct[2])); + +//------------------------- Gain calculation ----------------------------------- + gain = frameData[778]; + if(gain > 32767) + { + gain = gain - 65536; + } + + gain = params->gainEE / gain; + +//------------------------- To calculation ------------------------------------- + mode = (frameData[832] & 0x1000) >> 5; + + irDataCP[0] = frameData[776]; + irDataCP[1] = frameData[808]; + for( int i = 0; i < 2; i++) + { + if(irDataCP[i] > 32767) + { + irDataCP[i] = irDataCP[i] - 65536; + } + irDataCP[i] = irDataCP[i] * gain; + } + irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + if( mode == params->calibrationModeEE) + { + irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + } + else + { + irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); + } + + uint32_t _offset = _part*(768/2); + for( int pixelNumber = _offset; pixelNumber < (_offset+(768/2)); pixelNumber++) + { + ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; + chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2); + conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern); + + if(mode == 0) + { + pattern = ilPattern; + } + else + { + pattern = chessPattern; + } + + if(pattern == frameData[833]) + { + irData = frameData[pixelNumber]; + if(irData > 32767) + { + irData = irData - 65536; + } + irData = irData * gain; + + kta = params->kta[pixelNumber]/ktaScale; + kv = params->kv[pixelNumber]/kvScale; + irData = irData - params->offset[pixelNumber]*(1 + kta*(ta - 25))*(1 + kv*(vdd - 3.3)); + + if(mode != params->calibrationModeEE) + { + irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; + } + + irData = irData - params->tgc * irDataCP[subPage]; + irData = irData / emissivity; + + alphaCompensated = SCALEALPHA*alphaScale/params->alpha[pixelNumber]; + alphaCompensated = alphaCompensated*(1 + params->KsTa * (ta - 25)); + + Sx = alphaCompensated * alphaCompensated * alphaCompensated * (irData + alphaCompensated * taTr); + Sx = sqrt(sqrt(Sx)) * params->ksTo[1]; + + To = sqrt(sqrt(irData/(alphaCompensated * (1 - params->ksTo[1] * 273.15) + Sx) + taTr)) - 273.15; + + if(To < params->ct[1]) + { + range = 0; + } + else if(To < params->ct[2]) + { + range = 1; + } + else if(To < params->ct[3]) + { + range = 2; + } + else + { + range = 3; + } + + To = sqrt(sqrt(irData / (alphaCompensated * alphaCorrR[range] * (1 + params->ksTo[range] * (To - params->ct[range]))) + taTr)) - 273.15; + + result[pixelNumber] = To; + } + } +} + +//------------------------------------------------------------------------------ + +// void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result) +// { +// float vdd; +// float ta; +// float gain; +// float irDataCP[2]; +// float irData; +// float alphaCompensated; +// uint8_t mode; +// int8_t ilPattern; +// int8_t chessPattern; +// int8_t pattern; +// int8_t conversionPattern; +// float image; +// uint16_t subPage; +// float ktaScale; +// float kvScale; +// float kta; +// float kv; + +// subPage = frameData[833]; +// vdd = MLX90640_GetVdd(frameData, params); +// ta = MLX90640_GetTa(frameData, params); + +// ktaScale = pow(2,(double)params->ktaScale); +// kvScale = pow(2,(double)params->kvScale); + +// //------------------------- Gain calculation ----------------------------------- +// gain = frameData[778]; +// if(gain > 32767) +// { +// gain = gain - 65536; +// } + +// gain = params->gainEE / gain; + +// //------------------------- Image calculation ------------------------------------- +// mode = (frameData[832] & 0x1000) >> 5; + +// irDataCP[0] = frameData[776]; +// irDataCP[1] = frameData[808]; +// for( int i = 0; i < 2; i++) +// { +// if(irDataCP[i] > 32767) +// { +// irDataCP[i] = irDataCP[i] - 65536; +// } +// irDataCP[i] = irDataCP[i] * gain; +// } +// irDataCP[0] = irDataCP[0] - params->cpOffset[0] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); +// if( mode == params->calibrationModeEE) +// { +// irDataCP[1] = irDataCP[1] - params->cpOffset[1] * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); +// } +// else +// { +// irDataCP[1] = irDataCP[1] - (params->cpOffset[1] + params->ilChessC[0]) * (1 + params->cpKta * (ta - 25)) * (1 + params->cpKv * (vdd - 3.3)); +// } + +// for( int pixelNumber = 0; pixelNumber < 768; pixelNumber++) +// { +// ilPattern = pixelNumber / 32 - (pixelNumber / 64) * 2; +// chessPattern = ilPattern ^ (pixelNumber - (pixelNumber/2)*2); +// conversionPattern = ((pixelNumber + 2) / 4 - (pixelNumber + 3) / 4 + (pixelNumber + 1) / 4 - pixelNumber / 4) * (1 - 2 * ilPattern); + +// if(mode == 0) +// { +// pattern = ilPattern; +// } +// else +// { +// pattern = chessPattern; +// } + +// if(pattern == frameData[833]) +// { +// irData = frameData[pixelNumber]; +// if(irData > 32767) +// { +// irData = irData - 65536; +// } +// irData = irData * gain; + +// kta = params->kta[pixelNumber]/ktaScale; +// kv = params->kv[pixelNumber]/kvScale; +// irData = irData - params->offset[pixelNumber]*(1 + kta*(ta - 25))*(1 + kv*(vdd - 3.3)); + +// if(mode != params->calibrationModeEE) +// { +// irData = irData + params->ilChessC[2] * (2 * ilPattern - 1) - params->ilChessC[1] * conversionPattern; +// } + +// irData = irData - params->tgc * irDataCP[subPage]; + +// alphaCompensated = params->alpha[pixelNumber]; + +// image = irData*alphaCompensated; + +// result[pixelNumber] = image; +// } +// } +// } + +//------------------------------------------------------------------------------ + +float MLX90640_GetVdd(uint16_t *frameData, const paramsMLX90640 *params) +{ + float vdd; + float resolutionCorrection; + + int resolutionRAM; + + vdd = frameData[810]; + if(vdd > 32767) + { + vdd = vdd - 65536; + } + resolutionRAM = (frameData[832] & 0x0C00) >> 10; + resolutionCorrection = pow(2, (double)params->resolutionEE) / pow(2, (double)resolutionRAM); + vdd = (resolutionCorrection * vdd - params->vdd25) / params->kVdd + 3.3; + + return vdd; +} + +//------------------------------------------------------------------------------ + +float MLX90640_GetTa(uint16_t *frameData, const paramsMLX90640 *params) +{ + float ptat; + float ptatArt; + float vdd; + float ta; + + vdd = MLX90640_GetVdd(frameData, params); + + ptat = frameData[800]; + if(ptat > 32767) + { + ptat = ptat - 65536; + } + + ptatArt = frameData[768]; + if(ptatArt > 32767) + { + ptatArt = ptatArt - 65536; + } + ptatArt = (ptat / (ptat * params->alphaPTAT + ptatArt)) * pow(2, (double)18); + + ta = (ptatArt / (1 + params->KvPTAT * (vdd - 3.3)) - params->vPTAT25); + ta = ta / params->KtPTAT + 25; + + return ta; +} + +//------------------------------------------------------------------------------ + +int MLX90640_GetSubPageNumber(uint16_t *frameData) +{ + return frameData[833]; + +} + +//------------------------------------------------------------------------------ +void MLX90640_BadPixelsCorrection(uint16_t *pixels, float *to, int mode, paramsMLX90640 *params) +{ + float ap[4]; + uint8_t pix; + uint8_t line; + uint8_t column; + + pix = 0; + while(pixels[pix] != 0xFFFF) + { + line = pixels[pix]>>5; + column = pixels[pix] - (line<<5); + + if(mode == 1) + { + if(line == 0) + { + if(column == 0) + { + to[pixels[pix]] = to[33]; + } + else if(column == 31) + { + to[pixels[pix]] = to[62]; + } + else + { + to[pixels[pix]] = (to[pixels[pix]+31] + to[pixels[pix]+33])/2.0; + } + } + else if(line == 23) + { + if(column == 0) + { + to[pixels[pix]] = to[705]; + } + else if(column == 31) + { + to[pixels[pix]] = to[734]; + } + else + { + to[pixels[pix]] = (to[pixels[pix]-33] + to[pixels[pix]-31])/2.0; + } + } + else if(column == 0) + { + to[pixels[pix]] = (to[pixels[pix]-31] + to[pixels[pix]+33])/2.0; + } + else if(column == 31) + { + to[pixels[pix]] = (to[pixels[pix]-33] + to[pixels[pix]+31])/2.0; + } + else + { + ap[0] = to[pixels[pix]-33]; + ap[1] = to[pixels[pix]-31]; + ap[2] = to[pixels[pix]+31]; + ap[3] = to[pixels[pix]+33]; + to[pixels[pix]] = GetMedian(ap,4); + } + } + else + { + if(column == 0) + { + to[pixels[pix]] = to[pixels[pix]+1]; + } + else if(column == 1 || column == 30) + { + to[pixels[pix]] = (to[pixels[pix]-1]+to[pixels[pix]+1])/2.0; + } + else if(column == 31) + { + to[pixels[pix]] = to[pixels[pix]-1]; + } + else + { + if(IsPixelBad(pixels[pix]-2,params) == 0 && IsPixelBad(pixels[pix]+2,params) == 0) + { + ap[0] = to[pixels[pix]+1] - to[pixels[pix]+2]; + ap[1] = to[pixels[pix]-1] - to[pixels[pix]-2]; + if(fabs(ap[0]) > fabs(ap[1])) + { + to[pixels[pix]] = to[pixels[pix]-1] + ap[1]; + } + else + { + to[pixels[pix]] = to[pixels[pix]+1] + ap[0]; + } + } + else + { + to[pixels[pix]] = (to[pixels[pix]-1]+to[pixels[pix]+1])/2.0; + } + } + } + pix = pix + 1; + } +} + +//------------------------------------------------------------------------------ + +void ExtractVDDParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int16_t kVdd; + int16_t vdd25; + + kVdd = eeData[51]; + + kVdd = (eeData[51] & 0xFF00) >> 8; + if(kVdd > 127) + { + kVdd = kVdd - 256; + } + kVdd = 32 * kVdd; + vdd25 = eeData[51] & 0x00FF; + vdd25 = ((vdd25 - 256) << 5) - 8192; + + mlx90640->kVdd = kVdd; + mlx90640->vdd25 = vdd25; +} + +//------------------------------------------------------------------------------ + +void ExtractPTATParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float KvPTAT; + float KtPTAT; + int16_t vPTAT25; + float alphaPTAT; + + KvPTAT = (eeData[50] & 0xFC00) >> 10; + if(KvPTAT > 31) + { + KvPTAT = KvPTAT - 64; + } + KvPTAT = KvPTAT/4096; + + KtPTAT = eeData[50] & 0x03FF; + if(KtPTAT > 511) + { + KtPTAT = KtPTAT - 1024; + } + KtPTAT = KtPTAT/8; + + vPTAT25 = eeData[49]; + + alphaPTAT = (eeData[16] & 0xF000) / pow(2, (double)14) + 8.0f; + + mlx90640->KvPTAT = KvPTAT; + mlx90640->KtPTAT = KtPTAT; + mlx90640->vPTAT25 = vPTAT25; + mlx90640->alphaPTAT = alphaPTAT; +} + +//------------------------------------------------------------------------------ + +void ExtractGainParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int16_t gainEE; + + gainEE = eeData[48]; + if(gainEE > 32767) + { + gainEE = gainEE -65536; + } + + mlx90640->gainEE = gainEE; +} + +//------------------------------------------------------------------------------ + +void ExtractTgcParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float tgc; + tgc = eeData[60] & 0x00FF; + if(tgc > 127) + { + tgc = tgc - 256; + } + tgc = tgc / 32.0f; + + mlx90640->tgc = tgc; +} + +//------------------------------------------------------------------------------ + +void ExtractResolutionParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + uint8_t resolutionEE; + resolutionEE = (eeData[56] & 0x3000) >> 12; + + mlx90640->resolutionEE = resolutionEE; +} + +//------------------------------------------------------------------------------ + +void ExtractKsTaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float KsTa; + KsTa = (eeData[60] & 0xFF00) >> 8; + if(KsTa > 127) + { + KsTa = KsTa -256; + } + KsTa = KsTa / 8192.0f; + + mlx90640->KsTa = KsTa; +} + +//------------------------------------------------------------------------------ + +void ExtractKsToParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int KsToScale; + int8_t step; + + step = ((eeData[63] & 0x3000) >> 12) * 10; + + mlx90640->ct[0] = -40; + mlx90640->ct[1] = 0; + mlx90640->ct[2] = (eeData[63] & 0x00F0) >> 4; + mlx90640->ct[3] = (eeData[63] & 0x0F00) >> 8; + + mlx90640->ct[2] = mlx90640->ct[2]*step; + mlx90640->ct[3] = mlx90640->ct[2] + mlx90640->ct[3]*step; + mlx90640->ct[4] = 400; + + KsToScale = (eeData[63] & 0x000F) + 8; + KsToScale = 1 << KsToScale; + + mlx90640->ksTo[0] = eeData[61] & 0x00FF; + mlx90640->ksTo[1] = (eeData[61] & 0xFF00) >> 8; + mlx90640->ksTo[2] = eeData[62] & 0x00FF; + mlx90640->ksTo[3] = (eeData[62] & 0xFF00) >> 8; + + for(int i = 0; i < 4; i++) + { + if(mlx90640->ksTo[i] > 127) + { + mlx90640->ksTo[i] = mlx90640->ksTo[i] - 256; + } + mlx90640->ksTo[i] = mlx90640->ksTo[i] / KsToScale; + } + + mlx90640->ksTo[4] = -0.0002; +} + +//------------------------------------------------------------------------------ + +void ExtractAlphaParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int accRow[24]; + int accColumn[32]; + int p = 0; + int alphaRef; + uint8_t alphaScale; + uint8_t accRowScale; + uint8_t accColumnScale; + uint8_t accRemScale; + float alphaTemp[768]; + float temp; + + + accRemScale = eeData[32] & 0x000F; + accColumnScale = (eeData[32] & 0x00F0) >> 4; + accRowScale = (eeData[32] & 0x0F00) >> 8; + alphaScale = ((eeData[32] & 0xF000) >> 12) + 30; + alphaRef = eeData[33]; + + for(int i = 0; i < 6; i++) + { + p = i * 4; + accRow[p + 0] = (eeData[34 + i] & 0x000F); + accRow[p + 1] = (eeData[34 + i] & 0x00F0) >> 4; + accRow[p + 2] = (eeData[34 + i] & 0x0F00) >> 8; + accRow[p + 3] = (eeData[34 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 24; i++) + { + if (accRow[i] > 7) + { + accRow[i] = accRow[i] - 16; + } + } + + for(int i = 0; i < 8; i++) + { + p = i * 4; + accColumn[p + 0] = (eeData[40 + i] & 0x000F); + accColumn[p + 1] = (eeData[40 + i] & 0x00F0) >> 4; + accColumn[p + 2] = (eeData[40 + i] & 0x0F00) >> 8; + accColumn[p + 3] = (eeData[40 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 32; i ++) + { + if (accColumn[i] > 7) + { + accColumn[i] = accColumn[i] - 16; + } + } + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + alphaTemp[p] = (eeData[64 + p] & 0x03F0) >> 4; + if (alphaTemp[p] > 31) + { + alphaTemp[p] = alphaTemp[p] - 64; + } + alphaTemp[p] = alphaTemp[p]*(1 << accRemScale); + alphaTemp[p] = (alphaRef + (accRow[i] << accRowScale) + (accColumn[j] << accColumnScale) + alphaTemp[p]); + alphaTemp[p] = alphaTemp[p] / pow(2,(double)alphaScale); + alphaTemp[p] = alphaTemp[p] - mlx90640->tgc * (mlx90640->cpAlpha[0] + mlx90640->cpAlpha[1])/2; + alphaTemp[p] = SCALEALPHA/alphaTemp[p]; + } + } + + temp = alphaTemp[0]; + for(int i = 1; i < 768; i++) + { + if (alphaTemp[i] > temp) + { + temp = alphaTemp[i]; + } + } + + alphaScale = 0; + while(temp < 32767.4) + { + temp = temp*2; + alphaScale = alphaScale + 1; + } + + for(int i = 0; i < 768; i++) + { + temp = alphaTemp[i] * pow(2,(double)alphaScale); + mlx90640->alpha[i] = (temp + 0.5); + + } + + mlx90640->alphaScale = alphaScale; + +} + +//------------------------------------------------------------------------------ + +void ExtractOffsetParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int occRow[24]; + int occColumn[32]; + int p = 0; + int16_t offsetRef; + uint8_t occRowScale; + uint8_t occColumnScale; + uint8_t occRemScale; + + + occRemScale = (eeData[16] & 0x000F); + occColumnScale = (eeData[16] & 0x00F0) >> 4; + occRowScale = (eeData[16] & 0x0F00) >> 8; + offsetRef = eeData[17]; + if (offsetRef > 32767) + { + offsetRef = offsetRef - 65536; + } + + for(int i = 0; i < 6; i++) + { + p = i * 4; + occRow[p + 0] = (eeData[18 + i] & 0x000F); + occRow[p + 1] = (eeData[18 + i] & 0x00F0) >> 4; + occRow[p + 2] = (eeData[18 + i] & 0x0F00) >> 8; + occRow[p + 3] = (eeData[18 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 24; i++) + { + if (occRow[i] > 7) + { + occRow[i] = occRow[i] - 16; + } + } + + for(int i = 0; i < 8; i++) + { + p = i * 4; + occColumn[p + 0] = (eeData[24 + i] & 0x000F); + occColumn[p + 1] = (eeData[24 + i] & 0x00F0) >> 4; + occColumn[p + 2] = (eeData[24 + i] & 0x0F00) >> 8; + occColumn[p + 3] = (eeData[24 + i] & 0xF000) >> 12; + } + + for(int i = 0; i < 32; i ++) + { + if (occColumn[i] > 7) + { + occColumn[i] = occColumn[i] - 16; + } + } + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + mlx90640->offset[p] = (eeData[64 + p] & 0xFC00) >> 10; + if (mlx90640->offset[p] > 31) + { + mlx90640->offset[p] = mlx90640->offset[p] - 64; + } + mlx90640->offset[p] = mlx90640->offset[p]*(1 << occRemScale); + mlx90640->offset[p] = (offsetRef + (occRow[i] << occRowScale) + (occColumn[j] << occColumnScale) + mlx90640->offset[p]); + } + } +} + +//------------------------------------------------------------------------------ + +void ExtractKtaPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int p = 0; + int8_t KtaRC[4]; + int8_t KtaRoCo; + int8_t KtaRoCe; + int8_t KtaReCo; + int8_t KtaReCe; + uint8_t ktaScale1; + uint8_t ktaScale2; + uint8_t split; + float ktaTemp[768]; + float temp; + + KtaRoCo = (eeData[54] & 0xFF00) >> 8; + if (KtaRoCo > 127) + { + KtaRoCo = KtaRoCo - 256; + } + KtaRC[0] = KtaRoCo; + + KtaReCo = (eeData[54] & 0x00FF); + if (KtaReCo > 127) + { + KtaReCo = KtaReCo - 256; + } + KtaRC[2] = KtaReCo; + + KtaRoCe = (eeData[55] & 0xFF00) >> 8; + if (KtaRoCe > 127) + { + KtaRoCe = KtaRoCe - 256; + } + KtaRC[1] = KtaRoCe; + + KtaReCe = (eeData[55] & 0x00FF); + if (KtaReCe > 127) + { + KtaReCe = KtaReCe - 256; + } + KtaRC[3] = KtaReCe; + + ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8; + ktaScale2 = (eeData[56] & 0x000F); + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + split = 2*(p/32 - (p/64)*2) + p%2; + ktaTemp[p] = (eeData[64 + p] & 0x000E) >> 1; + if (ktaTemp[p] > 3) + { + ktaTemp[p] = ktaTemp[p] - 8; + } + ktaTemp[p] = ktaTemp[p] * (1 << ktaScale2); + ktaTemp[p] = KtaRC[split] + ktaTemp[p]; + ktaTemp[p] = ktaTemp[p] / pow(2,(double)ktaScale1); + //ktaTemp[p] = ktaTemp[p] * mlx90640->offset[p]; + } + } + + temp = fabs(ktaTemp[0]); + for(int i = 1; i < 768; i++) + { + if (fabs(ktaTemp[i]) > temp) + { + temp = fabs(ktaTemp[i]); + } + } + + ktaScale1 = 0; + while(temp < 63.4) + { + temp = temp*2; + ktaScale1 = ktaScale1 + 1; + } + + for(int i = 0; i < 768; i++) + { + temp = ktaTemp[i] * pow(2,(double)ktaScale1); + if (temp < 0) + { + mlx90640->kta[i] = (temp - 0.5); + } + else + { + mlx90640->kta[i] = (temp + 0.5); + } + + } + + mlx90640->ktaScale = ktaScale1; +} + + +//------------------------------------------------------------------------------ + +void ExtractKvPixelParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + int p = 0; + int8_t KvT[4]; + int8_t KvRoCo; + int8_t KvRoCe; + int8_t KvReCo; + int8_t KvReCe; + uint8_t kvScale; + uint8_t split; + float kvTemp[768]; + float temp; + + KvRoCo = (eeData[52] & 0xF000) >> 12; + if (KvRoCo > 7) + { + KvRoCo = KvRoCo - 16; + } + KvT[0] = KvRoCo; + + KvReCo = (eeData[52] & 0x0F00) >> 8; + if (KvReCo > 7) + { + KvReCo = KvReCo - 16; + } + KvT[2] = KvReCo; + + KvRoCe = (eeData[52] & 0x00F0) >> 4; + if (KvRoCe > 7) + { + KvRoCe = KvRoCe - 16; + } + KvT[1] = KvRoCe; + + KvReCe = (eeData[52] & 0x000F); + if (KvReCe > 7) + { + KvReCe = KvReCe - 16; + } + KvT[3] = KvReCe; + + kvScale = (eeData[56] & 0x0F00) >> 8; + + + for(int i = 0; i < 24; i++) + { + for(int j = 0; j < 32; j ++) + { + p = 32 * i +j; + split = 2*(p/32 - (p/64)*2) + p%2; + kvTemp[p] = KvT[split]; + kvTemp[p] = kvTemp[p] / pow(2,(double)kvScale); + //kvTemp[p] = kvTemp[p] * mlx90640->offset[p]; + } + } + + temp = fabs(kvTemp[0]); + for(int i = 1; i < 768; i++) + { + if (fabs(kvTemp[i]) > temp) + { + temp = fabs(kvTemp[i]); + } + } + + kvScale = 0; + while(temp < 63.4) + { + temp = temp*2; + kvScale = kvScale + 1; + } + + for(int i = 0; i < 768; i++) + { + temp = kvTemp[i] * pow(2,(double)kvScale); + if (temp < 0) + { + mlx90640->kv[i] = (temp - 0.5); + } + else + { + mlx90640->kv[i] = (temp + 0.5); + } + + } + + mlx90640->kvScale = kvScale; +} + +//------------------------------------------------------------------------------ + +void ExtractCPParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float alphaSP[2]; + int16_t offsetSP[2]; + float cpKv; + float cpKta; + uint8_t alphaScale; + uint8_t ktaScale1; + uint8_t kvScale; + + alphaScale = ((eeData[32] & 0xF000) >> 12) + 27; + + offsetSP[0] = (eeData[58] & 0x03FF); + if (offsetSP[0] > 511) + { + offsetSP[0] = offsetSP[0] - 1024; + } + + offsetSP[1] = (eeData[58] & 0xFC00) >> 10; + if (offsetSP[1] > 31) + { + offsetSP[1] = offsetSP[1] - 64; + } + offsetSP[1] = offsetSP[1] + offsetSP[0]; + + alphaSP[0] = (eeData[57] & 0x03FF); + if (alphaSP[0] > 511) + { + alphaSP[0] = alphaSP[0] - 1024; + } + alphaSP[0] = alphaSP[0] / pow(2,(double)alphaScale); + + alphaSP[1] = (eeData[57] & 0xFC00) >> 10; + if (alphaSP[1] > 31) + { + alphaSP[1] = alphaSP[1] - 64; + } + alphaSP[1] = (1 + alphaSP[1]/128) * alphaSP[0]; + + cpKta = (eeData[59] & 0x00FF); + if (cpKta > 127) + { + cpKta = cpKta - 256; + } + ktaScale1 = ((eeData[56] & 0x00F0) >> 4) + 8; + mlx90640->cpKta = cpKta / pow(2,(double)ktaScale1); + + cpKv = (eeData[59] & 0xFF00) >> 8; + if (cpKv > 127) + { + cpKv = cpKv - 256; + } + kvScale = (eeData[56] & 0x0F00) >> 8; + mlx90640->cpKv = cpKv / pow(2,(double)kvScale); + + mlx90640->cpAlpha[0] = alphaSP[0]; + mlx90640->cpAlpha[1] = alphaSP[1]; + mlx90640->cpOffset[0] = offsetSP[0]; + mlx90640->cpOffset[1] = offsetSP[1]; +} + +//------------------------------------------------------------------------------ + +void ExtractCILCParameters(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + float ilChessC[3]; + uint8_t calibrationModeEE; + + calibrationModeEE = (eeData[10] & 0x0800) >> 4; + calibrationModeEE = calibrationModeEE ^ 0x80; + + ilChessC[0] = (eeData[53] & 0x003F); + if (ilChessC[0] > 31) + { + ilChessC[0] = ilChessC[0] - 64; + } + ilChessC[0] = ilChessC[0] / 16.0f; + + ilChessC[1] = (eeData[53] & 0x07C0) >> 6; + if (ilChessC[1] > 15) + { + ilChessC[1] = ilChessC[1] - 32; + } + ilChessC[1] = ilChessC[1] / 2.0f; + + ilChessC[2] = (eeData[53] & 0xF800) >> 11; + if (ilChessC[2] > 15) + { + ilChessC[2] = ilChessC[2] - 32; + } + ilChessC[2] = ilChessC[2] / 8.0f; + + mlx90640->calibrationModeEE = calibrationModeEE; + mlx90640->ilChessC[0] = ilChessC[0]; + mlx90640->ilChessC[1] = ilChessC[1]; + mlx90640->ilChessC[2] = ilChessC[2]; +} + +//------------------------------------------------------------------------------ + +int ExtractDeviatingPixels(uint16_t *eeData, paramsMLX90640 *mlx90640) +{ + uint16_t pixCnt = 0; + uint16_t brokenPixCnt = 0; + uint16_t outlierPixCnt = 0; + int warn = 0; + int i; + + for(pixCnt = 0; pixCnt<5; pixCnt++) + { + mlx90640->brokenPixels[pixCnt] = 0xFFFF; + mlx90640->outlierPixels[pixCnt] = 0xFFFF; + } + + pixCnt = 0; + while (pixCnt < 768 && brokenPixCnt < 5 && outlierPixCnt < 5) + { + if(eeData[pixCnt+64] == 0) + { + mlx90640->brokenPixels[brokenPixCnt] = pixCnt; + brokenPixCnt = brokenPixCnt + 1; + } + else if((eeData[pixCnt+64] & 0x0001) != 0) + { + mlx90640->outlierPixels[outlierPixCnt] = pixCnt; + outlierPixCnt = outlierPixCnt + 1; + } + + pixCnt = pixCnt + 1; + + } + + if(brokenPixCnt > 4) + { + warn = -3; + } + else if(outlierPixCnt > 4) + { + warn = -4; + } + else if((brokenPixCnt + outlierPixCnt) > 4) + { + warn = -5; + } + else + { + for(pixCnt=0; pixCntbrokenPixels[pixCnt],mlx90640->brokenPixels[i]); + if(warn != 0) + { + return warn; + } + } + } + + for(pixCnt=0; pixCntoutlierPixels[pixCnt],mlx90640->outlierPixels[i]); + if(warn != 0) + { + return warn; + } + } + } + + for(pixCnt=0; pixCntbrokenPixels[pixCnt],mlx90640->outlierPixels[i]); + if(warn != 0) + { + return warn; + } + } + } + + } + + + return warn; + +} + +//------------------------------------------------------------------------------ + + int CheckAdjacentPixels(uint16_t pix1, uint16_t pix2) + { + int pixPosDif; + + pixPosDif = pix1 - pix2; + if(pixPosDif > -34 && pixPosDif < -30) + { + return -6; + } + if(pixPosDif > -2 && pixPosDif < 2) + { + return -6; + } + if(pixPosDif > 30 && pixPosDif < 34) + { + return -6; + } + + return 0; + } + +//------------------------------------------------------------------------------ + +float GetMedian(float *values, int n) + { + float temp; + + for(int i=0; ioutlierPixels[i] || pixel == params->brokenPixels[i]) + { + return 1; + } + } + + return 0; +} + +//------------------------------------------------------------------------------ diff --git a/lib/mlx90640-library/MLX90640_API.h b/lib/mlx90640-library/MLX90640_API.h new file mode 100644 index 000000000..efbcdff5b --- /dev/null +++ b/lib/mlx90640-library/MLX90640_API.h @@ -0,0 +1,74 @@ +/** + * @copyright (C) 2017 Melexis N.V. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef _MLX90640_API_H_ +#define _MLX90640_API_H_ + +#include + +#define SCALEALPHA 0.000001 + +typedef struct + { + int16_t kVdd; + int16_t vdd25; + float KvPTAT; + float KtPTAT; + uint16_t vPTAT25; + float alphaPTAT; + int16_t gainEE; + float tgc; + float cpKv; + float cpKta; + uint8_t resolutionEE; + uint8_t calibrationModeEE; + float KsTa; + float ksTo[5]; + int16_t ct[5]; + uint16_t alpha[768]; + uint8_t alphaScale; + int16_t offset[768]; + int8_t kta[768]; + uint8_t ktaScale; + int8_t kv[768]; + uint8_t kvScale; + float cpAlpha[2]; + int16_t cpOffset[2]; + float ilChessC[3]; + uint16_t brokenPixels[5]; + uint16_t outlierPixels[5]; + } paramsMLX90640; + + int MLX90640_DumpEE(uint8_t slaveAddr, uint16_t *eeData); + int MLX90640_SynchFrame(uint8_t slaveAddr); + // int MLX90640_TriggerMeasurement(uint8_t slaveAddr); + int MLX90640_GetFrameData(uint8_t slaveAddr, uint16_t *frameData); + int MLX90640_ExtractParameters(uint16_t *eeData, paramsMLX90640 *mlx90640,int _chunk); + float MLX90640_GetVdd(uint16_t *frameData, const paramsMLX90640 *params); + float MLX90640_GetTa(uint16_t *frameData, const paramsMLX90640 *params); + // void MLX90640_GetImage(uint16_t *frameData, const paramsMLX90640 *params, float *result); + void MLX90640_CalculateTo(uint16_t *frameData, const paramsMLX90640 *params, float emissivity, float tr, float *result, uint8_t _part); + int MLX90640_SetResolution(uint8_t slaveAddr, uint8_t resolution); + int MLX90640_GetCurResolution(uint8_t slaveAddr); + int MLX90640_SetRefreshRate(uint8_t slaveAddr, uint8_t refreshRate); + int MLX90640_GetRefreshRate(uint8_t slaveAddr); + int MLX90640_GetSubPageNumber(uint16_t *frameData); + int MLX90640_GetCurMode(uint8_t slaveAddr); + int MLX90640_SetInterleavedMode(uint8_t slaveAddr); + int MLX90640_SetChessMode(uint8_t slaveAddr); + void MLX90640_BadPixelsCorrection(uint16_t *pixels, float *to, int mode, paramsMLX90640 *params); + +#endif diff --git a/lib/vl53l1x-arduino-1.01/LICENSE.txt b/lib/vl53l1x-arduino-1.01/LICENSE.txt new file mode 100644 index 000000000..8a2abe9ed --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/LICENSE.txt @@ -0,0 +1,42 @@ +Most of the functionality of this library is based on the VL53L1X API provided +provided by ST (STSW-IMG007), and some of the explanatory comments are quoted +or paraphrased from the API source code, API user manual (UM2356), and VL53L1X +datasheet. Therefore, the license terms for the API source code (BSD 3-clause +"New" or "Revised" License) also apply to this derivative work, as specified +below. + +For more information, see + +https://www.pololu.com/ +https://forum.pololu.com/ + +-------------------------------------------------------------------------------- + +Copyright (c) 2017, STMicroelectronics +Copyright (c) 2018, Pololu Corporation +All Rights Reserved + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/lib/vl53l1x-arduino-1.01/README.md b/lib/vl53l1x-arduino-1.01/README.md new file mode 100644 index 000000000..41ec21d9f --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/README.md @@ -0,0 +1,167 @@ +# VL53L1X library for Arduino + +Version: 1.0.1
+Release date: 2018-09-19
+[![Build Status](https://travis-ci.org/pololu/vl53l1x-arduino.svg?branch=master)](https://travis-ci.org/pololu/vl53l1x-arduino)
+[www.pololu.com](https://www.pololu.com/) + +## Summary + +This is a library for the Arduino IDE that helps interface with ST's [VL53L1X time-of-flight distance sensor](https://www.pololu.com/product/3415). The library makes it simple to configure the sensor and read range data from it via I²C. + +## Supported platforms + +This library is designed to work with the Arduino IDE versions 1.6.x or later; we have not tested it with earlier versions. This library should support any Arduino-compatible board, including the [Pololu A-Star controllers](https://www.pololu.com/category/149/a-star-programmable-controllers). + +## Getting started + +### Hardware + +A [VL53L1X carrier](https://www.pololu.com/product/3415) can be purchased from Pololu's website. Before continuing, careful reading of the [product page](https://www.pololu.com/product/3415) as well as the VL53L1X datasheet is recommended. + +Make the following connections between the Arduino and the VL53L1X board: + +#### 5V Arduino boards + +(including Arduino Uno, Leonardo, Mega; Pololu A-Star 32U4) + + Arduino VL53L1X board + ------- ------------- + 5V - VIN + GND - GND + SDA - SDA + SCL - SCL + +#### 3.3V Arduino boards + +(including Arduino Due) + + Arduino VL53L1X board + ------- ------------- + 3V3 - VIN + GND - GND + SDA - SDA + SCL - SCL + +### Software + +If you are using version 1.6.2 or later of the [Arduino software (IDE)](http://www.arduino.cc/en/Main/Software), you can use the Library Manager to install this library: + +1. In the Arduino IDE, open the "Sketch" menu, select "Include Library", then "Manage Libraries...". +2. Search for "VL53L1X". +3. Click the VL53L1X entry in the list. +4. Click "Install". + +If this does not work, you can manually install the library: + +1. Download the [latest release archive from GitHub](https://github.com/pololu/vl53l1x-arduino/releases) and decompress it. +2. Rename the folder "vl53l1x-arduino-master" to "VL53L1X". +3. Move the "VL53L1X" folder into the "libraries" directory inside your Arduino sketchbook directory. You can view your sketchbook location by opening the "File" menu and selecting "Preferences" in the Arduino IDE. If there is not already a "libraries" folder in that location, you should make the folder yourself. +4. After installing the library, restart the Arduino IDE. + +## Examples + +Several example sketches are available that show how to use the library. You can access them from the Arduino IDE by opening the "File" menu, selecting "Examples", and then selecting "VL53L1X". If you cannot find these examples, the library was probably installed incorrectly and you should retry the installation instructions above. + +## ST's VL53L1X API and this library + +Most of the functionality of this library is based on the [VL53L1X API](http://www.st.com/content/st_com/en/products/embedded-software/proximity-sensors-software/stsw-img007.html) provided by ST (STSW-IMG007), and some of the explanatory comments in the code are quoted or paraphrased from the API source code, API user manual (UM2356), and the VL53L1X datasheet. For more explanation about the library code and how it was derived from the API, see the comments in VL53L1X.cpp. + +This library is intended to provide a quicker and easier way to get started using the VL53L1X with an Arduino-compatible controller, in contrast to using ST's API on the Arduino. The library has a more streamlined interface, as well as smaller storage and memory footprints. However, it does not currently implement some of the more advanced functionality available in the API (for example, calibrating the sensor to work well under a cover glass or selecting a smaller region of interest (ROI)), and it has less robust error checking. For advanced applications, especially when storage and memory are less of an issue, consider using the VL53L1X API directly. We have an [implementation of ST's VL53L1X API for Arduino](https://github.com/pololu/vl53l1x-st-api-arduino) available. + +## Library reference + +* `RangingData ranging_data`
+ This struct contains information about the last ranging measurement. Its members are: + * `uint16_t range_mm`
+ Range reading from the last measurement, in millimeters. (This reading can also be obtained as the return value of `read()`.) + * `RangeStatus range_status`
+ Status of the last measurement; see the definition of the `RangeStatus` enumeration type in VL53L1X.h (or the API user manual and source code) for descriptions of the possible statuses. A status of `VL53L1X::RangeValid` means there were no problems with the measurement. + * `float peak_signal_count_rate_MCPS`
+ Peak signal count rate of the last measurement, in units of mega counts per second. + * `float ambient_count_rate_MCPS`
+ Ambient count rate of the last measurement, in units of mega counts per second. + +* `uint8_t last_status`
+ The status of the last I²C write transmission. See the [`Wire.endTransmission()` documentation](http://arduino.cc/en/Reference/WireEndTransmission) for return values. + +* `VL53L1X()`
+ Constructor. + +* `void setAddress(uint8_t new_addr)`
+ Changes the I²C slave device address of the VL53L1X to the given value (7-bit). + +* `uint8_t getAddress()`
+ Returns the current I²C address. + +* `bool init(bool io_2v8 = true)`
+ Iniitializes and configures the sensor. If the optional argument `io_2v8` is true (the default if not specified), the sensor is configured for 2V8 mode (2.8 V I/O); if false, the sensor is left in 1V8 mode. The return value is a boolean indicating whether the initialization completed successfully. + +* `void writeReg(uint16_t reg, uint8_t value)`
+ Writes an 8-bit sensor register with the given value. + + Register address constants are defined by the `regAddr` enumeration type in VL53L1X.h.
+ Example use: `sensor.writeReg(VL53L1X::SOFT_RESET, 0x00);` + +* `void writeReg16Bit(uint16_t reg, uint16_t value)`
+ Writes a 16-bit sensor register with the given value. + +* `void writeReg32Bit(uint16_t reg, uint32_t value)`
+ Writes a 32-bit sensor register with the given value. + +* `uint8_t readReg(uint16_t reg)`
+ Reads an 8-bit sensor register and returns the value read. + +* `uint16_t readReg16Bit(uint16_t reg)`
+ Reads a 16-bit sensor register and returns the value read. + +* `uint32_t readReg32Bit(uint16_t reg)`
+ Reads a 32-bit sensor register and returns the value read. + +* `bool setDistanceMode(DistanceMode mode)`
+ Sets the distance mode of the sensor (`VL53L1X::Short`, `VL53L1X::Medium`, or `VL53L1X::Long`). Shorter distance modes are less affected by ambient light but have lower maximum ranges. See the datasheet for more information. The return value is a boolean indicating whether the requested mode was valid. + +* `DistanceMode getDistanceMode()`
+ Returns the previously set distance mode. + +* `bool setMeasurementTimingBudget(uint32_t budget_us)`
+ Sets the measurement timing budget to the given value in microseconds. This is the time allowed for one range measurement; a longer timing budget allows for more accurate measurements. The minimum budget is 20 ms (20000 us) in short distance mode and 33 ms for medium and long distance modes. See the VL53L1X datasheet for more information on range and timing limits. The return value is a boolean indicating whether the requested budget was valid. + +* `uint32_t getMeasurementTimingBudget()`
+ Returns the current measurement timing budget in microseconds. + +* `void startContinuous(uint32_t period_ms)`
+ Starts continuous ranging measurements. The specified inter-measurement period in milliseconds determines how often the sensor takes a measurement; if it is shorter than the timing budget, the sensor will start a new measurement as soon as the previous one finishes. + +* `void stopContinuous()`
+ Stops continuous mode. + +* `uint16_t read(bool blocking = true)`
+ After continuous ranging measurements have been started, calling this function returns a range reading in millimeters and updates the `ranging_data` struct with details about the last measurement. If the optional argument `blocking` is true (the default if not specified), this function will wait until data from a new measurement is available before returning. + + If you do not want this function to block, you can use the `dataReady()` function to check if new data is available before calling `read(false)`. Calling `read(false)` before new data is available will return a reading of 0, and `ranging_data.range_status` will have the value `VL53L1X::None`, indicating that there has been no update. + +* `uint16_t readRangeContinuousMillimeters(bool blocking = true)`
+ Alias of `read()` for convenience. + +* `bool dataReady()`
+ Returns a boolean indicating whether data from a new measurement is available from the sensor. + +* `static const char * rangeStatusToString(RangeStatus status)`
+ Converts a `RangeStatus` into a readable string describing that status. + + Note that on an AVR, the strings in this function are stored in RAM (dynamic memory), which makes working with them easier but uses up 200+ bytes of RAM (many AVR-based Arduinos only have about 2000 bytes of RAM). You can avoid this memory usage if you do not call this function in your sketch. + +* `void setTimeout(uint16_t timeout)`
+ Sets a timeout period in milliseconds after which read operations will abort if the sensor is not ready. A value of 0 disables the timeout. + +* `uint16_t getTimeout()`
+ Returns the current timeout period setting. + +* `bool timeoutOccurred()`
+ Indicates whether a read timeout has occurred since the last call to `timeoutOccurred()`. + +## Version history + +* 1.0.1 (2018-09-19): Fix Arduino 101 hanging in init(). +* 1.0.0 (2018-05-31): Original release. diff --git a/lib/vl53l1x-arduino-1.01/VL53L1X.cpp b/lib/vl53l1x-arduino-1.01/VL53L1X.cpp new file mode 100644 index 000000000..78d93e2d2 --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/VL53L1X.cpp @@ -0,0 +1,788 @@ +// Most of the functionality of this library is based on the VL53L1X API +// provided by ST (STSW-IMG007), and some of the explanatory comments are quoted +// or paraphrased from the API source code, API user manual (UM2356), and +// VL53L1X datasheet. + +#include +#include + +// Constructors //////////////////////////////////////////////////////////////// + +VL53L1X::VL53L1X() + : address(AddressDefault) + , io_timeout(0) // no timeout + , did_timeout(false) + , calibrated(false) + , saved_vhv_init(0) + , saved_vhv_timeout(0) + , distance_mode(Unknown) +{ +} + +// Public Methods ////////////////////////////////////////////////////////////// + +void VL53L1X::setAddress(uint8_t new_addr) +{ + writeReg(I2C_SLAVE__DEVICE_ADDRESS, new_addr & 0x7F); + address = new_addr; +} + +// Initialize sensor using settings taken mostly from VL53L1_DataInit() and +// VL53L1_StaticInit(). +// If io_2v8 (optional) is true or not given, the sensor is configured for 2V8 +// mode. +bool VL53L1X::init(bool io_2v8) +{ + // check model ID and module type registers (values specified in datasheet) + if (readReg16Bit(IDENTIFICATION__MODEL_ID) != 0xEACC) { return false; } + + // VL53L1_software_reset() begin + + writeReg(SOFT_RESET, 0x00); + delayMicroseconds(100); + writeReg(SOFT_RESET, 0x01); + + // give it some time to boot; otherwise the sensor NACKs during the readReg() + // call below and the Arduino 101 doesn't seem to handle that well + delay(1); + + // VL53L1_poll_for_boot_completion() begin + + startTimeout(); + + // check last_status in case we still get a NACK to try to deal with it correctly + while ((readReg(FIRMWARE__SYSTEM_STATUS) & 0x01) == 0 || last_status != 0) + { + if (checkTimeoutExpired()) + { + did_timeout = true; + return false; + } + } + // VL53L1_poll_for_boot_completion() end + + // VL53L1_software_reset() end + + // VL53L1_DataInit() begin + + // sensor uses 1V8 mode for I/O by default; switch to 2V8 mode if necessary + if (io_2v8) + { + writeReg(PAD_I2C_HV__EXTSUP_CONFIG, + readReg(PAD_I2C_HV__EXTSUP_CONFIG) | 0x01); + } + + // store oscillator info for later use + fast_osc_frequency = readReg16Bit(OSC_MEASURED__FAST_OSC__FREQUENCY); + osc_calibrate_val = readReg16Bit(RESULT__OSC_CALIBRATE_VAL); + + // VL53L1_DataInit() end + + // VL53L1_StaticInit() begin + + // Note that the API does not actually apply the configuration settings below + // when VL53L1_StaticInit() is called: it keeps a copy of the sensor's + // register contents in memory and doesn't actually write them until a + // measurement is started. Writing the configuration here means we don't have + // to keep it all in memory and avoids a lot of redundant writes later. + + // the API sets the preset mode to LOWPOWER_AUTONOMOUS here: + // VL53L1_set_preset_mode() begin + + // VL53L1_preset_mode_standard_ranging() begin + + // values labeled "tuning parm default" are from vl53l1_tuning_parm_defaults.h + // (API uses these in VL53L1_init_tuning_parm_storage_struct()) + + // static config + // API resets PAD_I2C_HV__EXTSUP_CONFIG here, but maybe we don't want to do + // that? (seems like it would disable 2V8 mode) + writeReg16Bit(DSS_CONFIG__TARGET_TOTAL_RATE_MCPS, TargetRate); // should already be this value after reset + writeReg(GPIO__TIO_HV_STATUS, 0x02); + writeReg(SIGMA_ESTIMATOR__EFFECTIVE_PULSE_WIDTH_NS, 8); // tuning parm default + writeReg(SIGMA_ESTIMATOR__EFFECTIVE_AMBIENT_WIDTH_NS, 16); // tuning parm default + writeReg(ALGO__CROSSTALK_COMPENSATION_VALID_HEIGHT_MM, 0x01); + writeReg(ALGO__RANGE_IGNORE_VALID_HEIGHT_MM, 0xFF); + writeReg(ALGO__RANGE_MIN_CLIP, 0); // tuning parm default + writeReg(ALGO__CONSISTENCY_CHECK__TOLERANCE, 2); // tuning parm default + + // general config + writeReg16Bit(SYSTEM__THRESH_RATE_HIGH, 0x0000); + writeReg16Bit(SYSTEM__THRESH_RATE_LOW, 0x0000); + writeReg(DSS_CONFIG__APERTURE_ATTENUATION, 0x38); + + // timing config + // most of these settings will be determined later by distance and timing + // budget configuration + writeReg16Bit(RANGE_CONFIG__SIGMA_THRESH, 360); // tuning parm default + writeReg16Bit(RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, 192); // tuning parm default + + // dynamic config + + writeReg(SYSTEM__GROUPED_PARAMETER_HOLD_0, 0x01); + writeReg(SYSTEM__GROUPED_PARAMETER_HOLD_1, 0x01); + writeReg(SD_CONFIG__QUANTIFIER, 2); // tuning parm default + + // VL53L1_preset_mode_standard_ranging() end + + // from VL53L1_preset_mode_timed_ranging_* + // GPH is 0 after reset, but writing GPH0 and GPH1 above seem to set GPH to 1, + // and things don't seem to work if we don't set GPH back to 0 (which the API + // does here). + writeReg(SYSTEM__GROUPED_PARAMETER_HOLD, 0x00); + writeReg(SYSTEM__SEED_CONFIG, 1); // tuning parm default + + // from VL53L1_config_low_power_auto_mode + writeReg(SYSTEM__SEQUENCE_CONFIG, 0x8B); // VHV, PHASECAL, DSS1, RANGE + writeReg16Bit(DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT, 200 << 8); + writeReg(DSS_CONFIG__ROI_MODE_CONTROL, 2); // REQUESTED_EFFFECTIVE_SPADS + + // VL53L1_set_preset_mode() end + + // default to long range, 50 ms timing budget + // note that this is different than what the API defaults to + setDistanceMode(Long); + setMeasurementTimingBudget(50000); + + // VL53L1_StaticInit() end + + // the API triggers this change in VL53L1_init_and_start_range() once a + // measurement is started; assumes MM1 and MM2 are disabled + writeReg16Bit(ALGO__PART_TO_PART_RANGE_OFFSET_MM, + readReg16Bit(MM_CONFIG__OUTER_OFFSET_MM) * 4); + + return true; +} + +// Write an 8-bit register +void VL53L1X::writeReg(uint16_t reg, uint8_t value) +{ + Wire.beginTransmission(address); + Wire.write((reg >> 8) & 0xFF); // reg high byte + Wire.write( reg & 0xFF); // reg low byte + Wire.write(value); + last_status = Wire.endTransmission(); +} + +// Write a 16-bit register +void VL53L1X::writeReg16Bit(uint16_t reg, uint16_t value) +{ + Wire.beginTransmission(address); + Wire.write((reg >> 8) & 0xFF); // reg high byte + Wire.write( reg & 0xFF); // reg low byte + Wire.write((value >> 8) & 0xFF); // value high byte + Wire.write( value & 0xFF); // value low byte + last_status = Wire.endTransmission(); +} + +// Write a 32-bit register +void VL53L1X::writeReg32Bit(uint16_t reg, uint32_t value) +{ + Wire.beginTransmission(address); + Wire.write((reg >> 8) & 0xFF); // reg high byte + Wire.write( reg & 0xFF); // reg low byte + Wire.write((value >> 24) & 0xFF); // value highest byte + Wire.write((value >> 16) & 0xFF); + Wire.write((value >> 8) & 0xFF); + Wire.write( value & 0xFF); // value lowest byte + last_status = Wire.endTransmission(); +} + +// Read an 8-bit register +uint8_t VL53L1X::readReg(regAddr reg) +{ + uint8_t value; + + Wire.beginTransmission(address); + Wire.write((reg >> 8) & 0xFF); // reg high byte + Wire.write( reg & 0xFF); // reg low byte + last_status = Wire.endTransmission(); + + Wire.requestFrom(address, (uint8_t)1); + value = Wire.read(); + + return value; +} + +// Read a 16-bit register +uint16_t VL53L1X::readReg16Bit(uint16_t reg) +{ + uint16_t value; + + Wire.beginTransmission(address); + Wire.write((reg >> 8) & 0xFF); // reg high byte + Wire.write( reg & 0xFF); // reg low byte + last_status = Wire.endTransmission(); + + Wire.requestFrom(address, (uint8_t)2); + value = (uint16_t)Wire.read() << 8; // value high byte + value |= Wire.read(); // value low byte + + return value; +} + +// Read a 32-bit register +uint32_t VL53L1X::readReg32Bit(uint16_t reg) +{ + uint32_t value; + + Wire.beginTransmission(address); + Wire.write((reg >> 8) & 0xFF); // reg high byte + Wire.write( reg & 0xFF); // reg low byte + last_status = Wire.endTransmission(); + + Wire.requestFrom(address, (uint8_t)4); + value = (uint32_t)Wire.read() << 24; // value highest byte + value |= (uint32_t)Wire.read() << 16; + value |= (uint16_t)Wire.read() << 8; + value |= Wire.read(); // value lowest byte + + return value; +} + +// set distance mode to Short, Medium, or Long +// based on VL53L1_SetDistanceMode() +bool VL53L1X::setDistanceMode(DistanceMode mode) +{ + // save existing timing budget + uint32_t budget_us = getMeasurementTimingBudget(); + + switch (mode) + { + case Short: + // from VL53L1_preset_mode_standard_ranging_short_range() + + // timing config + writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x07); + writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x05); + writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0x38); + + // dynamic config + writeReg(SD_CONFIG__WOI_SD0, 0x07); + writeReg(SD_CONFIG__WOI_SD1, 0x05); + writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 6); // tuning parm default + writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 6); // tuning parm default + + break; + + case Medium: + // from VL53L1_preset_mode_standard_ranging() + + // timing config + writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x0B); + writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x09); + writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0x78); + + // dynamic config + writeReg(SD_CONFIG__WOI_SD0, 0x0B); + writeReg(SD_CONFIG__WOI_SD1, 0x09); + writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 10); // tuning parm default + writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 10); // tuning parm default + + break; + + case Long: // long + // from VL53L1_preset_mode_standard_ranging_long_range() + + // timing config + writeReg(RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F); + writeReg(RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D); + writeReg(RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8); + + // dynamic config + writeReg(SD_CONFIG__WOI_SD0, 0x0F); + writeReg(SD_CONFIG__WOI_SD1, 0x0D); + writeReg(SD_CONFIG__INITIAL_PHASE_SD0, 14); // tuning parm default + writeReg(SD_CONFIG__INITIAL_PHASE_SD1, 14); // tuning parm default + + break; + + default: + // unrecognized mode - do nothing + return false; + } + + // reapply timing budget + setMeasurementTimingBudget(budget_us); + + // save mode so it can be returned by getDistanceMode() + distance_mode = mode; + + return true; +} + +// Set the measurement timing budget in microseconds, which is the time allowed +// for one measurement. A longer timing budget allows for more accurate +// measurements. +// based on VL53L1_SetMeasurementTimingBudgetMicroSeconds() +bool VL53L1X::setMeasurementTimingBudget(uint32_t budget_us) +{ + // assumes PresetMode is LOWPOWER_AUTONOMOUS + + if (budget_us <= TimingGuard) { return false; } + + uint32_t range_config_timeout_us = budget_us -= TimingGuard; + if (range_config_timeout_us > 1100000) { return false; } // FDA_MAX_TIMING_BUDGET_US * 2 + + range_config_timeout_us /= 2; + + // VL53L1_calc_timeout_register_values() begin + + uint32_t macro_period_us; + + // "Update Macro Period for Range A VCSEL Period" + macro_period_us = calcMacroPeriod(readReg(RANGE_CONFIG__VCSEL_PERIOD_A)); + + // "Update Phase timeout - uses Timing A" + // Timeout of 1000 is tuning parm default (TIMED_PHASECAL_CONFIG_TIMEOUT_US_DEFAULT) + // via VL53L1_get_preset_mode_timing_cfg(). + uint32_t phasecal_timeout_mclks = timeoutMicrosecondsToMclks(1000, macro_period_us); + if (phasecal_timeout_mclks > 0xFF) { phasecal_timeout_mclks = 0xFF; } + writeReg(PHASECAL_CONFIG__TIMEOUT_MACROP, phasecal_timeout_mclks); + + // "Update MM Timing A timeout" + // Timeout of 1 is tuning parm default (LOWPOWERAUTO_MM_CONFIG_TIMEOUT_US_DEFAULT) + // via VL53L1_get_preset_mode_timing_cfg(). With the API, the register + // actually ends up with a slightly different value because it gets assigned, + // retrieved, recalculated with a different macro period, and reassigned, + // but it probably doesn't matter because it seems like the MM ("mode + // mitigation"?) sequence steps are disabled in low power auto mode anyway. + writeReg16Bit(MM_CONFIG__TIMEOUT_MACROP_A, encodeTimeout( + timeoutMicrosecondsToMclks(1, macro_period_us))); + + // "Update Range Timing A timeout" + writeReg16Bit(RANGE_CONFIG__TIMEOUT_MACROP_A, encodeTimeout( + timeoutMicrosecondsToMclks(range_config_timeout_us, macro_period_us))); + + // "Update Macro Period for Range B VCSEL Period" + macro_period_us = calcMacroPeriod(readReg(RANGE_CONFIG__VCSEL_PERIOD_B)); + + // "Update MM Timing B timeout" + // (See earlier comment about MM Timing A timeout.) + writeReg16Bit(MM_CONFIG__TIMEOUT_MACROP_B, encodeTimeout( + timeoutMicrosecondsToMclks(1, macro_period_us))); + + // "Update Range Timing B timeout" + writeReg16Bit(RANGE_CONFIG__TIMEOUT_MACROP_B, encodeTimeout( + timeoutMicrosecondsToMclks(range_config_timeout_us, macro_period_us))); + + // VL53L1_calc_timeout_register_values() end + + return true; +} + +// Get the measurement timing budget in microseconds +// based on VL53L1_SetMeasurementTimingBudgetMicroSeconds() +uint32_t VL53L1X::getMeasurementTimingBudget() +{ + // assumes PresetMode is LOWPOWER_AUTONOMOUS and these sequence steps are + // enabled: VHV, PHASECAL, DSS1, RANGE + + // VL53L1_get_timeouts_us() begin + + // "Update Macro Period for Range A VCSEL Period" + uint32_t macro_period_us = calcMacroPeriod(readReg(RANGE_CONFIG__VCSEL_PERIOD_A)); + + // "Get Range Timing A timeout" + + uint32_t range_config_timeout_us = timeoutMclksToMicroseconds(decodeTimeout( + readReg16Bit(RANGE_CONFIG__TIMEOUT_MACROP_A)), macro_period_us); + + // VL53L1_get_timeouts_us() end + + return 2 * range_config_timeout_us + TimingGuard; +} + +// Start continuous ranging measurements, with the given inter-measurement +// period in milliseconds determining how often the sensor takes a measurement. +void VL53L1X::startContinuous(uint32_t period_ms) +{ + // from VL53L1_set_inter_measurement_period_ms() + writeReg32Bit(SYSTEM__INTERMEASUREMENT_PERIOD, period_ms * osc_calibrate_val); + + writeReg(SYSTEM__INTERRUPT_CLEAR, 0x01); // sys_interrupt_clear_range + writeReg(SYSTEM__MODE_START, 0x40); // mode_range__timed +} + +// Stop continuous measurements +// based on VL53L1_stop_range() +void VL53L1X::stopContinuous() +{ + writeReg(SYSTEM__MODE_START, 0x80); // mode_range__abort + + // VL53L1_low_power_auto_data_stop_range() begin + + calibrated = false; + + // "restore vhv configs" + if (saved_vhv_init != 0) + { + writeReg(VHV_CONFIG__INIT, saved_vhv_init); + } + if (saved_vhv_timeout != 0) + { + writeReg(VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, saved_vhv_timeout); + } + + // "remove phasecal override" + writeReg(PHASECAL_CONFIG__OVERRIDE, 0x00); + + // VL53L1_low_power_auto_data_stop_range() end +} + +// Returns a range reading in millimeters when continuous mode is active +// (readRangeSingleMillimeters() also calls this function after starting a +// single-shot range measurement) +uint16_t VL53L1X::read(bool blocking) +{ + if (blocking) + { + startTimeout(); + while (!dataReady()) + { + if (checkTimeoutExpired()) + { + did_timeout = true; + ranging_data.range_status = None; + ranging_data.range_mm = 0; + ranging_data.peak_signal_count_rate_MCPS = 0; + ranging_data.ambient_count_rate_MCPS = 0; + return ranging_data.range_mm; + } + } + } + + readResults(); + + if (!calibrated) + { + setupManualCalibration(); + calibrated = true; + } + + updateDSS(); + + getRangingData(); + + writeReg(SYSTEM__INTERRUPT_CLEAR, 0x01); // sys_interrupt_clear_range + + return ranging_data.range_mm; +} + +// convert a RangeStatus to a readable string +// Note that on an AVR, these strings are stored in RAM (dynamic memory), which +// makes working with them easier but uses up 200+ bytes of RAM (many AVR-based +// Arduinos only have about 2000 bytes of RAM). You can avoid this memory usage +// if you do not call this function in your sketch. +const char * VL53L1X::rangeStatusToString(RangeStatus status) +{ + switch (status) + { + case RangeValid: + return "range valid"; + + case SigmaFail: + return "sigma fail"; + + case SignalFail: + return "signal fail"; + + case RangeValidMinRangeClipped: + return "range valid, min range clipped"; + + case OutOfBoundsFail: + return "out of bounds fail"; + + case HardwareFail: + return "hardware fail"; + + case RangeValidNoWrapCheckFail: + return "range valid, no wrap check fail"; + + case WrapTargetFail: + return "wrap target fail"; + + case XtalkSignalFail: + return "xtalk signal fail"; + + case SynchronizationInt: + return "synchronization int"; + + case MinRangeFail: + return "min range fail"; + + case None: + return "no update"; + + default: + return "unknown status"; + } +} + +// Did a timeout occur in one of the read functions since the last call to +// timeoutOccurred()? +bool VL53L1X::timeoutOccurred() +{ + bool tmp = did_timeout; + did_timeout = false; + return tmp; +} + +// Private Methods ///////////////////////////////////////////////////////////// + +// "Setup ranges after the first one in low power auto mode by turning off +// FW calibration steps and programming static values" +// based on VL53L1_low_power_auto_setup_manual_calibration() +void VL53L1X::setupManualCalibration() +{ + // "save original vhv configs" + saved_vhv_init = readReg(VHV_CONFIG__INIT); + saved_vhv_timeout = readReg(VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND); + + // "disable VHV init" + writeReg(VHV_CONFIG__INIT, saved_vhv_init & 0x7F); + + // "set loop bound to tuning param" + writeReg(VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, + (saved_vhv_timeout & 0x03) + (3 << 2)); // tuning parm default (LOWPOWERAUTO_VHV_LOOP_BOUND_DEFAULT) + + // "override phasecal" + writeReg(PHASECAL_CONFIG__OVERRIDE, 0x01); + writeReg(CAL_CONFIG__VCSEL_START, readReg(PHASECAL_RESULT__VCSEL_START)); +} + +// read measurement results into buffer +void VL53L1X::readResults() +{ + Wire.beginTransmission(address); + Wire.write((RESULT__RANGE_STATUS >> 8) & 0xFF); // reg high byte + Wire.write( RESULT__RANGE_STATUS & 0xFF); // reg low byte + last_status = Wire.endTransmission(); + + Wire.requestFrom(address, (uint8_t)17); + + results.range_status = Wire.read(); + + Wire.read(); // report_status: not used + + results.stream_count = Wire.read(); + + results.dss_actual_effective_spads_sd0 = (uint16_t)Wire.read() << 8; // high byte + results.dss_actual_effective_spads_sd0 |= Wire.read(); // low byte + + Wire.read(); // peak_signal_count_rate_mcps_sd0: not used + Wire.read(); + + results.ambient_count_rate_mcps_sd0 = (uint16_t)Wire.read() << 8; // high byte + results.ambient_count_rate_mcps_sd0 |= Wire.read(); // low byte + + Wire.read(); // sigma_sd0: not used + Wire.read(); + + Wire.read(); // phase_sd0: not used + Wire.read(); + + results.final_crosstalk_corrected_range_mm_sd0 = (uint16_t)Wire.read() << 8; // high byte + results.final_crosstalk_corrected_range_mm_sd0 |= Wire.read(); // low byte + + results.peak_signal_count_rate_crosstalk_corrected_mcps_sd0 = (uint16_t)Wire.read() << 8; // high byte + results.peak_signal_count_rate_crosstalk_corrected_mcps_sd0 |= Wire.read(); // low byte +} + +// perform Dynamic SPAD Selection calculation/update +// based on VL53L1_low_power_auto_update_DSS() +void VL53L1X::updateDSS() +{ + uint16_t spadCount = results.dss_actual_effective_spads_sd0; + + if (spadCount != 0) + { + // "Calc total rate per spad" + + uint32_t totalRatePerSpad = + (uint32_t)results.peak_signal_count_rate_crosstalk_corrected_mcps_sd0 + + results.ambient_count_rate_mcps_sd0; + + // "clip to 16 bits" + if (totalRatePerSpad > 0xFFFF) { totalRatePerSpad = 0xFFFF; } + + // "shift up to take advantage of 32 bits" + totalRatePerSpad <<= 16; + + totalRatePerSpad /= spadCount; + + if (totalRatePerSpad != 0) + { + // "get the target rate and shift up by 16" + uint32_t requiredSpads = ((uint32_t)TargetRate << 16) / totalRatePerSpad; + + // "clip to 16 bit" + if (requiredSpads > 0xFFFF) { requiredSpads = 0xFFFF; } + + // "override DSS config" + writeReg16Bit(DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT, requiredSpads); + // DSS_CONFIG__ROI_MODE_CONTROL should already be set to REQUESTED_EFFFECTIVE_SPADS + + return; + } + } + + // If we reached this point, it means something above would have resulted in a + // divide by zero. + // "We want to gracefully set a spad target, not just exit with an error" + + // "set target to mid point" + writeReg16Bit(DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT, 0x8000); +} + +// get range, status, rates from results buffer +// based on VL53L1_GetRangingMeasurementData() +void VL53L1X::getRangingData() +{ + // VL53L1_copy_sys_and_core_results_to_range_results() begin + + uint16_t range = results.final_crosstalk_corrected_range_mm_sd0; + + // "apply correction gain" + // gain factor of 2011 is tuning parm default (VL53L1_TUNINGPARM_LITE_RANGING_GAIN_FACTOR_DEFAULT) + // Basically, this appears to scale the result by 2011/2048, or about 98% + // (with the 1024 added for proper rounding). + ranging_data.range_mm = ((uint32_t)range * 2011 + 0x0400) / 0x0800; + + // VL53L1_copy_sys_and_core_results_to_range_results() end + + // set range_status in ranging_data based on value of RESULT__RANGE_STATUS register + // mostly based on ConvertStatusLite() + switch(results.range_status) + { + case 17: // MULTCLIPFAIL + case 2: // VCSELWATCHDOGTESTFAILURE + case 1: // VCSELCONTINUITYTESTFAILURE + case 3: // NOVHVVALUEFOUND + // from SetSimpleData() + ranging_data.range_status = HardwareFail; + break; + + case 13: // USERROICLIP + // from SetSimpleData() + ranging_data.range_status = MinRangeFail; + break; + + case 18: // GPHSTREAMCOUNT0READY + ranging_data.range_status = SynchronizationInt; + break; + + case 5: // RANGEPHASECHECK + ranging_data.range_status = OutOfBoundsFail; + break; + + case 4: // MSRCNOTARGET + ranging_data.range_status = SignalFail; + break; + + case 6: // SIGMATHRESHOLDCHECK + ranging_data.range_status = SigmaFail; + break; + + case 7: // PHASECONSISTENCY + ranging_data.range_status = WrapTargetFail; + break; + + case 12: // RANGEIGNORETHRESHOLD + ranging_data.range_status = XtalkSignalFail; + break; + + case 8: // MINCLIP + ranging_data.range_status = RangeValidMinRangeClipped; + break; + + case 9: // RANGECOMPLETE + // from VL53L1_copy_sys_and_core_results_to_range_results() + if (results.stream_count == 0) + { + ranging_data.range_status = RangeValidNoWrapCheckFail; + } + else + { + ranging_data.range_status = RangeValid; + } + break; + + default: + ranging_data.range_status = None; + } + + // from SetSimpleData() + ranging_data.peak_signal_count_rate_MCPS = + countRateFixedToFloat(results.peak_signal_count_rate_crosstalk_corrected_mcps_sd0); + ranging_data.ambient_count_rate_MCPS = + countRateFixedToFloat(results.ambient_count_rate_mcps_sd0); +} + +// Decode sequence step timeout in MCLKs from register value +// based on VL53L1_decode_timeout() +uint32_t VL53L1X::decodeTimeout(uint16_t reg_val) +{ + return ((uint32_t)(reg_val & 0xFF) << (reg_val >> 8)) + 1; +} + +// Encode sequence step timeout register value from timeout in MCLKs +// based on VL53L1_encode_timeout() +uint16_t VL53L1X::encodeTimeout(uint32_t timeout_mclks) +{ + // encoded format: "(LSByte * 2^MSByte) + 1" + + uint32_t ls_byte = 0; + uint16_t ms_byte = 0; + + if (timeout_mclks > 0) + { + ls_byte = timeout_mclks - 1; + + while ((ls_byte & 0xFFFFFF00) > 0) + { + ls_byte >>= 1; + ms_byte++; + } + + return (ms_byte << 8) | (ls_byte & 0xFF); + } + else { return 0; } +} + +// Convert sequence step timeout from macro periods to microseconds with given +// macro period in microseconds (12.12 format) +// based on VL53L1_calc_timeout_us() +uint32_t VL53L1X::timeoutMclksToMicroseconds(uint32_t timeout_mclks, uint32_t macro_period_us) +{ + return ((uint64_t)timeout_mclks * macro_period_us + 0x800) >> 12; +} + +// Convert sequence step timeout from microseconds to macro periods with given +// macro period in microseconds (12.12 format) +// based on VL53L1_calc_timeout_mclks() +uint32_t VL53L1X::timeoutMicrosecondsToMclks(uint32_t timeout_us, uint32_t macro_period_us) +{ + return (((uint32_t)timeout_us << 12) + (macro_period_us >> 1)) / macro_period_us; +} + +// Calculate macro period in microseconds (12.12 format) with given VCSEL period +// assumes fast_osc_frequency has been read and stored +// based on VL53L1_calc_macro_period_us() +uint32_t VL53L1X::calcMacroPeriod(uint8_t vcsel_period) +{ + // from VL53L1_calc_pll_period_us() + // fast osc frequency in 4.12 format; PLL period in 0.24 format + uint32_t pll_period_us = ((uint32_t)0x01 << 30) / fast_osc_frequency; + + // from VL53L1_decode_vcsel_period() + uint8_t vcsel_period_pclks = (vcsel_period + 1) << 1; + + // VL53L1_MACRO_PERIOD_VCSEL_PERIODS = 2304 + uint32_t macro_period_us = (uint32_t)2304 * pll_period_us; + macro_period_us >>= 6; + macro_period_us *= vcsel_period_pclks; + macro_period_us >>= 6; + + return macro_period_us; +} diff --git a/lib/vl53l1x-arduino-1.01/VL53L1X.h b/lib/vl53l1x-arduino-1.01/VL53L1X.h new file mode 100644 index 000000000..689e9b6fd --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/VL53L1X.h @@ -0,0 +1,1384 @@ +#pragma once + +#include + +class VL53L1X +{ + public: + + // register addresses from API vl53l1x_register_map.h + enum regAddr : uint16_t + { + SOFT_RESET = 0x0000, + I2C_SLAVE__DEVICE_ADDRESS = 0x0001, + ANA_CONFIG__VHV_REF_SEL_VDDPIX = 0x0002, + ANA_CONFIG__VHV_REF_SEL_VQUENCH = 0x0003, + ANA_CONFIG__REG_AVDD1V2_SEL = 0x0004, + ANA_CONFIG__FAST_OSC__TRIM = 0x0005, + OSC_MEASURED__FAST_OSC__FREQUENCY = 0x0006, + OSC_MEASURED__FAST_OSC__FREQUENCY_HI = 0x0006, + OSC_MEASURED__FAST_OSC__FREQUENCY_LO = 0x0007, + VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND = 0x0008, + VHV_CONFIG__COUNT_THRESH = 0x0009, + VHV_CONFIG__OFFSET = 0x000A, + VHV_CONFIG__INIT = 0x000B, + GLOBAL_CONFIG__SPAD_ENABLES_REF_0 = 0x000D, + GLOBAL_CONFIG__SPAD_ENABLES_REF_1 = 0x000E, + GLOBAL_CONFIG__SPAD_ENABLES_REF_2 = 0x000F, + GLOBAL_CONFIG__SPAD_ENABLES_REF_3 = 0x0010, + GLOBAL_CONFIG__SPAD_ENABLES_REF_4 = 0x0011, + GLOBAL_CONFIG__SPAD_ENABLES_REF_5 = 0x0012, + GLOBAL_CONFIG__REF_EN_START_SELECT = 0x0013, + REF_SPAD_MAN__NUM_REQUESTED_REF_SPADS = 0x0014, + REF_SPAD_MAN__REF_LOCATION = 0x0015, + ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS = 0x0016, + ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS_HI = 0x0016, + ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS_LO = 0x0017, + ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS = 0x0018, + ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS_HI = 0x0018, + ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS_LO = 0x0019, + ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS = 0x001A, + ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS_HI = 0x001A, + ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS_LO = 0x001B, + REF_SPAD_CHAR__TOTAL_RATE_TARGET_MCPS = 0x001C, + REF_SPAD_CHAR__TOTAL_RATE_TARGET_MCPS_HI = 0x001C, + REF_SPAD_CHAR__TOTAL_RATE_TARGET_MCPS_LO = 0x001D, + ALGO__PART_TO_PART_RANGE_OFFSET_MM = 0x001E, + ALGO__PART_TO_PART_RANGE_OFFSET_MM_HI = 0x001E, + ALGO__PART_TO_PART_RANGE_OFFSET_MM_LO = 0x001F, + MM_CONFIG__INNER_OFFSET_MM = 0x0020, + MM_CONFIG__INNER_OFFSET_MM_HI = 0x0020, + MM_CONFIG__INNER_OFFSET_MM_LO = 0x0021, + MM_CONFIG__OUTER_OFFSET_MM = 0x0022, + MM_CONFIG__OUTER_OFFSET_MM_HI = 0x0022, + MM_CONFIG__OUTER_OFFSET_MM_LO = 0x0023, + DSS_CONFIG__TARGET_TOTAL_RATE_MCPS = 0x0024, + DSS_CONFIG__TARGET_TOTAL_RATE_MCPS_HI = 0x0024, + DSS_CONFIG__TARGET_TOTAL_RATE_MCPS_LO = 0x0025, + DEBUG__CTRL = 0x0026, + TEST_MODE__CTRL = 0x0027, + CLK_GATING__CTRL = 0x0028, + NVM_BIST__CTRL = 0x0029, + NVM_BIST__NUM_NVM_WORDS = 0x002A, + NVM_BIST__START_ADDRESS = 0x002B, + HOST_IF__STATUS = 0x002C, + PAD_I2C_HV__CONFIG = 0x002D, + PAD_I2C_HV__EXTSUP_CONFIG = 0x002E, + GPIO_HV_PAD__CTRL = 0x002F, + GPIO_HV_MUX__CTRL = 0x0030, + GPIO__TIO_HV_STATUS = 0x0031, + GPIO__FIO_HV_STATUS = 0x0032, + ANA_CONFIG__SPAD_SEL_PSWIDTH = 0x0033, + ANA_CONFIG__VCSEL_PULSE_WIDTH_OFFSET = 0x0034, + ANA_CONFIG__FAST_OSC__CONFIG_CTRL = 0x0035, + SIGMA_ESTIMATOR__EFFECTIVE_PULSE_WIDTH_NS = 0x0036, + SIGMA_ESTIMATOR__EFFECTIVE_AMBIENT_WIDTH_NS = 0x0037, + SIGMA_ESTIMATOR__SIGMA_REF_MM = 0x0038, + ALGO__CROSSTALK_COMPENSATION_VALID_HEIGHT_MM = 0x0039, + SPARE_HOST_CONFIG__STATIC_CONFIG_SPARE_0 = 0x003A, + SPARE_HOST_CONFIG__STATIC_CONFIG_SPARE_1 = 0x003B, + ALGO__RANGE_IGNORE_THRESHOLD_MCPS = 0x003C, + ALGO__RANGE_IGNORE_THRESHOLD_MCPS_HI = 0x003C, + ALGO__RANGE_IGNORE_THRESHOLD_MCPS_LO = 0x003D, + ALGO__RANGE_IGNORE_VALID_HEIGHT_MM = 0x003E, + ALGO__RANGE_MIN_CLIP = 0x003F, + ALGO__CONSISTENCY_CHECK__TOLERANCE = 0x0040, + SPARE_HOST_CONFIG__STATIC_CONFIG_SPARE_2 = 0x0041, + SD_CONFIG__RESET_STAGES_MSB = 0x0042, + SD_CONFIG__RESET_STAGES_LSB = 0x0043, + GPH_CONFIG__STREAM_COUNT_UPDATE_VALUE = 0x0044, + GLOBAL_CONFIG__STREAM_DIVIDER = 0x0045, + SYSTEM__INTERRUPT_CONFIG_GPIO = 0x0046, + CAL_CONFIG__VCSEL_START = 0x0047, + CAL_CONFIG__REPEAT_RATE = 0x0048, + CAL_CONFIG__REPEAT_RATE_HI = 0x0048, + CAL_CONFIG__REPEAT_RATE_LO = 0x0049, + GLOBAL_CONFIG__VCSEL_WIDTH = 0x004A, + PHASECAL_CONFIG__TIMEOUT_MACROP = 0x004B, + PHASECAL_CONFIG__TARGET = 0x004C, + PHASECAL_CONFIG__OVERRIDE = 0x004D, + DSS_CONFIG__ROI_MODE_CONTROL = 0x004F, + SYSTEM__THRESH_RATE_HIGH = 0x0050, + SYSTEM__THRESH_RATE_HIGH_HI = 0x0050, + SYSTEM__THRESH_RATE_HIGH_LO = 0x0051, + SYSTEM__THRESH_RATE_LOW = 0x0052, + SYSTEM__THRESH_RATE_LOW_HI = 0x0052, + SYSTEM__THRESH_RATE_LOW_LO = 0x0053, + DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT = 0x0054, + DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT_HI = 0x0054, + DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT_LO = 0x0055, + DSS_CONFIG__MANUAL_BLOCK_SELECT = 0x0056, + DSS_CONFIG__APERTURE_ATTENUATION = 0x0057, + DSS_CONFIG__MAX_SPADS_LIMIT = 0x0058, + DSS_CONFIG__MIN_SPADS_LIMIT = 0x0059, + MM_CONFIG__TIMEOUT_MACROP_A = 0x005A, // added by Pololu for 16-bit accesses + MM_CONFIG__TIMEOUT_MACROP_A_HI = 0x005A, + MM_CONFIG__TIMEOUT_MACROP_A_LO = 0x005B, + MM_CONFIG__TIMEOUT_MACROP_B = 0x005C, // added by Pololu for 16-bit accesses + MM_CONFIG__TIMEOUT_MACROP_B_HI = 0x005C, + MM_CONFIG__TIMEOUT_MACROP_B_LO = 0x005D, + RANGE_CONFIG__TIMEOUT_MACROP_A = 0x005E, // added by Pololu for 16-bit accesses + RANGE_CONFIG__TIMEOUT_MACROP_A_HI = 0x005E, + RANGE_CONFIG__TIMEOUT_MACROP_A_LO = 0x005F, + RANGE_CONFIG__VCSEL_PERIOD_A = 0x0060, + RANGE_CONFIG__TIMEOUT_MACROP_B = 0x0061, // added by Pololu for 16-bit accesses + RANGE_CONFIG__TIMEOUT_MACROP_B_HI = 0x0061, + RANGE_CONFIG__TIMEOUT_MACROP_B_LO = 0x0062, + RANGE_CONFIG__VCSEL_PERIOD_B = 0x0063, + RANGE_CONFIG__SIGMA_THRESH = 0x0064, + RANGE_CONFIG__SIGMA_THRESH_HI = 0x0064, + RANGE_CONFIG__SIGMA_THRESH_LO = 0x0065, + RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS = 0x0066, + RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS_HI = 0x0066, + RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS_LO = 0x0067, + RANGE_CONFIG__VALID_PHASE_LOW = 0x0068, + RANGE_CONFIG__VALID_PHASE_HIGH = 0x0069, + SYSTEM__INTERMEASUREMENT_PERIOD = 0x006C, + SYSTEM__INTERMEASUREMENT_PERIOD_3 = 0x006C, + SYSTEM__INTERMEASUREMENT_PERIOD_2 = 0x006D, + SYSTEM__INTERMEASUREMENT_PERIOD_1 = 0x006E, + SYSTEM__INTERMEASUREMENT_PERIOD_0 = 0x006F, + SYSTEM__FRACTIONAL_ENABLE = 0x0070, + SYSTEM__GROUPED_PARAMETER_HOLD_0 = 0x0071, + SYSTEM__THRESH_HIGH = 0x0072, + SYSTEM__THRESH_HIGH_HI = 0x0072, + SYSTEM__THRESH_HIGH_LO = 0x0073, + SYSTEM__THRESH_LOW = 0x0074, + SYSTEM__THRESH_LOW_HI = 0x0074, + SYSTEM__THRESH_LOW_LO = 0x0075, + SYSTEM__ENABLE_XTALK_PER_QUADRANT = 0x0076, + SYSTEM__SEED_CONFIG = 0x0077, + SD_CONFIG__WOI_SD0 = 0x0078, + SD_CONFIG__WOI_SD1 = 0x0079, + SD_CONFIG__INITIAL_PHASE_SD0 = 0x007A, + SD_CONFIG__INITIAL_PHASE_SD1 = 0x007B, + SYSTEM__GROUPED_PARAMETER_HOLD_1 = 0x007C, + SD_CONFIG__FIRST_ORDER_SELECT = 0x007D, + SD_CONFIG__QUANTIFIER = 0x007E, + ROI_CONFIG__USER_ROI_CENTRE_SPAD = 0x007F, + ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE = 0x0080, + SYSTEM__SEQUENCE_CONFIG = 0x0081, + SYSTEM__GROUPED_PARAMETER_HOLD = 0x0082, + POWER_MANAGEMENT__GO1_POWER_FORCE = 0x0083, + SYSTEM__STREAM_COUNT_CTRL = 0x0084, + FIRMWARE__ENABLE = 0x0085, + SYSTEM__INTERRUPT_CLEAR = 0x0086, + SYSTEM__MODE_START = 0x0087, + RESULT__INTERRUPT_STATUS = 0x0088, + RESULT__RANGE_STATUS = 0x0089, + RESULT__REPORT_STATUS = 0x008A, + RESULT__STREAM_COUNT = 0x008B, + RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x008C, + RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x008C, + RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x008D, + RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0 = 0x008E, + RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0_HI = 0x008E, + RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0_LO = 0x008F, + RESULT__AMBIENT_COUNT_RATE_MCPS_SD0 = 0x0090, + RESULT__AMBIENT_COUNT_RATE_MCPS_SD0_HI = 0x0090, + RESULT__AMBIENT_COUNT_RATE_MCPS_SD0_LO = 0x0091, + RESULT__SIGMA_SD0 = 0x0092, + RESULT__SIGMA_SD0_HI = 0x0092, + RESULT__SIGMA_SD0_LO = 0x0093, + RESULT__PHASE_SD0 = 0x0094, + RESULT__PHASE_SD0_HI = 0x0094, + RESULT__PHASE_SD0_LO = 0x0095, + RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0 = 0x0096, + RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0_HI = 0x0096, + RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0_LO = 0x0097, + RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0 = 0x0098, + RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_HI = 0x0098, + RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_LO = 0x0099, + RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x009A, + RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x009A, + RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x009B, + RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x009C, + RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x009C, + RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x009D, + RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0 = 0x009E, + RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0_HI = 0x009E, + RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0_LO = 0x009F, + RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1 = 0x00A0, + RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1_HI = 0x00A0, + RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1_LO = 0x00A1, + RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1 = 0x00A2, + RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1_HI = 0x00A2, + RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1_LO = 0x00A3, + RESULT__AMBIENT_COUNT_RATE_MCPS_SD1 = 0x00A4, + RESULT__AMBIENT_COUNT_RATE_MCPS_SD1_HI = 0x00A4, + RESULT__AMBIENT_COUNT_RATE_MCPS_SD1_LO = 0x00A5, + RESULT__SIGMA_SD1 = 0x00A6, + RESULT__SIGMA_SD1_HI = 0x00A6, + RESULT__SIGMA_SD1_LO = 0x00A7, + RESULT__PHASE_SD1 = 0x00A8, + RESULT__PHASE_SD1_HI = 0x00A8, + RESULT__PHASE_SD1_LO = 0x00A9, + RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1 = 0x00AA, + RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1_HI = 0x00AA, + RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1_LO = 0x00AB, + RESULT__SPARE_0_SD1 = 0x00AC, + RESULT__SPARE_0_SD1_HI = 0x00AC, + RESULT__SPARE_0_SD1_LO = 0x00AD, + RESULT__SPARE_1_SD1 = 0x00AE, + RESULT__SPARE_1_SD1_HI = 0x00AE, + RESULT__SPARE_1_SD1_LO = 0x00AF, + RESULT__SPARE_2_SD1 = 0x00B0, + RESULT__SPARE_2_SD1_HI = 0x00B0, + RESULT__SPARE_2_SD1_LO = 0x00B1, + RESULT__SPARE_3_SD1 = 0x00B2, + RESULT__THRESH_INFO = 0x00B3, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0 = 0x00B4, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_3 = 0x00B4, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_2 = 0x00B5, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_1 = 0x00B6, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_0 = 0x00B7, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD0 = 0x00B8, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_3 = 0x00B8, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_2 = 0x00B9, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_1 = 0x00BA, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_0 = 0x00BB, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0 = 0x00BC, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_3 = 0x00BC, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_2 = 0x00BD, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_1 = 0x00BE, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_0 = 0x00BF, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0 = 0x00C0, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_3 = 0x00C0, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_2 = 0x00C1, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_1 = 0x00C2, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_0 = 0x00C3, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1 = 0x00C4, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_3 = 0x00C4, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_2 = 0x00C5, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_1 = 0x00C6, + RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_0 = 0x00C7, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD1 = 0x00C8, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_3 = 0x00C8, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_2 = 0x00C9, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_1 = 0x00CA, + RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_0 = 0x00CB, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1 = 0x00CC, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_3 = 0x00CC, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_2 = 0x00CD, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_1 = 0x00CE, + RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_0 = 0x00CF, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1 = 0x00D0, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_3 = 0x00D0, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_2 = 0x00D1, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_1 = 0x00D2, + RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_0 = 0x00D3, + RESULT_CORE__SPARE_0 = 0x00D4, + PHASECAL_RESULT__REFERENCE_PHASE = 0x00D6, + PHASECAL_RESULT__REFERENCE_PHASE_HI = 0x00D6, + PHASECAL_RESULT__REFERENCE_PHASE_LO = 0x00D7, + PHASECAL_RESULT__VCSEL_START = 0x00D8, + REF_SPAD_CHAR_RESULT__NUM_ACTUAL_REF_SPADS = 0x00D9, + REF_SPAD_CHAR_RESULT__REF_LOCATION = 0x00DA, + VHV_RESULT__COLDBOOT_STATUS = 0x00DB, + VHV_RESULT__SEARCH_RESULT = 0x00DC, + VHV_RESULT__LATEST_SETTING = 0x00DD, + RESULT__OSC_CALIBRATE_VAL = 0x00DE, + RESULT__OSC_CALIBRATE_VAL_HI = 0x00DE, + RESULT__OSC_CALIBRATE_VAL_LO = 0x00DF, + ANA_CONFIG__POWERDOWN_GO1 = 0x00E0, + ANA_CONFIG__REF_BG_CTRL = 0x00E1, + ANA_CONFIG__REGDVDD1V2_CTRL = 0x00E2, + ANA_CONFIG__OSC_SLOW_CTRL = 0x00E3, + TEST_MODE__STATUS = 0x00E4, + FIRMWARE__SYSTEM_STATUS = 0x00E5, + FIRMWARE__MODE_STATUS = 0x00E6, + FIRMWARE__SECONDARY_MODE_STATUS = 0x00E7, + FIRMWARE__CAL_REPEAT_RATE_COUNTER = 0x00E8, + FIRMWARE__CAL_REPEAT_RATE_COUNTER_HI = 0x00E8, + FIRMWARE__CAL_REPEAT_RATE_COUNTER_LO = 0x00E9, + FIRMWARE__HISTOGRAM_BIN = 0x00EA, + GPH__SYSTEM__THRESH_HIGH = 0x00EC, + GPH__SYSTEM__THRESH_HIGH_HI = 0x00EC, + GPH__SYSTEM__THRESH_HIGH_LO = 0x00ED, + GPH__SYSTEM__THRESH_LOW = 0x00EE, + GPH__SYSTEM__THRESH_LOW_HI = 0x00EE, + GPH__SYSTEM__THRESH_LOW_LO = 0x00EF, + GPH__SYSTEM__ENABLE_XTALK_PER_QUADRANT = 0x00F0, + GPH__SPARE_0 = 0x00F1, + GPH__SD_CONFIG__WOI_SD0 = 0x00F2, + GPH__SD_CONFIG__WOI_SD1 = 0x00F3, + GPH__SD_CONFIG__INITIAL_PHASE_SD0 = 0x00F4, + GPH__SD_CONFIG__INITIAL_PHASE_SD1 = 0x00F5, + GPH__SD_CONFIG__FIRST_ORDER_SELECT = 0x00F6, + GPH__SD_CONFIG__QUANTIFIER = 0x00F7, + GPH__ROI_CONFIG__USER_ROI_CENTRE_SPAD = 0x00F8, + GPH__ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE = 0x00F9, + GPH__SYSTEM__SEQUENCE_CONFIG = 0x00FA, + GPH__GPH_ID = 0x00FB, + SYSTEM__INTERRUPT_SET = 0x00FC, + INTERRUPT_MANAGER__ENABLES = 0x00FD, + INTERRUPT_MANAGER__CLEAR = 0x00FE, + INTERRUPT_MANAGER__STATUS = 0x00FF, + MCU_TO_HOST_BANK__WR_ACCESS_EN = 0x0100, + POWER_MANAGEMENT__GO1_RESET_STATUS = 0x0101, + PAD_STARTUP_MODE__VALUE_RO = 0x0102, + PAD_STARTUP_MODE__VALUE_CTRL = 0x0103, + PLL_PERIOD_US = 0x0104, + PLL_PERIOD_US_3 = 0x0104, + PLL_PERIOD_US_2 = 0x0105, + PLL_PERIOD_US_1 = 0x0106, + PLL_PERIOD_US_0 = 0x0107, + INTERRUPT_SCHEDULER__DATA_OUT = 0x0108, + INTERRUPT_SCHEDULER__DATA_OUT_3 = 0x0108, + INTERRUPT_SCHEDULER__DATA_OUT_2 = 0x0109, + INTERRUPT_SCHEDULER__DATA_OUT_1 = 0x010A, + INTERRUPT_SCHEDULER__DATA_OUT_0 = 0x010B, + NVM_BIST__COMPLETE = 0x010C, + NVM_BIST__STATUS = 0x010D, + IDENTIFICATION__MODEL_ID = 0x010F, + IDENTIFICATION__MODULE_TYPE = 0x0110, + IDENTIFICATION__REVISION_ID = 0x0111, + IDENTIFICATION__MODULE_ID = 0x0112, + IDENTIFICATION__MODULE_ID_HI = 0x0112, + IDENTIFICATION__MODULE_ID_LO = 0x0113, + ANA_CONFIG__FAST_OSC__TRIM_MAX = 0x0114, + ANA_CONFIG__FAST_OSC__FREQ_SET = 0x0115, + ANA_CONFIG__VCSEL_TRIM = 0x0116, + ANA_CONFIG__VCSEL_SELION = 0x0117, + ANA_CONFIG__VCSEL_SELION_MAX = 0x0118, + PROTECTED_LASER_SAFETY__LOCK_BIT = 0x0119, + LASER_SAFETY__KEY = 0x011A, + LASER_SAFETY__KEY_RO = 0x011B, + LASER_SAFETY__CLIP = 0x011C, + LASER_SAFETY__MULT = 0x011D, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_0 = 0x011E, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_1 = 0x011F, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_2 = 0x0120, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_3 = 0x0121, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_4 = 0x0122, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_5 = 0x0123, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_6 = 0x0124, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_7 = 0x0125, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_8 = 0x0126, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_9 = 0x0127, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_10 = 0x0128, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_11 = 0x0129, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_12 = 0x012A, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_13 = 0x012B, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_14 = 0x012C, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_15 = 0x012D, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_16 = 0x012E, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_17 = 0x012F, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_18 = 0x0130, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_19 = 0x0131, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_20 = 0x0132, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_21 = 0x0133, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_22 = 0x0134, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_23 = 0x0135, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_24 = 0x0136, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_25 = 0x0137, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_26 = 0x0138, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_27 = 0x0139, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_28 = 0x013A, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_29 = 0x013B, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_30 = 0x013C, + GLOBAL_CONFIG__SPAD_ENABLES_RTN_31 = 0x013D, + ROI_CONFIG__MODE_ROI_CENTRE_SPAD = 0x013E, + ROI_CONFIG__MODE_ROI_XY_SIZE = 0x013F, + GO2_HOST_BANK_ACCESS__OVERRIDE = 0x0300, + MCU_UTIL_MULTIPLIER__MULTIPLICAND = 0x0400, + MCU_UTIL_MULTIPLIER__MULTIPLICAND_3 = 0x0400, + MCU_UTIL_MULTIPLIER__MULTIPLICAND_2 = 0x0401, + MCU_UTIL_MULTIPLIER__MULTIPLICAND_1 = 0x0402, + MCU_UTIL_MULTIPLIER__MULTIPLICAND_0 = 0x0403, + MCU_UTIL_MULTIPLIER__MULTIPLIER = 0x0404, + MCU_UTIL_MULTIPLIER__MULTIPLIER_3 = 0x0404, + MCU_UTIL_MULTIPLIER__MULTIPLIER_2 = 0x0405, + MCU_UTIL_MULTIPLIER__MULTIPLIER_1 = 0x0406, + MCU_UTIL_MULTIPLIER__MULTIPLIER_0 = 0x0407, + MCU_UTIL_MULTIPLIER__PRODUCT_HI = 0x0408, + MCU_UTIL_MULTIPLIER__PRODUCT_HI_3 = 0x0408, + MCU_UTIL_MULTIPLIER__PRODUCT_HI_2 = 0x0409, + MCU_UTIL_MULTIPLIER__PRODUCT_HI_1 = 0x040A, + MCU_UTIL_MULTIPLIER__PRODUCT_HI_0 = 0x040B, + MCU_UTIL_MULTIPLIER__PRODUCT_LO = 0x040C, + MCU_UTIL_MULTIPLIER__PRODUCT_LO_3 = 0x040C, + MCU_UTIL_MULTIPLIER__PRODUCT_LO_2 = 0x040D, + MCU_UTIL_MULTIPLIER__PRODUCT_LO_1 = 0x040E, + MCU_UTIL_MULTIPLIER__PRODUCT_LO_0 = 0x040F, + MCU_UTIL_MULTIPLIER__START = 0x0410, + MCU_UTIL_MULTIPLIER__STATUS = 0x0411, + MCU_UTIL_DIVIDER__START = 0x0412, + MCU_UTIL_DIVIDER__STATUS = 0x0413, + MCU_UTIL_DIVIDER__DIVIDEND = 0x0414, + MCU_UTIL_DIVIDER__DIVIDEND_3 = 0x0414, + MCU_UTIL_DIVIDER__DIVIDEND_2 = 0x0415, + MCU_UTIL_DIVIDER__DIVIDEND_1 = 0x0416, + MCU_UTIL_DIVIDER__DIVIDEND_0 = 0x0417, + MCU_UTIL_DIVIDER__DIVISOR = 0x0418, + MCU_UTIL_DIVIDER__DIVISOR_3 = 0x0418, + MCU_UTIL_DIVIDER__DIVISOR_2 = 0x0419, + MCU_UTIL_DIVIDER__DIVISOR_1 = 0x041A, + MCU_UTIL_DIVIDER__DIVISOR_0 = 0x041B, + MCU_UTIL_DIVIDER__QUOTIENT = 0x041C, + MCU_UTIL_DIVIDER__QUOTIENT_3 = 0x041C, + MCU_UTIL_DIVIDER__QUOTIENT_2 = 0x041D, + MCU_UTIL_DIVIDER__QUOTIENT_1 = 0x041E, + MCU_UTIL_DIVIDER__QUOTIENT_0 = 0x041F, + TIMER0__VALUE_IN = 0x0420, + TIMER0__VALUE_IN_3 = 0x0420, + TIMER0__VALUE_IN_2 = 0x0421, + TIMER0__VALUE_IN_1 = 0x0422, + TIMER0__VALUE_IN_0 = 0x0423, + TIMER1__VALUE_IN = 0x0424, + TIMER1__VALUE_IN_3 = 0x0424, + TIMER1__VALUE_IN_2 = 0x0425, + TIMER1__VALUE_IN_1 = 0x0426, + TIMER1__VALUE_IN_0 = 0x0427, + TIMER0__CTRL = 0x0428, + TIMER1__CTRL = 0x0429, + MCU_GENERAL_PURPOSE__GP_0 = 0x042C, + MCU_GENERAL_PURPOSE__GP_1 = 0x042D, + MCU_GENERAL_PURPOSE__GP_2 = 0x042E, + MCU_GENERAL_PURPOSE__GP_3 = 0x042F, + MCU_RANGE_CALC__CONFIG = 0x0430, + MCU_RANGE_CALC__OFFSET_CORRECTED_RANGE = 0x0432, + MCU_RANGE_CALC__OFFSET_CORRECTED_RANGE_HI = 0x0432, + MCU_RANGE_CALC__OFFSET_CORRECTED_RANGE_LO = 0x0433, + MCU_RANGE_CALC__SPARE_4 = 0x0434, + MCU_RANGE_CALC__SPARE_4_3 = 0x0434, + MCU_RANGE_CALC__SPARE_4_2 = 0x0435, + MCU_RANGE_CALC__SPARE_4_1 = 0x0436, + MCU_RANGE_CALC__SPARE_4_0 = 0x0437, + MCU_RANGE_CALC__AMBIENT_DURATION_PRE_CALC = 0x0438, + MCU_RANGE_CALC__AMBIENT_DURATION_PRE_CALC_HI = 0x0438, + MCU_RANGE_CALC__AMBIENT_DURATION_PRE_CALC_LO = 0x0439, + MCU_RANGE_CALC__ALGO_VCSEL_PERIOD = 0x043C, + MCU_RANGE_CALC__SPARE_5 = 0x043D, + MCU_RANGE_CALC__ALGO_TOTAL_PERIODS = 0x043E, + MCU_RANGE_CALC__ALGO_TOTAL_PERIODS_HI = 0x043E, + MCU_RANGE_CALC__ALGO_TOTAL_PERIODS_LO = 0x043F, + MCU_RANGE_CALC__ALGO_ACCUM_PHASE = 0x0440, + MCU_RANGE_CALC__ALGO_ACCUM_PHASE_3 = 0x0440, + MCU_RANGE_CALC__ALGO_ACCUM_PHASE_2 = 0x0441, + MCU_RANGE_CALC__ALGO_ACCUM_PHASE_1 = 0x0442, + MCU_RANGE_CALC__ALGO_ACCUM_PHASE_0 = 0x0443, + MCU_RANGE_CALC__ALGO_SIGNAL_EVENTS = 0x0444, + MCU_RANGE_CALC__ALGO_SIGNAL_EVENTS_3 = 0x0444, + MCU_RANGE_CALC__ALGO_SIGNAL_EVENTS_2 = 0x0445, + MCU_RANGE_CALC__ALGO_SIGNAL_EVENTS_1 = 0x0446, + MCU_RANGE_CALC__ALGO_SIGNAL_EVENTS_0 = 0x0447, + MCU_RANGE_CALC__ALGO_AMBIENT_EVENTS = 0x0448, + MCU_RANGE_CALC__ALGO_AMBIENT_EVENTS_3 = 0x0448, + MCU_RANGE_CALC__ALGO_AMBIENT_EVENTS_2 = 0x0449, + MCU_RANGE_CALC__ALGO_AMBIENT_EVENTS_1 = 0x044A, + MCU_RANGE_CALC__ALGO_AMBIENT_EVENTS_0 = 0x044B, + MCU_RANGE_CALC__SPARE_6 = 0x044C, + MCU_RANGE_CALC__SPARE_6_HI = 0x044C, + MCU_RANGE_CALC__SPARE_6_LO = 0x044D, + MCU_RANGE_CALC__ALGO_ADJUST_VCSEL_PERIOD = 0x044E, + MCU_RANGE_CALC__ALGO_ADJUST_VCSEL_PERIOD_HI = 0x044E, + MCU_RANGE_CALC__ALGO_ADJUST_VCSEL_PERIOD_LO = 0x044F, + MCU_RANGE_CALC__NUM_SPADS = 0x0450, + MCU_RANGE_CALC__NUM_SPADS_HI = 0x0450, + MCU_RANGE_CALC__NUM_SPADS_LO = 0x0451, + MCU_RANGE_CALC__PHASE_OUTPUT = 0x0452, + MCU_RANGE_CALC__PHASE_OUTPUT_HI = 0x0452, + MCU_RANGE_CALC__PHASE_OUTPUT_LO = 0x0453, + MCU_RANGE_CALC__RATE_PER_SPAD_MCPS = 0x0454, + MCU_RANGE_CALC__RATE_PER_SPAD_MCPS_3 = 0x0454, + MCU_RANGE_CALC__RATE_PER_SPAD_MCPS_2 = 0x0455, + MCU_RANGE_CALC__RATE_PER_SPAD_MCPS_1 = 0x0456, + MCU_RANGE_CALC__RATE_PER_SPAD_MCPS_0 = 0x0457, + MCU_RANGE_CALC__SPARE_7 = 0x0458, + MCU_RANGE_CALC__SPARE_8 = 0x0459, + MCU_RANGE_CALC__PEAK_SIGNAL_RATE_MCPS = 0x045A, + MCU_RANGE_CALC__PEAK_SIGNAL_RATE_MCPS_HI = 0x045A, + MCU_RANGE_CALC__PEAK_SIGNAL_RATE_MCPS_LO = 0x045B, + MCU_RANGE_CALC__AVG_SIGNAL_RATE_MCPS = 0x045C, + MCU_RANGE_CALC__AVG_SIGNAL_RATE_MCPS_HI = 0x045C, + MCU_RANGE_CALC__AVG_SIGNAL_RATE_MCPS_LO = 0x045D, + MCU_RANGE_CALC__AMBIENT_RATE_MCPS = 0x045E, + MCU_RANGE_CALC__AMBIENT_RATE_MCPS_HI = 0x045E, + MCU_RANGE_CALC__AMBIENT_RATE_MCPS_LO = 0x045F, + MCU_RANGE_CALC__XTALK = 0x0460, + MCU_RANGE_CALC__XTALK_HI = 0x0460, + MCU_RANGE_CALC__XTALK_LO = 0x0461, + MCU_RANGE_CALC__CALC_STATUS = 0x0462, + MCU_RANGE_CALC__DEBUG = 0x0463, + MCU_RANGE_CALC__PEAK_SIGNAL_RATE_XTALK_CORR_MCPS = 0x0464, + MCU_RANGE_CALC__PEAK_SIGNAL_RATE_XTALK_CORR_MCPS_HI = 0x0464, + MCU_RANGE_CALC__PEAK_SIGNAL_RATE_XTALK_CORR_MCPS_LO = 0x0465, + MCU_RANGE_CALC__SPARE_0 = 0x0468, + MCU_RANGE_CALC__SPARE_1 = 0x0469, + MCU_RANGE_CALC__SPARE_2 = 0x046A, + MCU_RANGE_CALC__SPARE_3 = 0x046B, + PATCH__CTRL = 0x0470, + PATCH__JMP_ENABLES = 0x0472, + PATCH__JMP_ENABLES_HI = 0x0472, + PATCH__JMP_ENABLES_LO = 0x0473, + PATCH__DATA_ENABLES = 0x0474, + PATCH__DATA_ENABLES_HI = 0x0474, + PATCH__DATA_ENABLES_LO = 0x0475, + PATCH__OFFSET_0 = 0x0476, + PATCH__OFFSET_0_HI = 0x0476, + PATCH__OFFSET_0_LO = 0x0477, + PATCH__OFFSET_1 = 0x0478, + PATCH__OFFSET_1_HI = 0x0478, + PATCH__OFFSET_1_LO = 0x0479, + PATCH__OFFSET_2 = 0x047A, + PATCH__OFFSET_2_HI = 0x047A, + PATCH__OFFSET_2_LO = 0x047B, + PATCH__OFFSET_3 = 0x047C, + PATCH__OFFSET_3_HI = 0x047C, + PATCH__OFFSET_3_LO = 0x047D, + PATCH__OFFSET_4 = 0x047E, + PATCH__OFFSET_4_HI = 0x047E, + PATCH__OFFSET_4_LO = 0x047F, + PATCH__OFFSET_5 = 0x0480, + PATCH__OFFSET_5_HI = 0x0480, + PATCH__OFFSET_5_LO = 0x0481, + PATCH__OFFSET_6 = 0x0482, + PATCH__OFFSET_6_HI = 0x0482, + PATCH__OFFSET_6_LO = 0x0483, + PATCH__OFFSET_7 = 0x0484, + PATCH__OFFSET_7_HI = 0x0484, + PATCH__OFFSET_7_LO = 0x0485, + PATCH__OFFSET_8 = 0x0486, + PATCH__OFFSET_8_HI = 0x0486, + PATCH__OFFSET_8_LO = 0x0487, + PATCH__OFFSET_9 = 0x0488, + PATCH__OFFSET_9_HI = 0x0488, + PATCH__OFFSET_9_LO = 0x0489, + PATCH__OFFSET_10 = 0x048A, + PATCH__OFFSET_10_HI = 0x048A, + PATCH__OFFSET_10_LO = 0x048B, + PATCH__OFFSET_11 = 0x048C, + PATCH__OFFSET_11_HI = 0x048C, + PATCH__OFFSET_11_LO = 0x048D, + PATCH__OFFSET_12 = 0x048E, + PATCH__OFFSET_12_HI = 0x048E, + PATCH__OFFSET_12_LO = 0x048F, + PATCH__OFFSET_13 = 0x0490, + PATCH__OFFSET_13_HI = 0x0490, + PATCH__OFFSET_13_LO = 0x0491, + PATCH__OFFSET_14 = 0x0492, + PATCH__OFFSET_14_HI = 0x0492, + PATCH__OFFSET_14_LO = 0x0493, + PATCH__OFFSET_15 = 0x0494, + PATCH__OFFSET_15_HI = 0x0494, + PATCH__OFFSET_15_LO = 0x0495, + PATCH__ADDRESS_0 = 0x0496, + PATCH__ADDRESS_0_HI = 0x0496, + PATCH__ADDRESS_0_LO = 0x0497, + PATCH__ADDRESS_1 = 0x0498, + PATCH__ADDRESS_1_HI = 0x0498, + PATCH__ADDRESS_1_LO = 0x0499, + PATCH__ADDRESS_2 = 0x049A, + PATCH__ADDRESS_2_HI = 0x049A, + PATCH__ADDRESS_2_LO = 0x049B, + PATCH__ADDRESS_3 = 0x049C, + PATCH__ADDRESS_3_HI = 0x049C, + PATCH__ADDRESS_3_LO = 0x049D, + PATCH__ADDRESS_4 = 0x049E, + PATCH__ADDRESS_4_HI = 0x049E, + PATCH__ADDRESS_4_LO = 0x049F, + PATCH__ADDRESS_5 = 0x04A0, + PATCH__ADDRESS_5_HI = 0x04A0, + PATCH__ADDRESS_5_LO = 0x04A1, + PATCH__ADDRESS_6 = 0x04A2, + PATCH__ADDRESS_6_HI = 0x04A2, + PATCH__ADDRESS_6_LO = 0x04A3, + PATCH__ADDRESS_7 = 0x04A4, + PATCH__ADDRESS_7_HI = 0x04A4, + PATCH__ADDRESS_7_LO = 0x04A5, + PATCH__ADDRESS_8 = 0x04A6, + PATCH__ADDRESS_8_HI = 0x04A6, + PATCH__ADDRESS_8_LO = 0x04A7, + PATCH__ADDRESS_9 = 0x04A8, + PATCH__ADDRESS_9_HI = 0x04A8, + PATCH__ADDRESS_9_LO = 0x04A9, + PATCH__ADDRESS_10 = 0x04AA, + PATCH__ADDRESS_10_HI = 0x04AA, + PATCH__ADDRESS_10_LO = 0x04AB, + PATCH__ADDRESS_11 = 0x04AC, + PATCH__ADDRESS_11_HI = 0x04AC, + PATCH__ADDRESS_11_LO = 0x04AD, + PATCH__ADDRESS_12 = 0x04AE, + PATCH__ADDRESS_12_HI = 0x04AE, + PATCH__ADDRESS_12_LO = 0x04AF, + PATCH__ADDRESS_13 = 0x04B0, + PATCH__ADDRESS_13_HI = 0x04B0, + PATCH__ADDRESS_13_LO = 0x04B1, + PATCH__ADDRESS_14 = 0x04B2, + PATCH__ADDRESS_14_HI = 0x04B2, + PATCH__ADDRESS_14_LO = 0x04B3, + PATCH__ADDRESS_15 = 0x04B4, + PATCH__ADDRESS_15_HI = 0x04B4, + PATCH__ADDRESS_15_LO = 0x04B5, + SPI_ASYNC_MUX__CTRL = 0x04C0, + CLK__CONFIG = 0x04C4, + GPIO_LV_MUX__CTRL = 0x04CC, + GPIO_LV_PAD__CTRL = 0x04CD, + PAD_I2C_LV__CONFIG = 0x04D0, + PAD_STARTUP_MODE__VALUE_RO_GO1 = 0x04D4, + HOST_IF__STATUS_GO1 = 0x04D5, + MCU_CLK_GATING__CTRL = 0x04D8, + TEST__BIST_ROM_CTRL = 0x04E0, + TEST__BIST_ROM_RESULT = 0x04E1, + TEST__BIST_ROM_MCU_SIG = 0x04E2, + TEST__BIST_ROM_MCU_SIG_HI = 0x04E2, + TEST__BIST_ROM_MCU_SIG_LO = 0x04E3, + TEST__BIST_RAM_CTRL = 0x04E4, + TEST__BIST_RAM_RESULT = 0x04E5, + TEST__TMC = 0x04E8, + TEST__PLL_BIST_MIN_THRESHOLD = 0x04F0, + TEST__PLL_BIST_MIN_THRESHOLD_HI = 0x04F0, + TEST__PLL_BIST_MIN_THRESHOLD_LO = 0x04F1, + TEST__PLL_BIST_MAX_THRESHOLD = 0x04F2, + TEST__PLL_BIST_MAX_THRESHOLD_HI = 0x04F2, + TEST__PLL_BIST_MAX_THRESHOLD_LO = 0x04F3, + TEST__PLL_BIST_COUNT_OUT = 0x04F4, + TEST__PLL_BIST_COUNT_OUT_HI = 0x04F4, + TEST__PLL_BIST_COUNT_OUT_LO = 0x04F5, + TEST__PLL_BIST_GONOGO = 0x04F6, + TEST__PLL_BIST_CTRL = 0x04F7, + RANGING_CORE__DEVICE_ID = 0x0680, + RANGING_CORE__REVISION_ID = 0x0681, + RANGING_CORE__CLK_CTRL1 = 0x0683, + RANGING_CORE__CLK_CTRL2 = 0x0684, + RANGING_CORE__WOI_1 = 0x0685, + RANGING_CORE__WOI_REF_1 = 0x0686, + RANGING_CORE__START_RANGING = 0x0687, + RANGING_CORE__LOW_LIMIT_1 = 0x0690, + RANGING_CORE__HIGH_LIMIT_1 = 0x0691, + RANGING_CORE__LOW_LIMIT_REF_1 = 0x0692, + RANGING_CORE__HIGH_LIMIT_REF_1 = 0x0693, + RANGING_CORE__QUANTIFIER_1_MSB = 0x0694, + RANGING_CORE__QUANTIFIER_1_LSB = 0x0695, + RANGING_CORE__QUANTIFIER_REF_1_MSB = 0x0696, + RANGING_CORE__QUANTIFIER_REF_1_LSB = 0x0697, + RANGING_CORE__AMBIENT_OFFSET_1_MSB = 0x0698, + RANGING_CORE__AMBIENT_OFFSET_1_LSB = 0x0699, + RANGING_CORE__AMBIENT_OFFSET_REF_1_MSB = 0x069A, + RANGING_CORE__AMBIENT_OFFSET_REF_1_LSB = 0x069B, + RANGING_CORE__FILTER_STRENGTH_1 = 0x069C, + RANGING_CORE__FILTER_STRENGTH_REF_1 = 0x069D, + RANGING_CORE__SIGNAL_EVENT_LIMIT_1_MSB = 0x069E, + RANGING_CORE__SIGNAL_EVENT_LIMIT_1_LSB = 0x069F, + RANGING_CORE__SIGNAL_EVENT_LIMIT_REF_1_MSB = 0x06A0, + RANGING_CORE__SIGNAL_EVENT_LIMIT_REF_1_LSB = 0x06A1, + RANGING_CORE__TIMEOUT_OVERALL_PERIODS_MSB = 0x06A4, + RANGING_CORE__TIMEOUT_OVERALL_PERIODS_LSB = 0x06A5, + RANGING_CORE__INVERT_HW = 0x06A6, + RANGING_CORE__FORCE_HW = 0x06A7, + RANGING_CORE__STATIC_HW_VALUE = 0x06A8, + RANGING_CORE__FORCE_CONTINUOUS_AMBIENT = 0x06A9, + RANGING_CORE__TEST_PHASE_SELECT_TO_FILTER = 0x06AA, + RANGING_CORE__TEST_PHASE_SELECT_TO_TIMING_GEN = 0x06AB, + RANGING_CORE__INITIAL_PHASE_VALUE_1 = 0x06AC, + RANGING_CORE__INITIAL_PHASE_VALUE_REF_1 = 0x06AD, + RANGING_CORE__FORCE_UP_IN = 0x06AE, + RANGING_CORE__FORCE_DN_IN = 0x06AF, + RANGING_CORE__STATIC_UP_VALUE_1 = 0x06B0, + RANGING_CORE__STATIC_UP_VALUE_REF_1 = 0x06B1, + RANGING_CORE__STATIC_DN_VALUE_1 = 0x06B2, + RANGING_CORE__STATIC_DN_VALUE_REF_1 = 0x06B3, + RANGING_CORE__MONITOR_UP_DN = 0x06B4, + RANGING_CORE__INVERT_UP_DN = 0x06B5, + RANGING_CORE__CPUMP_1 = 0x06B6, + RANGING_CORE__CPUMP_2 = 0x06B7, + RANGING_CORE__CPUMP_3 = 0x06B8, + RANGING_CORE__OSC_1 = 0x06B9, + RANGING_CORE__PLL_1 = 0x06BB, + RANGING_CORE__PLL_2 = 0x06BC, + RANGING_CORE__REFERENCE_1 = 0x06BD, + RANGING_CORE__REFERENCE_3 = 0x06BF, + RANGING_CORE__REFERENCE_4 = 0x06C0, + RANGING_CORE__REFERENCE_5 = 0x06C1, + RANGING_CORE__REGAVDD1V2 = 0x06C3, + RANGING_CORE__CALIB_1 = 0x06C4, + RANGING_CORE__CALIB_2 = 0x06C5, + RANGING_CORE__CALIB_3 = 0x06C6, + RANGING_CORE__TST_MUX_SEL1 = 0x06C9, + RANGING_CORE__TST_MUX_SEL2 = 0x06CA, + RANGING_CORE__TST_MUX = 0x06CB, + RANGING_CORE__GPIO_OUT_TESTMUX = 0x06CC, + RANGING_CORE__CUSTOM_FE = 0x06CD, + RANGING_CORE__CUSTOM_FE_2 = 0x06CE, + RANGING_CORE__SPAD_READOUT = 0x06CF, + RANGING_CORE__SPAD_READOUT_1 = 0x06D0, + RANGING_CORE__SPAD_READOUT_2 = 0x06D1, + RANGING_CORE__SPAD_PS = 0x06D2, + RANGING_CORE__LASER_SAFETY_2 = 0x06D4, + RANGING_CORE__NVM_CTRL__MODE = 0x0780, + RANGING_CORE__NVM_CTRL__PDN = 0x0781, + RANGING_CORE__NVM_CTRL__PROGN = 0x0782, + RANGING_CORE__NVM_CTRL__READN = 0x0783, + RANGING_CORE__NVM_CTRL__PULSE_WIDTH_MSB = 0x0784, + RANGING_CORE__NVM_CTRL__PULSE_WIDTH_LSB = 0x0785, + RANGING_CORE__NVM_CTRL__HV_RISE_MSB = 0x0786, + RANGING_CORE__NVM_CTRL__HV_RISE_LSB = 0x0787, + RANGING_CORE__NVM_CTRL__HV_FALL_MSB = 0x0788, + RANGING_CORE__NVM_CTRL__HV_FALL_LSB = 0x0789, + RANGING_CORE__NVM_CTRL__TST = 0x078A, + RANGING_CORE__NVM_CTRL__TESTREAD = 0x078B, + RANGING_CORE__NVM_CTRL__DATAIN_MMM = 0x078C, + RANGING_CORE__NVM_CTRL__DATAIN_LMM = 0x078D, + RANGING_CORE__NVM_CTRL__DATAIN_LLM = 0x078E, + RANGING_CORE__NVM_CTRL__DATAIN_LLL = 0x078F, + RANGING_CORE__NVM_CTRL__DATAOUT_MMM = 0x0790, + RANGING_CORE__NVM_CTRL__DATAOUT_LMM = 0x0791, + RANGING_CORE__NVM_CTRL__DATAOUT_LLM = 0x0792, + RANGING_CORE__NVM_CTRL__DATAOUT_LLL = 0x0793, + RANGING_CORE__NVM_CTRL__ADDR = 0x0794, + RANGING_CORE__NVM_CTRL__DATAOUT_ECC = 0x0795, + RANGING_CORE__RET_SPAD_EN_0 = 0x0796, + RANGING_CORE__RET_SPAD_EN_1 = 0x0797, + RANGING_CORE__RET_SPAD_EN_2 = 0x0798, + RANGING_CORE__RET_SPAD_EN_3 = 0x0799, + RANGING_CORE__RET_SPAD_EN_4 = 0x079A, + RANGING_CORE__RET_SPAD_EN_5 = 0x079B, + RANGING_CORE__RET_SPAD_EN_6 = 0x079C, + RANGING_CORE__RET_SPAD_EN_7 = 0x079D, + RANGING_CORE__RET_SPAD_EN_8 = 0x079E, + RANGING_CORE__RET_SPAD_EN_9 = 0x079F, + RANGING_CORE__RET_SPAD_EN_10 = 0x07A0, + RANGING_CORE__RET_SPAD_EN_11 = 0x07A1, + RANGING_CORE__RET_SPAD_EN_12 = 0x07A2, + RANGING_CORE__RET_SPAD_EN_13 = 0x07A3, + RANGING_CORE__RET_SPAD_EN_14 = 0x07A4, + RANGING_CORE__RET_SPAD_EN_15 = 0x07A5, + RANGING_CORE__RET_SPAD_EN_16 = 0x07A6, + RANGING_CORE__RET_SPAD_EN_17 = 0x07A7, + RANGING_CORE__SPAD_SHIFT_EN = 0x07BA, + RANGING_CORE__SPAD_DISABLE_CTRL = 0x07BB, + RANGING_CORE__SPAD_EN_SHIFT_OUT_DEBUG = 0x07BC, + RANGING_CORE__SPI_MODE = 0x07BD, + RANGING_CORE__GPIO_DIR = 0x07BE, + RANGING_CORE__VCSEL_PERIOD = 0x0880, + RANGING_CORE__VCSEL_START = 0x0881, + RANGING_CORE__VCSEL_STOP = 0x0882, + RANGING_CORE__VCSEL_1 = 0x0885, + RANGING_CORE__VCSEL_STATUS = 0x088D, + RANGING_CORE__STATUS = 0x0980, + RANGING_CORE__LASER_CONTINUITY_STATE = 0x0981, + RANGING_CORE__RANGE_1_MMM = 0x0982, + RANGING_CORE__RANGE_1_LMM = 0x0983, + RANGING_CORE__RANGE_1_LLM = 0x0984, + RANGING_CORE__RANGE_1_LLL = 0x0985, + RANGING_CORE__RANGE_REF_1_MMM = 0x0986, + RANGING_CORE__RANGE_REF_1_LMM = 0x0987, + RANGING_CORE__RANGE_REF_1_LLM = 0x0988, + RANGING_CORE__RANGE_REF_1_LLL = 0x0989, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_1_MMM = 0x098A, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_1_LMM = 0x098B, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_1_LLM = 0x098C, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_1_LLL = 0x098D, + RANGING_CORE__RANGING_TOTAL_EVENTS_1_MMM = 0x098E, + RANGING_CORE__RANGING_TOTAL_EVENTS_1_LMM = 0x098F, + RANGING_CORE__RANGING_TOTAL_EVENTS_1_LLM = 0x0990, + RANGING_CORE__RANGING_TOTAL_EVENTS_1_LLL = 0x0991, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_1_MMM = 0x0992, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_1_LMM = 0x0993, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_1_LLM = 0x0994, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_1_LLL = 0x0995, + RANGING_CORE__TOTAL_PERIODS_ELAPSED_1_MM = 0x0996, + RANGING_CORE__TOTAL_PERIODS_ELAPSED_1_LM = 0x0997, + RANGING_CORE__TOTAL_PERIODS_ELAPSED_1_LL = 0x0998, + RANGING_CORE__AMBIENT_MISMATCH_MM = 0x0999, + RANGING_CORE__AMBIENT_MISMATCH_LM = 0x099A, + RANGING_CORE__AMBIENT_MISMATCH_LL = 0x099B, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_REF_1_MMM = 0x099C, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_REF_1_LMM = 0x099D, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_REF_1_LLM = 0x099E, + RANGING_CORE__AMBIENT_WINDOW_EVENTS_REF_1_LLL = 0x099F, + RANGING_CORE__RANGING_TOTAL_EVENTS_REF_1_MMM = 0x09A0, + RANGING_CORE__RANGING_TOTAL_EVENTS_REF_1_LMM = 0x09A1, + RANGING_CORE__RANGING_TOTAL_EVENTS_REF_1_LLM = 0x09A2, + RANGING_CORE__RANGING_TOTAL_EVENTS_REF_1_LLL = 0x09A3, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_REF_1_MMM = 0x09A4, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_REF_1_LMM = 0x09A5, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_REF_1_LLM = 0x09A6, + RANGING_CORE__SIGNAL_TOTAL_EVENTS_REF_1_LLL = 0x09A7, + RANGING_CORE__TOTAL_PERIODS_ELAPSED_REF_1_MM = 0x09A8, + RANGING_CORE__TOTAL_PERIODS_ELAPSED_REF_1_LM = 0x09A9, + RANGING_CORE__TOTAL_PERIODS_ELAPSED_REF_1_LL = 0x09AA, + RANGING_CORE__AMBIENT_MISMATCH_REF_MM = 0x09AB, + RANGING_CORE__AMBIENT_MISMATCH_REF_LM = 0x09AC, + RANGING_CORE__AMBIENT_MISMATCH_REF_LL = 0x09AD, + RANGING_CORE__GPIO_CONFIG__A0 = 0x0A00, + RANGING_CORE__RESET_CONTROL__A0 = 0x0A01, + RANGING_CORE__INTR_MANAGER__A0 = 0x0A02, + RANGING_CORE__POWER_FSM_TIME_OSC__A0 = 0x0A06, + RANGING_CORE__VCSEL_ATEST__A0 = 0x0A07, + RANGING_CORE__VCSEL_PERIOD_CLIPPED__A0 = 0x0A08, + RANGING_CORE__VCSEL_STOP_CLIPPED__A0 = 0x0A09, + RANGING_CORE__CALIB_2__A0 = 0x0A0A, + RANGING_CORE__STOP_CONDITION__A0 = 0x0A0B, + RANGING_CORE__STATUS_RESET__A0 = 0x0A0C, + RANGING_CORE__READOUT_CFG__A0 = 0x0A0D, + RANGING_CORE__WINDOW_SETTING__A0 = 0x0A0E, + RANGING_CORE__VCSEL_DELAY__A0 = 0x0A1A, + RANGING_CORE__REFERENCE_2__A0 = 0x0A1B, + RANGING_CORE__REGAVDD1V2__A0 = 0x0A1D, + RANGING_CORE__TST_MUX__A0 = 0x0A1F, + RANGING_CORE__CUSTOM_FE_2__A0 = 0x0A20, + RANGING_CORE__SPAD_READOUT__A0 = 0x0A21, + RANGING_CORE__CPUMP_1__A0 = 0x0A22, + RANGING_CORE__SPARE_REGISTER__A0 = 0x0A23, + RANGING_CORE__VCSEL_CONT_STAGE5_BYPASS__A0 = 0x0A24, + RANGING_CORE__RET_SPAD_EN_18 = 0x0A25, + RANGING_CORE__RET_SPAD_EN_19 = 0x0A26, + RANGING_CORE__RET_SPAD_EN_20 = 0x0A27, + RANGING_CORE__RET_SPAD_EN_21 = 0x0A28, + RANGING_CORE__RET_SPAD_EN_22 = 0x0A29, + RANGING_CORE__RET_SPAD_EN_23 = 0x0A2A, + RANGING_CORE__RET_SPAD_EN_24 = 0x0A2B, + RANGING_CORE__RET_SPAD_EN_25 = 0x0A2C, + RANGING_CORE__RET_SPAD_EN_26 = 0x0A2D, + RANGING_CORE__RET_SPAD_EN_27 = 0x0A2E, + RANGING_CORE__RET_SPAD_EN_28 = 0x0A2F, + RANGING_CORE__RET_SPAD_EN_29 = 0x0A30, + RANGING_CORE__RET_SPAD_EN_30 = 0x0A31, + RANGING_CORE__RET_SPAD_EN_31 = 0x0A32, + RANGING_CORE__REF_SPAD_EN_0__EWOK = 0x0A33, + RANGING_CORE__REF_SPAD_EN_1__EWOK = 0x0A34, + RANGING_CORE__REF_SPAD_EN_2__EWOK = 0x0A35, + RANGING_CORE__REF_SPAD_EN_3__EWOK = 0x0A36, + RANGING_CORE__REF_SPAD_EN_4__EWOK = 0x0A37, + RANGING_CORE__REF_SPAD_EN_5__EWOK = 0x0A38, + RANGING_CORE__REF_EN_START_SELECT = 0x0A39, + RANGING_CORE__REGDVDD1V2_ATEST__EWOK = 0x0A41, + SOFT_RESET_GO1 = 0x0B00, + PRIVATE__PATCH_BASE_ADDR_RSLV = 0x0E00, + PREV_SHADOW_RESULT__INTERRUPT_STATUS = 0x0ED0, + PREV_SHADOW_RESULT__RANGE_STATUS = 0x0ED1, + PREV_SHADOW_RESULT__REPORT_STATUS = 0x0ED2, + PREV_SHADOW_RESULT__STREAM_COUNT = 0x0ED3, + PREV_SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x0ED4, + PREV_SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x0ED4, + PREV_SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x0ED5, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0 = 0x0ED6, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0_HI = 0x0ED6, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0_LO = 0x0ED7, + PREV_SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD0 = 0x0ED8, + PREV_SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD0_HI = 0x0ED8, + PREV_SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD0_LO = 0x0ED9, + PREV_SHADOW_RESULT__SIGMA_SD0 = 0x0EDA, + PREV_SHADOW_RESULT__SIGMA_SD0_HI = 0x0EDA, + PREV_SHADOW_RESULT__SIGMA_SD0_LO = 0x0EDB, + PREV_SHADOW_RESULT__PHASE_SD0 = 0x0EDC, + PREV_SHADOW_RESULT__PHASE_SD0_HI = 0x0EDC, + PREV_SHADOW_RESULT__PHASE_SD0_LO = 0x0EDD, + PREV_SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0 = 0x0EDE, + PREV_SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0_HI = 0x0EDE, + PREV_SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0_LO = 0x0EDF, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0 = 0x0EE0, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_HI = 0x0EE0, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_LO = 0x0EE1, + PREV_SHADOW_RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x0EE2, + PREV_SHADOW_RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x0EE2, + PREV_SHADOW_RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x0EE3, + PREV_SHADOW_RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x0EE4, + PREV_SHADOW_RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x0EE4, + PREV_SHADOW_RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x0EE5, + PREV_SHADOW_RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0 = 0x0EE6, + PREV_SHADOW_RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0_HI = 0x0EE6, + PREV_SHADOW_RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0_LO = 0x0EE7, + PREV_SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1 = 0x0EE8, + PREV_SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1_HI = 0x0EE8, + PREV_SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1_LO = 0x0EE9, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1 = 0x0EEA, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1_HI = 0x0EEA, + PREV_SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1_LO = 0x0EEB, + PREV_SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD1 = 0x0EEC, + PREV_SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD1_HI = 0x0EEC, + PREV_SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD1_LO = 0x0EED, + PREV_SHADOW_RESULT__SIGMA_SD1 = 0x0EEE, + PREV_SHADOW_RESULT__SIGMA_SD1_HI = 0x0EEE, + PREV_SHADOW_RESULT__SIGMA_SD1_LO = 0x0EEF, + PREV_SHADOW_RESULT__PHASE_SD1 = 0x0EF0, + PREV_SHADOW_RESULT__PHASE_SD1_HI = 0x0EF0, + PREV_SHADOW_RESULT__PHASE_SD1_LO = 0x0EF1, + PREV_SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1 = 0x0EF2, + PREV_SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1_HI = 0x0EF2, + PREV_SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1_LO = 0x0EF3, + PREV_SHADOW_RESULT__SPARE_0_SD1 = 0x0EF4, + PREV_SHADOW_RESULT__SPARE_0_SD1_HI = 0x0EF4, + PREV_SHADOW_RESULT__SPARE_0_SD1_LO = 0x0EF5, + PREV_SHADOW_RESULT__SPARE_1_SD1 = 0x0EF6, + PREV_SHADOW_RESULT__SPARE_1_SD1_HI = 0x0EF6, + PREV_SHADOW_RESULT__SPARE_1_SD1_LO = 0x0EF7, + PREV_SHADOW_RESULT__SPARE_2_SD1 = 0x0EF8, + PREV_SHADOW_RESULT__SPARE_2_SD1_HI = 0x0EF8, + PREV_SHADOW_RESULT__SPARE_2_SD1_LO = 0x0EF9, + PREV_SHADOW_RESULT__SPARE_3_SD1 = 0x0EFA, + PREV_SHADOW_RESULT__SPARE_3_SD1_HI = 0x0EFA, + PREV_SHADOW_RESULT__SPARE_3_SD1_LO = 0x0EFB, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0 = 0x0EFC, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_3 = 0x0EFC, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_2 = 0x0EFD, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_1 = 0x0EFE, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_0 = 0x0EFF, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0 = 0x0F00, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_3 = 0x0F00, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_2 = 0x0F01, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_1 = 0x0F02, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_0 = 0x0F03, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0 = 0x0F04, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_3 = 0x0F04, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_2 = 0x0F05, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_1 = 0x0F06, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_0 = 0x0F07, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0 = 0x0F08, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_3 = 0x0F08, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_2 = 0x0F09, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_1 = 0x0F0A, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_0 = 0x0F0B, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1 = 0x0F0C, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_3 = 0x0F0C, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_2 = 0x0F0D, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_1 = 0x0F0E, + PREV_SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_0 = 0x0F0F, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1 = 0x0F10, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_3 = 0x0F10, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_2 = 0x0F11, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_1 = 0x0F12, + PREV_SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_0 = 0x0F13, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1 = 0x0F14, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_3 = 0x0F14, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_2 = 0x0F15, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_1 = 0x0F16, + PREV_SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_0 = 0x0F17, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1 = 0x0F18, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_3 = 0x0F18, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_2 = 0x0F19, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_1 = 0x0F1A, + PREV_SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_0 = 0x0F1B, + PREV_SHADOW_RESULT_CORE__SPARE_0 = 0x0F1C, + RESULT__DEBUG_STATUS = 0x0F20, + RESULT__DEBUG_STAGE = 0x0F21, + GPH__SYSTEM__THRESH_RATE_HIGH = 0x0F24, + GPH__SYSTEM__THRESH_RATE_HIGH_HI = 0x0F24, + GPH__SYSTEM__THRESH_RATE_HIGH_LO = 0x0F25, + GPH__SYSTEM__THRESH_RATE_LOW = 0x0F26, + GPH__SYSTEM__THRESH_RATE_LOW_HI = 0x0F26, + GPH__SYSTEM__THRESH_RATE_LOW_LO = 0x0F27, + GPH__SYSTEM__INTERRUPT_CONFIG_GPIO = 0x0F28, + GPH__DSS_CONFIG__ROI_MODE_CONTROL = 0x0F2F, + GPH__DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT = 0x0F30, + GPH__DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT_HI = 0x0F30, + GPH__DSS_CONFIG__MANUAL_EFFECTIVE_SPADS_SELECT_LO = 0x0F31, + GPH__DSS_CONFIG__MANUAL_BLOCK_SELECT = 0x0F32, + GPH__DSS_CONFIG__MAX_SPADS_LIMIT = 0x0F33, + GPH__DSS_CONFIG__MIN_SPADS_LIMIT = 0x0F34, + GPH__MM_CONFIG__TIMEOUT_MACROP_A_HI = 0x0F36, + GPH__MM_CONFIG__TIMEOUT_MACROP_A_LO = 0x0F37, + GPH__MM_CONFIG__TIMEOUT_MACROP_B_HI = 0x0F38, + GPH__MM_CONFIG__TIMEOUT_MACROP_B_LO = 0x0F39, + GPH__RANGE_CONFIG__TIMEOUT_MACROP_A_HI = 0x0F3A, + GPH__RANGE_CONFIG__TIMEOUT_MACROP_A_LO = 0x0F3B, + GPH__RANGE_CONFIG__VCSEL_PERIOD_A = 0x0F3C, + GPH__RANGE_CONFIG__VCSEL_PERIOD_B = 0x0F3D, + GPH__RANGE_CONFIG__TIMEOUT_MACROP_B_HI = 0x0F3E, + GPH__RANGE_CONFIG__TIMEOUT_MACROP_B_LO = 0x0F3F, + GPH__RANGE_CONFIG__SIGMA_THRESH = 0x0F40, + GPH__RANGE_CONFIG__SIGMA_THRESH_HI = 0x0F40, + GPH__RANGE_CONFIG__SIGMA_THRESH_LO = 0x0F41, + GPH__RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS = 0x0F42, + GPH__RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS_HI = 0x0F42, + GPH__RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS_LO = 0x0F43, + GPH__RANGE_CONFIG__VALID_PHASE_LOW = 0x0F44, + GPH__RANGE_CONFIG__VALID_PHASE_HIGH = 0x0F45, + FIRMWARE__INTERNAL_STREAM_COUNT_DIV = 0x0F46, + FIRMWARE__INTERNAL_STREAM_COUNTER_VAL = 0x0F47, + DSS_CALC__ROI_CTRL = 0x0F54, + DSS_CALC__SPARE_1 = 0x0F55, + DSS_CALC__SPARE_2 = 0x0F56, + DSS_CALC__SPARE_3 = 0x0F57, + DSS_CALC__SPARE_4 = 0x0F58, + DSS_CALC__SPARE_5 = 0x0F59, + DSS_CALC__SPARE_6 = 0x0F5A, + DSS_CALC__SPARE_7 = 0x0F5B, + DSS_CALC__USER_ROI_SPAD_EN_0 = 0x0F5C, + DSS_CALC__USER_ROI_SPAD_EN_1 = 0x0F5D, + DSS_CALC__USER_ROI_SPAD_EN_2 = 0x0F5E, + DSS_CALC__USER_ROI_SPAD_EN_3 = 0x0F5F, + DSS_CALC__USER_ROI_SPAD_EN_4 = 0x0F60, + DSS_CALC__USER_ROI_SPAD_EN_5 = 0x0F61, + DSS_CALC__USER_ROI_SPAD_EN_6 = 0x0F62, + DSS_CALC__USER_ROI_SPAD_EN_7 = 0x0F63, + DSS_CALC__USER_ROI_SPAD_EN_8 = 0x0F64, + DSS_CALC__USER_ROI_SPAD_EN_9 = 0x0F65, + DSS_CALC__USER_ROI_SPAD_EN_10 = 0x0F66, + DSS_CALC__USER_ROI_SPAD_EN_11 = 0x0F67, + DSS_CALC__USER_ROI_SPAD_EN_12 = 0x0F68, + DSS_CALC__USER_ROI_SPAD_EN_13 = 0x0F69, + DSS_CALC__USER_ROI_SPAD_EN_14 = 0x0F6A, + DSS_CALC__USER_ROI_SPAD_EN_15 = 0x0F6B, + DSS_CALC__USER_ROI_SPAD_EN_16 = 0x0F6C, + DSS_CALC__USER_ROI_SPAD_EN_17 = 0x0F6D, + DSS_CALC__USER_ROI_SPAD_EN_18 = 0x0F6E, + DSS_CALC__USER_ROI_SPAD_EN_19 = 0x0F6F, + DSS_CALC__USER_ROI_SPAD_EN_20 = 0x0F70, + DSS_CALC__USER_ROI_SPAD_EN_21 = 0x0F71, + DSS_CALC__USER_ROI_SPAD_EN_22 = 0x0F72, + DSS_CALC__USER_ROI_SPAD_EN_23 = 0x0F73, + DSS_CALC__USER_ROI_SPAD_EN_24 = 0x0F74, + DSS_CALC__USER_ROI_SPAD_EN_25 = 0x0F75, + DSS_CALC__USER_ROI_SPAD_EN_26 = 0x0F76, + DSS_CALC__USER_ROI_SPAD_EN_27 = 0x0F77, + DSS_CALC__USER_ROI_SPAD_EN_28 = 0x0F78, + DSS_CALC__USER_ROI_SPAD_EN_29 = 0x0F79, + DSS_CALC__USER_ROI_SPAD_EN_30 = 0x0F7A, + DSS_CALC__USER_ROI_SPAD_EN_31 = 0x0F7B, + DSS_CALC__USER_ROI_0 = 0x0F7C, + DSS_CALC__USER_ROI_1 = 0x0F7D, + DSS_CALC__MODE_ROI_0 = 0x0F7E, + DSS_CALC__MODE_ROI_1 = 0x0F7F, + SIGMA_ESTIMATOR_CALC__SPARE_0 = 0x0F80, + VHV_RESULT__PEAK_SIGNAL_RATE_MCPS = 0x0F82, + VHV_RESULT__PEAK_SIGNAL_RATE_MCPS_HI = 0x0F82, + VHV_RESULT__PEAK_SIGNAL_RATE_MCPS_LO = 0x0F83, + VHV_RESULT__SIGNAL_TOTAL_EVENTS_REF = 0x0F84, + VHV_RESULT__SIGNAL_TOTAL_EVENTS_REF_3 = 0x0F84, + VHV_RESULT__SIGNAL_TOTAL_EVENTS_REF_2 = 0x0F85, + VHV_RESULT__SIGNAL_TOTAL_EVENTS_REF_1 = 0x0F86, + VHV_RESULT__SIGNAL_TOTAL_EVENTS_REF_0 = 0x0F87, + PHASECAL_RESULT__PHASE_OUTPUT_REF = 0x0F88, + PHASECAL_RESULT__PHASE_OUTPUT_REF_HI = 0x0F88, + PHASECAL_RESULT__PHASE_OUTPUT_REF_LO = 0x0F89, + DSS_RESULT__TOTAL_RATE_PER_SPAD = 0x0F8A, + DSS_RESULT__TOTAL_RATE_PER_SPAD_HI = 0x0F8A, + DSS_RESULT__TOTAL_RATE_PER_SPAD_LO = 0x0F8B, + DSS_RESULT__ENABLED_BLOCKS = 0x0F8C, + DSS_RESULT__NUM_REQUESTED_SPADS = 0x0F8E, + DSS_RESULT__NUM_REQUESTED_SPADS_HI = 0x0F8E, + DSS_RESULT__NUM_REQUESTED_SPADS_LO = 0x0F8F, + MM_RESULT__INNER_INTERSECTION_RATE = 0x0F92, + MM_RESULT__INNER_INTERSECTION_RATE_HI = 0x0F92, + MM_RESULT__INNER_INTERSECTION_RATE_LO = 0x0F93, + MM_RESULT__OUTER_COMPLEMENT_RATE = 0x0F94, + MM_RESULT__OUTER_COMPLEMENT_RATE_HI = 0x0F94, + MM_RESULT__OUTER_COMPLEMENT_RATE_LO = 0x0F95, + MM_RESULT__TOTAL_OFFSET = 0x0F96, + MM_RESULT__TOTAL_OFFSET_HI = 0x0F96, + MM_RESULT__TOTAL_OFFSET_LO = 0x0F97, + XTALK_CALC__XTALK_FOR_ENABLED_SPADS = 0x0F98, + XTALK_CALC__XTALK_FOR_ENABLED_SPADS_3 = 0x0F98, + XTALK_CALC__XTALK_FOR_ENABLED_SPADS_2 = 0x0F99, + XTALK_CALC__XTALK_FOR_ENABLED_SPADS_1 = 0x0F9A, + XTALK_CALC__XTALK_FOR_ENABLED_SPADS_0 = 0x0F9B, + XTALK_RESULT__AVG_XTALK_USER_ROI_KCPS = 0x0F9C, + XTALK_RESULT__AVG_XTALK_USER_ROI_KCPS_3 = 0x0F9C, + XTALK_RESULT__AVG_XTALK_USER_ROI_KCPS_2 = 0x0F9D, + XTALK_RESULT__AVG_XTALK_USER_ROI_KCPS_1 = 0x0F9E, + XTALK_RESULT__AVG_XTALK_USER_ROI_KCPS_0 = 0x0F9F, + XTALK_RESULT__AVG_XTALK_MM_INNER_ROI_KCPS = 0x0FA0, + XTALK_RESULT__AVG_XTALK_MM_INNER_ROI_KCPS_3 = 0x0FA0, + XTALK_RESULT__AVG_XTALK_MM_INNER_ROI_KCPS_2 = 0x0FA1, + XTALK_RESULT__AVG_XTALK_MM_INNER_ROI_KCPS_1 = 0x0FA2, + XTALK_RESULT__AVG_XTALK_MM_INNER_ROI_KCPS_0 = 0x0FA3, + XTALK_RESULT__AVG_XTALK_MM_OUTER_ROI_KCPS = 0x0FA4, + XTALK_RESULT__AVG_XTALK_MM_OUTER_ROI_KCPS_3 = 0x0FA4, + XTALK_RESULT__AVG_XTALK_MM_OUTER_ROI_KCPS_2 = 0x0FA5, + XTALK_RESULT__AVG_XTALK_MM_OUTER_ROI_KCPS_1 = 0x0FA6, + XTALK_RESULT__AVG_XTALK_MM_OUTER_ROI_KCPS_0 = 0x0FA7, + RANGE_RESULT__ACCUM_PHASE = 0x0FA8, + RANGE_RESULT__ACCUM_PHASE_3 = 0x0FA8, + RANGE_RESULT__ACCUM_PHASE_2 = 0x0FA9, + RANGE_RESULT__ACCUM_PHASE_1 = 0x0FAA, + RANGE_RESULT__ACCUM_PHASE_0 = 0x0FAB, + RANGE_RESULT__OFFSET_CORRECTED_RANGE = 0x0FAC, + RANGE_RESULT__OFFSET_CORRECTED_RANGE_HI = 0x0FAC, + RANGE_RESULT__OFFSET_CORRECTED_RANGE_LO = 0x0FAD, + SHADOW_PHASECAL_RESULT__VCSEL_START = 0x0FAE, + SHADOW_RESULT__INTERRUPT_STATUS = 0x0FB0, + SHADOW_RESULT__RANGE_STATUS = 0x0FB1, + SHADOW_RESULT__REPORT_STATUS = 0x0FB2, + SHADOW_RESULT__STREAM_COUNT = 0x0FB3, + SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x0FB4, + SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x0FB4, + SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x0FB5, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0 = 0x0FB6, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0_HI = 0x0FB6, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD0_LO = 0x0FB7, + SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD0 = 0x0FB8, + SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD0_HI = 0x0FB8, + SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD0_LO = 0x0FB9, + SHADOW_RESULT__SIGMA_SD0 = 0x0FBA, + SHADOW_RESULT__SIGMA_SD0_HI = 0x0FBA, + SHADOW_RESULT__SIGMA_SD0_LO = 0x0FBB, + SHADOW_RESULT__PHASE_SD0 = 0x0FBC, + SHADOW_RESULT__PHASE_SD0_HI = 0x0FBC, + SHADOW_RESULT__PHASE_SD0_LO = 0x0FBD, + SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0 = 0x0FBE, + SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0_HI = 0x0FBE, + SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0_LO = 0x0FBF, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0 = 0x0FC0, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_HI = 0x0FC0, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_LO = 0x0FC1, + SHADOW_RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x0FC2, + SHADOW_RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x0FC2, + SHADOW_RESULT__MM_INNER_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x0FC3, + SHADOW_RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0 = 0x0FC4, + SHADOW_RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0_HI = 0x0FC4, + SHADOW_RESULT__MM_OUTER_ACTUAL_EFFECTIVE_SPADS_SD0_LO = 0x0FC5, + SHADOW_RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0 = 0x0FC6, + SHADOW_RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0_HI = 0x0FC6, + SHADOW_RESULT__AVG_SIGNAL_COUNT_RATE_MCPS_SD0_LO = 0x0FC7, + SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1 = 0x0FC8, + SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1_HI = 0x0FC8, + SHADOW_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD1_LO = 0x0FC9, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1 = 0x0FCA, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1_HI = 0x0FCA, + SHADOW_RESULT__PEAK_SIGNAL_COUNT_RATE_MCPS_SD1_LO = 0x0FCB, + SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD1 = 0x0FCC, + SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD1_HI = 0x0FCC, + SHADOW_RESULT__AMBIENT_COUNT_RATE_MCPS_SD1_LO = 0x0FCD, + SHADOW_RESULT__SIGMA_SD1 = 0x0FCE, + SHADOW_RESULT__SIGMA_SD1_HI = 0x0FCE, + SHADOW_RESULT__SIGMA_SD1_LO = 0x0FCF, + SHADOW_RESULT__PHASE_SD1 = 0x0FD0, + SHADOW_RESULT__PHASE_SD1_HI = 0x0FD0, + SHADOW_RESULT__PHASE_SD1_LO = 0x0FD1, + SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1 = 0x0FD2, + SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1_HI = 0x0FD2, + SHADOW_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD1_LO = 0x0FD3, + SHADOW_RESULT__SPARE_0_SD1 = 0x0FD4, + SHADOW_RESULT__SPARE_0_SD1_HI = 0x0FD4, + SHADOW_RESULT__SPARE_0_SD1_LO = 0x0FD5, + SHADOW_RESULT__SPARE_1_SD1 = 0x0FD6, + SHADOW_RESULT__SPARE_1_SD1_HI = 0x0FD6, + SHADOW_RESULT__SPARE_1_SD1_LO = 0x0FD7, + SHADOW_RESULT__SPARE_2_SD1 = 0x0FD8, + SHADOW_RESULT__SPARE_2_SD1_HI = 0x0FD8, + SHADOW_RESULT__SPARE_2_SD1_LO = 0x0FD9, + SHADOW_RESULT__SPARE_3_SD1 = 0x0FDA, + SHADOW_RESULT__THRESH_INFO = 0x0FDB, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0 = 0x0FDC, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_3 = 0x0FDC, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_2 = 0x0FDD, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_1 = 0x0FDE, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD0_0 = 0x0FDF, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0 = 0x0FE0, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_3 = 0x0FE0, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_2 = 0x0FE1, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_1 = 0x0FE2, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD0_0 = 0x0FE3, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0 = 0x0FE4, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_3 = 0x0FE4, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_2 = 0x0FE5, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_1 = 0x0FE6, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD0_0 = 0x0FE7, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0 = 0x0FE8, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_3 = 0x0FE8, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_2 = 0x0FE9, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_1 = 0x0FEA, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD0_0 = 0x0FEB, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1 = 0x0FEC, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_3 = 0x0FEC, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_2 = 0x0FED, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_1 = 0x0FEE, + SHADOW_RESULT_CORE__AMBIENT_WINDOW_EVENTS_SD1_0 = 0x0FEF, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1 = 0x0FF0, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_3 = 0x0FF0, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_2 = 0x0FF1, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_1 = 0x0FF2, + SHADOW_RESULT_CORE__RANGING_TOTAL_EVENTS_SD1_0 = 0x0FF3, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1 = 0x0FF4, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_3 = 0x0FF4, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_2 = 0x0FF5, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_1 = 0x0FF6, + SHADOW_RESULT_CORE__SIGNAL_TOTAL_EVENTS_SD1_0 = 0x0FF7, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1 = 0x0FF8, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_3 = 0x0FF8, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_2 = 0x0FF9, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_1 = 0x0FFA, + SHADOW_RESULT_CORE__TOTAL_PERIODS_ELAPSED_SD1_0 = 0x0FFB, + SHADOW_RESULT_CORE__SPARE_0 = 0x0FFC, + SHADOW_PHASECAL_RESULT__REFERENCE_PHASE_HI = 0x0FFE, + SHADOW_PHASECAL_RESULT__REFERENCE_PHASE_LO = 0x0FFF, + }; + + enum DistanceMode { Short, Medium, Long, Unknown }; + + enum RangeStatus : uint8_t + { + RangeValid = 0, + + // "sigma estimator check is above the internal defined threshold" + // (sigma = standard deviation of measurement) + SigmaFail = 1, + + // "signal value is below the internal defined threshold" + SignalFail = 2, + + // "Target is below minimum detection threshold." + RangeValidMinRangeClipped = 3, + + // "phase is out of bounds" + // (nothing detected in range; try a longer distance mode if applicable) + OutOfBoundsFail = 4, + + // "HW or VCSEL failure" + HardwareFail = 5, + + // "The Range is valid but the wraparound check has not been done." + RangeValidNoWrapCheckFail = 6, + + // "Wrapped target, not matching phases" + // "no matching phase in other VCSEL period timing." + WrapTargetFail = 7, + + // "Internal algo underflow or overflow in lite ranging." + // ProcessingFail = 8: not used in API + + // "Specific to lite ranging." + // should never occur with this lib (which uses low power auto ranging, + // as the API does) + XtalkSignalFail = 9, + + // "1st interrupt when starting ranging in back to back mode. Ignore + // data." + // should never occur with this lib + SynchronizationInt = 10, // (the API spells this "syncronisation") + + // "All Range ok but object is result of multiple pulses merging together. + // Used by RQL for merged pulse detection" + // RangeValid MergedPulse = 11: not used in API + + // "Used by RQL as different to phase fail." + // TargetPresentLackOfSignal = 12: + + // "Target is below minimum detection threshold." + MinRangeFail = 13, + + // "The reported range is invalid" + // RangeInvalid = 14: can't actually be returned by API (range can never become negative, even after correction) + + // "No Update." + None = 255, + }; + + struct RangingData + { + uint16_t range_mm; + RangeStatus range_status; + float peak_signal_count_rate_MCPS; + float ambient_count_rate_MCPS; + }; + + RangingData ranging_data; + + uint8_t last_status; // status of last I2C transmission + + VL53L1X(); + + void setAddress(uint8_t new_addr); + uint8_t getAddress() { return address; } + + bool init(bool io_2v8 = true); + + void writeReg(uint16_t reg, uint8_t value); + void writeReg16Bit(uint16_t reg, uint16_t value); + void writeReg32Bit(uint16_t reg, uint32_t value); + uint8_t readReg(regAddr reg); + uint16_t readReg16Bit(uint16_t reg); + uint32_t readReg32Bit(uint16_t reg); + + bool setDistanceMode(DistanceMode mode); + DistanceMode getDistanceMode() { return distance_mode; } + + bool setMeasurementTimingBudget(uint32_t budget_us); + uint32_t getMeasurementTimingBudget(); + + void startContinuous(uint32_t period_ms); + void stopContinuous(); + uint16_t read(bool blocking = true); + uint16_t readRangeContinuousMillimeters(bool blocking = true) { return read(blocking); } // alias of read() + + // check if sensor has new reading available + // assumes interrupt is active low (GPIO_HV_MUX__CTRL bit 4 is 1) + bool dataReady() { return (readReg(GPIO__TIO_HV_STATUS) & 0x01) == 0; } + + static const char * rangeStatusToString(RangeStatus status); + + void setTimeout(uint16_t timeout) { io_timeout = timeout; } + uint16_t getTimeout() { return io_timeout; } + bool timeoutOccurred(); + + private: + + // The Arduino two-wire interface uses a 7-bit number for the address, + // and sets the last bit correctly based on reads and writes + static const uint8_t AddressDefault = 0b0101001; + + // value used in measurement timing budget calculations + // assumes PresetMode is LOWPOWER_AUTONOMOUS + // + // vhv = LOWPOWER_AUTO_VHV_LOOP_DURATION_US + LOWPOWERAUTO_VHV_LOOP_BOUND + // (tuning parm default) * LOWPOWER_AUTO_VHV_LOOP_DURATION_US + // = 245 + 3 * 245 = 980 + // TimingGuard = LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING + + // LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING + vhv + // = 1448 + 2100 + 980 = 4528 + static const uint32_t TimingGuard = 4528; + + // value in DSS_CONFIG__TARGET_TOTAL_RATE_MCPS register, used in DSS + // calculations + static const uint16_t TargetRate = 0x0A00; + + // for storing values read from RESULT__RANGE_STATUS (0x0089) + // through RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0_LOW + // (0x0099) + struct ResultBuffer + { + uint8_t range_status; + // uint8_t report_status: not used + uint8_t stream_count; + uint16_t dss_actual_effective_spads_sd0; + // uint16_t peak_signal_count_rate_mcps_sd0: not used + uint16_t ambient_count_rate_mcps_sd0; + // uint16_t sigma_sd0: not used + // uint16_t phase_sd0: not used + uint16_t final_crosstalk_corrected_range_mm_sd0; + uint16_t peak_signal_count_rate_crosstalk_corrected_mcps_sd0; + }; + + // making this static would save RAM for multiple instances as long as there + // aren't multiple sensors being read at the same time (e.g. on separate + // I2C buses) + ResultBuffer results; + + uint8_t address; + + uint16_t io_timeout; + bool did_timeout; + uint16_t timeout_start_ms; + + uint16_t fast_osc_frequency; + uint16_t osc_calibrate_val; + + bool calibrated; + uint8_t saved_vhv_init; + uint8_t saved_vhv_timeout; + + DistanceMode distance_mode; + + // Record the current time to check an upcoming timeout against + void startTimeout() { timeout_start_ms = millis(); } + + // Check if timeout is enabled (set to nonzero value) and has expired + bool checkTimeoutExpired() {return (io_timeout > 0) && ((uint16_t)(millis() - timeout_start_ms) > io_timeout); } + + void setupManualCalibration(); + void readResults(); + void updateDSS(); + void getRangingData(); + + static uint32_t decodeTimeout(uint16_t reg_val); + static uint16_t encodeTimeout(uint32_t timeout_mclks); + static uint32_t timeoutMclksToMicroseconds(uint32_t timeout_mclks, uint32_t macro_period_us); + static uint32_t timeoutMicrosecondsToMclks(uint32_t timeout_us, uint32_t macro_period_us); + uint32_t calcMacroPeriod(uint8_t vcsel_period); + + // Convert count rate from fixed point 9.7 format to float + float countRateFixedToFloat(uint16_t count_rate_fixed) { return (float)count_rate_fixed / (1 << 7); } +}; \ No newline at end of file diff --git a/lib/vl53l1x-arduino-1.01/examples/Continuous/Continuous.ino b/lib/vl53l1x-arduino-1.01/examples/Continuous/Continuous.ino new file mode 100644 index 000000000..e910466fa --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/examples/Continuous/Continuous.ino @@ -0,0 +1,44 @@ +/* +This example shows how to take simple range measurements with the VL53L1X. The +range readings are in units of mm. +*/ + +#include +#include + +VL53L1X sensor; + +void setup() +{ + Serial.begin(115200); + Wire.begin(); + Wire.setClock(400000); // use 400 kHz I2C + + sensor.setTimeout(500); + if (!sensor.init()) + { + Serial.println("Failed to detect and initialize sensor!"); + while (1); + } + + // Use long distance mode and allow up to 50000 us (50 ms) for a measurement. + // You can change these settings to adjust the performance of the sensor, but + // the minimum timing budget is 20 ms for short distance mode and 33 ms for + // medium and long distance modes. See the VL53L1X datasheet for more + // information on range and timing limits. + sensor.setDistanceMode(VL53L1X::Long); + sensor.setMeasurementTimingBudget(50000); + + // Start continuous readings at a rate of one measurement every 50 ms (the + // inter-measurement period). This period should be at least as long as the + // timing budget. + sensor.startContinuous(50); +} + +void loop() +{ + Serial.print(sensor.read()); + if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); } + + Serial.println(); +} diff --git a/lib/vl53l1x-arduino-1.01/examples/ContinuousWithDetails/ContinuousWithDetails.ino b/lib/vl53l1x-arduino-1.01/examples/ContinuousWithDetails/ContinuousWithDetails.ino new file mode 100644 index 000000000..938b79bfc --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/examples/ContinuousWithDetails/ContinuousWithDetails.ino @@ -0,0 +1,55 @@ +/* +This example takes range measurements with the VL53L1X and displays additional +details (status and signal/ambient rates) for each measurement, which can help +you determine whether the sensor is operating normally and the reported range is +valid. The range is in units of mm, and the rates are in units of MCPS (mega +counts per second). +*/ + +#include +#include + +VL53L1X sensor; + +void setup() +{ + Serial.begin(115200); + Wire.begin(); + Wire.setClock(400000); // use 400 kHz I2C + + sensor.setTimeout(500); + if (!sensor.init()) + { + Serial.println("Failed to detect and initialize sensor!"); + while (1); + } + + // Use long distance mode and allow up to 50000 us (50 ms) for a measurement. + // You can change these settings to adjust the performance of the sensor, but + // the minimum timing budget is 20 ms for short distance mode and 33 ms for + // medium and long distance modes. See the VL53L1X datasheet for more + // information on range and timing limits. + sensor.setDistanceMode(VL53L1X::Long); + sensor.setMeasurementTimingBudget(50000); + + // Start continuous readings at a rate of one measurement every 50 ms (the + // inter-measurement period). This period should be at least as long as the + // timing budget. + sensor.startContinuous(50); +} + +void loop() +{ + sensor.read(); + + Serial.print("range: "); + Serial.print(sensor.ranging_data.range_mm); + Serial.print("\tstatus: "); + Serial.print(VL53L1X::rangeStatusToString(sensor.ranging_data.range_status)); + Serial.print("\tpeak signal: "); + Serial.print(sensor.ranging_data.peak_signal_count_rate_MCPS); + Serial.print("\tambient: "); + Serial.print(sensor.ranging_data.ambient_count_rate_MCPS); + + Serial.println(); +} \ No newline at end of file diff --git a/lib/vl53l1x-arduino-1.01/keywords.txt b/lib/vl53l1x-arduino-1.01/keywords.txt new file mode 100644 index 000000000..32e3d3fa3 --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/keywords.txt @@ -0,0 +1,41 @@ +VL53L1X KEYWORD1 + +setAddress KEYWORD2 +getAddress KEYWORD2 +init KEYWORD2 +writeReg KEYWORD2 +writeReg16Bit KEYWORD2 +writeReg32Bit KEYWORD2 +readReg KEYWORD2 +readReg16Bit KEYWORD2 +readReg32Bit KEYWORD2 +setDistanceMode KEYWORD2 +getDistanceMode KEYWORD2 +setMeasurementTimingBudget KEYWORD2 +getMeasurementTimingBudget KEYWORD2 +startContinuous KEYWORD2 +stopContinuous KEYWORD2 +read KEYWORD2 +readRangeContinuousMillimeters KEYWORD2 +rangeStatusToString KEYWORD2 +setTimeout KEYWORD2 +getTimeout KEYWORD2 +timeoutOccurred KEYWORD2 + +Short LITERAL1 +Medium LITERAL1 +Long LITERAL1 +Unknown LITERAL1 + +RangeValid LITERAL1 +SigmaFail LITERAL1 +SignalFail LITERAL1 +RangeValidMinRangeClipped LITERAL1 +OutOfBoundsFail LITERAL1 +HardwareFail LITERAL1 +RangeValidNoWrapCheckFail LITERAL1 +WrapTargetFail LITERAL1 +XtalkSignalFail LITERAL1 +SyncronisationInt LITERAL1 +MinRangeFail LITERAL1 +None LITERAL1 \ No newline at end of file diff --git a/lib/vl53l1x-arduino-1.01/library.properties b/lib/vl53l1x-arduino-1.01/library.properties new file mode 100644 index 000000000..1bf06dae9 --- /dev/null +++ b/lib/vl53l1x-arduino-1.01/library.properties @@ -0,0 +1,9 @@ +name=VL53L1X +version=1.0.1 +author=Pololu +maintainer=Pololu +sentence=VL53L1X distance sensor library +paragraph=This is a library for the Arduino IDE that helps interface with ST's VL53L1X distance sensor. +category=Sensors +url=https://github.com/pololu/vl53l1x-arduino +architectures=* diff --git a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp index 85cb0663e..687661ac9 100755 --- a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp +++ b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.cpp @@ -654,7 +654,7 @@ bool ESP32_MailClient::readMail(IMAPData &imapData) { _sdOk = sdTest(); if (_sdOk) - if (!SD.exists(imapData._savePath.c_str())) + if (!imapData.fsp->exists(imapData._savePath.c_str())) createDirs(imapData._savePath); } else if (imapData._storageType == MailClientStorageType::SPIFFS) @@ -1788,6 +1788,7 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData) else { +/* if (!_sdOk) { if (smtpData._storageType == MailClientStorageType::SD) @@ -1798,13 +1799,18 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData) if (!_sdOk) continue; - +*/ bool file_existed = false; +/* if (smtpData._storageType == MailClientStorageType::SD) file_existed = SD.exists(smtpData._attach._filename[i].c_str()); else if (smtpData._storageType == MailClientStorageType::SPIFFS) file_existed = SPIFFS.exists(smtpData._attach._filename[i].c_str()); + else if (smtpData._storageType == MailClientStorageType::FFat) + file_existed = FFat.exists(smtpData._attach._filename[i].c_str()); +*/ + file_existed = smtpData.fsp->exists(smtpData._attach._filename[i].c_str()); if (file_existed) { smtpData._cbData._info = smtpData._attach._filename[i]; @@ -1820,10 +1826,15 @@ bool ESP32_MailClient::sendMail(SMTPData &smtpData) smtpData._net->getStreamPtr()->print(buf.c_str()); File file; + /* if (smtpData._storageType == MailClientStorageType::SD) file = SD.open(smtpData._attach._filename[i].c_str(), FILE_READ); else if (smtpData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(smtpData._attach._filename[i].c_str(), FILE_READ); + else if (smtpData._storageType == MailClientStorageType::FFat) + file = FFat.open(smtpData._attach._filename[i].c_str(), FILE_READ); + */ + file = smtpData.fsp->open(smtpData._attach._filename[i].c_str(), FILE_READ); send_base64_encode_mime_file(smtpData._net->getStreamPtr(), file); smtpData._net->getStreamPtr()->print(ESP32_MAIL_STR_34); @@ -2036,6 +2047,7 @@ bool ESP32_MailClient::sdBegin(uint8_t sck, uint8_t miso, uint8_t mosi, uint8_t _mosi = mosi; _ss = ss; _sdConfigSet = true; + SPI.begin(_sck, _miso, _mosi, _ss); return SD.begin(_ss, SPI); } @@ -2331,7 +2343,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT delete[] midx; if (imapData._storageType == MailClientStorageType::SD) - if (!SD.exists(filepath.c_str())) + if (!imapData.fsp->exists(filepath.c_str())) createDirs(filepath); if (!imapData._headerSaved) @@ -2353,7 +2365,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT } if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(filepath.c_str(), FILE_WRITE); + file = imapData.fsp->open(filepath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(filepath.c_str(), FILE_WRITE); } @@ -2922,7 +2934,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT delete[] midx; if (imapData._storageType == MailClientStorageType::SD) - if (!SD.exists(filepath.c_str())) + if (!imapData.fsp->exists(filepath.c_str())) createDirs(filepath); if (!imapData._headerSaved) @@ -2944,7 +2956,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT } if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(filepath.c_str(), FILE_WRITE); + file = imapData.fsp->open(filepath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(filepath.c_str(), FILE_WRITE); } @@ -3003,7 +3015,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT delete[] midx; if (imapData._storageType == MailClientStorageType::SD) - if (!SD.exists(filepath.c_str())) + if (!imapData.fsp->exists(filepath.c_str())) createDirs(filepath); filepath += ESP32_MAIL_STR_202; @@ -3011,7 +3023,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT filepath += imapData._messageDataInfo[mailIndex][messageDataIndex]._filename; if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(filepath.c_str(), FILE_WRITE); + file = imapData.fsp->open(filepath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(filepath.c_str(), FILE_WRITE); } @@ -3167,7 +3179,7 @@ bool ESP32_MailClient::waitIMAPResponse(IMAPData &imapData, uint8_t imapCommandT { if (imapData._storageType == MailClientStorageType::SD) - file = SD.open(hpath.c_str(), FILE_WRITE); + file = imapData.fsp->open(hpath.c_str(), FILE_WRITE); else if (imapData._storageType == MailClientStorageType::SPIFFS) file = SPIFFS.open(hpath.c_str(), FILE_WRITE); @@ -3714,6 +3726,17 @@ void IMAPData::setFetchUID(const String &fetchUID) void IMAPData::setFileStorageType(uint8_t storageType) { _storageType = storageType; + switch (storageType) { + case MailClientStorageType::SPIFFS: + fsp = &SPIFFS; + break; + case MailClientStorageType::SD: + fsp = &SD; + break; + case MailClientStorageType::FFat: + fsp = &FFat; + break; + } } void IMAPData::setDownloadAttachment(bool download) @@ -4733,6 +4756,17 @@ void SMTPData::removeAttachFile(uint8_t index) void SMTPData::setFileStorageType(uint8_t storageType) { _storageType = storageType; + switch (storageType) { + case MailClientStorageType::SPIFFS: + fsp = &SPIFFS; + break; + case MailClientStorageType::SD: + fsp = &SD; + break; + case MailClientStorageType::FFat: + fsp = &FFat; + break; + } } void SMTPData::clearAttachData() diff --git a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h index 943cd62f7..b6a8da775 100755 --- a/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h +++ b/libesp32/ESP32-Mail-Client/src/ESP32_MailClient.h @@ -48,6 +48,7 @@ #include "RFC2047.h" #include "ESP32MailHTTPClient.h" #include "ESP32TimeHelper.h" +#include #define FORMAT_SPIFFS_IF_FAILED true @@ -90,6 +91,7 @@ struct MailClientStorageType { static const uint8_t SPIFFS = 0; static const uint8_t SD = 1; + static const uint8_t FFat = 2; }; static const char ESP32_MAIL_STR_1[] PROGMEM = "Content-Type: multipart/mixed; boundary=\""; @@ -1302,6 +1304,7 @@ private: std::string _host = ""; uint16_t _port = 993; uint8_t _storageType = 1; + FS *fsp = &SD; bool _unseen = false; std::string _loginEmail = ""; std::string _loginPassword = ""; @@ -1859,7 +1862,7 @@ protected: string _host = ""; uint16_t _port = 0; uint8_t _storageType = 1; - + FS *fsp = &SD; string _fromName = ""; string _senderEmail = ""; string _subject = ""; diff --git a/libesp32/NimBLE-Arduino/API_DIFFERENCES.md b/libesp32/NimBLE-Arduino/API_DIFFERENCES.md deleted file mode 100644 index 91b0fd78f..000000000 --- a/libesp32/NimBLE-Arduino/API_DIFFERENCES.md +++ /dev/null @@ -1,245 +0,0 @@ -# Server API differnces: - -### Characteristics: -When creating a characteristic the properties are now set with `NIMBLE_PROPERTY::XXXX` instead of `BLECharacteristic::XXXX`. - -#### Previous: -``` -BLECharacteristic::PROPERTY_READ | -BLECharacteristic::PROPERTY_WRITE -``` - -#### Changed to: -``` -NIMBLE_PROPERTY::READ | -NIMBLE_PROPERTY::WRITE -``` - -#### The full list of properties: -``` -NIMBLE_PROPERTY::READ -NIMBLE_PROPERTY::READ_ENC -NIMBLE_PROPERTY::READ_AUTHEN -NIMBLE_PROPERTY::READ_AUTHOR -NIMBLE_PROPERTY::WRITE -NIMBLE_PROPERTY::WRITE_NR -NIMBLE_PROPERTY::WRITE_ENC -NIMBLE_PROPERTY::WRITE_AUTHEN -NIMBLE_PROPERTY::WRITE_AUTHOR -NIMBLE_PROPERTY::BROADCAST -NIMBLE_PROPERTY::NOTIFY -NIMBLE_PROPERTY::INDICATE -``` - -### Descriptors: -Descriptors are now created using the NimBLEcharacteristic method `createDescriptor()`. - -The previous method `addDescriptor()` is now a private function in the library. - -This was done because the NimBLE host automatically creates a 0x2902 descriptor if a characteristic has notify or indicate properties applied. -Due to this fact, this library also creates one automatically for your application. -The only reason to manually create this descriptor now is to assign callback functions. -If you do not require this functionality you can safely exclude the manual creation of that descriptor. - - -For any other descriptor, (except 0x2904, see below) it should now be created just as characteristics are -by invoking the `NimBLECharacteristic::createDescriptor` methods. -Which are defined as: -``` -NimBLEDescriptor* createDescriptor(const char* uuid, - uint32_t properties = NIMBLE_PROPERTY::READ | - NIMBLE_PROPERTY::WRITE, - uint16_t max_len = 100); - -NimBLEDescriptor* createDescriptor(NimBLEUUID uuid, - uint32_t properties = NIMBLE_PROPERTY::READ | - NIMBLE_PROPERTY::WRITE, - uint16_t max_len = 100); -``` -##### Example: -``` -pDescriptor = pCharacteristic->createDescriptor("ABCD", - NIMBLE_PROPERTY::READ | - NIMBLE_PROPERTY::WRITE | - NIMBLE_PROPERTY::WRITE_ENC, - 25);` -``` -Would create a descriptor with the UUID 0xABCD, publicly readable but only writable if paired/bonded (encrypted) and has a max value length of 25 bytes. - -For the 0x2904 descriptor, there is a special class that is created when you call `createDescriptor("2904")`. - -The pointer returned is of the base class `NimBLEDescriptor` but the call will create the derived class of `NimBLE2904` so you must cast the returned pointer to `NimBLE2904*` to access the specific class methods. - -##### Example: -``` -p2904 = (NimBLE2904*)pCharacteristic->createDescriptor("2904"); -``` - -#### Server Security: -Security is set on the characteristic or descriptor properties by applying one of the following: -``` -NIMBLE_PROPERTY::READ_ENC -NIMBLE_PROPERTY::READ_AUTHEN -NIMBLE_PROPERTY::READ_AUTHOR -NIMBLE_PROPERTY::WRITE_ENC -NIMBLE_PROPERTY::WRITE_AUTHEN -NIMBLE_PROPERTY::WRITE_AUTHOR -``` -When a peer wants to read or write a characteristic or descriptor with any of these properties applied -it will trigger the pairing process. By default the "just-works" pairing will be performed automatically. -This can be changed to use passkey authentication or numeric confirmation. See below for details. - - -# Client API Differences: -The `BLEAdvertisedDeviceCallbacks` class `onResult()` method now receives a pointer to the -`NimBLEAdvertisedDevice` object instead of a copy. - -`NimBLEClient::connect()` now takes an extra parameter to indicate if the client should download the services - database from the peripheral, default value is true. - -Defined as: -``` -bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true); -bool connect(NimBLEAddress address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); -``` -If set to false the client will use the services database it retrieved from the peripheral last time it connected. -This allows for faster connections and power saving if the devices just dropped connection and want to reconnect. - -``` -NimBLERemoteCharacteristic::writeValue(); -NimBLERemoteCharacteristic::registerForNotify(); -``` -Now return true or false to indicate success or failure so you can choose to disconnect or try again. - -``` -NimBLEClient::getServices() -NimBLERemoteService::getCharacteristics() -``` -Now return a pointer to a `std::vector` of the respective object database instead of `std::map`. - -`NimBLERemoteService::getCharacteristicsByHandle()` -Has been removed from the API as it is no longer maintained in the library. - -The last two above changes reduce the heap usage significantly with minimal application code adjustments. - -**UPDATED** on June 21, 2020 -> ``` -> NimBLEClient::getServices(bool refresh = false) -> NimBLERemoteService::getCharacteristics(bool refresh = false) -> NimBLERemoteCharacteristic::getDecriptors(bool refresh = false) ->``` -These methods now take an optional (bool) parameter. -If true it will clear the respective vector and retrieve all the respective attributes from the peripheral. -If false(default) it will return the respective vector empty or otherwise with the currently stored attributes. - -**Removed:** the automatic discovery of all peripheral attributes as they consumed time and resources for data -the user may not be interested in. - -**Added:** `NimBLEClient::discoverAtrributes()` for the user to discover all the peripheral attributes -to replace the the former functionality. - - -> ``` ->getService(NimBLEUUID) ->getCharacteristic(NimBLEUUID) ->getDescriptor(NimBLEUUID) ->``` -These methods will now check the respective vectors for the attribute object and, if not found, will retrieve (only) -the specified attribute from the peripheral. - -These changes allow more control for the user to manage the resources used for the attributes. -*** -#### Client Security: -The client will automatically initiate security when the peripheral responds that it's required. -The default configuration will use "just-works" pairing with no bonding, if you wish to enable bonding see below. - - -# Security: -Security callback functions are now incorporated in the client/server Callbacks class. -However backward compatibility with the `BLESecurity` class is retained to minimize app code changes. - -The relevant server callbacks are defined as: -``` -bool onConfirmPIN(uint32_t pin); // accept or reject the passkey -void onAuthenticationComplete(ble_gap_conn_desc* desc); // auth complete - details in desc -bool onPassKeyNotify(uint32_t pass_key); // receive the passkey sent by the client, accept or reject -``` -The relevant client callbacks are defined as: -``` -bool onConfirmPIN(uint32_t pin); // accept or reject the passkey -void onAuthenticationComplete(ble_gap_conn_desc* desc); // auth complete - details in desc -uint32_t onPassKeyRequest(); // return the passkey to send to the server -``` - -Security settings and IO capabilities are now set by the corresponding method of `NimBLEDevice::`. -``` -static void setSecurityAuth(bool bonding, bool mitm, bool sc); -static void setSecurityAuth(uint8_t auth_req); -static void setSecurityIOCap(uint8_t iocap); -static void setSecurityInitKey(uint8_t init_key); -static void setSecurityRespKey(uint8_t init_key); - - -/** - * @brief Set the authorization mode for this device. - * @param bonding, if true we allow bonding, false no bonding will be performed. - * @param mitm, if true we are capable of man in the middle protection, false if not. - * @param sc, if true we will perform secure connection pairing, false we will use legacy pairing. - */ -void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) - - - -/** - * @brief Set the authorization mode for this device. - * @param A bitmap indicating what modes are supported. - * The bits are defined as follows: - ** 0x01 BLE_SM_PAIR_AUTHREQ_BOND - ** 0x04 BLE_SM_PAIR_AUTHREQ_MITM - ** 0x08 BLE_SM_PAIR_AUTHREQ_SC - ** 0x10 BLE_SM_PAIR_AUTHREQ_KEYPRESS - not yet supported. - ** 0xe2 BLE_SM_PAIR_AUTHREQ_RESERVED - for reference only. - */ -void NimBLEDevice::setSecurityAuth(uint8_t auth_req) - - - -/** - * @brief Set the Input/Output capabilities of this device. - * @param One of the following: - ** 0x00 BLE_HS_IO_DISPLAY_ONLY DisplayOnly IO capability - ** 0x01 BLE_HS_IO_DISPLAY_YESNO DisplayYesNo IO capability - ** 0x02 BLE_HS_IO_KEYBOARD_ONLY KeyboardOnly IO capability - ** 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability - ** 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability - */ -void NimBLEDevice::setSecurityIOCap(uint8_t iocap) - - - -/** - * @brief If we are the initiator of the security procedure this sets the keys we will distribute. - * @param A bitmap indicating which keys to distribute during pairing. - * The bits are defined as follows: - ** 0x01: BLE_SM_PAIR_KEY_DIST_ENC - Distribute the encryption key. - ** 0x02: BLE_SM_PAIR_KEY_DIST_ID - Distribute the ID key (IRK). - ** 0x04: BLE_SM_PAIR_KEY_DIST_SIGN - ** 0x08: BLE_SM_PAIR_KEY_DIST_LINK - */ -void NimBLEDevice::setSecurityInitKey(uint8_t init_key) - - -/** - * @brief Set the keys we are willing to accept during pairing. - * @param A bitmap indicating which keys to accept during pairing. - * The bits are defined as follows: - ** 0x01: BLE_SM_PAIR_KEY_DIST_ENC - Accept the encryption key. - ** 0x02: BLE_SM_PAIR_KEY_DIST_ID - Accept the ID key (IRK). - ** 0x04: BLE_SM_PAIR_KEY_DIST_SIGN - ** 0x08: BLE_SM_PAIR_KEY_DIST_LINK - */ -void NimBLEDevice::setSecurityRespKey(uint8_t init_key) -``` - - I'm sure there are more things I have forgotten but this is all the majors. - I will update this document as necessary. diff --git a/libesp32/NimBLE-Arduino/CHANGELOG.md b/libesp32/NimBLE-Arduino/CHANGELOG.md new file mode 100644 index 000000000..8dfc5a141 --- /dev/null +++ b/libesp32/NimBLE-Arduino/CHANGELOG.md @@ -0,0 +1,38 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [1.0.2] - 2020-09-13 + +### Changed + +- `NimBLEAdvertising::start` Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a +callback that is invoked when advertsing ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API). + +- (Arduino) Maximum BLE connections can now be altered by only changing the value of `CONFIG_BT_NIMBLE_MAX_CONNECTIONS` in `nimconfig.h`. +Any changes to the controller max connection settings in `sdkconfig.h` will now have no effect when using this library. + +- (Arduino) Revert the previous change to fix the advertising start delay. Instead a replacement fix that routes all BLE controller commands from +a task running on core 0 (same as the controller) has been implemented. This improves response times and reliability for all BLE functions. + +## [1.0.1] - 2020-09-02 + +### Added + +- Empty `NimBLEAddress` constructor: `NimBLEAddress()` produces an address of 00:00:00:00:00:00 type 0. +- Documentation of the difference of NimBLEAddress::getNative vs the original bluedroid library. + +### Changed + +- notify_callback typedef is now defined as std::function to enable the use of std::bind to call a class member function. + +### Fixed + +- Fix advertising start delay when first called. + + +## [1.0.0] - 2020-08-22 + +First stable release. + +All the original library functionality is complete and many extras added with full documentation. diff --git a/libesp32/NimBLE-Arduino/README.md b/libesp32/NimBLE-Arduino/README.md index 04c3a80c5..120b0c782 100644 --- a/libesp32/NimBLE-Arduino/README.md +++ b/libesp32/NimBLE-Arduino/README.md @@ -1,36 +1,19 @@ -# *** UPDATES *** -**Breaking changes:** -**NEW** on June 21, 2020 -> ``` -> NimBLEClient::getServices(bool refresh = false) -> NimBLERemoteService::getCharacteristics(bool refresh = false) -> NimBLERemoteCharacteristic::getDecriptors(bool refresh = false) ->``` -These methods now take an optional (bool) parameter. -If true it will clear the respective vector and retrieve all the respective attributes from the peripheral. -If false(default) it will return the respective vector empty or otherwise with the currently stored attributes. - -**NEW** on May 23, 2020 -Client and scan now use `std::vector` instead of `std::map` for storing the remote attribute database. - -This change will affect your application code if you use `NimBLEClient::getServices()` or `NimBLERemoteService::getCharacteristics()` -in your application as they now return a pointer to `std::vector` of the respective attributes. - -In addition `NimBLERemoteService::getCharacteristicsByHandle()` has been removed as it is no longer maintained in the library. - -These changes were necessary due to the amount of resources required to use `std::map`, it was not justifed by any benfit it provided. - -It is expected that there will be minimal impact on most applications, if you need help adjusting your code please create an issue. +[Latest release ![Release Version](https://img.shields.io/github/release/h2zero/NimBLE-Arduino.svg?style=plastic) +![Release Date](https://img.shields.io/github/release-date/h2zero/NimBLE-Arduino.svg?style=plastic)](https://github.com/h2zero/NimBLE-Arduino/releases/latest/) +
# NimBLE-Arduino -A fork of the NimBLE stack restructured for compilation in the Ardruino IDE with a CPP library for use with ESP32. +A fork of the NimBLE stack restructured for compilation in the Ardruino IDE with a CPP library for use with ESP32. + +**Note for IDF users: This repo will not compile correctly in ESP-IDF. An ESP-IDF component version of this library can be [found here.](https://github.com/h2zero/esp-nimble-cpp)** This library **significantly** reduces resource usage and improves performance for ESP32 BLE applications as compared with the bluedroid based library. The goal is to maintain, as much as reasonable, compatibility with the original library but refactored to use the NimBLE stack. In addition, this library will be more actively developed and maintained -to provide improved capabilites and stability over the original. +to provide improved capabilites and stability over the original. +
-## Resource use improvement: +## Resource use improvement ### (Original) BLE_client example comparison (Debug): #### Arduino BLE Library @@ -50,21 +33,29 @@ Memory after connection: Free Heap: **173300** Sketch uses **603432** bytes (28%) of program storage space. Memory after connection: Free Heap: **269792** -**As shown: there is nearly a 50% reduction in flash use and approx. 100kB less ram consumed!** - - -# Installation: +**As shown: there is nearly a 50% reduction in flash use and approx. 100kB less ram consumed!** +
-Download as .zip and extract to Arduino/libraries folder, or in Arduino IDE from Sketch menu -> Include library -> Add .Zip library. +# Installation +**Arduino Library manager:** Go to `sketch` -> `Include Library` -> `Manage Libraries` and search for NimBLE and install. + +**Alternatively:** Download as .zip and extract to Arduino/libraries folder, or in Arduino IDE from Sketch menu -> Include library -> Add .Zip library. `#include "NimBLEDevice.h"` at the beginning of your sketch. -Tested and working with esp32-arduino v1.0.2 and 1.0.4 in Arduino IDE v1.8.12 and platform IO. +Tested and working with esp32-arduino in Arduino IDE and platform IO. +
+# Using +This library is intended to be compatible with the original ESP32 BLE functions and types with minor changes. -# Usage: +If you have not used the original Bluedroid library please refer to the [New user guide](docs/New_user_guide.md). -This library is intended to be compatible with the original ESP32 BLE functions and types with minor changes. +If you are familiar with the original library, see: [The migration guide](docs/Migration_guide.md) for details about breaking changes and migration. + +Also see [Improvements_and_updates](docs/Improvements_and_updates.md) for information about non-breaking changes. + +[Full API documentation and class list can be found here.](https://h2zero.github.io/esp-nimble-cpp/) Check the Refactored_original_examples in the examples folder for highlights of the differences with the original library. @@ -72,26 +63,25 @@ More advanced examples highlighting many available features are in examples/ Nim Beacon examples provided by @beegee-tokyo are in examples/ BLE_Beacon_Scanner, BLE_EddystoneTLM_Beacon, BLE_EddystoneURL_Beacon. -Change the settings in the `nimconfig.h` file to customize NimBLE to your project, such as increasing max connections, default is 3. +Change the settings in the `src/nimconfig.h` file to customize NimBLE to your project, +such as increasing max connections, default is 3, absolute maximum connections is 9. +
+# Development Status +This Library is tracking the esp-nimble repo, nimble-1.2.0-idf master branch, currently [@95bd864.](https://github.com/espressif/esp-nimble) -# Continuing development: - -This Library is tracking the esp-nimble repo, nimble-1.2.0-idf master branch, currently [@46c1d9f.](https://github.com/espressif/esp-nimble) - -Also tracking the NimBLE related changes in esp-idf, master branch, currently [@2ef4890.](https://github.com/espressif/esp-idf/tree/master/components/bt/host/nimble) - -# Acknowledgments: - -* @nkolban and @chegewara for the [original esp32 BLE library](https://github.com/nkolban/esp32-snippets) this project was derived from. -* @beegee-tokyo for contributing your time to test/debug and contributing the beacon examples. -* @Jeroen88 for the amazing help debugging and improving the client code. - - -# Todo: - -1. Create documentation. -2. Add BLE Mesh code. -3. Expose more NimBLE features. +Also tracking the NimBLE related changes in ESP-IDF, master branch, currently [@2ef4890.](https://github.com/espressif/esp-idf/tree/master/components/bt/host/nimble) +
+# Acknowledgments +* [nkolban](https://github.com/nkolban) and [chegewara](https://github.com/chegewara) for the [original esp32 BLE library](https://github.com/nkolban/esp32-snippets/tree/master/cpp_utils) this project was derived from. +* [beegee-tokyo](https://github.com/beegee-tokyo) for contributing your time to test/debug and contributing the beacon examples. +* [Jeroen88](https://github.com/Jeroen88) for the amazing help debugging and improving the client code. +
+# Todo +- Improve host reset handler +- Implement random address handling +- Implement bond management +- Add Bluetooth Mesh +
diff --git a/libesp32/NimBLE-Arduino/docs/Improvements_and_updates.md b/libesp32/NimBLE-Arduino/docs/Improvements_and_updates.md new file mode 100644 index 000000000..245defd2d --- /dev/null +++ b/libesp32/NimBLE-Arduino/docs/Improvements_and_updates.md @@ -0,0 +1,136 @@ +# Improvements and updates + +Many improvements have been made to this library vs the original, this is a brief overview of the most significant changes. +Refer to the [class documentation](https://h2zero.github.io/esp-nimble-cpp/annotated.html) for futher information on class specifics. + +* [Server](#server) +* [Advertising](#advertising) +* [Client](#client) +* [General](#general) +
+ + +# Server + +`NimBLECharacteristic::setValue(const T &s)` +`NimBLEDescriptor::setValue(const T &s)` + +Now use a template to accomodate standard and custom types/values. + +**Example** +``` +struct my_struct{ + uint8_t one; + uint16_t two; + uint32_t four; + uint64_t eight; + float flt; +}myStruct; + + myStruct.one = 1; + myStruct.two = 2; + myStruct.four = 4; + myStruct.eight = 8; + myStruct.flt = 1234.56; + + pCharacteristic->setValue(myStruct); + ``` +This will send the struct to the recieving client when read or a notification sent. + +`NimBLECharacteristic::getValue` now takes an optional timestamp parameter which will update it's value with +the time the last value was recieved. In addition an overloaded template has been added to retrieve the value +as a type specified by the user. + +**Example** +``` + time_t timestamp; + myStruct = pCharacteristic->getValue(×tamp); // timestamp optional +``` +
+ +**Advertising will automatically start when a client disconnects.** + +A new method `NimBLEServer::advertiseOnDisconnect(bool)` has been implemented to control this, true(default) = enabled. +
+ +`NimBLEServer::removeService` takes an additional parameter `bool deleteSvc` that if true will delete the service +and all characteristics / descriptors belonging to it and invalidating any pointers to them. + +If false the service is only removed from visibility by clients. The pointers to the service and +it's characteristics / descriptors will remain valid and the service can be re-added in the future +using `NimBLEServer::addService`. +
+ + +# Advertising +`NimBLEAdvertising::start` + +Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback +that is invoked when advertsing ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API). + +This provides an opportunity to update the advertisment data if desired. +
+ + +# Client + +`NimBLERemoteCharacteristic::readValue(time_t\*, bool)` +`NimBLERemoteDescriptor::readValue(bool)` + +Have been added as templates to allow reading the values as any specified type. + +**Example** +``` +struct my_struct{ + uint8_t one; + uint16_t two; + uint32_t four; + uint64_t eight; + float flt; +}myStruct; + + time_t timestamp; + myStruct = pRemoteCharacteristic->readValue(×tamp); // timestamp optional +``` +
+ +`NimBLERemoteCharacteristic::registerForNotify` +Has been **deprecated** as now the internally stored characteristic value is updated when notification/indication is recieved. + +`NimBLERemoteCharacteristic::subscribe` and `NimBLERemoteCharacteristic::unsubscribe` have been implemented to replace it. +A callback is no longer requred to get the most recent value unless timing is important. Instead, the application can call `NimBLERemoteCharacteristic::getValue` to +get the last updated value any time. + +In addition `NimBLERemoteCharacteristic::readValue` and `NimBLERemoteCharacteristic::getValue` take an optional timestamp parameter which will update it's value with +the time the last value was recieved. + +> NimBLEClient::getService +> NimBLERemoteService::getCharacteristic +> NimBLERemoteCharacteristic::getDescriptor + +These methods will now check the respective vectors for the attribute object and, if not found, will retrieve (only) +the specified attribute from the peripheral. + +These changes allow more control for the user to manage the resources used for the attributes. +
+ +`NimBLEClient::connect()` can now be called without an address or advertised device parameter. This will connect to the +device with the address previously set when last connected or set with `NimBLEDevice::setPeerAddress()`. + + +# General +To reduce resource use all instances of `std::map` have been replaced with `std::vector`. + +Use of `FreeRTOS::Semaphore` has been removed as it was consuming too much ram, the related files have been left in place to accomodate application use. + +Operators `==`, `!=` and `std::string` have been added to `NimBLEAddress` and `NimBLEUUID` for easier comparison and logging. + +New constructor for `NimBLEUUID(uint32_t, uint16_t, uint16_t, uint64_t)` added to lower memory use vs string construction. See: [#21](https://github.com/h2zero/NimBLE-Arduino/pull/21). + +Security/pairing operations are now handled in the respective `NimBLEClientCallbacks` and `NimBLEServerCallbacks` classes, `NimBLESecurity`(deprecated) remains for backward compatibility. + +Configuration options have been added to add or remove debugging information, when disabled (default) significatly reduces binary size. +In ESP-IDF the options are in menuconfig: `Main menu -> ESP-NimBLE-cpp configuration`. +For Arduino the options must be commented / uncommented in nimconfig.h. +
+ diff --git a/libesp32/NimBLE-Arduino/docs/Migration_guide.md b/libesp32/NimBLE-Arduino/docs/Migration_guide.md new file mode 100644 index 000000000..69671824a --- /dev/null +++ b/libesp32/NimBLE-Arduino/docs/Migration_guide.md @@ -0,0 +1,398 @@ +# Migrating from Bluedroid to NimBLE + +This guide describes the required changes to existing projects migrating from the original bluedroid API to NimBLE. + +**The changes listed here are only the required changes that must be made**, and a short overview of options for migrating existing applications. + +For more information on the improvements and additions please refer to the [class documentation](https://h2zero.github.io/esp-nimble-cpp/annotated.html) and [Improvements and updates](Improvements_and_updates.md) + +* [General Changes](#general-information) +* [Server](#server-api) + * [Services](#services) + * [characteristics](#characteristics) + * [descriptors](#descriptors) + * [Security](#server-security) +* [Advertising](#advertising-api) +* [Client](#client-api) + * [Remote Services](#remote-services) + * [Remote characteristics](#remote-characteristics) + * [Security](#client-security) +* [General Security](#security-api) +* [Configuration](#arduino-configuration) +
+ + +## General Information + +### Header Files +All classes are accessible by including `NimBLEDevice.h` in your application, no further headers need to be included. + +(Mainly for Arduino) You may choose to include `NimBLELog.h` in your appplication if you want to use the `NIMBLE_LOGx` macros for debugging. +These macros are used the same way as the `ESP_LOGx` macros. +
+ +### Class Names +Class names remain the same as the original with the addition of a "Nim" prefix. +For example `BLEDevice` is now `NimBLEDevice` and `BLEServer` is now `NimBLEServer` etc. + +For convienience definitions have been added to allow applications to use either name for all classes +this means **no class names need to be changed in existing code** and makes migrating easier. +
+ +### BLE Addresses +`BLEAddress` (`NimBLEAddress`) When constructing an address the constructor now takes an *(optional)* `uint8_t type` paramameter +to specify the address type. Default is (0) Public static address. + +For example `BLEAddress addr(11:22:33:44:55:66, 1)` will create the address object with an address type of: 1 (Random). + +As this paramameter is optional no changes to existing code are needed, it is mentioned here for information. +
+`BLEAddress::getNative` (`NimBLEAddress::getNative`) returns a uint8_t pointer to the native address byte array. +In this library the address bytes are stored in reverse order from the original library. This is due to the way +the NimBLE stack expects addresses to be presented to it. All other functions such as `toString` are +not affected as the endian change is made within them. +
+ + +## Server API +Creating a `BLEServer` instance is the same as original, no changes required. +For example `BLEDevice::createServer()` will work just as it did before. + +`BLEServerCallbacks` (`NimBLEServerCallbacks`) has new methods for handling security operations. +**Note:** All callback methods have default implementations which allows the application to implement only the methods applicable. +
+ + +### Services +Creating a `BLEService` (`NimBLEService`) instance is the same as original, no changes required. +For example `BLEServer::createService(SERVICE_UUID)` will work just as it did before. + + +### Characteristics +`BLEService::createCharacteristic` (`NimBLEService::createCharacteristic`) is used the same way as originally except the properties parameter has changed. + +When creating a characteristic the properties are now set with `NIMBLE_PROPERTY::XXXX` instead of `BLECharacteristic::XXXX`. + +#### Originally +> BLECharacteristic::PROPERTY_READ | +> BLECharacteristic::PROPERTY_WRITE + +#### Is Now +> NIMBLE_PROPERTY::READ | +> NIMBLE_PROPERTY::WRITE +
+ +#### The full list of properties +> NIMBLE_PROPERTY::READ +> NIMBLE_PROPERTY::READ_ENC +> NIMBLE_PROPERTY::READ_AUTHEN +> NIMBLE_PROPERTY::READ_AUTHOR +> NIMBLE_PROPERTY::WRITE +> NIMBLE_PROPERTY::WRITE_NR +> NIMBLE_PROPERTY::WRITE_ENC +> NIMBLE_PROPERTY::WRITE_AUTHEN +> NIMBLE_PROPERTY::WRITE_AUTHOR +> NIMBLE_PROPERTY::BROADCAST +> NIMBLE_PROPERTY::NOTIFY +> NIMBLE_PROPERTY::INDICATE +
+ +**Example:** +``` +BLECharacteristic *pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID, + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE + ); + +``` +Needs to be changed to: +``` +BLECharacteristic *pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID, + NIMBLE_PROPERTY::READ | + NIMBLE_PROPERTY::WRITE + ); +``` +
+ +`BLECharacteristicCallbacks` (`NimBLECharacteristicCallbacks`) has a new method `NimBLECharacteristicCallbacks::onSubscribe` +which is called when a client subscribes to notifications/indications. + +**Note:** All callback methods have default implementations which allows the application to implement only the methods applicable. +
+ +> BLECharacteristic::getData + +**Has been removed from the API.** +Originally this returned a `uint8_t*` to the internal data, which is volatile. +To prevent possibly throwing exceptions this has been removed and `NimBLECharacteristic::getValue` should be used +to get a copy of the data first which can then safely be accessed via pointer. + +**Example:** +``` +std::string value = pCharacteristic->getValue(); +uint8_t *pData = (uint8_t*)value.data(); +``` +Alternatively use the `getValue` template: +``` +my_struct_t myStruct = pChr->getValue(); +``` +
+ + +### Descriptors +The previous method `BLECharacteristic::addDescriptor()` has been removed. + +Descriptors are now created using the `NimBLECharacteristic::createDescriptor` method. + +BLE2902 or NimBLE2902 class has been removed. +NimBLE automatically creates the 0x2902 descriptor if a characteristic has a notification or indication property assigned to it. + +It was no longer useful to have a class for the 0x2902 descriptor as a new callback `NimBLECharacteristicCallbacks::onSubscribe` was added +to handle callback functionality and the client subscription status is handled internally. + +**Note:** Attempting to create a 0x2902 descriptor will trigger an assert to notify the error, +allowing the creation of it would cause a fault in the NimBLE stack. + +All other descriptors are now created just as characteristics are by using the `NimBLECharacteristic::createDescriptor` method (except 0x2904, see below). +Which are defined as: +``` +NimBLEDescriptor* createDescriptor(const char* uuid, + uint32_t properties = + NIMBLE_PROPERTY::READ | + NIMBLE_PROPERTY::WRITE, + uint16_t max_len = 100); + +NimBLEDescriptor* createDescriptor(NimBLEUUID uuid, + uint32_t properties = + NIMBLE_PROPERTY::READ | + NIMBLE_PROPERTY::WRITE, + uint16_t max_len = 100); +``` +##### Example +``` +pDescriptor = pCharacteristic->createDescriptor("ABCD", + NIMBLE_PROPERTY::READ | + NIMBLE_PROPERTY::WRITE | + NIMBLE_PROPERTY::WRITE_ENC, + 25); +``` +Would create a descriptor with the UUID 0xABCD, publicly readable but only writable if paired/bonded (encrypted) and has a max value length of 25 bytes. +
+ +For the 0x2904, there is a special class that is created when you call `createDescriptor("2904"). + +The pointer returned is of the base class `NimBLEDescriptor` but the call will create the derived class of `NimBLE2904` so you must cast the returned pointer to +`NimBLE2904` to access the specific class methods. + +##### Example +``` +p2904 = (NimBLE2904*)pCharacteristic->createDescriptor("2904"); +``` +
+ + +### Server Security +Security is set on the characteristic or descriptor properties by applying one of the following: +> NIMBLE_PROPERTY::READ_ENC +> NIMBLE_PROPERTY::READ_AUTHEN +> NIMBLE_PROPERTY::READ_AUTHOR +> NIMBLE_PROPERTY::WRITE_ENC +> NIMBLE_PROPERTY::WRITE_AUTHEN +> NIMBLE_PROPERTY::WRITE_AUTHOR + +When a peer wants to read or write a characteristic or descriptor with any of these properties applied +it will trigger the pairing process. By default the "just-works" pairing will be performed automatically. +This can be changed to use passkey authentication or numeric comparison. See [Security API](#security-api) for details. +
+ + +## Advertising API +Advertising works the same as the original API except: +> BLEAdvertising::setMinPreferred +> BLEAdvertising::setMaxPreferred + +These methods were found to not provide useful functionality and consumed valuable advertising space (6 bytes of 31) if used unknowingly. +If you wish to advertise these parameters you can still do so manually via `BLEAdvertisementData::addData` (`NimBLEAdvertisementData::addData`). +
+ +Calling `NimBLEAdvertising::setAdvertisementData` will entirely replace any data set with `NimBLEAdvertising::addServiceUUID`, or +`NimBLEAdvertising::setAppearance`. You should set all the data you wish to advertise within the `NimBLEAdvertisementData` instead. + +Calling `NimBLEAdvertising::setScanResponseData` without also calling `NimBLEAdvertising::setAdvertisementData` will have no effect. +When using custom scan response data you must also use custom advertisement data. +
+ +> BLEAdvertising::start (NimBLEAdvertising::start) + +Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback +that is invoked when advertsing ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API). + +This provides an opportunity to update the advertisment data if desired. +
+ + +## Client API + +Client instances are created just as before with `BLEDevice::createClient` (`NimBLEDevice::createClient`). + +Multiple client instances can be created, up to the maximum number of connections set in the config file (default: 3). +To delete a client instance you must use `NimBLEDevice::deleteClient`. + +`BLEClient::connect`(`NimBLEClient::connect`) Has had it's parameters altered. +Defined as: +> NimBLEClient::connect(bool deleteServices = true); +> NimBLEClient::connect(NimBLEAdvertisedDevice\* device, bool deleteServices = true); +> NimBLEClient::connect(NimBLEAddress address, bool deleteServices = true); + +The type parameter has been removed and a new bool parameter has been added to indicate if the client should +delete the attribute database previously retrieved (if applicable) for the peripheral, default value is true. +If set to false the client will use the attribute database it retrieved from the peripheral when previously connected. +This allows for faster connections and power saving if the devices dropped connection and are reconnecting. +
+ +> `BLEClient::getServices` (`NimBLEClient::getServices`) + +This method now takes an optional (bool) parameter to indicate if the services should be retrieved from the server (true) or +the currently known database returned (false : default). +Also now returns a pointer to `std::vector` instead of `std::map`. +
+ +**Removed:** the automatic discovery of all peripheral attributes as they consumed time and resources for data +the user may not be interested in. + +**Added:** `NimBLEClient::discoverAttributes` for the user to discover all the peripheral attributes +to replace the the removed automatic functionality. +
+ + +### Remote Services +`BLERemoteService` (`NimBLERemoteService`) Methods remain mostly unchanged with the exceptions of: + +> BLERemoteService::getCharacteristicsByHandle + +This method has been removed. +
+ +> `BLERemoteService::getCharacteristics` (`NimBLERemoteService::getCharacteristics`) + +This method now takes an optional (bool) parameter to indicate if the characteristics should be retrieved from the server (true) or +the currently known database returned (false : default). +Also now returns a pointer to `std::vector` instead of `std::map`. +
+ + +### Remote Characteristics +`BLERemoteCharacteristic` (`NimBLERemoteCharacteristic`) There have been a few changes to the methods in this class: + +> `BLERemoteCharacteristic::writeValue` (`NimBLERemoteCharacteristic::writeValue`) +> `BLERemoteCharacteristic::registerForNotify` (`NimBLERemoteCharacteristic::registerForNotify`) + +Now return true or false to indicate success or failure so you can choose to disconnect or try again. +
+ +> `BLERemoteCharacteristic::registerForNotify` (`NimBLERemoteCharacteristic::registerForNotify`) + +Is now **deprecated**. +> `NimBLERemoteCharacteristic::subscribe` +> `NimBLERemoteCharacteristic::unsubscribe` + +Are the new methods added to replace it. +
+ +> `BLERemoteCharacteristic::readUInt8` (`NimBLERemoteCharacteristic::readUInt8`) +> `BLERemoteCharacteristic::readUInt16` (`NimBLERemoteCharacteristic::readUInt16`) +> `BLERemoteCharacteristic::readUInt32` (`NimBLERemoteCharacteristic::readUInt32`) +> `BLERemoteCharacteristic::readFloat` (`NimBLERemoteCharacteristic::readFloat`) + +Are **deprecated** a template: NimBLERemoteCharacteristic::readValue(time_t\*, bool) has been added to replace them. +
+ +> `BLERemoteCharacteristic::readRawData` + +**Has been removed from the API** +Originally it stored an unnecessary copy of the data and was returning a `uint8_t` pointer to volatile internal data. +The user application should use `NimBLERemoteCharacteristic::readValue` or `NimBLERemoteCharacteristic::getValue`. +To obatain a copy of the data, then cast the returned std::string to the type required such as: +``` +std::string value = pChr->readValue(); +uint8_t *data = (uint8_t*)value.data(); +``` +Alternatively use the `readValue` template: +``` +my_struct_t myStruct = pChr->readValue(); +``` +
+ +> `BLERemoteCharacteristic::getDescriptors` (`NimBLERemoteCharacteristic::getDescriptors`) + +This method now takes an optional (bool) parameter to indicate if the descriptors should be retrieved from the server (true) or +the currently known database returned (false : default). +Also now returns a pointer to `std::vector` instead of `std::map`. +
+ + +### Client Security +The client will automatically initiate security when the peripheral responds that it's required. +The default configuration will use "just-works" pairing with no bonding, if you wish to enable bonding see below. +
+ + +## Security API +Security operations have been moved to `BLEDevice` (`NimBLEDevice`). + +Also security callback methods are now incorporated in the `NimBLEServerCallbacks` / `NimBLEClientCallbacks` classes. +However backward compatibility with the original `BLESecurity` (`NimBLESecurity`) class is retained to minimize application code changes. + +The callback methods are: + +> `bool onConfirmPIN(uint32_t pin)` + +Receives the pin when using numeric comparison authentication, `return true;` to accept. +
+ +> `uint32_t onPassKeyRequest()` + +For server callback; return the passkey expected from the client. +For client callback; return the passkey to send to the server. +
+ +> `void onAuthenticationComplete(ble_gap_conn_desc\* desc)` + +Authentication complete, success or failed information is in `desc`. +
+ +Security settings and IO capabilities are now set by the following methods of NimBLEDevice. +> `NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc)` +> `NimBLEDevice::setSecurityAuth(uint8_t auth_req)` + +Sets the authorization mode for this device. +
+ +> `NimBLEDevice::setSecurityIOCap(uint8_t iocap)` + +Sets the Input/Output capabilities of this device. +
+ +> `NimBLEDevice::setSecurityInitKey(uint8_t init_key)` + +If we are the initiator of the security procedure this sets the keys we will distribute. +
+ +> `NimBLEDevice::setSecurityRespKey(uint8_t resp_key)` + +Sets the keys we are willing to accept from the peer during pairing. +
+ + +## Arduino Configuration + +Unlike the original library pre-packaged in the esp32-arduino, this library has all the configuration +options that are normally set in menuconfig available in the *src/nimconfig.h* file. + +This allows Arduino users to fully customize the build, such as increasing max connections +or loading the BLE stack into external PSRAM. + +For details on the options, they are fully commented in *nimconfig.h* +
diff --git a/libesp32/NimBLE-Arduino/docs/New_user_guide.md b/libesp32/NimBLE-Arduino/docs/New_user_guide.md new file mode 100644 index 000000000..9f0eeee7d --- /dev/null +++ b/libesp32/NimBLE-Arduino/docs/New_user_guide.md @@ -0,0 +1,339 @@ +# New User Guide + +**Note:** If you are migrating an existing project from the original Bluedroid library please see the [Migration Guide.](Migration_guide.md) + +If you are a new user this will guide you through a simple server and client application. + +* [Creating a Server](#creating-a-server) +* [Creating a Client](#creating-a-client) +
+ +## Include Files +At the top of your application file add `#include NimBLEDevice.h`, this is the only header required and provides access to all classes. +
+ +## Using the Library +In order to perform any BLE tasks you must first initialize the library, this prepares the NimBLE stack to be ready for commands. + +To do this you must call `NimBLEDevice::init("your device name here")`, the parameter passed is a character string containing the name you want to advertise. +If you're not creating a server or do not want to advertise a name, simply pass an empty string for the parameter. + +This can be called any time you wish to use BLE functions and does not need to be called from app_main(IDF) or setup(Arduino) but usually is. +
+ + +## Creating a Server +BLE servers perform 2 tasks, they advertise their existance for clients to find them and they provide services which contain information for the connecting client. + +After initializing the NimBLE stack we create a server by calling `NimBLEDevice::createServer()`, this will create a server instance and return a pointer to it. + +Once we have created the server we need to tell it the services it hosts. +To do this we call `NimBLEServer::createService(const char* uuid)`. Which returns a pointer to an instance of `NimBLEService`. +The `uuid` parameter is a hexadecimal string with the uuid we want to give the service, it can be 16, 32, or 128 bits. + +For this example we will keep it simple and use a 16 bit value: ABCD. +
+ +**Example code:** +``` +#include "NimBLEDevice.h" + +// void setup() in Arduino +void app_main(void) +{ + NimBLEDevice::init("NimBLE"); + + NimBLEServer *pServer = NimBLEDevice::createServer(); + NimBLEService *pService = pServer->createService("ABCD"); +} +``` + +Now we have NimBLE initialized, a server created and a service assigned to it. +We can't do much with this yet so now we should add a characteristic to the service to provide some data. + +Next we call `NimBLEService::createCharacteristic` which returns a pointer to an instance of `NimBLECharacteristic`, and takes two parameters: +A `uuid` to specify the UUID of the characteristic and a bitmask of the properties we want applied to it. + +Just as with the service UUID we will use a simple 16 bit value: 1234. +The properties bitmask is a little more involved. It is a combination of NIMBLE_PROPERTY:: values. + +Here is the list of options: +> NIMBLE_PROPERTY\::READ +> NIMBLE_PROPERTY\::READ_ENC +> NIMBLE_PROPERTY\::READ_AUTHEN +> NIMBLE_PROPERTY\::READ_AUTHOR +> NIMBLE_PROPERTY\::WRITE +> NIMBLE_PROPERTY\::WRITE_NR +> NIMBLE_PROPERTY\::WRITE_ENC +> NIMBLE_PROPERTY\::WRITE_AUTHEN +> NIMBLE_PROPERTY\::WRITE_AUTHOR +> NIMBLE_PROPERTY\::BROADCAST +> NIMBLE_PROPERTY\::NOTIFY +> NIMBLE_PROPERTY\::INDICATE + +For this example we won't need to specify these as the default value is `NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE` +which will allow reading and writing values to the characteristic without encryption or security. +The function call will simply be `pService->createCharacteristic("1234");` +
+ +**Our example code now is:** +``` +#include "NimBLEDevice.h" + +// void setup() in Arduino +void app_main(void) +{ + NimBLEDevice::init("NimBLE"); + + NimBLEServer *pServer = NimBLEDevice::createServer(); + NimBLEService *pService = pServer->createService("ABCD"); + NimBLECharacteristic *pCharacteristic = pService->createCharacteristic("1234"); +} +``` + +All that's left to do now is start the sevice, give the characteristic a value and start advertising for clients. + +Fist we start the service by calling `NimBLEService::start()`. + +Next we need to call `NimBLECharacteristic::setValue` to set the characteristic value that the client will read. +There are many different types you can send as parameters for the value but for this example we will use a simple string. +`pCharacteristic->setValue("Hello BLE");` + +Next we need to advertise for connections. +To do this we create an instance of `NimBLEAdvertising` add our service to it (optional) and start advertisng. + +**The code for this will be:** +``` +NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); // create advertising instance +pAdvertising->addServiceUUID("ABCD"); // tell advertising the UUID of our service +pAdvertising->start(); // start advertising +``` +That's it, this will be enough to create a BLE server with a service and a characteristic and advertise for client connections. + +**The full example code:** +``` +#include "NimBLEDevice.h" + +// void setup() in Arduino +void app_main(void) +{ + NimBLEDevice::init("NimBLE"); + + NimBLEServer *pServer = NimBLEDevice::createServer(); + NimBLEService *pService = pServer->createService("ABCD"); + NimBLECharacteristic *pCharacteristic = pService->createCharacteristic("1234"); + + pService->start(); + pCharacteristic->setValue("Hello BLE"); + + NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); + pAdvertising->addServiceUUID("ABCD"); + pAdvertising->start(); +} +``` + +Now if you scan with your phone using nRFConnect or any other BLE app you should see a device named "NimBLE" with a service of "ABCD". + +For more advanced features and options please see the server examples in the examples folder. +
+ + +## Creating a Client + +BLE clients perform 2 tasks, they scan for advertising servers and form connections to them to read and write to their characteristics/descriptors. + +After initializing the NimBLE stack we create a scan instance by calling `NimBLEDevice::getScan()`, this will create a `NimBLEScan` instance and return a pointer to it. + +Once we have created the scan we can start looking for advertising servers. + +To do this we call `NimBLEScan::start(duration)`, the duration parameter is a uint32_t that specifies the number of seconds to scan for, +passing 0 will scan forever. + +In this example we will scan for 10 seconds. This is a blocking function (a non blocking overload is also available). +This call returns an instance of `NimBLEScanResults` when the scan completes which can be parsed for advertisers we are interested in. + +**Example Code:** +``` +#include "NimBLEDevice.h" + +// void setup() in Arduino +void app_main(void) +{ + NimBLEDevice::init(""); + + NimBLEScan *pScan = NimBLEDevice::getScan(); + NimBLEScanResults results = pScan->start(10); +} +``` +
+ +Now that we have scanned we need to check the results for any advertisers we are interested in connecting to. + +To do this we iterate through the results and check if any of the devices found are advertising the service we want `ABCD`. +Each result in `NimBLEScanResults` is a `NimBLEAdvertisedDevice` instance that we can access data from. + +We will check each device found for the `ABCD` service by calling `NimBLEAdvertisedDevice::isAdvertisingService`. +This takes an instance of `NimBLEUUID` as a parameter so we will need to create one. + +**The code for this looks like:** +``` +NimBLEUUID serviceUuid("ABCD"); + +for(int i = 0; i < results.getCount(); i++) { + NimBLEAdvertisedDevice device = results.getDevice(i); + + if (device.isAdvertisingService(serviceUuid)) { + // create a client and connect + } +} +``` +
+ +Now that we can scan and parse advertisers we need to be able to create a `NimBLEClient` instance and use it to connect. + +To do this we call `NimBLEDevice::createClient` which creates the `NimBLEClient` instance and returns a pointer to it. + +After this we call `NimBLEClient::connect` to connect to the advertiser. +This takes a pointer to the `NimBLEAdvertisedDevice` and returns `true` if successful. + +**Lets do that now:** +``` +NimBLEUUID serviceUuid("ABCD"); + +for(int i = 0; i < results.getCount(); i++) { + NimBLEAdvertisedDevice device = results.getDevice(i); + + if (device.isAdvertisingService(serviceUuid)) { + NimBLEClient *pClient = NimBLEDevice::createClient(); + + if(pClient->connect(&device)) { + //success + } else { + // failed to connect + } + } +} +``` +As shown, the call to `NimBLEClient::connect` should have it's eturn value tested to make sure it succeeded before proceeding to get data. +
+ +Next we need to access the servers data by asking it for the service and the characteristic we are interested in, then read the characteristic value. + +To do this we call `NimBLEClient::getService`, which takes as a parameter the UUID of the service and returns +a pointer an instance to `NimBLERemoteService` or `nullptr` if the service was not found. + +Next we will call `NimBLERemoteService::getCharateristic` which takes as a parameter the UUID of the service and returns +a pointer to an instance of `NimBLERemoteCharacteristic` or `nullptr` if not found. + +Finally we will read the characteristic value with `NimBLERemoteCharacteristic::readValue()`. + +**Here is what that looks like:** +``` +NimBLEUUID serviceUuid("ABCD"); + +for(int i = 0; i < results.getCount(); i++) { + NimBLEAdvertisedDevice device = results.getDevice(i); + + if (device.isAdvertisingService(serviceUuid)) { + NimBLEClient *pClient = NimBLEDevice::createClient(); + + if (pClient->connect(&device)) { + NimBLERemoteService *pService = pClient->getService(serviceUuid); + + if (pService != nullptr) { + NimBLERemoteCharacteristic *pCharacteristic = pService->getCharacteristic("1234"); + + if (pCharacteristic != nullptr) { + std::string value = pCharacteristic->readValue(); + // print or do whatever you need with the value + } + } + } else { + // failed to connect + } + } +} +``` +
+ +The last thing we should do is clean up once we are done with the connection. +Because multiple clients are supported and can be created we should delete them when finished with them to conserve resources. +This is done by calling `NimBLEDevice::deleteClient`. + +**Lets add that now:** +``` +NimBLEUUID serviceUuid("ABCD"); + +for(int i = 0; i < results.getCount(); i++) { + NimBLEAdvertisedDevice device = results.getDevice(i); + + if (device.isAdvertisingService(serviceUuid)) { + NimBLEClient *pClient = NimBLEDevice::createClient(); + + if (pClient->connect(&device)) { + NimBLERemoteService *pService = pClient->getService(serviceUuid); + + if (pService != nullptr) { + NimBLERemoteCharacteristic *pCharacteristic = pService->getCharacteristic("1234"); + + if (pCharacteristic != nullptr) { + std::string value = pCharacteristic->readValue(); + // print or do whatever you need with the value + } + } + } else { + // failed to connect + } + + NimBLEDevice::deleteClient(pClient); + } +} +``` +Note that there is no need to disconnect as that will be done when deleting the client instance. +
+ +**Here is the full example code:** +``` +#include "NimBLEDevice.h" + +// void setup() in Arduino +void app_main(void) +{ + NimBLEDevice::init(""); + + NimBLEScan *pScan = NimBLEDevice::getScan(); + NimBLEScanResults results = pScan->start(10); + + NimBLEUUID serviceUuid("ABCD"); + + for(int i = 0; i < results.getCount(); i++) { + NimBLEAdvertisedDevice device = results.getDevice(i); + + if (device.isAdvertisingService(serviceUuid)) { + NimBLEClient *pClient = NimBLEDevice::createClient(); + + if (pClient->connect(&device)) { + NimBLERemoteService *pService = pClient->getService(serviceUuid); + + if (pService != nullptr) { + NimBLERemoteCharacteristic *pCharacteristic = pService->getCharacteristic("1234"); + + if (pCharacteristic != nullptr) { + std::string value = pCharacteristic->readValue(); + // print or do whatever you need with the value + } + } + } else { + // failed to connect + } + + NimBLEDevice::deleteClient(pClient); + } + } +} +``` +
+ +For more advanced features and options please see the client examples in the examples folder. +
+ diff --git a/libesp32/NimBLE-Arduino/examples/NimBLE_Client/NimBLE_Client.ino b/libesp32/NimBLE-Arduino/examples/NimBLE_Client/NimBLE_Client.ino index 30eeb9fe3..9ef41b91c 100644 --- a/libesp32/NimBLE-Arduino/examples/NimBLE_Client/NimBLE_Client.ino +++ b/libesp32/NimBLE-Arduino/examples/NimBLE_Client/NimBLE_Client.ino @@ -107,9 +107,10 @@ class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks { void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify){ std::string str = (isNotify == true) ? "Notification" : "Indication"; str += " from "; - str += pRemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString(); - str += ": Service = " + pRemoteCharacteristic->getRemoteService()->getUUID().toString(); - str += ", Characteristic = " + pRemoteCharacteristic->getUUID().toString(); + /** NimBLEAddress and NimBLEUUID have std::string operators */ + str += std::string(pRemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress()); + str += ": Service = " + std::string(pRemoteCharacteristic->getRemoteService()->getUUID()); + str += ", Characteristic = " + std::string(pRemoteCharacteristic->getUUID()); str += ", Value = " + std::string((char*)pData, length); Serial.println(str.c_str()); } @@ -228,17 +229,22 @@ bool connectToServer() { } } + /** registerForNotify() has been deprecated and replaced with subscribe() / unsubscribe(). + * Subscribe parameter defaults are: notifications=true, notifyCallback=nullptr, response=false. + * Unsubscribe parameter defaults are: response=false. + */ if(pChr->canNotify()) { - /** Must send a callback to subscribe, if nullptr it will unsubscribe */ - if(!pChr->registerForNotify(notifyCB)) { + //if(!pChr->registerForNotify(notifyCB)) { + if(!pChr->subscribe(true, notifyCB)) { /** Disconnect if subscribe failed */ pClient->disconnect(); return false; } } else if(pChr->canIndicate()) { - /** Send false as second argument to subscribe to indications instead of notifications */ - if(!pChr->registerForNotify(notifyCB, false)) { + /** Send false as first argument to subscribe to indications instead of notifications */ + //if(!pChr->registerForNotify(notifyCB, false)) { + if(!pChr->subscribe(false, notifyCB)) { /** Disconnect if subscribe failed */ pClient->disconnect(); return false; @@ -289,17 +295,22 @@ bool connectToServer() { } } + /** registerForNotify() has been deprecated and replaced with subscribe() / unsubscribe(). + * Subscribe parameter defaults are: notifications=true, notifyCallback=nullptr, response=false. + * Unsubscribe parameter defaults are: response=false. + */ if(pChr->canNotify()) { - /** Must send a callback to subscribe, if nullptr it will unsubscribe */ - if(!pChr->registerForNotify(notifyCB)) { + //if(!pChr->registerForNotify(notifyCB)) { + if(!pChr->subscribe(true, notifyCB)) { /** Disconnect if subscribe failed */ pClient->disconnect(); return false; } } else if(pChr->canIndicate()) { - /** Send false as second argument to subscribe to indications instead of notifications */ - if(!pChr->registerForNotify(notifyCB, false)) { + /** Send false as first argument to subscribe to indications instead of notifications */ + //if(!pChr->registerForNotify(notifyCB, false)) { + if(!pChr->subscribe(false, notifyCB)) { /** Disconnect if subscribe failed */ pClient->disconnect(); return false; diff --git a/libesp32/NimBLE-Arduino/examples/NimBLE_Server/NimBLE_Server.ino b/libesp32/NimBLE-Arduino/examples/NimBLE_Server/NimBLE_Server.ino index e0be793d5..f66169ce8 100644 --- a/libesp32/NimBLE-Arduino/examples/NimBLE_Server/NimBLE_Server.ino +++ b/libesp32/NimBLE-Arduino/examples/NimBLE_Server/NimBLE_Server.ino @@ -9,8 +9,6 @@ */ #include -#include -#include static NimBLEServer* pServer; @@ -102,24 +100,33 @@ class CharacteristicCallbacks: public NimBLECharacteristicCallbacks { str += NimBLEUtils::returnCodeToString(code); Serial.println(str); }; + + void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue) { + String str = "Client ID: "; + str += desc->conn_handle; + str += " Address: "; + str += std::string(NimBLEAddress(desc->peer_ota_addr)).c_str(); + if(subValue == 0) { + str += " Unsubscribed to "; + }else if(subValue == 1) { + str += " Subscribed to notfications for "; + } else if(subValue == 2) { + str += " Subscribed to indications for "; + } else if(subValue == 3) { + str += " Subscribed to notifications and indications for "; + } + str += std::string(pCharacteristic->getUUID()).c_str(); + + Serial.println(str); + }; }; /** Handler class for descriptor actions */ class DescriptorCallbacks : public NimBLEDescriptorCallbacks { void onWrite(NimBLEDescriptor* pDescriptor) { - if(pDescriptor->getUUID().equals(NimBLEUUID("2902"))) { - /** Cast to NimBLE2902 to use the class specific functions. **/ - NimBLE2902* p2902 = (NimBLE2902*)pDescriptor; - if(p2902->getNotifications()) { - Serial.println("Client Subscribed to notfications"); - } else { - Serial.println("Client Unubscribed to notfications"); - } - } else { - std::string dscVal((char*)pDescriptor->getValue(), pDescriptor->getLength()); - Serial.print("Descriptor witten value:"); - Serial.println(dscVal.c_str()); - } + std::string dscVal((char*)pDescriptor->getValue(), pDescriptor->getLength()); + Serial.print("Descriptor witten value:"); + Serial.println(dscVal.c_str()); }; void onRead(NimBLEDescriptor* pDescriptor) { @@ -176,9 +183,9 @@ void setup() { pBeefCharacteristic->setValue("Burger"); pBeefCharacteristic->setCallbacks(&chrCallbacks); - /** 2902 and 2904 descriptors are a special case, when createDescriptor is called with - * either of those uuid's it will create the associated class with the correct properties - * and sizes. However we must cast the returned reference to the correct type as the method + /** 2904 descriptors are a special case, when createDescriptor is called with + * 0x2904 a NimBLE2904 class is created with the correct properties and sizes. + * However we must cast the returned reference to the correct type as the method * only returns a pointer to the base NimBLEDescriptor class. */ NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor("2904"); @@ -197,6 +204,10 @@ void setup() { pFoodCharacteristic->setValue("Fries"); pFoodCharacteristic->setCallbacks(&chrCallbacks); + /** Note a 0x2902 descriptor MUST NOT be created as NimBLE will create one automatically + * if notification or indication properties are assigned to a characteristic. + */ + /** Custom descriptor: Arguments are UUID, Properties, max length in bytes of the value */ NimBLEDescriptor* pC01Ddsc = pFoodCharacteristic->createDescriptor( "C01D", @@ -208,14 +219,6 @@ void setup() { pC01Ddsc->setValue("Send it back!"); pC01Ddsc->setCallbacks(&dscCallbacks); - /** Note a 2902 descriptor does NOT need to be created as any chactateristic with - * notification or indication properties will have one created autmatically. - * Manually creating it is only useful if you wish to handle callback functions - * as shown here. Otherwise this can be removed without loss of functionality. - */ - NimBLE2902* pFood2902 = (NimBLE2902*)pFoodCharacteristic->createDescriptor("2902"); - pFood2902->setCallbacks(&dscCallbacks); - /** Start the services when finished creating all Characteristics and Descriptors */ pDeadService->start(); pBaadService->start(); @@ -247,4 +250,4 @@ void loop() { } delay(2000); -} +} \ No newline at end of file diff --git a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_notify/BLE_notify.ino b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_notify/BLE_notify.ino index f57c52e2b..83f129b83 100644 --- a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_notify/BLE_notify.ino +++ b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_notify/BLE_notify.ino @@ -102,16 +102,13 @@ void setup() { // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // Create a BLE Descriptor - /*********** New createDescriptor method ************ - NOTE: There is no need to create the 2902 descriptor - as it will be created automatically if notifications + /*************************************************** + NOTE: DO NOT create a 2902 descriptor. + it will be created automatically if notifications or indications are enabled on a characteristic. pCharacteristic->addDescriptor(new BLE2902()); ****************************************************/ - /** Add properties the same way as characteristics now **/ - - pCharacteristic->createDescriptor("2902" /** , NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE **/); // Start the service pService->start(); @@ -119,7 +116,9 @@ void setup() { BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); - pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter + /**This method is removed as it was no longer useful and consumed advertising space + * pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter + */ BLEDevice::startAdvertising(); Serial.println("Waiting a client connection to notify..."); } diff --git a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server/BLE_server.ino b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server/BLE_server.ino index 82aa70aaa..652d77685 100644 --- a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server/BLE_server.ino +++ b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server/BLE_server.ino @@ -44,8 +44,10 @@ void setup() { BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); - pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue - pAdvertising->setMinPreferred(0x12); + /**These methods are removed as they are no longer useful and consumed advertising space + * pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue + * pAdvertising->setMinPreferred(0x12); + */ BLEDevice::startAdvertising(); Serial.println("Characteristic defined! Now you can read it in your phone!"); } diff --git a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server_multiconnect/BLE_server_multiconnect.ino b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server_multiconnect/BLE_server_multiconnect.ino index 025266650..2ec38c481 100644 --- a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server_multiconnect/BLE_server_multiconnect.ino +++ b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_server_multiconnect/BLE_server_multiconnect.ino @@ -105,16 +105,13 @@ void setup() { // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml // Create a BLE Descriptor - /*********** New createDescriptor method ************ - NOTE: There is no need to create the 2902 descriptor - as it will be created automatically if notifications + /*************************************************** + NOTE: DO NOT create a 2902 descriptor + it will be created automatically if notifications or indications are enabled on a characteristic. pCharacteristic->addDescriptor(new BLE2902()); ****************************************************/ - /** Add properties the same way as characteristics now **/ - - pCharacteristic->createDescriptor("2902" /** , NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE **/); // Start the service pService->start(); @@ -123,7 +120,9 @@ void setup() { BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); - pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter + /**This method is removed it was no longer useful and consumed advertising space + * pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter + */ BLEDevice::startAdvertising(); Serial.println("Waiting a client connection to notify..."); } diff --git a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_uart/BLE_uart.ino b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_uart/BLE_uart.ino index b83470cf6..0a31ef289 100644 --- a/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_uart/BLE_uart.ino +++ b/libesp32/NimBLE-Arduino/examples/Refactored_original_examples/BLE_uart/BLE_uart.ino @@ -112,15 +112,13 @@ void setup() { NIMBLE_PROPERTY::NOTIFY ); - /******* New createDescriptor method ******** - NOTE: There is no need to create the 2902 descriptor - as it will be created automatically if notifications or - indications are enabled on a characteristic. + /*************************************************** + NOTE: DO NOT create a 2902 descriptor + it will be created automatically if notifications + or indications are enabled on a characteristic. - pCharacteristic->addDescriptor(new BLE2902()); - ********************************************/ - /** Add properties the same way as characteristics now **/ - pTxCharacteristic->createDescriptor("2902" /** , NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE **/); + pCharacteristic->addDescriptor(new BLE2902()); + ****************************************************/ BLECharacteristic * pRxCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_RX, diff --git a/libesp32/NimBLE-Arduino/library.properties b/libesp32/NimBLE-Arduino/library.properties index 236fcd606..156c098c0 100644 --- a/libesp32/NimBLE-Arduino/library.properties +++ b/libesp32/NimBLE-Arduino/library.properties @@ -1,9 +1,9 @@ name=NimBLE-Arduino -version=0.9.0 -author=H2zero +version=1.0.2 +author=h2zero maintainer=h2zero -sentence=NimBLE library for Arduino -paragraph=A lighter-weight alternative to the bluedroid library for esp32. +sentence=Bluetooth low energy (BLE) library for arduino-esp32 based on NimBLE. +paragraph=This is a more updated and lower resource alternative to the original bluedroid BLE library for esp32. Uses 50% less flash space and approximately 100KB less ram with the same functionality. Nearly 100% compatible with existing application code, migration guide included. url=https://github.com/h2zero/NimBLE-Arduino category=Communication architectures=esp32 diff --git a/libesp32/NimBLE-Arduino/src/FreeRTOS.cpp b/libesp32/NimBLE-Arduino/src/FreeRTOS.cpp index f6bbe171b..ff1061c10 100644 --- a/libesp32/NimBLE-Arduino/src/FreeRTOS.cpp +++ b/libesp32/NimBLE-Arduino/src/FreeRTOS.cpp @@ -114,6 +114,10 @@ bool FreeRTOS::Semaphore::timedWait(std::string owner, uint32_t timeoutMs) { } // wait +/** + * @brief Construct a semaphore, the semaphore is given when created. + * @param [in] name A name string to provide debugging support. + */ FreeRTOS::Semaphore::Semaphore(std::string name) { m_usePthreads = false; // Are we using pThreads or FreeRTOS? if (m_usePthreads) { @@ -140,8 +144,7 @@ FreeRTOS::Semaphore::~Semaphore() { /** - * @brief Give a semaphore. - * The Semaphore is given. + * @brief Give the semaphore. */ void FreeRTOS::Semaphore::give() { NIMBLE_LOGD(LOG_TAG, "Semaphore giving: %s", toString().c_str()); diff --git a/libesp32/NimBLE-Arduino/src/FreeRTOS.h b/libesp32/NimBLE-Arduino/src/FreeRTOS.h index a6737b757..2f3d9386c 100644 --- a/libesp32/NimBLE-Arduino/src/FreeRTOS.h +++ b/libesp32/NimBLE-Arduino/src/FreeRTOS.h @@ -29,6 +29,9 @@ public: static uint32_t getTimeSinceStart(); +/** + * @brief A binary semaphore class that operates like a mutex, it is already given when constructed. + */ class Semaphore { public: Semaphore(std::string owner = ""); @@ -42,6 +45,10 @@ public: std::string toString(); bool timedWait(std::string owner = "", uint32_t timeoutMs = portMAX_DELAY); uint32_t wait(std::string owner = ""); + /** + * @brief Get the value of the semaphore. + * @return The value stored if the semaphore was given with give(value); + */ uint32_t value(){ return m_value; }; private: @@ -57,7 +64,7 @@ public: /** - * @brief Ringbuffer. + * @brief A wrapper class for a freeRTOS ringbuffer. */ class Ringbuffer { public: diff --git a/libesp32/NimBLE-Arduino/src/NimBLE2902.cpp b/libesp32/NimBLE-Arduino/src/NimBLE2902.cpp deleted file mode 100644 index 04a07b6f9..000000000 --- a/libesp32/NimBLE-Arduino/src/NimBLE2902.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * NimBLE2902.cpp - * - * Created: on March 10, 2020 - * Author H2zero - * - * Originally: - * - * BLE2902.cpp - * - * Created on: Jun 25, 2017 - * Author: kolban - */ - - -/* - * See also: - * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml - */ -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) - -#include "nimconfig.h" -#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) - -#include "NimBLE2902.h" - -NimBLE2902::NimBLE2902(NimBLECharacteristic* pCharacterisitic) -: NimBLEDescriptor(NimBLEUUID((uint16_t) 0x2902), - BLE_GATT_CHR_F_READ | - BLE_GATT_CHR_F_WRITE, - 2, pCharacterisitic) -{ - uint8_t data[2] = { 0, 0 }; - setValue(data, 2); -} // NimBLE2902 - - -/** - * @brief Get the notifications value. - * @return The notifications value. True if notifications are enabled and false if not. - */ -bool NimBLE2902::getNotifications() { - return (getValue()[0] & (1 << 0)) != 0; -} // getNotifications - - -/** - * @brief Get the indications value. - * @return The indications value. True if indications are enabled and false if not. - */ -bool NimBLE2902::getIndications() { - return (getValue()[0] & (1 << 1)) != 0; -} // getIndications - - -/** - * @brief Set the indications flag. - * @param [in] flag The indications flag. - */ -void NimBLE2902::setIndications(bool flag) { - uint8_t *pValue = getValue(); - if (flag) pValue[0] |= 1 << 1; - else pValue[0] &= ~(1 << 1); -} // setIndications - - -/** - * @brief Set the notifications flag. - * @param [in] flag The notifications flag. - */ -void NimBLE2902::setNotifications(bool flag) { - uint8_t *pValue = getValue(); - if (flag) pValue[0] |= 1 << 0; - else pValue[0] &= ~(1 << 0); -} // setNotifications - -#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) -#endif diff --git a/libesp32/NimBLE-Arduino/src/NimBLE2902.h b/libesp32/NimBLE-Arduino/src/NimBLE2902.h deleted file mode 100644 index 2d84b73dc..000000000 --- a/libesp32/NimBLE-Arduino/src/NimBLE2902.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * NimBLE2902.h - * - * Created: on March 10, 2020 - * Author H2zero - * - * Originally: - * - * BLE2902.h - * - * Created on: Jun 25, 2017 - * Author: kolban - */ - -#ifndef MAIN_NIMBLE2902_H_ -#define MAIN_NIMBLE2902_H_ -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) - -#include "nimconfig.h" -#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) - -#include "NimBLEDescriptor.h" - -#include - -#define NIMBLE_DESC_FLAG_NOTIFY 0x0001 -#define NIMBLE_DESC_FLAG_INDICATE 0x0002 - -typedef struct { - uint16_t conn_id; - uint16_t sub_val; -} chr_sub_status_t; - - -/** - * @brief Descriptor for Client Characteristic Configuration. - * - * This is a convenience descriptor for the Client Characteristic Configuration which has a UUID of 0x2902. - * - * See also: - * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml - */ -class NimBLE2902: public NimBLEDescriptor { -public: - bool getNotifications(); - bool getIndications(); - void setNotifications(bool flag); - void setIndications(bool flag); -private: - NimBLE2902(NimBLECharacteristic* pCharacterisitic); - friend class NimBLECharacteristic; - std::vector m_subscribedVec; - -}; // NimBLE2902 - -#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) -#endif /* CONFIG_BT_ENABLED */ -#endif /* MAIN_NIMBLE2902_H_ */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLEAddress.cpp b/libesp32/NimBLE-Arduino/src/NimBLEAddress.cpp index 8c2c68f79..e1d3e548b 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEAddress.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEAddress.cpp @@ -23,17 +23,26 @@ static const char* LOG_TAG = "NimBLEAddress"; /************************************************* -NOTE: NimBLE addresses are in INVERSE ORDER! -We will accomodate that fact in these methods. + * NOTE: NimBLE address bytes are in INVERSE ORDER! + * We will accomodate that fact in these methods. *************************************************/ /** - * @brief Create an address from the native ESP32 representation. - * @param [in] address The native representation. + * @brief Create an address from the native NimBLE representation. + * @param [in] address The native NimBLE address. */ NimBLEAddress::NimBLEAddress(ble_addr_t address) { memcpy(m_address, address.val, 6); -} // BLEAddress + m_addrType = address.type; +} // NimBLEAddress + + +/** + * @brief Create a blank address, i.e. 00:00:00:00:00:00, type 0. + */ +NimBLEAddress::NimBLEAddress() { + NimBLEAddress(""); +} // NimBLEAddress /** @@ -45,9 +54,12 @@ NimBLEAddress::NimBLEAddress(ble_addr_t address) { * ``` * which is 17 characters in length. * - * @param [in] stringAddress The hex representation of the address. + * @param [in] stringAddress The hex string representation of the address. + * @param [in] type The type of the address. */ -NimBLEAddress::NimBLEAddress(const std::string &stringAddress) { +NimBLEAddress::NimBLEAddress(const std::string &stringAddress, uint8_t type) { + m_addrType = type; + if (stringAddress.length() == 0) { memset(m_address, 0, 6); return; @@ -72,24 +84,29 @@ NimBLEAddress::NimBLEAddress(const std::string &stringAddress) { for(size_t index = 0; index < sizeof m_address; index++) { m_address[index] = data[index]; } -} // BLEAddress - - -/** - * @brief Constructor for compatibility with bluedroid esp library. - * @param [in] uint8_t[6] or esp_bd_addr_t struct containing the address. - */ -NimBLEAddress::NimBLEAddress(uint8_t address[6]) { - std::reverse_copy(address, address + sizeof m_address, m_address); } // NimBLEAddress /** - * @brief Constructor for address using a hex value. Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16" - * @param [in] uint64_t containing the address. + * @brief Constructor for compatibility with bluedroid esp library using native ESP representation. + * @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address. + * @param [in] type The type of the address. */ -NimBLEAddress::NimBLEAddress(const uint64_t &address) { +NimBLEAddress::NimBLEAddress(uint8_t address[6], uint8_t type) { + std::reverse_copy(address, address + sizeof m_address, m_address); + m_addrType = type; +} // NimBLEAddress + + +/** + * @brief Constructor for address using a hex value.\n + * Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16" + * @param [in] address uint64_t containing the address. + * @param [in] type The type of the address. + */ +NimBLEAddress::NimBLEAddress(const uint64_t &address, uint8_t type) { memcpy(m_address, &address, sizeof m_address); + m_addrType = type; } // NimBLEAddress @@ -104,14 +121,23 @@ bool NimBLEAddress::equals(const NimBLEAddress &otherAddress) const { /** - * @brief Return the native representation of the address. - * @return The native representation of the address. + * @brief Get the native representation of the address. + * @return a pointer to the uint8_t[6] array of the address. */ const uint8_t *NimBLEAddress::getNative() const { return m_address; } // getNative +/** + * @brief Get the address type. + * @return The address type. + */ +uint8_t NimBLEAddress::getType() const { + return m_addrType; +} // getType + + /** * @brief Convert a BLE address to a string. * @@ -122,30 +148,50 @@ const uint8_t *NimBLEAddress::getNative() const { * ``` * * @return The string representation of the address. + * @deprecated Use std::string() operator instead. */ std::string NimBLEAddress::toString() const { return std::string(*this); } // toString +/** + * @brief Convienience operator to check if this address is equal to another. + */ bool NimBLEAddress::operator ==(const NimBLEAddress & rhs) const { return memcmp(rhs.m_address, m_address, sizeof m_address) == 0; -} +} // operator == + +/** + * @brief Convienience operator to check if this address is not equal to another. + */ bool NimBLEAddress::operator !=(const NimBLEAddress & rhs) const { return !this->operator==(rhs); -} +} // operator != + +/** + * @brief Convienience operator to convert this address to string representation. + * @details This allows passing NimBLEAddress to functions + * that accept std::string and/or or it's methods as a parameter. + */ NimBLEAddress::operator std::string() const { char buffer[18]; - sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[5], m_address[4], m_address[3], m_address[2], m_address[1], m_address[0]); + snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x", + m_address[5], m_address[4], m_address[3], + m_address[2], m_address[1], m_address[0]); return std::string(buffer); -} +} // operator std::string + +/** + * @brief Convienience operator to convert the native address representation to uint_64. + */ NimBLEAddress::operator uint64_t() const { uint64_t address = 0; memcpy(&address, m_address, sizeof m_address); return address; -} +} // operator uint64_t #endif diff --git a/libesp32/NimBLE-Arduino/src/NimBLEAddress.h b/libesp32/NimBLE-Arduino/src/NimBLEAddress.h index 778ff8644..50f9231fe 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEAddress.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEAddress.h @@ -33,13 +33,15 @@ */ class NimBLEAddress { public: + NimBLEAddress(); NimBLEAddress(ble_addr_t address); - NimBLEAddress(uint8_t address[6]); - NimBLEAddress(const std::string &stringAddress); - NimBLEAddress(const uint64_t &address); - bool equals(const NimBLEAddress &otherAddress) const; - const uint8_t* getNative() const; - std::string toString() const; + NimBLEAddress(uint8_t address[6], uint8_t type = BLE_ADDR_PUBLIC); + NimBLEAddress(const std::string &stringAddress, uint8_t type = BLE_ADDR_PUBLIC); + NimBLEAddress(const uint64_t &address, uint8_t type = BLE_ADDR_PUBLIC); + bool equals(const NimBLEAddress &otherAddress) const; + const uint8_t* getNative() const; + std::string toString() const; + uint8_t getType() const; bool operator ==(const NimBLEAddress & rhs) const; bool operator !=(const NimBLEAddress & rhs) const; @@ -48,6 +50,7 @@ public: private: uint8_t m_address[6]; + uint8_t m_addrType; }; #endif /* CONFIG_BT_ENABLED */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.cpp b/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.cpp index c53bb688b..992b1f377 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.cpp @@ -17,6 +17,7 @@ #include "nimconfig.h" #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) +#include "NimBLEDevice.h" #include "NimBLEAdvertisedDevice.h" #include "NimBLEUtils.h" #include "NimBLELog.h" @@ -30,13 +31,10 @@ static const char* LOG_TAG = "NimBLEAdvertisedDevice"; NimBLEAdvertisedDevice::NimBLEAdvertisedDevice() { m_advType = 0; m_appearance = 0; - m_deviceType = 0; m_manufacturerData = ""; m_name = ""; m_rssi = -9999; - m_serviceData = ""; m_txPower = 0; - m_pScan = nullptr; m_payloadLength = 0; m_payload = nullptr; @@ -53,11 +51,7 @@ NimBLEAdvertisedDevice::NimBLEAdvertisedDevice() { /** - * @brief Get the address. - * - * Every %BLE device exposes an address that is used to identify it and subsequently connect to it. - * Call this function to obtain the address of the advertised device. - * + * @brief Get the address of the advertising device. * @return The address of the advertised device. */ NimBLEAddress NimBLEAdvertisedDevice::getAddress() { @@ -66,13 +60,17 @@ NimBLEAddress NimBLEAdvertisedDevice::getAddress() { /** - * @brief Get the advertised type. - * - * @return The advertised type of the advertised device. + * @brief Get the advertisement type. + * @return The advertising type the device is reporting: + * * BLE_HCI_ADV_TYPE_ADV_IND (0) - indirect advertising + * * BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD (1) - direct advertisng - high duty cycle + * * BLE_HCI_ADV_TYPE_ADV_SCAN_IND (2) - indirect scan response + * * BLE_HCI_ADV_TYPE_ADV_NONCONN_IND (3) - indirect advertisng - not connectable + * * BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD (4) - direct advertising - low duty cycle */ uint8_t NimBLEAdvertisedDevice::getAdvType() { return m_advType; -} // getAddress +} // getAdvType /** @@ -98,7 +96,7 @@ std::string NimBLEAdvertisedDevice::getManufacturerData() { /** - * @brief Get the name. + * @brief Get the advertised name. * @return The name of the advertised device. */ std::string NimBLEAdvertisedDevice::getName() { @@ -116,54 +114,100 @@ int NimBLEAdvertisedDevice::getRSSI() { /** - * @brief Get the scan object that created this advertisement. + * @brief Get the scan object that created this advertised device. * @return The scan object. */ NimBLEScan* NimBLEAdvertisedDevice::getScan() { - return m_pScan; + return NimBLEDevice::getScan(); } // getScan /** * @brief Get the service data. - * @return The ServiceData of the advertised device. + * @param [in] index The vector index of the service data requested. + * @return The advertised service data or empty string if no data. */ -std::string NimBLEAdvertisedDevice::getServiceData() { - return m_serviceData; +std::string NimBLEAdvertisedDevice::getServiceData(uint8_t index) { + if(index > m_serviceDataVec.size()) { + NIMBLE_LOGW(LOG_TAG, "getServiceData: index out of range"); + return ""; + } + return m_serviceDataVec[index].second; } //getServiceData /** - * @brief Get the service data UUID. - * @return The service data UUID. + * @brief Get the service data. + * @param [in] uuid The uuid of the service data requested. + * @return The advertised service data or empty string if no data. */ +std::string NimBLEAdvertisedDevice::getServiceData(const NimBLEUUID &uuid) const { + for(auto &it : m_serviceDataVec) { + if(it.first == uuid) { + return it.second; + } + } + NIMBLE_LOGW(LOG_TAG, "getServiceData: uuid not found"); + return ""; +} //getServiceData -NimBLEUUID NimBLEAdvertisedDevice::getServiceDataUUID() { - return m_serviceDataUUID; + +/** + * @brief Get the advertised service UUID. + * @param [in] index The vector index of the service data UUID requested. + * @return The advertised service UUID or an empty UUID if not found. + */ +NimBLEUUID NimBLEAdvertisedDevice::getServiceDataUUID(uint8_t index) { + if(!haveServiceData() || index > m_serviceDataVec.size()) { + NIMBLE_LOGW(LOG_TAG, "getServiceDataUUID: index out of range"); + return NimBLEUUID(""); + } + return m_serviceDataVec[index].first; } // getServiceDataUUID /** - * @brief Get the Service UUID. - * @return The Service UUID of the advertised device. + * @brief Get the count of advertised service data UUIDS + * @return The number of service data UUIDS in the vector. */ +size_t NimBLEAdvertisedDevice::getServiceDataCount() { + return m_serviceDataVec.size(); +} // getServiceDataCount -NimBLEUUID NimBLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventually, is no longer useful - return m_serviceUUIDs[0]; + +/** + * @brief Get the Service UUID. + * @param [in] index The vector index of the service UUID requested. + * @return The Service UUID of the advertised service, or an empty UUID if not found. + */ +NimBLEUUID NimBLEAdvertisedDevice::getServiceUUID(uint8_t index) { + if(!haveServiceUUID() || index > m_serviceUUIDs.size()) { + NIMBLE_LOGW(LOG_TAG, "getServiceUUID: index out of range"); + return NimBLEUUID(""); + } + return m_serviceUUIDs[index]; } // getServiceUUID /** - * @brief Check advertised serviced for existence required UUID + * @brief Get the number of services advertised + * @return The count of services in the advertising packet. + */ +size_t NimBLEAdvertisedDevice::getServiceUUIDCount() { + return m_serviceUUIDs.size(); +} // getServiceUUIDCount + + +/** + * @brief Check advertised services for existance of the required UUID * @return Return true if service is advertised */ -bool NimBLEAdvertisedDevice::isAdvertisingService(const NimBLEUUID &uuid){ +bool NimBLEAdvertisedDevice::isAdvertisingService(const NimBLEUUID &uuid) const { for (int i = 0; i < m_serviceUUIDs.size(); i++) { - NIMBLE_LOGI(LOG_TAG, "Comparing UUIDS: %s %s", m_serviceUUIDs[i].toString().c_str(), uuid.toString().c_str()); if (m_serviceUUIDs[i].equals(uuid)) return true; } return false; -} +} // isAdvertisingService /** @@ -250,121 +294,126 @@ bool NimBLEAdvertisedDevice::haveTXPower() { * * https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile */ - void NimBLEAdvertisedDevice::parseAdvertisement(ble_hs_adv_fields *fields) { - //char s[BLE_HS_ADV_MAX_SZ]; - uint8_t *u8p; - uint8_t length; - int i; + void NimBLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, uint8_t length) { + struct ble_hs_adv_fields fields; + int rc = ble_hs_adv_parse_fields(&fields, payload, length); + if (rc != 0) { + NIMBLE_LOGE(LOG_TAG, "Gap Event Parse ERROR."); + return; + } - if (fields->uuids16 != NULL) { - for (i = 0; i < fields->num_uuids16; i++) { - setServiceUUID(NimBLEUUID(fields->uuids16[i].value)); + m_payload = payload; + m_payloadLength = length; + +#if CONFIG_LOG_DEFAULT_LEVEL > 3 || (ARDUINO_ARCH_ESP32 && CORE_DEBUG_LEVEL >= 4) + char* pHex = NimBLEUtils::buildHexData(nullptr, m_payload, m_payloadLength); + NIMBLE_LOGD(LOG_TAG,"payload: %s", pHex); + free(pHex); +#endif + + if (fields.uuids16 != NULL) { + for (int i = 0; i < fields.num_uuids16; i++) { + setServiceUUID(NimBLEUUID(fields.uuids16[i].value)); } } - if (fields->uuids32 != NULL) { - for (i = 0; i < fields->num_uuids32; i++) { - setServiceUUID(NimBLEUUID(fields->uuids32[i].value)); + if (fields.uuids32 != NULL) { + for (int i = 0; i < fields.num_uuids32; i++) { + setServiceUUID(NimBLEUUID(fields.uuids32[i].value)); } } - if (fields->uuids128 != NULL) { - for (i = 0; i < fields->num_uuids128; i++) { - setServiceUUID(NimBLEUUID(&fields->uuids128[i])); + if (fields.uuids128 != NULL) { + for (int i = 0; i < fields.num_uuids128; i++) { + setServiceUUID(NimBLEUUID(&fields.uuids128[i])); } } - if (fields->name != NULL) { - setName(std::string(reinterpret_cast(fields->name), fields->name_len)); + if (fields.name != NULL) { + setName(std::string(reinterpret_cast(fields.name), fields.name_len)); } - if (fields->tx_pwr_lvl_is_present) { - setTXPower(fields->tx_pwr_lvl); + if (fields.tx_pwr_lvl_is_present) { + setTXPower(fields.tx_pwr_lvl); } - if (fields->svc_data_uuid16 != NULL) { + if (fields.svc_data_uuid16 != NULL || + fields.svc_data_uuid32 != NULL || + fields.svc_data_uuid128 != NULL) + { + ble_hs_adv_field *field; + uint8_t *data = payload; + while(length > 1) { + field = (ble_hs_adv_field*)data; - u8p = fields->svc_data_uuid16; - length = fields->svc_data_uuid16_len; - - if (length < 2) { - NIMBLE_LOGE(LOG_TAG,"Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA"); - } - else{ - uint16_t uuid = *(uint16_t*)u8p; - setServiceDataUUID(NimBLEUUID(uuid)); - if (length > 2) { - setServiceData(std::string(reinterpret_cast(u8p + 2), length - 2)); + if(field->length > length) { + break; } + + if(field->type == BLE_HS_ADV_TYPE_SVC_DATA_UUID16) { + if(field->length > 2) { + uint16_t uuid; + memcpy(&uuid, field->value, 2); + setServiceData(NimBLEUUID(uuid), std::string(reinterpret_cast(field->value + 2), field->length - 3)); + } + } + + if(field->type == BLE_HS_ADV_TYPE_SVC_DATA_UUID32) { + if(field->length > 4) { + uint32_t uuid; + memcpy(&uuid, field->value, 4); + setServiceData(NimBLEUUID(uuid), std::string(reinterpret_cast(field->value + 4), field->length - 5)); + } + } + + if(field->type == BLE_HS_ADV_TYPE_SVC_DATA_UUID128) { + if(field->length > 16) { + NimBLEUUID uuid(field->value, (size_t)16, false); + setServiceData(uuid, std::string(reinterpret_cast(field->value + 16), field->length - 17)); + } + } + + length -= 1 + field->length; + data += 1 + field->length; } } - if (fields->svc_data_uuid32 != NULL) { - - u8p = fields->svc_data_uuid16; - length = fields->svc_data_uuid16_len; - - if (length < 4) { - NIMBLE_LOGE(LOG_TAG,"Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA"); - } - - uint32_t uuid = *(uint32_t*) u8p; - setServiceDataUUID(NimBLEUUID(uuid)); - if (length > 4) { - setServiceData(std::string(reinterpret_cast(u8p + 4), length - 4)); - } + if (fields.appearance_is_present) { + setAppearance(fields.appearance); } - if (fields->svc_data_uuid128 != NULL) { - - u8p = fields->svc_data_uuid16; - length = fields->svc_data_uuid16_len; - - if (length < 16) { - NIMBLE_LOGE(LOG_TAG,"Length too small for ESP_BLE_AD_TYPE_128SERVICE_DATA"); - } - - setServiceDataUUID(NimBLEUUID(u8p, (size_t)16, false)); - if (length > 16) { - setServiceData(std::string(reinterpret_cast(u8p + 16), length - 16)); - } + if (fields.mfg_data != NULL) { + setManufacturerData(std::string(reinterpret_cast(fields.mfg_data), fields.mfg_data_len)); } - if (fields->appearance_is_present) { - NIMBLE_LOGD(LOG_TAG, " appearance=0x%04x", fields->appearance); - setAppearance(fields->appearance); - } - -/**** TODO: create storage and fucntions for these parameters - if (fields->public_tgt_addr != NULL) { +/* TODO: create storage and fucntions for these parameters + if (fields.public_tgt_addr != NULL) { NIMBLE_LOGD(LOG_TAG, " public_tgt_addr="); - u8p = fields->public_tgt_addr; - for (i = 0; i < fields->num_public_tgt_addrs; i++) { + u8p = fields.public_tgt_addr; + for (i = 0; i < fields.num_public_tgt_addrs; i++) { NIMBLE_LOGD(LOG_TAG, "public_tgt_addr=%s ", addr_str(u8p)); u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN; } NIMBLE_LOGD(LOG_TAG, "\n"); } - if (fields->slave_itvl_range != NULL) { + if (fields.slave_itvl_range != NULL) { NIMBLE_LOGD(LOG_TAG, " slave_itvl_range="); - print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN); + print_bytes(fields.slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN); NIMBLE_LOGD(LOG_TAG, "\n"); } - if (fields->adv_itvl_is_present) { - NIMBLE_LOGD(LOG_TAG, " adv_itvl=0x%04x\n", fields->adv_itvl); + if (fields.adv_itvl_is_present) { + NIMBLE_LOGD(LOG_TAG, " adv_itvl=0x%04x\n", fields.adv_itvl); } - if (fields->uri != NULL) { + if (fields.uri != NULL) { NIMBLE_LOGD(LOG_TAG, " uri="); - print_bytes(fields->uri, fields->uri_len); + print_bytes(fields.uri, fields.uri_len); NIMBLE_LOGD(LOG_TAG, "\n"); } */ - if (fields->mfg_data != NULL) { - setManufacturerData(std::string(reinterpret_cast(fields->mfg_data), fields->mfg_data_len)); - } + } //parseAdvertisement @@ -393,7 +442,6 @@ void NimBLEAdvertisedDevice::setAdvType(uint8_t advType) { void NimBLEAdvertisedDevice::setAppearance(uint16_t appearance) { m_appearance = appearance; m_haveAppearance = true; - NIMBLE_LOGD(LOG_TAG,"- appearance: %d", m_appearance); } // setAppearance @@ -404,10 +452,6 @@ void NimBLEAdvertisedDevice::setAppearance(uint16_t appearance) { void NimBLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) { m_manufacturerData = manufacturerData; m_haveManufacturerData = true; - - char* pHex = NimBLEUtils::buildHexData(nullptr, (uint8_t*) m_manufacturerData.data(), (uint8_t) m_manufacturerData.length()); - NIMBLE_LOGD(LOG_TAG,"- manufacturer data: %s", pHex); - free(pHex); } // setManufacturerData @@ -418,7 +462,6 @@ void NimBLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) { void NimBLEAdvertisedDevice::setName(std::string name) { m_name = name; m_haveName = true; - NIMBLE_LOGD(LOG_TAG,"- setName(): name: %s", m_name.c_str()); } // setName @@ -429,19 +472,9 @@ void NimBLEAdvertisedDevice::setName(std::string name) { void NimBLEAdvertisedDevice::setRSSI(int rssi) { m_rssi = rssi; m_haveRSSI = true; - NIMBLE_LOGD(LOG_TAG,"- setRSSI(): rssi: %d", m_rssi); } // setRSSI -/** - * @brief Set the Scan that created this advertised device. - * @param pScan The Scan that created this advertised device. - */ -void NimBLEAdvertisedDevice::setScan(NimBLEScan* pScan) { - m_pScan = pScan; -} // setScan - - /** * @brief Set the Service UUID for this device. * @param [in] serviceUUID The discovered serviceUUID @@ -459,36 +492,32 @@ void NimBLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) { void NimBLEAdvertisedDevice::setServiceUUID(NimBLEUUID serviceUUID) { // Don't add duplicates for (int i = 0; i < m_serviceUUIDs.size(); i++) { - if (m_serviceUUIDs[i].equals(serviceUUID)) { + if (m_serviceUUIDs[i] == serviceUUID) { return; } } m_serviceUUIDs.push_back(serviceUUID); m_haveServiceUUID = true; - NIMBLE_LOGD(LOG_TAG,"- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str()); } // setServiceUUID /** * @brief Set the ServiceData value. - * @param [in] data ServiceData value. + * @param [in] uuid The UUID that the service data belongs to. + * @param [in] data The service data. */ -void NimBLEAdvertisedDevice::setServiceData(std::string serviceData) { - m_haveServiceData = true; // Set the flag that indicates we have service data. - m_serviceData = serviceData; // Save the service data that we received. +void NimBLEAdvertisedDevice::setServiceData(NimBLEUUID uuid, std::string data) { + m_haveServiceData = true; + for(auto &it : m_serviceDataVec) { + if(it.first == uuid) { + it.second = data; + return; + } + } + m_serviceDataVec.push_back({uuid, data}); } //setServiceData -/** - * @brief Set the ServiceDataUUID value. - * @param [in] data ServiceDataUUID value. - */ -void NimBLEAdvertisedDevice::setServiceDataUUID(NimBLEUUID uuid) { - m_haveServiceData = true; // Set the flag that indicates we have service data. - m_serviceDataUUID = uuid; -} // setServiceDataUUID - - /** * @brief Set the power level for this device. * @param [in] txPower The discovered power level. @@ -496,7 +525,6 @@ void NimBLEAdvertisedDevice::setServiceDataUUID(NimBLEUUID uuid) { void NimBLEAdvertisedDevice::setTXPower(int8_t txPower) { m_txPower = txPower; m_haveTXPower = true; - NIMBLE_LOGD(LOG_TAG,"- txPower: %d", m_txPower); } // setTXPower @@ -532,43 +560,60 @@ std::string NimBLEAdvertisedDevice::toString() { res += val; } - res += ", advType: " + std::string(NimBLEUtils::advTypeToString(m_advType)); + if(haveServiceData()) { + size_t count = getServiceDataCount(); + res += "\nService Data:"; + for(size_t i = 0; i < count; i++) { + res += "\nUUID: " + std::string(getServiceDataUUID(i)); + res += ", Data: " + getServiceData(i); + } + } return res; } // toString +/** + * @brief Get the payload advertised by the device. + * @return The advertisement payload. + */ uint8_t* NimBLEAdvertisedDevice::getPayload() { return m_payload; -} +} // getPayload +/** + * @brief Get the advertised device address type. + * @return The device address type: + * * BLE_ADDR_PUBLIC (0x00) + * * BLE_ADDR_RANDOM (0x01) + * * BLE_ADDR_PUBLIC_ID (0x02) + * * BLE_ADDR_RANDOM_ID (0x03) + */ uint8_t NimBLEAdvertisedDevice::getAddressType() { - return m_addressType; -} + return m_address.getType(); +} // getAddressType +/** + * @brief Get the timeStamp of when the device last advertised. + * @return The timeStamp of when the device was last seen. + */ time_t NimBLEAdvertisedDevice::getTimestamp() { return m_timestamp; -} - - -void NimBLEAdvertisedDevice::setAddressType(uint8_t type) { - m_addressType = type; -} +} // getTimestamp +/** + * @brief Get the length of the payload advertised by the device. + * @return The size of the payload in bytes. + */ size_t NimBLEAdvertisedDevice::getPayloadLength() { return m_payloadLength; -} +} // getPayloadLength -void NimBLEAdvertisedDevice::setAdvertisementResult(uint8_t* payload, uint8_t length){ - m_payload = payload; - m_payloadLength = length; -} - #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) #endif /* CONFIG_BT_ENABLED */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.h b/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.h index c38d7001d..ebdb85c9e 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEAdvertisedDevice.h @@ -46,8 +46,16 @@ public: uint16_t getAppearance(); std::string getManufacturerData(); + /** + * @brief A template to convert the service data to . + * @tparam T The type to convert the data to. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: getManufacturerData(skipSizeCheck); + */ template - T getManufacturerData(bool skipSizeCheck = false) { + T getManufacturerData(bool skipSizeCheck = false) { std::string data = getManufacturerData(); if(!skipSizeCheck && data.size() < sizeof(T)) return T(); const char *pData = data.data(); @@ -57,51 +65,73 @@ public: std::string getName(); int getRSSI(); NimBLEScan* getScan(); - std::string getServiceData(); + size_t getServiceDataCount(); + std::string getServiceData(uint8_t index = 0); + std::string getServiceData(const NimBLEUUID &uuid) const; + /** + * @brief A template to convert the service data to . + * @tparam T The type to convert the data to. + * @param [in] index The vector index of the service data requested. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: getServiceData(skipSizeCheck); + */ template - T getServiceData(bool skipSizeCheck = false) { - std::string data = getServiceData(); + T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) { + std::string data = getServiceData(index); if(!skipSizeCheck && data.size() < sizeof(T)) return T(); const char *pData = data.data(); return *((T *)pData); } - NimBLEUUID getServiceDataUUID(); - NimBLEUUID getServiceUUID(); + /** + * @brief A template to convert the service data to . + * @tparam T The type to convert the data to. + * @param [in] uuid The uuid of the service data requested. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: getServiceData(skipSizeCheck); + */ + template + T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck = false) { + std::string data = getServiceData(uuid); + if(!skipSizeCheck && data.size() < sizeof(T)) return T(); + const char *pData = data.data(); + return *((T *)pData); + } + + NimBLEUUID getServiceDataUUID(uint8_t index = 0); + NimBLEUUID getServiceUUID(uint8_t index = 0); + size_t getServiceUUIDCount(); int8_t getTXPower(); uint8_t* getPayload(); size_t getPayloadLength(); uint8_t getAddressType(); time_t getTimestamp(); - void setAddressType(uint8_t type); - - - bool isAdvertisingService(const NimBLEUUID &uuid); - bool haveAppearance(); - bool haveManufacturerData(); - bool haveName(); - bool haveRSSI(); - bool haveServiceData(); - bool haveServiceUUID(); - bool haveTXPower(); - - std::string toString(); + bool isAdvertisingService(const NimBLEUUID &uuid) const; + bool haveAppearance(); + bool haveManufacturerData(); + bool haveName(); + bool haveRSSI(); + bool haveServiceData(); + bool haveServiceUUID(); + bool haveTXPower(); + std::string toString(); private: friend class NimBLEScan; - void parseAdvertisement(ble_hs_adv_fields *fields); + void parseAdvertisement(uint8_t* payload, uint8_t length); void setAddress(NimBLEAddress address); void setAdvType(uint8_t advType); - void setAdvertisementResult(uint8_t* payload, uint8_t length); void setAppearance(uint16_t appearance); void setManufacturerData(std::string manufacturerData); void setName(std::string name); void setRSSI(int rssi); - void setScan(NimBLEScan* pScan); - void setServiceData(std::string data); - void setServiceDataUUID(NimBLEUUID uuid); + void setServiceData(NimBLEUUID serviceUUID, std::string data); void setServiceUUID(const char* serviceUUID); void setServiceUUID(NimBLEUUID serviceUUID); void setTXPower(int8_t txPower); @@ -118,20 +148,17 @@ private: NimBLEAddress m_address = NimBLEAddress(""); uint8_t m_advType; uint16_t m_appearance; - int m_deviceType; std::string m_manufacturerData; std::string m_name; - NimBLEScan* m_pScan; int m_rssi; - std::vector m_serviceUUIDs; int8_t m_txPower; - std::string m_serviceData; - NimBLEUUID m_serviceDataUUID; uint8_t* m_payload; size_t m_payloadLength; - uint8_t m_addressType; time_t m_timestamp; bool m_callbackSent; + + std::vector m_serviceUUIDs; + std::vector>m_serviceDataVec; }; /** @@ -150,7 +177,6 @@ public: * As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the * device that was found. During any individual scan, a device will only be detected one time. */ - //virtual void onResult(NimBLEAdvertisedDevice advertisedDevice) = 0; virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0; }; diff --git a/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.cpp b/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.cpp index 7e1e07ae3..36bdbf9e9 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.cpp @@ -31,7 +31,6 @@ static const char* LOG_TAG = "NimBLEAdvertising"; /** * @brief Construct a default advertising object. - * */ NimBLEAdvertising::NimBLEAdvertising() { memset(&m_advData, 0, sizeof m_advData); @@ -55,6 +54,11 @@ NimBLEAdvertising::NimBLEAdvertising() { m_advParams.itvl_min = 0; m_advParams.itvl_max = 0; + m_customAdvData = false; + m_customScanResponseData = false; + m_scanResp = true; + m_advDataSet = false; + } // NimBLEAdvertising @@ -64,6 +68,7 @@ NimBLEAdvertising::NimBLEAdvertising() { */ void NimBLEAdvertising::addServiceUUID(const NimBLEUUID &serviceUUID) { m_serviceUUIDs.push_back(serviceUUID); + m_advDataSet = false; } // addServiceUUID @@ -76,45 +81,74 @@ void NimBLEAdvertising::addServiceUUID(const char* serviceUUID) { } // addServiceUUID +/** + * @brief Add a service uuid to exposed list of services. + * @param [in] serviceUUID The UUID of the service to expose. + */ +void NimBLEAdvertising::removeServiceUUID(const NimBLEUUID &serviceUUID) { + //m_serviceUUIDs.erase(std::remove_if(m_serviceUUIDs.begin(), m_serviceUUIDs.end(),[serviceUUID](const NimBLEUUID &s) {return serviceUUID == s;}), m_serviceUUIDs.end()); + for(auto it = m_serviceUUIDs.begin(); it != m_serviceUUIDs.end(); ++it) { + if((*it) == serviceUUID) { + m_serviceUUIDs.erase(it); + break; + } + } + m_advDataSet = false; +} // addServiceUUID + + /** * @brief Set the device appearance in the advertising data. - * The appearance attribute is of type 0x19. The codes for distinct appearances can be found here: + * The codes for distinct appearances can be found here:\n * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml. * @param [in] appearance The appearance of the device in the advertising data. - * @return N/A. */ void NimBLEAdvertising::setAppearance(uint16_t appearance) { m_advData.appearance = appearance; m_advData.appearance_is_present = 1; } // setAppearance + +/** + * @brief Set the type of advertisment to use. + * @param [in] adv_type: + * * BLE_HCI_ADV_TYPE_ADV_IND (0) - indirect advertising + * * BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD (1) - direct advertisng - high duty cycle + * * BLE_HCI_ADV_TYPE_ADV_SCAN_IND (2) - indirect scan response + * * BLE_HCI_ADV_TYPE_ADV_NONCONN_IND (3) - indirect advertisng - not connectable + * * BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD (4) - direct advertising - low duty cycle + */ void NimBLEAdvertising::setAdvertisementType(uint8_t adv_type){ m_advParams.conn_mode = adv_type; } // setAdvertisementType + +/** + * @brief Set the minimum advertising interval. + * @param [in] mininterval Minimum value for advertising interval in 0.625ms units, 0 = use default. + */ void NimBLEAdvertising::setMinInterval(uint16_t mininterval) { m_advParams.itvl_min = mininterval; } // setMinInterval + +/** + * @brief Set the maximum advertising interval. + * @param [in] maxinterval Maximum value for advertising interval in 0.625ms units, 0 = use default. + */ void NimBLEAdvertising::setMaxInterval(uint16_t maxinterval) { m_advParams.itvl_max = maxinterval; } // setMaxInterval -/* These are dummy functions for now for compatibility */ -void NimBLEAdvertising::setMinPreferred(uint16_t mininterval) { - //m_advData.min_interval = mininterval; -} // - -void NimBLEAdvertising::setMaxPreferred(uint16_t maxinterval) { - //m_advData.max_interval = maxinterval; -} // -/*******************************************************/ - - +/** + * @brief Set if scan response is available. + * @param [in] set true = scan response available. + */ void NimBLEAdvertising::setScanResponse(bool set) { m_scanResp = set; -} +} // setScanResponse + /** * @brief Set the filtering for the scan filter. @@ -145,9 +179,13 @@ void NimBLEAdvertising::setScanFilter(bool scanRequestWhitelistOnly, bool connec } } // setScanFilter + /** * @brief Set the advertisement data that is to be published in a regular advertisement. * @param [in] advertisementData The data to be advertised. + * @details The use of this function will replace any data set with addServiceUUID\n + * or setAppearance. If you wish for these to be advertised you must include them\n + * in the advertisementData parameter sent. */ void NimBLEAdvertising::setAdvertisementData(NimBLEAdvertisementData& advertisementData) { @@ -166,6 +204,8 @@ void NimBLEAdvertising::setAdvertisementData(NimBLEAdvertisementData& advertisem /** * @brief Set the advertisement data that is to be published in a scan response. * @param [in] advertisementData The data to be advertised. + * @details Calling this without also using setAdvertisementData will have no effect.\n + * When using custom scan response data you must also use custom advertisement data. */ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertisementData) { NIMBLE_LOGD(LOG_TAG, ">> setScanResponseData"); @@ -182,10 +222,10 @@ void NimBLEAdvertising::setScanResponseData(NimBLEAdvertisementData& advertiseme /** * @brief Start advertising. - * Start advertising. - * @return N/A. + * @param [in] duration The duration, in seconds, to advertise, 0 == advertise forever. + * @param [in] advCompleteCB A pointer to a callback to be invoked when advertising ends. */ -void NimBLEAdvertising::start() { +void NimBLEAdvertising::start(uint32_t duration, void (*advCompleteCB)(NimBLEAdvertising *pAdv)) { NIMBLE_LOGD(LOG_TAG, ">> Advertising start: customAdvData: %d, customScanResponseData: %d", m_customAdvData, m_customScanResponseData); // If Host is not synced we cannot start advertising. @@ -211,6 +251,15 @@ void NimBLEAdvertising::start() { return; } + if(duration == 0){ + duration = BLE_HS_FOREVER; + } + else{ + duration = duration*1000; // convert duration to milliseconds + } + + m_advCompCB = advCompleteCB; + int rc = 0; if (!m_customAdvData && !m_advDataSet) { @@ -356,13 +405,13 @@ void NimBLEAdvertising::start() { } #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) - rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER, + rc = ble_gap_adv_start(0, NULL, duration, &m_advParams, - (pServer != nullptr) ? NimBLEServer::handleGapEvent : NULL, - pServer); + (pServer != nullptr) ? NimBLEServer::handleGapEvent : NimBLEAdvertising::handleGapEvent, + (pServer != nullptr) ? (void*)pServer : (void*)this); #else - rc = ble_gap_adv_start(0, NULL, BLE_HS_FOREVER, - &m_advParams, NULL,NULL); + rc = ble_gap_adv_start(0, NULL, duration, + &m_advParams, NimBLEAdvertising::handleGapEvent, this); #endif if (rc != 0) { NIMBLE_LOGC(LOG_TAG, "Error enabling advertising; rc=%d, %s", rc, NimBLEUtils::returnCodeToString(rc)); @@ -375,8 +424,6 @@ void NimBLEAdvertising::start() { /** * @brief Stop advertising. - * Stop advertising. - * @return N/A. */ void NimBLEAdvertising::stop() { NIMBLE_LOGD(LOG_TAG, ">> stop"); @@ -391,6 +438,25 @@ void NimBLEAdvertising::stop() { /** + * @brief Handles the callback when advertising stops. + */ +void NimBLEAdvertising::advCompleteCB() { + if(m_advCompCB != nullptr) { + m_advCompCB(this); + } +} + + +/** + * @brief Check if currently advertising. + * @return true if advertising is active. + */ +bool NimBLEAdvertising::isAdvertising() { + return ble_gap_adv_active(); +} + + +/* * Host reset seems to clear advertising data, * we need clear the flag so it reloads it. */ @@ -399,6 +465,22 @@ void NimBLEAdvertising::onHostReset() { } +/** + * @brief Handler for gap events when not using peripheral role. + * @param [in] event the event data. + * @param [in] arg pointer to the advertising instance. + */ +/*STATIC*/ +int NimBLEAdvertising::handleGapEvent(struct ble_gap_event *event, void *arg) { + NimBLEAdvertising *pAdv = (NimBLEAdvertising*)arg; + + if(event->type == BLE_GAP_EVENT_ADV_COMPLETE) { + pAdv->advCompleteCB(); + } + return 0; +} + + /** * @brief Add data to the payload to be advertised. * @param [in] data The data to be added to the payload. @@ -411,6 +493,19 @@ void NimBLEAdvertisementData::addData(const std::string &data) { } // addData +/** + * @brief Add data to the payload to be advertised. + * @param [in] data The data to be added to the payload. + * @param [in] length The size of data to be added to the payload. + */ +void NimBLEAdvertisementData::addData(char * data, size_t length){ + if ((m_payload.length() + length) > BLE_HS_ADV_MAX_SZ) { + return; + } + m_payload.append(data,length); +} // addData + + /** * @brief Set the appearance. * @param [in] appearance The appearance code value. @@ -427,8 +522,8 @@ void NimBLEAdvertisementData::setAppearance(uint16_t appearance) { /** - * @brief Set the complete services. - * @param [in] uuid The single service to advertise. + * @brief Set the complete services to advertise. + * @param [in] uuid The UUID of the service. */ void NimBLEAdvertisementData::setCompleteServices(const NimBLEUUID &uuid) { char cdata[2]; @@ -465,16 +560,7 @@ void NimBLEAdvertisementData::setCompleteServices(const NimBLEUUID &uuid) { /** * @brief Set the advertisement flags. - * @param [in] The flags to be set in the advertisement. - * * ****DO NOT USE THESE**** - * * ESP_BLE_ADV_FLAG_LIMIT_DISC - * * ESP_BLE_ADV_FLAG_GEN_DISC - * * ESP_BLE_ADV_FLAG_BREDR_NOT_SPT - * * ESP_BLE_ADV_FLAG_DMT_CONTROLLER_SPT - * * ESP_BLE_ADV_FLAG_DMT_HOST_SPT - * * ESP_BLE_ADV_FLAG_NON_LIMIT_DISC - * * - * * ****THESE ARE SUPPORTED**** + * @param [in] flag The flags to be set in the advertisement. * * BLE_HS_ADV_F_DISC_LTD * * BLE_HS_ADV_F_DISC_GEN * * BLE_HS_ADV_F_BREDR_UNSUP - must always use with NimBLE @@ -490,7 +576,7 @@ void NimBLEAdvertisementData::setFlags(uint8_t flag) { /** * @brief Set manufacturer specific data. - * @param [in] data Manufacturer data. + * @param [in] data The manufacturer data to advertise. */ void NimBLEAdvertisementData::setManufacturerData(const std::string &data) { NIMBLE_LOGD("NimBLEAdvertisementData", ">> setManufacturerData"); @@ -503,8 +589,8 @@ void NimBLEAdvertisementData::setManufacturerData(const std::string &data) { /** - * @brief Set the name. - * @param [in] The complete name of the device. + * @brief Set the complete name of this device. + * @param [in] name The name to advertise. */ void NimBLEAdvertisementData::setName(const std::string &name) { NIMBLE_LOGD("NimBLEAdvertisementData", ">> setName: %s", name.c_str()); @@ -517,7 +603,7 @@ void NimBLEAdvertisementData::setName(const std::string &name) { /** - * @brief Set the partial services. + * @brief Set the partial services to advertise. * @param [in] uuid The single service to advertise. */ void NimBLEAdvertisementData::setPartialServices(const NimBLEUUID &uuid) { @@ -555,8 +641,8 @@ void NimBLEAdvertisementData::setPartialServices(const NimBLEUUID &uuid) { /** * @brief Set the service data (UUID + data) - * @param [in] uuid The UUID to set with the service data. Size of UUID will be used. - * @param [in] data The data to be associated with the service data advert. + * @param [in] uuid The UUID to set with the service data. + * @param [in] data The data to be associated with the service data advertised. */ void NimBLEAdvertisementData::setServiceData(const NimBLEUUID &uuid, const std::string &data) { char cdata[2]; @@ -593,7 +679,7 @@ void NimBLEAdvertisementData::setServiceData(const NimBLEUUID &uuid, const std:: /** * @brief Set the short name. - * @param [in] The short name of the device. + * @param [in] name The short name of the device. */ void NimBLEAdvertisementData::setShortName(const std::string &name) { NIMBLE_LOGD("NimBLEAdvertisementData", ">> setShortName: %s", name.c_str()); diff --git a/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.h b/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.h index ad76d7eec..2fab71004 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEAdvertising.h @@ -57,6 +57,7 @@ public: void setServiceData(const NimBLEUUID &uuid, const std::string &data); void setShortName(const std::string &name); void addData(const std::string &data); // Add data to the payload. + void addData(char * data, size_t length); std::string getPayload(); // Retrieve the current advert payload. private: @@ -75,34 +76,35 @@ public: NimBLEAdvertising(); void addServiceUUID(const NimBLEUUID &serviceUUID); void addServiceUUID(const char* serviceUUID); - void start(); + void removeServiceUUID(const NimBLEUUID &serviceUUID); + void start(uint32_t duration = 0, void (*advCompleteCB)(NimBLEAdvertising *pAdv) = nullptr); void stop(); void setAppearance(uint16_t appearance); void setAdvertisementType(uint8_t adv_type); void setMaxInterval(uint16_t maxinterval); void setMinInterval(uint16_t mininterval); void setAdvertisementData(NimBLEAdvertisementData& advertisementData); - void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly); + void setScanFilter(bool scanRequestWhitelistOnly, bool connectWhitelistOnly); void setScanResponseData(NimBLEAdvertisementData& advertisementData); - void setPrivateAddress(uint8_t type = BLE_ADDR_RANDOM); - - void setMinPreferred(uint16_t); - void setMaxPreferred(uint16_t); void setScanResponse(bool); + void advCompleteCB(); + bool isAdvertising(); private: friend class NimBLEDevice; - void onHostReset(); + void onHostReset(); + static int handleGapEvent(struct ble_gap_event *event, void *arg); - ble_hs_adv_fields m_advData; - ble_hs_adv_fields m_scanData; - ble_gap_adv_params m_advParams; + ble_hs_adv_fields m_advData; + ble_hs_adv_fields m_scanData; + ble_gap_adv_params m_advParams; std::vector m_serviceUUIDs; - bool m_customAdvData = false; // Are we using custom advertising data? - bool m_customScanResponseData = false; // Are we using custom scan response data? - bool m_scanResp = true; - bool m_advDataSet = false; + bool m_customAdvData; + bool m_customScanResponseData; + bool m_scanResp; + bool m_advDataSet; + void (*m_advCompCB)(NimBLEAdvertising *pAdv); }; diff --git a/libesp32/NimBLE-Arduino/src/NimBLEBeacon.cpp b/libesp32/NimBLE-Arduino/src/NimBLEBeacon.cpp index 718a507f5..8c4574bc2 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEBeacon.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEBeacon.cpp @@ -15,6 +15,7 @@ #if defined(CONFIG_BT_ENABLED) #include +#include #include "NimBLEBeacon.h" #include "NimBLELog.h" @@ -22,6 +23,10 @@ static const char* LOG_TAG = "NimBLEBeacon"; + +/** + * @brief Construct a default beacon object. + */ NimBLEBeacon::NimBLEBeacon() { m_beaconData.manufacturerId = 0x4c00; m_beaconData.subType = 0x02; @@ -32,32 +37,64 @@ NimBLEBeacon::NimBLEBeacon() { memset(m_beaconData.proximityUUID, 0, sizeof(m_beaconData.proximityUUID)); } // NimBLEBeacon + +/** + * @brief Retrieve the data that is being advertised. + * @return The advertised data. + */ std::string NimBLEBeacon::getData() { return std::string((char*) &m_beaconData, sizeof(m_beaconData)); } // getData + +/** + * @brief Get the major value being advertised. + * @return The major value advertised. + */ uint16_t NimBLEBeacon::getMajor() { return m_beaconData.major; } + +/** + * @brief Get the manufacturer ID being advertised. + * @return The manufacturer ID value advertised. + */ uint16_t NimBLEBeacon::getManufacturerId() { return m_beaconData.manufacturerId; } + +/** + * @brief Get the minor value being advertised. + * @return minor value advertised. + */ uint16_t NimBLEBeacon::getMinor() { return m_beaconData.minor; } + +/** + * @brief Get the proximity UUID being advertised. + * @return The UUID advertised. + */ NimBLEUUID NimBLEBeacon::getProximityUUID() { - return NimBLEUUID(m_beaconData.proximityUUID, 16, false); + return NimBLEUUID(m_beaconData.proximityUUID, 16, true); } + +/** + * @brief Get the signal power being advertised. + * @return signal power level advertised. + */ int8_t NimBLEBeacon::getSignalPower() { return m_beaconData.signalPower; } + /** - * Set the raw data for the beacon record. + * @brief Set the raw data for the beacon record. + * @param [in] data The raw beacon data. */ void NimBLEBeacon::setData(const std::string &data) { if (data.length() != sizeof(m_beaconData)) { @@ -68,24 +105,51 @@ void NimBLEBeacon::setData(const std::string &data) { memcpy(&m_beaconData, data.data(), sizeof(m_beaconData)); } // setData + +/** + * @brief Set the major value. + * @param [in] major The major value. + */ void NimBLEBeacon::setMajor(uint16_t major) { m_beaconData.major = ENDIAN_CHANGE_U16(major); } // setMajor + +/** + * @brief Set the manufacturer ID. + * @param [in] manufacturerId The manufacturer ID value. + */ void NimBLEBeacon::setManufacturerId(uint16_t manufacturerId) { m_beaconData.manufacturerId = ENDIAN_CHANGE_U16(manufacturerId); } // setManufacturerId + +/** + * @brief Set the minor value. + * @param [in] minor The minor value. + */ void NimBLEBeacon::setMinor(uint16_t minor) { m_beaconData.minor = ENDIAN_CHANGE_U16(minor); } // setMinior + +/** + * @brief Set the proximity UUID. + * @param [in] uuid The proximity UUID. + */ void NimBLEBeacon::setProximityUUID(const NimBLEUUID &uuid) { NimBLEUUID temp_uuid = uuid; temp_uuid.to128(); - memcpy(m_beaconData.proximityUUID, temp_uuid.getNative()->u128.value, 16); + std::reverse_copy(temp_uuid.getNative()->u128.value, + temp_uuid.getNative()->u128.value + 16, + m_beaconData.proximityUUID); } // setProximityUUID + +/** + * @brief Set the signal power. + * @param [in] signalPower The signal power value. + */ void NimBLEBeacon::setSignalPower(int8_t signalPower) { m_beaconData.signalPower = signalPower; } // setSignalPower diff --git a/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.cpp b/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.cpp index 0bd11f6ba..6f7d3d7e0 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.cpp @@ -16,16 +16,18 @@ #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #include "NimBLECharacteristic.h" -#include "NimBLE2902.h" #include "NimBLE2904.h" #include "NimBLEDevice.h" #include "NimBLELog.h" #define NULL_HANDLE (0xffff) +#define NIMBLE_SUB_NOTIFY 0x0001 +#define NIMBLE_SUB_INDICATE 0x0002 static NimBLECharacteristicCallbacks defaultCallback; static const char* LOG_TAG = "NimBLECharacteristic"; + /** * @brief Construct a characteristic * @param [in] uuid - UUID (const char*) for the characteristic. @@ -58,6 +60,9 @@ NimBLECharacteristic::NimBLECharacteristic(const NimBLEUUID &uuid, uint16_t prop * @brief Destructor. */ NimBLECharacteristic::~NimBLECharacteristic() { + for(auto &it : m_dscVec) { + delete it; + } } // ~NimBLECharacteristic @@ -65,6 +70,7 @@ NimBLECharacteristic::~NimBLECharacteristic() { * @brief Create a new BLE Descriptor associated with this characteristic. * @param [in] uuid - The UUID of the descriptor. * @param [in] properties - The properties of the descriptor. + * @param [in] max_len - The max length in bytes of the descriptor value. * @return The new BLE descriptor. */ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const char* uuid, uint32_t properties, uint16_t max_len) { @@ -76,25 +82,15 @@ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const char* uuid, uint3 * @brief Create a new BLE Descriptor associated with this characteristic. * @param [in] uuid - The UUID of the descriptor. * @param [in] properties - The properties of the descriptor. + * @param [in] max_len - The max length in bytes of the descriptor value. * @return The new BLE descriptor. */ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const NimBLEUUID &uuid, uint32_t properties, uint16_t max_len) { NimBLEDescriptor* pDescriptor = nullptr; if(uuid == NimBLEUUID(uint16_t(0x2902))) { - if(!(m_properties & BLE_GATT_CHR_F_NOTIFY) && !(m_properties & BLE_GATT_CHR_F_INDICATE)) { - assert(0 && "Cannot create 2902 descriptior without characteristic notification or indication property set"); - } - // We cannot have more than one 2902 descriptor, if it's already been created just return a pointer to it. - pDescriptor = getDescriptorByUUID(uuid); - if(pDescriptor == nullptr) { - pDescriptor = new NimBLE2902(this); - } else { - return pDescriptor; - } - + assert(0 && "0x2902 descriptors cannot be manually created"); } else if (uuid == NimBLEUUID(uint16_t(0x2904))) { pDescriptor = new NimBLE2904(this); - } else { pDescriptor = new NimBLEDescriptor(uuid, properties, max_len, this); } @@ -106,8 +102,8 @@ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const NimBLEUUID &uuid, /** * @brief Return the BLE Descriptor for the given UUID if associated with this characteristic. - * @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve. - * @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned. + * @param [in] uuid The UUID of the descriptor that we wish to retrieve. + * @return pointer to the NimBLEDescriptor. If no such descriptor is associated with the characteristic, nullptr is returned. */ NimBLEDescriptor* NimBLECharacteristic::getDescriptorByUUID(const char* uuid) { return getDescriptorByUUID(NimBLEUUID(uuid)); @@ -116,8 +112,8 @@ NimBLEDescriptor* NimBLECharacteristic::getDescriptorByUUID(const char* uuid) { /** * @brief Return the BLE Descriptor for the given UUID if associated with this characteristic. - * @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve. - * @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned. + * @param [in] uuid The UUID of the descriptor that we wish to retrieve. + * @return pointer to the NimBLEDescriptor. If no such descriptor is associated with the characteristic, nullptr is returned. */ NimBLEDescriptor* NimBLECharacteristic::getDescriptorByUUID(const NimBLEUUID &uuid) { for (auto &it : m_dscVec) { @@ -138,6 +134,10 @@ uint16_t NimBLECharacteristic::getHandle() { } // getHandle +/** + * @brief Get the properties of the characteristic. + * @return The properties of the characteristic. + */ uint16_t NimBLECharacteristic::getProperties() { return m_properties; } // getProperties @@ -162,7 +162,7 @@ NimBLEUUID NimBLECharacteristic::getUUID() { /** * @brief Retrieve the current value of the characteristic. - * @return A pointer to storage containing the current characteristic value. + * @return A std::string containing the current characteristic value. */ std::string NimBLECharacteristic::getValue(time_t *timestamp) { portENTER_CRITICAL(&m_valMux); @@ -189,12 +189,16 @@ size_t NimBLECharacteristic::getDataLength() { } +/** + * @brief STATIC callback to handle events from the NimBLE stack. + */ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { const ble_uuid_t *uuid; int rc; + struct ble_gap_conn_desc desc; NimBLECharacteristic* pCharacteristic = (NimBLECharacteristic*)arg; NIMBLE_LOGD(LOG_TAG, "Characteristic %s %s event", pCharacteristic->getUUID().toString().c_str(), @@ -207,7 +211,10 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han // If the packet header is only 8 bytes this is a follow up of a long read // so we don't want to call the onRead() callback again. if(ctxt->om->om_pkthdr_len > 8) { + rc = ble_gap_conn_find(conn_handle, &desc); + assert(rc == 0); pCharacteristic->m_pCallbacks->onRead(pCharacteristic); + pCharacteristic->m_pCallbacks->onRead(pCharacteristic, &desc); } portENTER_CRITICAL(&pCharacteristic->m_valMux); @@ -233,14 +240,15 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han if((len + next->om_len) > BLE_ATT_ATTR_MAX_LEN) { return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; } - memcpy(&buf[len-1], next->om_data, next->om_len); + memcpy(&buf[len], next->om_data, next->om_len); len += next->om_len; next = SLIST_NEXT(next, om_next); } - + rc = ble_gap_conn_find(conn_handle, &desc); + assert(rc == 0); pCharacteristic->setValue(buf, len); pCharacteristic->m_pCallbacks->onWrite(pCharacteristic); - + pCharacteristic->m_pCallbacks->onWrite(pCharacteristic, &desc); return 0; } default: @@ -253,70 +261,70 @@ int NimBLECharacteristic::handleGapEvent(uint16_t conn_handle, uint16_t attr_han /** - * @brief Set the subscribe status for this characteristic. - * This will maintain a map of subscribed clients and their indicate/notify status. - * @return N/A + * @brief Get the number of clients subscribed to the characteristic. + * @returns Number of clients subscribed to notifications / indications. + */ +size_t NimBLECharacteristic::getSubscribedCount() { + return m_subscribedVec.size(); +} + + +/** + * @brief Set the subscribe status for this characteristic.\n + * This will maintain a vector of subscribed clients and their indicate/notify status. */ void NimBLECharacteristic::setSubscribe(struct ble_gap_event *event) { - uint16_t subVal = 0; - if(event->subscribe.cur_notify) { - subVal |= NIMBLE_DESC_FLAG_NOTIFY; - } - if(event->subscribe.cur_indicate) { - subVal |= NIMBLE_DESC_FLAG_INDICATE; - } - - if(m_pTaskData != nullptr) { - m_pTaskData->rc = (subVal & NIMBLE_DESC_FLAG_INDICATE) ? 0 : - NimBLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED; - xTaskNotifyGive(m_pTaskData->task); - } - - NIMBLE_LOGI(LOG_TAG, "New subscribe value for conn: %d val: %d", - event->subscribe.conn_handle, subVal); - - NimBLE2902* p2902 = (NimBLE2902*)getDescriptorByUUID(uint16_t(0x2902)); - if(p2902 == nullptr){ - ESP_LOGE(LOG_TAG, "No 2902 descriptor found for %s", - std::string(getUUID()).c_str()); + ble_gap_conn_desc desc; + if(ble_gap_conn_find(event->subscribe.conn_handle, &desc) != 0) { return; } - p2902->setNotifications(subVal & NIMBLE_DESC_FLAG_NOTIFY); - p2902->setIndications(subVal & NIMBLE_DESC_FLAG_INDICATE); - p2902->m_pCallbacks->onWrite(p2902); + uint16_t subVal = 0; + if(event->subscribe.cur_notify > 0 && (m_properties & NIMBLE_PROPERTY::NOTIFY)) { + subVal |= NIMBLE_SUB_NOTIFY; + } + if(event->subscribe.cur_indicate && (m_properties & NIMBLE_PROPERTY::INDICATE)) { + subVal |= NIMBLE_SUB_INDICATE; + } + if(m_pTaskData != nullptr) { + m_pTaskData->rc = (subVal & NIMBLE_SUB_INDICATE) ? 0 : + NimBLECharacteristicCallbacks::Status::ERROR_INDICATE_DISABLED; + xTaskNotifyGive(m_pTaskData->task); + } + + NIMBLE_LOGI(LOG_TAG, "New subscribe value for conn: %d val: %d", + event->subscribe.conn_handle, subVal); - auto it = p2902->m_subscribedVec.begin(); - for(;it != p2902->m_subscribedVec.end(); ++it) { - if((*it).conn_id == event->subscribe.conn_handle) { + m_pCallbacks->onSubscribe(this, &desc, subVal); + + auto it = m_subscribedVec.begin(); + for(;it != m_subscribedVec.end(); ++it) { + if((*it).first == event->subscribe.conn_handle) { break; } } if(subVal > 0) { - if(it == p2902->m_subscribedVec.end()) { - chr_sub_status_t client_sub; - client_sub.conn_id = event->subscribe.conn_handle; - client_sub.sub_val = subVal; - p2902->m_subscribedVec.push_back(client_sub); + if(it == m_subscribedVec.end()) { + m_subscribedVec.push_back({event->subscribe.conn_handle, subVal}); return; } - (*it).sub_val = subVal; + (*it).second = subVal; - } else if(it != p2902->m_subscribedVec.end()) { - p2902->m_subscribedVec.erase(it); - p2902->m_subscribedVec.shrink_to_fit(); + } else if(it != m_subscribedVec.end()) { + m_subscribedVec.erase(it); + m_subscribedVec.shrink_to_fit(); } + } /** - * @brief Send an indication. - * An indication is a transmission of up to the first 20 bytes of the characteristic value. An indication - * will block waiting a positive confirmation from the client. - * @return N/A + * @brief Send an indication.\n + * An indication is a transmission of up to the first 20 bytes of the characteristic value.\n + * An indication will block waiting for a positive confirmation from the client. */ void NimBLECharacteristic::indicate() { NIMBLE_LOGD(LOG_TAG, ">> indicate: length: %d", getDataLength()); @@ -325,23 +333,24 @@ void NimBLECharacteristic::indicate() { } // indicate /** - * @brief Send a notify. - * A notification is a transmission of up to the first 20 bytes of the characteristic value. An notification - * will not block; it is a fire and forget. - * @return N/A. + * @brief Send a notification.\n + * A notification is a transmission of up to the first 20 bytes of the characteristic value.\n + * A notification will not block; it is a fire and forget. + * @param[in] is_notification if true sends a notification, false sends an indication. */ void NimBLECharacteristic::notify(bool is_notification) { NIMBLE_LOGD(LOG_TAG, ">> notify: length: %d", getDataLength()); - NimBLE2902* p2902 = (NimBLE2902*)getDescriptorByUUID(uint16_t(0x2902)); - if(p2902 == nullptr) { + if(!(m_properties & NIMBLE_PROPERTY::NOTIFY) && + !(m_properties & NIMBLE_PROPERTY::INDICATE)) + { NIMBLE_LOGE(LOG_TAG, "<< notify-Error; Notify/indicate not enabled for characterisitc: %s", std::string(getUUID()).c_str()); } - if (p2902->m_subscribedVec.size() == 0) { + if (m_subscribedVec.size() == 0) { NIMBLE_LOGD(LOG_TAG, "<< notify: No clients subscribed."); return; } @@ -355,18 +364,18 @@ void NimBLECharacteristic::notify(bool is_notification) { (m_properties & BLE_GATT_CHR_F_READ_ENC); int rc = 0; - for (auto &it : p2902->m_subscribedVec) { - uint16_t _mtu = getService()->getServer()->getPeerMTU(it.conn_id); + for (auto &it : m_subscribedVec) { + uint16_t _mtu = getService()->getServer()->getPeerMTU(it.first); // check if connected and subscribed - if(_mtu == 0 || it.sub_val == 0) { + if(_mtu == 0 || it.second == 0) { continue; } // check if security requirements are satisfied if(reqSec) { struct ble_gap_conn_desc desc; - rc = ble_gap_conn_find(it.conn_id, &desc); + rc = ble_gap_conn_find(it.first, &desc); if(rc != 0 || !desc.sec_state.encrypted) { continue; } @@ -376,13 +385,13 @@ void NimBLECharacteristic::notify(bool is_notification) { NIMBLE_LOGW(LOG_TAG, "- Truncating to %d bytes (maximum notify size)", _mtu - 3); } - if(is_notification && (!(it.sub_val & NIMBLE_DESC_FLAG_NOTIFY))) { + if(is_notification && (!(it.second & NIMBLE_SUB_NOTIFY))) { NIMBLE_LOGW(LOG_TAG, "Sending notification to client subscribed to indications, sending indication instead"); is_notification = false; } - if(!is_notification && (!(it.sub_val & NIMBLE_DESC_FLAG_INDICATE))) { + if(!is_notification && (!(it.second & NIMBLE_SUB_INDICATE))) { NIMBLE_LOGW(LOG_TAG, "Sending indication to client subscribed to notification, sending notification instead"); is_notification = true; @@ -399,7 +408,7 @@ void NimBLECharacteristic::notify(bool is_notification) { ble_task_data_t taskData = {nullptr, xTaskGetCurrentTaskHandle(),0, nullptr}; m_pTaskData = &taskData; - rc = ble_gattc_indicate_custom(it.conn_id, m_handle, om); + rc = ble_gattc_indicate_custom(it.first, m_handle, om); if(rc != 0){ statusRC = NimBLECharacteristicCallbacks::Status::ERROR_GATT; } else { @@ -418,7 +427,7 @@ void NimBLECharacteristic::notify(bool is_notification) { statusRC = NimBLECharacteristicCallbacks::Status::ERROR_INDICATE_FAILURE; } } else { - rc = ble_gattc_notify_custom(it.conn_id, m_handle, om); + rc = ble_gattc_notify_custom(it.first, m_handle, om); if(rc == 0) { statusRC = NimBLECharacteristicCallbacks::Status::SUCCESS_NOTIFY; } else { @@ -435,7 +444,8 @@ void NimBLECharacteristic::notify(bool is_notification) { /** * @brief Set the callback handlers for this characteristic. - * @param [in] pCallbacks An instance of a callbacks structure used to define any callbacks for the characteristic. + * @param [in] pCallbacks An instance of a NimBLECharacteristicCallbacks class\n + * used to define any callbacks for the characteristic. */ void NimBLECharacteristic::setCallbacks(NimBLECharacteristicCallbacks* pCallbacks) { if (pCallbacks != nullptr){ @@ -473,11 +483,9 @@ void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) { /** - * @brief Set the value of the characteristic from string data. - * We set the value of the characteristic from the bytes contained in the - * string. - * @param [in] Set the value of the characteristic. - * @return N/A. + * @brief Set the value of the characteristic from string data.\n + * We set the value of the characteristic from the bytes contained in the string. + * @param [in] value the std::string value of the characteristic. */ void NimBLECharacteristic::setValue(const std::string &value) { setValue((uint8_t*)(value.data()), value.length()); @@ -515,6 +523,14 @@ void NimBLECharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onRead: default"); } // onRead +/** + * @brief Callback function to support a read request. + * @param [in] pCharacteristic The characteristic that is the source of the event. + * @param [in] desc The connection description struct that is associated with the peer that performed the read. + */ +void NimBLECharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc) { + NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onRead: default"); +} // onRead /** * @brief Callback function to support a write request. @@ -524,6 +540,14 @@ void NimBLECharacteristicCallbacks::onWrite(NimBLECharacteristic* pCharacteristi NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onWrite: default"); } // onWrite +/** + * @brief Callback function to support a write request. + * @param [in] pCharacteristic The characteristic that is the source of the event. + * @param [in] desc The connection description struct that is associated with the peer that performed the write. + */ +void NimBLECharacteristicCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc) { + NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onWrite: default"); +} // onWrite /** * @brief Callback function to support a Notify request. @@ -537,12 +561,31 @@ void NimBLECharacteristicCallbacks::onNotify(NimBLECharacteristic* pCharacterist /** * @brief Callback function to support a Notify/Indicate Status report. * @param [in] pCharacteristic The characteristic that is the source of the event. - * @param [in] s Status of the notification/indication - * @param [in] code Additional code of underlying errors + * @param [in] s Status of the notification/indication. + * @param [in] code Additional return code from the NimBLE stack. */ void NimBLECharacteristicCallbacks::onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code) { NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onStatus: default"); } // onStatus + +/** + * @brief Callback function called when a client changes subscription status. + * @param [in] pCharacteristic The characteristic that is the source of the event. + * @param [in] desc The connection description struct that is associated with the client. + * @param [in] subValue The subscription status: + * * 0 = Un-Subscribed + * * 1 = Notifications + * * 2 = Indications + * * 3 = Notifications and Indications + */ +void NimBLECharacteristicCallbacks::onSubscribe(NimBLECharacteristic* pCharacteristic, + ble_gap_conn_desc* desc, + uint16_t subValue) +{ + NIMBLE_LOGD("NimBLECharacteristicCallbacks", "onSubscribe: default"); +} + + #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #endif /* CONFIG_BT_ENABLED */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.h b/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.h index 4474e5f29..1c7418aef 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.h +++ b/libesp32/NimBLE-Arduino/src/NimBLECharacteristic.h @@ -75,6 +75,15 @@ public: NimBLEUUID getUUID(); std::string getValue(time_t *timestamp = nullptr); + /** + * @brief A template to convert the characteristic data to . + * @tparam T The type to convert the data to. + * @param [in] timestamp A pointer to a time_t struct to store the time the value was read. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: getValue(×tamp, skipSizeCheck); + */ template T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) { std::string value = getValue(); @@ -90,6 +99,10 @@ public: void setValue(const uint8_t* data, size_t size); void setValue(const std::string &value); + /** + * @brief Convenience template to set the characteristic value to val. + * @param [in] s The value to set. + */ template void setValue(const T &s) { setValue((uint8_t*)&s, sizeof(T)); @@ -97,6 +110,7 @@ public: std::string toString(); uint16_t getHandle(); + size_t getSubscribedCount(); private: @@ -132,6 +146,8 @@ private: ble_task_data_t *m_pTaskData; portMUX_TYPE m_valMux; time_t m_timestamp; + + std::vector> m_subscribedVec; }; // NimBLECharacteristic @@ -144,6 +160,12 @@ private: */ class NimBLECharacteristicCallbacks { public: + +/** + * @brief An enum to provide the callback the status of the + * notification/indication, implemented for backward compatibility. + * @deprecated To be removed in the future as the NimBLE stack return code is also provided. + */ typedef enum { SUCCESS_INDICATE, SUCCESS_NOTIFY, @@ -157,9 +179,12 @@ public: virtual ~NimBLECharacteristicCallbacks(); virtual void onRead(NimBLECharacteristic* pCharacteristic); + virtual void onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc); virtual void onWrite(NimBLECharacteristic* pCharacteristic); + virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc); virtual void onNotify(NimBLECharacteristic* pCharacteristic); virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code); + virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue); }; #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) diff --git a/libesp32/NimBLE-Arduino/src/NimBLEClient.cpp b/libesp32/NimBLE-Arduino/src/NimBLEClient.cpp index 71f1c9afc..539a7a016 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEClient.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEClient.cpp @@ -48,8 +48,12 @@ static NimBLEClientCallbacks defaultCallbacks; * */ -NimBLEClient::NimBLEClient() -{ + +/** + * @brief Constructor, private - only callable by NimBLEDevice::createClient + * to ensure proper handling of the list of client objects. + */ +NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(peerAddress) { m_pClientCallbacks = &defaultCallbacks; m_conn_id = BLE_HS_CONN_HANDLE_NONE; m_isConnected = false; @@ -86,7 +90,7 @@ NimBLEClient::~NimBLEClient() { /** - * @brief Delete any existing services. + * @brief Delete all service objects created by this client and clear the vector. */ void NimBLEClient::deleteServices() { NIMBLE_LOGD(LOG_TAG, ">> deleteServices"); @@ -123,30 +127,36 @@ size_t NimBLEClient::deleteService(const NimBLEUUID &uuid) { /** - * NOT NEEDED - */ - /* -void NimBLEClient::onHostReset() { - -} - */ - -/** - * Add overloaded function to ease connect to peer device with not public address - */ -bool NimBLEClient::connect(NimBLEAdvertisedDevice* device, bool refreshServices) { - NimBLEAddress address(device->getAddress()); - uint8_t type = device->getAddressType(); - return connect(address, type, refreshServices); -} - - -/** - * @brief Connect to the partner (BLE Server). - * @param [in] address The address of the partner. + * @brief Connect to the BLE Server. + * @param [in] deleteAttibutes If true this will delete any attribute objects this client may already\n + * have created and clears the vectors after successful connection. * @return True on success. */ -bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refreshServices) { +bool NimBLEClient::connect(bool deleteAttibutes) { + return connect(m_peerAddress, deleteAttibutes); +} + +/** + * @brief Connect to an advertising device. + * @param [in] device The device to connect to. + * @param [in] deleteAttibutes If true this will delete any attribute objects this client may already\n + * have created and clears the vectors after successful connection. + * @return True on success. + */ +bool NimBLEClient::connect(NimBLEAdvertisedDevice* device, bool deleteAttibutes) { + NimBLEAddress address(device->getAddress()); + return connect(address, deleteAttibutes); +} + + +/** + * @brief Connect to the BLE Server. + * @param [in] address The address of the server. + * @param [in] deleteAttibutes If true this will delete any attribute objects this client may already\n + * have created and clears the vectors after successful connection. + * @return True on success. + */ +bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) { NIMBLE_LOGD(LOG_TAG, ">> connect(%s)", address.toString().c_str()); if(!NimBLEDevice::m_synced) { @@ -163,17 +173,23 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr return false; } - int rc = 0; - m_peerAddress = address; + if(address == NimBLEAddress("")) { + NIMBLE_LOGE(LOG_TAG, "Invalid peer address;(NULL)"); + return false; + } else if(m_peerAddress != address) { + m_peerAddress = address; + } ble_addr_t peerAddrt; - memcpy(&peerAddrt.val, address.getNative(),6); - peerAddrt.type = type; + memcpy(&peerAddrt.val, m_peerAddress.getNative(),6); + peerAddrt.type = m_peerAddress.getType(); ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; m_pTaskData = &taskData; - /** Try to connect the the advertiser. Allow 30 seconds (30000 ms) for + int rc = 0; + + /* Try to connect the the advertiser. Allow 30 seconds (30000 ms) for * timeout (default value of m_connectTimeout). * Loop on BLE_HS_EBUSY if the scan hasn't stopped yet. */ @@ -186,10 +202,9 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr }while(rc == BLE_HS_EBUSY); if (rc != 0 && rc != BLE_HS_EDONE) { - NIMBLE_LOGE(LOG_TAG, "Error: Failed to connect to device; addr_type=%d " + NIMBLE_LOGE(LOG_TAG, "Error: Failed to connect to device; " "addr=%s, rc=%d; %s", - type, - m_peerAddress.toString().c_str(), + std::string(m_peerAddress).c_str(), rc, NimBLEUtils::returnCodeToString(rc)); m_pTaskData = nullptr; m_waitingToConnect = false; @@ -205,8 +220,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr return false; } - if(refreshServices) { - NIMBLE_LOGD(LOG_TAG, "Refreshing Services for: (%s)", address.toString().c_str()); + if(deleteAttibutes) { deleteServices(); } @@ -218,33 +232,38 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr /** - * @brief Called when a characteristic or descriptor requires encryption or authentication to access it. - * This will pair with the device and bond if enabled. + * @brief Initiate a secure connection (pair/bond) with the server.\n + * Called automatically when a characteristic or descriptor requires encryption or authentication to access it. * @return True on success. */ bool NimBLEClient::secureConnection() { ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr}; - m_pTaskData = &taskData; - int rc = NimBLEDevice::startSecurity(m_conn_id); - if(rc != 0){ - m_pTaskData = nullptr; - return false; - } + int retryCount = 1; - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + do { + m_pTaskData = &taskData; + + int rc = NimBLEDevice::startSecurity(m_conn_id); + if(rc != 0){ + m_pTaskData = nullptr; + return false; + } + + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + } while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--); if(taskData.rc != 0){ return false; } return true; -} +} // secureConnection /** * @brief Disconnect from the peer. - * @return N/A. + * @return Error code from NimBLE stack, 0 = success. */ int NimBLEClient::disconnect(uint8_t reason) { NIMBLE_LOGD(LOG_TAG, ">> disconnect()"); @@ -264,6 +283,12 @@ int NimBLEClient::disconnect(uint8_t reason) { /** * @brief Set the connection paramaters to use when connecting to a server. + * @param [in] minInterval minimum connection interval in 0.625ms units. + * @param [in] maxInterval maximum connection interval in 0.625ms units. + * @param [in] latency number of packets allowed to skip (extends max interval) + * @param [in] timeout the timeout time in 10ms units before disconnecting + * @param [in] scanInterval the scan interval to use when attempting to connect in 0.625ms units. + * @param [in] scanWindow the scan window to use when attempting to connect in 0.625ms units. */ void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout, @@ -271,12 +296,12 @@ void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterva uint16_t minConnTime, uint16_t maxConnTime)*/ { - m_pConnParams.scan_itvl = scanInterval; // Scan interval in 0.625ms units - m_pConnParams.scan_window = scanWindow; // Scan window in 0.625ms units - m_pConnParams.itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms - m_pConnParams.itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms - m_pConnParams.latency = latency; // number of packets allowed to skip (extends max interval) - m_pConnParams.supervision_timeout = timeout; // timeout = 400*10ms = 4000ms + m_pConnParams.scan_itvl = scanInterval; + m_pConnParams.scan_window = scanWindow; + m_pConnParams.itvl_min = minInterval; + m_pConnParams.itvl_max = maxInterval; + m_pConnParams.latency = latency; + m_pConnParams.supervision_timeout = timeout; // These are not used by NimBLE at this time - Must leave at defaults //m_pConnParams->min_ce_len = minConnTime; // Minimum length of connection event in 0.625ms units @@ -284,11 +309,16 @@ void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterva int rc = NimBLEUtils::checkConnParams(&m_pConnParams); assert(rc == 0 && "Invalid Connection parameters"); -} +} // setConnectionParams /** - * Update connection parameters can be called only after connection has been established + * @brief Update the connection parameters: + * * Can only be used after a connection has been established. + * @param [in] minInterval minimum connection interval in 0.625ms units. + * @param [in] maxInterval maximum connection interval in 0.625ms units. + * @param [in] latency number of packets allowed to skip (extends max interval) + * @param [in] timeout the timeout time in 10ms units before disconnecting */ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout) @@ -308,16 +338,16 @@ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval, NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); } -} +} // updateConnParams /** - * @brief Set the timeout to wait for connection attempt to complete - * @params[in] Time to wait in seconds. + * @brief Set the timeout to wait for connection attempt to complete. + * @param [in] time The number of seconds before timeout. */ void NimBLEClient::setConnectTimeout(uint8_t time) { m_connectTimeout = (uint32_t)(time * 1000); -} +} // setConnectTimeout /** @@ -334,7 +364,23 @@ uint16_t NimBLEClient::getConnId() { */ NimBLEAddress NimBLEClient::getPeerAddress() { return m_peerAddress; -} // getAddress +} // getPeerAddress + + +/** + * @brief Set the peer address. + * @param [in] address The address of the peer that this client is + * connected or should connect to. + */ +void NimBLEClient::setPeerAddress(const NimBLEAddress &address) { + if(isConnected()) { + NIMBLE_LOGE(LOG_TAG, "Cannot set peer address while connected"); + return; + } + + m_peerAddress = address; + NIMBLE_LOGD(LOG_TAG, "Peer address set: %s", std::string(m_peerAddress).c_str()); +} // setPeerAddress /** @@ -381,7 +427,7 @@ std::vector::iterator NimBLEClient::end() { /** * @brief Get the service BLE Remote Service instance corresponding to the uuid. * @param [in] uuid The UUID of the service being sought. - * @return A reference to the Service or nullptr if don't know about it. + * @return A pointer to the service or nullptr if not found. */ NimBLERemoteService* NimBLEClient::getService(const char* uuid) { return getService(NimBLEUUID(uuid)); @@ -391,7 +437,7 @@ NimBLERemoteService* NimBLEClient::getService(const char* uuid) { /** * @brief Get the service object corresponding to the uuid. * @param [in] uuid The UUID of the service being sought. - * @return A reference to the Service or nullptr if don't know about it. + * @return A pointer to the service or nullptr if not found. */ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) { NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str()); @@ -416,12 +462,11 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) { /** - * @Get a pointer to the vector of found services. - * @param [in] bool value to indicate if the current vector should be cleared and - * subsequently all services retrieved from the peripheral. - * If false the vector will be returned with the currently stored services, - * If true it will retrieve all services from the peripheral and return the vector with all services - * @return a pointer to the vector of available services. + * @brief Get a pointer to the vector of found services. + * @param [in] refresh If true the current services vector will be cleared and\n + * all services will be retrieved from the peripheral.\n + * If false the vector will be returned with the currently stored services. + * @return A pointer to the vector of available services. */ std::vector* NimBLEClient::getServices(bool refresh) { if(refresh) { @@ -435,11 +480,11 @@ std::vector* NimBLEClient::getServices(bool refresh) { } } return &m_servicesVector; -} +} // getServices /** - * @ Retrieves the full database of attributes that the peripheral has available. + * @brief Retrieves the full database of attributes that the peripheral has available. */ void NimBLEClient::discoverAttributes() { for(auto svc: *getServices(true)) { @@ -447,14 +492,12 @@ void NimBLEClient::discoverAttributes() { chr->getDescriptors(true); } } -} +} // discoverAttributes /** - * @brief Ask the remote %BLE server for its services. - * A %BLE Server exposes a set of services for its partners. Here we ask the server for its set of - * services and wait until we have received them all. - * We then ask for the characteristics for each service found and their desciptors. + * @brief Ask the remote %BLE server for its services.\n + * Here we ask the server for its set of services and wait until we have received them all. * @return true on success otherwise false if an error occurred */ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) { @@ -502,7 +545,7 @@ bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) { /** - * @brief STATIC Callback for the service discovery API function. + * @brief STATIC Callback for the service discovery API function.\n * When a service is found or there is none left or there was an error * the API will call this and report findings. */ @@ -574,6 +617,7 @@ std::string NimBLEClient::getValue(const NimBLEUUID &serviceUUID, const NimBLEUU * @brief Set the value of a specific characteristic associated with a specific service. * @param [in] serviceUUID The service that owns the characteristic. * @param [in] characteristicUUID The characteristic whose value we wish to write. + * @param [in] value The value to write to the characteristic. * @returns true if successful otherwise false */ bool NimBLEClient::setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, @@ -600,17 +644,17 @@ bool NimBLEClient::setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &cha /** * @brief Get the current mtu of this connection. + * @returns The MTU value. */ uint16_t NimBLEClient::getMTU() { return ble_att_mtu(m_conn_id); -} +} // getMTU /** * @brief Handle a received GAP event. - * - * @param [in] event - * @param [in] arg = pointer to the client instance + * @param [in] event The event structure sent by the NimBLE stack. + * @param [in] arg A pointer to the client instance that registered for this callback. */ /*STATIC*/ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) { NimBLEClient* client = (NimBLEClient*)arg; @@ -783,12 +827,15 @@ uint16_t NimBLEClient::getMTU() { return 0; } - if(event->enc_change.status == 0) { + if(event->enc_change.status == 0 || event->enc_change.status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) { struct ble_gap_conn_desc desc; - rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc); + rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc); assert(rc == 0); - if(NimBLEDevice::m_securityCallbacks != nullptr) { + if (event->enc_change.status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) { + // Key is missing, try deleting. + ble_store_util_delete_peer(&desc.peer_id_addr); + } else if(NimBLEDevice::m_securityCallbacks != nullptr) { NimBLEDevice::m_securityCallbacks->onAuthenticationComplete(&desc); } else { client->m_pClientCallbacks->onAuthenticationComplete(&desc); @@ -855,7 +902,7 @@ uint16_t NimBLEClient::getMTU() { pkey.passkey = NimBLEDevice::m_securityCallbacks->onPassKeyRequest(); ///////////////////////////////////////////// } else { - client->m_pClientCallbacks->onPassKeyRequest(); + pkey.passkey = client->m_pClientCallbacks->onPassKeyRequest(); } rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey); @@ -893,7 +940,9 @@ bool NimBLEClient::isConnected() { /** - * @brief Set the callbacks that will be invoked. + * @brief Set the callbacks that will be invoked when events are received. + * @param [in] pClientCallbacks A pointer to a class to receive the event callbacks. + * @param [in] deleteCallbacks If true this will delete the callback class sent when the client is destructed. */ void NimBLEClient::setClientCallbacks(NimBLEClientCallbacks* pClientCallbacks, bool deleteCallbacks) { if (pClientCallbacks != nullptr){ @@ -938,7 +987,7 @@ uint32_t NimBLEClientCallbacks::onPassKeyRequest(){ NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyRequest: default: 123456"); return 123456; } - +/* void NimBLEClientCallbacks::onPassKeyNotify(uint32_t pass_key){ NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyNotify: default: %d", pass_key); } @@ -946,7 +995,7 @@ void NimBLEClientCallbacks::onPassKeyNotify(uint32_t pass_key){ bool NimBLEClientCallbacks::onSecurityRequest(){ NIMBLE_LOGD("NimBLEClientCallbacks", "onSecurityRequest: default: true"); return true; -} +}*/ void NimBLEClientCallbacks::onAuthenticationComplete(ble_gap_conn_desc* desc){ NIMBLE_LOGD("NimBLEClientCallbacks", "onAuthenticationComplete: default"); } diff --git a/libesp32/NimBLE-Arduino/src/NimBLEClient.h b/libesp32/NimBLE-Arduino/src/NimBLEClient.h index 888b42d02..ddeef3cc3 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEClient.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEClient.h @@ -38,11 +38,12 @@ class NimBLEAdvertisedDevice; */ class NimBLEClient { public: - bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true); - bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC, - bool refreshServices = true); + bool connect(NimBLEAdvertisedDevice* device, bool deleteAttibutes = true); + bool connect(const NimBLEAddress &address, bool deleteAttibutes = true); + bool connect(bool deleteAttibutes = true); int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); NimBLEAddress getPeerAddress(); + void setPeerAddress(const NimBLEAddress &address); int getRssi(); std::vector* getServices(bool refresh = false); std::vector::iterator begin(); @@ -70,7 +71,7 @@ public: void discoverAttributes(); private: - NimBLEClient(); + NimBLEClient(const NimBLEAddress &peerAddress); ~NimBLEClient(); friend class NimBLEDevice; @@ -83,7 +84,7 @@ private: void *arg); bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr); - NimBLEAddress m_peerAddress = NimBLEAddress(""); + NimBLEAddress m_peerAddress; uint16_t m_conn_id; bool m_isConnected; bool m_waitingToConnect; @@ -107,13 +108,48 @@ private: class NimBLEClientCallbacks { public: virtual ~NimBLEClientCallbacks() {}; + + /** + * @brief Called after client connects. + * @param [in] pClient A pointer to the calling client object. + */ virtual void onConnect(NimBLEClient* pClient); + + /** + * @brief Called when disconnected from the server. + * @param [in] pClient A pointer to the calling client object. + */ virtual void onDisconnect(NimBLEClient* pClient); + + /** + * @brief Called when server requests to update the connection parameters. + * @param [in] pClient A pointer to the calling client object. + * @param [in] params A pointer to the struct containing the connection parameters requested. + * @return True to accept the parmeters. + */ virtual bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params); + + /** + * @brief Called when server requests a passkey for pairing. + * @return The passkey to be sent to the server. + */ virtual uint32_t onPassKeyRequest(); - virtual void onPassKeyNotify(uint32_t pass_key); - virtual bool onSecurityRequest(); + + /*virtual void onPassKeyNotify(uint32_t pass_key); + virtual bool onSecurityRequest();*/ + + /** + * @brief Called when the pairing procedure is complete. + * @param [in] desc A pointer to the struct containing the connection information.\n + * This can be used to check the status of the connection encryption/pairing. + */ virtual void onAuthenticationComplete(ble_gap_conn_desc* desc); + + /** + * @brief Called when using numeric comparision for pairing. + * @param [in] pin The pin to compare with the server. + * @return True to accept the pin. + */ virtual bool onConfirmPIN(uint32_t pin); }; diff --git a/libesp32/NimBLE-Arduino/src/NimBLEDescriptor.h b/libesp32/NimBLE-Arduino/src/NimBLEDescriptor.h index f4978f570..16b6edceb 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEDescriptor.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEDescriptor.h @@ -52,6 +52,10 @@ public: void setValue(const std::string &value); std::string toString(); + /** + * @brief Convenience template to set the descriptor value to val. + * @param [in] s The value to set. + */ template void setValue(const T &s) { setValue((uint8_t*)&s, sizeof(T)); @@ -101,6 +105,8 @@ public: virtual void onWrite(NimBLEDescriptor* pDescriptor); }; +#include "NimBLE2904.h" + #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #endif /* CONFIG_BT_ENABLED */ #endif /* MAIN_NIMBLEDESCRIPTOR_H_ */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLEDevice.cpp b/libesp32/NimBLE-Arduino/src/NimBLEDevice.cpp index b3d882ba3..fb36e6e57 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEDevice.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEDevice.cpp @@ -90,6 +90,10 @@ NimBLESecurityCallbacks* NimBLEDevice::m_securityCallbacks = nullptr; #if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) +/** + * @brief Get the instance of the advertising object. + * @return A pointer to the advertising object. + */ NimBLEAdvertising* NimBLEDevice::getAdvertising() { if(m_bleAdvertising == nullptr) { m_bleAdvertising = new NimBLEAdvertising(); @@ -98,11 +102,17 @@ NimBLEAdvertising* NimBLEDevice::getAdvertising() { } +/** + * @brief Convenience function to begin advertising. + */ void NimBLEDevice::startAdvertising() { getAdvertising()->start(); } // startAdvertising +/** + * @brief Convenience function to stop advertising. + */ void NimBLEDevice::stopAdvertising() { getAdvertising()->stop(); } // stopAdvertising @@ -123,19 +133,22 @@ void NimBLEDevice::stopAdvertising() { } // getScan #endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) + /** * @brief Creates a new client object and maintains a list of all client objects * each client can connect to 1 peripheral device. + * @param [in] peerAddress An optional peer address that is copied to the new client + * object, allows for calling NimBLEClient::connect(bool) without a device or address parameter. * @return A reference to the new client object. */ #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) -/* STATIC */ NimBLEClient* NimBLEDevice::createClient() { +/* STATIC */ NimBLEClient* NimBLEDevice::createClient(NimBLEAddress peerAddress) { if(m_cList.size() >= NIMBLE_MAX_CONNECTIONS) { NIMBLE_LOGW("Number of clients exceeds Max connections. Max=(%d)", NIMBLE_MAX_CONNECTIONS); } - NimBLEClient* pClient = new NimBLEClient(); + NimBLEClient* pClient = new NimBLEClient(peerAddress); m_cList.push_back(pClient); return pClient; @@ -143,9 +156,9 @@ void NimBLEDevice::stopAdvertising() { /** - * @brief Delete the client object and remove it from the list. - * Check if it is connected or trying to connect and close/stop it first. - * @param [in] Pointer to the client object. + * @brief Delete the client object and remove it from the list.\n + * Checks if it is connected or trying to connect and disconnects/stops it first. + * @param [in] pClient A pointer to the client object. */ /* STATIC */ bool NimBLEDevice::deleteClient(NimBLEClient* pClient) { if(pClient == nullptr) { @@ -183,8 +196,8 @@ void NimBLEDevice::stopAdvertising() { /** - * @brief get the list of clients. - * @return a pointer to the list of clients. + * @brief Get the list of created client objects. + * @return A pointer to the list of clients. */ /* STATIC */std::list* NimBLEDevice::getClientList() { return &m_cList; @@ -192,8 +205,8 @@ void NimBLEDevice::stopAdvertising() { /** - * @brief get the size of the list of clients. - * @return a pointer to the list of clients. + * @brief Get the number of created client objects. + * @return Number of client objects created. */ /* STATIC */size_t NimBLEDevice::getClientListSize() { return m_cList.size(); @@ -202,8 +215,8 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Get a reference to a client by connection ID. - * @param [in] The client connection ID to search for. - * @return A reference pointer to the client with the spcified connection ID. + * @param [in] conn_id The client connection ID to search for. + * @return A pointer to the client object with the spcified connection ID. */ /* STATIC */NimBLEClient* NimBLEDevice::getClientByID(uint16_t conn_id) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { @@ -218,8 +231,8 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Get a reference to a client by peer address. - * @param [in] a NimBLEAddress of the peer to search for. - * @return A reference pointer to the client with the peer address. + * @param [in] peer_addr The address of the peer to search for. + * @return A pointer to the client object with the peer address. */ /* STATIC */NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { @@ -233,7 +246,7 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Finds the first disconnected client in the list. - * @return A reference pointer to the first client that is not connected to a peer. + * @return A pointer to the first client object that is not connected to a peer. */ /* STATIC */NimBLEClient* NimBLEDevice::getDisconnectedClient() { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { @@ -249,16 +262,28 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Set the transmission power. - * The power level can be one of: - * * ESP_PWR_LVL_N12 = 0, !< Corresponding to -12dbm - * * ESP_PWR_LVL_N9 = 1, !< Corresponding to -9dbm - * * ESP_PWR_LVL_N6 = 2, !< Corresponding to -6dbm - * * ESP_PWR_LVL_N3 = 3, !< Corresponding to -3dbm - * * ESP_PWR_LVL_N0 = 4, !< Corresponding to 0dbm - * * ESP_PWR_LVL_P3 = 5, !< Corresponding to +3dbm - * * ESP_PWR_LVL_P6 = 6, !< Corresponding to +6dbm - * * ESP_PWR_LVL_P9 = 7, !< Corresponding to +9dbm - * @param [in] powerLevel. + * @param [in] powerLevel The power level to set, can be one of: + * * ESP_PWR_LVL_N12 = 0, Corresponding to -12dbm + * * ESP_PWR_LVL_N9 = 1, Corresponding to -9dbm + * * ESP_PWR_LVL_N6 = 2, Corresponding to -6dbm + * * ESP_PWR_LVL_N3 = 3, Corresponding to -3dbm + * * ESP_PWR_LVL_N0 = 4, Corresponding to 0dbm + * * ESP_PWR_LVL_P3 = 5, Corresponding to +3dbm + * * ESP_PWR_LVL_P6 = 6, Corresponding to +6dbm + * * ESP_PWR_LVL_P9 = 7, Corresponding to +9dbm + * @param [in] powerType The BLE function to set the power level for, can be one of: + * * ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, For connection handle 0 + * * ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, For connection handle 1 + * * ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, For connection handle 2 + * * ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, For connection handle 3 + * * ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, For connection handle 4 + * * ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, For connection handle 5 + * * ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, For connection handle 6 + * * ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, For connection handle 7 + * * ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, For connection handle 8 + * * ESP_BLE_PWR_TYPE_ADV = 9, For advertising + * * ESP_BLE_PWR_TYPE_SCAN = 10, For scan + * * ESP_BLE_PWR_TYPE_DEFAULT = 11, For default, if not set other, it will use default value */ /* STATIC */ void NimBLEDevice::setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType) { NIMBLE_LOGD(LOG_TAG, ">> setPower: %d (type: %d)", powerLevel, powerType); @@ -270,6 +295,24 @@ void NimBLEDevice::stopAdvertising() { } // setPower +/** + * @brief Set the transmission power. + * @param [in] powerType The power level to set, can be one of: + * * ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, For connection handle 0 + * * ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, For connection handle 1 + * * ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, For connection handle 2 + * * ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, For connection handle 3 + * * ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, For connection handle 4 + * * ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, For connection handle 5 + * * ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, For connection handle 6 + * * ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, For connection handle 7 + * * ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, For connection handle 8 + * * ESP_BLE_PWR_TYPE_ADV = 9, For advertising + * * ESP_BLE_PWR_TYPE_SCAN = 10, For scan + * * ESP_BLE_PWR_TYPE_DEFAULT = 11, For default, if not set other, it will use default value + * @return the power level currently used by the type specified. + */ + /* STATIC */ int NimBLEDevice::getPower(esp_ble_power_type_t powerType) { switch(esp_ble_tx_power_get(powerType)) { @@ -302,7 +345,6 @@ void NimBLEDevice::stopAdvertising() { */ /* STATIC*/ NimBLEAddress NimBLEDevice::getAddress() { ble_addr_t addr = {BLE_ADDR_PUBLIC, 0}; - //ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL) if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL)) { NIMBLE_LOGD(LOG_TAG, "Public address not found, checking random"); @@ -324,9 +366,9 @@ void NimBLEDevice::stopAdvertising() { /** - * @brief Setup local mtu that will be used to negotiate mtu during request from client peer - * @param [in] mtu Value to set local mtu, should be larger than 23 and lower or equal to - * BLE_ATT_MTU_MAX = 527 + * @brief Setup local mtu that will be used to negotiate mtu during request from client peer. + * @param [in] mtu Value to set local mtu: + * * This should be larger than 23 and lower or equal to BLE_ATT_MTU_MAX = 527. */ /* STATIC */int NimBLEDevice::setMTU(uint16_t mtu) { NIMBLE_LOGD(LOG_TAG, ">> setLocalMTU: %d", mtu); @@ -344,6 +386,7 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Get local MTU value set. + * @return The current preferred MTU setting. */ /* STATIC */uint16_t NimBLEDevice::getMTU() { return ble_att_preferred_mtu(); @@ -352,6 +395,7 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Host reset, we pass the message so we don't make calls until resynced. + * @param [in] reason The reason code for the reset. */ /* STATIC */ void NimBLEDevice::onReset(int reason) { @@ -389,7 +433,7 @@ void NimBLEDevice::stopAdvertising() { /** - * @brief Host resynced with controller, all clear to make calls. + * @brief Host resynced with controller, all clear to make calls to the stack. */ /* STATIC */ void NimBLEDevice::onSync(void) { @@ -439,7 +483,7 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Initialize the %BLE environment. - * @param deviceName The device name of the device. + * @param [in] deviceName The device name of the device. */ /* STATIC */ void NimBLEDevice::init(const std::string &deviceName) { if(!initialized){ @@ -497,8 +541,10 @@ void NimBLEDevice::stopAdvertising() { /** * @brief Shutdown the NimBLE stack/controller. + * @param [in] clearAll If true, deletes all server/advertising/scan/client objects after deinitializing. + * @note If clearAll is true when called, any references to the created objects become invalid. */ -/* STATIC */ void NimBLEDevice::deinit() { +/* STATIC */ void NimBLEDevice::deinit(bool clearAll) { int ret = nimble_port_stop(); if (ret == 0) { nimble_port_deinit(); @@ -510,12 +556,49 @@ void NimBLEDevice::stopAdvertising() { initialized = false; m_synced = false; + + if(clearAll) { +#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) + if(NimBLEDevice::m_pServer != nullptr) { + delete NimBLEDevice::m_pServer; + NimBLEDevice::m_pServer = nullptr; + } +#endif + +#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) + if(NimBLEDevice::m_bleAdvertising != nullptr) { + delete NimBLEDevice::m_bleAdvertising; + NimBLEDevice::m_bleAdvertising = nullptr; + } +#endif + +#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) + if(NimBLEDevice::m_pScan != nullptr) { + delete NimBLEDevice::m_pScan; + NimBLEDevice::m_pScan= nullptr; + } +#endif + +#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) + for(auto &it : m_cList) { + deleteClient(it); + m_cList.clear(); + } +#endif + + m_ignoreList.clear(); + + if(m_securityCallbacks != nullptr) { + delete m_securityCallbacks; + } + } } } // deinit /** * @brief Check if the initialization is complete. + * @return true if initialized. */ bool NimBLEDevice::getInitialized() { return initialized; @@ -524,9 +607,9 @@ bool NimBLEDevice::getInitialized() { /** * @brief Set the authorization mode for this device. - * @param bonding, if true we allow bonding, false no bonding will be performed. - * @param mitm, if true we are capable of man in the middle protection, false if not. - * @param sc, if true we will perform secure connection pairing, false we will use legacy pairing. + * @param bonding If true we allow bonding, false no bonding will be performed. + * @param mitm If true we are capable of man in the middle protection, false if not. + * @param sc If true we will perform secure connection pairing, false we will use legacy pairing. */ /*STATIC*/ void NimBLEDevice::setSecurityAuth(bool bonding, bool mitm, bool sc) { NIMBLE_LOGD(LOG_TAG, "Setting bonding: %d, mitm: %d, sc: %d",bonding,mitm,sc); @@ -538,13 +621,12 @@ bool NimBLEDevice::getInitialized() { /** * @brief Set the authorization mode for this device. - * @param A bitmap indicating what modes are supported. - * The bits are defined as follows: - ** 0x01 BLE_SM_PAIR_AUTHREQ_BOND - ** 0x04 BLE_SM_PAIR_AUTHREQ_MITM - ** 0x08 BLE_SM_PAIR_AUTHREQ_SC - ** 0x10 BLE_SM_PAIR_AUTHREQ_KEYPRESS - not yet supported. - ** 0xe2 BLE_SM_PAIR_AUTHREQ_RESERVED - for reference only. + * @param auth_req A bitmap indicating what modes are supported.\n + * The available bits are defined as: + * * 0x01 BLE_SM_PAIR_AUTHREQ_BOND + * * 0x04 BLE_SM_PAIR_AUTHREQ_MITM + * * 0x08 BLE_SM_PAIR_AUTHREQ_SC + * * 0x10 BLE_SM_PAIR_AUTHREQ_KEYPRESS - not yet supported. */ /*STATIC*/void NimBLEDevice::setSecurityAuth(uint8_t auth_req) { NimBLEDevice::setSecurityAuth((auth_req & BLE_SM_PAIR_AUTHREQ_BOND)>0, @@ -555,12 +637,12 @@ bool NimBLEDevice::getInitialized() { /** * @brief Set the Input/Output capabilities of this device. - * @param One of the following: - ** 0x00 BLE_HS_IO_DISPLAY_ONLY DisplayOnly IO capability - ** 0x01 BLE_HS_IO_DISPLAY_YESNO DisplayYesNo IO capability - ** 0x02 BLE_HS_IO_KEYBOARD_ONLY KeyboardOnly IO capability - ** 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability - ** 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability + * @param iocap One of the following values: + * * 0x00 BLE_HS_IO_DISPLAY_ONLY DisplayOnly IO capability + * * 0x01 BLE_HS_IO_DISPLAY_YESNO DisplayYesNo IO capability + * * 0x02 BLE_HS_IO_KEYBOARD_ONLY KeyboardOnly IO capability + * * 0x03 BLE_HS_IO_NO_INPUT_OUTPUT NoInputNoOutput IO capability + * * 0x04 BLE_HS_IO_KEYBOARD_DISPLAY KeyboardDisplay Only IO capability */ /*STATIC*/ void NimBLEDevice::setSecurityIOCap(uint8_t iocap) { ble_hs_cfg.sm_io_cap = iocap; @@ -569,12 +651,12 @@ bool NimBLEDevice::getInitialized() { /** * @brief If we are the initiator of the security procedure this sets the keys we will distribute. - * @param A bitmap indicating which keys to distribute during pairing. - * The bits are defined as follows: - ** 0x01: BLE_SM_PAIR_KEY_DIST_ENC - Distribute the encryption key. - ** 0x02: BLE_SM_PAIR_KEY_DIST_ID - Distribute the ID key (IRK). - ** 0x04: BLE_SM_PAIR_KEY_DIST_SIGN - ** 0x08: BLE_SM_PAIR_KEY_DIST_LINK + * @param init_key A bitmap indicating which keys to distribute during pairing.\n + * The available bits are defined as: + * * 0x01: BLE_SM_PAIR_KEY_DIST_ENC - Distribute the encryption key. + * * 0x02: BLE_SM_PAIR_KEY_DIST_ID - Distribute the ID key (IRK). + * * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN + * * 0x08: BLE_SM_PAIR_KEY_DIST_LINK */ /*STATIC*/void NimBLEDevice::setSecurityInitKey(uint8_t init_key) { ble_hs_cfg.sm_our_key_dist = init_key; @@ -583,20 +665,21 @@ bool NimBLEDevice::getInitialized() { /** * @brief Set the keys we are willing to accept during pairing. - * @param A bitmap indicating which keys to accept during pairing. - * The bits are defined as follows: - ** 0x01: BLE_SM_PAIR_KEY_DIST_ENC - Accept the encryption key. - ** 0x02: BLE_SM_PAIR_KEY_DIST_ID - Accept the ID key (IRK). - ** 0x04: BLE_SM_PAIR_KEY_DIST_SIGN - ** 0x08: BLE_SM_PAIR_KEY_DIST_LINK + * @param resp_key A bitmap indicating which keys to accept during pairing. + * The available bits are defined as: + * * 0x01: BLE_SM_PAIR_KEY_DIST_ENC - Accept the encryption key. + * * 0x02: BLE_SM_PAIR_KEY_DIST_ID - Accept the ID key (IRK). + * * 0x04: BLE_SM_PAIR_KEY_DIST_SIGN + * * 0x08: BLE_SM_PAIR_KEY_DIST_LINK */ -/*STATIC*/void NimBLEDevice::setSecurityRespKey(uint8_t init_key) { - ble_hs_cfg.sm_their_key_dist = init_key; +/*STATIC*/void NimBLEDevice::setSecurityRespKey(uint8_t resp_key) { + ble_hs_cfg.sm_their_key_dist = resp_key; } // setsSecurityRespKey /** - * @brief Set the passkey for pairing. + * @brief Set the passkey the server will ask for when pairing. + * @param [in] pin The passkey to use. */ /*STATIC*/void NimBLEDevice::setSecurityPasskey(uint32_t pin) { m_passkey = pin; @@ -604,7 +687,8 @@ bool NimBLEDevice::getInitialized() { /** - * @brief Get the passkey for pairing. + * @brief Get the current passkey used for pairing. + * @return The current passkey. */ /*STATIC*/uint32_t NimBLEDevice::getSecurityPasskey() { return m_passkey; @@ -613,7 +697,8 @@ bool NimBLEDevice::getInitialized() { /** * @brief Set callbacks that will be used to handle encryption negotiation events and authentication events - * @param [in] cllbacks Pointer to NimBLESecurityCallbacks class + * @param [in] callbacks Pointer to NimBLESecurityCallbacks class + * @deprecated For backward compatibility, New code should use client/server callback methods. */ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { NimBLEDevice::m_securityCallbacks = callbacks; @@ -622,8 +707,8 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { /** * @brief Start the connection securing and authorization for this connection. - * @param Connection id of the client. - * @returns host return code 0 if success. + * @param conn_id The connection id of the peer device. + * @returns NimBLE stack return code, 0 = success. */ /* STATIC */int NimBLEDevice::startSecurity(uint16_t conn_id) { /* if(m_securityCallbacks != nullptr) { @@ -641,6 +726,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { /** * @brief Check if the device address is on our ignore list. + * @param [in] address The address to look for. * @return True if ignoring. */ /*STATIC*/ bool NimBLEDevice::isIgnored(const NimBLEAddress &address) { @@ -656,7 +742,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { /** * @brief Add a device to the ignore list. - * @param Address of the device we want to ignore. + * @param [in] address The address of the device we want to ignore. */ /*STATIC*/ void NimBLEDevice::addIgnored(const NimBLEAddress &address) { m_ignoreList.push_back(address); @@ -665,7 +751,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { /** * @brief Remove a device from the ignore list. - * @param Address of the device we want to remove from the list. + * @param [in] address The address of the device we want to remove from the list. */ /*STATIC*/void NimBLEDevice::removeIgnored(const NimBLEAddress &address) { for(auto it = m_ignoreList.begin(); it != m_ignoreList.end(); ++it) { @@ -679,6 +765,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) { /** * @brief Set a custom callback for gap events. + * @param [in] handler The function to call when gap events occur. */ void NimBLEDevice::setCustomGapHandler(gap_event_handler handler) { m_customGapHandler = handler; diff --git a/libesp32/NimBLE-Arduino/src/NimBLEDevice.h b/libesp32/NimBLE-Arduino/src/NimBLEDevice.h index 412647987..252c52afd 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEDevice.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEDevice.h @@ -81,17 +81,17 @@ #define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS #endif -/** - * @brief BLE functions. - */ - typedef int (*gap_event_handler)(ble_gap_event *event, void *arg); +typedef int (*gap_event_handler)(ble_gap_event *event, void *arg); extern "C" void ble_store_config_init(void); +/** + * @brief A model of a %BLE Device from which all the BLE roles are created. + */ class NimBLEDevice { public: static void init(const std::string &deviceName); - static void deinit(); + static void deinit(bool clearAll = false); static bool getInitialized(); static NimBLEAddress getAddress(); static std::string toString(); @@ -130,7 +130,7 @@ public: #endif #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) - static NimBLEClient* createClient(); + static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress("")); static bool deleteClient(NimBLEClient* pClient); static NimBLEClient* getClientByID(uint16_t conn_id); static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr); @@ -181,8 +181,6 @@ private: static NimBLESecurityCallbacks* m_securityCallbacks; static uint32_t m_passkey; static ble_gap_event_listener m_listener; - -public: static gap_event_handler m_customGapHandler; }; diff --git a/libesp32/NimBLE-Arduino/src/NimBLEEddystoneTLM.cpp b/libesp32/NimBLE-Arduino/src/NimBLEEddystoneTLM.cpp index 990a36f52..a07294265 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEEddystoneTLM.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEEddystoneTLM.cpp @@ -24,7 +24,9 @@ static const char LOG_TAG[] = "NimBLEEddystoneTLM"; - +/** + * @brief Construct a default EddystoneTLM beacon object. + */ NimBLEEddystoneTLM::NimBLEEddystoneTLM() { beaconUUID = 0xFEAA; m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE; @@ -35,34 +37,73 @@ NimBLEEddystoneTLM::NimBLEEddystoneTLM() { m_eddystoneData.tmil = 0; } // NimBLEEddystoneTLM + +/** + * @brief Retrieve the data that is being advertised. + * @return The advertised data. + */ std::string NimBLEEddystoneTLM::getData() { return std::string((char*) &m_eddystoneData, sizeof(m_eddystoneData)); } // getData + +/** + * @brief Get the UUID being advertised. + * @return The UUID advertised. + */ NimBLEUUID NimBLEEddystoneTLM::getUUID() { return NimBLEUUID(beaconUUID); } // getUUID + +/** + * @brief Get the version being advertised. + * @return The version number. + */ uint8_t NimBLEEddystoneTLM::getVersion() { return m_eddystoneData.version; } // getVersion + +/** + * @brief Get the battery voltage. + * @return The battery voltage. + */ uint16_t NimBLEEddystoneTLM::getVolt() { return ENDIAN_CHANGE_U16(m_eddystoneData.volt); } // getVolt + +/** + * @brief Get the temperature being advertised. + * @return The temperature value. + */ float NimBLEEddystoneTLM::getTemp() { return ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f; } // getTemp +/** + * @brief Get the count of advertisments sent. + * @return The number of advertisments. + */ uint32_t NimBLEEddystoneTLM::getCount() { return ENDIAN_CHANGE_U32(m_eddystoneData.advCount); } // getCount + +/** + * @brief Get the advertisment time. + * @return The advertisment time. + */ uint32_t NimBLEEddystoneTLM::getTime() { return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10; } // getTime + +/** + * @brief Get a string representation of the beacon. + * @return The string representation. + */ std::string NimBLEEddystoneTLM::toString() { std::string out = ""; uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil); @@ -113,8 +154,10 @@ std::string NimBLEEddystoneTLM::toString() { return out; } // toString + /** - * Set the raw data for the beacon record. + * @brief Set the raw data for the beacon advertisment. + * @param [in] data The raw data to advertise. */ void NimBLEEddystoneTLM::setData(const std::string &data) { if (data.length() != sizeof(m_eddystoneData)) { @@ -125,26 +168,56 @@ void NimBLEEddystoneTLM::setData(const std::string &data) { memcpy(&m_eddystoneData, data.data(), data.length()); } // setData + +/** + * @brief Set the UUID to advertise. + * @param [in] l_uuid The UUID. + */ void NimBLEEddystoneTLM::setUUID(const NimBLEUUID &l_uuid) { beaconUUID = l_uuid.getNative()->u16.value; } // setUUID + +/** + * @brief Set the version to advertise. + * @param [in] version The version number. + */ void NimBLEEddystoneTLM::setVersion(uint8_t version) { m_eddystoneData.version = version; } // setVersion + +/** + * @brief Set the battery voltage to advertise. + * @param [in] volt The voltage in millivolts. + */ void NimBLEEddystoneTLM::setVolt(uint16_t volt) { m_eddystoneData.volt = volt; } // setVolt + +/** + * @brief Set the temperature to advertise. + * @param [in] temp The temperature value. + */ void NimBLEEddystoneTLM::setTemp(float temp) { m_eddystoneData.temp = (uint16_t)temp; } // setTemp + +/** + * @brief Set the advertisment count. + * @param [in] advCount The advertisment number. + */ void NimBLEEddystoneTLM::setCount(uint32_t advCount) { m_eddystoneData.advCount = advCount; } // setCount + +/** + * @brief Set the advertisment time. + * @param [in] tmil The advertisment time in milliseconds. + */ void NimBLEEddystoneTLM::setTime(uint32_t tmil) { m_eddystoneData.tmil = tmil; } // setTime diff --git a/libesp32/NimBLE-Arduino/src/NimBLEEddystoneURL.cpp b/libesp32/NimBLE-Arduino/src/NimBLEEddystoneURL.cpp index ce7975958..7c3194c28 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEEddystoneURL.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEEddystoneURL.cpp @@ -21,6 +21,10 @@ static const char LOG_TAG[] = "NimBLEEddystoneURL"; + +/** + * @brief Construct a default EddystoneURL beacon object. + */ NimBLEEddystoneURL::NimBLEEddystoneURL() { beaconUUID = 0xFEAA; lengthURL = 0; @@ -29,22 +33,47 @@ NimBLEEddystoneURL::NimBLEEddystoneURL() { memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url)); } // BLEEddystoneURL + +/** + * @brief Retrieve the data that is being advertised. + * @return The advertised data. + */ std::string NimBLEEddystoneURL::getData() { return std::string((char*) &m_eddystoneData, sizeof(m_eddystoneData)); } // getData + +/** + * @brief Get the UUID being advertised. + * @return The UUID advertised. + */ NimBLEUUID NimBLEEddystoneURL::getUUID() { return NimBLEUUID(beaconUUID); } // getUUID + +/** + * @brief Get the transmit power being advertised. + * @return The transmit power. + */ int8_t NimBLEEddystoneURL::getPower() { return m_eddystoneData.advertisedTxPower; } // getPower + +/** + * @brief Get the raw URL being advertised. + * @return The raw URL. + */ std::string NimBLEEddystoneURL::getURL() { return std::string((char*) &m_eddystoneData.url, sizeof(m_eddystoneData.url)); } // getURL + +/** + * @brief Get the full URL being advertised. + * @return The full URL. + */ std::string NimBLEEddystoneURL::getDecodedURL() { std::string decodedURL = ""; @@ -123,7 +152,8 @@ std::string NimBLEEddystoneURL::getDecodedURL() { /** - * Set the raw data for the beacon record. + * @brief Set the raw data for the beacon advertisment. + * @param [in] data The raw data to advertise. */ void NimBLEEddystoneURL::setData(const std::string &data) { if (data.length() > sizeof(m_eddystoneData)) { @@ -136,14 +166,29 @@ void NimBLEEddystoneURL::setData(const std::string &data) { lengthURL = data.length() - (sizeof(m_eddystoneData) - sizeof(m_eddystoneData.url)); } // setData + +/** + * @brief Set the UUID to advertise. + * @param [in] l_uuid The UUID. + */ void NimBLEEddystoneURL::setUUID(const NimBLEUUID &l_uuid) { beaconUUID = l_uuid.getNative()->u16.value; } // setUUID + +/** + * @brief Set the transmit power to advertise. + * @param [in] advertisedTxPower The transmit power level. + */ void NimBLEEddystoneURL::setPower(int8_t advertisedTxPower) { m_eddystoneData.advertisedTxPower = advertisedTxPower; } // setPower + +/** + * @brief Set the URL to advertise. + * @param [in] url The URL. + */ void NimBLEEddystoneURL::setURL(const std::string &url) { if (url.length() > sizeof(m_eddystoneData.url)) { NIMBLE_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d", diff --git a/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp b/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp index 23089ca77..154206c73 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.cpp @@ -260,13 +260,12 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU /** - * @Get a pointer to the vector of found descriptors. - * @param [in] bool value to indicate if the current vector should be cleared and - * subsequently all descriptors for this characteristic retrieved from the peripheral. - * If false the vector will be returned with the currently stored descriptors, - * if the vector is empty it will retrieve all descriptors for this characteristic - * from the peripheral. - * @return a pointer to the vector of descriptors for this characteristic. + * @brief Get a pointer to the vector of found descriptors. + * @param [in] refresh If true the current descriptor vector will be cleared and\n + * all descriptors for this characteristic retrieved from the peripheral.\n + * If false the vector will be returned with the currently stored descriptors + * of this characteristic. + * @return A pointer to the vector of descriptors for this characteristic. */ std::vector* NimBLERemoteCharacteristic::getDescriptors(bool refresh) { if(refresh) { @@ -338,6 +337,7 @@ NimBLEUUID NimBLERemoteCharacteristic::getUUID() { /** * @brief Get the value of the remote characteristic. + * @param [in] timestamp A pointer to a time_t struct to store the time the value was read. * @return The value of the remote characteristic. */ std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) { @@ -355,6 +355,7 @@ std::string NimBLERemoteCharacteristic::getValue(time_t *timestamp) { /** * @brief Read an unsigned 16 bit value * @return The unsigned 16 bit value. + * @deprecated Use readValue(). */ uint16_t NimBLERemoteCharacteristic::readUInt16() { return readValue(); @@ -364,6 +365,7 @@ uint16_t NimBLERemoteCharacteristic::readUInt16() { /** * @brief Read an unsigned 32 bit value. * @return the unsigned 32 bit value. + * @deprecated Use readValue(). */ uint32_t NimBLERemoteCharacteristic::readUInt32() { return readValue(); @@ -373,6 +375,7 @@ uint32_t NimBLERemoteCharacteristic::readUInt32() { /** * @brief Read a byte value * @return The value as a byte + * @deprecated Use readValue(). */ uint8_t NimBLERemoteCharacteristic::readUInt8() { return readValue(); @@ -390,6 +393,7 @@ float NimBLERemoteCharacteristic::readFloat() { /** * @brief Read the value of the remote characteristic. + * @param [in] timestamp A pointer to a time_t struct to store the time the value was read. * @return The value of the remote characteristic. */ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) { @@ -458,7 +462,7 @@ std::string NimBLERemoteCharacteristic::readValue(time_t *timestamp) { /** * @brief Callback for characteristic read operation. - * @return 0 or error code. + * @return success == 0 or error code. */ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, @@ -498,12 +502,13 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle, /** * @brief Subscribe or unsubscribe for notifications or indications. - * @param [in] uint16_t val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications. - * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then no callback - * is performed for notifications. + * @param [in] val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications. + * @param [in] notifyCallback A callback to be invoked for a notification. + * @param [in] response If write response required set this to true. + * If NULL is provided then no callback is performed. * @return true if successful. */ -bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_callback notifyCallback) { +bool NimBLERemoteCharacteristic::setNotify(uint16_t val, notify_callback notifyCallback, bool response) { NIMBLE_LOGD(LOG_TAG, ">> setNotify(): %s, %02x", toString().c_str(), val); NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902)); @@ -522,43 +527,44 @@ bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_c /** * @brief Subscribe for notifications or indications. - * @param [in] bool if true, subscribe for notifications, false subscribe for indications. - * @param [in] bool if true, require a write response from the descriptor write operation. - * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then no callback - * is performed for notifications. + * @param [in] notifications If true, subscribe for notifications, false subscribe for indications. + * @param [in] notifyCallback A callback to be invoked for a notification. + * @param [in] response If true, require a write response from the descriptor write operation. + * If NULL is provided then no callback is performed. * @return true if successful. */ -bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, notify_callback notifyCallback) { +bool NimBLERemoteCharacteristic::subscribe(bool notifications, notify_callback notifyCallback, bool response) { if(notifications) { - return setNotify(0x01, response, notifyCallback); + return setNotify(0x01, notifyCallback, response); } else { - return setNotify(0x02, response, notifyCallback); + return setNotify(0x02, notifyCallback, response); } } // subscribe /** * @brief Unsubscribe for notifications or indications. - * @param [in] bool if true, require a write response from the descriptor write operation. + * @param [in] response bool if true, require a write response from the descriptor write operation. * @return true if successful. */ bool NimBLERemoteCharacteristic::unsubscribe(bool response) { - return setNotify(0x00, response); + return setNotify(0x00, nullptr, response); } // unsubscribe /** * @brief backward-compatibility method for subscribe/unsubscribe notifications/indications - * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we are - * unregistering for notifications. - * @param [in] bool if true, register for notifications, false register for indications. - * @param [in] bool if true, require a write response from the descriptor write operation. + * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we + * will unregister for notifications. + * @param [in] notifications If true, register for notifications, false register for indications. + * @param [in] response If true, require a write response from the descriptor write operation. * @return true if successful. + * @deprecated Use subscribe() / unsubscribe() instead. */ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool response) { bool success; if(notifyCallback != nullptr) { - success = subscribe(notifications, response, notifyCallback); + success = subscribe(notifications, notifyCallback, response); } else { success = unsubscribe(response); } @@ -568,10 +574,9 @@ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallbac /** * @brief Delete the descriptors in the descriptor vector. - * We maintain a vector called m_descriptorVector that contains pointers to BLERemoteDescriptors + * @details We maintain a vector called m_descriptorVector that contains pointers to NimBLERemoteDescriptors * object references. Since we allocated these in this class, we are also responsible for deleting * them. This method does just that. - * @return N/A. */ void NimBLERemoteCharacteristic::deleteDescriptors() { NIMBLE_LOGD(LOG_TAG, ">> deleteDescriptors"); @@ -587,7 +592,7 @@ void NimBLERemoteCharacteristic::deleteDescriptors() { /** * @brief Delete descriptor by UUID * @param [in] uuid The UUID of the descriptor to be deleted. - * @return Number of services left. + * @return Number of descriptors left in the vector. */ size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID &uuid) { NIMBLE_LOGD(LOG_TAG, ">> deleteDescriptor"); @@ -607,7 +612,7 @@ size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID &uuid) { /** - * @brief Convert a BLERemoteCharacteristic to a string representation; + * @brief Convert a NimBLERemoteCharacteristic to a string representation; * @return a String representation. */ std::string NimBLERemoteCharacteristic::toString() { @@ -725,7 +730,7 @@ bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length, /** * @brief Callback for characteristic write operation. - * @return 0 or error code. + * @return success == 0 or error code. */ int NimBLERemoteCharacteristic::onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error, diff --git a/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.h b/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.h index 364efb59d..720a4da29 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.h +++ b/libesp32/NimBLE-Arduino/src/NimBLERemoteCharacteristic.h @@ -24,13 +24,14 @@ #include "NimBLERemoteDescriptor.h" #include +#include class NimBLERemoteService; class NimBLERemoteDescriptor; -typedef void (*notify_callback)(NimBLERemoteCharacteristic* pBLERemoteCharacteristic, - uint8_t* pData, size_t length, bool isNotify); +typedef std::function notify_callback; typedef struct { const NimBLEUUID *uuid; @@ -63,6 +64,15 @@ public: NimBLEUUID getUUID(); std::string readValue(time_t *timestamp = nullptr); + /** + * @brief A template to convert the remote characteristic data to . + * @tparam T The type to convert the data to. + * @param [in] timestamp A pointer to a time_t struct to store the time the value was read. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: readValue(×tamp, skipSizeCheck); + */ template T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) { std::string value = readValue(timestamp); @@ -77,6 +87,15 @@ public: float readFloat() __attribute__ ((deprecated("Use template readValue()"))); std::string getValue(time_t *timestamp = nullptr); + /** + * @brief A template to convert the remote characteristic data to . + * @tparam T The type to convert the data to. + * @param [in] timestamp A pointer to a time_t struct to store the time the value was read. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: getValue(×tamp, skipSizeCheck); + */ template T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) { std::string value = getValue(timestamp); @@ -86,9 +105,9 @@ public: } bool subscribe(bool notifications = true, - bool response = true, - notify_callback notifyCallback = nullptr); - bool unsubscribe(bool response = true); + notify_callback notifyCallback = nullptr, + bool response = false); + bool unsubscribe(bool response = false); bool registerForNotify(notify_callback notifyCallback, bool notifications = true, bool response = true) @@ -98,6 +117,11 @@ public: bool response = false); bool writeValue(const std::string &newValue, bool response = false); + /** + * @brief Convenience template to set the remote characteristic value to val. + * @param [in] s The value to write. + * @param [in] response True == request write response. + */ template bool writeValue(const T &s, bool response = false) { return writeValue((uint8_t*)&s, sizeof(T), response); @@ -115,7 +139,7 @@ private: friend class NimBLERemoteDescriptor; // Private member functions - bool setNotify(uint16_t val, bool response = true, notify_callback notifyCallback = nullptr); + bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true); bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr); static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg); diff --git a/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.cpp b/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.cpp index d17ae6a0e..9281e7df7 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.cpp @@ -25,8 +25,8 @@ static const char* LOG_TAG = "NimBLERemoteDescriptor"; /** * @brief Remote descriptor constructor. - * @param [in] Reference to the Characteristic that this belongs to. - * @param [in] Reference to the struct that contains the descriptor information. + * @param [in] pRemoteCharacteristic A pointer to the Characteristic that this belongs to. + * @param [in] dsc A pointer to the struct that contains the descriptor information. */ NimBLERemoteDescriptor::NimBLERemoteDescriptor(NimBLERemoteCharacteristic* pRemoteCharacteristic, const struct ble_gatt_dsc *dsc) @@ -78,6 +78,11 @@ NimBLEUUID NimBLERemoteDescriptor::getUUID() { } // getUUID +/** + * @brief Read a byte value + * @return The value as a byte + * @deprecated Use readValue(). + */ uint8_t NimBLERemoteDescriptor::readUInt8() { std::string value = readValue(); if (value.length() >= 1) { @@ -87,6 +92,11 @@ uint8_t NimBLERemoteDescriptor::readUInt8() { } // readUInt8 +/** + * @brief Read an unsigned 16 bit value + * @return The unsigned 16 bit value. + * @deprecated Use readValue(). + */ uint16_t NimBLERemoteDescriptor::readUInt16() { std::string value = readValue(); if (value.length() >= 2) { @@ -96,6 +106,11 @@ uint16_t NimBLERemoteDescriptor::readUInt16() { } // readUInt16 +/** + * @brief Read an unsigned 32 bit value. + * @return the unsigned 32 bit value. + * @deprecated Use readValue(). + */ uint32_t NimBLERemoteDescriptor::readUInt32() { std::string value = readValue(); if (value.length() >= 4) { @@ -105,6 +120,10 @@ uint32_t NimBLERemoteDescriptor::readUInt32() { } // readUInt32 +/** + * @brief Read the value of the remote descriptor. + * @return The value of the remote descriptor. + */ std::string NimBLERemoteDescriptor::readValue() { NIMBLE_LOGD(LOG_TAG, ">> Descriptor readValue: %s", toString().c_str()); @@ -161,7 +180,7 @@ std::string NimBLERemoteDescriptor::readValue() { /** * @brief Callback for Descriptor read operation. - * @return 0 or error code. + * @return success == 0 or error code. */ int NimBLERemoteDescriptor::onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, @@ -200,8 +219,8 @@ int NimBLERemoteDescriptor::onReadCB(uint16_t conn_handle, /** - * @brief Return a string representation of this BLE Remote Descriptor. - * @retun A string representation of this BLE Remote Descriptor. + * @brief Return a string representation of this Remote Descriptor. + * @return A string representation of this Remote Descriptor. */ std::string NimBLERemoteDescriptor::toString() { std::string res = "Descriptor: uuid: " + getUUID().toString(); @@ -216,7 +235,7 @@ std::string NimBLERemoteDescriptor::toString() { /** * @brief Callback for descriptor write operation. - * @return 0 or error code. + * @return success == 0 or error code. */ int NimBLERemoteDescriptor::onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error, @@ -242,7 +261,8 @@ int NimBLERemoteDescriptor::onWriteCB(uint16_t conn_handle, * @brief Write data to the BLE Remote Descriptor. * @param [in] data The data to send to the remote descriptor. * @param [in] length The length of the data to send. - * @param [in] response True if we expect a response. + * @param [in] response True if we expect a write response. + * @return True if successful */ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool response) { @@ -322,11 +342,11 @@ bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool * @brief Write data represented as a string to the BLE Remote Descriptor. * @param [in] newValue The data to send to the remote descriptor. * @param [in] response True if we expect a response. + * @return True if successful */ bool NimBLERemoteDescriptor::writeValue(const std::string &newValue, bool response) { return writeValue((uint8_t*) newValue.data(), newValue.length(), response); } // writeValue - #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) #endif /* CONFIG_BT_ENABLED */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.h b/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.h index 554e9dc16..b52738ef7 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.h +++ b/libesp32/NimBLE-Arduino/src/NimBLERemoteDescriptor.h @@ -33,6 +33,14 @@ public: NimBLEUUID getUUID(); std::string readValue(); + /** + * @brief A template to convert the remote descriptor data to . + * @tparam T The type to convert the data to. + * @param [in] skipSizeCheck If true it will skip checking if the data size is less than sizeof(). + * @return The data converted to or NULL if skipSizeCheck is false and the data is + * less than sizeof(). + * @details Use: readValue(skipSizeCheck); + */ template T readValue(bool skipSizeCheck = false) { std::string value = readValue(); @@ -41,12 +49,18 @@ public: return *((T *)pData); } - uint8_t readUInt8() __attribute__ ((deprecated)); - uint16_t readUInt16() __attribute__ ((deprecated)); - uint32_t readUInt32() __attribute__ ((deprecated)); + uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue()"))); + uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue()"))); + uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue()"))); std::string toString(void); bool writeValue(const uint8_t* data, size_t length, bool response = false); bool writeValue(const std::string &newValue, bool response = false); + + /** + * @brief Convenience template to set the remote descriptor value to val. + * @param [in] s The value to write. + * @param [in] response True == request write response. + */ template bool writeValue(const T &s, bool response = false) { return writeValue((uint8_t*)&s, sizeof(T), response); diff --git a/libesp32/NimBLE-Arduino/src/NimBLERemoteService.cpp b/libesp32/NimBLE-Arduino/src/NimBLERemoteService.cpp index db9eabca1..8901175dc 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLERemoteService.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLERemoteService.cpp @@ -26,8 +26,8 @@ static const char* LOG_TAG = "NimBLERemoteService"; /** * @brief Remote Service constructor. - * @param [in] Reference to the client this belongs to. - * @param [in] Refernce to the structure with the services' information. + * @param [in] pClient A pointer to the client this belongs to. + * @param [in] service A pointer to the structure with the service information. */ NimBLERemoteService::NimBLERemoteService(NimBLEClient* pClient, const struct ble_gatt_svc* service) { @@ -55,7 +55,6 @@ NimBLERemoteService::NimBLERemoteService(NimBLEClient* pClient, const struct ble /** * @brief When deleting the service make sure we delete all characteristics and descriptors. - * Also release any semaphores they may be holding. */ NimBLERemoteService::~NimBLERemoteService() { deleteCharacteristics(); @@ -83,7 +82,7 @@ std::vector::iterator NimBLERemoteService::end() { /** * @brief Get the remote characteristic object for the characteristic UUID. * @param [in] uuid Remote characteristic uuid. - * @return Reference to the remote characteristic object. + * @return A pointer to the remote characteristic object. */ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* uuid) { return getCharacteristic(NimBLEUUID(uuid)); @@ -93,7 +92,7 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* u /** * @brief Get the characteristic object for the UUID. * @param [in] uuid Characteristic uuid. - * @return Reference to the characteristic object, or nullptr if not found. + * @return A pointer to the characteristic object, or nullptr if not found. */ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEUUID &uuid) { for(auto &it: m_characteristicVector) { @@ -114,15 +113,12 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU /** - * @Get a pointer to the vector of found characteristics. - * @param [in] bool value to indicate if the current vector should be cleared and - * subsequently all characteristics for this service retrieved from the peripheral. - * If false the vector will be returned with the currently stored characteristics, - * If true it will retrieve all characteristics of this service from the peripheral - * and return the vector with all characteristics for this service. - * @return a pointer to the vector of descriptors for this characteristic. + * @brief Get a pointer to the vector of found characteristics. + * @param [in] refresh If true the current characteristics vector will cleared and + * all characteristics for this service retrieved from the peripheral. + * If false the vector will be returned with the currently stored characteristics of this service. + * @return A pointer to the vector of descriptors for this characteristic. */ - std::vector* NimBLERemoteService::getCharacteristics(bool refresh) { if(refresh) { deleteCharacteristics(); @@ -140,6 +136,7 @@ std::vector* NimBLERemoteService::getCharacteristic /** * @brief Callback for Characterisic discovery. + * @return success == 0 or error code. */ int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle, const struct ble_gatt_error *error, @@ -182,7 +179,7 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle, /** * @brief Retrieve all the characteristics for this service. * This function will not return until we have all the characteristics. - * @return N/A + * @return True if successful. */ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID *uuid_filter) { NIMBLE_LOGD(LOG_TAG, ">> retrieveCharacteristics() for service: %s", getUUID().toString().c_str()); @@ -299,10 +296,9 @@ bool NimBLERemoteService::setValue(const NimBLEUUID &characteristicUuid, const s /** * @brief Delete the characteristics in the characteristics vector. - * We maintain a vector called m_characteristicsVector that contains pointers to BLERemoteCharacteristic + * @details We maintain a vector called m_characteristicsVector that contains pointers to BLERemoteCharacteristic * object references. Since we allocated these in this class, we are also responsible for deleting * them. This method does just that. - * @return N/A. */ void NimBLERemoteService::deleteCharacteristics() { NIMBLE_LOGD(LOG_TAG, ">> deleteCharacteristics"); @@ -316,7 +312,7 @@ void NimBLERemoteService::deleteCharacteristics() { /** * @brief Delete characteristic by UUID - * @param [in] uuid The UUID of the characteristic to be cleared. + * @param [in] uuid The UUID of the characteristic to be removed from the local database. * @return Number of characteristics left. */ size_t NimBLERemoteService::deleteCharacteristic(const NimBLEUUID &uuid) { diff --git a/libesp32/NimBLE-Arduino/src/NimBLERemoteService.h b/libesp32/NimBLE-Arduino/src/NimBLERemoteService.h index 2d63c5d70..751c9effb 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLERemoteService.h +++ b/libesp32/NimBLE-Arduino/src/NimBLERemoteService.h @@ -45,7 +45,7 @@ public: void deleteCharacteristics(); size_t deleteCharacteristic(const NimBLEUUID &uuid); NimBLEClient* getClient(void); - uint16_t getHandle(); + //uint16_t getHandle(); NimBLEUUID getUUID(void); std::string getValue(const NimBLEUUID &characteristicUuid); bool setValue(const NimBLEUUID &characteristicUuid, diff --git a/libesp32/NimBLE-Arduino/src/NimBLEScan.cpp b/libesp32/NimBLE-Arduino/src/NimBLEScan.cpp index dbf88741d..dc82de549 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEScan.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEScan.cpp @@ -44,6 +44,13 @@ NimBLEScan::NimBLEScan() { } +/** + * @brief Scan destructor, release any allocated resources. + */ +NimBLEScan::~NimBLEScan() { + clearResults(); +} + /** * @brief Handle GAP events related to scans. * @param [in] event The event type for this event. @@ -52,8 +59,6 @@ NimBLEScan::NimBLEScan() { /*STATIC*/int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) { NimBLEScan* pScan = (NimBLEScan*)arg; - struct ble_hs_adv_fields fields; - int rc = 0; switch(event->type) { @@ -63,13 +68,6 @@ NimBLEScan::NimBLEScan() { return 0; } - rc = ble_hs_adv_parse_fields(&fields, event->disc.data, - event->disc.length_data); - if (rc != 0) { - NIMBLE_LOGE(LOG_TAG, "Gap Event Parse ERROR."); - return 0; - } - NimBLEAddress advertisedAddress(event->disc.addr); // Examine our list of ignored addresses and stop processing if we don't want to see it or are already connected @@ -92,7 +90,6 @@ NimBLEScan::NimBLEScan() { // Otherwise just update the relevant parameters of the already known device. if(advertisedDevice == nullptr){ advertisedDevice = new NimBLEAdvertisedDevice(); - advertisedDevice->setAddressType(event->disc.addr.type); advertisedDevice->setAddress(advertisedAddress); advertisedDevice->setAdvType(event->disc.event_type); pScan->m_scanResults.m_advertisedDevicesVector.push_back(advertisedDevice); @@ -102,9 +99,9 @@ NimBLEScan::NimBLEScan() { NIMBLE_LOGI(LOG_TAG, "UPDATING PREVIOUSLY FOUND DEVICE: %s", advertisedAddress.toString().c_str()); } advertisedDevice->setRSSI(event->disc.rssi); - advertisedDevice->parseAdvertisement(&fields); - advertisedDevice->setScan(pScan); - advertisedDevice->setAdvertisementResult(event->disc.data, event->disc.length_data); + if(event->disc.length_data > 0) { + advertisedDevice->parseAdvertisement(event->disc.data, event->disc.length_data); + } advertisedDevice->m_timestamp = time(nullptr); if (pScan->m_pAdvertisedDeviceCallbacks) { @@ -151,7 +148,6 @@ NimBLEScan::NimBLEScan() { * @brief Should we perform an active or passive scan? * The default is a passive scan. An active scan means that we will wish a scan response. * @param [in] active If true, we perform an active scan otherwise a passive scan. - * @return N/A. */ void NimBLEScan::setActiveScan(bool active) { if (active) { @@ -165,40 +161,40 @@ void NimBLEScan::setActiveScan(bool active) { /** * @brief Set whether or not the BLE controller should only report results * from devices it has not already seen. - * @param [in] active If true, scanned devices will only be reported once. + * @param [in] enabled If true, scanned devices will only be reported once. * @details The controller has a limited buffer and will start reporting * dupicate devices once the limit is reached. */ -void NimBLEScan::setDuplicateFilter(bool active) { - m_scan_params.filter_duplicates = active; +void NimBLEScan::setDuplicateFilter(bool enabled) { + m_scan_params.filter_duplicates = enabled; } // setDuplicateFilter /** * @brief Set whether or not the BLE controller only report scan results * from devices advertising in limited discovery mode, i.e. directed advertising. - * @param [in] active If true, only limited discovery devices will be in scan results. + * @param [in] enabled If true, only limited discovery devices will be in scan results. */ -void NimBLEScan::setLimitedOnly(bool active) { - m_scan_params.limited = active; +void NimBLEScan::setLimitedOnly(bool enabled) { + m_scan_params.limited = enabled; } // setLimited /** * @brief Sets the scan filter policy. * @param [in] filter Can be one of: - * BLE_HCI_SCAN_FILT_NO_WL (0) - * Scanner processes all advertising packets (white list not used) except + * * BLE_HCI_SCAN_FILT_NO_WL (0) + * Scanner processes all advertising packets (white list not used) except\n * directed, connectable advertising packets not sent to the scanner. - * BLE_HCI_SCAN_FILT_USE_WL (1) - * Scanner processes advertisements from white list only. A connectable, + * * BLE_HCI_SCAN_FILT_USE_WL (1) + * Scanner processes advertisements from white list only. A connectable,\n * directed advertisment is ignored unless it contains scanners address. - * BLE_HCI_SCAN_FILT_NO_WL_INITA (2) - * Scanner process all advertising packets (white list not used). A + * * BLE_HCI_SCAN_FILT_NO_WL_INITA (2) + * Scanner process all advertising packets (white list not used). A\n * connectable, directed advertisement shall not be ignored if the InitA * is a resolvable private address. - * BLE_HCI_SCAN_FILT_USE_WL_INITA (3) - * Scanner process advertisements from white list only. A connectable, + * * BLE_HCI_SCAN_FILT_USE_WL_INITA (3) + * Scanner process advertisements from white list only. A connectable,\n * directed advertisement shall not be ignored if the InitA is a * resolvable private address. */ @@ -221,7 +217,7 @@ void NimBLEScan::setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks* p /** * @brief Set the interval to scan. - * @param [in] The interval in msecs. + * @param [in] intervalMSecs The scan interval (how often) in milliseconds. */ void NimBLEScan::setInterval(uint16_t intervalMSecs) { m_scan_params.itvl = intervalMSecs / 0.625; @@ -237,11 +233,20 @@ void NimBLEScan::setWindow(uint16_t windowMSecs) { } // setWindow +/** + * @brief Get the status of the scanner. + * @return true if scanning or scan starting. + */ +bool NimBLEScan::isScanning() { + return !m_stopped; +} + + /** * @brief Start scanning. * @param [in] duration The duration in seconds for which to scan. * @param [in] scanCompleteCB A function to be called when scanning has completed. - * @param [in] are we continue scan (true) or we want to clear stored devices (false) + * @param [in] is_continue Set to true to save previous scan results, false to clear them. * @return True if scan started or false if there was an error. */ bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResults), bool is_continue) { @@ -309,7 +314,8 @@ bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResul /** * @brief Start scanning and block until scanning has been completed. * @param [in] duration The duration in seconds for which to scan. - * @return The BLEScanResults. + * @param [in] is_continue Set to true to save previous scan results, false to clear them. + * @return The NimBLEScanResults. */ NimBLEScanResults NimBLEScan::start(uint32_t duration, bool is_continue) { if(duration == 0) { @@ -330,7 +336,7 @@ NimBLEScanResults NimBLEScan::start(uint32_t duration, bool is_continue) { /** * @brief Stop an in progress scan. - * @return N/A. + * @return True if successful. */ bool NimBLEScan::stop() { NIMBLE_LOGD(LOG_TAG, ">> stop()"); @@ -343,7 +349,7 @@ bool NimBLEScan::stop() { m_stopped = true; - if (m_scanCompleteCB != nullptr) { + if (rc != BLE_HS_EALREADY && m_scanCompleteCB != nullptr) { m_scanCompleteCB(m_scanResults); } @@ -356,7 +362,11 @@ bool NimBLEScan::stop() { } // stop -// delete peer device from cache after disconnecting, it is required in case we are connecting to devices with not public address +/** + * @brief Delete peer device from the scan results vector. + * @param [in] address The address of the device to delete from the results. + * @details After disconnecting, it may be required in the case we were connected to a device without a public address. + */ void NimBLEScan::erase(const NimBLEAddress &address) { NIMBLE_LOGI(LOG_TAG, "erase device: %s", address.toString().c_str()); @@ -371,8 +381,7 @@ void NimBLEScan::erase(const NimBLEAddress &address) { /** - * @brief If the host reset the scan will have stopped so we should flag it and release the semaphore. - * @return N/A. + * @brief If the host reset the scan will have stopped so we should set the flag as stopped. */ void NimBLEScan::onHostReset() { m_stopped = true; @@ -411,7 +420,7 @@ void NimBLEScanResults::dump() { /** - * @brief Return the count of devices found in the last scan. + * @brief Get the count of devices found in the last scan. * @return The number of devices found in the last scan. */ int NimBLEScanResults::getCount() { @@ -449,7 +458,7 @@ std::vector::iterator NimBLEScanResults::end() { /** - * @brief Return a pointer to the specified device at the given address. + * @brief Get a pointer to the specified device at the given address. * If the address is not found a nullptr is returned. * @param [in] address The address of the device. * @return A pointer to the device at the specified address. diff --git a/libesp32/NimBLE-Arduino/src/NimBLEScan.h b/libesp32/NimBLE-Arduino/src/NimBLEScan.h index 5bc7bcf35..822629025 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEScan.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEScan.h @@ -33,9 +33,9 @@ class NimBLEAdvertisedDeviceCallbacks; class NimBLEAddress; /** - * @brief The result of having performed a scan. - * When a scan completes, we have a set of found devices. Each device is described - * by a BLEAdvertisedDevice object. The number of items in the set is given by + * @brief A class that contains and operates on the results of a BLE scan. + * @details When a scan completes, we have a set of found devices. Each device is described + * by a NimBLEAdvertisedDevice object. The number of items in the set is given by * getCount(). We can retrieve a device by calling getDevice() passing in the * index (starting at 0) of the desired device. */ @@ -62,12 +62,13 @@ class NimBLEScan { public: bool start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResults), bool is_continue = false); NimBLEScanResults start(uint32_t duration, bool is_continue = false); + bool isScanning(); void setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates = false); void setActiveScan(bool active); void setInterval(uint16_t intervalMSecs); void setWindow(uint16_t windowMSecs); - void setDuplicateFilter(bool active); - void setLimitedOnly(bool active); + void setDuplicateFilter(bool enabled); + void setLimitedOnly(bool enabled); void setFilterPolicy(uint8_t filter); bool stop(); void clearResults(); @@ -76,8 +77,10 @@ public: private: - NimBLEScan(); friend class NimBLEDevice; + + NimBLEScan(); + ~NimBLEScan(); static int handleGapEvent(ble_gap_event* event, void* arg); void onHostReset(); diff --git a/libesp32/NimBLE-Arduino/src/NimBLESecurity.cpp b/libesp32/NimBLE-Arduino/src/NimBLESecurity.cpp index 8a0dbd952..aa0629698 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLESecurity.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLESecurity.cpp @@ -18,12 +18,6 @@ #include "NimBLESecurity.h" #include "NimBLEDevice.h" -/** - * @brief This class is for backward compatibility with the bluedroid based library. - * Use the new security functions in NimBLEDevice instead. - * New callback functions in NimBLEServer and NimBLEClient. - */ - NimBLESecurity::NimBLESecurity() { } @@ -33,6 +27,15 @@ NimBLESecurity::~NimBLESecurity() { /** * @brief Set requested authentication mode + * @param [in] auth_req A bitmask containing one or more of: + * * ESP_LE_AUTH_NO_BOND 0x00 + * * ESP_LE_AUTH_BOND 0x01 + * * ESP_LE_AUTH_REQ_MITM (1 << 2) + * * ESP_LE_AUTH_REQ_BOND_MITM (ESP_LE_AUTH_BOND | ESP_LE_AUTH_REQ_MITM) + * * ESP_LE_AUTH_REQ_SC_ONLY (1 << 3) + * * ESP_LE_AUTH_REQ_SC_BOND (ESP_LE_AUTH_BOND | ESP_LE_AUTH_REQ_SC_ONLY) + * * ESP_LE_AUTH_REQ_SC_MITM (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY) + * * ESP_LE_AUTH_REQ_SC_MITM_BOND (ESP_LE_AUTH_REQ_MITM | ESP_LE_AUTH_REQ_SC_ONLY | ESP_LE_AUTH_BOND) */ void NimBLESecurity::setAuthenticationMode(esp_ble_auth_req_t auth_req) { NimBLEDevice::setSecurityAuth((auth_req & BLE_SM_PAIR_AUTHREQ_BOND)>0, @@ -42,8 +45,15 @@ void NimBLESecurity::setAuthenticationMode(esp_ble_auth_req_t auth_req) { /** - * @brief Set our device IO capability to let end user perform authorization - * either by displaying or entering generated 6-digits pin code + * @brief Set our device IO capability to let end user perform authorization + * either by displaying or entering generated 6-digit pin code or use \"just works\". + * @param [in] iocap The IO capabilites our device has.\n + * Can be set to one of: + * * ESP_IO_CAP_OUT 0 + * * ESP_IO_CAP_IO 1 + * * ESP_IO_CAP_IN 2 + * * ESP_IO_CAP_NONE 3 + * * ESP_IO_CAP_KBDISP 4 */ void NimBLESecurity::setCapability(esp_ble_io_cap_t iocap) { NimBLEDevice::setSecurityIOCap(iocap); @@ -51,8 +61,13 @@ void NimBLESecurity::setCapability(esp_ble_io_cap_t iocap) { /** - * @brief Init encryption key by server - * @param key_size is value between 7 and 16 + * @brief Sets the keys we will distibute during encryption. + * @param [in] init_key A bitmask of the keys we will distibute.\n + * Can be one or more of: + * * ESP_BLE_ENC_KEY_MASK (1 << 0) + * * ESP_BLE_ID_KEY_MASK (1 << 1) + * * ESP_BLE_CSR_KEY_MASK (1 << 2) + * * ESP_BLE_LINK_KEY_MASK (1 << 3) */ void NimBLESecurity::setInitEncryptionKey(uint8_t init_key) { NimBLEDevice::setSecurityInitKey(init_key); @@ -60,8 +75,13 @@ void NimBLESecurity::setInitEncryptionKey(uint8_t init_key) { /** - * @brief Init encryption key by client - * @param key_size is value between 7 and 16 + * @brief Sets the keys we will accept during encryption. + * @param [in] resp_key A bitmask of the keys we will accept.\n + * Can be one or more of: + * * ESP_BLE_ENC_KEY_MASK (1 << 0) + * * ESP_BLE_ID_KEY_MASK (1 << 1) + * * ESP_BLE_CSR_KEY_MASK (1 << 2) + * * ESP_BLE_LINK_KEY_MASK (1 << 3) */ void NimBLESecurity::setRespEncryptionKey(uint8_t resp_key) { NimBLEDevice::setSecurityRespKey(resp_key); @@ -70,7 +90,6 @@ void NimBLESecurity::setRespEncryptionKey(uint8_t resp_key) { /** *@todo Requires implementation - * */ void NimBLESecurity::setKeySize(uint8_t key_size) { @@ -80,7 +99,8 @@ void NimBLESecurity::setKeySize(uint8_t key_size) { /** - * Setup for static PIN connection. + * @brief Sets a static PIN used to authenticate/encrypt the connection. + * @param [in] pin The 6 digit pin code to accept. */ void NimBLESecurity::setStaticPIN(uint32_t pin){ //uint32_t passkey = pin; diff --git a/libesp32/NimBLE-Arduino/src/NimBLESecurity.h b/libesp32/NimBLE-Arduino/src/NimBLESecurity.h index 50c732c9b..5a7619f45 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLESecurity.h +++ b/libesp32/NimBLE-Arduino/src/NimBLESecurity.h @@ -12,10 +12,6 @@ * Author: chegewara */ -/** This class exists for backward compatibility - Should not be used in new code - * See the security functions in NimBLEDevice and callbacks in NimBLEServer / NimBLEClient - */ - #ifndef COMPONENTS_NIMBLESECURITY_H_ #define COMPONENTS_NIMBLESECURITY_H_ #include "sdkconfig.h" @@ -56,6 +52,12 @@ typedef uint8_t esp_ble_auth_req_t; /*!< combination of the above bit pattern */ typedef uint8_t esp_ble_io_cap_t; /*!< combination of the io capability */ + +/** + * @brief A class to handle BLE security operations. + * Deprecated - provided for backward compatibility only. + * @deprecated Use the security methods provided in NimBLEDevice instead. + */ class NimBLESecurity { public: NimBLESecurity(); @@ -78,8 +80,10 @@ private: }; // BLESecurity -/* - * @brief Callbacks to handle GAP events related to authorization +/** + * @brief Callbacks to handle GAP events related to authorization. + * Deprecated - provided for backward compatibility only. + * @deprecated Use the callbacks provided in NimBLEClientCallbacks and NimBLEServerCallbacks instead. */ class NimBLESecurityCallbacks { public: @@ -95,21 +99,25 @@ public: /** * @brief Provide us 6-digits code to perform authentication. * It requires that our device is capable to display this code to end user - * @param + * @param [in] pass_key The PIN provided by the peer. */ virtual void onPassKeyNotify(uint32_t pass_key) = 0; /** * @brief Here we can make decision if we want to let negotiate authorization with peer device or not - * return Return true if we accept this peer device request + * @return Return true if we accept this peer device request */ - virtual bool onSecurityRequest() = 0 ; /** - * Provide us information when authentication process is completed + * @brief Provides us information when authentication process is completed */ virtual void onAuthenticationComplete(ble_gap_conn_desc*) = 0; + /** + * @brief Called when using numeric comparison for authentication. + * @param [in] pin The PIN to compare. + * @return True to accept and pair. + */ virtual bool onConfirmPIN(uint32_t pin) = 0; }; // BLESecurityCallbacks diff --git a/libesp32/NimBLE-Arduino/src/NimBLEServer.cpp b/libesp32/NimBLE-Arduino/src/NimBLEServer.cpp index 376b02144..8c75192a9 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEServer.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEServer.cpp @@ -22,6 +22,10 @@ #include "NimBLEDevice.h" #include "NimBLELog.h" +#include "services/gap/ble_svc_gap.h" +#include "services/gatt/ble_svc_gatt.h" + + static const char* LOG_TAG = "NimBLEServer"; static NimBLEServerCallbacks defaultCallbacks; @@ -37,9 +41,25 @@ NimBLEServer::NimBLEServer() { m_pServerCallbacks = &defaultCallbacks; m_gattsStarted = false; m_advertiseOnDisconnect = true; + m_svcChanged = false; + m_deleteCallbacks = true; } // NimBLEServer +/** + * @brief Destructor: frees all resources / attributes created. + */ +NimBLEServer::~NimBLEServer() { + for(auto &it : m_svcVec) { + delete it; + } + + if(m_deleteCallbacks && m_pServerCallbacks != &defaultCallbacks) { + delete m_pServerCallbacks; + } +} + + /** * @brief Create a %BLE Service. * @param [in] uuid The UUID of the new service. @@ -71,6 +91,12 @@ NimBLEService* NimBLEServer::createService(const NimBLEUUID &uuid, uint32_t numH NimBLEService* pService = new NimBLEService(uuid, numHandles, this); m_svcVec.push_back(pService); // Save a reference to this service being on this server. + if(m_gattsStarted) { + ble_svc_gatt_changed(0x0001, 0xffff); + m_svcChanged = true; + resetGATT(); + } + NIMBLE_LOGD(LOG_TAG, "<< createService"); return pService; } // createService @@ -146,17 +172,21 @@ void NimBLEServer::start() { NIMBLE_LOGI(LOG_TAG, "Service changed characterisic handle: %d", m_svcChgChrHdl); */ - // Build a vector of characteristics with Notify / Indicate capabilities for event handling + // Get the assigned service handles and build a vector of characteristics + // with Notify / Indicate capabilities for event handling for(auto &svc : m_svcVec) { + if(svc->m_removed == 0) { + rc = ble_gatts_find_svc(&svc->getUUID().getNative()->u, &svc->m_handle); + if(rc != 0) { + abort(); + } + } + for(auto &chr : svc->m_chrVec) { // if Notify / Indicate is enabled but we didn't create the descriptor // we do it now. if((chr->m_properties & BLE_GATT_CHR_F_INDICATE) || (chr->m_properties & BLE_GATT_CHR_F_NOTIFY)) { - - if(nullptr == chr->getDescriptorByUUID(uint16_t(0x2902))) { - chr->createDescriptor(uint16_t(0x2902)); - } m_notifyChrVec.push_back(chr); } } @@ -168,8 +198,8 @@ void NimBLEServer::start() { /** * @brief Disconnect the specified client with optional reason. - * @param [in] Connection Id of the client to disconnect. - * @param [in] Reason code for disconnecting. + * @param [in] connId Connection Id of the client to disconnect. + * @param [in] reason code for disconnecting. * @return NimBLE host return code. */ int NimBLEServer::disconnect(uint16_t connId, uint8_t reason) { @@ -188,7 +218,7 @@ int NimBLEServer::disconnect(uint16_t connId, uint8_t reason) { /** * @brief Set the server to automatically start advertising when a client disconnects. - * @param [in] bool true == advertise, false == don't advertise. + * @param [in] aod true == advertise, false == don't advertise. */ void NimBLEServer::advertiseOnDisconnect(bool aod) { m_advertiseOnDisconnect = aod; @@ -260,6 +290,11 @@ size_t NimBLEServer::getConnectedCount() { server->m_connectedPeersVec.end(), event->disconnect.conn.conn_handle), server->m_connectedPeersVec.end()); + + if(server->m_svcChanged) { + server->resetGATT(); + } + server->m_pServerCallbacks->onDisconnect(server); if(server->m_advertiseOnDisconnect) { @@ -269,9 +304,9 @@ size_t NimBLEServer::getConnectedCount() { } // BLE_GAP_EVENT_DISCONNECT case BLE_GAP_EVENT_SUBSCRIBE: { - NIMBLE_LOGI(LOG_TAG, "subscribe event; cur_notify=%d\n value handle; " - "val_handle=%d\n", - event->subscribe.cur_notify, event->subscribe.attr_handle); + NIMBLE_LOGI(LOG_TAG, "subscribe event; attr_handle=%d, subscribed: %s", + event->subscribe.attr_handle, + (event->subscribe.cur_notify ? "true":"false")); for(auto &it : server->m_notifyChrVec) { if(it->getHandle() == event->subscribe.attr_handle) { @@ -318,6 +353,12 @@ size_t NimBLEServer::getConnectedCount() { return 0; } // BLE_GAP_EVENT_NOTIFY_TX + case BLE_GAP_EVENT_ADV_COMPLETE: { + NIMBLE_LOGD(LOG_TAG, "Advertising Complete"); + NimBLEDevice::getAdvertising()->advCompleteCB(); + return 0; + } + case BLE_GAP_EVENT_CONN_UPDATE: { NIMBLE_LOGD(LOG_TAG, "Connection parameters updated."); return 0; @@ -435,16 +476,121 @@ size_t NimBLEServer::getConnectedCount() { * events are detected. * * @param [in] pCallbacks The callbacks to be invoked. + * @param [in] deleteCallbacks if true callback class will be deleted when server is destructed. */ -void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks) { +void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks, bool deleteCallbacks) { if (pCallbacks != nullptr){ m_pServerCallbacks = pCallbacks; + m_deleteCallbacks = deleteCallbacks; } else { m_pServerCallbacks = &defaultCallbacks; } } // setCallbacks +/** + * @brief Remove a service from the server. + * + * @details Immediately removes access to the service by clients, sends a service changed indication, + * and removes the service (if applicable) from the advertisments. + * The service is not deleted unless the deleteSvc parameter is true, otherwise the service remains + * available and can be re-added in the future. If desired a removed but not deleted service can + * be deleted later by calling this method with deleteSvc set to true. + * + * @note The service will not be removed from the database until all open connections are closed + * as it requires resetting the GATT server. In the interim the service will have it's visibility disabled. + * + * @note Advertising will need to be restarted by the user after calling this as we must stop + * advertising in order to remove the service. + * + * @param [in] service The service object to remove. + * @param [in] deleteSvc true if the service should be deleted. + */ +void NimBLEServer::removeService(NimBLEService* service, bool deleteSvc) { + // Check if the service was already removed and if so check if this + // is being called to delete the object and do so if requested. + // Otherwise, ignore the call and return. + if(service->m_removed > 0) { + if(deleteSvc) { + for(auto it = m_svcVec.begin(); it != m_svcVec.end(); ++it) { + if ((*it)->getUUID() == service->getUUID()) { + delete *it; + m_svcVec.erase(it); + break; + } + } + } + + return; + } + + int rc = ble_gatts_svc_set_visibility(service->getHandle(), 0); + if(rc !=0) { + return; + } + + service->m_removed = deleteSvc ? 2 : 1; + m_svcChanged = true; + + ble_svc_gatt_changed(0x0001, 0xffff); + resetGATT(); + NimBLEDevice::getAdvertising()->removeServiceUUID(service->getUUID()); +} + + +/** + * @brief Adds a service which was already created, but removed from availability. + * @param [in] service The service object to add. + * @note If it is desired to advertise the service it must be added by + * calling NimBLEAdvertising::addServiceUUID. + */ +void NimBLEServer::addService(NimBLEService* service) { + // If adding a service that was not removed just return. + if(service->m_removed == 0) { + return; + } + + service->m_removed = 0; + m_svcChanged = true; + + ble_svc_gatt_changed(0x0001, 0xffff); + resetGATT(); +} + + +/** + * @brief Resets the GATT server, used when services are added/removed after initialization. + */ +void NimBLEServer::resetGATT() { + if(getConnectedCount() > 0) { + return; + } + + NimBLEDevice::stopAdvertising(); + ble_gatts_reset(); + ble_svc_gap_init(); + ble_svc_gatt_init(); + + for(auto it = m_svcVec.begin(); it != m_svcVec.end(); ) { + if ((*it)->m_removed > 0) { + if ((*it)->m_removed == 2) { + delete *it; + it = m_svcVec.erase(it); + } else { + ++it; + } + continue; + } + + (*it)->start(); + ++it; + } + + m_svcChanged = false; + m_gattsStarted = false; +} + + /** * @brief Start advertising. * @@ -516,7 +662,7 @@ uint32_t NimBLEServerCallbacks::onPassKeyRequest(){ NIMBLE_LOGD("NimBLEServerCallbacks", "onPassKeyRequest: default: 123456"); return 123456; } - +/* void NimBLEServerCallbacks::onPassKeyNotify(uint32_t pass_key){ NIMBLE_LOGD("NimBLEServerCallbacks", "onPassKeyNotify: default: %d", pass_key); } @@ -525,6 +671,7 @@ bool NimBLEServerCallbacks::onSecurityRequest(){ NIMBLE_LOGD("NimBLEServerCallbacks", "onSecurityRequest: default: true"); return true; } +*/ void NimBLEServerCallbacks::onAuthenticationComplete(ble_gap_conn_desc*){ NIMBLE_LOGD("NimBLEServerCallbacks", "onAuthenticationComplete: default"); } diff --git a/libesp32/NimBLE-Arduino/src/NimBLEServer.h b/libesp32/NimBLE-Arduino/src/NimBLEServer.h index cfaa8acc7..1fa24b23c 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEServer.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEServer.h @@ -41,8 +41,11 @@ public: NimBLEService* createService(const char* uuid); NimBLEService* createService(const NimBLEUUID &uuid, uint32_t numHandles=15, uint8_t inst_id=0); + void removeService(NimBLEService* service, bool deleteSvc = false); + void addService(NimBLEService* service); NimBLEAdvertising* getAdvertising(); - void setCallbacks(NimBLEServerCallbacks* pCallbacks); + void setCallbacks(NimBLEServerCallbacks* pCallbacks, + bool deleteCallbacks = true); void startAdvertising(); void stopAdvertising(); void start(); @@ -54,18 +57,21 @@ public: uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout); uint16_t getPeerMTU(uint16_t conn_id); - std::vector getPeerDevices(); +// std::vector getPeerDevices(); void advertiseOnDisconnect(bool); private: NimBLEServer(); + ~NimBLEServer(); friend class NimBLECharacteristic; friend class NimBLEDevice; friend class NimBLEAdvertising; bool m_gattsStarted; bool m_advertiseOnDisconnect; + bool m_svcChanged; NimBLEServerCallbacks* m_pServerCallbacks; + bool m_deleteCallbacks; std::vector m_connectedPeersVec; // uint16_t m_svcChgChrHdl; // Future use @@ -74,6 +80,7 @@ private: std::vector m_notifyChrVec; static int handleGapEvent(struct ble_gap_event *event, void *arg); + void resetGATT(); }; // NimBLEServer @@ -83,29 +90,52 @@ private: class NimBLEServerCallbacks { public: virtual ~NimBLEServerCallbacks() {}; + /** - * @brief Handle a new client connection. - * - * When a new client connects, we are invoked. - * - * @param [in] pServer A reference to the %BLE server that received the client connection. + * @brief Handle a client connection. + * This is called when a client connects. + * @param [in] pServer A pointer to the %BLE server that received the client connection. */ virtual void onConnect(NimBLEServer* pServer); - virtual void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc); + /** - * @brief Handle an existing client disconnection. - * - * When an existing client disconnects, we are invoked. - * + * @brief Handle a client connection. + * This is called when a client connects. + * @param [in] pServer A pointer to the %BLE server that received the client connection. + * @param [in] desc A pointer to the connection description structure containig information + * about the connection parameters. + */ + virtual void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc); + + /** + * @brief Handle a client disconnection. + * This is called when a client disconnects. * @param [in] pServer A reference to the %BLE server that received the existing client disconnection. */ virtual void onDisconnect(NimBLEServer* pServer); - virtual uint32_t onPassKeyRequest(); //{return 0;} - virtual void onPassKeyNotify(uint32_t pass_key); //{} - virtual bool onSecurityRequest(); //{return true;} - virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);//{}; - virtual bool onConfirmPIN(uint32_t pin);//{return true;} + /** + * @brief Called when a client requests a passkey for pairing. + * @return The passkey to be sent to the client. + */ + virtual uint32_t onPassKeyRequest(); + + //virtual void onPassKeyNotify(uint32_t pass_key); + //virtual bool onSecurityRequest(); + + /** + * @brief Called when the pairing procedure is complete. + * @param [in] desc A pointer to the struct containing the connection information.\n + * This can be used to check the status of the connection encryption/pairing. + */ + virtual void onAuthenticationComplete(ble_gap_conn_desc* desc); + + /** + * @brief Called when using numeric comparision for pairing. + * @param [in] pin The pin to compare with the client. + * @return True to accept the pin. + */ + virtual bool onConfirmPIN(uint32_t pin); }; // NimBLEServerCallbacks diff --git a/libesp32/NimBLE-Arduino/src/NimBLEService.cpp b/libesp32/NimBLE-Arduino/src/NimBLEService.cpp index c2631ab36..3420da2c7 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEService.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEService.cpp @@ -49,13 +49,35 @@ NimBLEService::NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer * @param [in] a pointer to the server instance that this service belongs to. */ NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLEServer* pServer) { - m_uuid = uuid; - m_handle = NULL_HANDLE; - m_pServer = pServer; - m_numHandles = numHandles; + m_uuid = uuid; + m_handle = NULL_HANDLE; + m_pServer = pServer; + m_numHandles = numHandles; + m_pSvcDef = nullptr; + m_removed = 0; + } // NimBLEService +NimBLEService::~NimBLEService() { + if(m_pSvcDef != nullptr) { + if(m_pSvcDef->characteristics != nullptr) { + for(int i=0; m_pSvcDef->characteristics[i].uuid != NULL; ++i) { + if(m_pSvcDef->characteristics[i].descriptors) { + delete(m_pSvcDef->characteristics[i].descriptors); + } + } + delete(m_pSvcDef->characteristics); + } + + delete(m_pSvcDef); + } + + for(auto &it : m_chrVec) { + delete it; + } +} + /** * @brief Dump details of this BLE GATT service. * @return N/A. @@ -94,98 +116,84 @@ NimBLEUUID NimBLEService::getUUID() { * and registers it with the NimBLE stack. * @return bool success/failure . */ - bool NimBLEService::start() { NIMBLE_LOGD(LOG_TAG, ">> start(): Starting service: %s", toString().c_str()); int rc = 0; - // Nimble requires an array of services to be sent to the api - // Since we are adding 1 at a time we create an array of 2 and set the type - // of the second service to 0 to indicate the end of the array. - ble_gatt_svc_def* svc = new ble_gatt_svc_def[2]; - ble_gatt_chr_def* pChr_a = nullptr; - ble_gatt_dsc_def* pDsc_a = nullptr; - svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY; - svc[0].uuid = &m_uuid.getNative()->u; - svc[0].includes = NULL; + if(m_pSvcDef == nullptr) { + // Nimble requires an array of services to be sent to the api + // Since we are adding 1 at a time we create an array of 2 and set the type + // of the second service to 0 to indicate the end of the array. + ble_gatt_svc_def* svc = new ble_gatt_svc_def[2]; + ble_gatt_chr_def* pChr_a = nullptr; + ble_gatt_dsc_def* pDsc_a = nullptr; - size_t numChrs = m_chrVec.size(); + svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY; + svc[0].uuid = &m_uuid.getNative()->u; + svc[0].includes = NULL; - NIMBLE_LOGD(LOG_TAG,"Adding %d characteristics for service %s", numChrs, toString().c_str()); + size_t numChrs = m_chrVec.size(); - if(!numChrs){ - svc[0].characteristics = NULL; - }else{ - // Nimble requires the last characteristic to have it's uuid = 0 to indicate the end - // of the characteristics for the service. We create 1 extra and set it to null - // for this purpose. - pChr_a = new ble_gatt_chr_def[numChrs+1]; - NimBLECharacteristic* pCharacteristic = *m_chrVec.begin(); + NIMBLE_LOGD(LOG_TAG,"Adding %d characteristics for service %s", numChrs, toString().c_str()); - for(uint8_t i=0; i < numChrs;) { - uint8_t numDscs = pCharacteristic->m_dscVec.size(); - if(numDscs) { - // skip 2902 as it's automatically created by NimBLE - // if Indicate or Notify flags are set - if(((pCharacteristic->m_properties & BLE_GATT_CHR_F_INDICATE) || - (pCharacteristic->m_properties & BLE_GATT_CHR_F_NOTIFY)) && - pCharacteristic->getDescriptorByUUID("2902") != nullptr) - { - numDscs--; - } - } + if(!numChrs){ + svc[0].characteristics = NULL; + }else{ + // Nimble requires the last characteristic to have it's uuid = 0 to indicate the end + // of the characteristics for the service. We create 1 extra and set it to null + // for this purpose. + pChr_a = new ble_gatt_chr_def[numChrs+1]; + NimBLECharacteristic* pCharacteristic = *m_chrVec.begin(); - if(!numDscs){ - pChr_a[i].descriptors = NULL; - } else { - // Must have last descriptor uuid = 0 so we have to create 1 extra - //NIMBLE_LOGD(LOG_TAG, "Adding %d descriptors", numDscs); - pDsc_a = new ble_gatt_dsc_def[numDscs+1]; - NimBLEDescriptor* pDescriptor = *pCharacteristic->m_dscVec.begin(); - for(uint8_t d=0; d < numDscs;) { - // skip 2902 - if(pDescriptor->m_uuid == NimBLEUUID(uint16_t(0x2902))) { - //NIMBLE_LOGD(LOG_TAG, "Skipped 0x2902"); - pDescriptor = *(pCharacteristic->m_dscVec.begin()+d+1); - continue; + for(uint8_t i=0; i < numChrs;) { + uint8_t numDscs = pCharacteristic->m_dscVec.size(); + + if(!numDscs){ + pChr_a[i].descriptors = NULL; + } else { + // Must have last descriptor uuid = 0 so we have to create 1 extra + pDsc_a = new ble_gatt_dsc_def[numDscs+1]; + NimBLEDescriptor* pDescriptor = *pCharacteristic->m_dscVec.begin(); + for(uint8_t d=0; d < numDscs;) { + pDsc_a[d].uuid = &pDescriptor->m_uuid.getNative()->u; + pDsc_a[d].att_flags = pDescriptor->m_properties; + pDsc_a[d].min_key_size = 0; + pDsc_a[d].access_cb = NimBLEDescriptor::handleGapEvent; + pDsc_a[d].arg = pDescriptor; + d++; + pDescriptor = *(pCharacteristic->m_dscVec.begin() + d); } - pDsc_a[d].uuid = &pDescriptor->m_uuid.getNative()->u; - pDsc_a[d].att_flags = pDescriptor->m_properties; - pDsc_a[d].min_key_size = 0; - pDsc_a[d].access_cb = NimBLEDescriptor::handleGapEvent; - pDsc_a[d].arg = pDescriptor; - d++; - pDescriptor = *(pCharacteristic->m_dscVec.begin() + d); + + pDsc_a[numDscs].uuid = NULL; + pChr_a[i].descriptors = pDsc_a; } - pDsc_a[numDscs].uuid = NULL; - pChr_a[i].descriptors = pDsc_a; + pChr_a[i].uuid = &pCharacteristic->m_uuid.getNative()->u; + pChr_a[i].access_cb = NimBLECharacteristic::handleGapEvent; + pChr_a[i].arg = pCharacteristic; + pChr_a[i].flags = pCharacteristic->m_properties; + pChr_a[i].min_key_size = 0; + pChr_a[i].val_handle = &pCharacteristic->m_handle; + i++; + pCharacteristic = *(m_chrVec.begin() + i); } - pChr_a[i].uuid = &pCharacteristic->m_uuid.getNative()->u; - pChr_a[i].access_cb = NimBLECharacteristic::handleGapEvent; - pChr_a[i].arg = pCharacteristic; - pChr_a[i].flags = pCharacteristic->m_properties; - pChr_a[i].min_key_size = 0; - pChr_a[i].val_handle = &pCharacteristic->m_handle; - i++; - pCharacteristic = *(m_chrVec.begin() + i); + pChr_a[numChrs].uuid = NULL; + svc[0].characteristics = pChr_a; } - pChr_a[numChrs].uuid = NULL; - svc[0].characteristics = pChr_a; + // end of services must indicate to api with type = 0 + svc[1].type = 0; + m_pSvcDef = svc; } - // end of services must indicate to api with type = 0 - svc[1].type = 0; - - rc = ble_gatts_count_cfg((const ble_gatt_svc_def*)svc); + rc = ble_gatts_count_cfg((const ble_gatt_svc_def*)m_pSvcDef); if (rc != 0) { NIMBLE_LOGE(LOG_TAG, "ble_gatts_count_cfg failed, rc= %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); return false; } - rc = ble_gatts_add_svcs((const ble_gatt_svc_def*)svc); + rc = ble_gatts_add_svcs((const ble_gatt_svc_def*)m_pSvcDef); if (rc != 0) { NIMBLE_LOGE(LOG_TAG, "ble_gatts_add_svcs, rc= %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); return false; @@ -238,11 +246,21 @@ NimBLECharacteristic* NimBLEService::createCharacteristic(const NimBLEUUID &uuid } // createCharacteristic +/** + * @brief Get a pointer to the characteristic object with the specified UUID. + * @param [in] uuid The UUID of the characteristic. + * @return A pointer to the characteristic object or nullptr if not found. + */ NimBLECharacteristic* NimBLEService::getCharacteristic(const char* uuid) { return getCharacteristic(NimBLEUUID(uuid)); } +/** + * @brief Get a pointer to the characteristic object with the specified UUID. + * @param [in] uuid The UUID of the characteristic. + * @return A pointer to the characteristic object or nullptr if not found. + */ NimBLECharacteristic* NimBLEService::getCharacteristic(const NimBLEUUID &uuid) { for (auto &it : m_chrVec) { if (it->getUUID() == uuid) { diff --git a/libesp32/NimBLE-Arduino/src/NimBLEService.h b/libesp32/NimBLE-Arduino/src/NimBLEService.h index 4c1fa2adf..191d97fa5 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEService.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEService.h @@ -57,6 +57,7 @@ public: private: NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer* pServer); NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLEServer* pServer); + ~NimBLEService(); friend class NimBLEServer; friend class NimBLEDevice; @@ -65,7 +66,8 @@ private: NimBLEServer* m_pServer; NimBLEUUID m_uuid; uint16_t m_numHandles; - + ble_gatt_svc_def* m_pSvcDef; + uint8_t m_removed; std::vector m_chrVec; }; // NimBLEService diff --git a/libesp32/NimBLE-Arduino/src/NimBLEUUID.cpp b/libesp32/NimBLE-Arduino/src/NimBLEUUID.cpp index 4a9d7e876..1b00a3237 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEUUID.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEUUID.cpp @@ -73,15 +73,11 @@ static const char* LOG_TAG = "NimBLEUUID"; /** * @brief Create a UUID from 16 bytes of memory. - * * @param [in] pData The pointer to the start of the UUID. * @param [in] size The size of the data. * @param [in] msbFirst Is the MSB first in pData memory? */ NimBLEUUID::NimBLEUUID(const uint8_t* pData, size_t size, bool msbFirst) { -/*** TODO: change this to use the Nimble function for various lenght UUIDs: - int ble_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len); -***/ if (size != 16) { NIMBLE_LOGE(LOG_TAG,"ERROR: UUID length not 16 bytes"); return; @@ -99,7 +95,6 @@ NimBLEUUID::NimBLEUUID(const uint8_t* pData, size_t size, bool msbFirst) { /** * @brief Create a UUID from the 16bit value. - * * @param [in] uuid The 16bit short form UUID. */ NimBLEUUID::NimBLEUUID(uint16_t uuid) { @@ -111,7 +106,6 @@ NimBLEUUID::NimBLEUUID(uint16_t uuid) { /** * @brief Create a UUID from the 32bit value. - * * @param [in] uuid The 32bit short form UUID. */ NimBLEUUID::NimBLEUUID(uint32_t uuid) { @@ -123,7 +117,6 @@ NimBLEUUID::NimBLEUUID(uint32_t uuid) { /** * @brief Create a UUID from the native UUID. - * * @param [in] uuid The native UUID. */ NimBLEUUID::NimBLEUUID(const ble_uuid128_t* uuid) { @@ -135,8 +128,8 @@ NimBLEUUID::NimBLEUUID(const ble_uuid128_t* uuid) { /** * @brief Create a UUID from the 128bit value using hex parts instead of string, - * instead of BLEUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6"), it becomes - * BLEUUID(0xebe0ccb0, 0x7a0a, 0x4b0c, 0x8a1a6ff2997da3a6) + * instead of NimBLEUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6"), it becomes + * NimBLEUUID(0xebe0ccb0, 0x7a0a, 0x4b0c, 0x8a1a6ff2997da3a6) * * @param [in] first The first 32bit of the UUID. * @param [in] second The next 16bit of the UUID. @@ -153,6 +146,9 @@ NimBLEUUID::NimBLEUUID(uint32_t first, uint16_t second, uint16_t third, uint64_t } +/** + * @brief Creates an empty UUID. + */ NimBLEUUID::NimBLEUUID() { m_valueSet = false; } // NimBLEUUID @@ -180,29 +176,31 @@ bool NimBLEUUID::equals(const NimBLEUUID &uuid) const { /** - * Create a BLEUUID from a string of the form: + * Create a NimBLEUUID from a string of the form: * 0xNNNN * 0xNNNNNNNN - * 0x + * 0x * NNNN * NNNNNNNN - * + * + * + * @param [in] uuid The string to create the UUID from. */ -NimBLEUUID NimBLEUUID::fromString(const std::string &_uuid) { +NimBLEUUID NimBLEUUID::fromString(const std::string &uuid) { uint8_t start = 0; - if (strstr(_uuid.c_str(), "0x") != nullptr) { // If the string starts with 0x, skip those characters. + if (strstr(uuid.c_str(), "0x") != nullptr) { // If the string starts with 0x, skip those characters. start = 2; } - uint8_t len = _uuid.length() - start; // Calculate the length of the string we are going to use. + uint8_t len = uuid.length() - start; // Calculate the length of the string we are going to use. if(len == 4) { - uint16_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16); + uint16_t x = strtoul(uuid.substr(start, len).c_str(), NULL, 16); return NimBLEUUID(x); } else if (len == 8) { - uint32_t x = strtoul(_uuid.substr(start, len).c_str(), NULL, 16); + uint32_t x = strtoul(uuid.substr(start, len).c_str(), NULL, 16); return NimBLEUUID(x); } else if (len == 36) { - return NimBLEUUID(_uuid); + return NimBLEUUID(uuid); } return NimBLEUUID(); } // fromString @@ -210,8 +208,7 @@ NimBLEUUID NimBLEUUID::fromString(const std::string &_uuid) { /** * @brief Get the native UUID value. - * - * @return The native UUID value or NULL if not set. + * @return The native UUID value or nullptr if not set. */ const ble_uuid_any_t* NimBLEUUID::getNative() const { if (m_valueSet == false) { @@ -224,9 +221,9 @@ const ble_uuid_any_t* NimBLEUUID::getNative() const { /** * @brief Convert a UUID to its 128 bit representation. - * - * A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method + * @details A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method * will convert 16 or 32 bit representations to the full 128bit. + * @return The NimBLEUUID converted to 128bit. */ const NimBLEUUID &NimBLEUUID::to128() { // If we either don't have a value or are already a 128 bit UUID, nothing further to do. @@ -248,19 +245,23 @@ const NimBLEUUID &NimBLEUUID::to128() { /** * @brief Get a string representation of the UUID. - * + * @details * The format of a string is: * 01234567 8901 2345 6789 012345678901 * 0000180d-0000-1000-8000-00805f9b34fb * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 * * @return A string representation of the UUID. + * @deprecated Use std::string() operator instead. */ std::string NimBLEUUID::toString() const { return std::string(*this); } // toString +/** + * @brief Convienience operator to check if this UUID is equal to another. + */ bool NimBLEUUID::operator ==(const NimBLEUUID & rhs) const { if(m_valueSet && rhs.m_valueSet) { return ble_uuid_cmp(&m_uuid.u, &rhs.m_uuid.u) == 0; @@ -270,11 +271,19 @@ bool NimBLEUUID::operator ==(const NimBLEUUID & rhs) const { } +/** + * @brief Convienience operator to check if this UUID is not equal to another. + */ bool NimBLEUUID::operator !=(const NimBLEUUID & rhs) const { return !this->operator==(rhs); } +/** + * @brief Convienience operator to convert this UUID to string representation. + * @details This allows passing NimBLEUUID to functions + * that accept std::string and/or or it's methods as a parameter. + */ NimBLEUUID::operator std::string() const { if (!m_valueSet) return std::string(); // If we have no value, nothing to format. diff --git a/libesp32/NimBLE-Arduino/src/NimBLEUUID.h b/libesp32/NimBLE-Arduino/src/NimBLEUUID.h index f07bb3df5..982f9c36d 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEUUID.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEUUID.h @@ -3,7 +3,7 @@ * * Created: on Jan 24 2020 * Author H2zero - * + * * Originally: * * BLEUUID.h @@ -37,20 +37,21 @@ public: NimBLEUUID(const uint8_t* pData, size_t size, bool msbFirst); NimBLEUUID(uint32_t first, uint16_t second, uint16_t third, uint64_t fourth); NimBLEUUID(); - uint8_t bitSize() const; // Get the number of bits in this uuid. - bool equals(const NimBLEUUID &uuid) const; + + uint8_t bitSize() const; + bool equals(const NimBLEUUID &uuid) const; const ble_uuid_any_t* getNative() const; - const NimBLEUUID & to128(); - std::string toString() const; - static NimBLEUUID fromString(const std::string &uuid); // Create a NimBLEUUID from a string + const NimBLEUUID & to128(); + std::string toString() const; + static NimBLEUUID fromString(const std::string &uuid); bool operator ==(const NimBLEUUID & rhs) const; bool operator !=(const NimBLEUUID & rhs) const; operator std::string() const; private: - ble_uuid_any_t m_uuid; // The underlying UUID structure that this class wraps. - bool m_valueSet = false; // Is there a value set for this instance. + ble_uuid_any_t m_uuid; + bool m_valueSet = false; }; // NimBLEUUID #endif /* CONFIG_BT_ENABLED */ #endif /* COMPONENTS_NIMBLEUUID_H_ */ diff --git a/libesp32/NimBLE-Arduino/src/NimBLEUtils.cpp b/libesp32/NimBLE-Arduino/src/NimBLEUtils.cpp index 1f1f22c25..7a1f55b41 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEUtils.cpp +++ b/libesp32/NimBLE-Arduino/src/NimBLEUtils.cpp @@ -16,6 +16,11 @@ static const char* LOG_TAG = "NimBLEUtils"; +/** + * @brief A function for checking validity of connection parameters. + * @param [in] params A pointer to the structure containing the parameters to check. + * @return valid == 0 or error code. + */ int NimBLEUtils::checkConnParams(ble_gap_conn_params* params) { /* Check connection interval min */ if ((params->itvl_min < BLE_HCI_CONN_ITVL_MIN) || @@ -49,6 +54,11 @@ int NimBLEUtils::checkConnParams(ble_gap_conn_params* params) { } +/** + * @brief Converts a return code from the NimBLE stack to a text string. + * @param [in] rc The return code to convert. + * @return A string representation of the return code. + */ const char* NimBLEUtils::returnCodeToString(int rc) { #if defined(CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT) switch(rc) { @@ -338,9 +348,9 @@ const char* NimBLEUtils::returnCodeToString(int rc) { /** - * @brief Convert the BLE Advertising Data flags to a string. - * @param adFlags The flags to convert - * @return std::string A string representation of the advertising flags. + * @brief Convert the advertising type flag to a string. + * @param advType The type to convert. + * @return A string representation of the advertising flags. */ const char* NimBLEUtils::advTypeToString(uint8_t advType) { #if defined(CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT) @@ -367,7 +377,7 @@ const char* NimBLEUtils::advTypeToString(uint8_t advType) { /** * @brief Create a hex representation of data. * - * @param [in] target Where to write the hex string. If this is null, we malloc storage. + * @param [in] target Where to write the hex string. If this is null, we malloc storage. * @param [in] source The start of the binary data. * @param [in] length The length of the data to convert. * @return A pointer to the formatted buffer. @@ -400,6 +410,11 @@ char* NimBLEUtils::buildHexData(uint8_t* target, const uint8_t* source, uint8_t } // buildHexData +/** + * @brief Utility function to log the gap event info. + * @param [in] event A pointer to the gap event structure. + * @param [in] arg Unused. + */ void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){ #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) NIMBLE_LOGD(LOG_TAG, "Received a GAP event: %s", gapEventToString(event->type)); @@ -408,7 +423,7 @@ void NimBLEUtils::dumpGapEvent(ble_gap_event *event, void *arg){ /** - * @brief Convert a BT GAP event type to a string representation. + * @brief Convert a GAP event type to a string representation. * @param [in] eventType The type of event. * @return A string representation of the event type. */ @@ -493,198 +508,4 @@ const char* NimBLEUtils::gapEventToString(uint8_t eventType) { #endif // #if defined(CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT) } // gapEventToString - -/** - * Utility function to log an array of bytes. - */ -void print_bytes(const uint8_t *bytes, int len) -{ - int i; - - for (i = 0; i < len; i++) { - MODLOG_DFLT(ERROR, "%s0x%02x", i != 0 ? ":" : "", bytes[i]); - if(i % 30 == 0){ - MODLOG_DFLT(ERROR, "\n"); - } - } - - MODLOG_DFLT(ERROR, "\n"); -} - -void print_mbuf(const struct os_mbuf *om) -{ - int colon; - - colon = 0; - while (om != NULL) { - if (colon) { - MODLOG_DFLT(DEBUG, ":"); - } else { - colon = 1; - } - print_bytes(om->om_data, om->om_len); - om = SLIST_NEXT(om, om_next); - } -} - -char *addr_str(const void *addr) -{ - static char buf[6 * 2 + 5 + 1]; - const uint8_t *u8p; - - u8p = (const uint8_t*)addr; - sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", - u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]); - - return buf; -} - -void print_uuid(const ble_uuid_t *uuid) -{ - char buf[BLE_UUID_STR_LEN]; - - MODLOG_DFLT(DEBUG, "%s", ble_uuid_to_str(uuid, buf)); -} - -void print_adv_fields(const struct ble_hs_adv_fields *fields) -{ - char s[BLE_HS_ADV_MAX_SZ]; - const uint8_t *u8p; - int i; - - if (fields->flags != 0) { - MODLOG_DFLT(DEBUG, " flags=0x%02x\n", fields->flags); - } - - if (fields->uuids16 != NULL) { - MODLOG_DFLT(DEBUG, " uuids16(%scomplete)=", - fields->uuids16_is_complete ? "" : "in"); - for (i = 0; i < fields->num_uuids16; i++) { - print_uuid(&fields->uuids16[i].u); - MODLOG_DFLT(DEBUG, " "); - } - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->uuids32 != NULL) { - MODLOG_DFLT(DEBUG, " uuids32(%scomplete)=", - fields->uuids32_is_complete ? "" : "in"); - for (i = 0; i < fields->num_uuids32; i++) { - print_uuid(&fields->uuids32[i].u); - MODLOG_DFLT(DEBUG, " "); - } - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->uuids128 != NULL) { - MODLOG_DFLT(DEBUG, " uuids128(%scomplete)=", - fields->uuids128_is_complete ? "" : "in"); - for (i = 0; i < fields->num_uuids128; i++) { - print_uuid(&fields->uuids128[i].u); - MODLOG_DFLT(DEBUG, " "); - } - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->name != NULL) { - assert(fields->name_len < sizeof s - 1); - memcpy(s, fields->name, fields->name_len); - s[fields->name_len] = '\0'; - MODLOG_DFLT(DEBUG, " name(%scomplete)=%s\n", - fields->name_is_complete ? "" : "in", s); - } - - if (fields->tx_pwr_lvl_is_present) { - MODLOG_DFLT(DEBUG, " tx_pwr_lvl=%d\n", fields->tx_pwr_lvl); - } - - if (fields->slave_itvl_range != NULL) { - MODLOG_DFLT(DEBUG, " slave_itvl_range="); - print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN); - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->svc_data_uuid16 != NULL) { - MODLOG_DFLT(DEBUG, " svc_data_uuid16="); - print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len); - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->public_tgt_addr != NULL) { - MODLOG_DFLT(DEBUG, " public_tgt_addr="); - u8p = fields->public_tgt_addr; - for (i = 0; i < fields->num_public_tgt_addrs; i++) { - MODLOG_DFLT(DEBUG, "public_tgt_addr=%s ", addr_str(u8p)); - u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN; - } - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->appearance_is_present) { - MODLOG_DFLT(DEBUG, " appearance=0x%04x\n", fields->appearance); - } - - if (fields->adv_itvl_is_present) { - MODLOG_DFLT(DEBUG, " adv_itvl=0x%04x\n", fields->adv_itvl); - } - - if (fields->svc_data_uuid32 != NULL) { - MODLOG_DFLT(DEBUG, " svc_data_uuid32="); - print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len); - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->svc_data_uuid128 != NULL) { - MODLOG_DFLT(DEBUG, " svc_data_uuid128="); - print_bytes(fields->svc_data_uuid128, fields->svc_data_uuid128_len); - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->uri != NULL) { - MODLOG_DFLT(DEBUG, " uri="); - print_bytes(fields->uri, fields->uri_len); - MODLOG_DFLT(DEBUG, "\n"); - } - - if (fields->mfg_data != NULL) { - MODLOG_DFLT(DEBUG, " mfg_data="); - print_bytes(fields->mfg_data, fields->mfg_data_len); - MODLOG_DFLT(DEBUG, "\n"); - } -} - - - /** - * Logs information about a connection to the console. - */ -void print_conn_desc(const struct ble_gap_conn_desc *desc) -{ - MODLOG_DFLT(DEBUG, "handle=%d our_ota_addr_type=%d our_ota_addr=%s ", - desc->conn_handle, desc->our_ota_addr.type, - addr_str(desc->our_ota_addr.val)); - MODLOG_DFLT(DEBUG, "our_id_addr_type=%d our_id_addr=%s ", - desc->our_id_addr.type, addr_str(desc->our_id_addr.val)); - MODLOG_DFLT(DEBUG, "peer_ota_addr_type=%d peer_ota_addr=%s ", - desc->peer_ota_addr.type, addr_str(desc->peer_ota_addr.val)); - MODLOG_DFLT(DEBUG, "peer_id_addr_type=%d peer_id_addr=%s ", - desc->peer_id_addr.type, addr_str(desc->peer_id_addr.val)); - MODLOG_DFLT(DEBUG, "conn_itvl=%d conn_latency=%d supervision_timeout=%d " - "encrypted=%d authenticated=%d bonded=%d", - desc->conn_itvl, desc->conn_latency, - desc->supervision_timeout, - desc->sec_state.encrypted, - desc->sec_state.authenticated, - desc->sec_state.bonded); -} - - -void print_addr(const void *addr) -{ - const uint8_t *u8p; - - u8p = (uint8_t*)addr; - MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x", - u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]); -} - #endif //CONFIG_BT_ENABLED diff --git a/libesp32/NimBLE-Arduino/src/NimBLEUtils.h b/libesp32/NimBLE-Arduino/src/NimBLEUtils.h index 891f83596..acbc93e72 100644 --- a/libesp32/NimBLE-Arduino/src/NimBLEUtils.h +++ b/libesp32/NimBLE-Arduino/src/NimBLEUtils.h @@ -27,14 +27,10 @@ typedef struct { std::string *buf; } ble_task_data_t; -extern "C"{ -char *addr_str(const void *addr); -void print_conn_desc(const struct ble_gap_conn_desc *desc); -void print_adv_fields(const struct ble_hs_adv_fields *fields); -void print_addr(const void *addr); -void print_bytes(const uint8_t *bytes, int len); -} +/** + * @brief A BLE Utility class with methods for debugging and general purpose use. + */ class NimBLEUtils { public: static void dumpGapEvent(ble_gap_event *event, void *arg); diff --git a/libesp32/NimBLE-Arduino/src/esp-hci/src/esp_nimble_hci.c b/libesp32/NimBLE-Arduino/src/esp-hci/src/esp_nimble_hci.c index e4ab99932..0494f0a18 100644 --- a/libesp32/NimBLE-Arduino/src/esp-hci/src/esp_nimble_hci.c +++ b/libesp32/NimBLE-Arduino/src/esp-hci/src/esp_nimble_hci.c @@ -30,6 +30,7 @@ #include "esp_bt.h" #include "freertos/semphr.h" #include "esp_compiler.h" +#include "esp_ipc.h" #define NIMBLE_VHCI_TIMEOUT_MS 2000 @@ -78,21 +79,29 @@ void ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb, ble_hci_rx_acl_hs_arg = acl_arg; } +void ble_hci_trans_hs_cmd_tx_on_core_0(void *arg) +{ + uint8_t *cmd = arg; + uint16_t len = BLE_HCI_CMD_HDR_LEN + cmd[3] + 1; + esp_vhci_host_send_packet(cmd, len); +} int ble_hci_trans_hs_cmd_tx(uint8_t *cmd) { - uint16_t len; uint8_t rc = 0; assert(cmd != NULL); *cmd = BLE_HCI_UART_H4_CMD; - len = BLE_HCI_CMD_HDR_LEN + cmd[3] + 1; if (!esp_vhci_host_check_send_available()) { ESP_LOGD(TAG, "Controller not ready to receive packets"); } if (xSemaphoreTake(vhci_send_sem, NIMBLE_VHCI_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE) { - esp_vhci_host_send_packet(cmd, len); + if (xPortGetCoreID() != 0) { + esp_ipc_call_blocking(0, ble_hci_trans_hs_cmd_tx_on_core_0, cmd); + } else { + ble_hci_trans_hs_cmd_tx_on_core_0(cmd); + } } else { rc = BLE_HS_ETIMEOUT_HCI; } @@ -111,27 +120,37 @@ int ble_hci_trans_ll_evt_tx(uint8_t *hci_ev) return rc; } +void ble_hci_trans_hs_acl_tx_on_core_0(void *arg) +{ + uint8_t data[MYNEWT_VAL(BLE_ACL_BUF_SIZE) + 1]; + struct os_mbuf *om = arg; + uint16_t len = 1 + OS_MBUF_PKTLEN(om); + + data[0] = BLE_HCI_UART_H4_ACL; + os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), &data[1]); + + esp_vhci_host_send_packet(data, len); +} + int ble_hci_trans_hs_acl_tx(struct os_mbuf *om) { - uint16_t len = 0; - uint8_t data[MYNEWT_VAL(BLE_ACL_BUF_SIZE) + 1], rc = 0; + uint8_t rc = 0; /* If this packet is zero length, just free it */ if (OS_MBUF_PKTLEN(om) == 0) { os_mbuf_free_chain(om); return 0; } - data[0] = BLE_HCI_UART_H4_ACL; - len++; if (!esp_vhci_host_check_send_available()) { ESP_LOGD(TAG, "Controller not ready to receive packets"); } - os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), &data[1]); - len += OS_MBUF_PKTLEN(om); - if (xSemaphoreTake(vhci_send_sem, NIMBLE_VHCI_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE) { - esp_vhci_host_send_packet(data, len); + if (xPortGetCoreID() != 0) { + esp_ipc_call_blocking(0, ble_hci_trans_hs_acl_tx_on_core_0, om); + } else { + ble_hci_trans_hs_acl_tx_on_core_0(om); + } } else { rc = BLE_HS_ETIMEOUT_HCI; } @@ -451,6 +470,7 @@ esp_err_t esp_nimble_hci_and_controller_init(void) esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); + bt_cfg.ble_max_conn = CONFIG_BT_NIMBLE_MAX_CONNECTIONS; if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { return ret; diff --git a/libesp32/NimBLE-Arduino/src/nimble/host/mesh/src/src/ble_hs_resolv_priv.h b/libesp32/NimBLE-Arduino/src/nimble/host/mesh/src/src/ble_hs_resolv_priv.h index e76a26a55..7bc8a2fe6 100644 --- a/libesp32/NimBLE-Arduino/src/nimble/host/mesh/src/src/ble_hs_resolv_priv.h +++ b/libesp32/NimBLE-Arduino/src/nimble/host/mesh/src/src/ble_hs_resolv_priv.h @@ -53,6 +53,7 @@ struct ble_hs_peer_sec { */ struct ble_hs_dev_records { bool rec_used; + uint8_t rand_addr_type; uint8_t pseudo_addr[BLE_DEV_ADDR_LEN]; uint8_t rand_addr[BLE_DEV_ADDR_LEN]; uint8_t identity_addr[BLE_DEV_ADDR_LEN]; diff --git a/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv.c b/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv.c index 60c77c9b8..46e5577f6 100644 --- a/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv.c +++ b/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv.c @@ -181,6 +181,44 @@ is_rpa_resolvable_by_peer_rec(struct ble_hs_dev_records *p_dev_rec, uint8_t *pee return false; } +static void +ble_rpa_replace_id_with_rand_addr(uint8_t *addr_type, uint8_t *peer_addr) +{ + struct ble_hs_dev_records *p_dev_rec; + ble_addr_t p_addr = {0}; + struct ble_hs_conn *conn = NULL; + + p_dev_rec = ble_rpa_find_peer_dev_rec(peer_addr); + + if (p_dev_rec != NULL) { + if (memcmp(p_dev_rec->rand_addr, p_dev_rec->identity_addr, BLE_DEV_ADDR_LEN)) { + /* OTA address (before resolving) gets saved in RAND_ADDR when the peer + * record is fetched from resolving list. Replace peer address + * with rand_addr to maintain status quo for new pairing/encryption request. */ + p_addr.type = *addr_type; + memcpy(&p_addr.val[0], peer_addr, BLE_DEV_ADDR_LEN); + + ble_hs_lock(); + + conn = ble_hs_conn_find_by_addr(&p_addr); + /* Rewrite the peer address history in ble_hs_conn. Need to take + * this step to avoid taking wrong address during re-pairing + * process */ + if (conn != NULL) { + conn->bhc_peer_rpa_addr.type = p_dev_rec->rand_addr_type; + memcpy(&conn->bhc_peer_rpa_addr.val[0], p_dev_rec->rand_addr, BLE_DEV_ADDR_LEN); + conn->bhc_peer_addr.type = p_dev_rec->rand_addr_type; + memcpy(&conn->bhc_peer_addr.val[0], p_dev_rec->rand_addr, BLE_DEV_ADDR_LEN); + BLE_HS_LOG(DEBUG, "\n Replace Identity addr with random addr received at" + " start of the connection\n"); + } + + ble_hs_unlock(); + } + } + return; +} + /* Add peer to peer device records. * * @return 0 if added successfully, @@ -227,6 +265,7 @@ ble_rpa_find_rl_from_peer_records(uint8_t *peer_addr, uint8_t *peer_addr_type) if (is_rpa_resolvable_by_peer_rec(p_dev_rec, peer_addr)) { memcpy(p_dev_rec->rand_addr, peer_addr, BLE_DEV_ADDR_LEN); + p_dev_rec->rand_addr_type = *peer_addr_type; rl = ble_hs_resolv_list_find(p_dev_rec->identity_addr); if (rl) { memcpy(peer_addr, p_dev_rec->identity_addr, BLE_DEV_ADDR_LEN); @@ -277,13 +316,10 @@ ble_rpa_replace_peer_params_with_rl(uint8_t *peer_addr, uint8_t *addr_type, if (is_rpa) { ble_hs_log_flat_buf(peer_addr, BLE_DEV_ADDR_LEN); - rl_tmp = ble_hs_resolv_list_find(peer_addr); + BLE_HS_LOG(DEBUG, "\n"); - /* Try to find from your peer_device records, if RL doesn't - * exist */ - if (rl_tmp == NULL) { - rl_tmp = ble_rpa_find_rl_from_peer_records(peer_addr, addr_type); - } + /* Try to find RL from your peer_device records */ + rl_tmp = ble_rpa_find_rl_from_peer_records(peer_addr, addr_type); } if (rl != NULL) { @@ -571,7 +607,7 @@ ble_hs_resolv_list_add(uint8_t *cmdbuf) int ble_hs_resolv_list_rmv(uint8_t addr_type, uint8_t *ident_addr) { - int position; + int position, rc = BLE_HS_ENOENT; /* Remove from IRK records */ position = ble_hs_is_on_resolv_list(ident_addr, addr_type); @@ -583,10 +619,15 @@ ble_hs_resolv_list_rmv(uint8_t addr_type, uint8_t *ident_addr) ble_hs_resolv_entry)); --g_ble_hs_resolv_data.rl_cnt; - return 0; + rc = 0; } - return BLE_HS_ENOENT; + /* As we are removing the RL record, it is needed to change + * peer_address to its latest received OTA address, this helps when existing bond at + * peer side is removed */ + ble_rpa_replace_id_with_rand_addr(&addr_type, ident_addr); + + return rc; } /** diff --git a/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv_priv.h b/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv_priv.h index e76a26a55..7bc8a2fe6 100644 --- a/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv_priv.h +++ b/libesp32/NimBLE-Arduino/src/nimble/host/src/ble_hs_resolv_priv.h @@ -53,6 +53,7 @@ struct ble_hs_peer_sec { */ struct ble_hs_dev_records { bool rec_used; + uint8_t rand_addr_type; uint8_t pseudo_addr[BLE_DEV_ADDR_LEN]; uint8_t rand_addr[BLE_DEV_ADDR_LEN]; uint8_t identity_addr[BLE_DEV_ADDR_LEN]; diff --git a/libesp32/NimBLE-Arduino/src/nimconfig.h b/libesp32/NimBLE-Arduino/src/nimconfig.h index 907e6bc26..2f10fa2fc 100644 --- a/libesp32/NimBLE-Arduino/src/nimconfig.h +++ b/libesp32/NimBLE-Arduino/src/nimconfig.h @@ -1,12 +1,21 @@ +/** @file + * + * IGNORE THIS FILE IF USING ESP-IDF, USE MENUCONFIG TO SET NIMBLE OPTIONS. + * + * The config options here are for Arduino use only. + */ + #pragma once #include "sdkconfig.h" -/** For ESP-IDF compatibility - * - * Some versions of ESP-IDF used the config name format "CONFIG_NIMBLE_". - * This converts them to "CONFIG_BT_NIMBLE_" format used in the latest IDF. + +/* + * For ESP-IDF compatibility + * Some versions of ESP-IDF used the config name format "CONFIG_NIMBLE_". + * This converts them to "CONFIG_BT_NIMBLE_" format used in the latest IDF. */ -/* Detect if using ESP-IDF or Arduino (Arduino won't have these defines in sdkconfig)*/ + +/* Detect if using ESP-IDF or Arduino (Arduino won't have these defines in sdkconfig) */ #if defined(CONFIG_BT_NIMBLE_TASK_STACK_SIZE) || defined(CONFIG_NIMBLE_TASK_STACK_SIZE) #if defined(CONFIG_NIMBLE_ENABLED) && !defined(CONFIG_BT_NIMBLE_ENABLED) @@ -36,99 +45,171 @@ #else // Using Arduino /*********************************************** - * Arduino config options + * Arduino config options start here **********************************************/ -/** Comment out if not using NimBLE Client functions +/** @brief Comment out if not using NimBLE Client functions \n * Reduces flash size by approx. 7kB. */ #define CONFIG_BT_NIMBLE_ROLE_CENTRAL -/** Comment out if not using NimBLE Scan functions +/** @brief Comment out if not using NimBLE Scan functions \n * Reduces flash size by approx. 26kB. */ #define CONFIG_BT_NIMBLE_ROLE_OBSERVER -/** Comment out if not using NimBLE Server functions +/** @brief Comment out if not using NimBLE Server functions \n * Reduces flash size by approx. 16kB. */ // #define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL -/** Comment out if not using NimBLE Advertising functions +/** @brief Comment out if not using NimBLE Advertising functions \n * Reduces flash size by approx. 5kB. */ // #define CONFIG_BT_NIMBLE_ROLE_BROADCASTER -/** Uncomment to see debug log messages from the NimBLE host +/* Uncomment to see debug log messages from the NimBLE host * Uses approx. 32kB of flash memory. */ // #define CONFIG_BT_NIMBLE_DEBUG -/** Uncomment to see NimBLE host return codes as text debug log messages. +/* Uncomment to see NimBLE host return codes as text debug log messages. * Uses approx. 7kB of flash memory. */ // #define CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT -/** Uncomment to see GAP event codes as text in debug log messages. +/* Uncomment to see GAP event codes as text in debug log messages. * Uses approx. 1kB of flash memory. */ // #define CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT -/** Uncomment to see advertisment types as text while scanning in debug log messages. +/* Uncomment to see advertisment types as text while scanning in debug log messages. * Uses approx. 250 bytes of flash memory. */ // #define CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT -/** Sets the core NimBLE host runs on */ +/** @brief Sets the core NimBLE host runs on */ #define CONFIG_BT_NIMBLE_PINNED_TO_CORE 0 -/** Sets the stack size for the NimBLE host task */ +/** @brief Sets the stack size for the NimBLE host task */ #define CONFIG_BT_NIMBLE_TASK_STACK_SIZE 4096 -/** Sets the number of simultaneous connections (esp controller max is 9) */ +/** + * @brief Sets the memory pool where NimBLE will be loaded + * @details By default NimBLE is loaded in internal ram.\n + * To use external PSRAM you must change this to `#define CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL 1` + */ +#define CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL 1 + +/** @brief Sets the number of simultaneous connections (esp controller max is 9) */ #define CONFIG_BT_NIMBLE_MAX_CONNECTIONS 3 -/** Sets the number of devices allowed to store/bond with */ +/** @brief Sets the number of devices allowed to store/bond with */ #define CONFIG_BT_NIMBLE_MAX_BONDS 3 -/** Sets the number of CCCD's to store per bonded device */ +/** @brief Sets the maximum number of CCCD subscriptions to store */ #define CONFIG_BT_NIMBLE_MAX_CCCDS 8 +/** @brief Set if CCCD's and bond data should be stored in NVS */ #define CONFIG_BT_NIMBLE_NVS_PERSIST 0 + +/** @brief Allow legacy paring */ #define CONFIG_BT_NIMBLE_SM_LEGACY 1 + +/** @brief Allow BLE secure connections */ #define CONFIG_BT_NIMBLE_SM_SC 1 + +/** @brief Default device name */ #define CONFIG_BT_NIMBLE_SVC_GAP_DEVICE_NAME "nimble" + +/** @brief Max device name length (bytes) */ #define CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN 31 + +/** @brief Default MTU size */ #define CONFIG_BT_NIMBLE_ATT_PREFERRED_MTU 256 + +/** @brief Default GAP appearance */ #define CONFIG_BT_NIMBLE_SVC_GAP_APPEARANCE 0x0 + +/** @brief ACL Buffer count */ #define CONFIG_BT_NIMBLE_ACL_BUF_COUNT 12 + +/** @brief ACL Buffer size */ #define CONFIG_BT_NIMBLE_ACL_BUF_SIZE 255 + +/** @brief HCI Event Buffer size */ #define CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE 70 + +/** @brief Number of high priority HCI event buffers */ #define CONFIG_BT_NIMBLE_HCI_EVT_HI_BUF_COUNT 30 + +/** @brief Number of low priority HCI event buffers */ #define CONFIG_BT_NIMBLE_HCI_EVT_LO_BUF_COUNT 8 + +/** + * @brief Sets the number of MSYS buffers available. + * @details MSYS is a system level mbuf registry. For prepare write & prepare \n + * responses MBUFs are allocated out of msys_1 pool. This may need to be increased if\n + * you are sending large blocks of data with a low MTU. E.g: 512 bytes with 23 MTU will fail. + */ #define CONFIG_BT_NIMBLE_MSYS1_BLOCK_COUNT 12 + + +/** @brief Random address refresh time in seconds */ +#define CONFIG_BT_NIMBLE_RPA_TIMEOUT 900 + +/** @brief Maximum number of connection oriented channels */ +#define CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM 0 + +/* These should not be altered */ #define CONFIG_BT_NIMBLE_HS_FLOW_CTRL 1 #define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_ITVL 1000 #define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_THRESH 2 #define CONFIG_BT_NIMBLE_HS_FLOW_CTRL_TX_ON_DISCONNECT 1 -#define CONFIG_BT_NIMBLE_RPA_TIMEOUT 900 -#define CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM 0 -/** Do not comment out */ #ifndef CONFIG_BT_ENABLED #define CONFIG_BT_ENABLED #endif + #define CONFIG_BTDM_CONTROLLER_MODE_BLE_ONLY -#define CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_INTERNAL #endif // #if defined(CONFIG_BT_NIMBLE_TASK_STACK_SIZE) || defined(CONFIG_NIMBLE_TASK_STACK_SIZE) -/** Cannot use client without scan */ +/********************************** + End Arduino config +**********************************/ + + +/* Cannot use client without scan */ #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) && !defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER) #define CONFIG_BT_NIMBLE_ROLE_OBSERVER #endif -/** Cannot use server without advertise */ +/* Cannot use server without advertise */ #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) && !defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER) #define CONFIG_BT_NIMBLE_ROLE_BROADCASTER #endif + + + +#ifdef _DOXYGEN_ +/** @brief Uncomment to see debug log messages from the NimBLE host \n + * Uses approx. 32kB of flash memory. + */ +#define CONFIG_BT_NIMBLE_DEBUG + +/** @brief Uncomment to see NimBLE host return codes as text debug log messages. \n + * Uses approx. 7kB of flash memory. + */ +#define CONFIG_NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT + +/** @brief Uncomment to see GAP event codes as text in debug log messages. \n + * Uses approx. 1kB of flash memory. + */ +#define CONFIG_NIMBLE_CPP_ENABLE_GAP_EVENT_CODE_TEXT + +/** @brief Uncomment to see advertisment types as text while scanning in debug log messages. \n + * Uses approx. 250 bytes of flash memory. + */ +#define CONFIG_NIMBLE_CPP_ENABLE_ADVERTISMENT_TYPE_TEXT +#endif // _DOXYGEN_ diff --git a/libesp32/NimBLE-Arduino/src/porting/nimble/src/os_mempool.c b/libesp32/NimBLE-Arduino/src/porting/nimble/src/os_mempool.c index f4c712ff1..ba837e7d6 100644 --- a/libesp32/NimBLE-Arduino/src/porting/nimble/src/os_mempool.c +++ b/libesp32/NimBLE-Arduino/src/porting/nimble/src/os_mempool.c @@ -346,7 +346,8 @@ os_mempool_info_get_next(struct os_mempool *mp, struct os_mempool_info *omi) omi->omi_num_blocks = cur->mp_num_blocks; omi->omi_num_free = cur->mp_num_free; omi->omi_min_free = cur->mp_min_free; - strncpy(omi->omi_name, cur->name, sizeof(omi->omi_name)); + strncpy(omi->omi_name, cur->name, sizeof(omi->omi_name) - 1); + omi->omi_name[sizeof(omi->omi_name) - 1] = '\0'; return (cur); } diff --git a/platformio_override_sample.ini b/platformio_override_sample.ini index ddd74a60d..23b43b1d5 100644 --- a/platformio_override_sample.ini +++ b/platformio_override_sample.ini @@ -9,6 +9,8 @@ ; http://docs.platformio.org/en/stable/projectconf.html [platformio] +; For best Gitpod performance remove the ";" in the next line. Needed Platformio files are cached and installed at first run +;core_dir = .platformio extra_configs = platformio_tasmota_cenv.ini ; *** Build/upload environment @@ -36,7 +38,6 @@ default_envs = [common] -platform = ${core.platform} platform_packages = ${core.platform_packages} build_unflags = ${core.build_unflags} build_flags = ${core.build_flags} @@ -74,12 +75,10 @@ extra_scripts = ${scripts_defaults.extra_scripts} [core] ; Activate only (one set) if you want to override the standard core defined in platformio.ini !!! -;platform = ${tasmota_stage.platform} ;platform_packages = ${tasmota_stage.platform_packages} ;build_unflags = ${tasmota_stage.build_unflags} ;build_flags = ${tasmota_stage.build_flags} -;platform = ${core_stage.platform} ;platform_packages = ${core_stage.platform_packages} ;build_unflags = ${core_stage.build_unflags} ;build_flags = ${core_stage.build_flags} @@ -87,8 +86,7 @@ extra_scripts = ${scripts_defaults.extra_scripts} [tasmota_stage] ; *** Esp8266 core for Arduino version Tasmota stage -platform = espressif8266@2.6.2 -platform_packages = jason2866/framework-arduinoespressif8266 +platform_packages = framework-arduinoespressif8266@https://github.com/Jason2866/Arduino/releases/download/2.7.4.2/esp8266-2.7.4.2.zip build_unflags = ${esp_defaults.build_unflags} build_flags = ${esp82xx_defaults.build_flags} @@ -123,8 +121,7 @@ build_flags = ${esp82xx_defaults.build_flags} [core_stage] ; *** Esp8266 core version. Tasmota stage or Arduino stage version. Built with GCC 10.1 toolchain -platform = espressif8266@2.6.2 -platform_packages = framework-arduinoespressif8266 @ https://github.com/Jason2866/platform-espressif8266/releases/download/2.9.0/framework-arduinoespressif8266-3.20900.0.tar.gz +platform_packages = framework-arduinoespressif8266 @ https://github.com/Jason2866/platform-espressif8266/releases/download/2.9.1/framework-arduinoespressif8266-3.20901.0.tar.gz ;framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git toolchain-xtensa @ ~2.100100.0 build_unflags = ${esp_defaults.build_unflags} @@ -160,6 +157,17 @@ build_flags = ${esp82xx_defaults.build_flags} ; -lstdc++-exc +[core32] +; Activate Stage Core32 by removing ";" in next line, if you want to override the standard core32 +;platform_packages = ${core32_stage.platform_packages} + + +[core32_stage] +platform_packages = tool-esptoolpy@1.20800.0 + ; latest working commit + framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#c09ec5bd3d35ba7dfc135755ab300e2b45416def + + ; *** Debug version used for PlatformIO Home Project Inspection [env:tasmota-debug] build_type = debug diff --git a/platformio_tasmota32.ini b/platformio_tasmota32.ini index 739ec2338..bdd07be81 100644 --- a/platformio_tasmota32.ini +++ b/platformio_tasmota32.ini @@ -39,9 +39,13 @@ default_envs = ${build_envs.default_envs} ; tasmota32-UK -[common32] +[core32] platform = espressif32@2.0.0 platform_packages = tool-esptoolpy@1.20800.0 + +[common32] +platform = ${core32.platform} +platform_packages = ${core32.platform_packages} board = esp32dev board_build.ldscript = esp32_out.ld board_build.partitions = esp32_partition_app1984k_spiffs64k.csv diff --git a/tasmota/CHANGELOG.md b/tasmota/CHANGELOG.md index 2e90fdb8c..907dd36fc 100644 --- a/tasmota/CHANGELOG.md +++ b/tasmota/CHANGELOG.md @@ -1,14 +1,34 @@ ## Released -### 8.5.0 20200909 - -- Release Hannah - ## Unreleased (development) -### 8.4.0.3 20200823 +### 8.5.0.1 20200907 - Fix energy total counters (#9263, #9266) +- Fix crash in ``ZbRestore`` +- Fix reset BMP sensors when executing command ``SaveData`` and define USE_DEEPSLEEP enabled (#9300) +- Fix ``status 0`` message when using define USE_MQTT_TLS due to small log buffer (#9305) +- Fix ``status 13`` exception 9 when more than one shutter is configured +- Fix ``status 13`` json message +- Fix Shelly 2.5 higher temperature regression from 8.2.0.1 (#7991) +- Change replace ArduinoJson with JSMN for JSON parsing +- Change ``WakeUp`` uses 256 steps instead of 100 (#9241) +- Add command ``SetOption110 1`` to disable Zigbee auto-config when pairing new devices +- Add command ``SetOption111 1`` to enable frequency output for buzzer GPIO (#8994) +- Add command ``SetOption112 1`` to enable friendly name in zigbee topic (use with SetOption89) +- Add ``#define USE_MQTT_AWS_IOT_LIGHT`` for password based AWS IoT authentication +- Add ``#define MQTT_LWT_OFFLINE`` and ``#define MQTT_LWT_ONLINE`` to user_config.h (#9395) +- Add new shutter modes (#9244) +- Add Zigbee auto-config when pairing +- Add support for MLX90640 IR array temperature sensor by Christian Baars +- Add support for VL53L1X time of flight sensor by Johann Obermeier + +### 8.5.0 20200907 + +- Release Hannah + +### 8.4.0.3 20200823 + - Change references from http://thehackbox.org/tasmota/ to http://ota.tasmota.com/tasmota/ - Add command ``PowerDelta1`` to ``PowerDelta3`` to trigger on up to three phases (#9134) - Add Zigbee web ui widget for Lights diff --git a/tasmota/StackThunk_light.cpp b/tasmota/StackThunk_light.cpp index c9f9bc78e..3c0d91bcf 100644 --- a/tasmota/StackThunk_light.cpp +++ b/tasmota/StackThunk_light.cpp @@ -40,8 +40,10 @@ uint32_t *stack_thunk_light_save = NULL; /* Saved A1 while in BearSSL */ uint32_t stack_thunk_light_refcnt = 0; //#define _stackSize (5600/4) -#ifdef USE_MQTT_TLS_FORCE_EC_CIPHER +#if defined(USE_MQTT_AWS_IOT) #define _stackSize (5300/4) // using a light version of bearssl we can save 300 bytes +#elif defined(USE_MQTT_TLS_FORCE_EC_CIPHER) + #define _stackSize (4800/4) // no private key, we can reduce a little, max observed 4300 #else #define _stackSize (3600/4) // using a light version of bearssl we can save 2k #endif diff --git a/tasmota/WiFiClientSecureLightBearSSL.cpp b/tasmota/WiFiClientSecureLightBearSSL.cpp index 209555c3f..f0a6bcfbb 100755 --- a/tasmota/WiFiClientSecureLightBearSSL.cpp +++ b/tasmota/WiFiClientSecureLightBearSSL.cpp @@ -872,21 +872,16 @@ extern "C" { #ifdef USE_MQTT_TLS_FORCE_EC_CIPHER // we support only P256 EC curve for AWS IoT, no EC curve for Letsencrypt unless forced - br_ssl_engine_set_ec(&cc->eng, &br_ec_p256_m15); + br_ssl_engine_set_ec(&cc->eng, &br_ec_p256_m15); // TODO #endif + static const char * alpn_mqtt = "mqtt"; + br_ssl_engine_set_protocol_names(&cc->eng, &alpn_mqtt, 1); } } // Called by connect() to do the actual SSL setup and handshake. // Returns if the SSL handshake succeeded. bool WiFiClientSecure_light::_connectSSL(const char* hostName) { -// #ifdef USE_MQTT_AWS_IOT -// if ((!_chain_P) || (!_sk_ec_P)) { -// setLastError(ERR_MISSING_EC_KEY); -// return false; -// } -// #endif - // Validation context, either full CA validation or checking only fingerprints #ifdef USE_MQTT_TLS_CA_CERT br_x509_minimal_context *x509_minimal; diff --git a/tasmota/i18n.h b/tasmota/i18n.h index ba7e925f8..50c87660f 100644 --- a/tasmota/i18n.h +++ b/tasmota/i18n.h @@ -585,6 +585,7 @@ #define D_CMND_SHUTTER_TOGGLEDIR "ToggleDir" #define D_CMND_SHUTTER_UP "Up" #define D_CMND_SHUTTER_DOWN "Down" +#define D_CMND_SHUTTER_MODE "Mode" #define D_CMND_SHUTTER_STOPOPEN "StopOpen" #define D_CMND_SHUTTER_STOPCLOSE "StopClose" #define D_CMND_SHUTTER_STOPTOGGLE "StopToggle" @@ -606,6 +607,7 @@ #define D_CMND_SHUTTER_LOCK "Lock" #define D_CMND_SHUTTER_ENABLEENDSTOPTIME "EnableEndStopTime" #define D_CMND_SHUTTER_INVERTWEBBUTTONS "InvertWebButtons" +#define D_CMND_SHUTTER_PWMRANGE "PWMRange" // Commands xdrv_32_hotplug.ino #define D_CMND_HOTPLUG "HotPlug" @@ -722,7 +724,7 @@ const char S_RSLT_POWER[] PROGMEM = D_RSLT_POWER; const char S_RSLT_RESULT[] PROGMEM = D_RSLT_RESULT; const char S_RSLT_WARNING[] PROGMEM = D_RSLT_WARNING; const char S_LWT[] PROGMEM = D_LWT; -const char S_OFFLINE[] PROGMEM = D_OFFLINE; +const char S_LWT_OFFLINE[] PROGMEM = MQTT_LWT_OFFLINE; // support.ino static const char kMonthNames[] = D_MONTH3LIST; diff --git a/tasmota/language/cs_CZ.h b/tasmota/language/cs_CZ.h index 1200ceab5..c7190998c 100644 --- a/tasmota/language/cs_CZ.h +++ b/tasmota/language/cs_CZ.h @@ -125,10 +125,10 @@ #define D_NOISE "Hluk" #define D_NONE "Žádný" #define D_OFF "Vyp." -#define D_OFFLINE "Neaktivní" +#define D_OFFLINE "Offline" // Don't translate, LWT message! Nepředkládat, LWT zpráva! #define D_OK "OK" #define D_ON "Zap." -#define D_ONLINE "Aktivní" +#define D_ONLINE "Online" // Don't translate, LWT message! Nepředkládat, LWT zpráva! #define D_PASSWORD "Heslo" #define D_PORT "Port" #define D_POWER_FACTOR "Účiník" diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 5f94c5fc0..eed9062f0 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -369,8 +369,11 @@ //#define USE_ARDUINO_OTA // Add optional support for Arduino OTA (+13k code) // -- MQTT ---------------------------------------- -#define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on) -#define MQTT_CLEAN_SESSION 1 // Mqtt clean session connection (0 = No clean session, 1 = Clean session (default)) +#define MQTT_LWT_OFFLINE "Offline" // MQTT LWT offline topic message +#define MQTT_LWT_ONLINE "Online" // MQTT LWT online topic message + +#define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on) +#define MQTT_CLEAN_SESSION 1 // Mqtt clean session connection (0 = No clean session, 1 = Clean session (default)) // -- MQTT - Domoticz ----------------------------- #define USE_DOMOTICZ // Enable Domoticz (+6k code, +0.3k mem) @@ -389,7 +392,8 @@ // #define USE_MQTT_TLS_CA_CERT // Force full CA validation instead of fingerprints, slower, but simpler to use. (+2.2k code, +1.9k mem during connection handshake) // This includes the LetsEncrypt CA in tasmota_ca.ino for verifying server certificates // #define USE_MQTT_TLS_FORCE_EC_CIPHER // Force Elliptic Curve cipher (higher security) required by some servers (automatically enabled with USE_MQTT_AWS_IOT) (+11.4k code, +0.4k mem) -// #define USE_MQTT_AWS_IOT // Enable MQTT for AWS IoT - requires a private key (+11.9k code, +0.4k mem) +// #define USE_MQTT_AWS_IOT_LIGHT // Enable MQTT for AWS IoT in light mode, with user/password instead of private certificate +// #define USE_MQTT_AWS_IOT // [Deprecated] Enable MQTT for AWS IoT - requires a private key (+11.9k code, +0.4k mem) // Note: you need to generate a private key + certificate per device and update 'tasmota/tasmota_aws_iot.cpp' // Full documentation here: https://github.com/arendst/Tasmota/wiki/AWS-IoT // #define USE_4K_RSA // Support 4096 bits certificates, instead of 2048 @@ -538,6 +542,7 @@ // #define USE_SPS30 // [I2cDriver30] Enable Sensiron SPS30 particle sensor (I2C address 0x69) (+1.7 code) #define USE_ADE7953 // [I2cDriver7] Enable ADE7953 Energy monitor as used on Shelly 2.5 (I2C address 0x38) (+1k5) // #define USE_VL53L0X // [I2cDriver31] Enable VL53L0x time of flight sensor (I2C address 0x29) (+4k code) +// #define USE_VL53L1X // [I2cDriver54] Enable support for VL53L1X sensor (I2C address 0x29) using Pololu VL53L1X library (+2k9 code) // #define USE_MLX90614 // [I2cDriver32] Enable MLX90614 ir temp sensor (I2C address 0x5a) (+0.6k code) // #define USE_CHIRP // [I2cDriver33] Enable CHIRP soil moisture sensor (variable I2C address, default 0x20) // #define USE_PAJ7620 // [I2cDriver34] Enable PAJ7620 gesture sensor (I2C address 0x73) (+2.5k code) @@ -557,6 +562,7 @@ // #define USE_VEML7700 // [I2cDriver50] Enable VEML7700 Ambient Light sensor (I2C addresses 0x10) (+4k5 code) // #define USE_MCP9808 // [I2cDriver51] Enable MCP9808 temperature sensor (I2C addresses 0x18 - 0x1F) (+0k9 code) // #define USE_HP303B // [I2cDriver52] Enable HP303B temperature and pressure sensor (I2C address 0x76 or 0x77) (+6k2 code) +// #define USE_MLX90640 // [I2cDriver53] Enable MLX90640 IR array temperature sensor (I2C address 0x33) (+20k code) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 @@ -820,7 +826,7 @@ #include "user_config_override.h" // Configuration overrides for my_user_config.h #endif -#if defined(USE_DISCOVERY) && defined(USE_MQTT_AWS_IOT) +#if defined(USE_DISCOVERY) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_AWS_IOT_LIGHT)) #error "Select either USE_DISCOVERY or USE_MQTT_AWS_IOT, mDNS takes too much code space and is not needed for AWS IoT" #endif diff --git a/tasmota/sendemail.ino b/tasmota/sendemail.ino index 0a4c4de0e..bc080bd7f 100644 --- a/tasmota/sendemail.ino +++ b/tasmota/sendemail.ino @@ -330,6 +330,12 @@ String buffer; if (!buffer.startsWith(F("354"))) { goto exit; } + + buffer = F("MIME-Version: 1.0\r\n"); + client->print(buffer); + buffer = F("Content-Type: Multipart/mixed; boundary=frontier\r\n"); + client->print(buffer); + buffer = F("From: "); buffer += from; client->println(buffer); @@ -350,6 +356,7 @@ String buffer; AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),buffer.c_str()); #endif + #ifdef USE_SCRIPT if (*msg=='*' && *(msg+1)==0) { g_client=client; @@ -378,9 +385,98 @@ exit: } void xsend_message_txt(char *msg) { - g_client->println(msg); -} +#ifdef DEBUG_EMAIL_PORT + AddLog_P2(LOG_LEVEL_INFO, PSTR("%s"),msg); +#endif +#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) + if (*msg=='@') { + msg++; + attach_File(msg); + } else if (*msg=='&') { + msg++; + attach_Array(msg); + } else { + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n\r\n")); + g_client->println(msg); + g_client->print(F("\r\n--frontier\r\n")); + } #else + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n\r\n")); + g_client->println(msg); + g_client->print(F("\r\n--frontier\r\n")); +#endif +} + +#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) +#include +extern FS *fsp; + +void attach_File(char *path) { + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n")); + char buff[64]; + char *cp = path; + while (*cp=='/') cp++; + File file = fsp->open(path, "r"); + if (file) { + sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n"), cp); + g_client->write(buff); + uint16_t flen = file.size(); + uint8_t fbuff[64]; + uint16_t blen = sizeof(fbuff); + while (flen>0) { + file.read(fbuff, blen); + flen -= blen; + g_client->write(fbuff, blen); + if (flenprint(F("\r\n\r\nfile not found!\r\n")); + } + g_client->print(F("\r\n--frontier\r\n")); +} + +float *get_array_by_name(char *name, uint16_t *alen); +void flt2char(float num, char *nbuff); + +void attach_Array(char *aname) { + float *array = 0; + uint16_t alen; + array = get_array_by_name(aname, &alen); + g_client->print(F("--frontier\r\n")); + g_client->print(F("Content-Type: text/plain\r\n")); + if (array && alen) { +#ifdef DEBUG_EMAIL_PORT + AddLog_P2(LOG_LEVEL_INFO, PSTR("array found %d"),alen); +#endif + char buff[64]; + sprintf_P(buff,PSTR("Content-Disposition: attachment; filename=\"%s.txt\"\r\n\r\n"), aname); + g_client->write(buff); + float *fp=array; + for (uint32_t cnt = 0; cntwrite(nbuff, strlen(nbuff)); + } + } else { + g_client->print(F("\r\n\r\narray not found!\r\n")); + } + g_client->print(F("\r\n--frontier\r\n")); +} + +#endif // defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) + +#else + /* * Created by K. Suwatchai (Mobizt) @@ -413,6 +509,9 @@ void script_send_email_body(void(*func)(char *)); #define xPSTR(a) a //The Email Sending data object contains config and data to send SMTPData smtpData; +#define MAX_ATTCHMENTS 8 +char *attachments[MAX_ATTCHMENTS]; +uint8_t num_attachments; //Callback function to get the Email sending status //void sendCallback(SendStatus info); @@ -435,7 +534,7 @@ uint16_t SendMail(char *buffer) { // return if not enough memory uint32_t mem=ESP.getFreeHeap(); - AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem); + //AddLog_P2(LOG_LEVEL_INFO, PSTR("heap: %d"),mem); if (memprint(F("\r\n\r\narray not found!\r\n")); + } +} void send_message_txt(char *txt) { - if (*txt=='&') { + if (*txt=='@') { txt++; smtpData.addAttachFile(txt); + } else if (*txt=='&') { + txt++; + attach_Array(txt); } else if (*txt=='$') { txt++; #if defined(ESP32) && defined(USE_WEBCAM) diff --git a/tasmota/settings.h b/tasmota/settings.h index 60c837364..83f6a1a17 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -129,9 +129,9 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t virtual_ct_cw : 1; // bit 25 (v8.4.0.1) - SetOption107 - Virtual CT Channel - signals whether the hardware white is cold CW (true) or warm WW (false) uint32_t teleinfo_rawdata : 1; // bit 26 (v8.4.0.2) - SetOption108 - enable Teleinfo + Tasmota Energy device (0) or Teleinfo raw data only (1) uint32_t alexa_gen_1 : 1; // bit 27 (v8.4.0.3) - SetOption109 - Alexa gen1 mode - if you only have Echo Dot 2nd gen devices - uint32_t spare28 : 1; // bit 28 - uint32_t spare29 : 1; // bit 29 - uint32_t spare30 : 1; // bit 30 + uint32_t zb_disable_autobind : 1; // bit 28 (v8.5.0.1) - SetOption110 - disable Zigbee auto-config when pairing new devices + uint32_t buzzer_freq_mode : 1; // bit 29 (v8.5.0.1) - SetOption111 - Use frequency output for buzzer pin instead of on/off signal + uint32_t zb_topic_fname : 1; // bit 30 (v8.5.0.1) - SetOption112 - Use friendly name in zigbee topic (use with SetOption89) uint32_t spare31 : 1; // bit 31 }; } SysBitfield4; @@ -389,7 +389,7 @@ struct { uint8_t display_font; // 312 char ex_state_text[4][11]; // 313 - uint8_t ex_energy_power_delta; // 33F - Free since 6.6.0.20 + uint8_t tuyamcu_topic; // 33F Manage tuyaSend topic. ex_energy_power_delta on 6.6.0.20, replaced on 8.5.0.1 uint16_t domoticz_update_timer; // 340 uint16_t pwm_range; // 342 @@ -557,7 +557,7 @@ struct { uint16_t dimmer_hw_min; // E90 uint16_t dimmer_hw_max; // E92 uint32_t deepsleep; // E94 - uint16_t ex2_energy_power_delta; // E98 - Free since 8.4.0.3 + uint16_t hass_new_discovery; // E98 - ex2_energy_power_delta on 8.4.0.3, replaced on 8.5.0.1 uint8_t shutter_motordelay[MAX_SHUTTERS]; // E9A int8_t temp_comp; // E9E uint8_t weight_change; // E9F @@ -609,12 +609,12 @@ struct { uint8_t ledpwm_off; // F40 uint8_t tcp_baudrate; // F41 uint8_t fallback_module; // F42 - - uint8_t free_f43[1]; // F43 - + uint8_t shutter_mode; // F43 uint16_t energy_power_delta[3]; // F44 + uint16_t shutter_pwmrange[2][MAX_SHUTTERS]; // F4A - uint8_t free_f4e[106]; // F4A - Decrement if adding new Setting variables just above and below + + uint8_t free_f5a[89]; // F5A - Decrement if adding new Setting variables just above and below // Only 32 bit boundary variables below SysBitfield5 flag5; // FB4 diff --git a/tasmota/settings.ino b/tasmota/settings.ino index a26c89b3d..ddc5dfc2d 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -1356,8 +1356,8 @@ void SettingsDelta(void) Settings.ex_sbaudrate = 0; */ Settings.flag3.fast_power_cycle_disable = 0; - Settings.ex2_energy_power_delta = Settings.ex_energy_power_delta; - Settings.ex_energy_power_delta = 0; + Settings.hass_new_discovery = Settings.tuyamcu_topic; // replaced ex2_energy_power_delta on 8.5.0.1 + Settings.tuyamcu_topic = 0; // replaced ex_energy_power_delta on 8.5.0.1 } if (Settings.version < 0x06060015) { if ((EX_WIFI_SMARTCONFIG == Settings.ex_sta_config) || (EX_WIFI_WPSCONFIG == Settings.ex_sta_config)) { @@ -1514,7 +1514,7 @@ void SettingsDelta(void) Settings.fallback_module = FALLBACK_MODULE; } if (Settings.version < 0x08040003) { - Settings.energy_power_delta[0] = Settings.ex2_energy_power_delta; + Settings.energy_power_delta[0] = Settings.hass_new_discovery; // replaced ex2_energy_power_delta on 8.5.0.1 Settings.energy_power_delta[1] = 0; Settings.energy_power_delta[2] = 0; } diff --git a/tasmota/support.ino b/tasmota/support.ino index 8d4044906..0751b2b95 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -116,7 +116,7 @@ String GetResetReason(void) /*********************************************************************************************\ * Miscellaneous \*********************************************************************************************/ - +/* String GetBinary(const void* ptr, size_t count) { uint32_t value = *(uint32_t*)ptr; value <<= (32 - count); @@ -128,6 +128,18 @@ String GetBinary(const void* ptr, size_t count) { } return result; } +*/ +String GetBinary8(uint8_t value, size_t count) { + if (count > 8) { count = 8; } + value <<= (8 - count); + String result; + result.reserve(count + 1); + for (uint32_t i = 0; i < count; i++) { + result += (value &0x80) ? '1' : '0'; + value <<= 1; + } + return result; +} // Get span until single character in string size_t strchrspn(const char *str1, int character) @@ -1412,31 +1424,28 @@ bool GetUsedInModule(uint32_t val, uint16_t *arr) return false; } -bool JsonTemplate(const char* dataBuf) +bool JsonTemplate(char* dataBuf) { // {"NAME":"Generic","GPIO":[17,254,29,254,7,254,254,254,138,254,139,254,254],"FLAG":1,"BASE":255} if (strlen(dataBuf) < 9) { return false; } // Workaround exception if empty JSON like {} - Needs checks -#ifdef ESP8266 - StaticJsonBuffer<400> jb; // 331 from https://arduinojson.org/v5/assistant/ -#else - StaticJsonBuffer<999> jb; // 654 from https://arduinojson.org/v5/assistant/ -#endif - JsonObject& obj = jb.parseObject(dataBuf); - if (!obj.success()) { return false; } + JsonParser parser((char*) dataBuf); + JsonParserObject root = parser.getRootObject(); + if (!root) { return false; } // All parameters are optional allowing for partial changes - const char* name = obj[D_JSON_NAME]; - if (name != nullptr) { - SettingsUpdateText(SET_TEMPLATE_NAME, name); + JsonParserToken val = root[PSTR(D_JSON_NAME)]; + if (val) { + SettingsUpdateText(SET_TEMPLATE_NAME, val.getStr()); } - if (obj[D_JSON_GPIO].success()) { + JsonParserArray arr = root[PSTR(D_JSON_GPIO)]; + if (arr) { 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; + Settings.user_template.gp.io[i] = arr[i].getUInt(); #else // ESP32 - uint16_t gpio = obj[D_JSON_GPIO][i] | 0; + uint16_t gpio = arr[i].getUInt(); if (gpio == (AGPIO(GPIO_NONE) +1)) { gpio = AGPIO(GPIO_USER); } @@ -1444,12 +1453,14 @@ bool JsonTemplate(const char* dataBuf) #endif } } - if (obj[D_JSON_FLAG].success()) { - uint32_t flag = obj[D_JSON_FLAG] | 0; + val = root[PSTR(D_JSON_FLAG)]; + if (val) { + uint32_t flag = val.getUInt(); memcpy(&Settings.user_template.flag, &flag, sizeof(gpio_flag)); } - if (obj[D_JSON_BASE].success()) { - uint32_t base = obj[D_JSON_BASE]; + val = root[PSTR(D_JSON_BASE)]; + if (val) { + uint32_t base = val.getUInt(); if ((0 == base) || !ValidTemplateModule(base -1)) { base = 18; } Settings.user_template_base = base -1; // Default WEMOS } @@ -2006,3 +2017,27 @@ String Decompress(const char * compressed, size_t uncompressed_size) { } #endif // USE_UNISHOX_COMPRESSION + +/*********************************************************************************************\ + * High entropy hardware random generator + * Thanks to DigitalAlchemist +\*********************************************************************************************/ +// Based on code from https://raw.githubusercontent.com/espressif/esp-idf/master/components/esp32/hw_random.c +uint32_t HwRandom(void) { +#if ESP8266 + // https://web.archive.org/web/20160922031242/http://esp8266-re.foogod.com/wiki/Random_Number_Generator + #define _RAND_ADDR 0x3FF20E44UL +#else // ESP32 + #define _RAND_ADDR 0x3FF75144UL +#endif + static uint32_t last_ccount = 0; + uint32_t ccount; + uint32_t result = 0; + do { + ccount = ESP.getCycleCount(); + result ^= *(volatile uint32_t *)_RAND_ADDR; + } while (ccount - last_ccount < 64); + last_ccount = ccount; + return result ^ *(volatile uint32_t *)_RAND_ADDR; +#undef _RAND_ADDR +} diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 0cdba2969..67fa2d07f 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -583,10 +583,12 @@ void CmndStatus(void) if (i > 0) { ResponseAppend_P(PSTR(",")); } ResponseAppend_P(PSTR("{\"" D_STATUS13_SHUTTER "%d\":{\"Relay1\":%d,\"Relay2\":%d,\"Open\":%d,\"Close\":%d," "\"50perc\":%d,\"Delay\":%d,\"Opt\":\"%s\"," - "\"Calib\":\"%d:%d:%d:%d:%d\"}"), + "\"Calib\":\"%d:%d:%d:%d:%d\"," + "\"Mode\":\"%d\"}}"), i, Settings.shutter_startrelay[i], Settings.shutter_startrelay[i] +1, Settings.shutter_opentime[i], Settings.shutter_closetime[i], - Settings.shutter_set50percent[i], Settings.shutter_motordelay[i], GetBinary(&Settings.shutter_options[i], 4).c_str(), - Settings.shuttercoeff[0][i], Settings.shuttercoeff[1][i], Settings.shuttercoeff[2][i], Settings.shuttercoeff[3][i], Settings.shuttercoeff[4][i]); + Settings.shutter_set50percent[i], Settings.shutter_motordelay[i], GetBinary8(Settings.shutter_options[i], 4).c_str(), + Settings.shuttercoeff[0][i], Settings.shuttercoeff[1][i], Settings.shuttercoeff[2][i], Settings.shuttercoeff[3][i], Settings.shuttercoeff[4][i], + Settings.shutter_mode); } ResponseJsonEnd(); MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "13")); diff --git a/tasmota/support_features.ino b/tasmota/support_features.ino index d7f8328d3..dc49a757f 100644 --- a/tasmota/support_features.ino +++ b/tasmota/support_features.ino @@ -601,12 +601,14 @@ void GetFeatures(void) #ifdef USE_I2S_AUDIO feature6 |= 0x00800000; // xdrv_42_i2s_audio.ino #endif - -// feature6 |= 0x01000000; -// feature6 |= 0x02000000; +#ifdef USE_MLX90640 + feature6 |= 0x01000000; // xdrv_43_mlx90640.ino +#endif +#if defined(USE_I2C) && defined(USE_VL53L1X) + feature6 |= 0x02000000; // xsns_77_vl53l1x.ino +#endif // feature6 |= 0x04000000; // feature6 |= 0x08000000; - // feature6 |= 0x10000000; #if defined(ESP32) && defined(USE_TTGO_WATCH) feature6 |= 0x20000000; // xdrv_83_esp32watch.ino diff --git a/tasmota/support_json.ino b/tasmota/support_json.ino deleted file mode 100644 index b78c68eab..000000000 --- a/tasmota/support_json.ino +++ /dev/null @@ -1,115 +0,0 @@ -/* - support_json.ino - JSON support functions - - Copyright (C) 2020 Theo Arends and Stephan Hadinger - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -/*********************************************************************************************\ - * JSON parsing -\*********************************************************************************************/ - -// does the character needs to be escaped, and if so with which character -char EscapeJSONChar(char c) { - if ((c == '\"') || (c == '\\')) { - return c; - } - if (c == '\n') { return 'n'; } - if (c == '\t') { return 't'; } - if (c == '\r') { return 'r'; } - if (c == '\f') { return 'f'; } - if (c == '\b') { return 'b'; } - return 0; -} - -String EscapeJSONString(const char *str) { - // As this function is used in ResponseCmndChar() and ResponseCmndIdxChar() - // it needs to be PROGMEM safe! - String r(""); - if (nullptr == str) { return r; } - - bool needs_escape = false; - size_t len_out = 1; - const char* c = str; - char ch = '.'; - while (ch != '\0') { - ch = pgm_read_byte(c++); - if (EscapeJSONChar(ch)) { - len_out++; - needs_escape = true; - } - len_out++; - } - - if (needs_escape) { - // we need to escape some chars - // allocate target buffer - r.reserve(len_out); - c = str; - char *d = r.begin(); - char ch = '.'; - while (ch != '\0') { - ch = pgm_read_byte(c++); - char c2 = EscapeJSONChar(ch); - if (c2) { - *d++ = '\\'; - *d++ = c2; - } else { - *d++ = ch; - } - } - *d = 0; // add NULL terminator - r = (char*) r.begin(); // assign the buffer to the string - } else { - r = FPSTR(str); - } - - return r; -} - -/*********************************************************************************************\ - * Find key - case insensitive -\*********************************************************************************************/ - -// Given a JsonObject, finds the value as JsonVariant for the key needle. -// The search is case-insensitive, and will find the first match in the order of keys in JSON -// -// If the key is not found, returns a nullptr -// Input: needle cannot be NULL but may be PROGMEM -const JsonVariant &GetCaseInsensitive(const JsonObject &json, const char *needle) { - // key can be in PROGMEM - // if needle == "?" then we return the first valid key - bool wildcard = strcmp_P("?", needle) == 0; - if ((nullptr == &json) || (nullptr == needle) || (0 == pgm_read_byte(needle)) || (!json.success())) { - return *(JsonVariant*)nullptr; - } - - for (JsonObject::const_iterator it=json.begin(); it!=json.end(); ++it) { - const char *key = it->key; - const JsonVariant &value = it->value; - - if (wildcard || (0 == strcasecmp_P(key, needle))) { - return value; - } - } - // if not found - return *(JsonVariant*)nullptr; -} - -// This function returns true if the JsonObject contains the specified key -// It's just a wrapper to the previous function but it can be tricky to test nullptr on an object ref -bool HasKeyCaseInsensitive(const JsonObject &json, const char *needle) { - return &GetCaseInsensitive(json, needle) != nullptr; -} diff --git a/tasmota/tasmota.h b/tasmota/tasmota.h index 96cd9e49c..cffb9e57c 100644 --- a/tasmota/tasmota.h +++ b/tasmota/tasmota.h @@ -353,10 +353,13 @@ const SerConfu8 kTasmotaSerialConfig[] PROGMEM = { enum TuyaSupportedFunctions { TUYA_MCU_FUNC_NONE, TUYA_MCU_FUNC_SWT1 = 1, TUYA_MCU_FUNC_SWT2, TUYA_MCU_FUNC_SWT3, TUYA_MCU_FUNC_SWT4, TUYA_MCU_FUNC_REL1 = 11, TUYA_MCU_FUNC_REL2, TUYA_MCU_FUNC_REL3, TUYA_MCU_FUNC_REL4, TUYA_MCU_FUNC_REL5, - TUYA_MCU_FUNC_REL6, TUYA_MCU_FUNC_REL7, TUYA_MCU_FUNC_REL8, TUYA_MCU_FUNC_DIMMER = 21, TUYA_MCU_FUNC_POWER = 31, - TUYA_MCU_FUNC_CURRENT, TUYA_MCU_FUNC_VOLTAGE, TUYA_MCU_FUNC_BATTERY_STATE, TUYA_MCU_FUNC_BATTERY_PERCENTAGE, + TUYA_MCU_FUNC_REL6, TUYA_MCU_FUNC_REL7, TUYA_MCU_FUNC_REL8, TUYA_MCU_FUNC_DIMMER = 21, TUYA_MCU_FUNC_DIMMER2, + TUYA_MCU_FUNC_CT, TUYA_MCU_FUNC_RGB, TUYA_MCU_FUNC_WHITE, TUYA_MCU_FUNC_MODESET, TUYA_MCU_FUNC_REPORT1, TUYA_MCU_FUNC_REPORT2, + TUYA_MCU_FUNC_POWER = 31, TUYA_MCU_FUNC_CURRENT, TUYA_MCU_FUNC_VOLTAGE, TUYA_MCU_FUNC_BATTERY_STATE, TUYA_MCU_FUNC_BATTERY_PERCENTAGE, TUYA_MCU_FUNC_REL1_INV = 41, TUYA_MCU_FUNC_REL2_INV, TUYA_MCU_FUNC_REL3_INV, TUYA_MCU_FUNC_REL4_INV, TUYA_MCU_FUNC_REL5_INV, - TUYA_MCU_FUNC_REL6_INV, TUYA_MCU_FUNC_REL7_INV, TUYA_MCU_FUNC_REL8_INV, TUYA_MCU_FUNC_LOWPOWER_MODE = 51, TUYA_MCU_FUNC_LAST = 255 + TUYA_MCU_FUNC_REL6_INV, TUYA_MCU_FUNC_REL7_INV, TUYA_MCU_FUNC_REL8_INV, TUYA_MCU_FUNC_LOWPOWER_MODE = 51, + TUYA_MCU_FUNC_FAN3 = 61, TUYA_MCU_FUNC_FAN4, TUYA_MCU_FUNC_FAN5, TUYA_MCU_FUNC_FAN6, + TUYA_MCU_FUNC_MOTOR_DIR = 97, TUYA_MCU_FUNC_ERROR = 98 , TUYA_MCU_FUNC_DUMMY = 99, TUYA_MCU_FUNC_LAST = 255 }; #endif // _TASMOTA_H_ diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index b8d3293da..9ffed1f52 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -50,7 +50,8 @@ #include // Ota #include // Ota #include // Webserver, Updater -#include // WemoHue, IRremote, Domoticz +#include +#include #ifdef USE_ARDUINO_OTA #include // Arduino OTA #ifndef USE_DISCOVERY diff --git a/tasmota/tasmota_configurations.h b/tasmota/tasmota_configurations.h index 25da1c180..f94844bf9 100644 --- a/tasmota/tasmota_configurations.h +++ b/tasmota/tasmota_configurations.h @@ -113,6 +113,7 @@ //#define USE_SPS30 // Enable Sensiron SPS30 particle sensor (I2C address 0x69) (+1.7 code) #define USE_ADE7953 // Enable ADE7953 Energy monitor as used on Shelly 2.5 (I2C address 0x38) (+1k5) //#define USE_VL53L0X // Enable VL53L0x time of flight sensor (I2C address 0x29) (+4k code) +//#define USE_VL53L1X // Enable support for VL53L1X sensor (I2C address 0x29) using Pololu VL53L1X library (+2k9 code) //#define USE_MLX90614 // Enable MLX90614 ir temp sensor (I2C address 0x5a) (+0.6k code) //#define USE_CHIRP // Enable CHIRP soil moisture sensor (variable I2C address, default 0x20) //#define USE_PAJ7620 // Enable PAJ7620 gesture sensor (I2C address 0x73) (+2.5k code) diff --git a/tasmota/tasmota_globals.h b/tasmota/tasmota_globals.h index edf933c12..8c6215eab 100644 --- a/tasmota/tasmota_globals.h +++ b/tasmota/tasmota_globals.h @@ -122,11 +122,11 @@ String EthernetMacAddress(void); #define WS2812_LEDS 30 // [Pixels] Number of LEDs #endif -#ifdef USE_MQTT_TLS - const uint16_t WEB_LOG_SIZE = 2000; // Max number of characters in weblog -#else +//#ifdef USE_MQTT_TLS // Set to 4000 on 20200922 per #9305 +// const uint16_t WEB_LOG_SIZE = 2000; // Max number of characters in weblog +//#else const uint16_t WEB_LOG_SIZE = 4000; // Max number of characters in weblog -#endif +//#endif #if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2) || defined(ARDUINO_ESP8266_RELEASE_2_5_0) || defined(ARDUINO_ESP8266_RELEASE_2_5_1) || defined(ARDUINO_ESP8266_RELEASE_2_5_2) #error "Arduino ESP8266 Core versions before 2.7.1 are not supported" @@ -144,6 +144,12 @@ String EthernetMacAddress(void); #ifndef MQTT_CLEAN_SESSION #define MQTT_CLEAN_SESSION 1 // 0 = No clean session, 1 = Clean session (default) #endif +#ifndef MQTT_LWT_OFFLINE +#define MQTT_LWT_OFFLINE "Offline" // MQTT LWT offline topic message +#endif +#ifndef MQTT_LWT_ONLINE +#define MQTT_LWT_ONLINE "Online" // MQTT LWT online topic message +#endif #ifndef MESSZ //#define MESSZ 1040 // Max number of characters in JSON message string (Hass discovery and nice MQTT_MAX_PACKET_SIZE = 1200) diff --git a/tasmota/tasmota_template.h b/tasmota/tasmota_template.h index 32a914f8f..944165eef 100644 --- a/tasmota/tasmota_template.h +++ b/tasmota/tasmota_template.h @@ -254,6 +254,229 @@ enum ProgramSelectablePins { GPIO_USER, // User configurable needs to be 255 GPIO_MAX }; +/* +// Indexed by UserSelectablePins to convert legacy (8-bit) GPIOs +const uint16_t kGpioConvert[] PROGMEM = { + GPIO_NONE, + AGPIO(GPIO_DHT11), // DHT11 + AGPIO(GPIO_DHT22), // DHT21, DHT22, AM2301, AM2302, AM2321 + AGPIO(GPIO_SI7021), // iTead SI7021 + AGPIO(GPIO_DSB), // Single wire DS18B20 or DS18S20 + AGPIO(GPIO_I2C_SCL), // I2C SCL + AGPIO(GPIO_I2C_SDA), // I2C SDA + AGPIO(GPIO_WS2812), // WS2812 Led string + AGPIO(GPIO_IRSEND), // IR remote + AGPIO(GPIO_SWT1), // Switch + AGPIO(GPIO_SWT1) +1, + AGPIO(GPIO_SWT1) +2, + AGPIO(GPIO_SWT1) +3, + AGPIO(GPIO_SWT1) +4, + AGPIO(GPIO_SWT1) +5, + AGPIO(GPIO_SWT1) +6, + AGPIO(GPIO_SWT1) +7, + AGPIO(GPIO_KEY1), // Button + AGPIO(GPIO_KEY1) +1, + AGPIO(GPIO_KEY1) +2, + AGPIO(GPIO_KEY1) +3, + AGPIO(GPIO_REL1), // Relay + AGPIO(GPIO_REL1) +1, + AGPIO(GPIO_REL1) +2, + AGPIO(GPIO_REL1) +3, + AGPIO(GPIO_REL1) +4, + AGPIO(GPIO_REL1) +5, + AGPIO(GPIO_REL1) +6, + AGPIO(GPIO_REL1) +7, + AGPIO(GPIO_REL1_INV), // Relay inverted + AGPIO(GPIO_REL1_INV) +1, + AGPIO(GPIO_REL1_INV) +2, + AGPIO(GPIO_REL1_INV) +3, + AGPIO(GPIO_REL1_INV) +4, + AGPIO(GPIO_REL1_INV) +5, + AGPIO(GPIO_REL1_INV) +6, + AGPIO(GPIO_REL1_INV) +7, + AGPIO(GPIO_PWM1), // PWM + AGPIO(GPIO_PWM1) +1, + AGPIO(GPIO_PWM1) +2, + AGPIO(GPIO_PWM1) +3, + AGPIO(GPIO_PWM1) +4, + AGPIO(GPIO_CNTR1), // Counter + AGPIO(GPIO_CNTR1) +1, + AGPIO(GPIO_CNTR1) +2, + AGPIO(GPIO_CNTR1) +3, + AGPIO(GPIO_PWM1_INV), // PWM inverted + AGPIO(GPIO_PWM1_INV) +1, + AGPIO(GPIO_PWM1_INV) +2, + AGPIO(GPIO_PWM1_INV) +3, + AGPIO(GPIO_PWM1_INV) +4, + AGPIO(GPIO_IRRECV), // IR receive + AGPIO(GPIO_LED1), // Led + AGPIO(GPIO_LED1) +1, + AGPIO(GPIO_LED1) +2, + AGPIO(GPIO_LED1) +3, + AGPIO(GPIO_LED1_INV), // Led inverted + AGPIO(GPIO_LED1_INV) +1, + AGPIO(GPIO_LED1_INV) +2, + AGPIO(GPIO_LED1_INV) +3, + AGPIO(GPIO_MHZ_TXD), // MH-Z19 Serial interface + AGPIO(GPIO_MHZ_RXD), + AGPIO(GPIO_PZEM0XX_TX), // PZEM0XX Serial interface + AGPIO(GPIO_PZEM004_RX), // PZEM004T Serial interface + AGPIO(GPIO_SAIR_TX), // SenseAir Serial interface + AGPIO(GPIO_SAIR_RX), // SenseAir Serial interface + AGPIO(GPIO_SPI_CS), // SPI Chip Select + AGPIO(GPIO_SPI_DC), // SPI Data Direction + AGPIO(GPIO_BACKLIGHT), // Display backlight control + AGPIO(GPIO_PMS5003_RX), // Plantower PMS5003 Serial interface + AGPIO(GPIO_SDS0X1_RX), // Nova Fitness SDS011 Serial interface + AGPIO(GPIO_SBR_TX), // Serial Bridge Serial interface + AGPIO(GPIO_SBR_RX), // Serial Bridge Serial interface + AGPIO(GPIO_SR04_TRIG), // SR04 Tri/TXgger pin + AGPIO(GPIO_SR04_ECHO), // SR04 Ech/RXo pin + AGPIO(GPIO_SDM120_TX), // SDM120 Serial interface + AGPIO(GPIO_SDM120_RX), // SDM120 Serial interface + AGPIO(GPIO_SDM630_TX), // SDM630 Serial interface + AGPIO(GPIO_SDM630_RX), // SDM630 Serial interface + AGPIO(GPIO_TM16CLK), // TM1638 Clock + AGPIO(GPIO_TM16DIO), // TM1638 Data I/O + AGPIO(GPIO_TM16STB), // TM1638 Strobe + AGPIO(GPIO_SWT1_NP), // Switch no pullup + AGPIO(GPIO_SWT1_NP) +1, + AGPIO(GPIO_SWT1_NP) +2, + AGPIO(GPIO_SWT1_NP) +3, + AGPIO(GPIO_SWT1_NP) +4, + AGPIO(GPIO_SWT1_NP) +5, + AGPIO(GPIO_SWT1_NP) +6, + AGPIO(GPIO_SWT1_NP) +7, + AGPIO(GPIO_KEY1_NP), // Button no pullup + AGPIO(GPIO_KEY1_NP) +1, + AGPIO(GPIO_KEY1_NP) +2, + AGPIO(GPIO_KEY1_NP) +3, + AGPIO(GPIO_CNTR1_NP), // Counter no pullup + AGPIO(GPIO_CNTR1_NP) +1, + AGPIO(GPIO_CNTR1_NP) +2, + AGPIO(GPIO_CNTR1_NP) +3, + AGPIO(GPIO_PZEM016_RX), // PZEM-014,016 Serial Modbus interface + AGPIO(GPIO_PZEM017_RX), // PZEM-003,017 Serial Modbus interface + AGPIO(GPIO_MP3_DFR562), // RB-DFR-562, DFPlayer Mini MP3 Player Serial interface + AGPIO(GPIO_SDS0X1_TX), // Nova Fitness SDS011 Serial interface + AGPIO(GPIO_HX711_SCK), // HX711 Load Cell clock + AGPIO(GPIO_HX711_DAT), // HX711 Load Cell data + AGPIO(GPIO_TX2X_TXD_BLACK), // TX20/TX23 Transmission Pin + AGPIO(GPIO_RFSEND), // RF transmitter + AGPIO(GPIO_RFRECV), // RF receiver + AGPIO(GPIO_TUYA_TX), // Tuya Serial interface + AGPIO(GPIO_TUYA_RX), // Tuya Serial interface + AGPIO(GPIO_MGC3130_XFER), + AGPIO(GPIO_MGC3130_RESET), + AGPIO(GPIO_SSPI_MISO), // Software SPI Master Input Client Output + AGPIO(GPIO_SSPI_MOSI), // Software SPI Master Output Client Input + AGPIO(GPIO_SSPI_SCLK), // Software SPI Serial Clock + AGPIO(GPIO_SSPI_CS), // Software SPI Chip Select + AGPIO(GPIO_SSPI_DC), // Software SPI Data or Command + AGPIO(GPIO_RF_SENSOR), // Rf receiver with sensor decoding + AGPIO(GPIO_AZ_TXD), // AZ-Instrument 7798 CO2 datalogger Serial interface + AGPIO(GPIO_AZ_RXD), // AZ-Instrument 7798 CO2 datalogger Serial interface + AGPIO(GPIO_MAX31855CS), // MAX31855 Serial interface + AGPIO(GPIO_MAX31855CLK), // MAX31855 Serial interface + AGPIO(GPIO_MAX31855DO), // MAX31855 Serial interface + AGPIO(GPIO_KEY1_INV), // Button inverted + AGPIO(GPIO_KEY1_INV) +1, + AGPIO(GPIO_KEY1_INV) +2, + AGPIO(GPIO_KEY1_INV) +3, + AGPIO(GPIO_KEY1_INV_NP), // Button inverted no pullup + AGPIO(GPIO_KEY1_INV_NP) +1, + AGPIO(GPIO_KEY1_INV_NP) +2, + AGPIO(GPIO_KEY1_INV_NP) +3, + AGPIO(GPIO_NRG_SEL), // HLW8012/HLJ-01 Sel output (1 = Voltage) + AGPIO(GPIO_NRG_SEL_INV), // HLW8012/HLJ-01 Sel output (0 = Voltage) + AGPIO(GPIO_NRG_CF1), // HLW8012/HLJ-01 CF1 voltage / current + AGPIO(GPIO_HLW_CF), // HLW8012 CF power + AGPIO(GPIO_HJL_CF), // HJL-01/BL0937 CF power + AGPIO(GPIO_MCP39F5_TX), // MCP39F501 Serial interface (Shelly2) + AGPIO(GPIO_MCP39F5_RX), // MCP39F501 Serial interface (Shelly2) + AGPIO(GPIO_MCP39F5_RST), // MCP39F501 Reset (Shelly2) + AGPIO(GPIO_PN532_TXD), // PN532 HSU Tx + AGPIO(GPIO_PN532_RXD), // PN532 HSU Rx + AGPIO(GPIO_SM16716_CLK), // SM16716 CLOCK + AGPIO(GPIO_SM16716_DAT), // SM16716 DATA + AGPIO(GPIO_SM16716_SEL), // SM16716 SELECT + AGPIO(GPIO_DI), // my92x1 PWM input + AGPIO(GPIO_DCKI), // my92x1 CLK input + AGPIO(GPIO_CSE7766_TX), // CSE7766 Serial interface (S31 and Pow R2) + AGPIO(GPIO_CSE7766_RX), // CSE7766 Serial interface (S31 and Pow R2) + AGPIO(GPIO_ARIRFRCV), // AriLux RF Receive input + AGPIO(GPIO_TXD), // Serial interface + AGPIO(GPIO_RXD), // Serial interface + AGPIO(GPIO_ROT1A), // Rotary A Pin + AGPIO(GPIO_ROT1B), // Rotary B Pin + AGPIO(GPIO_ROT1A) +1, // Rotary A Pin + AGPIO(GPIO_ROT1B) +1, // Rotary B Pin + AGPIO(GPIO_HRE_CLOCK), + AGPIO(GPIO_HRE_DATA), + AGPIO(GPIO_ADE7953_IRQ), // ADE7953 IRQ + AGPIO(GPIO_LEDLNK), // Link led + AGPIO(GPIO_LEDLNK_INV), // Inverted link led + AGPIO(GPIO_ARIRFSEL), // Arilux RF Receive input selected + AGPIO(GPIO_BUZZER), // Buzzer + AGPIO(GPIO_BUZZER_INV), // Inverted buzzer + AGPIO(GPIO_OLED_RESET), // OLED Display Reset + AGPIO(GPIO_SOLAXX1_TX), // Solax Inverter tx pin + AGPIO(GPIO_SOLAXX1_RX), // Solax Inverter rx pin + AGPIO(GPIO_ZIGBEE_TX), // Zigbee Serial interface + AGPIO(GPIO_ZIGBEE_RX), // Zigbee Serial interface + AGPIO(GPIO_RDM6300_RX), + AGPIO(GPIO_IBEACON_TX), + AGPIO(GPIO_IBEACON_RX), + AGPIO(GPIO_A4988_DIR), // A4988 direction pin + AGPIO(GPIO_A4988_STP), // A4988 step pin + AGPIO(GPIO_A4988_ENA), // A4988 enabled pin + AGPIO(GPIO_A4988_MS1), // A4988 microstep pin1 + AGPIO(GPIO_A4988_MS2), // A4988 microstep pin2 + AGPIO(GPIO_A4988_MS3), // A4988 microstep pin3 + AGPIO(GPIO_DDS2382_TX), // DDS2382 Serial interface + AGPIO(GPIO_DDS2382_RX), // DDS2382 Serial interface + AGPIO(GPIO_DDSU666_TX), // DDSU666 Serial interface + AGPIO(GPIO_DDSU666_RX), // DDSU666 Serial interface + AGPIO(GPIO_SM2135_CLK), // SM2135 CLOCK + AGPIO(GPIO_SM2135_DAT), // SM2135 DATA + AGPIO(GPIO_DEEPSLEEP), + AGPIO(GPIO_EXS_ENABLE), // EXS MCU Enable + AGPIO(GPIO_TASMOTACLIENT_TXD), // Tasmota Client TX + AGPIO(GPIO_TASMOTACLIENT_RXD), // Tasmota Client RX + AGPIO(GPIO_TASMOTACLIENT_RST), // Tasmota Client Reset + AGPIO(GPIO_TASMOTACLIENT_RST_INV), // Tasmota Client Reset Inverted + AGPIO(GPIO_HPMA_RX), // Honeywell HPMA115S0 Serial interface + AGPIO(GPIO_HPMA_TX), // Honeywell HPMA115S0 Serial interface + AGPIO(GPIO_GPS_RX), // GPS serial interface + AGPIO(GPIO_GPS_TX), // GPS serial interface + AGPIO(GPIO_DSB_OUT), // Pseudo Single wire DS18B20 or DS18S20 + AGPIO(GPIO_DHT11_OUT), // Pseudo Single wire DHT11, DHT21, DHT22, AM2301, AM2302, AM2321 + AGPIO(GPIO_HM10_RX), // GPS serial interface + AGPIO(GPIO_HM10_TX), // GPS serial interface + AGPIO(GPIO_LE01MR_RX), // F7F LE-01MR energy meter rx pin + AGPIO(GPIO_LE01MR_TX), // F7F LE-01MR energy meter tx pin + AGPIO(GPIO_CC1101_GDO0), // CC1101 pin for RX + AGPIO(GPIO_CC1101_GDO2), // CC1101 pin for RX + AGPIO(GPIO_HRXL_RX), + AGPIO(GPIO_ELECTRIQ_MOODL_TX), + AGPIO(GPIO_AS3935), // AS3935 IRQ Pin + AGPIO(GPIO_PMS5003_TX), // Plantower PMS5003 Serial interface + AGPIO(GPIO_BOILER_OT_RX), + AGPIO(GPIO_BOILER_OT_TX), + AGPIO(GPIO_WINDMETER_SPEED), + AGPIO(GPIO_BL0940_RX), // BL0940 Serial interface + AGPIO(GPIO_TCP_TX), // TCP Serial bridge + AGPIO(GPIO_TCP_RX), // TCP Serial bridge + AGPIO(GPIO_TELEINFO_RX), + AGPIO(GPIO_TELEINFO_ENABLE), + AGPIO(GPIO_LMT01), // LMT01, count pulses on GPIO + AGPIO(GPIO_IEM3000_TX), // IEM3000 Serial interface + AGPIO(GPIO_IEM3000_RX), // IEM3000 Serial interface + AGPIO(GPIO_ZIGBEE_RST), // Zigbee reset + AGPIO(GPIO_DYP_RX) +}; +*/ + // Text in webpage Module Parameters and commands GPIOS and GPIO const char kSensorNames[] PROGMEM = D_SENSOR_NONE "|" @@ -1032,1065 +1255,1065 @@ const uint8_t kModuleTemplateList[MAXMODULE] PROGMEM = { \*********************************************************************************************/ const mytmplt8266 kModules8266[TMP_MAXMODULE_8285] PROGMEM = { - { // SONOFF_BASIC - Sonoff Basic (ESP8266) - AGPIO(GPIO_KEY1), // GPIO00 Button - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Only available on newer Sonoff Basic R2 V1 - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_USER), // GPIO04 Optional sensor - 0, // GPIO05 - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Red Led and Relay (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Green Led (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_USER), // GPIO14 Optional sensor - 0, // GPIO15 - 0, // GPIO16 - 0 // ADC0 Analog input + { // SONOFF_BASIC - Sonoff Basic (ESP8266) + GPIO_KEY1, // GPIO00 Button + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Only available on newer Sonoff Basic R2 V1 + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_USER, // GPIO04 Optional sensor + 0, // GPIO05 + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Red Led and Relay (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Green Led (0 = On, 1 = Off) - Link and Power status + GPIO_USER, // GPIO14 Optional sensor + 0, // GPIO15 + 0, // GPIO16 + 0 // ADC0 Analog input }, - { // SONOFF_SV - Sonoff SV (ESP8266) - AGPIO(GPIO_KEY1), // GPIO00 Button - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor + { // SONOFF_SV - Sonoff SV (ESP8266) + GPIO_KEY1, // GPIO00 Button + GPIO_USER, // GPIO01 Serial RXD and Optional sensor 0, - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_USER), // GPIO04 Optional sensor - AGPIO(GPIO_USER), // GPIO05 Optional sensor - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Red Led and Relay (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Green Led (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_USER), // GPIO14 Optional sensor + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_USER, // GPIO04 Optional sensor + GPIO_USER, // GPIO05 Optional sensor + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Red Led and Relay (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Green Led (0 = On, 1 = Off) - Link and Power status + GPIO_USER, // GPIO14 Optional sensor 0, 0, - AGPIO(ADC0_USER) // ADC0 Analog input + ADC0_USER // ADC0 Analog input }, - { // SONOFF_DUAL - Sonoff Dual (ESP8266) - AGPIO(GPIO_USER), // GPIO00 Pad - AGPIO(GPIO_TXD), // GPIO01 Relay control + { // SONOFF_DUAL - Sonoff Dual (ESP8266) + GPIO_USER, // GPIO00 Pad + GPIO_TXD, // GPIO01 Relay control 0, - AGPIO(GPIO_RXD), // GPIO03 Relay control - AGPIO(GPIO_USER), // GPIO04 Optional sensor + GPIO_RXD, // GPIO03 Relay control + GPIO_USER, // GPIO04 Optional sensor 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) 0, - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_USER), // GPIO14 Optional sensor + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + GPIO_USER, // GPIO14 Optional sensor 0, 0, 0 }, - { // SONOFF_POW - Sonoff Pow (ESP8266 - HLW8012) - AGPIO(GPIO_KEY1), // GPIO00 Button + { // SONOFF_POW - Sonoff Pow (ESP8266 - HLW8012) + GPIO_KEY1, // GPIO00 Button 0, 0, 0, 0, - AGPIO(GPIO_NRG_SEL), // GPIO05 HLW8012 Sel output (1 = Voltage) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Red Led and Relay (0 = Off, 1 = On) - AGPIO(GPIO_NRG_CF1), // GPIO13 HLW8012 CF1 voltage / current - AGPIO(GPIO_HLW_CF), // GPIO14 HLW8012 CF power - AGPIO(GPIO_LED1), // GPIO15 Blue Led (0 = On, 1 = Off) - Link and Power status + GPIO_NRG_SEL, // GPIO05 HLW8012 Sel output (1 = Voltage) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Red Led and Relay (0 = Off, 1 = On) + GPIO_NRG_CF1, // GPIO13 HLW8012 CF1 voltage / current + GPIO_HLW_CF, // GPIO14 HLW8012 CF power + GPIO_LED1, // GPIO15 Blue Led (0 = On, 1 = Off) - Link and Power status 0, 0 }, - { // SONOFF_LED - Sonoff LED (ESP8266) - AGPIO(GPIO_KEY1), // GPIO00 Button + { // SONOFF_LED - Sonoff LED (ESP8266) + GPIO_KEY1, // GPIO00 Button 0, 0, 0, - AGPIO(GPIO_USER), // GPIO04 Optional sensor (PWM3 Green) - AGPIO(GPIO_USER), // GPIO05 Optional sensor (PWM2 Red) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1), // GPIO12 Cold light (PWM0 Cold) - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_PWM1) +1, // GPIO14 Warm light (PWM1 Warm) - AGPIO(GPIO_USER), // GPIO15 Optional sensor (PWM4 Blue) + GPIO_USER, // GPIO04 Optional sensor (PWM3 Green) + GPIO_USER, // GPIO05 Optional sensor (PWM2 Red) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM1, // GPIO12 Cold light (PWM0 Cold) + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + GPIO_PWM2, // GPIO14 Warm light (PWM1 Warm) + GPIO_USER, // GPIO15 Optional sensor (PWM4 Blue) 0, 0 }, - { // ELECTRODRAGON - ElectroDragon IoT Relay Board (ESP8266) - AGPIO(GPIO_KEY1) +1, // GPIO00 Button 2 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_KEY1), // GPIO02 Button 1 - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_USER), // GPIO04 Optional sensor - AGPIO(GPIO_USER), // GPIO05 Optional sensor - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1) +1, // GPIO12 Red Led and Relay 2 (0 = Off, 1 = On) - AGPIO(GPIO_REL1), // GPIO13 Red Led and Relay 1 (0 = Off, 1 = On) - AGPIO(GPIO_USER), // GPIO14 Optional sensor - AGPIO(GPIO_USER), // GPIO15 Optional sensor - AGPIO(GPIO_LED1), // GPIO16 Green/Blue Led (1 = On, 0 = Off) - Link and Power status - AGPIO(ADC0_USER) // ADC0 A0 Analog input + { // ELECTRODRAGON - ElectroDragon IoT Relay Board (ESP8266) + GPIO_KEY2, // GPIO00 Button 2 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_KEY1, // GPIO02 Button 1 + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_USER, // GPIO04 Optional sensor + GPIO_USER, // GPIO05 Optional sensor + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL2, // GPIO12 Red Led and Relay 2 (0 = Off, 1 = On) + GPIO_REL1, // GPIO13 Red Led and Relay 1 (0 = Off, 1 = On) + GPIO_USER, // GPIO14 Optional sensor + GPIO_USER, // GPIO15 Optional sensor + GPIO_LED1, // GPIO16 Green/Blue Led (1 = On, 0 = Off) - Link and Power status + ADC0_USER // ADC0 A0 Analog input }, - { // EXS_RELAY - ES-Store Latching relay(s) (ESP8266) - // https://ex-store.de/ESP8266-WiFi-Relay-V31 - // V3.1 Module Pin 1 VCC 3V3, Module Pin 6 GND - // https://ex-store.de/2-Kanal-WiFi-WLan-Relay-V5-Blackline-fuer-Unterputzmontage - AGPIO(GPIO_USER), // GPIO00 V3.1 Module Pin 8 - V5.0 Module Pin 4 - AGPIO(GPIO_USER), // GPIO01 UART0_TXD V3.1 Module Pin 2 - V5.0 Module Pin 3 - AGPIO(GPIO_USER), // GPIO02 V3.1 Module Pin 7 - AGPIO(GPIO_USER), // GPIO03 UART0_RXD V3.1 Module Pin 3 - AGPIO(GPIO_USER), // GPIO04 V3.1 Module Pin 10 - V5.0 Module Pin 2 - AGPIO(GPIO_USER), // GPIO05 V3.1 Module Pin 9 - V5.0 Module Pin 1 - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Relay1 ( 1 = Off) - AGPIO(GPIO_REL1) +1, // GPIO13 Relay1 ( 1 = On) - AGPIO(GPIO_USER), // GPIO14 V3.1 Module Pin 5 - V5.0 GPIO_REL3_INV Relay2 ( 1 = Off) - AGPIO(GPIO_LED1), // GPIO15 V5.0 LED1 - Link and Power status - AGPIO(GPIO_USER), // GPIO16 V3.1 Module Pin 4 - V5.0 GPIO_REL4_INV Relay2 ( 1 = On) + { // EXS_RELAY - ES-Store Latching relay(s) (ESP8266) + // https://ex-store.de/ESP8266-WiFi-Relay-V31 + // V3.1 Module Pin 1 VCC 3V3, Module Pin 6 GND + // https://ex-store.de/2-Kanal-WiFi-WLan-Relay-V5-Blackline-fuer-Unterputzmontage + GPIO_USER, // GPIO00 V3.1 Module Pin 8 - V5.0 Module Pin 4 + GPIO_USER, // GPIO01 UART0_TXD V3.1 Module Pin 2 - V5.0 Module Pin 3 + GPIO_USER, // GPIO02 V3.1 Module Pin 7 + GPIO_USER, // GPIO03 UART0_RXD V3.1 Module Pin 3 + GPIO_USER, // GPIO04 V3.1 Module Pin 10 - V5.0 Module Pin 2 + GPIO_USER, // GPIO05 V3.1 Module Pin 9 - V5.0 Module Pin 1 + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Relay1 ( 1 = Off) + GPIO_REL2, // GPIO13 Relay1 ( 1 = On) + GPIO_USER, // GPIO14 V3.1 Module Pin 5 - V5.0 GPIO_REL3_INV Relay2 ( 1 = Off) + GPIO_LED1, // GPIO15 V5.0 LED1 - Link and Power status + GPIO_USER, // GPIO16 V3.1 Module Pin 4 - V5.0 GPIO_REL4_INV Relay2 ( 1 = On) 0 }, - { // WION - Indoor Tap (ESP8266) - // https://www.amazon.com/gp/product/B00ZYLUBJU/ref=s9_acsd_al_bw_c_x_3_w - AGPIO(GPIO_USER), // GPIO00 Optional sensor (pm clock) + { // WION - Indoor Tap (ESP8266) + // https://www.amazon.com/gp/product/B00ZYLUBJU/ref=s9_acsd_al_bw_c_x_3_w + GPIO_USER, // GPIO00 Optional sensor (pm clock) 0, - AGPIO(GPIO_LED1), // GPIO02 Green Led (1 = On, 0 = Off) - Link and Power status + GPIO_LED1, // GPIO02 Green Led (1 = On, 0 = Off) - Link and Power status 0, 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 Optional sensor (pm data) - AGPIO(GPIO_KEY1), // GPIO13 Button + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 Optional sensor (pm data) + GPIO_KEY1, // GPIO13 Button 0, - AGPIO(GPIO_REL1), // GPIO15 Relay (0 = Off, 1 = On) + GPIO_REL1, // GPIO15 Relay (0 = Off, 1 = On) 0, 0 }, - { // SONOFF_DEV - Sonoff Dev (ESP8266) - AGPIO(GPIO_KEY1), // GPIO00 E-FW Button - AGPIO(GPIO_USER), // GPIO01 TX Serial RXD and Optional sensor + { // SONOFF_DEV - Sonoff Dev (ESP8266) + GPIO_KEY1, // GPIO00 E-FW Button + GPIO_USER, // GPIO01 TX Serial RXD and Optional sensor 0, // GPIO02 - AGPIO(GPIO_USER), // GPIO03 RX Serial TXD and Optional sensor - AGPIO(GPIO_USER), // GPIO04 Optional sensor - AGPIO(GPIO_USER), // GPIO05 Optional sensor - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 - AGPIO(GPIO_USER), // GPIO13 BLUE LED - AGPIO(GPIO_USER), // GPIO14 Optional sensor - 0, // GPIO15 - 0, // GPIO16 - AGPIO(ADC0_USER) // ADC0 A0 Analog input + GPIO_USER, // GPIO03 RX Serial TXD and Optional sensor + GPIO_USER, // GPIO04 Optional sensor + GPIO_USER, // GPIO05 Optional sensor + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 + GPIO_USER, // GPIO13 BLUE LED + GPIO_USER, // GPIO14 Optional sensor + 0, // GPIO15 + 0, // GPIO16 + ADC0_USER // ADC0 A0 Analog input }, - { // H801 - Lixada H801 Wifi (ESP8266) - AGPIO(GPIO_USER), // GPIO00 E-FW Button - AGPIO(GPIO_LED1), // GPIO01 Green LED - Link and Power status - AGPIO(GPIO_USER), // GPIO02 TX and Optional sensor - Pin next to TX on the PCB - AGPIO(GPIO_USER), // GPIO03 RX and Optional sensor - Pin next to GND on the PCB - AGPIO(GPIO_PWM1) +4, // GPIO04 W2 - PWM5 - AGPIO(GPIO_LED1_INV) +1, // GPIO05 Red LED - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +2, // GPIO12 Blue - AGPIO(GPIO_PWM1) +1, // GPIO13 Green - AGPIO(GPIO_PWM1) +3, // GPIO14 W1 - PWM4 - AGPIO(GPIO_PWM1), // GPIO15 Red + { // H801 - Lixada H801 Wifi (ESP8266) + GPIO_USER, // GPIO00 E-FW Button + GPIO_LED1, // GPIO01 Green LED - Link and Power status + GPIO_USER, // GPIO02 TX and Optional sensor - Pin next to TX on the PCB + GPIO_USER, // GPIO03 RX and Optional sensor - Pin next to GND on the PCB + GPIO_PWM5, // GPIO04 W2 - PWM5 + GPIO_LED2_INV, // GPIO05 Red LED + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM3, // GPIO12 Blue + GPIO_PWM2, // GPIO13 Green + GPIO_PWM4, // GPIO14 W1 - PWM4 + GPIO_PWM1, // GPIO15 Red 0, 0 }, - { // SONOFF_SC - onoff SC (ESP8266) - AGPIO(GPIO_KEY1), // GPIO00 Button - AGPIO(GPIO_TXD), // GPIO01 RXD to ATMEGA328P - AGPIO(GPIO_USER), // GPIO02 Optional sensor - AGPIO(GPIO_RXD), // GPIO03 TXD to ATMEGA328P + { // SONOFF_SC - onoff SC (ESP8266) + GPIO_KEY1, // GPIO00 Button + GPIO_TXD, // GPIO01 RXD to ATMEGA328P + GPIO_USER, // GPIO02 Optional sensor + GPIO_RXD, // GPIO03 TXD to ATMEGA328P 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) 0, - AGPIO(GPIO_LED1_INV), // GPIO13 Green Led (0 = On, 1 = Off) - Link and Power status + GPIO_LED1_INV, // GPIO13 Green Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // SONOFF_BN - Sonoff BN-SZ01 Ceiling led (ESP8285) + { // SONOFF_BN - Sonoff BN-SZ01 Ceiling led (ESP8285) 0, 0, 0, 0, 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1), // GPIO12 Light - AGPIO(GPIO_LED1_INV), // GPIO13 Red Led (0 = On, 1 = Off) - Link and Power status + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM1, // GPIO12 Light + GPIO_LED1_INV, // GPIO13 Red Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // HUAFAN_SS - Hua Fan Smart Socket (ESP8266) - like Sonoff Pow - AGPIO(GPIO_LEDLNK_INV), // GPIO00 Blue Led (0 = On, 1 = Off) - Link status + { // HUAFAN_SS - Hua Fan Smart Socket (ESP8266) - like Sonoff Pow + GPIO_LEDLNK_INV, // GPIO00 Blue Led (0 = On, 1 = Off) - Link status 0, 0, - AGPIO(GPIO_LED1_INV), // GPIO03 Red Led (0 = On, 1 = Off) - Power status - AGPIO(GPIO_KEY1), // GPIO04 Button - AGPIO(GPIO_REL1_INV), // GPIO05 Relay (0 = On, 1 = Off) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_CF1), // GPIO12 HLW8012 CF1 voltage / current - AGPIO(GPIO_NRG_SEL), // GPIO13 HLW8012 Sel output (1 = Voltage) - AGPIO(GPIO_HLW_CF), // GPIO14 HLW8012 CF power + GPIO_LED1_INV, // GPIO03 Red Led (0 = On, 1 = Off) - Power status + GPIO_KEY1, // GPIO04 Button + GPIO_REL1_INV, // GPIO05 Relay (0 = On, 1 = Off) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_CF1, // GPIO12 HLW8012 CF1 voltage / current + GPIO_NRG_SEL, // GPIO13 HLW8012 Sel output (1 = Voltage) + GPIO_HLW_CF, // GPIO14 HLW8012 CF power 0, 0, 0 }, - { // SONOFF_BRIDGE - Sonoff RF Bridge 433 (ESP8285) - AGPIO(GPIO_KEY1), // GPIO00 Button - AGPIO(GPIO_TXD), // GPIO01 RF bridge control - AGPIO(GPIO_USER), // GPIO02 Optional sensor - AGPIO(GPIO_RXD), // GPIO03 RF bridge control - AGPIO(GPIO_USER), // GPIO04 Optional sensor - AGPIO(GPIO_USER), // GPIO05 Optional sensor - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 Optional sensor - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_USER), // GPIO14 Optional sensor + { // SONOFF_BRIDGE - Sonoff RF Bridge 433 (ESP8285) + GPIO_KEY1, // GPIO00 Button + GPIO_TXD, // GPIO01 RF bridge control + GPIO_USER, // GPIO02 Optional sensor + GPIO_RXD, // GPIO03 RF bridge control + GPIO_USER, // GPIO04 Optional sensor + GPIO_USER, // GPIO05 Optional sensor + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 Optional sensor + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + GPIO_USER, // GPIO14 Optional sensor 0, 0, 0 }, - { // SONOFF_B1 - Sonoff B1 (ESP8285 - my9231) - AGPIO(GPIO_KEY1), // GPIO00 Pad - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor pad - AGPIO(GPIO_USER), // GPIO02 Optional sensor SDA pad - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor pad + { // SONOFF_B1 - Sonoff B1 (ESP8285 - my9231) + GPIO_KEY1, // GPIO00 Pad + GPIO_USER, // GPIO01 Serial RXD and Optional sensor pad + GPIO_USER, // GPIO02 Optional sensor SDA pad + GPIO_USER, // GPIO03 Serial TXD and Optional sensor pad 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_DI), // GPIO12 my9231 DI + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_DI, // GPIO12 my9231 DI 0, - AGPIO(GPIO_DCKI), // GPIO14 my9231 DCKI + GPIO_DCKI, // GPIO14 my9231 DCKI 0, 0, 0 }, - { // AILIGHT - Ai-Thinker RGBW led (ESP8266 - my9291) - AGPIO(GPIO_KEY1), // GPIO00 Pad - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor pad - AGPIO(GPIO_USER), // GPIO02 Optional sensor SDA pad - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor pad + { // AILIGHT - Ai-Thinker RGBW led (ESP8266 - my9291) + GPIO_KEY1, // GPIO00 Pad + GPIO_USER, // GPIO01 Serial RXD and Optional sensor pad + GPIO_USER, // GPIO02 Optional sensor SDA pad + GPIO_USER, // GPIO03 Serial TXD and Optional sensor pad 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) 0, - AGPIO(GPIO_DI), // GPIO13 my9291 DI + GPIO_DI, // GPIO13 my9291 DI 0, - AGPIO(GPIO_DCKI), // GPIO15 my9291 DCKI + GPIO_DCKI, // GPIO15 my9291 DCKI 0, 0 }, - { // SONOFF_T11 - Sonoff T1 1CH (ESP8285) - AGPIO(GPIO_KEY1), // GPIO00 Button 1 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Optional Sensor (J3 Pin 5) - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor + { // SONOFF_T11 - Sonoff T1 1CH (ESP8285) + GPIO_KEY1, // GPIO00 Button 1 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Optional Sensor (J3 Pin 5) + GPIO_USER, // GPIO03 Serial TXD and Optional sensor 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Blue Led and Relay 1 (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Blue Led and Relay 1 (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // SUPLA1 - Supla Espablo (ESP8266) - // http://www.wykop.pl/ramka/3325399/diy-supla-do-puszki-instalacyjnej-podtynkowej-supla-org/ - 0, // GPIO00 Flash jumper - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor + { // SUPLA1 - Supla Espablo (ESP8266) + // http://www.wykop.pl/ramka/3325399/diy-supla-do-puszki-instalacyjnej-podtynkowej-supla-org/ + 0, // GPIO00 Flash jumper + GPIO_USER, // GPIO01 Serial RXD and Optional sensor #ifdef USE_DS18x20 - AGPIO(GPIO_DSB), // GPIO02 DS18B20 sensor + GPIO_DSB, // GPIO02 DS18B20 sensor #else - AGPIO(GPIO_USER), // GPIO02 Optional sensor + GPIO_USER, // GPIO02 Optional sensor #endif - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_KEY1), // GPIO04 Button 1 - AGPIO(GPIO_REL1), // GPIO05 Relay 1 (0 = Off, 1 = On) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 Optional sensor - AGPIO(GPIO_REL1) +1, // GPIO13 Relay 2 (0 = Off, 1 = On) - AGPIO(GPIO_USER), // GPIO14 Optional sensor + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_KEY1, // GPIO04 Button 1 + GPIO_REL1, // GPIO05 Relay 1 (0 = Off, 1 = On) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 Optional sensor + GPIO_REL2, // GPIO13 Relay 2 (0 = Off, 1 = On) + GPIO_USER, // GPIO14 Optional sensor 0, - AGPIO(GPIO_LED1), // GPIO16 Led (1 = On, 0 = Off) - Link and Power status - AGPIO(ADC0_USER) // ADC0 A0 Analog input + GPIO_LED1, // GPIO16 Led (1 = On, 0 = Off) - Link and Power status + ADC0_USER // ADC0 A0 Analog input }, - { // WITTY - Witty Cloud Dev Board (ESP8266) - // https://www.aliexpress.com/item/ESP8266-serial-WIFI-Witty-cloud-Development-Board-ESP-12F-module-MINI-nodemcu/32643464555.html - AGPIO(GPIO_USER), // GPIO00 D3 flash push button on interface board - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_LED1_INV), // GPIO02 D4 Blue Led (0 = On, 1 = Off) on ESP-12F - Link and Power status - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_KEY1), // GPIO04 D2 push button on ESP-12F board - AGPIO(GPIO_USER), // GPIO05 D1 optional sensor - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +1, // GPIO12 D6 RGB LED Green - AGPIO(GPIO_PWM1) +2, // GPIO13 D7 RGB LED Blue - AGPIO(GPIO_USER), // GPIO14 D5 optional sensor - AGPIO(GPIO_PWM1), // GPIO15 D8 RGB LED Red - AGPIO(GPIO_USER), // GPIO16 D0 optional sensor - AGPIO(ADC0_USER) // ADC0 A0 Light sensor / Requires USE_ADC_VCC in user_config.h to be disabled + { // WITTY - Witty Cloud Dev Board (ESP8266) + // https://www.aliexpress.com/item/ESP8266-serial-WIFI-Witty-cloud-Development-Board-ESP-12F-module-MINI-nodemcu/32643464555.html + GPIO_USER, // GPIO00 D3 flash push button on interface board + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_LED1_INV, // GPIO02 D4 Blue Led (0 = On, 1 = Off) on ESP-12F - Link and Power status + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_KEY1, // GPIO04 D2 push button on ESP-12F board + GPIO_USER, // GPIO05 D1 optional sensor + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM2, // GPIO12 D6 RGB LED Green + GPIO_PWM3, // GPIO13 D7 RGB LED Blue + GPIO_USER, // GPIO14 D5 optional sensor + GPIO_PWM1, // GPIO15 D8 RGB LED Red + GPIO_USER, // GPIO16 D0 optional sensor + ADC0_USER // ADC0 A0 Light sensor / Requires USE_ADC_VCC in user_config.h to be disabled }, - { // YUNSHAN - Yunshan Wifi Relay (ESP8266) - // https://www.ebay.com/p/Esp8266-220v-10a-Network-Relay-WiFi-Module/1369583381 - // Schematics and Info https://ucexperiment.wordpress.com/2016/12/18/yunshan-esp8266-250v-15a-acdc-network-wifi-relay-module/ - 0, // GPIO00 Flash jumper - Module Pin 8 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - Module Pin 2 - AGPIO(GPIO_LED1_INV), // GPIO02 Blue Led (0 = On, 1 = Off) on ESP-12F - Module Pin 7 - Link and Power status - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - Module Pin 3 - AGPIO(GPIO_REL1), // GPIO04 Red Led and Relay (0 = Off, 1 = On) - Module Pin 10 - AGPIO(GPIO_KEY1), // GPIO05 Blue Led and OptoCoupler input - Module Pin 9 - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) + { // YUNSHAN - Yunshan Wifi Relay (ESP8266) + // https://www.ebay.com/p/Esp8266-220v-10a-Network-Relay-WiFi-Module/1369583381 + // Schematics and Info https://ucexperiment.wordpress.com/2016/12/18/yunshan-esp8266-250v-15a-acdc-network-wifi-relay-module/ + 0, // GPIO00 Flash jumper - Module Pin 8 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor - Module Pin 2 + GPIO_LED1_INV, // GPIO02 Blue Led (0 = On, 1 = Off) on ESP-12F - Module Pin 7 - Link and Power status + GPIO_USER, // GPIO03 Serial TXD and Optional sensor - Module Pin 3 + GPIO_REL1, // GPIO04 Red Led and Relay (0 = Off, 1 = On) - Module Pin 10 + GPIO_KEY1, // GPIO05 Blue Led and OptoCoupler input - Module Pin 9 + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) 0, 0, 0, 0, 0, 0 }, - { // MAGICHOME - Magic Home (aka Flux-light) (ESP8266) and Arilux LC10 (ESP8285) - // https://www.aliexpress.com/item/Magic-Home-Mini-RGB-RGBW-Wifi-Controller-For-Led-Strip-Panel-light-Timing-Function-16million-colors/32686853650.html + { // MAGICHOME - Magic Home (aka Flux-light) (ESP8266) and Arilux LC10 (ESP8285) + // https://www.aliexpress.com/item/Magic-Home-Mini-RGB-RGBW-Wifi-Controller-For-Led-Strip-Panel-light-Timing-Function-16million-colors/32686853650.html 0, - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_LED1_INV), // GPIO02 Blue onboard LED - Link and Power status - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_ARIRFRCV), // GPIO04 IR or RF receiver (optional) (Arilux LC10) - AGPIO(GPIO_PWM1) +1, // GPIO05 RGB LED Green - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +2, // GPIO12 RGB LED Blue - AGPIO(GPIO_USER), // GPIO13 RGBW LED White (optional - set to PWM4 for Cold White or Warm White as used on Arilux LC10) - AGPIO(GPIO_PWM1), // GPIO14 RGB LED Red - AGPIO(GPIO_ARIRFSEL), // GPIO15 RF receiver control (Arilux LC10) + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_LED1_INV, // GPIO02 Blue onboard LED - Link and Power status + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_ARIRFRCV, // GPIO04 IR or RF receiver (optional) (Arilux LC10) + GPIO_PWM2, // GPIO05 RGB LED Green + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM3, // GPIO12 RGB LED Blue + GPIO_USER, // GPIO13 RGBW LED White (optional - set to PWM4 for Cold White or Warm White as used on Arilux LC10) + GPIO_PWM1, // GPIO14 RGB LED Red + GPIO_ARIRFSEL, // GPIO15 RF receiver control (Arilux LC10) 0, 0 }, - { // LUANIHVIO - ESP8266_HVIO - // https://luani.de/projekte/esp8266-hvio/ - 0, // GPIO00 Flash jumper - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Optional sensor / I2C SDA pad - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_REL1), // GPIO04 Relay 1 (0 = Off, 1 = On) - AGPIO(GPIO_REL1) +1, // GPIO05 Relay 2 (0 = Off, 1 = On) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_SWT1), // GPIO12 External input 1 (0 = On, 1 = Off) - AGPIO(GPIO_SWT1) +1, // GPIO13 External input 2 (0 = On, 1 = Off) - AGPIO(GPIO_USER), // GPIO14 Optional sensor / I2C SCL pad - AGPIO(GPIO_LED1), // GPIO15 Led (1 = On, 0 = Off) - Link and Power status + { // LUANIHVIO - ESP8266_HVIO + // https://luani.de/projekte/esp8266-hvio/ + 0, // GPIO00 Flash jumper + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Optional sensor / I2C SDA pad + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_REL1, // GPIO04 Relay 1 (0 = Off, 1 = On) + GPIO_REL2, // GPIO05 Relay 2 (0 = Off, 1 = On) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_SWT1, // GPIO12 External input 1 (0 = On, 1 = Off) + GPIO_SWT2, // GPIO13 External input 2 (0 = On, 1 = Off) + GPIO_USER, // GPIO14 Optional sensor / I2C SCL pad + GPIO_LED1, // GPIO15 Led (1 = On, 0 = Off) - Link and Power status 0, - AGPIO(ADC0_USER) // ADC0 A0 Analog input + ADC0_USER // ADC0 A0 Analog input }, - { // KMC_70011 - KMC 70011 - // https://www.amazon.com/KMC-Timing-Monitoring-Network-125V-240V/dp/B06XRX2GTQ - AGPIO(GPIO_KEY1), // GPIO00 Button + { // KMC_70011 - KMC 70011 + // https://www.amazon.com/KMC-Timing-Monitoring-Network-125V-240V/dp/B06XRX2GTQ + GPIO_KEY1, // GPIO00 Button 0, 0, 0, - AGPIO(GPIO_HLW_CF), // GPIO04 HLW8012 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 HLW8012 CF1 voltage / current - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL), // GPIO12 HLW8012 SEL (1 = Voltage) - AGPIO(GPIO_LED1_INV), // GPIO13 Green Led - Link and Power status - AGPIO(GPIO_REL1), // GPIO14 Relay + GPIO_HLW_CF, // GPIO04 HLW8012 CF power + GPIO_NRG_CF1, // GPIO05 HLW8012 CF1 voltage / current + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL, // GPIO12 HLW8012 SEL (1 = Voltage) + GPIO_LED1_INV, // GPIO13 Green Led - Link and Power status + GPIO_REL1, // GPIO14 Relay 0, 0, 0 }, - { // ARILUX_LC01 - Arilux AL-LC01 (ESP8285) - // https://www.banggood.com/nl/ARILUX-AL-LC01-Super-Mini-LED-WIFI-Smart-RGB-Controller-For-RGB-LED-Strip-Light-DC-9-12V-p-1058603.html - // (PwmFrequency 1111Hz) - AGPIO(GPIO_KEY1), // GPIO00 Optional Button - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_ARIRFSEL), // GPIO02 RF receiver control - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_ARIRFRCV), // GPIO04 IR or RF receiver (optional) - AGPIO(GPIO_PWM1), // GPIO05 RGB LED Red - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +1, // GPIO12 RGB LED Green - AGPIO(GPIO_PWM1) +2, // GPIO13 RGB LED Blue - AGPIO(GPIO_USER), // GPIO14 RGBW LED White (optional - set to PWM4 for Cold White or Warm White) + { // ARILUX_LC01 - Arilux AL-LC01 (ESP8285) + // https://www.banggood.com/nl/ARILUX-AL-LC01-Super-Mini-LED-WIFI-Smart-RGB-Controller-For-RGB-LED-Strip-Light-DC-9-12V-p-1058603.html + // (PwmFrequency 1111Hz) + GPIO_KEY1, // GPIO00 Optional Button + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_ARIRFSEL, // GPIO02 RF receiver control + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_ARIRFRCV, // GPIO04 IR or RF receiver (optional) + GPIO_PWM1, // GPIO05 RGB LED Red + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM2, // GPIO12 RGB LED Green + GPIO_PWM3, // GPIO13 RGB LED Blue + GPIO_USER, // GPIO14 RGBW LED White (optional - set to PWM4 for Cold White or Warm White) 0, 0, 0 }, - { // ARILUX_LC11 - Arilux AL-LC11 (ESP8266) - // https://www.banggood.com/nl/ARILUX-AL-LC11-Super-Mini-LED-WIFI-APP-Controller-RF-Remote-Control-For-RGBWW-LED-Strip-DC9-28V-p-1085112.html - // (PwmFrequency 540Hz) - AGPIO(GPIO_KEY1), // GPIO00 Optional Button - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_ARIRFSEL), // GPIO02 RF receiver control - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_PWM1) +1, // GPIO04 RGB LED Green - AGPIO(GPIO_PWM1), // GPIO05 RGB LED Red - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +4, // GPIO12 RGBCW LED Warm - AGPIO(GPIO_PWM1) +3, // GPIO13 RGBW LED Cold - AGPIO(GPIO_PWM1) +2, // GPIO14 RGB LED Blue - AGPIO(GPIO_ARIRFRCV), // GPIO15 RF receiver input + { // ARILUX_LC11 - Arilux AL-LC11 (ESP8266) + // https://www.banggood.com/nl/ARILUX-AL-LC11-Super-Mini-LED-WIFI-APP-Controller-RF-Remote-Control-For-RGBWW-LED-Strip-DC9-28V-p-1085112.html + // (PwmFrequency 540Hz) + GPIO_KEY1, // GPIO00 Optional Button + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_ARIRFSEL, // GPIO02 RF receiver control + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_PWM2, // GPIO04 RGB LED Green + GPIO_PWM1, // GPIO05 RGB LED Red + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM5, // GPIO12 RGBCW LED Warm + GPIO_PWM4, // GPIO13 RGBW LED Cold + GPIO_PWM3, // GPIO14 RGB LED Blue + GPIO_ARIRFRCV, // GPIO15 RF receiver input 0, 0 }, - { // ARILUX_LC06 - Arilux AL-LC06 (ESP8285) - // https://www.banggood.com/ARILUX-AL-LC06-LED-WIFI-Smartphone-Controller-Romote-5-Channels-DC12-24V-For-RGBWW-Strip-light-p-1061476.html - AGPIO(GPIO_KEY1), // GPIO00 Optional Button - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Empty pad - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_USER), // GPIO04 W2 - PWM5 + { // ARILUX_LC06 - Arilux AL-LC06 (ESP8285) + // https://www.banggood.com/ARILUX-AL-LC06-LED-WIFI-Smartphone-Controller-Romote-5-Channels-DC12-24V-For-RGBWW-Strip-light-p-1061476.html + GPIO_KEY1, // GPIO00 Optional Button + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Empty pad + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_USER, // GPIO04 W2 - PWM5 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +1, // GPIO12 RGB LED Green - AGPIO(GPIO_PWM1) +2, // GPIO13 RGB LED Blue - AGPIO(GPIO_PWM1), // GPIO14 RGB LED Red - AGPIO(GPIO_USER), // GPIO15 RGBW LED White + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM2, // GPIO12 RGB LED Green + GPIO_PWM3, // GPIO13 RGB LED Blue + GPIO_PWM1, // GPIO14 RGB LED Red + GPIO_USER, // GPIO15 RGBW LED White 0, 0 }, - { // ZENGGE_ZF_WF017 - Zenggee ZJ-WF017-A (ESP12S)) - // https://www.ebay.com/p/Smartphone-Android-IOS-WiFi-Music-Controller-for-RGB-5050-3528-LED-Strip-Light/534446632?_trksid=p2047675.l2644 - AGPIO(GPIO_KEY1), // GPIO00 Optional Button + { // ZENGGE_ZF_WF017 - Zenggee ZJ-WF017-A (ESP12S)) + // https://www.ebay.com/p/Smartphone-Android-IOS-WiFi-Music-Controller-for-RGB-5050-3528-LED-Strip-Light/534446632?_trksid=p2047675.l2644 + GPIO_KEY1, // GPIO00 Optional Button 0, - AGPIO(GPIO_USER), // GPIO02 Empty pad + GPIO_USER, // GPIO02 Empty pad 0, - AGPIO(GPIO_USER), // GPIO04 W2 - PWM5 + GPIO_USER, // GPIO04 W2 - PWM5 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +1, // GPIO12 RGB LED Green - AGPIO(GPIO_PWM1), // GPIO13 RGB LED Red - AGPIO(GPIO_PWM1) +2, // GPIO14 RGB LED Blue + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM2, // GPIO12 RGB LED Green + GPIO_PWM1, // GPIO13 RGB LED Red + GPIO_PWM3, // GPIO14 RGB LED Blue 0, 0, 0 }, - { // SONOFF_POW_R2 - Sonoff Pow R2 (ESP8285 - CSE7766) - AGPIO(GPIO_KEY1), // GPIO00 Button - AGPIO(GPIO_CSE7766_TX), // GPIO01 Serial RXD 4800 baud 8E1 CSE7766 energy sensor + { // SONOFF_POW_R2 - Sonoff Pow R2 (ESP8285 - CSE7766) + GPIO_KEY1, // GPIO00 Button + GPIO_CSE7766_TX, // GPIO01 Serial RXD 4800 baud 8E1 CSE7766 energy sensor 0, - AGPIO(GPIO_CSE7766_RX), // GPIO03 Serial TXD + GPIO_CSE7766_RX, // GPIO03 Serial TXD 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Red Led and Relay (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Red Led and Relay (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // BLITZWOLF_BWSHP - BlitzWolf BW-SHP2 and BW-SHP6 (ESP8285 - BL0937 or HJL-01 Energy Monitoring) - // https://www.banggood.com/BlitzWolf-BW-SHP2-Smart-WIFI-Socket-EU-Plug-220V-16A-Work-with-Amazon-Alexa-Google-Assistant-p-1292899.html - // https://www.amazon.de/Steckdose-Homecube-intelligente-Verbrauchsanzeige-funktioniert/dp/B076Q2LKHG/ref=sr_1_fkmr0_1 - // https://www.amazon.de/Intelligente-Stromverbrauch-Fernsteurung-Schaltbare-Energieklasse/dp/B076WZQS4S/ref=sr_1_1 - // https://www.aliexpress.com/store/product/BlitzWolf-BW-SHP6-EU-Plug-Metering-Version-WIFI-Smart-Socket-220V-240V-10A-Work-with-Amazon/1965360_32945504669.html - AGPIO(GPIO_LED1_INV), // GPIO00 Red Led (1 = On, 0 = Off) - Power status - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_LEDLNK_INV), // GPIO02 Blue Led (1 = On, 0 = Off) - Link status - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor + { // BLITZWOLF_BWSHP - BlitzWolf BW-SHP2 and BW-SHP6 (ESP8285 - BL0937 or HJL-01 Energy Monitoring) + // https://www.banggood.com/BlitzWolf-BW-SHP2-Smart-WIFI-Socket-EU-Plug-220V-16A-Work-with-Amazon-Alexa-Google-Assistant-p-1292899.html + // https://www.amazon.de/Steckdose-Homecube-intelligente-Verbrauchsanzeige-funktioniert/dp/B076Q2LKHG/ref=sr_1_fkmr0_1 + // https://www.amazon.de/Intelligente-Stromverbrauch-Fernsteurung-Schaltbare-Energieklasse/dp/B076WZQS4S/ref=sr_1_1 + // https://www.aliexpress.com/store/product/BlitzWolf-BW-SHP6-EU-Plug-Metering-Version-WIFI-Smart-Socket-220V-240V-10A-Work-with-Amazon/1965360_32945504669.html + GPIO_LED1_INV, // GPIO00 Red Led (1 = On, 0 = Off) - Power status + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_LEDLNK_INV, // GPIO02 Blue Led (1 = On, 0 = Off) - Link status + GPIO_USER, // GPIO03 Serial TXD and Optional sensor 0, - AGPIO(GPIO_HJL_CF), // GPIO05 BL0937 or HJL-01 CF power - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) - AGPIO(GPIO_KEY1), // GPIO13 Button - AGPIO(GPIO_NRG_CF1), // GPIO14 BL0937 or HJL-01 CF1 current / voltage - AGPIO(GPIO_REL1), // GPIO15 Relay (0 = Off, 1 = On) + GPIO_HJL_CF, // GPIO05 BL0937 or HJL-01 CF power + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) + GPIO_KEY1, // GPIO13 Button + GPIO_NRG_CF1, // GPIO14 BL0937 or HJL-01 CF1 current / voltage + GPIO_REL1, // GPIO15 Relay (0 = Off, 1 = On) 0, 0 }, - { // SHELLY1 - Shelly1 Open Source (ESP8266 - 2MB) - https://shelly.cloud/shelly1-open-source/ - AGPIO(GPIO_USER), // GPIO00 - Can be changed to GPIO_USER, only if Shelly is powered with 12V DC - AGPIO(GPIO_USER), // GPIO01 Serial RXD - Can be changed to GPIO_USER, only if Shelly is powered with 12V DC + { // SHELLY1 - Shelly1 Open Source (ESP8266 - 2MB) - https://shelly.cloud/shelly1-open-source/ + GPIO_USER, // GPIO00 - Can be changed to GPIO_USER, only if Shelly is powered with 12V DC + GPIO_USER, // GPIO01 Serial RXD - Can be changed to GPIO_USER, only if Shelly is powered with 12V DC 0, - AGPIO(GPIO_USER), // GPIO03 Serial TXD - Can be changed to GPIO_USER, only if Shelly is powered with 12V DC - AGPIO(GPIO_REL1), // GPIO04 Relay (0 = Off, 1 = On) - AGPIO(GPIO_SWT1_NP), // GPIO05 SW pin - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO03 Serial TXD - Can be changed to GPIO_USER, only if Shelly is powered with 12V DC + GPIO_REL1, // GPIO04 Relay (0 = Off, 1 = On) + GPIO_SWT1_NP, // GPIO05 SW pin + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) 0, 0, 0, 0, 0, 0 }, - { // SHELLY2 - Shelly2 (ESP8266 - 2MB) - https://shelly.cloud/shelly2/ + { // SHELLY2 - Shelly2 (ESP8266 - 2MB) - https://shelly.cloud/shelly2/ 0, - AGPIO(GPIO_MCP39F5_TX), // GPIO01 MCP39F501 Serial input + GPIO_MCP39F5_TX, // GPIO01 MCP39F501 Serial input 0, - AGPIO(GPIO_MCP39F5_RX), // GPIO03 MCP39F501 Serial output - AGPIO(GPIO_REL1), // GPIO04 - AGPIO(GPIO_REL1) +1, // GPIO05 - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_SWT1), // GPIO12 + GPIO_MCP39F5_RX, // GPIO03 MCP39F501 Serial output + GPIO_REL1, // GPIO04 + GPIO_REL2, // GPIO05 + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_SWT1, // GPIO12 0, - AGPIO(GPIO_SWT1) +1, // GPIO14 - AGPIO(GPIO_MCP39F5_RST), // GPIO15 MCP39F501 Reset + GPIO_SWT2, // GPIO14 + GPIO_MCP39F5_RST, // GPIO15 MCP39F501 Reset 0, 0 }, - { // PHILIPS - Xiaomi Philips bulb (ESP8266) + { // PHILIPS - Xiaomi Philips bulb (ESP8266) 0, 0, 0, 0, 0, 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_PWM1) +1, // GPIO12 cold/warm light + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_PWM2, // GPIO12 cold/warm light 0, 0, - AGPIO(GPIO_PWM1), // GPIO15 light intensity + GPIO_PWM1, // GPIO15 light intensity 0, 0 }, - { // NEO_COOLCAM - Neo Coolcam (ESP8266) - // https://www.banggood.com/NEO-COOLCAM-WiFi-Mini-Smart-Plug-APP-Remote-Control-Timing-Smart-Socket-EU-Plug-p-1288562.html?cur_warehouse=CN + { // NEO_COOLCAM - Neo Coolcam (ESP8266) + // https://www.banggood.com/NEO-COOLCAM-WiFi-Mini-Smart-Plug-APP-Remote-Control-Timing-Smart-Socket-EU-Plug-p-1288562.html?cur_warehouse=CN 0, 0, 0, 0, - AGPIO(GPIO_LED1_INV), // GPIO04 Red Led (0 = On, 1 = Off) - Link and Power status + GPIO_LED1_INV, // GPIO04 Red Led (0 = On, 1 = Off) - Link and Power status 0, - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Red Led and Relay (0 = Off, 1 = On) - AGPIO(GPIO_KEY1), // GPIO13 Button + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Red Led and Relay (0 = Off, 1 = On) + GPIO_KEY1, // GPIO13 Button 0, 0, 0, 0 }, - { // ESP_SWITCH - Michael Haustein 4 channel wall switch (ESP07 = ESP8266) - // Use rules for further actions like - rule on power1#state do publish cmnd/other_device/power %value% endon - AGPIO(GPIO_KEY1) +1, // GPIO00 Button 2 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_REL1_INV) +2, // GPIO02 Yellow Led 3 (0 = On, 1 = Off) - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_KEY1), // GPIO04 Button 1 - AGPIO(GPIO_REL1_INV) +1, // GPIO05 Red Led 2 (0 = On, 1 = Off) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1_INV) +3, // GPIO12 Blue Led 4 (0 = On, 1 = Off) - AGPIO(GPIO_KEY1) +3, // GPIO13 Button 4 - AGPIO(GPIO_KEY1) +2, // GPIO14 Button 3 - AGPIO(GPIO_LED1), // GPIO15 Optional sensor - AGPIO(GPIO_REL1_INV), // GPIO16 Green Led 1 (0 = On, 1 = Off) + { // ESP_SWITCH - Michael Haustein 4 channel wall switch (ESP07 = ESP8266) + // Use rules for further actions like - rule on power1#state do publish cmnd/other_device/power %value% endon + GPIO_KEY2, // GPIO00 Button 2 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_REL3_INV, // GPIO02 Yellow Led 3 (0 = On, 1 = Off) + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_KEY1, // GPIO04 Button 1 + GPIO_REL2_INV, // GPIO05 Red Led 2 (0 = On, 1 = Off) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL4_INV, // GPIO12 Blue Led 4 (0 = On, 1 = Off) + GPIO_KEY4, // GPIO13 Button 4 + GPIO_KEY3, // GPIO14 Button 3 + GPIO_LED1, // GPIO15 Optional sensor + GPIO_REL1_INV, // GPIO16 Green Led 1 (0 = On, 1 = Off) 0 }, - { // OBI - OBI socket (ESP8266) - https://www.obi.de/hausfunksteuerung/wifi-stecker-schuko/p/2291706 - AGPIO(GPIO_USER), // GPIO00 - AGPIO(GPIO_USER), // GPIO01 Serial RXD + { // OBI - OBI socket (ESP8266) - https://www.obi.de/hausfunksteuerung/wifi-stecker-schuko/p/2291706 + GPIO_USER, // GPIO00 + GPIO_USER, // GPIO01 Serial RXD 0, - AGPIO(GPIO_USER), // GPIO03 Serial TXD - AGPIO(GPIO_LED1), // GPIO04 Blue LED - Link and Power status - AGPIO(GPIO_REL1), // GPIO05 (Relay OFF, but used as Relay Switch) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_LED1) +2, // GPIO12 (Relay ON, but set to LOW, so we can switch with GPIO05) - AGPIO(GPIO_USER), // GPIO13 - AGPIO(GPIO_KEY1), // GPIO14 Button + GPIO_USER, // GPIO03 Serial TXD + GPIO_LED1, // GPIO04 Blue LED - Link and Power status + GPIO_REL1, // GPIO05 (Relay OFF, but used as Relay Switch) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_LED3, // GPIO12 (Relay ON, but set to LOW, so we can switch with GPIO05) + GPIO_USER, // GPIO13 + GPIO_KEY1, // GPIO14 Button 0, - AGPIO(GPIO_USER), // GPIO16 - AGPIO(ADC0_USER) // ADC0 A0 Analog input + GPIO_USER, // GPIO16 + ADC0_USER // ADC0 A0 Analog input }, - { // TECKIN - https://www.amazon.de/gp/product/B07D5V139R + { // TECKIN - https://www.amazon.de/gp/product/B07D5V139R 0, - AGPIO(GPIO_KEY1), // GPIO01 Serial TXD and Button + GPIO_KEY1, // GPIO01 Serial TXD and Button 0, - AGPIO(GPIO_LED1_INV), // GPIO03 Serial RXD and Red Led (0 = On, 1 = Off) - Power status - AGPIO(GPIO_HJL_CF), // GPIO04 BL0937 or HJL-01 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 BL0937 or HJL-01 CF1 current / voltage - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) - AGPIO(GPIO_LEDLNK_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link status - AGPIO(GPIO_REL1), // GPIO14 Relay (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO03 Serial RXD and Red Led (0 = On, 1 = Off) - Power status + GPIO_HJL_CF, // GPIO04 BL0937 or HJL-01 CF power + GPIO_NRG_CF1, // GPIO05 BL0937 or HJL-01 CF1 current / voltage + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) + GPIO_LEDLNK_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link status + GPIO_REL1, // GPIO14 Relay (0 = Off, 1 = On) 0, 0, 0 }, - { // APLIC_WDP303075 - Aplic WDP 303075 (ESP8285 - HLW8012 Energy Monitoring) - // https://www.amazon.de/dp/B07CNWVNJ2 + { // APLIC_WDP303075 - Aplic WDP 303075 (ESP8285 - HLW8012 Energy Monitoring) + // https://www.amazon.de/dp/B07CNWVNJ2 0, 0, 0, - AGPIO(GPIO_KEY1), // GPIO03 Button - AGPIO(GPIO_HLW_CF), // GPIO04 HLW8012 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 HLW8012 CF1 current / voltage - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 HLW8012 CF Sel output (0 = Voltage) - AGPIO(GPIO_LED1_INV), // GPIO13 LED (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_REL1), // GPIO14 Relay SRU 5VDC SDA (0 = Off, 1 = On ) + GPIO_KEY1, // GPIO03 Button + GPIO_HLW_CF, // GPIO04 HLW8012 CF power + GPIO_NRG_CF1, // GPIO05 HLW8012 CF1 current / voltage + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 HLW8012 CF Sel output (0 = Voltage) + GPIO_LED1_INV, // GPIO13 LED (0 = On, 1 = Off) - Link and Power status + GPIO_REL1, // GPIO14 Relay SRU 5VDC SDA (0 = Off, 1 = On ) 0, 0, 0 }, - { // TUYA_DIMMER - Tuya MCU device (ESP8266 w/ separate MCU) - // https://www.amazon.com/gp/product/B07CTNSZZ8/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 - AGPIO(GPIO_USER), // Virtual Button (controlled by MCU) - AGPIO(GPIO_USER), // GPIO01 MCU serial control - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), // GPIO03 MCU serial control - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), // GPIO14 Green Led - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), + { // TUYA_DIMMER - Tuya MCU device (ESP8266 w/ separate MCU) + // https://www.amazon.com/gp/product/B07CTNSZZ8/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 + GPIO_USER, // Virtual Button (controlled by MCU) + GPIO_USER, // GPIO01 MCU serial control + GPIO_USER, + GPIO_USER, // GPIO03 MCU serial control + GPIO_USER, + GPIO_USER, + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, + GPIO_USER, + GPIO_USER, // GPIO14 Green Led + GPIO_USER, + GPIO_USER, 0 }, - { // GOSUND - https://www.amazon.de/gp/product/B0777BWS1P + { // GOSUND - https://www.amazon.de/gp/product/B0777BWS1P 0, - AGPIO(GPIO_LEDLNK_INV), // GPIO01 Serial RXD and LED1 (blue) inv - Link status + GPIO_LEDLNK_INV, // GPIO01 Serial RXD and LED1 (blue) inv - Link status 0, - AGPIO(GPIO_KEY1), // GPIO03 Serial TXD and Button - AGPIO(GPIO_HJL_CF), // GPIO04 BL0937 or HJL-01 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 BL0937 or HJL-01 CF1 current / voltage - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) - AGPIO(GPIO_LED1_INV), // GPIO13 LED2 (red) inv - Power status - AGPIO(GPIO_REL1), // GPIO14 Relay (0 = Off, 1 = On) + GPIO_KEY1, // GPIO03 Serial TXD and Button + GPIO_HJL_CF, // GPIO04 BL0937 or HJL-01 CF power + GPIO_NRG_CF1, // GPIO05 BL0937 or HJL-01 CF1 current / voltage + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) + GPIO_LED1_INV, // GPIO13 LED2 (red) inv - Power status + GPIO_REL1, // GPIO14 Relay (0 = Off, 1 = On) 0, 0, 0 }, - { // ARMTRONIX_DIMMERS - ARMTRONIX Dimmer, one or two channel (ESP8266 w/ separate MCU dimmer) - // https://www.tindie.com/products/Armtronix/wifi-ac-dimmer-two-triac-board/ - // https://www.tindie.com/products/Armtronix/wifi-ac-dimmer-esp8266-one-triac-board-alexaecho/ - AGPIO(GPIO_USER), - AGPIO(GPIO_TXD), // GPIO01 MCU serial control - AGPIO(GPIO_USER), - AGPIO(GPIO_RXD), // GPIO03 MCU serial control - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), + { // ARMTRONIX_DIMMERS - ARMTRONIX Dimmer, one or two channel (ESP8266 w/ separate MCU dimmer) + // https://www.tindie.com/products/Armtronix/wifi-ac-dimmer-two-triac-board/ + // https://www.tindie.com/products/Armtronix/wifi-ac-dimmer-esp8266-one-triac-board-alexaecho/ + GPIO_USER, + GPIO_TXD, // GPIO01 MCU serial control + GPIO_USER, + GPIO_RXD, // GPIO03 MCU serial control + GPIO_USER, + GPIO_USER, + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, + GPIO_USER, + GPIO_USER, + GPIO_USER, + GPIO_USER, 0 }, - { // SK03_TUYA - Outdoor smart plug with power monitoring HLW8012 chip - https://www.amazon.com/gp/product/B07CG7MBPV - AGPIO(GPIO_KEY1), // GPIO00 Button + { // SK03_TUYA - Outdoor smart plug with power monitoring HLW8012 chip - https://www.amazon.com/gp/product/B07CG7MBPV + GPIO_KEY1, // GPIO00 Button 0, 0, 0, - AGPIO(GPIO_HLW_CF), // GPIO04 HLW8012 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 HLW8012 CF1 current / voltage - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 HLW8012 CF Sel output (0 = Voltage) - AGPIO(GPIO_LED1_INV), // GPIO13 Red Led (0 = On, 1 = Off) - Power status - AGPIO(GPIO_LEDLNK_INV), // GPIO14 Blue Led (0 = On, 1 = Off) - Link status - AGPIO(GPIO_REL1), // GPIO15 Relay (0 = Off, 1 = On) + GPIO_HLW_CF, // GPIO04 HLW8012 CF power + GPIO_NRG_CF1, // GPIO05 HLW8012 CF1 current / voltage + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 HLW8012 CF Sel output (0 = Voltage) + GPIO_LED1_INV, // GPIO13 Red Led (0 = On, 1 = Off) - Power status + GPIO_LEDLNK_INV, // GPIO14 Blue Led (0 = On, 1 = Off) - Link status + GPIO_REL1, // GPIO15 Relay (0 = Off, 1 = On) 0, 0 }, - { // PS_16_DZ - PS-16-DZ Dimmer (ESP8266 w/ separate Nuvoton MCU dimmer) - // https://www.aliexpress.com/item/SM-Smart-WIFI-Wall-Dimmer-Light-Switch-US-Ewelink-APP-Remote-Control-Wi-Fi-Wirele-Work/32871151902.html - AGPIO(GPIO_USER), - AGPIO(GPIO_TXD), // GPIO01 MCU serial control - AGPIO(GPIO_USER), - AGPIO(GPIO_RXD), // GPIO03 MCU serial control - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), - AGPIO(GPIO_LED1), // GPIO13 WiFi LED - Link and Power status - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), - AGPIO(GPIO_USER), + { // PS_16_DZ - PS-16-DZ Dimmer (ESP8266 w/ separate Nuvoton MCU dimmer) + // https://www.aliexpress.com/item/SM-Smart-WIFI-Wall-Dimmer-Light-Switch-US-Ewelink-APP-Remote-Control-Wi-Fi-Wirele-Work/32871151902.html + GPIO_USER, + GPIO_TXD, // GPIO01 MCU serial control + GPIO_USER, + GPIO_RXD, // GPIO03 MCU serial control + GPIO_USER, + GPIO_USER, + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, + GPIO_LED1, // GPIO13 WiFi LED - Link and Power status + GPIO_USER, + GPIO_USER, + GPIO_USER, 0 }, - { // TECKIN_US - Teckin SP20 US with Energy Monitoring - // https://www.amazon.com/Outlet-Compatible-Monitoring-Function-Required/dp/B079Q5W22B - // https://www.amazon.com/Outlet-ZOOZEE-Monitoring-Function-Compatible/dp/B07J2LR5KN - AGPIO(GPIO_LED1_INV), // GPIO00 Red Led (1 = On, 0 = Off) - Power status + { // TECKIN_US - Teckin SP20 US with Energy Monitoring + // https://www.amazon.com/Outlet-Compatible-Monitoring-Function-Required/dp/B079Q5W22B + // https://www.amazon.com/Outlet-ZOOZEE-Monitoring-Function-Compatible/dp/B07J2LR5KN + GPIO_LED1_INV, // GPIO00 Red Led (1 = On, 0 = Off) - Power status 0, - AGPIO(GPIO_LEDLNK_INV), // GPIO02 Blue Led (1 = On, 0 = Off) - Link status + GPIO_LEDLNK_INV, // GPIO02 Blue Led (1 = On, 0 = Off) - Link status 0, - AGPIO(GPIO_REL1), // GPIO04 Relay (0 = Off, 1 = On) - AGPIO(GPIO_HJL_CF), // GPIO05 BL0937 or HJL-01 CF power - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) - AGPIO(GPIO_KEY1), // GPIO13 Button - AGPIO(GPIO_NRG_CF1), // GPIO14 BL0937 or HJL-01 CF1 current / voltage + GPIO_REL1, // GPIO04 Relay (0 = Off, 1 = On) + GPIO_HJL_CF, // GPIO05 BL0937 or HJL-01 CF power + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) + GPIO_KEY1, // GPIO13 Button + GPIO_NRG_CF1, // GPIO14 BL0937 or HJL-01 CF1 current / voltage 0, 0, 0 }, - { // MANZOKU_EU_4 - "MANZOKU" labeled power strip, EU version - // https://www.amazon.de/Steckdosenleiste-AOFO-Mehrfachsteckdose-Überspannungsschutz-Sprachsteuerung/dp/B07GBSD11P/ - // https://www.amazon.de/Steckdosenleiste-Geekbes-USB-Anschluss-Kompatibel-gesteuert/dp/B078W23BW9/ - 0, // GPIO00 - 0, // GPIO01 Serial RXD + { // MANZOKU_EU_4 - "MANZOKU" labeled power strip, EU version + // https://www.amazon.de/Steckdosenleiste-AOFO-Mehrfachsteckdose-Überspannungsschutz-Sprachsteuerung/dp/B07GBSD11P/ + // https://www.amazon.de/Steckdosenleiste-Geekbes-USB-Anschluss-Kompatibel-gesteuert/dp/B078W23BW9/ + 0, // GPIO00 + 0, // GPIO01 Serial RXD 0, - AGPIO(GPIO_KEY1), // GPIO03 Serial TXD + Button - AGPIO(GPIO_REL1) +1, // GPIO04 Relay 2 - AGPIO(GPIO_REL1), // GPIO05 Relay 1 - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1) +2, // GPIO12 Relay 3 - AGPIO(GPIO_REL1) +3, // GPIO13 Relay 4 - AGPIO(GPIO_USER), // GPIO14 + GPIO_KEY1, // GPIO03 Serial TXD + Button + GPIO_REL2, // GPIO04 Relay 2 + GPIO_REL1, // GPIO05 Relay 1 + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL3, // GPIO12 Relay 3 + GPIO_REL4, // GPIO13 Relay 4 + GPIO_USER, // GPIO14 0, - AGPIO(GPIO_USER), // GPIO16 + GPIO_USER, // GPIO16 0 }, - { // OBI2 - OBI socket (ESP8266) - https://www.obi.de/hausfunksteuerung/wifi-stecker-schuko-2-stueck-weiss/p/4077673 - 0, // GPIO00 - 0, // GPIO01 Serial RXD + { // OBI2 - OBI socket (ESP8266) - https://www.obi.de/hausfunksteuerung/wifi-stecker-schuko-2-stueck-weiss/p/4077673 + 0, // GPIO00 + 0, // GPIO01 Serial RXD 0, - 0, // GPIO03 Serial TXD - AGPIO(GPIO_REL1), // GPIO04 Relay 1 - AGPIO(GPIO_KEY1), // GPIO05 Button - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_LEDLNK_INV), // GPIO12 Green LED - Link status - AGPIO(GPIO_LED1), // GPIO13 Red LED - Power status + 0, // GPIO03 Serial TXD + GPIO_REL1, // GPIO04 Relay 1 + GPIO_KEY1, // GPIO05 Button + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_LEDLNK_INV, // GPIO12 Green LED - Link status + GPIO_LED1, // GPIO13 Red LED - Power status 0, 0, 0, 0 }, - { // YTF_IR_BRIDGE - https://www.aliexpress.com/item/Tuya-universal-Smart-IR-Hub-remote-control-Voice-Control-AC-TV-Work-With-Alexa-Google-Home/32951202513.html - AGPIO(GPIO_USER), // GPIO00 - AGPIO(GPIO_USER), // GPIO01 Serial RXD - AGPIO(GPIO_USER), // GPIO02 - AGPIO(GPIO_USER), // GPIO03 Serial TXD - AGPIO(GPIO_LED1_INV), // GPIO04 Blue Led - Link status - AGPIO(GPIO_IRRECV), // GPIO05 IR Receiver - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - 0, // GPIO12 - AGPIO(GPIO_KEY1), // GPIO13 Button - AGPIO(GPIO_IRSEND), // GPIO14 IR Transmitter + { // YTF_IR_BRIDGE - https://www.aliexpress.com/item/Tuya-universal-Smart-IR-Hub-remote-control-Voice-Control-AC-TV-Work-With-Alexa-Google-Home/32951202513.html + GPIO_USER, // GPIO00 + GPIO_USER, // GPIO01 Serial RXD + GPIO_USER, // GPIO02 + GPIO_USER, // GPIO03 Serial TXD + GPIO_LED1_INV, // GPIO04 Blue Led - Link status + GPIO_IRRECV, // GPIO05 IR Receiver + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + 0, // GPIO12 + GPIO_KEY1, // GPIO13 Button + GPIO_IRSEND, // GPIO14 IR Transmitter 0, 0, 0 }, - { // DIGOO - Digoo DG-SP202 - // https://www.banggood.com/DIGOO-DG-SP202-Dual-EU-Plug-Smart-WIFI-Socket-Individual-Controllable-Energy-Monitor-Remote-Control-Timing-Smart-Home-Outlet-let-p-1375323.html - AGPIO(GPIO_KEY1), // GPIO00 Button1 - 0, // GPIO01 Serial RXD - 0, // GPIO02 - 0, // GPIO03 Serial TXD - AGPIO(GPIO_HJL_CF), // GPIO04 BL0937 or HJL-01 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 BL0937 or HJL-01 CF1 current / voltage - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) - AGPIO(GPIO_LED1), // GPIO13 Blue Leds - Link Status - AGPIO(GPIO_REL1) +1, // GPIO14 Relay2 (0 = Off, 1 = On) and Red Led - AGPIO(GPIO_REL1), // GPIO15 Relay1 (0 = Off, 1 = On) and Red Led - AGPIO(GPIO_KEY1_NP) +1, // GPIO16 Button2, externally pulled up + { // DIGOO - Digoo DG-SP202 + // https://www.banggood.com/DIGOO-DG-SP202-Dual-EU-Plug-Smart-WIFI-Socket-Individual-Controllable-Energy-Monitor-Remote-Control-Timing-Smart-Home-Outlet-let-p-1375323.html + GPIO_KEY1, // GPIO00 Button1 + 0, // GPIO01 Serial RXD + 0, // GPIO02 + 0, // GPIO03 Serial TXD + GPIO_HJL_CF, // GPIO04 BL0937 or HJL-01 CF power + GPIO_NRG_CF1, // GPIO05 BL0937 or HJL-01 CF1 current / voltage + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 or HJL-01 Sel output (0 = Voltage) + GPIO_LED1, // GPIO13 Blue Leds - Link Status + GPIO_REL2, // GPIO14 Relay2 (0 = Off, 1 = On) and Red Led + GPIO_REL1, // GPIO15 Relay1 (0 = Off, 1 = On) and Red Led + GPIO_KEY2_NP, // GPIO16 Button2, externally pulled up 0 }, - { // KA10 - SMANERGY KA10 (ESP8285 - BL0937 Energy Monitoring) - https://www.amazon.es/dp/B07MBTCH2Y - 0, // GPIO00 - AGPIO(GPIO_LEDLNK_INV), // GPIO01 Blue LED - Link status - 0, // GPIO02 - AGPIO(GPIO_KEY1), // GPIO03 Button - AGPIO(GPIO_HJL_CF), // GPIO04 BL0937 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 BL0937 CF1 voltage / current - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 Sel output (1 = Voltage) - AGPIO(GPIO_LED1), // GPIO13 Red LED - Power status - AGPIO(GPIO_REL1), // GPIO14 Relay 1 + { // KA10 - SMANERGY KA10 (ESP8285 - BL0937 Energy Monitoring) - https://www.amazon.es/dp/B07MBTCH2Y + 0, // GPIO00 + GPIO_LEDLNK_INV, // GPIO01 Blue LED - Link status + 0, // GPIO02 + GPIO_KEY1, // GPIO03 Button + GPIO_HJL_CF, // GPIO04 BL0937 CF power + GPIO_NRG_CF1, // GPIO05 BL0937 CF1 voltage / current + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 Sel output (1 = Voltage) + GPIO_LED1, // GPIO13 Red LED - Power status + GPIO_REL1, // GPIO14 Relay 1 0, 0, 0 }, - { // ZX2820 - AGPIO(GPIO_KEY1), // GPIO00 Button + { // ZX2820 + GPIO_KEY1, // GPIO00 Button 0, 0, 0, - AGPIO(GPIO_HLW_CF), // GPIO04 HLW8012 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 HLW8012 CF1 voltage / current - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 HLW8012 SEL (0 = Voltage) - AGPIO(GPIO_LED1_INV), // GPIO13 Green Led - Link and Power status - AGPIO(GPIO_REL1), // GPIO14 Relay + GPIO_HLW_CF, // GPIO04 HLW8012 CF power + GPIO_NRG_CF1, // GPIO05 HLW8012 CF1 voltage / current + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 HLW8012 SEL (0 = Voltage) + GPIO_LED1_INV, // GPIO13 Green Led - Link and Power status + GPIO_REL1, // GPIO14 Relay 0, 0, 0 }, - { // MI_DESK_LAMP - Mi LED Desk Lamp - https://www.mi.com/global/smartlamp/ + { // MI_DESK_LAMP - Mi LED Desk Lamp - https://www.mi.com/global/smartlamp/ 0, 0, - AGPIO(GPIO_KEY1), // GPIO02 Button + GPIO_KEY1, // GPIO02 Button 0, - AGPIO(GPIO_PWM1), // GPIO04 Cold White - AGPIO(GPIO_PWM1) +1, // GPIO05 Warm White - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_ROT1A), // GPIO12 Rotary switch A pin - AGPIO(GPIO_ROT1B), // GPIO13 Rotary switch B pin + GPIO_PWM1, // GPIO04 Cold White + GPIO_PWM2, // GPIO05 Warm White + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_ROT1A, // GPIO12 Rotary switch A pin + GPIO_ROT1B, // GPIO13 Rotary switch B pin 0, 0, 0, 0 }, - { // SP10 - Tuya SP10 (BL0937 Energy Monitoring) - // https://www.aliexpress.com/item/Smart-Mini-WiFi-Plug-Outlet-Switch-Work-With-ForEcho-Alexa-Google-Home-Remote-EU-Smart-Socket/32963670423.html - 0, // GPIO00 - AGPIO(GPIO_PWM1), // GPIO01 Nightlight - 0, // GPIO02 - AGPIO(GPIO_KEY1), // GPIO03 Button - AGPIO(GPIO_HJL_CF), // GPIO04 BL0937 CF power - AGPIO(GPIO_NRG_CF1), // GPIO05 BL0937 CF1 voltage / current - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_NRG_SEL_INV), // GPIO12 BL0937 Sel output (1 = Voltage) - AGPIO(GPIO_LED1), // GPIO13 Blue LED - Link status - AGPIO(GPIO_REL1), // GPIO14 Relay and red LED + { // SP10 - Tuya SP10 (BL0937 Energy Monitoring) + // https://www.aliexpress.com/item/Smart-Mini-WiFi-Plug-Outlet-Switch-Work-With-ForEcho-Alexa-Google-Home-Remote-EU-Smart-Socket/32963670423.html + 0, // GPIO00 + GPIO_PWM1, // GPIO01 Nightlight + 0, // GPIO02 + GPIO_KEY1, // GPIO03 Button + GPIO_HJL_CF, // GPIO04 BL0937 CF power + GPIO_NRG_CF1, // GPIO05 BL0937 CF1 voltage / current + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_NRG_SEL_INV, // GPIO12 BL0937 Sel output (1 = Voltage) + GPIO_LED1, // GPIO13 Blue LED - Link status + GPIO_REL1, // GPIO14 Relay and red LED 0, 0, 0 }, - { // WAGA - WAGA life CHCZ02MB (HJL-01 Energy Monitoring) - // https://www.ebay.com/itm/332595697006 - AGPIO(GPIO_LED1_INV), // GPIO00 Red LED - 0, // GPIO01 Serial RXD - 0, // GPIO02 - AGPIO(GPIO_NRG_SEL_INV), // GPIO03 HJL-01 Sel output (1 = Voltage) - 0, // GPIO04 - AGPIO(GPIO_HJL_CF), // GPIO05 HJL-01 CF power - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Relay - AGPIO(GPIO_KEY1), // GPIO13 Button - AGPIO(GPIO_NRG_CF1), // GPIO14 HJL-01 CF1 voltage / current - AGPIO(GPIO_LEDLNK_INV), // GPIO15 Blue LED - Link status + { // WAGA - WAGA life CHCZ02MB (HJL-01 Energy Monitoring) + // https://www.ebay.com/itm/332595697006 + GPIO_LED1_INV, // GPIO00 Red LED + 0, // GPIO01 Serial RXD + 0, // GPIO02 + GPIO_NRG_SEL_INV, // GPIO03 HJL-01 Sel output (1 = Voltage) + 0, // GPIO04 + GPIO_HJL_CF, // GPIO05 HJL-01 CF power + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Relay + GPIO_KEY1, // GPIO13 Button + GPIO_NRG_CF1, // GPIO14 HJL-01 CF1 voltage / current + GPIO_LEDLNK_INV, // GPIO15 Blue LED - Link status 0, 0 }, - { // SYF05 - Sunyesmart SYF05 (a.k.a. Fcmila) = TYWE3S + SM16726 - // Also works with Merkury 904 RGBW Bulbs with 13 set to GPIO_SM16716_SEL - // https://www.flipkart.com/fc-mila-bxav-xs-ad-smart-bulb/p/itmf85zgs45fzr7n - // https://docs.tuya.com/en/hardware/WiFi-module/wifi-e3s-module.html - // http://www.datasheet-pdf.com/PDF/SM16716-Datasheet-Sunmoon-932771 - AGPIO(GPIO_USER), // GPIO00 N.C. - 0, // GPIO01 Serial RXD - AGPIO(GPIO_USER), // GPIO02 N.C. - 0, // GPIO03 Serial TXD - AGPIO(GPIO_SM16716_CLK), // GPIO04 SM16716 Clock - AGPIO(GPIO_PWM1), // GPIO05 White - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 Alt. White on some devices - AGPIO(GPIO_USER), // GPIO13 SM16716 Enable on some devices - AGPIO(GPIO_SM16716_DAT), // GPIO14 SM16716 Data - 0, // GPIO15 wired to GND - AGPIO(GPIO_USER), // GPIO16 N.C. - AGPIO(ADC0_USER) // ADC0 A0 Analog input + { // SYF05 - Sunyesmart SYF05 (a.k.a. Fcmila) = TYWE3S + SM16726 + // Also works with Merkury 904 RGBW Bulbs with 13 set to GPIO_SM16716_SEL + // https://www.flipkart.com/fc-mila-bxav-xs-ad-smart-bulb/p/itmf85zgs45fzr7n + // https://docs.tuya.com/en/hardware/WiFi-module/wifi-e3s-module.html + // http://www.datasheet-pdf.com/PDF/SM16716-Datasheet-Sunmoon-932771 + GPIO_USER, // GPIO00 N.C. + 0, // GPIO01 Serial RXD + GPIO_USER, // GPIO02 N.C. + 0, // GPIO03 Serial TXD + GPIO_SM16716_CLK, // GPIO04 SM16716 Clock + GPIO_PWM1, // GPIO05 White + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 Alt. White on some devices + GPIO_USER, // GPIO13 SM16716 Enable on some devices + GPIO_SM16716_DAT, // GPIO14 SM16716 Data + 0, // GPIO15 wired to GND + GPIO_USER, // GPIO16 N.C. + ADC0_USER // ADC0 A0 Analog input }, - { // EXS_DIMMER - EX-Store WiFi Dimmer v4, two channel (ESP8266 w/ separate MCU dimmer) - // https://ex-store.de/2-Kanal-RS232-WiFi-WLan-Dimmer-Modul-V4-fuer-Unterputzmontage-230V-3A - // https://ex-store.de/2-Kanal-RS232-WiFi-WLan-Dimmer-Modul-V4-fuer-Unterputzmontage-230V-3A-ESP8266-V12-Stift-und-Buchsenleisten + { // EXS_DIMMER - EX-Store WiFi Dimmer v4, two channel (ESP8266 w/ separate MCU dimmer) + // https://ex-store.de/2-Kanal-RS232-WiFi-WLan-Dimmer-Modul-V4-fuer-Unterputzmontage-230V-3A + // https://ex-store.de/2-Kanal-RS232-WiFi-WLan-Dimmer-Modul-V4-fuer-Unterputzmontage-230V-3A-ESP8266-V12-Stift-und-Buchsenleisten 0, - AGPIO(GPIO_TXD), // GPIO01 MCU serial control - AGPIO(GPIO_LEDLNK), // GPIO02 LED Link - AGPIO(GPIO_RXD), // GPIO03 MCU serial control - AGPIO(GPIO_USER), // GPIO04 - AGPIO(GPIO_USER), // GPIO05 - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 - AGPIO(GPIO_EXS_ENABLE), // GPIO13 EXS MCU Enable - AGPIO(GPIO_USER), // GPIO14 - 0, // GPIO15 + GPIO_TXD, // GPIO01 MCU serial control + GPIO_LEDLNK, // GPIO02 LED Link + GPIO_RXD, // GPIO03 MCU serial control + GPIO_USER, // GPIO04 + GPIO_USER, // GPIO05 + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 + GPIO_EXS_ENABLE, // GPIO13 EXS MCU Enable + GPIO_USER, // GPIO14 + 0, // GPIO15 0, 0 }, - { // PWM_DIMMER - Support for Martin Jerry/acenx/Tessan/NTONPOWER SD0x PWM - // dimmer switches. The brightness of the load for these dimmers is - // controlled by a PWM GPIO pin. There are typically power, up & down - // buttons and 4 LED's. Examples are: - // https://www.amazon.com/dp/B07FXYSVR1 - // https://www.amazon.com/dp/B07V26Q3VD - // https://www.amazon.com/dp/B07K67D43J - // https://www.amazon.com/dp/B07TTGFWFM - AGPIO(GPIO_KEY1) +2, // GPIO00 Up button - AGPIO(GPIO_KEY1) +1, // GPIO01 Down button - 0, // GPIO02 - AGPIO(GPIO_LED1_INV) +3, // GPIO03 Level 5 LED - AGPIO(GPIO_LEDLNK_INV), // GPIO04 LED Link - AGPIO(GPIO_LED1_INV) +2, // GPIO05 Level 4 LED - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_LED1_INV) +1, // GPIO12 Level 3 LED - AGPIO(GPIO_PWM1), // GPIO13 Dimmer PWM - AGPIO(GPIO_LED1_INV), // GPIO12 Level 2 LED - AGPIO(GPIO_KEY1_INV), // GPIO15 Power button - AGPIO(GPIO_REL1_INV), // GPIO16 Power relay/Level 1 LED + { // PWM_DIMMER - Support for Martin Jerry/acenx/Tessan/NTONPOWER SD0x PWM + // dimmer switches. The brightness of the load for these dimmers is + // controlled by a PWM GPIO pin. There are typically power, up & down + // buttons and 4 LED's. Examples are: + // https://www.amazon.com/dp/B07FXYSVR1 + // https://www.amazon.com/dp/B07V26Q3VD + // https://www.amazon.com/dp/B07K67D43J + // https://www.amazon.com/dp/B07TTGFWFM + GPIO_KEY3, // GPIO00 Up button + GPIO_KEY2, // GPIO01 Down button + 0, // GPIO02 + GPIO_LED4_INV, // GPIO03 Level 5 LED + GPIO_LEDLNK_INV, // GPIO04 LED Link + GPIO_LED3_INV, // GPIO05 Level 4 LED + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_LED2_INV, // GPIO12 Level 3 LED + GPIO_PWM1, // GPIO13 Dimmer PWM + GPIO_LED1_INV, // GPIO12 Level 2 LED + GPIO_KEY1_INV, // GPIO15 Power button + GPIO_REL1_INV, // GPIO16 Power relay/Level 1 LED 0 }, - { // SONOFF_ZB_BRIDGE - Sonoff Zigbee Bridge (ESP8266) - AGPIO(GPIO_LED1_INV), // GPIO00 Green Led (0 = On, 1 = Off) - Traffic between ESP and EFR - AGPIO(GPIO_ZIGBEE_TX), // GPIO01 Zigbee Serial control - 0, // GPIO02 - AGPIO(GPIO_ZIGBEE_RX), // GPIO03 Zigbee Serial control - AGPIO(GPIO_ZIGBEE_RST), // GPIO04 Zigbee Reset - 0, // GPIO05 EFR32 Bootloader mode (drive Low for Gecko Bootloader, inactive or high for Zigbee EmberZNet) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_I2C_SDA), // GPIO12 I2C SDA - connected to 512KB EEPROM - AGPIO(GPIO_LEDLNK_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link status - AGPIO(GPIO_I2C_SCL), // GPIO14 I2C SCL - connected to 512KB EEPROM - 0, // GPIO15 connected to IO15 pad, also used for logging - AGPIO(GPIO_KEY1), // GPIO16 Button + { // SONOFF_ZB_BRIDGE - Sonoff Zigbee Bridge (ESP8266) + GPIO_LED1_INV, // GPIO00 Green Led (0 = On, 1 = Off) - Traffic between ESP and EFR + GPIO_ZIGBEE_TX, // GPIO01 Zigbee Serial control + 0, // GPIO02 + GPIO_ZIGBEE_RX, // GPIO03 Zigbee Serial control + GPIO_ZIGBEE_RST, // GPIO04 Zigbee Reset + 0, // GPIO05 EFR32 Bootloader mode (drive Low for Gecko Bootloader, inactive or high for Zigbee EmberZNet) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_I2C_SDA, // GPIO12 I2C SDA - connected to 512KB EEPROM + GPIO_LEDLNK_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link status + GPIO_I2C_SCL, // GPIO14 I2C SCL - connected to 512KB EEPROM + 0, // GPIO15 connected to IO15 pad, also used for logging + GPIO_KEY1, // GPIO16 Button 0 } }; @@ -2100,113 +2323,113 @@ const mytmplt8266 kModules8266[TMP_MAXMODULE_8285] PROGMEM = { \*********************************************************************************************/ const mytmplt kModules8285[TMP_MAXMODULE_8266 - TMP_WEMOS] PROGMEM = { - { // WEMOS - Any ESP8266/ESP8285 device like WeMos and NodeMCU hardware (ESP8266) - AGPIO(GPIO_USER), // GPIO00 D3 Wemos Button Shield - AGPIO(GPIO_USER), // GPIO01 TX Serial RXD - AGPIO(GPIO_USER), // GPIO02 D4 Wemos DHT Shield - AGPIO(GPIO_USER), // GPIO03 RX Serial TXD and Optional sensor - AGPIO(GPIO_USER), // GPIO04 D2 Wemos I2C SDA - AGPIO(GPIO_USER), // GPIO05 D1 Wemos I2C SCL / Wemos Relay Shield (0 = Off, 1 = On) / Wemos WS2812B RGB led Shield - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - AGPIO(GPIO_USER), // GPIO09 (SD_DATA2 Flash QIO or ESP8285) - AGPIO(GPIO_USER), // GPIO10 (SD_DATA3 Flash QIO or ESP8285) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_USER), // GPIO12 D6 - AGPIO(GPIO_USER), // GPIO13 D7 - AGPIO(GPIO_USER), // GPIO14 D5 - AGPIO(GPIO_USER), // GPIO15 D8 - AGPIO(GPIO_USER), // GPIO16 D0 Wemos Wake - AGPIO(ADC0_USER) // ADC0 A0 Analog input + { // WEMOS - Any ESP8266/ESP8285 device like WeMos and NodeMCU hardware (ESP8266) + GPIO_USER, // GPIO00 D3 Wemos Button Shield + GPIO_USER, // GPIO01 TX Serial RXD + GPIO_USER, // GPIO02 D4 Wemos DHT Shield + GPIO_USER, // GPIO03 RX Serial TXD and Optional sensor + GPIO_USER, // GPIO04 D2 Wemos I2C SDA + GPIO_USER, // GPIO05 D1 Wemos I2C SCL / Wemos Relay Shield (0 = Off, 1 = On) / Wemos WS2812B RGB led Shield + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + GPIO_USER, // GPIO09 (SD_DATA2 Flash QIO or ESP8285) + GPIO_USER, // GPIO10 (SD_DATA3 Flash QIO or ESP8285) + // GPIO11 (SD_CMD Flash) + GPIO_USER, // GPIO12 D6 + GPIO_USER, // GPIO13 D7 + GPIO_USER, // GPIO14 D5 + GPIO_USER, // GPIO15 D8 + GPIO_USER, // GPIO16 D0 Wemos Wake + ADC0_USER // ADC0 A0 Analog input }, - { // SONOFF_4CH - Sonoff 4CH (ESP8285) - AGPIO(GPIO_KEY1), // GPIO00 Button 1 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Optional sensor - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_REL1) +2, // GPIO04 Sonoff 4CH Red Led and Relay 3 (0 = Off, 1 = On) - AGPIO(GPIO_REL1) +1, // GPIO05 Sonoff 4CH Red Led and Relay 2 (0 = Off, 1 = On) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - AGPIO(GPIO_KEY1) +1, // GPIO09 Button 2 - AGPIO(GPIO_KEY1) +2, // GPIO10 Button 3 - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Red Led and Relay 1 (0 = Off, 1 = On) - Link and Power status - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - AGPIO(GPIO_KEY1) +3, // GPIO14 Button 4 - AGPIO(GPIO_REL1) +3, // GPIO15 Red Led and Relay 4 (0 = Off, 1 = On) + { // SONOFF_4CH - Sonoff 4CH (ESP8285) + GPIO_KEY1, // GPIO00 Button 1 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Optional sensor + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_REL3, // GPIO04 Sonoff 4CH Red Led and Relay 3 (0 = Off, 1 = On) + GPIO_REL2, // GPIO05 Sonoff 4CH Red Led and Relay 2 (0 = Off, 1 = On) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + GPIO_KEY2, // GPIO09 Button 2 + GPIO_KEY3, // GPIO10 Button 3 + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Red Led and Relay 1 (0 = Off, 1 = On) - Link and Power status + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) + GPIO_KEY4, // GPIO14 Button 4 + GPIO_REL4, // GPIO15 Red Led and Relay 4 (0 = Off, 1 = On) 0, 0 }, - { // SONOFF_T12 - Sonoff T1 2CH (ESP8285) - AGPIO(GPIO_KEY1), // GPIO00 Button 1 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Optional Sensor (J3 Pin 5) - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor + { // SONOFF_T12 - Sonoff T1 2CH (ESP8285) + GPIO_KEY1, // GPIO00 Button 1 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Optional Sensor (J3 Pin 5) + GPIO_USER, // GPIO03 Serial TXD and Optional sensor 0, - AGPIO(GPIO_REL1) +1, // GPIO05 Blue Led and Relay 2 (0 = Off, 1 = On) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - AGPIO(GPIO_KEY1) +1, // GPIO09 Button 2 - 0, // GPIO10 - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Blue Led and Relay 1 (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + GPIO_REL2, // GPIO05 Blue Led and Relay 2 (0 = Off, 1 = On) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + GPIO_KEY2, // GPIO09 Button 2 + 0, // GPIO10 + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Blue Led and Relay 1 (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // SONOFF_T13 - Sonoff T1 3CH (ESP8285) - AGPIO(GPIO_KEY1), // GPIO00 Button 1 - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor - AGPIO(GPIO_USER), // GPIO02 Optional Sensor (J3 Pin 5) - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor - AGPIO(GPIO_REL1) +2, // GPIO04 Blue Led and Relay 3 (0 = Off, 1 = On) - AGPIO(GPIO_REL1) +1, // GPIO05 Blue Led and Relay 2 (0 = Off, 1 = On) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - AGPIO(GPIO_KEY1) +1, // GPIO09 Button 2 - AGPIO(GPIO_KEY1) +2, // GPIO10 Button 3 - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Blue Led and Relay 1 (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + { // SONOFF_T13 - Sonoff T1 3CH (ESP8285) + GPIO_KEY1, // GPIO00 Button 1 + GPIO_USER, // GPIO01 Serial RXD and Optional sensor + GPIO_USER, // GPIO02 Optional Sensor (J3 Pin 5) + GPIO_USER, // GPIO03 Serial TXD and Optional sensor + GPIO_REL3, // GPIO04 Blue Led and Relay 3 (0 = Off, 1 = On) + GPIO_REL2, // GPIO05 Blue Led and Relay 2 (0 = Off, 1 = On) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + GPIO_KEY2, // GPIO09 Button 2 + GPIO_KEY3, // GPIO10 Button 3 + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Blue Led and Relay 1 (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // SONOFF_DUAL_R2 - Sonoff Dual R2 (ESP8285) - AGPIO(GPIO_USER), // GPIO00 Button 0 on header (0 = On, 1 = Off) - AGPIO(GPIO_USER), // GPIO01 Serial RXD and Optional sensor + { // SONOFF_DUAL_R2 - Sonoff Dual R2 (ESP8285) + GPIO_USER, // GPIO00 Button 0 on header (0 = On, 1 = Off) + GPIO_USER, // GPIO01 Serial RXD and Optional sensor 0, - AGPIO(GPIO_USER), // GPIO03 Serial TXD and Optional sensor + GPIO_USER, // GPIO03 Serial TXD and Optional sensor 0, - AGPIO(GPIO_REL1) +1, // GPIO05 Relay 2 (0 = Off, 1 = On) - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - AGPIO(GPIO_USER), // GPIO09 Button 1 on header (0 = On, 1 = Off) - AGPIO(GPIO_KEY1), // GPIO10 Button on casing - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1), // GPIO12 Relay 1 (0 = Off, 1 = On) - AGPIO(GPIO_LED1_INV), // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status + GPIO_REL2, // GPIO05 Relay 2 (0 = Off, 1 = On) + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + GPIO_USER, // GPIO09 Button 1 on header (0 = On, 1 = Off) + GPIO_KEY1, // GPIO10 Button on casing + // GPIO11 (SD_CMD Flash) + GPIO_REL1, // GPIO12 Relay 1 (0 = Off, 1 = On) + GPIO_LED1_INV, // GPIO13 Blue Led (0 = On, 1 = Off) - Link and Power status 0, 0, 0, 0 }, - { // SONOFF_IFAN03 - Sonoff iFan03 (ESP8285) - AGPIO(GPIO_KEY1), // GPIO00 WIFI_KEY0 Button 1 - AGPIO(GPIO_TXD), // GPIO01 ESP_TXD Serial RXD connection to P0.5 of RF microcontroller - 0, // GPIO02 ESP_LOG - AGPIO(GPIO_RXD), // GPIO03 ESP_RXD Serial TXD connection to P0.4 of RF microcontroller - 0, // GPIO04 DEBUG_RX - 0, // GPIO05 DEBUG_TX - // GPIO06 (SD_CLK Flash) - // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) - // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) - AGPIO(GPIO_REL1_INV), // GPIO09 WIFI_O0 Relay 1 (0 = Off, 1 = On) controlling the light - AGPIO(GPIO_BUZZER_INV), // GPIO10 WIFI_O4 Buzzer (0 = Off, 1 = On) - // GPIO11 (SD_CMD Flash) - AGPIO(GPIO_REL1) +2, // GPIO12 WIFI_O2 Relay 3 (0 = Off, 1 = On) controlling the fan - AGPIO(GPIO_LED1_INV), // GPIO13 WIFI_CHK Blue Led on PCA (0 = On, 1 = Off) - Link and Power status - AGPIO(GPIO_REL1) +1, // GPIO14 WIFI_O1 Relay 2 (0 = Off, 1 = On) controlling the fan - AGPIO(GPIO_REL1) +3, // GPIO15 WIFI_O3 Relay 4 (0 = Off, 1 = On) controlling the fan + { // SONOFF_IFAN03 - Sonoff iFan03 (ESP8285) + GPIO_KEY1, // GPIO00 WIFI_KEY0 Button 1 + GPIO_TXD, // GPIO01 ESP_TXD Serial RXD connection to P0.5 of RF microcontroller + 0, // GPIO02 ESP_LOG + GPIO_RXD, // GPIO03 ESP_RXD Serial TXD connection to P0.4 of RF microcontroller + 0, // GPIO04 DEBUG_RX + 0, // GPIO05 DEBUG_TX + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + GPIO_REL1_INV, // GPIO09 WIFI_O0 Relay 1 (0 = Off, 1 = On) controlling the light + GPIO_BUZZER_INV, // GPIO10 WIFI_O4 Buzzer (0 = Off, 1 = On) + // GPIO11 (SD_CMD Flash) + GPIO_REL3, // GPIO12 WIFI_O2 Relay 3 (0 = Off, 1 = On) controlling the fan + GPIO_LED1_INV, // GPIO13 WIFI_CHK Blue Led on PCA (0 = On, 1 = Off) - Link and Power status + GPIO_REL2, // GPIO14 WIFI_O1 Relay 2 (0 = Off, 1 = On) controlling the fan + GPIO_REL4, // GPIO15 WIFI_O3 Relay 4 (0 = Off, 1 = On) controlling the fan 0, 0 } }; diff --git a/tasmota/tasmota_version.h b/tasmota/tasmota_version.h index e4942be91..3103534c2 100644 --- a/tasmota/tasmota_version.h +++ b/tasmota/tasmota_version.h @@ -20,7 +20,7 @@ #ifndef _TASMOTA_VERSION_H_ #define _TASMOTA_VERSION_H_ -const uint32_t VERSION = 0x08050000; +const uint32_t VERSION = 0x08050001; // Lowest compatible version const uint32_t VERSION_COMPATIBLE = 0x07010006; diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index ed15c9e12..48abbb490 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -1465,6 +1465,31 @@ void HandleRoot(void) #endif // USE_SONOFF_IFAN WSContentSend_P(PSTR("")); } +#ifdef USE_TUYA_MCU + if (IsModuleTuya()) { + uint8_t modeset = 0; + if (AsModuleTuyaMS()) { + WSContentSend_P(HTTP_TABLE100); + WSContentSend_P(PSTR("
")); + snprintf_P(stemp, sizeof(stemp), PSTR("" D_JSON_IRHVAC_MODE "")); + WSContentSend_P(HTTP_DEVICE_CONTROL, 26, devices_present + 1, + (strlen(SettingsText(SET_BUTTON1 + devices_present))) ? SettingsText(SET_BUTTON1 + devices_present) : stemp, ""); + WSContentSend_P(PSTR("")); + modeset = 1; + } + if (IsTuyaFanCtrl()) { + uint8_t device = devices_present + modeset; + WSContentSend_P(HTTP_TABLE100); + WSContentSend_P(PSTR("
")); + for (uint32_t i = device + 1; i <= (TuyaFanSpeeds() + device) + 1; i++) { + snprintf_P(stemp, sizeof(stemp), PSTR("%d"), i - (device + 1)); + WSContentSend_P(HTTP_DEVICE_CONTROL, 16, i, + (strlen(SettingsText(SET_BUTTON1 + i))) ? SettingsText(SET_BUTTON1 + i) : stemp, ""); + } + WSContentSend_P(PSTR("")); + } + } +#endif // USE_TUYA_MCU #ifdef USE_SONOFF_RF if (SONOFF_BRIDGE == my_module_type) { WSContentSend_P(HTTP_TABLE100); @@ -1535,6 +1560,33 @@ bool HandleRootStatusRefresh(void) } } else { #endif // USE_SONOFF_IFAN +#ifdef USE_TUYA_MCU + if (IsModuleTuya()) { + uint8_t FuncIdx = 0; + if (device <= devices_present) { + ExecuteCommandPower(device, POWER_TOGGLE, SRC_IGNORE); + } else { + if (AsModuleTuyaMS() && device == devices_present + 1) { + uint8_t dpId = TuyaGetDpId(TUYA_MCU_FUNC_MODESET); + snprintf_P(svalue, sizeof(svalue), PSTR("Tuyasend4 %d,%d"), dpId, !TuyaModeSet()); + ExecuteCommand(svalue, SRC_WEBGUI); + } + if (IsTuyaFanCtrl()) { + uint8_t dpId = 0; + for (uint32_t i = 0; i <= 3; i++) { // Tuya Function FAN3 to FAN6 + if (TuyaGetDpId(TUYA_MCU_FUNC_FAN3 + i) != 0) { + dpId = TuyaGetDpId(TUYA_MCU_FUNC_FAN3 + i); + } + } + if ((AsModuleTuyaMS() && device != devices_present + 1) || !AsModuleTuyaMS()) { + if (AsModuleTuyaMS()) {FuncIdx = 1;} + snprintf_P(svalue, sizeof(svalue), PSTR("Tuyasend2 %d,%d"), dpId, (device - (devices_present + FuncIdx) - 1)); + ExecuteCommand(svalue, SRC_WEBGUI); + } + } + } + } else { +#endif // USE_TUYA_MCU #ifdef USE_SHUTTER int32_t ShutterWebButton; if (ShutterWebButton = IsShutterWebButton(device)) { @@ -1549,6 +1601,9 @@ bool HandleRootStatusRefresh(void) #ifdef USE_SONOFF_IFAN } #endif // USE_SONOFF_IFAN +#ifdef USE_TUYA_MCU + } +#endif // USE_TUYA_MCU } #ifdef USE_LIGHT WebGetArg("d0", tmp, sizeof(tmp)); // 0 - 100 Dimmer value @@ -1629,8 +1684,22 @@ bool HandleRootStatusRefresh(void) #ifdef USE_SONOFF_IFAN } #endif // USE_SONOFF_IFAN + WSContentSend_P(PSTR("")); } +#ifdef USE_TUYA_MCU + if (IsModuleTuya()) { + uint32_t fanspeed = TuyaFanState(); + uint32_t modeset = TuyaModeSet(); + if (IsTuyaFanCtrl() && !AsModuleTuyaMS()) { + WSContentSend_P(PSTR("
" D_JSON_IRHVAC_FANSPEED ": %d
"), fanspeed); + } else if (!IsTuyaFanCtrl() && AsModuleTuyaMS()) { + WSContentSend_P(PSTR("
" D_JSON_IRHVAC_MODE ": %d
"), modeset); + } else if (IsTuyaFanCtrl() && AsModuleTuyaMS()) { + WSContentSend_P(PSTR("
" D_JSON_IRHVAC_MODE ": %d - " D_JSON_IRHVAC_FANSPEED ": %d
"), modeset, fanspeed); + } + } +#endif // USE_TUYA_MCU WSContentEnd(); return true; @@ -3275,22 +3344,18 @@ bool JsonWebColor(const char* dataBuf) // Default pre v7 (Light theme) // {"WebColor":["#000","#fff","#f2f2f2","#000","#fff","#000","#fff","#f00","#008000","#fff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#fff","#999","#000"]} // {"WebColor":["#000000","#ffffff","#f2f2f2","#000000","#ffffff","#000000","#ffffff","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} - char dataBufLc[strlen(dataBuf) +1]; - LowerCase(dataBufLc, dataBuf); - RemoveSpace(dataBufLc); - if (strlen(dataBufLc) < 9) { return false; } // Workaround exception if empty JSON like {} - Needs checks - - StaticJsonBuffer<450> jb; // 421 from https://arduinojson.org/v5/assistant/ - JsonObject& obj = jb.parseObject(dataBufLc); - if (!obj.success()) { return false; } - - char parm_lc[10]; - if (obj[LowerCase(parm_lc, D_CMND_WEBCOLOR)].success()) { - for (uint32_t i = 0; i < COL_LAST; i++) { - const char* color = obj[parm_lc][i]; - if (color != nullptr) { - WebHexCode(i, color); + JsonParser parser((char*) dataBuf); + JsonParserObject root = parser.getRootObject(); + JsonParserArray arr = root[PSTR(D_CMND_WEBCOLOR)].getArray(); + if (arr) { // if arr is valid, i.e. json is valid, the key D_CMND_WEBCOLOR was found and the token is an arra + uint32_t i = 0; + for (auto color : arr) { + if (i < COL_LAST) { + WebHexCode(i, color.getStr()); + } else { + break; } + i++; } } return true; diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 767e65b8e..cc0ed8e62 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -57,7 +57,7 @@ struct MQTT { uint8_t initial_connection_state = 2; // MQTT connection messages state bool connected = false; // MQTT virtual connection status bool allowed = false; // MQTT enabled and parameters valid - bool tls_private_key = false; // MQTT require a private key before connecting + bool mqtt_tls = false; // MQTT TLS is enabled } Mqtt; #ifdef USE_MQTT_TLS @@ -149,22 +149,24 @@ void MqttInit(void) // Turn on TLS for port 8883 (TLS) and 8884 (TLS, client certificate) Settings.flag4.mqtt_tls = true; } + Mqtt.mqtt_tls = Settings.flag4.mqtt_tls; // this flag should not change even if we change the SetOption (until reboot) // Detect AWS IoT and set default parameters String host = String(SettingsText(SET_MQTT_HOST)); if (host.indexOf(".iot.") && host.endsWith(".amazonaws.com")) { // look for ".iot." and ".amazonaws.com" in the domain name Settings.flag4.mqtt_no_retain = true; - Mqtt.tls_private_key = true; } - if (Settings.flag4.mqtt_tls) { + if (Mqtt.mqtt_tls) { tlsClient = new BearSSL::WiFiClientSecure_light(1024,1024); #ifdef USE_MQTT_AWS_IOT loadTlsDir(); // load key and certificate data from Flash - tlsClient->setClientECCert(AWS_IoT_Client_Certificate, - AWS_IoT_Private_Key, - 0xFFFF /* all usages, don't care */, 0); + if ((nullptr != AWS_IoT_Private_Key) && (nullptr != AWS_IoT_Client_Certificate)) { + tlsClient->setClientECCert(AWS_IoT_Client_Certificate, + AWS_IoT_Private_Key, + 0xFFFF /* all usages, don't care */, 0); + } #endif #ifdef USE_MQTT_TLS_CA_CERT @@ -353,7 +355,7 @@ void MqttPublishPrefixTopic_P(uint32_t prefix, const char* subtopic, bool retain GetTopic_P(stopic, prefix, mqtt_topic, romram); MqttPublish(stopic, retained); -#ifdef USE_MQTT_AWS_IOT +#if defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_AWS_IOT_LIGHT) if ((prefix > 0) && (Settings.flag4.awsiot_shadow) && (Mqtt.connected)) { // placeholder for SetOptionXX // compute the target topic char *topic = SettingsText(SET_MQTT_TOPIC); @@ -492,7 +494,7 @@ void MqttConnected(void) Mqtt.connect_count++; GetTopic_P(stopic, TELE, mqtt_topic, S_LWT); - Response_P(PSTR(D_ONLINE)); + Response_P(PSTR(MQTT_LWT_ONLINE)); MqttPublish(stopic, true); if (!Settings.flag4.only_json_message) { // SetOption90 - Disable non-json MQTT response @@ -578,8 +580,8 @@ void MqttReconnect(void) } #if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) // don't enable MQTT for AWS IoT if Private Key or Certificate are not set - if (Settings.flag4.mqtt_tls && Mqtt.tls_private_key) { - if (!AWS_IoT_Private_Key || !AWS_IoT_Client_Certificate) { + if (Mqtt.mqtt_tls) { + if (0 == strlen(SettingsText(SET_MQTT_PWD))) { // we anticipate that an empty password does not make sense with TLS. This avoids failed connections Mqtt.allowed = false; } } @@ -610,11 +612,11 @@ void MqttReconnect(void) } GetTopic_P(stopic, TELE, mqtt_topic, S_LWT); - Response_P(S_OFFLINE); + Response_P(S_LWT_OFFLINE); if (MqttClient.connected()) { MqttClient.disconnect(); } #ifdef USE_MQTT_TLS - if (Settings.flag4.mqtt_tls) { + if (Mqtt.mqtt_tls) { tlsClient->stop(); } else { EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) @@ -632,10 +634,12 @@ void MqttReconnect(void) MqttClient.setCallback(MqttDataHandler); #if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) // re-assign private keys in case it was updated in between - if (Settings.flag4.mqtt_tls) { - tlsClient->setClientECCert(AWS_IoT_Client_Certificate, - AWS_IoT_Private_Key, - 0xFFFF /* all usages, don't care */, 0); + if (Mqtt.mqtt_tls) { + if ((nullptr != AWS_IoT_Private_Key) && (nullptr != AWS_IoT_Client_Certificate)) { + tlsClient->setClientECCert(AWS_IoT_Client_Certificate, + AWS_IoT_Private_Key, + 0xFFFF /* all usages, don't care */, 0); + } } #endif MqttClient.setServer(SettingsText(SET_MQTT_HOST), Settings.mqtt_port); @@ -645,7 +649,7 @@ void MqttReconnect(void) bool allow_all_fingerprints; bool learn_fingerprint1; bool learn_fingerprint2; - if (Settings.flag4.mqtt_tls) { + if (Mqtt.mqtt_tls) { allow_all_fingerprints = false; learn_fingerprint1 = is_fingerprint_mono_value(Settings.mqtt_fingerprint[0], 0x00); learn_fingerprint2 = is_fingerprint_mono_value(Settings.mqtt_fingerprint[1], 0x00); @@ -658,16 +662,18 @@ void MqttReconnect(void) #endif bool lwt_retain = Settings.flag4.mqtt_no_retain ? false : true; // no retained last will if "no_retain" #if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) - if (Settings.flag4.mqtt_tls && Mqtt.tls_private_key) { - // If we require private key then we should null user/pwd - mqtt_user = nullptr; - mqtt_pwd = nullptr; + if (Mqtt.mqtt_tls) { + if ((nullptr != AWS_IoT_Private_Key) && (nullptr != AWS_IoT_Client_Certificate)) { + // if private key is there, we remove user/pwd + mqtt_user = nullptr; + mqtt_pwd = nullptr; + } } #endif if (MqttClient.connect(mqtt_client, mqtt_user, mqtt_pwd, stopic, 1, lwt_retain, mqtt_data, MQTT_CLEAN_SESSION)) { #ifdef USE_MQTT_TLS - if (Settings.flag4.mqtt_tls) { + if (Mqtt.mqtt_tls) { AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "TLS connected in %d ms, max ThunkStack used %d"), millis() - mqtt_connect_time, tlsClient->getMaxThunkStackUse()); if (!tlsClient->getMFLNStatus()) { @@ -739,7 +745,7 @@ void MqttReconnect(void) MqttConnected(); } else { #ifdef USE_MQTT_TLS - if (Settings.flag4.mqtt_tls) { + if (Mqtt.mqtt_tls) { AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "TLS connection error: %d"), tlsClient->getLastError()); } #endif @@ -889,8 +895,8 @@ void CmndFullTopic(void) char stemp1[TOPSZ]; strlcpy(stemp1, (SC_DEFAULT == Shortcut()) ? MQTT_FULLTOPIC : XdrvMailbox.data, sizeof(stemp1)); if (strcmp(stemp1, SettingsText(SET_MQTT_FULLTOPIC))) { - Response_P((Settings.flag.mqtt_offline) ? S_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format - MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic + Response_P((Settings.flag.mqtt_offline) ? S_LWT_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format + MqttPublishPrefixTopic_P(TELE, S_LWT, true); // Offline or remove previous retained topic SettingsUpdateText(SET_MQTT_FULLTOPIC, stemp1); restart_flag = 2; } @@ -993,8 +999,8 @@ void CmndTopic(void) char stemp1[TOPSZ]; strlcpy(stemp1, (SC_DEFAULT == Shortcut()) ? MQTT_TOPIC : XdrvMailbox.data, sizeof(stemp1)); if (strcmp(stemp1, SettingsText(SET_MQTT_TOPIC))) { - Response_P((Settings.flag.mqtt_offline) ? S_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format - MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic + Response_P((Settings.flag.mqtt_offline) ? S_LWT_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format + MqttPublishPrefixTopic_P(TELE, S_LWT, true); // Offline or remove previous retained topic SettingsUpdateText(SET_MQTT_TOPIC, stemp1); restart_flag = 2; } @@ -1311,7 +1317,7 @@ void HandleMqttConfiguration(void) SettingsText(SET_MQTT_HOST), Settings.mqtt_port, #ifdef USE_MQTT_TLS - Settings.flag4.mqtt_tls ? " checked" : "", // SetOption102 - Enable MQTT TLS + Mqtt.mqtt_tls ? " checked" : "", // SetOption102 - Enable MQTT TLS #endif // USE_MQTT_TLS Format(str, MQTT_CLIENT_ID, sizeof(str)), MQTT_CLIENT_ID, SettingsText(SET_MQTT_CLIENT)); WSContentSend_P(HTTP_FORM_MQTT2, @@ -1336,7 +1342,7 @@ void MqttSaveSettings(void) strlcpy(stemp2, (!strlen(tmp)) ? MQTT_FULLTOPIC : tmp, sizeof(stemp2)); MakeValidMqtt(1, stemp2); if ((strcmp(stemp, SettingsText(SET_MQTT_TOPIC))) || (strcmp(stemp2, SettingsText(SET_MQTT_FULLTOPIC)))) { - Response_P((Settings.flag.mqtt_offline) ? S_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format + Response_P((Settings.flag.mqtt_offline) ? S_LWT_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format MqttPublishPrefixTopic_P(TELE, S_LWT, true); // Offline or remove previous retained topic } SettingsUpdateText(SET_MQTT_TOPIC, stemp); @@ -1346,11 +1352,11 @@ void MqttSaveSettings(void) WebGetArg("ml", tmp, sizeof(tmp)); Settings.mqtt_port = (!strlen(tmp)) ? MQTT_PORT : atoi(tmp); #ifdef USE_MQTT_TLS - Settings.flag4.mqtt_tls = Webserver->hasArg("b3"); // SetOption102 - Enable MQTT TLS + Mqtt.mqtt_tls = Webserver->hasArg("b3"); // SetOption102 - Enable MQTT TLS #endif WebGetArg("mc", tmp, sizeof(tmp)); SettingsUpdateText(SET_MQTT_CLIENT, (!strlen(tmp)) ? MQTT_CLIENT_ID : tmp); -#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) +#if defined(USE_MQTT_TLS) && (defined(USE_MQTT_AWS_IOT) || defined(USE_MQTT_AWS_IOT_LIGHT)) AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT D_CMND_MQTTHOST " %s, " D_CMND_MQTTPORT " %d, " D_CMND_MQTTCLIENT " %s, " D_CMND_TOPIC " %s, " D_CMND_FULLTOPIC " %s"), SettingsText(SET_MQTT_HOST), Settings.mqtt_port, SettingsText(SET_MQTT_CLIENT), SettingsText(SET_MQTT_TOPIC), SettingsText(SET_MQTT_FULLTOPIC)); #else // USE_MQTT_AWS_IOT diff --git a/tasmota/xdrv_04_light.ino b/tasmota/xdrv_04_light.ino index 941659d62..b90d882c8 100644 --- a/tasmota/xdrv_04_light.ino +++ b/tasmota/xdrv_04_light.ino @@ -263,8 +263,6 @@ struct LIGHT { uint32_t strip_timer_counter = 0; // Bars and Gradient power_t power = 0; // Power for each channel if SetOption68, or boolean if single light - uint16_t wakeup_counter = 0; - uint8_t entry_color[LST_MAX]; uint8_t current_color[LST_MAX]; uint8_t new_color[LST_MAX]; @@ -276,11 +274,12 @@ struct LIGHT { uint8_t subtype = 0; // LST_ subtype uint8_t device = 0; uint8_t old_power = 1; - uint8_t wakeup_active = 0; - uint8_t wakeup_dimmer = 0; + uint8_t wakeup_active = 0; // 0=inctive, 1=on-going, 2=about to start, 3=will be triggered next cycle uint8_t fixed_color_index = 1; uint8_t pwm_offset = 0; // Offset in color buffer uint8_t max_scheme = LS_MAX -1; + + uint32_t wakeup_start_time = 0; bool update = true; bool pwm_multi_channels = false; // SetOption68, treat each PWM channel as an independant dimmer @@ -1866,25 +1865,27 @@ void LightAnimate(void) light_controller.calcLevels(Light.new_color); break; case LS_WAKEUP: - if (2 == Light.wakeup_active) { - Light.wakeup_active = 1; - for (uint32_t i = 0; i < Light.subtype; i++) { - Light.new_color[i] = 0; + { + if (2 == Light.wakeup_active) { + Light.wakeup_active = 1; + for (uint32_t i = 0; i < Light.subtype; i++) { + Light.new_color[i] = 0; + } + Light.wakeup_start_time = millis(); } - Light.wakeup_counter = 0; - Light.wakeup_dimmer = 0; - } - Light.wakeup_counter++; - if (Light.wakeup_counter > ((Settings.light_wakeup * STATES) / Settings.light_dimmer)) { - Light.wakeup_counter = 0; - Light.wakeup_dimmer++; - if (Light.wakeup_dimmer <= Settings.light_dimmer) { - light_state.setDimmer(Light.wakeup_dimmer); + // which step are we in a range 0..1023 + uint32_t step_10 = ((millis() - Light.wakeup_start_time) * 1023) / (Settings.light_wakeup * 1000); + if (step_10 > 1023) { step_10 = 1023; } // sanity check + uint8_t wakeup_bri = changeUIntScale(step_10, 0, 1023, 0, LightStateClass::DimmerToBri(Settings.light_dimmer)); + + if (wakeup_bri != light_state.getBri()) { + light_state.setBri(wakeup_bri); light_controller.calcLevels(); for (uint32_t i = 0; i < Light.subtype; i++) { Light.new_color[i] = Light.current_color[i]; } - } else { + } + if (1023 == step_10) { Response_P(PSTR("{\"" D_CMND_WAKEUP "\":\"" D_JSON_DONE "\"")); ResponseLightState(1); ResponseJsonEnd(); diff --git a/tasmota/xdrv_05_irremote.ino b/tasmota/xdrv_05_irremote.ino index bb5ce0c3c..690dc0c61 100644 --- a/tasmota/xdrv_05_irremote.ino +++ b/tasmota/xdrv_05_irremote.ino @@ -176,30 +176,21 @@ void IrReceiveCheck(void) uint32_t IrRemoteCmndIrSendJson(void) { - // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(3) + 40 = 96 // IRsend { "protocol": "RC5", "bits": 12, "data":"0xC86" } // IRsend { "protocol": "SAMSUNG", "bits": 32, "data": 551502015 } - char dataBufUc[XdrvMailbox.data_len + 1]; - UpperCase(dataBufUc, XdrvMailbox.data); - RemoveSpace(dataBufUc); - if (strlen(dataBufUc) < 8) { - return IE_INVALID_JSON; - } - - StaticJsonBuffer<140> jsonBuf; - JsonObject &root = jsonBuf.parseObject(dataBufUc); - if (!root.success()) { - return IE_INVALID_JSON; - } + RemoveSpace(XdrvMailbox.data); // TODO is this really needed? + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { return IE_INVALID_JSON; } // IRsend { "protocol": "SAMSUNG", "bits": 32, "data": 551502015 } // IRsend { "protocol": "NEC", "bits": 32, "data":"0x02FDFE80", "repeat": 2 } - char parm_uc[10]; - const char *protocol = root[UpperCase_P(parm_uc, PSTR(D_JSON_IR_PROTOCOL))]; - uint16_t bits = root[UpperCase_P(parm_uc, PSTR(D_JSON_IR_BITS))]; - uint64_t data = strtoull(root[UpperCase_P(parm_uc, PSTR(D_JSON_IR_DATA))], nullptr, 0); - uint16_t repeat = root[UpperCase_P(parm_uc, PSTR(D_JSON_IR_REPEAT))]; + const char *protocol = root.getStr(PSTR(D_JSON_IR_PROTOCOL), ""); + uint16_t bits = root.getUInt(PSTR(D_JSON_IR_BITS), 0); + uint64_t data = root.getULong(PSTR(D_JSON_IR_DATA), 0); + uint16_t repeat = root.getUInt(PSTR(D_JSON_IR_REPEAT), 0); + // check if the IRSend is great than repeat if (XdrvMailbox.index > repeat + 1) { repeat = XdrvMailbox.index - 1; @@ -243,7 +234,6 @@ void CmndIrSend(void) uint8_t error = IE_SYNTAX_IRSEND; if (XdrvMailbox.data_len) { -// error = (strstr(XdrvMailbox.data, "{") == nullptr) ? IrRemoteCmndIrSendRaw() : IrRemoteCmndIrSendJson(); if (strstr(XdrvMailbox.data, "{") == nullptr) { error = IE_INVALID_JSON; } else { diff --git a/tasmota/xdrv_05_irremote_full.ino b/tasmota/xdrv_05_irremote_full.ino index 9649e87b4..0b5f09ca3 100644 --- a/tasmota/xdrv_05_irremote_full.ino +++ b/tasmota/xdrv_05_irremote_full.ino @@ -111,38 +111,39 @@ void IrReceiveInit(void) } String sendACJsonState(const stdAc::state_t &state) { - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.createObject(); - json[D_JSON_IRHVAC_VENDOR] = typeToString(state.protocol); - json[D_JSON_IRHVAC_MODEL] = state.model; - json[D_JSON_IRHVAC_POWER] = IRac::boolToString(state.power); - json[D_JSON_IRHVAC_MODE] = IRac::opmodeToString(state.mode); + JsonGeneratorObject json; + json.add(PSTR(D_JSON_IRHVAC_VENDOR), typeToString(state.protocol)); + json.add(PSTR(D_JSON_IRHVAC_MODEL), state.model); + // Home Assistant wants mode to be off if power is also off & vice-versa. if (state.mode == stdAc::opmode_t::kOff || !state.power) { - json[D_JSON_IRHVAC_MODE] = IRac::opmodeToString(stdAc::opmode_t::kOff); - json[D_JSON_IRHVAC_POWER] = IRac::boolToString(false); - } - json[D_JSON_IRHVAC_CELSIUS] = IRac::boolToString(state.celsius); - if (floorf(state.degrees) == state.degrees) { - json[D_JSON_IRHVAC_TEMP] = floorf(state.degrees); // integer + json.add(PSTR(D_JSON_IRHVAC_MODE), IRac::opmodeToString(stdAc::opmode_t::kOff)); + json.add(PSTR(D_JSON_IRHVAC_POWER), IRac::boolToString(false)); } else { - json[D_JSON_IRHVAC_TEMP] = RawJson(String(state.degrees, 1)); // non-integer, limit to only 1 sub-digit + json.add(PSTR(D_JSON_IRHVAC_MODE), IRac::opmodeToString(state.mode)); + json.add(PSTR(D_JSON_IRHVAC_POWER), IRac::boolToString(state.power)); + } + json.add(PSTR(D_JSON_IRHVAC_CELSIUS), IRac::boolToString(state.celsius)); + if (floorf(state.degrees) == state.degrees) { + json.add(PSTR(D_JSON_IRHVAC_TEMP), (int32_t) floorf(state.degrees)); // integer + } else { + // TODO can do better here + json.addStrRaw(PSTR(D_JSON_IRHVAC_TEMP), String(state.degrees, 1).c_str()); // non-integer, limit to only 1 sub-digit } - json[D_JSON_IRHVAC_FANSPEED] = IRac::fanspeedToString(state.fanspeed); - json[D_JSON_IRHVAC_SWINGV] = IRac::swingvToString(state.swingv); - json[D_JSON_IRHVAC_SWINGH] = IRac::swinghToString(state.swingh); - json[D_JSON_IRHVAC_QUIET] = IRac::boolToString(state.quiet); - json[D_JSON_IRHVAC_TURBO] = IRac::boolToString(state.turbo); - json[D_JSON_IRHVAC_ECONO] = IRac::boolToString(state.econo); - json[D_JSON_IRHVAC_LIGHT] = IRac::boolToString(state.light); - json[D_JSON_IRHVAC_FILTER] = IRac::boolToString(state.filter); - json[D_JSON_IRHVAC_CLEAN] = IRac::boolToString(state.clean); - json[D_JSON_IRHVAC_BEEP] = IRac::boolToString(state.beep); - json[D_JSON_IRHVAC_SLEEP] = state.sleep; - String payload = ""; - payload.reserve(200); - json.printTo(payload); + json.add(PSTR(D_JSON_IRHVAC_FANSPEED), IRac::fanspeedToString(state.fanspeed)); + json.add(PSTR(D_JSON_IRHVAC_SWINGV), IRac::swingvToString(state.swingv)); + json.add(PSTR(D_JSON_IRHVAC_SWINGH), IRac::swinghToString(state.swingh)); + json.add(PSTR(D_JSON_IRHVAC_QUIET), IRac::boolToString(state.quiet)); + json.add(PSTR(D_JSON_IRHVAC_TURBO), IRac::boolToString(state.turbo)); + json.add(PSTR(D_JSON_IRHVAC_ECONO), IRac::boolToString(state.econo)); + json.add(PSTR(D_JSON_IRHVAC_LIGHT), IRac::boolToString(state.light)); + json.add(PSTR(D_JSON_IRHVAC_FILTER), IRac::boolToString(state.filter)); + json.add(PSTR(D_JSON_IRHVAC_CLEAN), IRac::boolToString(state.clean)); + json.add(PSTR(D_JSON_IRHVAC_BEEP), IRac::boolToString(state.beep)); + json.add(PSTR(D_JSON_IRHVAC_SLEEP), state.sleep); + + String payload = json.toString(); // copy string before returning, the original is on the stack return payload; } @@ -267,6 +268,16 @@ String listSupportedProtocols(bool hvac) { return l; } +bool strToBool(class JsonParserToken token, bool def) { + if (token.isBool() || token.isNum()) { + return token.getBool(); + } else if (token.isStr()) { + return IRac::strToBool(token.getStr()); + } else { + return def; + } +} + // used to convert values 0-5 to fanspeed_t const stdAc::fanspeed_t IrHvacFanSpeed[] PROGMEM = { stdAc::fanspeed_t::kAuto, stdAc::fanspeed_t::kMin, stdAc::fanspeed_t::kLow,stdAc::fanspeed_t::kMedium, @@ -275,17 +286,11 @@ const stdAc::fanspeed_t IrHvacFanSpeed[] PROGMEM = { stdAc::fanspeed_t::kAuto, uint32_t IrRemoteCmndIrHvacJson(void) { stdAc::state_t state, prev; - char parm_uc[12]; //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("IRHVAC: Received %s"), XdrvMailbox.data); - char dataBufUc[XdrvMailbox.data_len + 1]; - UpperCase(dataBufUc, XdrvMailbox.data); - RemoveSpace(dataBufUc); - if (strlen(dataBufUc) < 8) { return IE_INVALID_JSON; } - - DynamicJsonBuffer jsonBuf; - JsonObject &json = jsonBuf.parseObject(dataBufUc); - if (!json.success()) { return IE_INVALID_JSON; } + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { return IE_INVALID_JSON; } // from: https://github.com/crankyoldgit/IRremoteESP8266/blob/master/examples/CommonAcControl/CommonAcControl.ino state.protocol = decode_type_t::UNKNOWN; @@ -307,60 +312,44 @@ uint32_t IrRemoteCmndIrHvacJson(void) state.clean = false; // Turn off any Cleaning options if we can. state.clock = -1; // Don't set any current time if we can avoid it. - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_VENDOR)); - if (json.containsKey(parm_uc)) { state.protocol = strToDecodeType(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_PROTOCOL)); - if (json.containsKey(parm_uc)) { state.protocol = strToDecodeType(json[parm_uc]); } // also support 'protocol' + JsonParserToken val; + if (val = root[PSTR(D_JSON_IRHVAC_VENDOR)]) { state.protocol = strToDecodeType(val.getStr()); } + if (val = root[PSTR(D_JSON_IRHVAC_PROTOCOL)]) { state.protocol = strToDecodeType(val.getStr()); } if (decode_type_t::UNKNOWN == state.protocol) { return IE_UNSUPPORTED_HVAC; } if (!IRac::isProtocolSupported(state.protocol)) { return IE_UNSUPPORTED_HVAC; } // for fan speed, we also support 1-5 values - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_FANSPEED)); - if (json.containsKey(parm_uc)) { - uint32_t fan_speed = json[parm_uc]; + JsonParserToken tok_fan_speed = root[PSTR(D_JSON_IRHVAC_FANSPEED)]; + if (tok_fan_speed) { + uint32_t fan_speed = tok_fan_speed.getUInt(); if ((fan_speed >= 1) && (fan_speed <= 5)) { state.fanspeed = (stdAc::fanspeed_t) pgm_read_byte(&IrHvacFanSpeed[fan_speed]); } else { - state.fanspeed = IRac::strToFanspeed(json[parm_uc]); + state.fanspeed = IRac::strToFanspeed(tok_fan_speed.getStr()); } } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_MODEL)); - if (json.containsKey(parm_uc)) { state.model = IRac::strToModel(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_MODE)); - if (json.containsKey(parm_uc)) { state.mode = IRac::strToOpmode(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_SWINGV)); - if (json.containsKey(parm_uc)) { state.swingv = IRac::strToSwingV(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_SWINGH)); - if (json.containsKey(parm_uc)) { state.swingh = IRac::strToSwingH(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_TEMP)); - if (json.containsKey(parm_uc)) { state.degrees = json[parm_uc]; } + if (val = root[PSTR(D_JSON_IRHVAC_MODEL)]) { state.model = IRac::strToModel(val.getStr()); } + if (val = root[PSTR(D_JSON_IRHVAC_MODE)]) { state.mode = IRac::strToOpmode(val.getStr()); } + if (val = root[PSTR(D_JSON_IRHVAC_SWINGV)]) { state.swingv = IRac::strToSwingV(val.getStr()); } + if (val = root[PSTR(D_JSON_IRHVAC_SWINGH)]) { state.swingh = IRac::strToSwingH(val.getStr()); } + state.degrees = root.getFloat(PSTR(D_JSON_IRHVAC_TEMP), state.degrees); // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("model %d, mode %d, fanspeed %d, swingv %d, swingh %d"), // state.model, state.mode, state.fanspeed, state.swingv, state.swingh); // decode booleans - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_POWER)); - if (json.containsKey(parm_uc)) { state.power = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_CELSIUS)); - if (json.containsKey(parm_uc)) { state.celsius = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_LIGHT)); - if (json.containsKey(parm_uc)) { state.light = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_BEEP)); - if (json.containsKey(parm_uc)) { state.beep = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_ECONO)); - if (json.containsKey(parm_uc)) { state.econo = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_FILTER)); - if (json.containsKey(parm_uc)) { state.filter = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_TURBO)); - if (json.containsKey(parm_uc)) { state.turbo = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_QUIET)); - if (json.containsKey(parm_uc)) { state.quiet = IRac::strToBool(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_CLEAN)); - if (json.containsKey(parm_uc)) { state.clean = IRac::strToBool(json[parm_uc]); } + state.power = strToBool(root[PSTR(D_JSON_IRHVAC_POWER)], state.power); + state.celsius = strToBool(root[PSTR(D_JSON_IRHVAC_CELSIUS)], state.celsius); + state.light = strToBool(root[PSTR(D_JSON_IRHVAC_LIGHT)], state.light); + state.beep = strToBool(root[PSTR(D_JSON_IRHVAC_BEEP)], state.beep); + state.econo = strToBool(root[PSTR(D_JSON_IRHVAC_ECONO)], state.econo); + state.filter = strToBool(root[PSTR(D_JSON_IRHVAC_FILTER)], state.filter); + state.turbo = strToBool(root[PSTR(D_JSON_IRHVAC_TURBO)], state.turbo); + state.quiet = strToBool(root[PSTR(D_JSON_IRHVAC_QUIET)], state.quiet); + state.clean = strToBool(root[PSTR(D_JSON_IRHVAC_CLEAN)], state.clean); // optional timer and clock - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_SLEEP)); - if (json[parm_uc]) { state.sleep = json[parm_uc]; } + state.sleep = root.getInt(PSTR(D_JSON_IRHVAC_SLEEP), state.sleep); //if (json[D_JSON_IRHVAC_CLOCK]) { state.clock = json[D_JSON_IRHVAC_CLOCK]; } // not sure it's useful to support 'clock' IRac ac(Pin(GPIO_IRSEND)); @@ -387,40 +376,32 @@ void CmndIrHvac(void) uint32_t IrRemoteCmndIrSendJson(void) { - char parm_uc[12]; // used to convert JSON keys to uppercase - // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(3) + 40 = 96 // IRsend { "protocol": "RC5", "bits": 12, "data":"0xC86" } // IRsend { "protocol": "SAMSUNG", "bits": 32, "data": 551502015 } - char dataBufUc[XdrvMailbox.data_len + 1]; - UpperCase(dataBufUc, XdrvMailbox.data); - RemoveSpace(dataBufUc); - if (strlen(dataBufUc) < 8) { return IE_INVALID_JSON; } - - DynamicJsonBuffer jsonBuf; - JsonObject &json = jsonBuf.parseObject(dataBufUc); - if (!json.success()) { return IE_INVALID_JSON; } + RemoveSpace(XdrvMailbox.data); // TODO is this really needed? + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { return IE_INVALID_JSON; } // IRsend { "protocol": "SAMSUNG", "bits": 32, "data": 551502015 } // IRsend { "protocol": "NEC", "bits": 32, "data":"0x02FDFE80", "repeat": 2 } - decode_type_t protocol = decode_type_t::UNKNOWN; - uint16_t bits = 0; - uint64_t data; - uint8_t repeat = 0; + JsonParserToken value; - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_VENDOR)); - if (json.containsKey(parm_uc)) { protocol = strToDecodeType(json[parm_uc]); } - UpperCase_P(parm_uc, PSTR(D_JSON_IRHVAC_PROTOCOL)); - if (json.containsKey(parm_uc)) { protocol = strToDecodeType(json[parm_uc]); } // also support 'protocol' + decode_type_t protocol = decode_type_t::UNKNOWN; + value = root[PSTR(D_JSON_IRHVAC_VENDOR)]; + if (root) { protocol = strToDecodeType(value.getStr()); } + value = root[PSTR(D_JSON_IRHVAC_PROTOCOL)]; + if (root) { protocol = strToDecodeType(value.getStr()); } if (decode_type_t::UNKNOWN == protocol) { return IE_UNSUPPORTED_PROTOCOL; } - UpperCase_P(parm_uc, PSTR(D_JSON_IR_BITS)); - if (json.containsKey(parm_uc)) { bits = json[parm_uc]; } - UpperCase_P(parm_uc, PSTR(D_JSON_IR_REPEAT)); - if (json.containsKey(parm_uc)) { repeat = json[parm_uc]; } - UpperCase_P(parm_uc, PSTR(D_JSON_IR_DATALSB)); // accept LSB values - if (json.containsKey(parm_uc)) { data = reverseBitsInBytes64(strtoull(json[parm_uc], nullptr, 0)); } - UpperCase_P(parm_uc, PSTR(D_JSON_IR_DATA)); // or classical MSB (takes priority) - if (json.containsKey(parm_uc)) { data = strtoull(json[parm_uc], nullptr, 0); } + uint16_t bits = root.getUInt(PSTR(D_JSON_IR_BITS), 0); + uint16_t repeat = root.getUInt(PSTR(D_JSON_IR_REPEAT), 0); + + uint64_t data; + value = root[PSTR(D_JSON_IR_DATALSB)]; + if (root) { data = reverseBitsInBytes64(value.getULong()); } // accept LSB values + value = root[PSTR(D_JSON_IR_DATA)]; + if (value) { data = value.getULong(); } // or classical MSB (takes priority) if (0 == bits) { return IE_SYNTAX_IRSEND; } // check if the IRSend is greater than repeat, but can be overriden with JSON diff --git a/tasmota/xdrv_06_snfbridge.ino b/tasmota/xdrv_06_snfbridge.ino index af6f928bd..228564a45 100644 --- a/tasmota/xdrv_06_snfbridge.ino +++ b/tasmota/xdrv_06_snfbridge.ino @@ -127,7 +127,7 @@ ssize_t rf_decode_and_write(uint8_t *record, size_t size) uint16_t address = h->address_high * 0x100 + h->address_low; do { - err = c2_programming_init(); + err = c2_programming_init(C2_DEVID_EFM8BB1); err = c2_block_write(address, h->data, h->len); } while (err != C2_SUCCESS && retries--); } else if (h->record_type == IHX_RT_END_OF_FILE) { @@ -179,7 +179,7 @@ uint8_t rf_erase_flash(void) uint8_t err; for (uint32_t i = 0; i < 4; i++) { // HACK: Try multiple times as the command sometimes fails (unclear why) - err = c2_programming_init(); + err = c2_programming_init(C2_DEVID_EFM8BB1); if (err != C2_SUCCESS) { return 10; // Failed to init RF chip } diff --git a/tasmota/xdrv_07_domoticz.ino b/tasmota/xdrv_07_domoticz.ino index 2ea842491..0f4dafeb9 100644 --- a/tasmota/xdrv_07_domoticz.ino +++ b/tasmota/xdrv_07_domoticz.ino @@ -189,33 +189,13 @@ void DomoticzMqttSubscribe(void) { } } -/* - * ArduinoJSON Domoticz Switch entry used to calculate jsonBuf: JSON_OBJECT_SIZE(11) + 129 = 313 -{ - "Battery" : 255, - "RSSI" : 12, - "dtype" : "Light/Switch", - "id" : "000140E7", - "idx" : 159, - "name" : "Sonoff1", - "nvalue" : 1, - "stype" : "Switch", - "svalue1" : "0", - "switchType" : "Dimmer", - "unit" : 1 -} - * Fail on this one -{ - "LastUpdate" : "2018-10-02 20:39:45", - "Name" : "Sfeerverlichting", - "Status" : "Off", - "Timers" : "true", - "Type" : "Group", - "idx" : "2" -} -*/ - bool DomoticzMqttData(void) { +/* + XdrvMailbox.topic = topic; + XdrvMailbox.index = strlen(topic); + XdrvMailbox.data = (char*)data; + XdrvMailbox.data_len = data_len; +*/ domoticz_update_flag = true; if (strncasecmp_P(XdrvMailbox.topic, PSTR(DOMOTICZ_OUT_TOPIC), strlen(DOMOTICZ_OUT_TOPIC)) != 0) { @@ -226,19 +206,16 @@ bool DomoticzMqttData(void) { if (XdrvMailbox.data_len < 20) { return true; // No valid data } - StaticJsonBuffer<400> jsonBuf; - JsonObject& domoticz = jsonBuf.parseObject(XdrvMailbox.data); - if (!domoticz.success()) { + JsonParser parser(XdrvMailbox.data); + JsonParserObject domoticz = parser.getRootObject(); + if (!domoticz) { return true; // To much or invalid data } // if (strcmp_P(domoticz["dtype"],PSTR("Light/Switch"))) { // return true; // } - uint32_t idx = domoticz["idx"]; - int16_t nvalue = -1; - if (domoticz.containsKey("nvalue")) { - nvalue = domoticz["nvalue"]; - } + uint32_t idx = domoticz.getUInt(PSTR("idx"), 0); + int16_t nvalue = domoticz.getInt(PSTR("nvalue"), -1); AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_DOMOTICZ "idx %d, nvalue %d"), idx, nvalue); @@ -247,20 +224,19 @@ bool DomoticzMqttData(void) { uint8_t maxdev = (devices_present > MAX_DOMOTICZ_IDX) ? MAX_DOMOTICZ_IDX : devices_present; for (uint32_t i = 0; i < maxdev; i++) { if (idx == Settings.domoticz_relay_idx[i]) { - bool iscolordimmer = strcmp_P(domoticz["dtype"],PSTR("Color Switch")) == 0; - bool isShutter = strcmp_P(domoticz["dtype"],PSTR("Light/Switch")) == 0 & strncmp_P(domoticz["switchType"],PSTR("Blinds"), 6) == 0; + bool iscolordimmer = strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Color Switch")) == 0; + bool isShutter = strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Light/Switch")) == 0 & strncmp_P(domoticz.getStr(PSTR("switchType")),PSTR("Blinds"), 6) == 0; char stemp1[10]; snprintf_P(stemp1, sizeof(stemp1), PSTR("%d"), i +1); #ifdef USE_SONOFF_IFAN if (IsModuleIfan() && (1 == i)) { // Idx 2 is fanspeed - uint8_t svalue = 0; - if (domoticz.containsKey("svalue1")) { - svalue = domoticz["svalue1"]; - } else { - return true; // Invalid data + JsonParserToken svalue_tok = domoticz[PSTR("svalue1")]; + if (!svalue_tok) { + return true; } - svalue = (nvalue == 2) ? svalue / 10 : 0; + uint8_t svalue = svalue_tok.getUInt(); + svalue = (2 == nvalue) ? svalue / 10 : 0; if (GetFanspeed() == svalue) { return true; // Stop loop as already set } @@ -273,18 +249,10 @@ bool DomoticzMqttData(void) { } else #endif // USE_SONOFF_IFAN #ifdef USE_SHUTTER - if (isShutter) - { - if (domoticz.containsKey("nvalue")) { - nvalue = domoticz["nvalue"]; - } - - uint8_t position = 0; - if (domoticz.containsKey("svalue1")) { - position = domoticz["svalue1"]; - } + if (isShutter) { + uint8_t position = domoticz.getUInt(PSTR("svalue1"), 0); if (nvalue != 2) { - position = nvalue == 0 ? 0 : 100; + position = (0 == nvalue) ? 0 : 100; } snprintf_P(XdrvMailbox.topic, TOPSZ, PSTR("/" D_PRFX_SHUTTER D_CMND_SHUTTER_POSITION)); @@ -297,19 +265,16 @@ bool DomoticzMqttData(void) { #ifdef USE_LIGHT if (iscolordimmer && 10 == nvalue) { // Color_SetColor // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Set_a_light_to_a_certain_color_or_color_temperature - JsonObject& color = domoticz["Color"]; - uint16_t level = nvalue = domoticz["svalue1"]; - uint16_t r = color["r"]; r = r * level / 100; - uint16_t g = color["g"]; g = g * level / 100; - uint16_t b = color["b"]; b = b * level / 100; - uint16_t cw = color["cw"]; cw = cw * level / 100; - uint16_t ww = color["ww"]; ww = ww * level / 100; - uint16_t m = 0; - uint16_t t = 0; - if (color.containsKey("m")) { - m = color["m"]; - t = color["t"]; - } + JsonParserObject color = domoticz[PSTR("Color")].getObject(); + // JsonObject& color = domoticz["Color"]; + uint16_t level = nvalue = domoticz.getUInt(PSTR("svalue1"), 0); + uint16_t r = color.getUInt(PSTR("r"), 0) * level / 100; + uint16_t g = color.getUInt(PSTR("g"), 0) * level / 100; + uint16_t b = color.getUInt(PSTR("b"), 0) * level / 100; + uint16_t cw = color.getUInt(PSTR("cw"), 0) * level / 100; + uint16_t ww = color.getUInt(PSTR("ww"), 0) * level / 100; + uint16_t m = color.getUInt(PSTR("m"), 0); + uint16_t t = color.getUInt(PSTR("t"), 0); if (2 == m) { // White with color temperature. Valid fields: t snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_BACKLOG)); snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR(D_CMND_COLORTEMPERATURE " %d;" D_CMND_DIMMER " %d"), changeUIntScale(t, 0, 255, CT_MIN, CT_MAX), level); @@ -321,8 +286,8 @@ bool DomoticzMqttData(void) { } else if ((!iscolordimmer && 2 == nvalue) || // gswitch_sSetLevel (iscolordimmer && 15 == nvalue)) { // Color_SetBrightnessLevel - if (domoticz.containsKey("svalue1")) { - nvalue = domoticz["svalue1"]; + if (domoticz[PSTR("svalue1")]) { + nvalue = domoticz.getUInt(PSTR("svalue1"), 0); } else { return true; // Invalid data } diff --git a/tasmota/xdrv_09_timers.ino b/tasmota/xdrv_09_timers.ino index 1ecfb0b67..35503836f 100644 --- a/tasmota/xdrv_09_timers.ino +++ b/tasmota/xdrv_09_timers.ino @@ -341,32 +341,33 @@ void CmndTimer(void) #if defined(USE_RULES)==0 && defined(USE_SCRIPT)==0 if (devices_present) { #endif - char dataBufUc[XdrvMailbox.data_len + 1]; - UpperCase(dataBufUc, XdrvMailbox.data); - StaticJsonBuffer<256> jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(dataBufUc); - if (!root.success()) { + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { Response_P(PSTR("{\"" D_CMND_TIMER "%d\":\"" D_JSON_INVALID_JSON "\"}"), index); // JSON decode failed error = 1; } else { char parm_uc[10]; index--; - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_ARM))].success()) { - Settings.timer[index].arm = (root[parm_uc] != 0); + JsonParserToken val = root[PSTR(D_JSON_TIMER_ARM)]; + if (val) { + Settings.timer[index].arm = (val.getInt() != 0); } #ifdef USE_SUNRISE - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_MODE))].success()) { - Settings.timer[index].mode = (uint8_t)root[parm_uc] & 0x03; + val = root[PSTR(D_JSON_TIMER_MODE)]; + if (val) { + Settings.timer[index].mode = val.getUInt() & 0x03; } #endif - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_TIME))].success()) { + val = root[PSTR(D_JSON_TIMER_TIME)]; + if (val) { uint16_t itime = 0; int8_t value = 0; uint8_t sign = 0; char time_str[10]; - strlcpy(time_str, root[parm_uc], sizeof(time_str)); + strlcpy(time_str, val.getStr(), sizeof(time_str)); const char *substr = strtok(time_str, ":"); if (substr != nullptr) { if (strchr(substr, '-')) { @@ -387,14 +388,16 @@ void CmndTimer(void) } Settings.timer[index].time = itime; } - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_WINDOW))].success()) { - Settings.timer[index].window = (uint8_t)root[parm_uc] & 0x0F; + val = root[PSTR(D_JSON_TIMER_WINDOW)]; + if (val) { + Settings.timer[index].window = val.getUInt() & 0x0F; TimerSetRandomWindow(index); } - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_DAYS))].success()) { + val = root[PSTR(D_JSON_TIMER_DAYS)]; + if (val) { // SMTWTFS = 1234567 = 0011001 = 00TW00S = --TW--S Settings.timer[index].days = 0; - const char *tday = root[parm_uc]; + const char *tday = val.getStr(); uint8_t i = 0; char ch = *tday++; while ((ch != '\0') && (i < 7)) { @@ -404,15 +407,18 @@ void CmndTimer(void) ch = *tday++; } } - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_REPEAT))].success()) { - Settings.timer[index].repeat = (root[parm_uc] != 0); + val = root[PSTR(D_JSON_TIMER_REPEAT)]; + if (val) { + Settings.timer[index].repeat = (val.getUInt() != 0); } - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_OUTPUT))].success()) { - uint8_t device = ((uint8_t)root[parm_uc] -1) & 0x0F; + val = root[PSTR(D_JSON_TIMER_OUTPUT)]; + if (val) { + uint8_t device = (val.getUInt() -1) & 0x0F; Settings.timer[index].device = (device < devices_present) ? device : 0; } - if (root[UpperCase_P(parm_uc, PSTR(D_JSON_TIMER_ACTION))].success()) { - uint8_t action = (uint8_t)root[parm_uc] & 0x03; + val = root[PSTR(D_JSON_TIMER_ACTION)]; + if (val) { + uint8_t action = val.getUInt() & 0x03; Settings.timer[index].power = (devices_present) ? action : 3; // If no devices than only allow rules } diff --git a/tasmota/xdrv_10_rules.ino b/tasmota/xdrv_10_rules.ino index c377c4a91..2938e2d41 100644 --- a/tasmota/xdrv_10_rules.ino +++ b/tasmota/xdrv_10_rules.ino @@ -497,22 +497,19 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule) rule_name = rule_name.substring(0, pos); // "SUBTYPE1#CURRENT" } -// StaticJsonBuffer<1280> jsonBuf; // Was 1024 until 20200811 - DynamicJsonBuffer jsonBuf; // Was static until 20200812 - JsonObject &root = jsonBuf.parseObject(event); - if (!root.success()) { + String buf = event; // copy the string into a new buffer that will be modified + JsonParser parser((char*)buf.c_str()); + JsonParserObject obj = parser.getRootObject(); + if (!obj) { AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Event too long (%d)"), event.length()); - return false; - } // No valid JSON data - JsonObject *obj = &root; + return false; // No valid JSON data + } String subtype; uint32_t i = 0; while ((pos = rule_name.indexOf("#")) > 0) { // "SUBTYPE1#SUBTYPE2#CURRENT" subtype = rule_name.substring(0, pos); - const JsonVariant & val = GetCaseInsensitive(*obj, subtype.c_str()); - if (nullptr == &val) { return false; } // not found - obj = &(val.as()); - if (!obj->success()) { return false; } // not a JsonObject + obj = obj[subtype.c_str()].getObject(); + if (!obj) { return false; } // not found rule_name = rule_name.substring(pos +1); if (i++ > 10) { return false; } // Abandon possible loop @@ -520,13 +517,17 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule) yield(); } - const JsonVariant & val = GetCaseInsensitive(*obj, rule_name.c_str()); - if (nullptr == &val) { return false; } // last level not found + JsonParserToken val = obj[rule_name.c_str()]; + if (!val) { return false; } // last level not found const char* str_value; if (rule_name_idx) { - str_value = (*obj)[rule_name][rule_name_idx -1]; // "CURRENT[1]" + if (val.isArray()) { + str_value = (val.getArray())[rule_name_idx -1].getStr(); + } else { + str_value = val.getStr(); + } } else { - str_value = (*obj)[rule_name]; // "CURRENT" + str_value = val.getStr(); // "CURRENT" } //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Name %s, Value |%s|, TrigCnt %d, TrigSt %d, Source %s, Json %s"), @@ -719,9 +720,11 @@ bool RuleSetProcess(uint8_t rule_set, String &event_saved) RulesVarReplace(commands, F("%UPTIME%"), String(MinutesUptime())); RulesVarReplace(commands, F("%TIMESTAMP%"), GetDateAndTime(DT_LOCAL)); RulesVarReplace(commands, F("%TOPIC%"), mqtt_topic); - char unique_id[7]; - snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X"), ESP_getChipId()); - RulesVarReplace(commands, F("%DEVICEID%"), unique_id); + snprintf_P(stemp, sizeof(stemp), PSTR("%06X"), ESP_getChipId()); + RulesVarReplace(commands, F("%DEVICEID%"), stemp); + String mac_address = WiFi.macAddress(); + mac_address.replace(":", ""); + RulesVarReplace(commands, F("%MACADDR%"), mac_address); #if defined(USE_TIMERS) && defined(USE_SUNRISE) RulesVarReplace(commands, F("%SUNRISE%"), String(SunMinutes(0))); RulesVarReplace(commands, F("%SUNSET%"), String(SunMinutes(1))); @@ -1030,20 +1033,27 @@ bool RulesMqttData(void) if (event_item.Key.length() == 0) { //If did not specify Key value = sData; } else { //If specified Key, need to parse Key/Value from JSON data - StaticJsonBuffer<500> jsonBuf; - JsonObject& jsonData = jsonBuf.parseObject(sData); + JsonParser parser((char*)sData.c_str()); + JsonParserObject jsonData = parser.getRootObject(); + String key1 = event_item.Key; String key2; - if (!jsonData.success()) break; //Failed to parse JSON data, ignore this message. + if (!jsonData) break; //Failed to parse JSON data, ignore this message. int dot; if ((dot = key1.indexOf('.')) > 0) { key2 = key1.substring(dot+1); key1 = key1.substring(0, dot); - if (!jsonData[key1][key2].success()) break; //Failed to get the key/value, ignore this message. - value = (const char *)jsonData[key1][key2]; + JsonParserToken value_tok = jsonData[key1.c_str()].getObject()[key2.c_str()]; + if (!value_tok) break; //Failed to get the key/value, ignore this message. + value = value_tok.getStr(); + // if (!jsonData[key1][key2].success()) break; //Failed to get the key/value, ignore this message. + // value = (const char *)jsonData[key1][key2]; } else { - if (!jsonData[key1].success()) break; - value = (const char *)jsonData[key1]; + JsonParserToken value_tok = jsonData[key1.c_str()]; + if (!value_tok) break; //Failed to get the key/value, ignore this message. + value = value_tok.getStr(); + // if (!jsonData[key1].success()) break; + // value = (const char *)jsonData[key1]; } } value.trim(); diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index cd5f986d2..8f5181914 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -65,6 +65,7 @@ keywords if then else endif, or, and are better readable for beginners (others m #define SCRIPT_MAXPERM (PMEM_SIZE)-4/sizeof(float) #define MAX_SCRIPT_SIZE MAX_RULE_SIZE*MAX_RULE_SETS +#define MAX_SARRAY_NUM 32 uint32_t EncodeLightId(uint8_t relay_id); uint32_t DecodeLightId(uint32_t hue_id); @@ -79,6 +80,7 @@ uint32_t DecodeLightId(uint32_t hue_id); #undef LITTLEFS_SCRIPT_SIZE #undef EEP_SCRIPT_SIZE #undef USE_SCRIPT_COMPRESSION + #if USE_SCRIPT_FATFS==-1 #ifdef ESP32 @@ -136,19 +138,19 @@ Ticker Script_ticker4; void Script_ticker1_end(void) { Script_ticker1.detach(); - Run_Scripter(">ti1", 4,0); + Run_Scripter(">ti1", 4, 0); } void Script_ticker2_end(void) { Script_ticker2.detach(); - Run_Scripter(">ti2", 4,0); + Run_Scripter(">ti2", 4, 0); } void Script_ticker3_end(void) { Script_ticker3.detach(); - Run_Scripter(">ti3", 4,0); + Run_Scripter(">ti3", 4, 0); } void Script_ticker4_end(void) { Script_ticker4.detach(); - Run_Scripter(">ti4", 4,0); + Run_Scripter(">ti4", 4, 0); } #endif @@ -169,7 +171,7 @@ FS *fsp; #ifdef LITTLEFS_SCRIPT_SIZE -void SaveFile(const char *name,const uint8_t *buf,uint32_t len) { +void SaveFile(const char *name, const uint8_t *buf, uint32_t len) { File file = fsp->open(name, "w"); if (!file) return; file.write(buf, len); @@ -179,7 +181,7 @@ void SaveFile(const char *name,const uint8_t *buf,uint32_t len) { uint8_t fs_mounted=0; -void LoadFile(const char *name,uint8_t *buf,uint32_t len) { +void LoadFile(const char *name, uint8_t *buf, uint32_t len) { if (!fs_mounted) { #ifdef ESP32 if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) { @@ -214,7 +216,7 @@ FS *fsp; #else SDClass *fsp; #endif -#endif +#endif //USE_SCRIPT_FATFS #ifndef ESP32 // esp8266 @@ -250,9 +252,9 @@ SDClass *fsp; #define FAT_SCRIPT_NAME "script.txt" #endif -#if USE_STANDARD_SPI_LIBRARY==0 -#warning ("FATFS standard spi should be used"); -#endif +//#if USE_STANDARD_SPI_LIBRARY==0 +//#warning ("FATFS standard spi should be used"); +//#endif #endif // USE_SCRIPT_FATFS @@ -377,7 +379,11 @@ struct SCRIPT_MEM { struct T_INDEX *type; // type and index pointer struct M_FILT *mfilt; char *glob_vnp; // var name pointer +#ifdef SCRIPT_LARGE_VNBUFF + uint16_t *vnp_offset; +#else uint8_t *vnp_offset; +#endif char *glob_snp; // string vars pointer char *scriptptr; char *section_ptr; @@ -390,21 +396,23 @@ struct SCRIPT_MEM { void *script_mem; uint16_t script_mem_size; uint8_t script_dprec; + uint8_t script_lzero; uint8_t var_not_found; uint8_t glob_error; uint8_t max_ssize; uint8_t script_loglevel; uint8_t flags; - uint8_t si_num; - uint8_t siro_num; - char *last_index_string; + uint8_t si_num[3]; + uint8_t siro_num[3]; + uint8_t sind_num; + char *last_index_string[3]; #ifdef USE_SCRIPT_FATFS File files[SFS_MAX]; FILE_FLAGS file_flags[SFS_MAX]; uint8_t script_sd_found; char flink[2][14]; -#endif +#endif //USE_SCRIPT_FATFS #ifdef USE_SCRIPT_GLOBVARS UDP_FLAGS udp_flags; #endif @@ -423,8 +431,8 @@ char * IPAddressToString(const IPAddress& ip_address) { sprintf_P(ipbuffer, PSTR("%u.%u.%u.%u"), ip_address[0], ip_address[1], ip_address[2], ip_address[3]); return ipbuffer; } -#endif -#endif +#endif //USE_DEVICE_GROUPS +#endif //USE_SCRIPT_GLOBVARS int16_t last_findex; int16_t last_sindex; @@ -433,13 +441,37 @@ uint8_t fast_script=0; uint8_t glob_script=0; uint32_t script_lastmillis; +void flt2char(float num, char *nbuff) { + dtostrfd(num, glob_script_mem.script_dprec, nbuff); +} +// convert float to char with leading zeros +void f2char(float num, uint32_t dprec, uint32_t lzeros, char *nbuff) { + dtostrfd(num, dprec, nbuff); + if (lzeros>1) { + // check leading zeros + uint32_t nd = num; + nd/=10; + nd+=1; + if (lzeros>nd) { + // insert zeros + char cpbuf[24]; + char *cp = cpbuf; + for (uint32_t cnt = 0; cnt < lzeros - nd; cnt++) { + *cp++='0'; + } + *cp=0; + strcat(cpbuf,nbuff); + strcpy(nbuff,cpbuf); + } + } +} #ifdef USE_BUTTON_EVENT int8_t script_button[MAX_KEYS]; -#endif +#endif //USE_BUTTON_EVENT -char *GetNumericResult(char *lp,uint8_t lastop,float *fp,JsonObject *jo); -char *GetStringResult(char *lp,uint8_t lastop,char *cp,JsonObject *jo); +char *GetNumericArgument(char *lp,uint8_t lastop,float *fp, JsonParserObject *jo); +char *GetStringArgument(char *lp,uint8_t lastop,char *cp, JsonParserObject *jo); char *ForceStringVar(char *lp,char *dstr); void send_download(void); uint8_t reject(char *name); @@ -447,75 +479,80 @@ uint8_t reject(char *name); void ScriptEverySecond(void) { if (bitRead(Settings.rule_enabled, 0)) { - struct T_INDEX *vtp=glob_script_mem.type; - float delta=(millis()-script_lastmillis)/1000.0; - script_lastmillis=millis(); + struct T_INDEX *vtp = glob_script_mem.type; + float delta = (millis() - script_lastmillis) / 1000.0; + script_lastmillis = millis(); for (uint8_t count=0; count0) { // decrement - *fp-=delta; - if (*fp<0) *fp=0; + *fp -= delta; + if (*fp<0) *fp = 0; } } if (vtp[count].bits.is_autoinc) { // increments timers - float *fp=&glob_script_mem.fvars[vtp[count].index]; + float *fp = &glob_script_mem.fvars[vtp[count].index]; if (*fp>=0) { - *fp+=delta; + *fp += delta; } } } - Run_Scripter(">S",2,0); + Run_Scripter(">S", 2, 0); } } void RulesTeleperiod(void) { - if (bitRead(Settings.rule_enabled, 0) && mqtt_data[0]) Run_Scripter(">T",2, mqtt_data); + if (bitRead(Settings.rule_enabled, 0) && mqtt_data[0]) Run_Scripter(">T", 2, mqtt_data); } // EEPROM MACROS // i2c eeprom -#define EEP_WRITE(A,B,C) eeprom_writeBytes(A,B,(uint8_t*)C); -#define EEP_READ(A,B,C) eeprom_readBytes(A,B,(uint8_t*)C); +#define EEP_WRITE(A,B,C) eeprom_writeBytes(A, B, (uint8_t*)C); +#define EEP_READ(A,B,C) eeprom_readBytes(A, B, (uint8_t*)C); #define SCRIPT_SKIP_SPACES while (*lp==' ' || *lp=='\t') lp++; #define SCRIPT_SKIP_EOL while (*lp==SCRIPT_EOL) lp++; -float *Get_MFAddr(uint8_t index,uint16_t *len,uint16_t *ipos); +float *Get_MFAddr(uint8_t index, uint16_t *len, uint16_t *ipos); // allocates all variables and presets them int16_t Init_Scripter(void) { char *script; - script=glob_script_mem.script_ram; + script = glob_script_mem.script_ram; // scan lines for >DEF - uint16_t lines=0,nvars=0,svars=0,vars=0; - char *lp=script; + uint16_t lines = 0; + uint16_t nvars = 0; + uint16_t svars = 0; + uint16_t vars = 0; + char *lp = script; char vnames[MAXVARS*10]; - char *vnames_p=vnames; + char *vnames_p = vnames; char *vnp[MAXVARS]; - char **vnp_p=vnp; + char **vnp_p = vnp; char strings[MAXSVARS*SCRIPT_MAXSSIZE]; struct M_FILT mfilt[MAXFILT]; - char *strings_p=strings; + char *strings_p = strings; char *snp[MAXSVARS]; - char **snp_p=snp; - uint8_t numperm=0,numflt=0,count; + char **snp_p = snp; + uint8_t numperm = 0; + uint8_t numflt = 0; + uint16_t count; - glob_script_mem.max_ssize=SCRIPT_SVARSIZE; - glob_script_mem.scriptptr=0; + glob_script_mem.max_ssize = SCRIPT_SVARSIZE; + glob_script_mem.scriptptr = 0; if (!*script) return -999; float fvalues[MAXVARS]; struct T_INDEX vtypes[MAXVARS]; - char init=0; + char init = 0; while (1) { // check line // skip leading spaces @@ -527,84 +564,84 @@ char *script; if (init) { // init section if (*lp=='>' || !*lp) { - init=0; + init = 0; break; } - char *op=strchr(lp,'='); + char *op = strchr(lp, '='); if (op) { - vtypes[vars].bits.data=0; + vtypes[vars].bits.data = 0; // found variable definition if (*lp=='p' && *(lp+1)==':') { - lp+=2; + lp += 2; if (numpermMAXFILT) { return -6; } } else { - vtypes[vars].bits.is_filter=0; + vtypes[vars].bits.is_filter = 0; } - *vnp_p++=vnames_p; + *vnp_p++ = vnames_p; while (lpMAXNVARS) { return -1; @@ -616,27 +653,27 @@ char *script; while (*op==' ') op++; if (isdigit(*op)) { // lenght define follows - uint16_t flen=atoi(op); + uint16_t flen = atoi(op); if (flen>MAX_ARRAY_SIZE) { // limit array size - flen=MAX_ARRAY_SIZE; + flen = MAX_ARRAY_SIZE; } - mfilt[numflt-1].numvals&=OR_FILT_MASK; - mfilt[numflt-1].numvals|=flen&AND_FILT_MASK; + mfilt[numflt-1].numvals &= OR_FILT_MASK; + mfilt[numflt-1].numvals |= flen&AND_FILT_MASK; } } } else { // string vars op++; - *snp_p++=strings_p; + *snp_p ++= strings_p; while (*op!='\"') { if (*op==SCRIPT_EOL) break; - *strings_p++=*op++; + *strings_p++ = *op++; } - *strings_p++=0; - vtypes[vars].bits.is_string=1; - vtypes[vars].index=svars; + *strings_p++ = 0; + vtypes[vars].bits.is_string = 1; + vtypes[vars].index = svars; svars++; if (svars>MAXSVARS) { return -2; @@ -648,15 +685,15 @@ char *script; } } } else { - if (!strncmp(lp,">D",2)) { - lp+=2; + if (!strncmp(lp, ">D", 2)) { + lp += 2; SCRIPT_SKIP_SPACES if (isdigit(*lp)) { - uint8_t ssize=atoi(lp)+1; + uint8_t ssize = atoi(lp)+1; if (ssize<10 || ssize>SCRIPT_MAXSSIZE) ssize=SCRIPT_MAXSSIZE; - glob_script_mem.max_ssize=ssize; + glob_script_mem.max_ssize = ssize; } - init=1; + init = 1; } } // next line @@ -666,85 +703,105 @@ char *script; lp++; } - uint16_t fsize=0; + uint16_t fsize = 0; for (count=0; count255) { + if (index > MAXVNSIZ) { free(glob_script_mem.script_mem); return -5; } @@ -753,71 +810,72 @@ char *script; AddLog_P2(LOG_LEVEL_INFO, PSTR("Script: nv=%d, tv=%d, vns=%d, ram=%d"), nvars, svars, index, glob_script_mem.script_mem_size); // copy string variables - char *cp1=glob_script_mem.glob_snp; - char *sp=strings; - for (count=0; countnumvals=mfilt[count].numvals; - mp+=sizeof(struct M_FILT)+((mfilt[count].numvals&AND_FILT_MASK)-1)*sizeof(float); + uint8_t *mp = (uint8_t*)glob_script_mem.mfilt; + for (count = 0; countnumvals = mfilt[count].numvals; + mp += sizeof(struct M_FILT) + ((mfilt[count].numvals & AND_FILT_MASK) - 1) * sizeof(float); } - glob_script_mem.numvars=vars; - glob_script_mem.script_dprec=SCRIPT_FLOAT_PRECISION; - glob_script_mem.script_loglevel=LOG_LEVEL_INFO; + glob_script_mem.numvars = vars; + glob_script_mem.script_dprec = SCRIPT_FLOAT_PRECISION; + glob_script_mem.script_lzero = 0; + glob_script_mem.script_loglevel = LOG_LEVEL_INFO; #if SCRIPT_DEBUG>2 - struct T_INDEX *dvtp=glob_script_mem.type; - for (uint8_t count=0; count=0 // user sd card - fsp=&SD; + fsp = &SD; if (SD.begin(USE_SCRIPT_FATFS)) { #else // use flash file @@ -836,53 +894,115 @@ char *script; if (FFat.begin(true)) { #else if (fsp->begin()) { -#endif +#endif // ESP32 #endif // USE_SCRIPT_FATFS>=0 - glob_script_mem.script_sd_found=1; + glob_script_mem.script_sd_found = 1; } else { - glob_script_mem.script_sd_found=0; + glob_script_mem.script_sd_found = 0; } } - for (uint8_t cnt=0;cnt0 ClaimSerial(); SetSerialBaudrate(9600); -#endif +#endif //SCRIPT_DEBUG // store start of actual program here - glob_script_mem.scriptptr=lp-1; - glob_script_mem.scriptptr_bu=glob_script_mem.scriptptr; + glob_script_mem.scriptptr = lp - 1; + glob_script_mem.scriptptr_bu = glob_script_mem.scriptptr; #ifdef USE_SCRIPT_GLOBVARS if (glob_script_mem.udp_flags.udp_used) { Script_Init_UDP(); - glob_script=Run_Scripter(">G",-2,0); + glob_script = Run_Scripter(">G", -2, 0); } -#endif +#endif //USE_SCRIPT_GLOBVARS return 0; } +#ifdef USE_SCRIPT_FATFS +uint32_t get_fsinfo(uint32_t sel) { +uint32_t result = 0; +#ifdef ESP32 +#if USE_SCRIPT_FATFS >=0 + if (sel == 0) { + result = SD.totalBytes()/1000; + } else if (sel == 1) { + result = (SD.totalBytes() - SD.usedBytes())/1000; + } +#else + if (sel == 0) { + result = FFat.totalBytes()/1000; + } else if (sel == 1) { + result = FFat.freeBytes()/1000; + } +#endif // USE_SCRIPT_FATFS>=0 +#else + // ESP8266 + FSInfo64 fsinfo; + fsp->info64(fsinfo); + if (sel == 0) { + result = fsinfo.totalBytes/1000; + } else if (sel == 1) { + result = (fsinfo.totalBytes - fsinfo.usedBytes)/1000; + } +#endif // ESP32 + return result; +} + +// format number with thousand marker +void form1000(uint32_t number, char *dp, char sc) { + char str[32]; + sprintf(str, "%d", number); + char *sp = str; + uint32_t inum = strlen(sp)/3; + uint32_t fnum = strlen(sp)%3; + if (!fnum) inum--; + for (uint32_t count=0; count<=inum; count++) { + if (fnum){ + memcpy(dp,sp,fnum); + dp+=fnum; + sp+=fnum; + fnum=0; + } else { + memcpy(dp,sp,3); + dp+=3; + sp+=3; + } + if (count!=inum) { + *dp++=sc; + } + } + *dp=0; +} + +#endif //USE_SCRIPT_FATFS + #ifdef USE_SCRIPT_GLOBVARS #define SCRIPT_UDP_BUFFER_SIZE 128 #define SCRIPT_UDP_PORT 1999 IPAddress script_udp_remote_ip; void Script_Stop_UDP(void) { - Script_PortUdp.flush(); - Script_PortUdp.stop(); - glob_script_mem.udp_flags.udp_connected = 0; + if (!glob_script_mem.udp_flags.udp_used) return; + if (glob_script_mem.udp_flags.udp_connected) { + Script_PortUdp.flush(); + Script_PortUdp.stop(); + glob_script_mem.udp_flags.udp_connected = 0; + } } void Script_Init_UDP() { if (global_state.network_down) return; + if (!glob_script_mem.udp_flags.udp_used) return; if (glob_script_mem.udp_flags.udp_connected) return; if (Script_PortUdp.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), SCRIPT_UDP_PORT)) { @@ -893,48 +1013,49 @@ void Script_Init_UDP() { glob_script_mem.udp_flags.udp_connected = 0; } } + void Script_PollUdp(void) { if (global_state.network_down) return; if (!glob_script_mem.udp_flags.udp_used) return; if (glob_script_mem.udp_flags.udp_connected ) { while (Script_PortUdp.parsePacket()) { char packet_buffer[SCRIPT_UDP_BUFFER_SIZE]; - int32_t len = Script_PortUdp.read(packet_buffer, SCRIPT_UDP_BUFFER_SIZE -1); + int32_t len = Script_PortUdp.read(packet_buffer, SCRIPT_UDP_BUFFER_SIZE - 1); packet_buffer[len] = 0; script_udp_remote_ip = Script_PortUdp.remoteIP(); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("UDP: Packet %s - %d - %s"), packet_buffer, len, script_udp_remote_ip.toString().c_str()); char *lp=packet_buffer; - if (!strncmp(lp,"=>",2)) { - lp+=2; - char *cp=strchr(lp,'='); + if (!strncmp(lp,"=>", 2)) { + lp += 2; + char *cp=strchr(lp, '='); if (cp) { char vnam[32]; - for (uint32_t count=0; countG",2,0); + Run_Scripter(">G", 2, 0); } } } @@ -950,167 +1071,186 @@ void script_udp_sendvar(char *vname,float *fp,char *sp) { if (!glob_script_mem.udp_flags.udp_used) return; if (!glob_script_mem.udp_flags.udp_connected) return; - char sbuf[SCRIPT_MAXSSIZE+4]; - strcpy(sbuf,"=>"); - strcat(sbuf,vname); - strcat(sbuf,"="); + char sbuf[SCRIPT_MAXSSIZE + 4]; + strcpy(sbuf, "=>"); + strcat(sbuf, vname); + strcat(sbuf, "="); if (fp) { char flstr[16]; - dtostrfd(*fp,8,flstr); - strcat(sbuf,flstr); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("num var updated - %s"),sbuf); + dtostrfd(*fp, 8, flstr); + strcat(sbuf, flstr); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("num var updated - %s"), sbuf); } else { - strcat(sbuf,sp); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("string var updated - %s"),sbuf); + strcat(sbuf, sp); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("string var updated - %s"), sbuf); } - Script_PortUdp.beginPacket(IPAddress(239,255,255,250), SCRIPT_UDP_PORT); + Script_PortUdp.beginPacket(IPAddress(239, 255, 255, 250), SCRIPT_UDP_PORT); // Udp.print(String("RET UC: ") + String(recv_Packet)); - Script_PortUdp.write((const uint8_t*)sbuf,strlen(sbuf)); + Script_PortUdp.write((const uint8_t*)sbuf, strlen(sbuf)); Script_PortUdp.endPacket(); } -#endif +#endif //USE_SCRIPT_GLOBVARS #ifdef USE_LIGHT #ifdef USE_WS2812 void ws2812_set_array(float *array ,uint32_t len, uint32_t offset) { Ws2812ForceSuspend(); - for (uint32_t cnt=0;cntSettings.light_pixels) break; - uint32_t col=array[cnt]; - Ws2812SetColor(index+1,col>>16,col>>8,col,0); + uint32_t col = array[cnt]; + Ws2812SetColor(index + 1, col>>16, col>>8, col, 0); } Ws2812ForceUpdate(); } -#endif -#endif +#endif //USE_WS2812 +#endif //USE_LIGHT -float median_array(float *array,uint16_t len) { +float median_array(float *array, uint16_t len) { uint8_t ind[len]; - uint8_t mind=0,index=0,flg; - float min=FLT_MAX; + uint8_t mind = 0; + uint8_t index = 0; + uint8_t flg; + float min = FLT_MAX; - for (uint8_t hcnt=0; hcntnumvals&AND_FILT_MASK; - if (ipos) *ipos=mflp->index; + *len = mflp->numvals & AND_FILT_MASK; + if (ipos) *ipos = mflp->index; return mflp->rbuff; } - mp+=sizeof(struct M_FILT)+((mflp->numvals&AND_FILT_MASK)-1)*sizeof(float); + mp += sizeof(struct M_FILT) + ((mflp->numvals & AND_FILT_MASK) - 1) * sizeof(float); } return 0; } +char *isvar(char *lp, uint8_t *vtype, struct T_INDEX *tind, float *fp, char *sp, JsonParserObject *jo); -float Get_MFVal(uint8_t index,int16_t bind) { - uint8_t *mp=(uint8_t*)glob_script_mem.mfilt; - for (uint8_t count=0; countnumvals&AND_FILT_MASK; + uint16_t maxind = mflp->numvals & AND_FILT_MASK; if (!bind) { return mflp->index; } if (bind<0) { return maxind; } - if (bind<1 || bind>maxind) bind=maxind; - return mflp->rbuff[bind-1]; + if (bind<1 || bind>maxind) bind = maxind; + return mflp->rbuff[bind - 1]; } - mp+=sizeof(struct M_FILT)+((mflp->numvals&AND_FILT_MASK)-1)*sizeof(float); + mp += sizeof(struct M_FILT) + ((mflp->numvals & AND_FILT_MASK) - 1) * sizeof(float); } return 0; } -void Set_MFVal(uint8_t index,uint16_t bind,float val) { - uint8_t *mp=(uint8_t*)glob_script_mem.mfilt; - for (uint8_t count=0; countnumvals&AND_FILT_MASK; + uint16_t maxind = mflp->numvals & AND_FILT_MASK; if (!bind) { - mflp->index=val; + mflp->index = val; } else { - if (bind<1 || bind>maxind) bind=maxind; - mflp->rbuff[bind-1]=val; + if (bind<1 || bind>maxind) bind = maxind; + mflp->rbuff[bind-1] = val; } return; } - mp+=sizeof(struct M_FILT)+((mflp->numvals&AND_FILT_MASK)-1)*sizeof(float); + mp += sizeof(struct M_FILT) + ((mflp->numvals & AND_FILT_MASK) - 1) * sizeof(float); } } float Get_MFilter(uint8_t index) { - uint8_t *mp=(uint8_t*)glob_script_mem.mfilt; - for (uint8_t count=0; countnumvals&OR_FILT_MASK) { + if (mflp->numvals & OR_FILT_MASK) { // moving average - return mflp->maccu/(mflp->numvals&AND_FILT_MASK); + return mflp->maccu / (mflp->numvals & AND_FILT_MASK); } else { // median, sort array indices - return median_array(mflp->rbuff,mflp->numvals); + return median_array(mflp->rbuff, mflp->numvals); } } - mp+=sizeof(struct M_FILT)+((mflp->numvals&AND_FILT_MASK)-1)*sizeof(float); + mp += sizeof(struct M_FILT) + ((mflp->numvals & AND_FILT_MASK) - 1) * sizeof(float); } return 0; } void Set_MFilter(uint8_t index, float invar) { - uint8_t *mp=(uint8_t*)glob_script_mem.mfilt; - for (uint8_t count=0; countnumvals&OR_FILT_MASK) { + if (mflp->numvals & OR_FILT_MASK) { // moving average - mflp->maccu-=mflp->rbuff[mflp->index]; - mflp->maccu+=invar; - mflp->rbuff[mflp->index]=invar; + mflp->maccu -= mflp->rbuff[mflp->index]; + mflp->maccu += invar; + mflp->rbuff[mflp->index] = invar; mflp->index++; - if (mflp->index>=(mflp->numvals&AND_FILT_MASK)) mflp->index=0; + if (mflp->index>=(mflp->numvals&AND_FILT_MASK)) mflp->index = 0; } else { // median - mflp->rbuff[mflp->index]=invar; + mflp->rbuff[mflp->index] = invar; mflp->index++; - if (mflp->index>=mflp->numvals) mflp->index=0; + if (mflp->index>=mflp->numvals) mflp->index = 0; } break; } - mp+=sizeof(struct M_FILT)+((mflp->numvals&AND_FILT_MASK)-1)*sizeof(float); + mp += sizeof(struct M_FILT) + ((mflp->numvals & AND_FILT_MASK) - 1) * sizeof(float); } } @@ -1124,17 +1264,16 @@ int8_t index; float DoMedian5(uint8_t index, float in) { - if (index>=MEDIAN_FILTER_NUM) index=0; + if (index>=MEDIAN_FILTER_NUM) index = 0; - struct MEDIAN_FILTER* mf=&script_mf[index]; - mf->buffer[mf->index]=in; + struct MEDIAN_FILTER* mf = &script_mf[index]; + mf->buffer[mf->index] = in; mf->index++; - if (mf->index>=MEDIAN_SIZE) mf->index=0; - return median_array(mf->buffer,MEDIAN_SIZE); + if (mf->index>=MEDIAN_SIZE) mf->index = 0; + return median_array(mf->buffer, MEDIAN_SIZE); } #ifdef USE_LIGHT -//#ifdef USE_WS2812 uint32_t HSVToRGB(uint16_t hue, uint8_t saturation, uint8_t value) { float r = 0, g = 0, b = 0; struct HSV { @@ -1143,9 +1282,9 @@ struct HSV { float V; } hsv; -hsv.H=hue; -hsv.S=(float)saturation/100.0; -hsv.V=(float)value/100.0; +hsv.H = hue; +hsv.S = (float)saturation / 100.0; +hsv.V = (float)value / 100.0; if (hsv.S == 0) { r = hsv.V; @@ -1208,16 +1347,16 @@ if (hsv.S == 0) { } - uint8_t ir,ig,ib; - ir=r*255; - ig=g*255; - ib=b*255; + uint8_t ir, ig, ib; + ir = r * 255; + ig = g * 255; + ib = b * 255; - uint32_t rgb=(ir<<16)|(ig<<8)|ib; + uint32_t rgb = (ir<<16) | (ig<<8) | ib; return rgb; } -#endif -//#endif +#endif //USE_LIGHT + #ifdef USE_ANGLE_FUNC uint32_t pulse_time_hl; @@ -1249,8 +1388,8 @@ uint32_t MeasurePulseTime(int32_t in) { if (in >= 0) { // define pin; pt_pin = in; - pinMode(pt_pin&0x3f,INPUT_PULLUP); - attachInterrupt(pt_pin&0x3f, MP_Timer, CHANGE); + pinMode(pt_pin & 0x3f, INPUT_PULLUP); + attachInterrupt(pt_pin & 0x3f, MP_Timer, CHANGE); pulse_ltime_lh = millis(); pulse_ltime_hl = millis(); return 0; @@ -1269,26 +1408,26 @@ uint32_t MeasurePulseTime(int32_t in) { #ifdef USE_SCRIPT_GLOBVARS uint32_t match_vars(char *dvnam, float **fp, char **sp, uint32_t *ind) { - uint16_t olen=strlen(dvnam); - struct T_INDEX *vtp=glob_script_mem.type; - for (uint32_t count=0; count0 && glob_script_mem.last_index_string[isind]) { + free(glob_script_mem.last_index_string[isind]); + } + char *sstart = lp; + uint8_t slen = 0; + for (uint32_t cnt = 0; cnt<256; cnt++) { + if (*lp=='\n' || *lp=='"' || *lp==0) { + lp++; + if (cnt>0 && !slen) { + slen++; + } + glob_script_mem.siro_num[isind] = slen; + break; + } + if (*lp=='|') { + slen++; + } + lp++; + } + + glob_script_mem.si_num[isind] = fvar; + if (glob_script_mem.si_num[isind]>0) { + if (glob_script_mem.si_num[isind]>MAX_SARRAY_NUM) { + glob_script_mem.si_num[isind] = MAX_SARRAY_NUM; + } + + glob_script_mem.last_index_string[isind] = (char*)calloc(glob_script_mem.max_ssize*glob_script_mem.si_num[isind], 1); + for (uint32_t cnt = 0; cntglob_script_mem.si_num[isind]) { + index = glob_script_mem.si_num[isind]; + } + strlcpy(str,glob_script_mem.last_index_string[isind] + (index * glob_script_mem.max_ssize), glob_script_mem.max_ssize); + } + } + lp++; + if (sp) strlcpy(sp, str, glob_script_mem.max_ssize); + return lp; +} // vtype => ff=nothing found, fe=constant number,fd = constant string else bit 7 => 80 = string, 0 = number // no flash strings here for performance reasons!!! -char *isvar(char *lp, uint8_t *vtype,struct T_INDEX *tind,float *fp,char *sp,JsonObject *jo) { - uint16_t count,len=0; - uint8_t nres=0; +char *isvar(char *lp, uint8_t *vtype, struct T_INDEX *tind, float *fp, char *sp, JsonParserObject *jo) { + uint16_t count,len = 0; + uint8_t nres = 0; char vname[32]; - float fvar=0; - tind->index=0; - tind->bits.data=0; + float fvar = 0; + tind->index = 0; + tind->bits.data = 0; if (isdigit(*lp) || (*lp=='-' && isdigit(*(lp+1))) || *lp=='.') { // isnumber if (fp) { if (*lp=='0' && *(lp+1)=='x') { - lp+=2; - *fp=strtol(lp,0,16); + lp += 2; + *fp = strtol(lp, 0, 16); } else { - *fp=CharToFloat(lp); + *fp = CharToFloat(lp); } } if (*lp=='-') lp++; @@ -1324,109 +1538,109 @@ char *isvar(char *lp, uint8_t *vtype,struct T_INDEX *tind,float *fp,char *sp,Jso if (*lp==0 || *lp==SCRIPT_EOL) break; lp++; } - tind->bits.constant=1; - tind->bits.is_string=0; - *vtype=NUM_RES; + tind->bits.constant = 1; + tind->bits.is_string = 0; + *vtype = NUM_RES; return lp; } if (*lp=='"') { lp++; while (*lp!='"') { if (*lp==0 || *lp==SCRIPT_EOL) break; - uint8_t iob=*lp; + uint8_t iob = *lp; if (iob=='\\') { lp++; if (*lp=='t') { - iob='\t'; + iob = '\t'; } else if (*lp=='n') { - iob='\n'; + iob = '\n'; } else if (*lp=='r') { - iob='\r'; + iob = '\r'; } else if (*lp=='\\') { - iob='\\'; + iob = '\\'; } else { lp--; } - if (sp) *sp++=iob; + if (sp) *sp++ = iob; } else { - if (sp) *sp++=iob; + if (sp) *sp++ = iob; } lp++; } - if (sp) *sp=0; - *vtype=STR_RES; - tind->bits.constant=1; - tind->bits.is_string=1; - return lp+1; + if (sp) *sp = 0; + *vtype = STR_RES; + tind->bits.constant = 1; + tind->bits.is_string = 1; + return lp + 1; } if (*lp=='-') { // inverted var - nres=1; + nres = 1; lp++; } - const char *term="\n\r ])=+-/*%>index=VAR_NV; - glob_script_mem.var_not_found=1; + *vtype = VAR_NV; + tind->index = VAR_NV; + glob_script_mem.var_not_found = 1; return lp; } - struct T_INDEX *vtp=glob_script_mem.type; + struct T_INDEX *vtp = glob_script_mem.type; char dvnam[32]; - strcpy (dvnam,vname); - uint8_t olen=len; - last_findex=-1; - last_sindex=-1; - char *ja=strchr(dvnam,'['); + strcpy (dvnam, vname); + uint8_t olen = len; + last_findex = -1; + last_sindex = -1; + char *ja = strchr(dvnam, '['); if (ja) { - *ja=0; + *ja = 0; ja++; - olen=strlen(dvnam); + olen = strlen(dvnam); } - for (count=0; countindex=count; // overwrite with global var index + if (!strncmp(cp, dvnam, olen)) { + uint8_t index = vtp[count].index; + *tind = vtp[count]; + tind->index = count; // overwrite with global var index if (vtp[count].bits.is_string==0) { - *vtype=NTYPE|index; + *vtype = NTYPE | index; if (vtp[count].bits.is_filter) { if (ja) { - lp+=olen+1; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - last_findex=fvar; - fvar=Get_MFVal(index,fvar); - len=1; + lp += olen + 1; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + last_findex = fvar; + fvar = Get_MFVal(index, fvar); + len = 1; } else { - fvar=Get_MFilter(index); + fvar = Get_MFilter(index); } } else { - fvar=glob_script_mem.fvars[index]; + fvar = glob_script_mem.fvars[index]; } - if (nres) fvar=-fvar; - if (fp) *fp=fvar; + if (nres) fvar = -fvar; + if (fp) *fp = fvar; } else { - *vtype=STYPE|index; - if (sp) strlcpy(sp,glob_script_mem.glob_snp+(index*glob_script_mem.max_ssize),SCRIPT_MAXSSIZE); + *vtype = STYPE|index; + if (sp) strlcpy(sp, glob_script_mem.glob_snp + (index * glob_script_mem.max_ssize), SCRIPT_MAXSSIZE); } - return lp+len; + return lp + len; } } } @@ -1434,52 +1648,52 @@ char *isvar(char *lp, uint8_t *vtype,struct T_INDEX *tind,float *fp,char *sp,Jso if (jo) { // look for json input char jvname[32]; - strcpy(jvname,vname); + strcpy(jvname, vname); const char* str_value; uint8_t aindex; String vn; - char *ja=strchr(jvname,'['); + char *ja=strchr(jvname, '['); if (ja) { // json array - *ja=0; + *ja = 0; ja++; // fetch array index float fvar; - GetNumericResult(ja,OPER_EQU,&fvar,0); - aindex=fvar; - if (aindex<1 || aindex>6) aindex=1; + GetNumericArgument(ja, OPER_EQU, &fvar, 0); + aindex = fvar; + if (aindex<1 || aindex>6) aindex = 1; aindex--; } - if (jo->success()) { - char *subtype=strchr(jvname,'#'); + if (jo->isValid()) { + char *subtype = strchr(jvname, '#'); char *subtype2; if (subtype) { - *subtype=0; + *subtype = 0; subtype++; - subtype2=strchr(subtype,'#'); + subtype2 = strchr(subtype, '#'); if (subtype2) { - *subtype2=0; + *subtype2 = 0; *subtype2++; } } - vn=jvname; - str_value = (*jo)[vn]; - if ((*jo)[vn].success()) { + vn = jvname; + str_value = (*jo)[vn].getStr(); + if ((*jo)[vn].isValid()) { if (subtype) { - JsonObject &jobj1=(*jo)[vn]; - if (jobj1.success()) { - vn=subtype; - jo=&jobj1; - str_value = (*jo)[vn]; - if ((*jo)[vn].success()) { + JsonParserObject jobj1 = (*jo)[vn]; + if (jobj1.isValid()) { + vn = subtype; + jo = &jobj1; + str_value = (*jo)[vn].getStr(); + if ((*jo)[vn].isValid()) { // 2. stage if (subtype2) { - JsonObject &jobj2=(*jo)[vn]; - if ((*jo)[vn].success()) { - vn=subtype2; - jo=&jobj2; - str_value = (*jo)[vn]; - if ((*jo)[vn].success()) { + JsonParserObject jobj2 = (*jo)[vn]; + if ((*jo)[vn].isValid()) { + vn = subtype2; + jo = &jobj2; + str_value = (*jo)[vn].getStr(); + if ((*jo)[vn].isValid()) { goto skip; } else { goto chknext; @@ -1498,37 +1712,37 @@ char *isvar(char *lp, uint8_t *vtype,struct T_INDEX *tind,float *fp,char *sp,Jso skip: if (ja) { // json array - str_value = (*jo)[vn][aindex]; + str_value = (*jo)[vn].getArray()[aindex].getStr(); } if (str_value && *str_value) { - if ((*jo).is(vn)) { - if (!strncmp(str_value,"ON",2)) { - if (fp) *fp=1; + if ((*jo)[vn].isStr()) { + if (!strncmp(str_value, "ON", 2)) { + if (fp) *fp = 1; goto nexit; - } else if (!strncmp(str_value,"OFF",3)) { - if (fp) *fp=0; + } else if (!strncmp(str_value, "OFF", 3)) { + if (fp) *fp = 0; goto nexit; } else { - *vtype=STR_RES; - tind->bits.constant=1; - tind->bits.is_string=1; - if (sp) strlcpy(sp,str_value,SCRIPT_MAXSSIZE); - return lp+len; + *vtype = STR_RES; + tind->bits.constant = 1; + tind->bits.is_string = 1; + if (sp) strlcpy(sp, str_value, SCRIPT_MAXSSIZE); + return lp + len; } } else { if (fp) { - if (!strncmp(vn.c_str(),"Epoch",5)) { - *fp=atoi(str_value)-(uint32_t)EPOCH_OFFSET; + if (!strncmp(vn.c_str(), "Epoch", 5)) { + *fp = atoi(str_value) - (uint32_t)EPOCH_OFFSET; } else { - *fp=CharToFloat((char*)str_value); + *fp = CharToFloat((char*)str_value); } } nexit: - *vtype=NUM_RES; - tind->bits.constant=1; - tind->bits.is_string=0; - return lp+len; + *vtype = NUM_RES; + tind->bits.constant = 1; + tind->bits.is_string = 0; + return lp + len; } } } @@ -1539,29 +1753,35 @@ chknext: switch (vname[0]) { case 'a': #ifdef USE_ANGLE_FUNC - if (!strncmp(vname,"acos(",5)) { - lp+=5; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - fvar=acosf(fvar); - lp++; - len=0; - goto exit; - } -#endif - if (!strncmp(vname,"asc(",4)) { - char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp+4,OPER_EQU,str,0); - fvar=str[0]; + if (!strncmp(vname, "acos(", 5)) { + lp=GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); + fvar = acosf(fvar); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"adc(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); + if (!strncmp(vname, "abs(", 4)) { + lp=GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + fvar = fabs(fvar); + lp++; + len = 0; + goto exit; + } +#endif + if (!strncmp(vname, "asc(", 4)) { + char str[SCRIPT_MAXSSIZE]; + lp = GetStringArgument(lp + 4, OPER_EQU, str, 0); + fvar = str[0]; + lp++; + len = 0; + goto exit; + } + if (!strncmp(vname, "adc(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); while (*lp==' ') lp++; - float fvar1=1; + float fvar1 = 1; if (*lp!=')') { - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0); if (fvar1<32 || fvar1>39) fvar1 = 32; } lp++; @@ -1569,146 +1789,144 @@ chknext: #ifdef ESP32 // ESP32 #ifdef USE_ADC - fvar=AdcRead(fvar1, fvar); + fvar = AdcRead(fvar1, fvar); #else - fvar=999.999; + fvar = 999.999; #endif // USE_ADC #else // ESP8266 #ifndef USE_ADC_VCC - fvar=AdcRead(fvar); + fvar = AdcRead(fvar); #else - fvar=(float)ESP.getVcc()/1000.0; + fvar = (float)ESP.getVcc() / 1000.0; #endif // USE_ADC_VCC #endif // ESP32 - len=0; + len = 0; goto exit; } break; case 'b': - if (!strncmp(vname,"boot",4)) { + if (!strncmp(vname, "boot", 4)) { if (rules_flag.system_boot) { - rules_flag.system_boot=0; - fvar=1; + rules_flag.system_boot = 0; + fvar = 1; } goto exit; } #ifdef USE_BUTTON_EVENT - if (!strncmp(vname,"bt[",3)) { + if (!strncmp(vname, "bt[", 3)) { // tasmota button state - GetNumericResult(vname+3,OPER_EQU,&fvar,0); - uint32_t index=fvar; - if (index<1 || index>MAX_KEYS) index=1; - fvar=script_button[index-1]; - script_button[index-1]|=0x80; + GetNumericArgument(vname+3, OPER_EQU, &fvar, 0); + uint32_t index = fvar; + if (index<1 || index>MAX_KEYS) index = 1; + fvar=script_button[index - 1]; + script_button[index - 1] |= 0x80; len++; goto exit; } -#endif +#endif //USE_BUTTON_EVENT break; case 'c': - if (!strncmp(vname,"chg[",4)) { + if (!strncmp(vname, "chg[", 4)) { // var changed struct T_INDEX ind; uint8_t vtype; - isvar(vname+4,&vtype,&ind,0,0,0); + isvar(vname + 4, &vtype, &ind, 0, 0, 0); if (!ind.bits.constant) { - uint8_t index=glob_script_mem.type[ind.index].index; - if (glob_script_mem.fvars[index]!=glob_script_mem.s_fvars[index]) { + uint8_t index = glob_script_mem.type[ind.index].index; + if (glob_script_mem.fvars[index] != glob_script_mem.s_fvars[index]) { // var has changed - glob_script_mem.s_fvars[index]=glob_script_mem.fvars[index]; - fvar=1; + glob_script_mem.s_fvars[index] = glob_script_mem.fvars[index]; + fvar = 1; len++; goto exit; } else { - fvar=0; + fvar = 0; len++; goto exit; } } } #ifdef ESP32 - if (!strncmp(vname,"core",4)) { - fvar=xPortGetCoreID(); + if (!strncmp(vname, "core", 4)) { + fvar = xPortGetCoreID(); goto exit; } #ifdef USE_SCRIPT_TASK - if (!strncmp(vname,"ct(",3)) { - lp+=3; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + if (!strncmp(vname, "ct(", 3)) { + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0); while (*lp==' ') lp++; float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0); while (*lp==' ') lp++; float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); lp++; - fvar=scripter_create_task(fvar,fvar1,fvar2); - len=0; + fvar = scripter_create_task(fvar, fvar1, fvar2); + len = 0; goto exit; } -#endif -#endif +#endif //USE_SCRIPT_TASK +#endif //ESP32 break; case 'd': - if (!strncmp(vname,"day",3)) { - fvar=RtcTime.day_of_month; + if (!strncmp(vname, "day", 3)) { + fvar = RtcTime.day_of_month; goto exit; } break; case 'e': - if (!strncmp(vname,"epoch",5)) { - fvar=UtcTime()-(uint32_t)EPOCH_OFFSET; + if (!strncmp(vname, "epoch", 5)) { + fvar = UtcTime() - (uint32_t)EPOCH_OFFSET; goto exit; } - if (!strncmp(vname,"eres",4)) { - fvar=event_handeled; - tind->index=SCRIPT_EVENT_HANDLED; + if (!strncmp(vname, "eres", 4)) { + fvar = event_handeled; + tind->index = SCRIPT_EVENT_HANDLED; goto exit_settable; } #ifdef USE_ENERGY_SENSOR - if (!strncmp(vname,"enrg[",5)) { - lp+=5; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + if (!strncmp(vname, "enrg[", 5)) { + lp=GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); while (*lp==' ') lp++; switch ((uint32_t)fvar) { case 0: - fvar=Energy.total; + fvar = Energy.total; break; case 1: - fvar=Energy.voltage[0]; + fvar = Energy.voltage[0]; break; case 2: - fvar=Energy.voltage[1]; + fvar = Energy.voltage[1]; break; case 3: - fvar=Energy.voltage[2]; + fvar = Energy.voltage[2]; break; case 4: - fvar=Energy.current[0]; + fvar = Energy.current[0]; break; case 5: - fvar=Energy.current[1]; + fvar = Energy.current[1]; break; case 6: - fvar=Energy.current[2]; + fvar = Energy.current[2]; break; case 7: - fvar=Energy.active_power[0]; + fvar = Energy.active_power[0]; break; case 8: - fvar=Energy.active_power[1]; + fvar = Energy.active_power[1]; break; case 9: - fvar=Energy.active_power[2]; + fvar = Energy.active_power[2]; break; default: - fvar=99999; + fvar = 99999; break; } - len=0; + len = 0; lp++; goto exit; } @@ -1717,166 +1935,161 @@ chknext: case 'f': //#define DEBUG_FS #ifdef USE_SCRIPT_FATFS - if (!strncmp(vname,"fo(",3)) { - lp+=3; + if (!strncmp(vname, "fo(", 3)) { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,0); + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); while (*lp==' ') lp++; - uint8_t mode=0; + uint8_t mode = 0; if ((*lp=='r') || (*lp=='w') || (*lp=='a')) { switch (*lp) { case 'r': - mode=0; + mode = 0; break; case 'w': - mode=1; + mode = 1; break; case 'a': - mode=2; + mode = 2; break; } lp++; } else { - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - mode=fvar; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + mode = fvar; } - fvar=-1; - for (uint8_t cnt=0;cntopen(str,FILE_READ); + glob_script_mem.files[cnt] = fsp->open(str, FILE_READ); if (glob_script_mem.files[cnt].isDirectory()) { glob_script_mem.files[cnt].rewindDirectory(); - glob_script_mem.file_flags[cnt].is_dir=1; + glob_script_mem.file_flags[cnt].is_dir = 1; } else { - glob_script_mem.file_flags[cnt].is_dir=0; + glob_script_mem.file_flags[cnt].is_dir = 0; } } else { if (mode==1) { - glob_script_mem.files[cnt]=fsp->open(str,FILE_WRITE); + glob_script_mem.files[cnt] = fsp->open(str,FILE_WRITE); #ifdef DEBUG_FS - AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for write %d"),cnt); + AddLog_P2(LOG_LEVEL_INFO, PSTR("open file for write %d"), cnt); #endif } else { - glob_script_mem.files[cnt]=fsp->open(str,FILE_APPEND); + glob_script_mem.files[cnt] = fsp->open(str,FILE_APPEND); #ifdef DEBUG_FS - AddLog_P2(LOG_LEVEL_INFO,PSTR("open file for append %d"),cnt); + AddLog_P2(LOG_LEVEL_INFO, PSTR("open file for append %d"), cnt); #endif } } if (glob_script_mem.files[cnt]) { - fvar=cnt; - glob_script_mem.file_flags[cnt].is_open=1; + fvar = cnt; + glob_script_mem.file_flags[cnt].is_open = 1; } else { - AddLog_P(LOG_LEVEL_INFO,PSTR("file open failed")); + AddLog_P(LOG_LEVEL_INFO, PSTR("file open failed")); } break; } } lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"fc(",3)) { - lp+=3; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + if (!strncmp(vname, "fc(", 3)) { + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0); if (fvar>=0) { - uint8_t ind=fvar; - if (ind>=SFS_MAX) ind=SFS_MAX-1; + uint8_t ind = fvar; + if (ind>=SFS_MAX) ind = SFS_MAX - 1; #ifdef DEBUG_FS - AddLog_P2(LOG_LEVEL_INFO,PSTR("closing file %d"),ind); + AddLog_P2(LOG_LEVEL_INFO, PSTR("closing file %d"), ind); #endif glob_script_mem.files[ind].close(); - glob_script_mem.file_flags[ind].is_open=0; + glob_script_mem.file_flags[ind].is_open = 0; } - fvar=0; + fvar = 0; lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"ff(",3)) { - lp+=3; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - uint8_t ind=fvar; - if (ind>=SFS_MAX) ind=SFS_MAX-1; + if (!strncmp(vname, "ff(", 3)) { + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0); + uint8_t ind = fvar; + if (ind>=SFS_MAX) ind = SFS_MAX - 1; glob_script_mem.files[ind].flush(); - fvar=0; + fvar = 0; lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"fw(",3)) { - lp+=3; + if (!strncmp(vname, "fw(", 3)) { char str[SCRIPT_MAXSSIZE]; - lp=ForceStringVar(lp,str); + lp = ForceStringVar(lp + 3, str); while (*lp==' ') lp++; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - uint8_t ind=fvar; - if (ind>=SFS_MAX) ind=SFS_MAX-1; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + uint8_t ind = fvar; + if (ind>=SFS_MAX) ind = SFS_MAX - 1; if (glob_script_mem.file_flags[ind].is_open) { - fvar=glob_script_mem.files[ind].print(str); + fvar = glob_script_mem.files[ind].print(str); } else { - fvar=0; + fvar = 0; } lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"fr(",3)) { - lp+=3; + if (!strncmp(vname, "fr(", 3)) { struct T_INDEX ind; uint8_t vtype; - uint8_t sindex=0; - lp=isvar(lp,&vtype,&ind,0,0,0); + uint8_t sindex = 0; + lp = isvar(lp + 3, &vtype, &ind, 0, 0, 0); if (vtype!=VAR_NV) { // found variable as result if ((vtype&STYPE)==0) { // error - fvar=0; + fvar = 0; goto exit; } else { // string result - sindex=glob_script_mem.type[ind.index].index; + sindex = glob_script_mem.type[ind.index].index; } } else { // error - fvar=0; + fvar = 0; goto exit; } while (*lp==' ') lp++; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - uint8_t find=fvar; - if (find>=SFS_MAX) find=SFS_MAX-1; - uint8_t index=0; - char str[glob_script_mem.max_ssize+1]; - char *cp=str; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + uint8_t find = fvar; + if (find>=SFS_MAX) find = SFS_MAX - 1; + uint8_t index = 0; + char str[glob_script_mem.max_ssize + 1]; + char *cp = str; if (glob_script_mem.file_flags[find].is_open) { if (glob_script_mem.file_flags[find].is_dir) { while (true) { - File entry=glob_script_mem.files[find].openNextFile(); + File entry = glob_script_mem.files[find].openNextFile(); if (entry) { if (!reject((char*)entry.name())) { - char *ep=(char*)entry.name(); + char *ep = (char*)entry.name(); if (*ep=='/') ep++; char *lcp = strrchr(ep,'/'); if (lcp) { - ep=lcp+1; + ep = lcp + 1; } - strcpy(str,ep); + strcpy(str, ep); entry.close(); break; } } else { - *cp=0; + *cp = 0; break; } entry.close(); } - index=strlen(str); + index = strlen(str); } else { while (glob_script_mem.files[find].available()) { uint8_t buf[1]; @@ -1884,473 +2097,513 @@ chknext: if (buf[0]=='\t' || buf[0]==',' || buf[0]=='\n' || buf[0]=='\r') { break; } else { - *cp++=buf[0]; + *cp++ = buf[0]; index++; - if (index>=glob_script_mem.max_ssize-1) break; + if (index>=glob_script_mem.max_ssize - 1) break; } } - *cp=0; + *cp = 0; } } else { - strcpy(str,"file error"); + strcpy(str, "file error"); } lp++; - strlcpy(glob_script_mem.glob_snp+(sindex*glob_script_mem.max_ssize),str,glob_script_mem.max_ssize); - fvar=index; - len=0; + strlcpy(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); + fvar = index; + len = 0; goto exit; } - if (!strncmp(vname,"fd(",3)) { - lp+=3; - char str[glob_script_mem.max_ssize+1]; - lp=GetStringResult(lp,OPER_EQU,str,0); + if (!strncmp(vname, "fd(", 3)) { + char str[glob_script_mem.max_ssize + 1]; + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); fsp->remove(str); lp++; - len=0; + len = 0; goto exit; } #if defined(ESP32) && defined(USE_WEBCAM) - if (!strncmp(vname,"fwp(",4)) { - lp+=4; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + if (!strncmp(vname, "fwp(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); while (*lp==' ') lp++; float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); - uint8_t ind=fvar1; - if (ind>=SFS_MAX) ind=SFS_MAX-1; + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0); + uint8_t ind = fvar1; + if (ind>=SFS_MAX) ind = SFS_MAX - 1; if (glob_script_mem.file_flags[ind].is_open) { uint8_t *buff; - float maxps=WcGetPicstore(-1,0); - if (fvar<1 || fvar>maxps) fvar=1; - uint32_t len=WcGetPicstore(fvar-1, &buff); + float maxps = WcGetPicstore(-1, 0); + if (fvar<1 || fvar>maxps) fvar = 1; + uint32_t len = WcGetPicstore(fvar - 1, &buff); if (len) { //glob_script_mem.files[ind].seek(0,SeekEnd); - fvar=glob_script_mem.files[ind].write(buff,len); + fvar = glob_script_mem.files[ind].write(buff, len); } else { - fvar=0; + fvar = 0; } //AddLog_P2(LOG_LEVEL_INFO, PSTR("picture save: %d"), len); } else { - fvar=0; + fvar = 0; } lp++; - len=0; + len = 0; goto exit; } -#endif +#endif //ESP32 && USE_WEBCAM #ifdef USE_SCRIPT_FATFS_EXT - if (!strncmp(vname,"fe(",3)) { - lp+=3; - char str[glob_script_mem.max_ssize+1]; - lp=GetStringResult(lp,OPER_EQU,str,0); + if (!strncmp(vname, "fe(", 3)) { + char str[glob_script_mem.max_ssize + 1]; + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); // execute script - File ef=fsp->open(str,FILE_READ); + File ef = fsp->open(str, FILE_READ); if (ef) { - uint16_t fsiz=ef.size(); + uint16_t fsiz = ef.size(); if (fsiz<2048) { - char *script=(char*)calloc(fsiz+16,1); + char *script = (char*)calloc(fsiz + 16, 1); if (script) { ef.read((uint8_t*)script,fsiz); execute_script(script); free(script); - fvar=1; + fvar = 1; } } ef.close(); } lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"fmd(",4)) { - lp+=4; - char str[glob_script_mem.max_ssize+1]; - lp=GetStringResult(lp,OPER_EQU,str,0); - fvar=fsp->mkdir(str); + if (!strncmp(vname, "fmd(", 4)) { + char str[glob_script_mem.max_ssize + 1]; + lp = GetStringArgument(lp + 4, OPER_EQU, str, 0); + fvar = fsp->mkdir(str); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"frd(",4)) { - lp+=4; - char str[glob_script_mem.max_ssize+1]; - lp=GetStringResult(lp,OPER_EQU,str,0); - fvar=fsp->rmdir(str); + if (!strncmp(vname, "frd(", 4)) { + char str[glob_script_mem.max_ssize + 1]; + lp = GetStringArgument(lp + 4, OPER_EQU, str, 0); + fvar = fsp->rmdir(str); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"fx(",3)) { - lp+=3; - char str[glob_script_mem.max_ssize+1]; - lp=GetStringResult(lp,OPER_EQU,str,0); - if (fsp->exists(str)) fvar=1; - else fvar=0; + if (!strncmp(vname, "fx(", 3)) { + char str[glob_script_mem.max_ssize + 1]; + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); + if (fsp->exists(str)) fvar = 1; + else fvar = 0; lp++; - len=0; + len = 0; goto exit; } + + if (!strncmp(vname, "fsi(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + fvar = get_fsinfo(fvar); + lp++; + len = 0; + goto exit; + } + + if (!strncmp(vname, "fwa(", 4)) { + struct T_INDEX ind; + uint8_t vtype; + lp = isvar(lp + 4, &vtype, &ind, 0, 0, 0); + if (vtype!=VAR_NV && (vtype&STYPE)==0 && glob_script_mem.type[ind.index].bits.is_filter) { + // found array as result + + } else { + // error + fvar = 0; + goto exit; + } + + while (*lp==' ') lp++; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + uint8_t index = fvar; + if (index>=SFS_MAX) index = SFS_MAX - 1; + if (glob_script_mem.file_flags[index].is_open) { + uint16_t len = 0; + float *fa = Get_MFAddr(glob_script_mem.type[ind.index].index, &len, 0); + char dstr[24]; + for (uint32_t cnt = 0; cnt=SFS_MAX) find = SFS_MAX - 1; + char str[glob_script_mem.max_ssize + 1]; + if (glob_script_mem.file_flags[find].is_open) { + uint16_t len = 0; + float *fa = Get_MFAddr(glob_script_mem.type[ind.index].index, &len, 0); + char dstr[24]; + for (uint32_t cnt = 0; cnt=glob_script_mem.max_ssize - 1) break; + } + } + *cp = 0; + *fa++=CharToFloat(str); + } + } else { + fvar = 0; + } + lp++; + len = 0; + goto exit; + } + #endif // USE_SCRIPT_FATFS_EXT - if (!strncmp(vname,"fl1(",4) || !strncmp(vname,"fl2(",4) ) { - uint8_t lknum=*(lp+2)&3; - lp+=4; - char str[glob_script_mem.max_ssize+1]; - lp=GetStringResult(lp,OPER_EQU,str,0); - if (lknum<1 || lknum>2) lknum=1; - strlcpy(glob_script_mem.flink[lknum-1],str,14); + if (!strncmp(vname, "fl1(", 4) || !strncmp(vname, "fl2(", 4) ) { + uint8_t lknum = *(lp+2)&3; + char str[glob_script_mem.max_ssize + 1]; + lp = GetStringArgument(lp + 4, OPER_EQU, str, 0); + if (lknum<1 || lknum>2) lknum = 1; + strlcpy(glob_script_mem.flink[lknum - 1], str, 14); lp++; - fvar=0; - len=0; + fvar = 0; + len = 0; goto exit; } - if (!strncmp(vname,"fsm",3)) { + if (!strncmp(vname, "fsm", 3)) { fvar=glob_script_mem.script_sd_found; //card_init(); goto exit; } #endif //USE_SCRIPT_FATFS - if (!strncmp(vname,"freq",4)) { + if (!strncmp(vname, "freq", 4)) { #ifdef ESP32 - fvar=getCpuFrequencyMhz(); + fvar = getCpuFrequencyMhz(); #else - fvar=ESP.getCpuFreqMHz(); + fvar = ESP.getCpuFreqMHz(); #endif goto exit; } break; case 'g': - if (!strncmp(vname,"gtmp",4)) { - fvar=global_temperature_celsius; + if (!strncmp(vname, "gtmp", 4)) { + fvar = global_temperature_celsius; goto exit; } - if (!strncmp(vname,"ghum",4)) { - fvar=global_humidity; + if (!strncmp(vname, "ghum", 4)) { + fvar = global_humidity; goto exit; } - if (!strncmp(vname,"gprs",4)) { - fvar=global_pressure_hpa; + if (!strncmp(vname, "gprs", 4)) { + fvar = global_pressure_hpa; goto exit; } - if (!strncmp(vname,"gtopic",6)) { - if (sp) strlcpy(sp,SettingsText(SET_MQTT_GRP_TOPIC),glob_script_mem.max_ssize); + if (!strncmp(vname, "gtopic", 6)) { + if (sp) strlcpy(sp, SettingsText(SET_MQTT_GRP_TOPIC), glob_script_mem.max_ssize); goto strexit; } #ifdef SCRIPT_GET_HTTPS_JP - if (!strncmp(vname,"gjp(",4)) { + if (!strncmp(vname, "gjp(", 4)) { char host[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp+4,OPER_EQU,host,0); + lp = GetStringArgument(lp + 4, OPER_EQU, host, 0); SCRIPT_SKIP_SPACES char path[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,path,0); - fvar=call2https(host,path); + lp = GetStringArgument(lp, OPER_EQU, path, 0); + fvar = call2https(host, path); lp++; - len=0; + len = 0; goto exit; } -#endif +#endif //SCRIPT_GET_HTTPS_JP break; case 'h': - if (!strncmp(vname,"hours",5)) { - fvar=RtcTime.hour; + if (!strncmp(vname, "hours", 5)) { + fvar = RtcTime.hour; goto exit; } - if (!strncmp(vname,"heap",4)) { - fvar=ESP_getFreeHeap(); + if (!strncmp(vname, "heap", 4)) { + fvar = ESP_getFreeHeap(); goto exit; } - if (!strncmp(vname,"hn(",3)) { - lp=GetNumericResult(lp+3,OPER_EQU,&fvar,0); - if (fvar<0 || fvar>255) fvar=0; + if (!strncmp(vname, "hn(", 3)) { + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0); + if (fvar<0 || fvar>255) fvar = 0; lp++; - len=0; + len = 0; if (sp) { - sprintf(sp,"%02x",(uint8_t)fvar); + sprintf(sp, "%02x", (uint8_t)fvar); } goto strexit; } - if (!strncmp(vname,"hx(",3)) { - lp=GetNumericResult(lp+3,OPER_EQU,&fvar,0); + if (!strncmp(vname, "hx(", 3)) { + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0); lp++; - len=0; + len = 0; if (sp) { - sprintf(sp,"%08x",(uint32_t)fvar); + sprintf(sp, "%08x", (uint32_t)fvar); } goto strexit; } - if (!strncmp(vname,"hd(",3)) { + if (!strncmp(vname, "hd(", 3)) { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp+3,OPER_EQU,str,0); - fvar=strtol(str,NULL,16); + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); + fvar = strtol(str, NULL, 16); lp++; - len=0; + len = 0; goto exit; } #ifdef USE_LIGHT -//#ifdef USE_WS2812 - if (!strncmp(vname,"hsvrgb(",7)) { - lp=GetNumericResult(lp+7,OPER_EQU,&fvar,0); - if (fvar<0 || fvar>360) fvar=0; + if (!strncmp(vname, "hsvrgb(", 7)) { + lp = GetNumericArgument(lp + 7, OPER_EQU, &fvar, 0); + if (fvar<0 || fvar>360) fvar = 0; SCRIPT_SKIP_SPACES // arg2 float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - if (fvar2<0 || fvar2>100) fvar2=0; + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + if (fvar2<0 || fvar2>100) fvar2 = 0; SCRIPT_SKIP_SPACES // arg3 float fvar3; - lp=GetNumericResult(lp,OPER_EQU,&fvar3,0); - if (fvar3<0 || fvar3>100) fvar3=0; - - fvar=HSVToRGB(fvar,fvar2,fvar3); + lp = GetNumericArgument(lp, OPER_EQU, &fvar3, 0); + if (fvar3<0 || fvar3>100) fvar3 = 0; + fvar = HSVToRGB(fvar, fvar2, fvar3); lp++; - len=0; + len = 0; goto exit; } -//#endif -#endif +#endif //USE_LIGHT break; -#define MAX_SARRAY_NUM 32 case 'i': - if (!strncmp(vname,"int(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); - fvar=floor(fvar); + if (!strncmp(vname, "int(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + fvar = floor(fvar); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"is(",3)) { - lp=GetNumericResult(lp+3,OPER_EQU,&fvar,0); - SCRIPT_SKIP_SPACES - if (*lp!='"') { - break; - } - lp++; - - if (glob_script_mem.si_num>0 && glob_script_mem.last_index_string) { - free(glob_script_mem.last_index_string); - } - char *sstart=lp; - uint8_t slen=0; - for (uint32_t cnt=0; cnt<256; cnt++) { - if (*lp=='\n' || *lp=='"' || *lp==0) { - lp++; - if (cnt>0 && !slen) { - slen++; - } - glob_script_mem.siro_num=slen; - break; - } - if (*lp=='|') { - slen++; - } - lp++; - } - - glob_script_mem.si_num = fvar; - if (glob_script_mem.si_num>0) { - if (glob_script_mem.si_num>MAX_SARRAY_NUM) { - glob_script_mem.si_num=MAX_SARRAY_NUM; - } - - glob_script_mem.last_index_string=(char*)calloc(glob_script_mem.max_ssize*glob_script_mem.si_num,1); - for (uint32_t cnt=0; cntglob_script_mem.si_num) { - index=glob_script_mem.si_num; - } - strlcpy(str,glob_script_mem.last_index_string+(index*glob_script_mem.max_ssize),glob_script_mem.max_ssize); - } - } - lp++; - if (sp) strlcpy(sp,str,glob_script_mem.max_ssize); - len=0; + if (!strncmp(vname, "is1(", 4)) { + lp = isargs(lp + 4, 1); + fvar = 0; + len = 0; + goto exit; + } + if (!strncmp(vname, "is2(", 4)) { + lp = isargs(lp + 4, 2); + fvar = 0; + len = 0; + goto exit; + } + if (!strncmp(vname, "is[", 3)) { + lp = isget(lp + 3, sp, 0); + len = 0; + goto strexit; + } + if (!strncmp(vname, "is1[", 4)) { + lp = isget(lp + 4, sp, 1); + len = 0; + goto strexit; + } + if (!strncmp(vname, "is2[", 4)) { + lp = isget(lp + 4, sp, 2); + len = 0; goto strexit; } break; case 'l': - if (!strncmp(vname,"lip",3)) { - if (sp) strlcpy(sp,(const char*)WiFi.localIP().toString().c_str(),glob_script_mem.max_ssize); + if (!strncmp(vname, "lip", 3)) { + if (sp) strlcpy(sp, (const char*)WiFi.localIP().toString().c_str(), glob_script_mem.max_ssize); goto strexit; } #ifdef USE_SCRIPT_GLOBVARS - if (!strncmp(vname,"luip",4)) { - if (sp) strlcpy(sp,IPAddressToString(last_udp_ip),glob_script_mem.max_ssize); + if (!strncmp(vname, "luip", 4)) { + if (sp) strlcpy(sp, IPAddressToString(last_udp_ip), glob_script_mem.max_ssize); goto strexit; } -#endif - if (!strncmp(vname,"loglvl",6)) { - fvar=glob_script_mem.script_loglevel; - tind->index=SCRIPT_LOGLEVEL; +#endif //USE_SCRIPT_GLOBVARS + if (!strncmp(vname, "loglvl", 6)) { + fvar = glob_script_mem.script_loglevel; + tind->index = SCRIPT_LOGLEVEL; exit_settable: - if (fp) *fp=fvar; - *vtype=NTYPE; - tind->bits.settable=1; - tind->bits.is_string=0; - return lp+len; + if (fp) *fp = fvar; + *vtype = NTYPE; + tind->bits.settable = 1; + tind->bits.is_string = 0; + return lp + len; } break; case 'm': - if (!strncmp(vname,"med(",4)) { + if (!strncmp(vname, "med(", 4)) { float fvar1; - lp=GetNumericResult(lp+4,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar1, 0); SCRIPT_SKIP_SPACES // arg2 float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - fvar=DoMedian5(fvar1,fvar2); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + fvar = DoMedian5(fvar1, fvar2); lp++; - len=0; + len = 0; goto exit; } #ifdef USE_ANGLE_FUNC - if (!strncmp(vname,"mpt(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); - fvar=MeasurePulseTime(fvar); + if (!strncmp(vname, "mpt(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + fvar = MeasurePulseTime(fvar); lp++; - len=0; + len = 0; goto exit; } -#endif - if (!strncmp(vname,"micros",6)) { - fvar=micros(); +#endif //USE_ANGLE_FUNC + if (!strncmp(vname, "micros", 6)) { + fvar = micros(); goto exit; } - if (!strncmp(vname,"millis",6)) { - fvar=millis(); + if (!strncmp(vname, "millis", 6)) { + fvar = millis(); goto exit; } - if (!strncmp(vname,"mins",4)) { - fvar=RtcTime.minute; + if (!strncmp(vname, "mins", 4)) { + fvar = RtcTime.minute; goto exit; } - if (!strncmp(vname,"month",5)) { - fvar=RtcTime.month; + if (!strncmp(vname, "month", 5)) { + fvar = RtcTime.month; goto exit; } - if (!strncmp(vname,"mqttc",5)) { + if (!strncmp(vname, "mqttc", 5)) { if (rules_flag.mqtt_connected) { - rules_flag.mqtt_connected=0; - fvar=1; + rules_flag.mqtt_connected = 0; + fvar = 1; } goto exit; } - if (!strncmp(vname,"mqttd",5)) { + if (!strncmp(vname, "mqttd", 5)) { if (rules_flag.mqtt_disconnected) { - rules_flag.mqtt_disconnected=0; - fvar=1; + rules_flag.mqtt_disconnected = 0; + fvar = 1; } goto exit; } - if (!strncmp(vname,"mqtts",5)) { - fvar=!global_state.mqtt_down; + if (!strncmp(vname, "mqtts", 5)) { + fvar = !global_state.mqtt_down; goto exit; } - if (!strncmp(vname,"mp(",3)) { - lp+=3; + if (!strncmp(vname, "mp(", 3)) { float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar1, 0); SCRIPT_SKIP_SPACES while (*lp!=')') { - char *opp=lp; + char *opp = lp; lp++; float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); SCRIPT_SKIP_SPACES - fvar=fvar1; + fvar = fvar1; if ((*opp=='<' && fvar1' && fvar1>fvar2) || - (*opp=='=' && fvar1==fvar2)) - { - if (*lp!='<' && *lp!='>' && *lp!='=' && *lp!=')' && *lp!=SCRIPT_EOL) { - float fvar3; - lp=GetNumericResult(lp,OPER_EQU,&fvar3,0); - SCRIPT_SKIP_SPACES - fvar=fvar3; - } else { - fvar=fvar2; - } - break; + (*opp=='=' && fvar1==fvar2)) { + if (*lp!='<' && *lp!='>' && *lp!='=' && *lp!=')' && *lp!=SCRIPT_EOL) { + float fvar3; + lp = GetNumericArgument(lp, OPER_EQU, &fvar3, 0); + SCRIPT_SKIP_SPACES + fvar=fvar3; + } else { + fvar = fvar2; + } + break; } while (*lp!='<' && *lp!='>' && *lp!='=' && *lp!=')' && *lp!=SCRIPT_EOL) lp++; } - len=0; + len = 0; goto exit; } #ifdef USE_MORITZ - if (!strncmp(vname,"mo(",3)) { + if (!strncmp(vname, "mo(", 3)) { float fvar1; - lp=GetNumericResult(lp+3,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar1, 0); SCRIPT_SKIP_SPACES float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); SCRIPT_SKIP_SPACES char rbuff[64]; - fvar=mo_getvars(fvar1,fvar2,rbuff); + fvar = mo_getvars(fvar1, fvar2, rbuff); lp++; - if (sp) strlcpy(sp,rbuff,glob_script_mem.max_ssize); - len=0; + if (sp) strlcpy(sp, rbuff, glob_script_mem.max_ssize); + len = 0; goto strexit; } -#endif +#endif //USE_MORITZ break; case 'p': - if (!strncmp(vname,"pin[",4)) { + if (!strncmp(vname, "pin[", 4)) { // raw pin level - GetNumericResult(vname+4,OPER_EQU,&fvar,0); - fvar=digitalRead((uint8_t)fvar); + GetNumericArgument(vname + 4, OPER_EQU, &fvar, 0); + fvar = digitalRead((uint8_t)fvar); // skip ] bracket len++; goto exit; } - if (!strncmp(vname,"pn[",3)) { - GetNumericResult(vname+3,OPER_EQU,&fvar,0); - fvar=Pin(fvar); + if (!strncmp(vname, "pn[", 3)) { + GetNumericArgument(vname + 3, OPER_EQU, &fvar, 0); + fvar = Pin(fvar); // skip ] bracket len++; goto exit; } #if defined(ESP32) && (defined(USE_I2S_AUDIO) || defined(USE_TTGO_WATCH)) - if (!strncmp(vname,"pl(",3)) { + if (!strncmp(vname, "pl(", 3)) { char path[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp+3,OPER_EQU,path,0); + lp = GetStringArgument(lp + 3, OPER_EQU, path, 0); Play_mp3(path); len++; - len=0; + len = 0; goto exit; } #endif // USE_I2S_AUDIO - if (!strncmp(vname,"pd[",3)) { - GetNumericResult(vname+3,OPER_EQU,&fvar,0); - uint8_t gpiopin=fvar; + if (!strncmp(vname, "pd[", 3)) { + GetNumericArgument(vname + 3, OPER_EQU, &fvar, 0); + uint8_t gpiopin = fvar; /* for (uint8_t i=0;iMAX_COUNTERS) index=1; - fvar=RtcSettings.pulse_counter[index-1]; - len+=1; + if (!strncmp(vname, "pc[", 3)) { + GetNumericArgument(vname + 3, OPER_EQU, &fvar, 0); + uint8_t index = fvar; + if (index<1 || index>MAX_COUNTERS) index = 1; + fvar = RtcSettings.pulse_counter[index - 1]; + len += 1; goto exit; } break; case 'r': - if (!strncmp(vname,"ram",3)) { - fvar=glob_script_mem.script_mem_size+(glob_script_mem.script_size)+(PMEM_SIZE); + if (!strncmp(vname, "ram", 3)) { + fvar = glob_script_mem.script_mem_size + (glob_script_mem.script_size) + (PMEM_SIZE); goto exit; } - if (!strncmp(vname,"rnd(",4)) { + if (!strncmp(vname, "rnd(", 4)) { // tasmota switch state - GetNumericResult(vname+4,OPER_EQU,&fvar,0); + GetNumericArgument(vname + 4, OPER_EQU, &fvar, 0); if (fvar<0) { randomSeed(-fvar); - fvar=0; + fvar = 0; } else { - fvar=random(fvar); + fvar = random(fvar); } // skip ] bracket len++; @@ -2444,81 +2696,78 @@ chknext: } break; case 's': - if (!strncmp(vname,"secs",4)) { - fvar=RtcTime.second; + if (!strncmp(vname, "secs", 4)) { + fvar = RtcTime.second; goto exit; } - if (!strncmp(vname,"sw[",3)) { + if (!strncmp(vname, "sw[", 3)) { // tasmota switch state - GetNumericResult(vname+3,OPER_EQU,&fvar,0); - fvar=SwitchLastState((uint32_t)fvar); + GetNumericArgument(vname + 3, OPER_EQU, &fvar, 0); + fvar = SwitchLastState((uint32_t)fvar); // skip ] bracket len++; goto exit; } - if (!strncmp(vname,"stack",5)) { - fvar=GetStack(); + if (!strncmp(vname, "stack", 5)) { + fvar = GetStack(); goto exit; } - if (!strncmp(vname,"slen",4)) { - fvar=strlen(glob_script_mem.script_ram); + if (!strncmp(vname, "slen", 4)) { + fvar = strlen(glob_script_mem.script_ram); goto exit; } - if (!strncmp(vname,"sl(",3)) { - lp+=3; + if (!strncmp(vname, "sl(", 3)) { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,0); + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); lp++; - len=0; - fvar=strlen(str); + len = 0; + fvar = strlen(str); goto exit; } - if (!strncmp(vname,"sb(",3)) { - lp+=3; + if (!strncmp(vname, "sb(", 3)) { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,0); + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); SCRIPT_SKIP_SPACES float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0); SCRIPT_SKIP_SPACES float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); lp++; - len=0; + len = 0; if (fvar1<0) { - fvar1=strlen(str)+fvar1; + fvar1 = strlen(str) + fvar1; } - memcpy(sp,&str[(uint8_t)fvar1],(uint8_t)fvar2); + memcpy(sp, &str[(uint8_t)fvar1], (uint8_t)fvar2); sp[(uint8_t)fvar2] = '\0'; goto strexit; } - if (!strncmp(vname,"st(",3)) { - lp+=3; + if (!strncmp(vname, "st(", 3)) { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,0); + lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); while (*lp==' ') lp++; char token[2]; - token[0]=*lp++; - token[1]=0; + token[0] = *lp++; + token[1] = 0; while (*lp==' ') lp++; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); // skip ) bracket lp++; - len=0; + len = 0; if (sp) { // get stringtoken - char *st=strtok(str,token); + char *st = strtok(str, token); if (!st) { - *sp=0; + *sp = 0; } else { - for (uint8_t cnt=1; cnt<=fvar; cnt++) { + for (uint8_t cnt = 1; cnt<=fvar; cnt++) { if (cnt==fvar) { - strcpy(sp,st); + strcpy(sp, st); break; } - st=strtok(NULL,token); + st = strtok(NULL, token); if (!st) { - *sp=0; + *sp = 0; break; } } @@ -2526,249 +2775,237 @@ chknext: } goto strexit; } - if (!strncmp(vname,"s(",2)) { - lp+=2; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - char str[glob_script_mem.max_ssize+1]; - dtostrfd(fvar,glob_script_mem.script_dprec,str); - if (sp) strlcpy(sp,str,glob_script_mem.max_ssize); + if (!strncmp(vname, "s(", 2)) { + lp = GetNumericArgument(lp + 2, OPER_EQU, &fvar, 0); + char str[glob_script_mem.max_ssize + 1]; + f2char(fvar, glob_script_mem.script_dprec, glob_script_mem.script_lzero, str); + if (sp) strlcpy(sp, str, glob_script_mem.max_ssize); lp++; - len=0; + len = 0; goto strexit; } #if defined(ESP32) && (defined(USE_I2S_AUDIO) || defined(USE_TTGO_WATCH)) - if (!strncmp(vname,"say(",4)) { + if (!strncmp(vname, "say(", 4)) { char text[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp+4,OPER_EQU,text,0); + lp = GetStringArgument(lp + 4, OPER_EQU, text, 0); Say(text); len++; - len=0; + len = 0; goto exit; } #endif // USE_I2S_AUDIO #ifdef ESP32 - if (!strncmp(vname,"sf(",3)) { - lp+=2; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - if (fvar<80) fvar=80; - if (fvar>240) fvar=240; + if (!strncmp(vname, "sf(", 3)) { + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, 0); + if (fvar<80) fvar = 80; + if (fvar>240) fvar = 240; setCpuFrequencyMhz(fvar); - fvar=getCpuFrequencyMhz(); + fvar = getCpuFrequencyMhz(); lp++; - len=0; + len = 0; goto exit; } -#endif +#endif //ESP32 #ifdef USE_TTGO_WATCH - if (!strncmp(vname,"slp(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); + if (!strncmp(vname, "slp(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); SCRIPT_SKIP_SPACES TTGO_Sleep(fvar); lp++; - len=0; + len = 0; goto exit; } -#endif +#endif //USE_TTGO_WATCH #if defined(USE_TIMERS) && defined(USE_SUNRISE) - if (!strncmp(vname,"sunrise",7)) { - fvar=SunMinutes(0); + if (!strncmp(vname, "sunrise", 7)) { + fvar = SunMinutes(0); goto exit; } - if (!strncmp(vname,"sunset",6)) { - fvar=SunMinutes(1); + if (!strncmp(vname, "sunset", 6)) { + fvar = SunMinutes(1); goto exit; } -#endif +#endif //USE_TIMERS #ifdef USE_SHUTTER - if (!strncmp(vname,"sht[",4)) { - GetNumericResult(vname+4,OPER_EQU,&fvar,0); - uint8_t index=fvar; + if (!strncmp(vname, "sht[", 4)) { + GetNumericArgument(vname + 4, OPER_EQU, &fvar, 0); + uint8_t index = fvar; if (index<=shutters_present) { - fvar=Settings.shutter_position[index-1]; + fvar = Settings.shutter_position[index - 1]; } else { - fvar=-1; + fvar = -1; } - len+=1; + len += 1; goto exit; } -#endif +#endif //USE_SHUTTER #ifdef USE_ANGLE_FUNC - if (!strncmp(vname,"sin(",4)) { - lp+=4; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - fvar=sinf(fvar); + if (!strncmp(vname, "sin(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + fvar = sinf(fvar); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"sqrt(",5)) { - lp+=5; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - fvar=sqrtf(fvar); + if (!strncmp(vname, "sqrt(", 5)) { + lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); + fvar = sqrtf(fvar); lp++; - len=0; + len = 0; goto exit; } -#endif +#endif //USE_ANGLE_FUNC #if defined(USE_SML_M) && defined (USE_SML_SCRIPT_CMD) - if (!strncmp(vname,"sml[",4)) { - lp+=4; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + if (!strncmp(vname, "sml[", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); SCRIPT_SKIP_SPACES - fvar=SML_GetVal(fvar); + fvar = SML_GetVal(fvar); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"sml(",4)) { - lp+=4; + if (!strncmp(vname, "sml(", 4)) { float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar1, 0); SCRIPT_SKIP_SPACES float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); SCRIPT_SKIP_SPACES if (fvar2==0) { float fvar3; - lp=GetNumericResult(lp,OPER_EQU,&fvar3,0); - fvar=SML_SetBaud(fvar1,fvar3); + lp = GetNumericArgument(lp, OPER_EQU, &fvar3, 0); + fvar = SML_SetBaud(fvar1, fvar3); } else if (fvar2==1) { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,0); - fvar=SML_Write(fvar1,str); + lp = GetStringArgument(lp, OPER_EQU, str, 0); + fvar = SML_Write(fvar1, str); } else if (fvar2==2) { char str[SCRIPT_MAXSSIZE]; - str[0]=0; - fvar=SML_Read(fvar1,str,SCRIPT_MAXSSIZE); - if (sp) strlcpy(sp,str,glob_script_mem.max_ssize); + str[0] = 0; + fvar = SML_Read(fvar1, str, SCRIPT_MAXSSIZE); + if (sp) strlcpy(sp, str, glob_script_mem.max_ssize); lp++; - len=0; + len = 0; goto strexit; } else { #ifdef ED300L - fvar=SML_Status(fvar1); + fvar = SML_Status(fvar1); #else - fvar=0; -#endif + fvar = 0; +#endif //ED300L } lp++; - len=0; + len = 0; goto exit; } -#endif +#endif //USE_SML_M break; case 't': - if (!strncmp(vname,"time",4)) { - fvar=MinutesPastMidnight(); + if (!strncmp(vname, "time", 4)) { + fvar = MinutesPastMidnight(); goto exit; } - if (!strncmp(vname,"tper",4)) { - fvar=Settings.tele_period; - tind->index=SCRIPT_TELEPERIOD; + if (!strncmp(vname, "tper", 4)) { + fvar = Settings.tele_period; + tind->index = SCRIPT_TELEPERIOD; goto exit_settable; } - if (!strncmp(vname,"tinit",5)) { - if (rules_flag.time_init) { - rules_flag.time_init=0; - fvar=1; - } + if (!strncmp(vname, "tinit", 5)) { + fvar = rules_flag.time_init; goto exit; } - if (!strncmp(vname,"tset",4)) { - if (rules_flag.time_set) { - rules_flag.time_set=0; - fvar=1; - } + if (!strncmp(vname, "tset", 4)) { + fvar = rules_flag.time_set; goto exit; } - if (!strncmp(vname,"tstamp",6)) { - if (sp) strlcpy(sp,GetDateAndTime(DT_LOCAL).c_str(),glob_script_mem.max_ssize); + if (!strncmp(vname, "tstamp", 6)) { + if (sp) strlcpy(sp, GetDateAndTime(DT_LOCAL).c_str(), glob_script_mem.max_ssize); goto strexit; } - if (!strncmp(vname,"topic",5)) { - if (sp) strlcpy(sp,SettingsText(SET_MQTT_TOPIC),glob_script_mem.max_ssize); + if (!strncmp(vname, "topic", 5)) { + if (sp) strlcpy(sp, SettingsText(SET_MQTT_TOPIC), glob_script_mem.max_ssize); goto strexit; } #ifdef USE_SCRIPT_TIMER - if (!strncmp(vname,"ts1(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); - if (fvar<10) fvar=10; + if (!strncmp(vname, "ts1(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + if (fvar<10) fvar = 10; Script_ticker1.attach_ms(fvar, Script_ticker1_end); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"ts2(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); - if (fvar<10) fvar=10; + if (!strncmp(vname, "ts2(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + if (fvar<10) fvar = 10; Script_ticker2.attach_ms(fvar, Script_ticker2_end); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"ts3(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); - if (fvar<10) fvar=10; + if (!strncmp(vname, "ts3(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + if (fvar<10) fvar = 10; Script_ticker3.attach_ms(fvar, Script_ticker3_end); lp++; - len=0; + len = 0; goto exit; } - if (!strncmp(vname,"ts4(",4)) { - lp=GetNumericResult(lp+4,OPER_EQU,&fvar,0); - if (fvar<10) fvar=10; + if (!strncmp(vname, "ts4(", 4)) { + lp = GetNumericArgument(lp + 4, OPER_EQU, &fvar, 0); + if (fvar<10) fvar = 10; Script_ticker4.attach_ms(fvar, Script_ticker4_end); lp++; - len=0; + len = 0; goto exit; } #endif // USE_SCRIPT_TIMER #ifdef USE_DISPLAY #ifdef USE_TOUCH_BUTTONS - if (!strncmp(vname,"tbut[",5)) { - GetNumericResult(vname+5,OPER_EQU,&fvar,0); - uint8_t index=fvar; - if (index<1 || index>MAXBUTTONS) index=1; + if (!strncmp(vname, "tbut[", 5)) { + GetNumericArgument(vname + 5, OPER_EQU, &fvar, 0); + uint8_t index = fvar; + if (index<1 || index>MAXBUTTONS) index = 1; index--; if (buttons[index]) { - fvar=buttons[index]->vpower.on_off; + fvar = buttons[index]->vpower.on_off; } else { - fvar=-1; + fvar = -1; } - len+=1; + len += 1; goto exit; } -#endif -#endif +#endif //USE_TOUCH_BUTTONS +#endif //USE_DISPLAY break; case 'u': - if (!strncmp(vname,"uptime",6)) { - fvar=MinutesUptime(); + if (!strncmp(vname, "uptime", 6)) { + fvar = MinutesUptime(); goto exit; } - if (!strncmp(vname,"upsecs",6)) { - fvar=uptime; + if (!strncmp(vname, "upsecs", 6)) { + fvar = uptime; goto exit; } - if (!strncmp(vname,"upd[",4)) { + if (!strncmp(vname, "upd[", 4)) { // var was updated struct T_INDEX ind; uint8_t vtype; - isvar(vname+4,&vtype,&ind,0,0,0); + isvar(vname + 4, &vtype, &ind, 0, 0, 0); if (!ind.bits.constant) { if (!ind.bits.changed) { - fvar=0; + fvar = 0; len++; goto exit; } else { - glob_script_mem.type[ind.index].bits.changed=0; - fvar=1; + glob_script_mem.type[ind.index].bits.changed = 0; + fvar = 1; len++; goto exit; } @@ -2779,112 +3016,111 @@ chknext: case 'w': #if defined(ESP32) && defined(USE_WEBCAM) - if (!strncmp(vname,"wc(",3)) { - lp+=3; + if (!strncmp(vname, "wc(", 3)) { float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); + lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar1, 0); SCRIPT_SKIP_SPACES switch ((uint32)fvar1) { case 0: { float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - fvar=WcSetup(fvar2); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + fvar = WcSetup(fvar2); } break; case 1: { float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - fvar=WcGetFrame(fvar2); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + fvar = WcGetFrame(fvar2); } break; case 2: { float fvar2,fvar3; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); SCRIPT_SKIP_SPACES - lp=GetNumericResult(lp,OPER_EQU,&fvar3,0); - fvar=WcSetOptions(fvar2,fvar3); + lp = GetNumericArgument(lp, OPER_EQU, &fvar3, 0); + fvar = WcSetOptions(fvar2, fvar3); } break; case 3: - fvar=WcGetWidth(); + fvar = WcGetWidth(); break; case 4: - fvar=WcGetHeight(); + fvar = WcGetHeight(); break; case 5: { float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - fvar=WcSetStreamserver(fvar2); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + fvar = WcSetStreamserver(fvar2); } break; case 6: { float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - fvar=WcSetMotionDetect(fvar2); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + fvar = WcSetMotionDetect(fvar2); } break; #ifdef USE_FACE_DETECT case 7: { float fvar2; - lp=GetNumericResult(lp,OPER_EQU,&fvar2,0); - fvar=WcSetFaceDetect(fvar2); + lp = GetNumericArgument(lp, OPER_EQU, &fvar2, 0); + fvar = WcSetFaceDetect(fvar2); } break; -#endif +#endif //USE_FACE_DETECT default: - fvar=0; + fvar = 0; } lp++; - len=0; + len = 0; goto exit; } #endif //ESP32, USE_WEBCAM #if defined(USE_TTGO_WATCH) && defined(USE_BMA423) - if (!strncmp(vname,"wdclk",5)) { - fvar=TTGO_doubleclick(); + if (!strncmp(vname, "wdclk", 5)) { + fvar = TTGO_doubleclick(); goto exit; } - if (!strncmp(vname,"wbut",4)) { - fvar=TTGO_button(); + if (!strncmp(vname, "wbut", 4)) { + fvar = TTGO_button(); goto exit; } #endif // USE_TTGO_WATCH #if defined(USE_TTGO_WATCH) && defined(USE_FT5206) - if (!strncmp(vname,"wtch(",5)) { - lp=GetNumericResult(lp+5,OPER_EQU,&fvar,0); - fvar=Touch_Status(fvar); + if (!strncmp(vname, "wtch(", 5)) { + lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); + fvar = Touch_Status(fvar); lp++; - len=0; + len = 0; goto exit; } #endif // USE_FT5206 - if (!strncmp(vname,"wday",4)) { - fvar=RtcTime.day_of_week; + if (!strncmp(vname, "wday", 4)) { + fvar = RtcTime.day_of_week; goto exit; } - if (!strncmp(vname,"wific",5)) { + if (!strncmp(vname, "wific", 5)) { if (rules_flag.wifi_connected) { - rules_flag.wifi_connected=0; - fvar=1; + rules_flag.wifi_connected = 0; + fvar = 1; } goto exit; } - if (!strncmp(vname,"wifid",5)) { + if (!strncmp(vname, "wifid", 5)) { if (rules_flag.wifi_disconnected) { - rules_flag.wifi_disconnected=0; - fvar=1; + rules_flag.wifi_disconnected = 0; + fvar = 1; } goto exit; } - if (!strncmp(vname,"wifis",5)) { - fvar=!global_state.wifi_down; + if (!strncmp(vname, "wifis", 5)) { + fvar = !global_state.wifi_down; goto exit; } break; case 'y': - if (!strncmp(vname,"year",4)) { - fvar=RtcTime.year; + if (!strncmp(vname, "year", 4)) { + fvar = RtcTime.year; goto exit; } break; @@ -2894,23 +3130,23 @@ chknext: // nothing valid found notfound: if (fp) *fp=0; - *vtype=VAR_NV; - tind->index=VAR_NV; - glob_script_mem.var_not_found=1; + *vtype = VAR_NV; + tind->index = VAR_NV; + glob_script_mem.var_not_found = 1; return lp; // return constant numbers exit: - if (fp) *fp=fvar; - *vtype=NUM_RES; - tind->bits.constant=1; - tind->bits.is_string=0; - return lp+len; + if (fp) *fp = fvar; + *vtype = NUM_RES; + tind->bits.constant = 1; + tind->bits.is_string = 0; + return lp + len; // return constant strings strexit: - *vtype=STYPE; - tind->bits.constant=1; - tind->bits.is_string=1; - return lp+len; + *vtype = STYPE; + tind->bits.constant = 1; + tind->bits.is_string = 1; + return lp + len; } @@ -2918,113 +3154,113 @@ strexit: char *getop(char *lp, uint8_t *operand) { switch (*lp) { case '=': - if (*(lp+1)=='=') { - *operand=OPER_EQUEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_EQUEQU; + return lp + 2; } else { - *operand=OPER_EQU; - return lp+1; + *operand = OPER_EQU; + return lp + 1; } break; case '+': - if (*(lp+1)=='=') { - *operand=OPER_PLSEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_PLSEQU; + return lp + 2; } else { - *operand=OPER_PLS; - return lp+1; + *operand = OPER_PLS; + return lp + 1; } break; case '-': - if (*(lp+1)=='=') { - *operand=OPER_MINEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_MINEQU; + return lp + 2; } else { - *operand=OPER_MIN; - return lp+1; + *operand = OPER_MIN; + return lp + 1; } break; case '*': - if (*(lp+1)=='=') { - *operand=OPER_MULEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_MULEQU; + return lp + 2; } else { - *operand=OPER_MUL; - return lp+1; + *operand = OPER_MUL; + return lp + 1; } break; case '/': - if (*(lp+1)=='=') { - *operand=OPER_DIVEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_DIVEQU; + return lp + 2; } else { - *operand=OPER_DIV; - return lp+1; + *operand = OPER_DIV; + return lp + 1; } break; case '!': - if (*(lp+1)=='=') { - *operand=OPER_NOTEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_NOTEQU; + return lp + 2; } break; case '>': - if (*(lp+1)=='=') { - *operand=OPER_GRTEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_GRTEQU; + return lp + 2; } else { - *operand=OPER_GRT; - return lp+1; + *operand = OPER_GRT; + return lp + 1; } break; case '<': - if (*(lp+1)=='=') { - *operand=OPER_LOWEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_LOWEQU; + return lp + 2; } else { - *operand=OPER_LOW; - return lp+1; + *operand = OPER_LOW; + return lp + 1; } break; case '%': - if (*(lp+1)=='=') { - *operand=OPER_PERCEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_PERCEQU; + return lp + 2; } else { - *operand=OPER_PERC; - return lp+1; + *operand = OPER_PERC; + return lp + 1; } break; case '^': - if (*(lp+1)=='=') { - *operand=OPER_XOREQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_XOREQU; + return lp + 2; } else { - *operand=OPER_XOR; - return lp+1; + *operand = OPER_XOR; + return lp + 1; } break; case '&': - if (*(lp+1)=='=') { - *operand=OPER_ANDEQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_ANDEQU; + return lp + 2; } else { - *operand=OPER_AND; - return lp+1; + *operand = OPER_AND; + return lp + 1; } break; case '|': - if (*(lp+1)=='=') { - *operand=OPER_OREQU; - return lp+2; + if (*(lp + 1)=='=') { + *operand = OPER_OREQU; + return lp + 2; } else { - *operand=OPER_OR; - return lp+1; + *operand = OPER_OR; + return lp + 1; } break; } - *operand=0; + *operand = 0; return lp; } @@ -3043,31 +3279,31 @@ uint16_t GetStack(void) { register uint8_t *sp asm("a1"); return (sp - pxTaskGetStackStart(NULL)); } -#endif +#endif //ESP8266 -char *GetStringResult(char *lp,uint8_t lastop,char *cp,JsonObject *jo) { - uint8_t operand=0; +char *GetStringArgument(char *lp, uint8_t lastop, char *cp, JsonParserObject *jo) { + uint8_t operand = 0; uint8_t vtype; char *slp; struct T_INDEX ind; char str[SCRIPT_MAXSSIZE],str1[SCRIPT_MAXSSIZE]; while (1) { - lp=isvar(lp,&vtype,&ind,0,str1,jo); - if (vtype!=STR_RES && !(vtype&STYPE)) { + lp=isvar(lp, &vtype, &ind, 0, str1, jo); + if (vtype!=STR_RES && !(vtype & STYPE)) { // numeric type - glob_script_mem.glob_error=1; + glob_script_mem.glob_error = 1; return lp; } switch (lastop) { case OPER_EQU: - strlcpy(str,str1,sizeof(str)); + strlcpy(str, str1, sizeof(str)); break; case OPER_PLS: - strncat(str,str1,sizeof(str)-strlen(str1)); + strncat(str, str1, sizeof(str) - strlen(str1)); break; } - slp=lp; - lp=getop(lp,&operand); + slp = lp; + lp = getop(lp, &operand); switch (operand) { case OPER_EQUEQU: case OPER_NOTEQU: @@ -3075,24 +3311,24 @@ char *GetStringResult(char *lp,uint8_t lastop,char *cp,JsonObject *jo) { case OPER_LOWEQU: case OPER_GRT: case OPER_GRTEQU: - lp=slp; - strcpy(cp,str); + lp = slp; + strcpy(cp, str); return lp; break; default: break; } - lastop=operand; + lastop = operand; if (!operand) { - strcpy(cp,str); + strcpy(cp, str); return lp; } } return lp; } -char *GetNumericResult(char *lp,uint8_t lastop,float *fp,JsonObject *jo) { -uint8_t operand=0; +char *GetNumericArgument(char *lp, uint8_t lastop, float *fp, JsonParserObject *jo) { +uint8_t operand = 0; float fvar1,fvar; char *slp; uint8_t vtype; @@ -3101,50 +3337,50 @@ struct T_INDEX ind; // get 1. value if (*lp=='(') { lp++; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,jo); + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, jo); lp++; //if (*lp==')') lp++; } else { - lp=isvar(lp,&vtype,&ind,&fvar1,0,jo); - if (vtype!=NUM_RES && vtype&STYPE) { + lp = isvar(lp, &vtype, &ind, &fvar1, 0, jo); + if ((vtype!=NUM_RES) && (vtype&STYPE)) { // string type - glob_script_mem.glob_error=1; + glob_script_mem.glob_error = 1; } } switch (lastop) { case OPER_EQU: - fvar=fvar1; + fvar = fvar1; break; case OPER_PLS: - fvar+=fvar1; + fvar += fvar1; break; case OPER_MIN: - fvar-=fvar1; + fvar -= fvar1; break; case OPER_MUL: - fvar*=fvar1; + fvar *= fvar1; break; case OPER_DIV: - fvar/=fvar1; + fvar /= fvar1; break; case OPER_PERC: - fvar=fmodf(fvar,fvar1); + fvar = fmodf(fvar, fvar1); break; case OPER_XOR: - fvar=(uint32_t)fvar^(uint32_t)fvar1; + fvar = (uint32_t)fvar ^ (uint32_t)fvar1; break; case OPER_AND: - fvar=(uint32_t)fvar&(uint32_t)fvar1; + fvar = (uint32_t)fvar & (uint32_t)fvar1; break; case OPER_OR: - fvar=(uint32_t)fvar|(uint32_t)fvar1; + fvar = (uint32_t)fvar | (uint32_t)fvar1; break; default: break; } - slp=lp; - lp=getop(lp,&operand); + slp = lp; + lp = getop(lp, &operand); switch (operand) { case OPER_EQUEQU: case OPER_NOTEQU: @@ -3152,102 +3388,121 @@ struct T_INDEX ind; case OPER_LOWEQU: case OPER_GRT: case OPER_GRTEQU: - lp=slp; - *fp=fvar; + lp = slp; + *fp = fvar; return lp; break; default: break; } - lastop=operand; + lastop = operand; if (!operand) { - *fp=fvar; + *fp = fvar; return lp; } } } -char *ForceStringVar(char *lp,char *dstr) { +char *ForceStringVar(char *lp, char *dstr) { float fvar; - char *slp=lp; - glob_script_mem.glob_error=0; - lp=GetStringResult(lp,OPER_EQU,dstr,0); + char *slp = lp; + glob_script_mem.glob_error = 0; + lp = GetStringArgument(lp, OPER_EQU, dstr, 0); if (glob_script_mem.glob_error) { // mismatch - lp=GetNumericResult(slp,OPER_EQU,&fvar,0); - dtostrfd(fvar,6,dstr); - glob_script_mem.glob_error=0; + lp = GetNumericArgument(slp, OPER_EQU, &fvar, 0); + dtostrfd(fvar, 6, dstr); + glob_script_mem.glob_error = 0; } return lp; } // replace vars in cmd %var% -void Replace_Cmd_Vars(char *srcbuf,uint32_t srcsize, char *dstbuf,uint32_t dstsize) { +void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dstsize) { char *cp; uint16_t count; uint8_t vtype; - uint8_t dprec=glob_script_mem.script_dprec; + uint8_t dprec = glob_script_mem.script_dprec; + uint8_t lzero = glob_script_mem.script_lzero; float fvar; - cp=srcbuf; + cp = srcbuf; struct T_INDEX ind; char string[SCRIPT_MAXSSIZE]; - dstsize-=2; - for (count=0;count=sizeof(str)) len=len>=sizeof(str); - strlcpy(str,cp,len); + if (len>=sizeof(str)) len = sizeof(str); + strlcpy(str, cp, len); toSLog(str); } void toLogEOL(const char *s1,const char *str) { if (!str) return; - uint8_t index=0; - char *cp=log_data; - strcpy(cp,s1); - cp+=strlen(s1); + uint8_t index = 0; + char *cp = log_data; + strcpy(cp, s1); + cp += strlen(s1); while (*str) { if (*str==SCRIPT_EOL) break; - *cp++=*str++; + *cp++ = *str++; } - *cp=0; + *cp = 0; AddLog(LOG_LEVEL_INFO); } @@ -3297,70 +3552,70 @@ void toSLog(const char *str) { #endif } -char *Evaluate_expression(char *lp,uint8_t and_or, uint8_t *result,JsonObject *jo) { +char *Evaluate_expression(char *lp, uint8_t and_or, uint8_t *result, JsonParserObject *jo) { float fvar,*dfvar,fvar1; uint8_t numeric; struct T_INDEX ind; - uint8_t vtype=0,lastop; - uint8_t res=0; - char *llp=lp; + uint8_t vtype = 0,lastop; + uint8_t res = 0; + char *llp = lp; char *slp; SCRIPT_SKIP_SPACES if (*lp=='(') { - uint8_t res=0; - uint8_t xand_or=0; + uint8_t res = 0; + uint8_t xand_or = 0; lp++; loop: SCRIPT_SKIP_SPACES - lp=Evaluate_expression(lp,xand_or,&res,jo); + lp = Evaluate_expression(lp, xand_or, &res, jo); if (*lp==')') { lp++; goto exit0; } // check for next and or SCRIPT_SKIP_SPACES - if (!strncmp(lp,"or",2)) { - lp+=2; - xand_or=1; + if (!strncmp(lp, "or", 2)) { + lp += 2; + xand_or = 1; goto loop; - } else if (!strncmp(lp,"and",3)) { - lp+=3; - xand_or=2; + } else if (!strncmp(lp, "and", 3)) { + lp += 3; + xand_or = 2; goto loop; } exit0: if (!and_or) { - *result=res; + *result = res; } else if (and_or==1) { *result|=res; } else { - *result&=res; + *result &= res; } goto exit10; } - llp=lp; + llp = lp; // compare - dfvar=&fvar; - glob_script_mem.glob_error=0; - slp=lp; - numeric=1; - lp=GetNumericResult(lp,OPER_EQU,dfvar,0); + dfvar = &fvar; + glob_script_mem.glob_error = 0; + slp = lp; + numeric = 1; + lp = GetNumericArgument(lp, OPER_EQU, dfvar, 0); if (glob_script_mem.glob_error==1) { // was string, not number char cmpstr[SCRIPT_MAXSSIZE]; - lp=slp; - numeric=0; + lp = slp; + numeric = 0; // get the string - lp=isvar(lp,&vtype,&ind,0,cmpstr,0); - lp=getop(lp,&lastop); + lp = isvar(lp, &vtype, &ind, 0, cmpstr, 0); + lp = getop(lp, &lastop); // compare string char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,jo); + lp = GetStringArgument(lp, OPER_EQU, str, jo); if (lastop==OPER_EQUEQU || lastop==OPER_NOTEQU) { - res=strcmp(cmpstr,str); + res = strcmp(cmpstr, str); if (lastop==OPER_EQUEQU) res=!res; goto exit; } @@ -3368,26 +3623,26 @@ exit0: } else { // numeric // evaluate operand - lp=getop(lp,&lastop); - lp=GetNumericResult(lp,OPER_EQU,&fvar1,jo); + lp = getop(lp, &lastop); + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, jo); switch (lastop) { case OPER_EQUEQU: - res=(*dfvar==fvar1); + res = (*dfvar==fvar1); break; case OPER_NOTEQU: - res=(*dfvar!=fvar1); + res = (*dfvar!=fvar1); break; case OPER_LOW: - res=(*dfvarfvar1); + res = (*dfvar>fvar1); break; case OPER_GRTEQU: - res=(*dfvar>=fvar1); + res = (*dfvar>=fvar1); break; default: // error @@ -3396,11 +3651,11 @@ exit0: exit: if (!and_or) { - *result=res; + *result = res; } else if (and_or==1) { - *result|=res; + *result |= res; } else { - *result&=res; + *result &= res; } } @@ -3408,8 +3663,8 @@ exit: exit10: #if IFTHEN_DEBUG>0 char tbuff[128]; - sprintf(tbuff,"p1=%d,p2=%d,cmpres=%d,and_or=%d line: ",(int32_t)*dfvar,(int32_t)fvar1,*result,and_or); - toLogEOL(tbuff,llp); + sprintf(tbuff,"p1=%d,p2=%d,cmpres=%d,and_or=%d line: ", (int32_t)*dfvar, (int32_t)fvar1, *result, and_or); + toLogEOL(tbuff, llp); #endif return lp; } @@ -3420,28 +3675,28 @@ TimerHandle_t beep_th; void StopBeep( TimerHandle_t xTimer ); void StopBeep( TimerHandle_t xTimer ) { - ledcWriteTone(7,0); + ledcWriteTone(7, 0); xTimerStop(xTimer, 0); } void esp32_beep(int32_t freq ,uint32_t len) { if (freq<0) { - ledcSetup(7,500,10); - ledcAttachPin(-freq,7); - ledcWriteTone(7,0); + ledcSetup(7, 500, 10); + ledcAttachPin(-freq, 7); + ledcWriteTone(7, 0); if (!beep_th) { - beep_th = xTimerCreate("beep",100,pdFALSE,( void * ) 0,StopBeep); + beep_th = xTimerCreate("beep", 100, pdFALSE, ( void * ) 0, StopBeep); } } else { if (!beep_th) return; if (!freq) { - ledcWriteTone(7,0); + ledcWriteTone(7, 0); xTimerStop(beep_th, 10); return; } if (len < 10) return; if (xTimerIsTimerActive(beep_th)) return; - ledcWriteTone(7,freq); + ledcWriteTone(7, freq); uint32_t ticks = pdMS_TO_TICKS(len); xTimerChangePeriod( beep_th, ticks, 10); } @@ -3460,40 +3715,38 @@ int16_t Run_Scripter(const char *type, int8_t tlen, char *js) { if (tasm_cmd_activ && tlen>0) return 0; - JsonObject *jo=0; - DynamicJsonBuffer jsonBuffer; // on heap - JsonObject &jobj=jsonBuffer.parseObject(js); + JsonParserObject jo; if (js) { - jo=&jobj; - } else { - jo=0; + String jss = js; // copy the string to a new buffer, not sure we can change the original buffer + JsonParser parser((char*)jss.c_str()); + jo = parser.getRootObject(); } - return Run_script_sub(type, tlen, jo); + return Run_script_sub(type, tlen, &jo); } -int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { +int16_t Run_script_sub(const char *type, int8_t tlen, JsonParserObject *jo) { uint8_t vtype=0,sindex,xflg,floop=0,globvindex,fromscriptcmd=0; char *lp_next; int16_t globaindex,saindex; struct T_INDEX ind; - uint8_t operand,lastop,numeric=1,if_state[IF_NEST],if_exe[IF_NEST],if_result[IF_NEST],and_or,ifstck=0; - if_state[ifstck]=0; - if_result[ifstck]=0; - if_exe[ifstck]=1; + uint8_t operand,lastop,numeric = 1,if_state[IF_NEST],if_exe[IF_NEST],if_result[IF_NEST],and_or,ifstck = 0; + if_state[ifstck] = 0; + if_result[ifstck] = 0; + if_exe[ifstck] = 1; char cmpstr[SCRIPT_MAXSSIZE]; - uint8_t check=0; + uint8_t check = 0; if (tlen<0) { - tlen=abs(tlen); - check=1; + tlen = abs(tlen); + check = 1; } float *dfvar,*cv_count,cv_max,cv_inc; char *cv_ptr; - float fvar=0,fvar1,sysvar,swvar; - uint8_t section=0,sysv_type=0,swflg=0; + float fvar = 0,fvar1,sysvar,swvar; + uint8_t section = 0,sysv_type = 0,swflg = 0; - char *lp=glob_script_mem.scriptptr; + char *lp = glob_script_mem.scriptptr; while (1) { // check line @@ -3514,71 +3767,71 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { if (*lp=='#') { return 0; } - glob_script_mem.var_not_found=0; + glob_script_mem.var_not_found = 0; //#if SCRIPT_DEBUG>0 #ifdef IFTHEN_DEBUG char tbuff[128]; - sprintf(tbuff,"stack=%d,exe=%d,state=%d,cmpres=%d line: ",ifstck,if_exe[ifstck],if_state[ifstck],if_result[ifstck]); - toLogEOL(tbuff,lp); -#endif + sprintf(tbuff, "stack=%d,exe=%d,state=%d,cmpres=%d line: ", ifstck, if_exe[ifstck], if_state[ifstck], if_result[ifstck]); + toLogEOL(tbuff, lp); +#endif //IFTHEN_DEBUG //if (if_state[s_ifstck]==3 && if_result[s_ifstck]) goto next_line; //if (if_state[s_ifstck]==2 && !if_result[s_ifstck]) goto next_line; - if (!strncmp(lp,"if",2)) { - lp+=2; + if (!strncmp(lp, "if", 2)) { + lp += 2; if (ifstck=2) { - lp+=5; + if_state[ifstck] = 1; + if_result[ifstck] = 0; + if (ifstck==1) if_exe[ifstck] = 1; + else if_exe[ifstck] = if_exe[ifstck - 1]; + and_or = 0; + } else if (!strncmp(lp, "then", 4) && if_state[ifstck]==1) { + lp += 4; + if_state[ifstck] = 2; + if (if_exe[ifstck - 1]) if_exe[ifstck] = if_result[ifstck]; + } else if (!strncmp(lp, "else", 4) && if_state[ifstck]==2) { + lp += 4; + if_state[ifstck] = 3; + if (if_exe[ifstck - 1]) if_exe[ifstck] = !if_result[ifstck]; + } else if (!strncmp(lp, "endif", 5) && if_state[ifstck]>=2) { + lp += 5; if (ifstck>0) { - if_state[ifstck]=0; + if_state[ifstck] = 0; ifstck--; } goto next_line; - } else if (!strncmp(lp,"or",2) && if_state[ifstck]==1) { - lp+=2; - and_or=1; - } else if (!strncmp(lp,"and",3) && if_state[ifstck]==1) { - lp+=3; - and_or=2; + } else if (!strncmp(lp, "or", 2) && if_state[ifstck]==1) { + lp += 2; + and_or = 1; + } else if (!strncmp(lp, "and", 3) && if_state[ifstck]==1) { + lp += 3; + and_or = 2; } if (*lp=='{' && if_state[ifstck]==1) { - lp+=1; // then - if_state[ifstck]=2; - if (if_exe[ifstck-1]) if_exe[ifstck]=if_result[ifstck]; + lp += 1; // then + if_state[ifstck] = 2; + if (if_exe[ifstck - 1]) if_exe[ifstck]=if_result[ifstck]; } else if (*lp=='{' && if_state[ifstck]==3) { - lp+=1; // after else + lp += 1; // after else //if_state[ifstck]=3; } else if (*lp=='}' && if_state[ifstck]>=2) { lp++; // must check for else - char *slp=lp; - uint8_t iselse=0; - for (uint8_t count=0; count<8;count++) { + char *slp = lp; + uint8_t iselse = 0; + for (uint8_t count = 0; count<8;count++) { if (*lp=='}') { // must be endif break; } - if (!strncmp(lp,"else",4)) { + if (!strncmp(lp, "else", 4)) { // is before else, no endif - if_state[ifstck]=3; + if_state[ifstck] = 3; if (if_exe[ifstck-1]) if_exe[ifstck]=!if_result[ifstck]; - lp+=4; - iselse=1; + lp += 4; + iselse = 1; SCRIPT_SKIP_SPACES if (*lp=='{') lp++; break; @@ -3586,111 +3839,111 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { lp++; } if (!iselse) { - lp=slp; + lp = slp; // endif if (ifstck>0) { - if_state[ifstck]=0; + if_state[ifstck] = 0; ifstck--; } goto next_line; } } - if (!strncmp(lp,"for",3)) { + if (!strncmp(lp, "for", 3)) { // start for next loop, fetch 3 params // simple implementation, zero loop count not supported - lp+=3; + lp += 3; SCRIPT_SKIP_SPACES - lp_next=0; - lp=isvar(lp,&vtype,&ind,0,0,0); + lp_next = 0; + lp = isvar(lp, &vtype, &ind, 0, 0, 0); if ((vtype!=VAR_NV) && (vtype&STYPE)==0) { // numeric var - uint8_t index=glob_script_mem.type[ind.index].index; - cv_count=&glob_script_mem.fvars[index]; + uint8_t index = glob_script_mem.type[ind.index].index; + cv_count = &glob_script_mem.fvars[index]; SCRIPT_SKIP_SPACES - lp=GetNumericResult(lp,OPER_EQU,cv_count,0); + lp = GetNumericArgument(lp, OPER_EQU, cv_count, 0); SCRIPT_SKIP_SPACES - lp=GetNumericResult(lp,OPER_EQU,&cv_max,0); + lp = GetNumericArgument(lp, OPER_EQU, &cv_max, 0); SCRIPT_SKIP_SPACES - lp=GetNumericResult(lp,OPER_EQU,&cv_inc,0); + lp = GetNumericArgument(lp, OPER_EQU, &cv_inc, 0); //SCRIPT_SKIP_EOL - cv_ptr=lp; + cv_ptr = lp; if (*cv_count<=cv_max && cv_inc>0) { // inc loop - floop=1; + floop = 1; } else { // dec loop - floop=2; + floop = 2; if (cv_inc>0) { - floop=1; + floop = 1; } } } else { // error - toLogEOL("for error",lp); + toLogEOL("for error", lp); } - } else if (!strncmp(lp,"next",4)) { - lp_next=lp; + } else if (!strncmp(lp, "next", 4)) { + lp_next = lp; if (floop>0) { // for next loop - *cv_count+=cv_inc; + *cv_count += cv_inc; if (floop==1) { if (*cv_count<=cv_max) { - lp=cv_ptr; + lp = cv_ptr; } else { - lp+=4; - floop=0; + lp += 4; + floop = 0; } } else { if (*cv_count>=cv_max) { - lp=cv_ptr; + lp = cv_ptr; } else { - lp+=4; - floop=0; + lp += 4; + floop = 0; } } } } - if (!strncmp(lp,"switch",6)) { - lp+=6; + if (!strncmp(lp, "switch", 6)) { + lp += 6; SCRIPT_SKIP_SPACES - char *slp=lp; - lp=GetNumericResult(lp,OPER_EQU,&swvar,0); + char *slp = lp; + lp = GetNumericArgument(lp, OPER_EQU, &swvar, 0); if (glob_script_mem.glob_error==1) { // was string, not number - lp=slp; + lp = slp; // get the string - lp=isvar(lp,&vtype,&ind,0,cmpstr,0); - swflg=0x81; + lp = isvar(lp, &vtype, &ind, 0, cmpstr, 0); + swflg = 0x81; } else { - swflg=1; + swflg = 1; } - } else if (!strncmp(lp,"case",4) && swflg>0) { - lp+=4; + } else if (!strncmp(lp, "case", 4) && swflg>0) { + lp += 4; SCRIPT_SKIP_SPACES float cvar; - if (!(swflg&0x80)) { - lp=GetNumericResult(lp,OPER_EQU,&cvar,0); + if (!(swflg & 0x80)) { + lp = GetNumericArgument(lp, OPER_EQU, &cvar, 0); if (swvar!=cvar) { - swflg=2; + swflg = 2; } else { - swflg=1; + swflg = 1; } } else { char str[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,str,0); - if (!strcmp(cmpstr,str)) { - swflg=0x81; + lp = GetStringArgument(lp, OPER_EQU, str, 0); + if (!strcmp(cmpstr, str)) { + swflg = 0x81; } else { - swflg=0x82; + swflg = 0x82; } } - } else if (!strncmp(lp,"ends",4) && swflg>0) { - lp+=4; - swflg=0; + } else if (!strncmp(lp, "ends", 4) && swflg>0) { + lp += 4; + swflg = 0; } - if ((swflg&3)==2) goto next_line; + if ((swflg & 3)==2) goto next_line; SCRIPT_SKIP_SPACES //SCRIPT_SKIP_EOL @@ -3702,34 +3955,38 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { if (!if_exe[ifstck] && if_state[ifstck]!=1) goto next_line; #ifdef IFTHEN_DEBUG - sprintf(tbuff,"stack=%d,exe=%d,state=%d,cmpres=%d execute line: ",ifstck,if_exe[ifstck],if_state[ifstck],if_result[ifstck]); - toLogEOL(tbuff,lp); -#endif + sprintf(tbuff, "stack=%d,exe=%d,state=%d,cmpres=%d execute line: ", ifstck, if_exe[ifstck], if_state[ifstck], if_result[ifstck]); + toLogEOL(tbuff, lp); +#endif //IFTHEN_DEBUG - if (!strncmp(lp,"break",5)) { - lp+=5; + if (!strncmp(lp, "break", 5)) { + lp += 5; if (floop) { // should break loop if (lp_next) { - lp=lp_next; + lp = lp_next; } - floop=0; + floop = 0; } else { - section=0; + section = 0; } goto next_line; - } else if (!strncmp(lp,"dp",2) && isdigit(*(lp+2))) { - lp+=2; + } else if (!strncmp(lp, "dp", 2) && isdigit(*(lp + 2))) { + lp += 2; // number precision - glob_script_mem.script_dprec=atoi(lp); + if (*(lp + 1)== '.') { + glob_script_mem.script_lzero = atoi(lp); + lp+=2; + } + glob_script_mem.script_dprec = atoi(lp); goto next_line; } #ifdef USE_DISPLAY - else if (!strncmp(lp,"dt",2)) { + else if (!strncmp(lp, "dt", 2)) { char dstbuf[256]; - lp+=2; + lp += 2; SCRIPT_SKIP_SPACES - Replace_Cmd_Vars(lp,1,dstbuf,sizeof(dstbuf)); + Replace_Cmd_Vars(lp, 1, dstbuf, sizeof(dstbuf)); char *savptr = XdrvMailbox.data; XdrvMailbox.data = dstbuf; XdrvMailbox.data_len = 0; @@ -3737,165 +3994,160 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { XdrvMailbox.data = savptr; goto next_line; } -#endif - else if (!strncmp(lp,"delay(",6)) { - lp+=5; +#endif //USE_DISPLAY + else if (!strncmp(lp, "delay(", 6)) { // delay - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); delay(fvar); goto next_line; - } else if (!strncmp(lp,"spinm(",6)) { - lp+=6; + } else if (!strncmp(lp, "spinm(", 6)) { // set pin mode - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - int8_t pinnr=fvar; + lp = GetNumericArgument(lp + 6, OPER_EQU, &fvar, 0); + int8_t pinnr = fvar; SCRIPT_SKIP_SPACES - uint8_t mode=0; + uint8_t mode = 0; if ((*lp=='I') || (*lp=='O') || (*lp=='P')) { switch (*lp) { case 'I': - mode=0; + mode = 0; break; case 'O': - mode=1; + mode = 1; break; case 'P': - mode=2; + mode = 2; break; } lp++; } else { - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - mode=fvar; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + mode = fvar; } uint8_t pm=0; - if (mode==0) pm=INPUT; - if (mode==1) pm=OUTPUT; - if (mode==2) pm=INPUT_PULLUP; - pinMode(pinnr,pm); + if (mode==0) pm = INPUT; + if (mode==1) pm = OUTPUT; + if (mode==2) pm = INPUT_PULLUP; + pinMode(pinnr, pm); goto next_line; - } else if (!strncmp(lp,"spin(",5)) { - lp+=5; + } else if (!strncmp(lp, "spin(", 5)) { // set pin - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - int8_t pinnr=fvar; + lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); + int8_t pinnr = fvar; SCRIPT_SKIP_SPACES - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); - int8_t mode=fvar; - digitalWrite(pinnr,mode&1); + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); + int8_t mode = fvar; + digitalWrite(pinnr, mode & 1); goto next_line; - } else if (!strncmp(lp,"svars(",5)) { - lp+=5; + } else if (!strncmp(lp, "svars(", 5)) { + lp += 5; // save vars Scripter_save_pvars(); goto next_line; } #ifdef USE_LIGHT #ifdef USE_WS2812 - else if (!strncmp(lp,"ws2812(",7)) { - lp+=7; - lp=isvar(lp,&vtype,&ind,0,0,0); + else if (!strncmp(lp, "ws2812(", 7)) { + lp = isvar(lp + 7, &vtype, &ind, 0, 0, 0); if (vtype!=VAR_NV) { SCRIPT_SKIP_SPACES if (*lp!=')') { - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); } else { - fvar=0; + fvar = 0; } // found variable as result - uint8_t index=glob_script_mem.type[ind.index].index; + uint8_t index = glob_script_mem.type[ind.index].index; if ((vtype&STYPE)==0) { // numeric result if (glob_script_mem.type[ind.index].bits.is_filter) { - uint16_t len=0; - float *fa=Get_MFAddr(index,&len,0); + uint16_t len = 0; + float *fa = Get_MFAddr(index, &len, 0); //Serial.printf(">> 2 %d\n",(uint32_t)*fa); - if (fa && len) ws2812_set_array(fa,len,fvar); + if (fa && len) ws2812_set_array(fa, len, fvar); } } } goto next_line; } -#endif -#endif +#endif //USE_WS2812 +#endif //USE_LIGHT #ifdef ESP32 - else if (!strncmp(lp,"beep(",5)) { - lp+=5; - lp=GetNumericResult(lp,OPER_EQU,&fvar,0); + else if (!strncmp(lp, "beep(", 5)) { + lp = GetNumericArgument(lp + 5, OPER_EQU, &fvar, 0); SCRIPT_SKIP_SPACES float fvar1; - lp=GetNumericResult(lp,OPER_EQU,&fvar1,0); - esp32_beep(fvar,fvar1); + lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0); + esp32_beep(fvar, fvar1); lp++; goto next_line; } -#endif +#endif //ESP32 else if (!strncmp(lp,"=>",2) || !strncmp(lp,"->",2) || !strncmp(lp,"+>",2) || !strncmp(lp,"print",5)) { // execute cmd - uint8_t sflag=0,pflg=0,svmqtt,swll; + uint8_t sflag = 0,pflg = 0,svmqtt,swll; if (*lp=='p') { - pflg=1; - lp+=5; + pflg = 1; + lp += 5; } else { - if (*lp=='-') sflag=1; - if (*lp=='+') sflag=2; - lp+=2; + if (*lp=='-') sflag = 1; + if (*lp=='+') sflag = 2; + lp += 2; } - char *slp=lp; + char *slp = lp; SCRIPT_SKIP_SPACES #define SCRIPT_CMDMEM 512 - char *cmdmem=(char*)malloc(SCRIPT_CMDMEM); + char *cmdmem = (char*)malloc(SCRIPT_CMDMEM); if (cmdmem) { - char *cmd=cmdmem; + char *cmd = cmdmem; uint16_t count; - for (count=0; count",1,jo); - glob_script_mem.scriptptr=svd_sp; + char *svd_sp = glob_script_mem.scriptptr; + strcat(str, "\n#"); + glob_script_mem.scriptptr = str; + Run_script_sub(">", 1, jo); + glob_script_mem.scriptptr = svd_sp; } // check for variable result if (if_state[ifstck]==1) { // evaluate exxpression - lp=Evaluate_expression(lp,and_or,&if_result[ifstck],jo); + lp = Evaluate_expression(lp, and_or, &if_result[ifstck], jo); SCRIPT_SKIP_SPACES if (*lp=='{' && if_state[ifstck]==1) { - lp+=1; // then - if_state[ifstck]=2; - if (if_exe[ifstck-1]) if_exe[ifstck]=if_result[ifstck]; + lp += 1; // then + if_state[ifstck] = 2; + if (if_exe[ifstck - 1]) if_exe[ifstck] = if_result[ifstck]; } goto next_line; } else { - char *vnp=lp; - lp=isvar(lp,&vtype,&ind,&sysvar,0,0); + char *vnp = lp; + lp = isvar(lp, &vtype, &ind, &sysvar, 0, 0); if (vtype!=VAR_NV) { #ifdef USE_SCRIPT_GLOBVARS char varname[16]; - uint32_t vnl=(uint32_t)lp-(uint32)vnp; - strncpy(varname,vnp,vnl); - varname[vnl]=0; -#endif + uint32_t vnl = (uint32_t)lp - (uint32)vnp; + strncpy(varname, vnp, vnl); + varname[vnl] = 0; +#endif //USE_SCRIPT_GLOBVARS // found variable as result - globvindex=ind.index; // save destination var index here - globaindex=last_findex; - uint8_t index=glob_script_mem.type[ind.index].index; + globvindex = ind.index; // save destination var index here + globaindex = last_findex; + uint8_t index = glob_script_mem.type[ind.index].index; if ((vtype&STYPE)==0) { // numeric result if (ind.bits.settable || ind.bits.is_filter) { - dfvar=&sysvar; + dfvar = &sysvar; if (ind.bits.settable) { - sysv_type=ind.index; + sysv_type = ind.index; } else { - sysv_type=0; + sysv_type = 0; } } else { - dfvar=&glob_script_mem.fvars[index]; - sysv_type=0; + dfvar = &glob_script_mem.fvars[index]; + sysv_type = 0; } - numeric=1; - lp=getop(lp,&lastop); - char *slp=lp; - glob_script_mem.glob_error=0; - lp=GetNumericResult(lp,OPER_EQU,&fvar,jo); + numeric = 1; + lp = getop(lp, &lastop); + char *slp = lp; + glob_script_mem.glob_error = 0; + lp = GetNumericArgument(lp, OPER_EQU, &fvar, jo); if (glob_script_mem.glob_error==1) { // mismatch was string, not number // get the string and convert to number - lp=isvar(slp,&vtype,&ind,0,cmpstr,jo); - fvar=CharToFloat(cmpstr); + lp = isvar(slp, &vtype, &ind, 0, cmpstr, jo); + fvar = CharToFloat(cmpstr); } switch (lastop) { case OPER_EQU: @@ -3984,105 +4236,105 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { if (!jo) toLogEOL("var not found: ",lp); goto next_line; } - *dfvar=fvar; + *dfvar = fvar; break; case OPER_PLSEQU: - *dfvar+=fvar; + *dfvar += fvar; break; case OPER_MINEQU: - *dfvar-=fvar; + *dfvar -= fvar; break; case OPER_MULEQU: - *dfvar*=fvar; + *dfvar *= fvar; break; case OPER_DIVEQU: - *dfvar/=fvar; + *dfvar /= fvar; break; case OPER_PERCEQU: - *dfvar=fmodf(*dfvar,fvar); + *dfvar = fmodf(*dfvar, fvar); break; case OPER_ANDEQU: - *dfvar=(uint32_t)*dfvar&(uint32_t)fvar; + *dfvar = (uint32_t)*dfvar & (uint32_t)fvar; break; case OPER_OREQU: - *dfvar=(uint32_t)*dfvar|(uint32_t)fvar; + *dfvar = (uint32_t)*dfvar | (uint32_t)fvar; break; case OPER_XOREQU: - *dfvar=(uint32_t)*dfvar^(uint32_t)fvar; + *dfvar = (uint32_t)*dfvar ^ (uint32_t)fvar; break; default: // error break; } // var was changed - glob_script_mem.type[globvindex].bits.changed=1; + glob_script_mem.type[globvindex].bits.changed = 1; #ifdef USE_SCRIPT_GLOBVARS if (glob_script_mem.type[globvindex].bits.global) { - script_udp_sendvar(varname,dfvar,0); + script_udp_sendvar(varname, dfvar, 0); } -#endif +#endif //USE_SCRIPT_GLOBVARS if (glob_script_mem.type[globvindex].bits.is_filter) { if (globaindex>=0) { - Set_MFVal(glob_script_mem.type[globvindex].index,globaindex,*dfvar); + Set_MFVal(glob_script_mem.type[globvindex].index, globaindex, *dfvar); } else { - Set_MFilter(glob_script_mem.type[globvindex].index,*dfvar); + Set_MFilter(glob_script_mem.type[globvindex].index, *dfvar); } } if (sysv_type) { switch (sysv_type) { case SCRIPT_LOGLEVEL: - glob_script_mem.script_loglevel=*dfvar; + glob_script_mem.script_loglevel = *dfvar; break; case SCRIPT_TELEPERIOD: - if (*dfvar<10) *dfvar=10; - if (*dfvar>300) *dfvar=300; - Settings.tele_period=*dfvar; + if (*dfvar<10) *dfvar = 10; + if (*dfvar>300) *dfvar = 300; + Settings.tele_period = *dfvar; break; case SCRIPT_EVENT_HANDLED: - event_handeled=*dfvar; + event_handeled = *dfvar; break; } - sysv_type=0; + sysv_type = 0; } } else { // string result - numeric=0; - sindex=index; - saindex=last_sindex; + numeric = 0; + sindex = index; + saindex = last_sindex; // string result char str[SCRIPT_MAXSSIZE]; - lp=getop(lp,&lastop); - char *slp=lp; - glob_script_mem.glob_error=0; - lp=GetStringResult(lp,OPER_EQU,str,jo); + lp = getop(lp, &lastop); + char *slp = lp; + glob_script_mem.glob_error = 0; + lp = GetStringArgument(lp, OPER_EQU, str, jo); if (!jo && glob_script_mem.glob_error) { // mismatch - lp=GetNumericResult(slp,OPER_EQU,&fvar,0); - dtostrfd(fvar,6,str); - glob_script_mem.glob_error=0; + lp = GetNumericArgument(slp, OPER_EQU, &fvar, 0); + dtostrfd(fvar, 6, str); + glob_script_mem.glob_error = 0; } if (!glob_script_mem.var_not_found) { // var was changed - glob_script_mem.type[globvindex].bits.changed=1; + glob_script_mem.type[globvindex].bits.changed = 1; #ifdef USE_SCRIPT_GLOBVARS if (glob_script_mem.type[globvindex].bits.global) { - script_udp_sendvar(varname,0,str); + script_udp_sendvar(varname, 0, str); } -#endif +#endif //USE_SCRIPT_GLOBVARS if (saindex>=0) { if (lastop==OPER_EQU) { - strlcpy(glob_script_mem.last_index_string+(saindex*glob_script_mem.max_ssize),str,glob_script_mem.max_ssize); + strlcpy(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); } else if (lastop==OPER_PLSEQU) { - strncat(glob_script_mem.last_index_string+(saindex*glob_script_mem.max_ssize),str,glob_script_mem.max_ssize); + strncat(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); } - last_sindex=-1; + last_sindex = -1; } else { if (lastop==OPER_EQU) { - strlcpy(glob_script_mem.glob_snp+(sindex*glob_script_mem.max_ssize),str,glob_script_mem.max_ssize); + strlcpy(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); } else if (lastop==OPER_PLSEQU) { - strncat(glob_script_mem.glob_snp+(sindex*glob_script_mem.max_ssize),str,glob_script_mem.max_ssize); + strncat(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); } } } @@ -4091,7 +4343,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { } SCRIPT_SKIP_SPACES if (*lp=='{' && if_state[ifstck]==3) { - lp+=1; // else + lp += 1; // else //if_state[ifstck]=3; } goto next_line; @@ -4102,67 +4354,67 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { if (*lp=='>' && tlen==1) { // called from cmdline lp++; - section=1; - fromscriptcmd=1; + section = 1; + fromscriptcmd = 1; goto startline; } - if (!strncmp(lp,type,tlen)) { + if (!strncmp(lp, type, tlen)) { // found section - section=1; - glob_script_mem.section_ptr=lp; + section = 1; + glob_script_mem.section_ptr = lp; if (check) { return 99; } // check for subroutine - char *ctype=(char*)type; + char *ctype = (char*)type; if (*ctype=='#') { // check for parameter - ctype+=tlen; + ctype += tlen; if (*ctype=='(' && *(lp+tlen)=='(') { float fparam; - numeric=1; - glob_script_mem.glob_error=0; - GetNumericResult((char*)ctype,OPER_EQU,&fparam,0); + numeric = 1; + glob_script_mem.glob_error = 0; + GetNumericArgument((char*)ctype, OPER_EQU, &fparam, 0); if (glob_script_mem.glob_error==1) { // was string, not number - numeric=0; + numeric = 0; // get the string - GetStringResult((char*)ctype+1,OPER_EQU,cmpstr,0); + GetStringArgument((char*)ctype + 1, OPER_EQU, cmpstr, 0); } - lp+=tlen; + lp += tlen; if (*lp=='(') { // fetch destination lp++; - lp=isvar(lp,&vtype,&ind,0,0,0); + lp = isvar(lp, &vtype, &ind, 0, 0, 0); if (vtype!=VAR_NV) { // found variable as result - uint8_t index=glob_script_mem.type[ind.index].index; + uint8_t index = glob_script_mem.type[ind.index].index; if ((vtype&STYPE)==0) { // numeric result - dfvar=&glob_script_mem.fvars[index]; + dfvar = &glob_script_mem.fvars[index]; if (numeric) { - *dfvar=fparam; + *dfvar = fparam; } else { // mismatch - *dfvar=CharToFloat(cmpstr); + *dfvar = CharToFloat(cmpstr); } } else { // string result - sindex=index; + sindex = index; if (!numeric) { - strlcpy(glob_script_mem.glob_snp+(sindex*glob_script_mem.max_ssize),cmpstr,glob_script_mem.max_ssize); + strlcpy(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), cmpstr, glob_script_mem.max_ssize); } else { // mismatch - dtostrfd(fparam,6,glob_script_mem.glob_snp+(sindex*glob_script_mem.max_ssize)); + dtostrfd(fparam, 6, glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize)); } } } } } else { - lp+=tlen; + lp += tlen; if (*ctype=='(' || (*lp!=SCRIPT_EOL && *lp!='?')) { // revert - section=0; + section = 0; } } } @@ -4201,11 +4453,11 @@ void ScripterEvery100ms(void) { if (strlen(mqtt_data)) { mqtt_data[0] = '{'; snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data); - Run_Scripter(">T",2, mqtt_data); + Run_Scripter(">T", 2, mqtt_data); } } if (Settings.rule_enabled) { - if (fast_script==99) Run_Scripter(">F",2,0); + if (fast_script==99) Run_Scripter(">F", 2, 0); } } @@ -4213,48 +4465,48 @@ void ScripterEvery100ms(void) { // can hold 11 floats or floats + strings // should report overflow later void Scripter_save_pvars(void) { - int16_t mlen=0; - float *fp=(float*)glob_script_mem.script_pram; + int16_t mlen = 0; + float *fp = (float*)glob_script_mem.script_pram; mlen+=sizeof(float); - struct T_INDEX *vtp=glob_script_mem.type; - for (uint8_t count=0; countglob_script_mem.script_pram_size) { - vtp[count].bits.is_permanent=0; + vtp[count].bits.is_permanent = 0; return; } while (len--) { - *fp++=*fa++; + *fp++ = *fa++; } } else { - mlen+=sizeof(float); + mlen += sizeof(float); if (mlen>glob_script_mem.script_pram_size) { - vtp[count].bits.is_permanent=0; + vtp[count].bits.is_permanent = 0; return; } - *fp++=glob_script_mem.fvars[index]; + *fp++ = glob_script_mem.fvars[index]; } } } - char *cp=(char*)fp; - for (uint8_t count=0; countglob_script_mem.script_pram_size) { - vtp[count].bits.is_permanent=0; + vtp[count].bits.is_permanent = 0; return; } - strcpy(cp,sp); - cp+=slen+1; + strcpy(cp, sp); + cp += slen + 1; } } } @@ -4351,7 +4603,7 @@ const char HTTP_FORM_SCRIPT1b[] PROGMEM = "});" -#endif +#endif //SCRIPT_STRIP_COMMENTS ""; @@ -4381,8 +4633,8 @@ const char HTTP_FORM_FILE_UPLOAD[] PROGMEM = "
" "
 %s" " "; const char HTTP_FORM_FILE_UPG[] PROGMEM = -"
" -"

" +"" +"

" "
"; const char HTTP_FORM_FILE_UPGb[] PROGMEM = @@ -4390,6 +4642,9 @@ const char HTTP_FORM_FILE_UPGb[] PROGMEM = "
" ""; +const char HTTP_FORM_FILE_UPGc[] PROGMEM = +"
total size: %s kB - free: %s kB
"; + const char HTTP_FORM_SDC_DIRa[] PROGMEM = "
"; const char HTTP_FORM_SDC_DIRb[] PROGMEM = @@ -4415,38 +4670,38 @@ void script_upload_start(void) { HTTPUpload& upload = Webserver->upload(); if (upload.status == UPLOAD_FILE_START) { //AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload start")); - script_ex_ptr=(uint8_t*)glob_script_mem.script_ram; + script_ex_ptr = (uint8_t*)glob_script_mem.script_ram; //AddLog_P2(LOG_LEVEL_INFO, PSTR("HTP: upload file %s, %d"),upload.filename.c_str(),upload.totalSize); - if (strcmp(upload.filename.c_str(),"execute_script")) { - Web.upload_error=1; + if (strcmp(upload.filename.c_str(), "execute_script")) { + Web.upload_error = 1; WSSend(500, CT_PLAIN, F("500: wrong filename")); return; } if (upload.totalSize>=glob_script_mem.script_size) { - Web.upload_error=1; + Web.upload_error = 1; WSSend(500, CT_PLAIN, F("500: file to large")); return; } - uplsize=0; + uplsize = 0; sc_state = bitRead(Settings.rule_enabled, 0); - bitWrite(Settings.rule_enabled,0,0); + bitWrite(Settings.rule_enabled, 0, 0); } else if(upload.status == UPLOAD_FILE_WRITE) { //AddLog_P(LOG_LEVEL_INFO, PSTR("HTP: upload write")); - uint32_t csiz=upload.currentSize; - uint32_t tsiz=glob_script_mem.script_size-1; + uint32_t csiz = upload.currentSize; + uint32_t tsiz = glob_script_mem.script_size - 1; if (uplsizeopen(path, FILE_READ); + File dir = fsp->open(path, FILE_READ); if (dir) { dir.rewindDirectory(); if (strlen(path)>1) { - snprintf_P(npath,sizeof(npath),PSTR("http://%s/upl?download=%s"),WiFi.localIP().toString().c_str(),path); - for (uint8_t cnt=strlen(npath)-1;cnt>0;cnt--) { + snprintf_P(npath, sizeof(npath), PSTR("http://%s/upl?download=%s"), WiFi.localIP().toString().c_str(),path); + for (uint8_t cnt = strlen(npath) - 1; cnt>0; cnt--) { if (npath[cnt]=='/') { - if (npath[cnt-1]=='=') npath[cnt+1]=0; - else npath[cnt]=0; + if (npath[cnt - 1]=='=') npath[cnt + 1] = 0; + else npath[cnt] = 0; break; } } - WSContentSend_P(HTTP_FORM_SDC_DIRd,npath,path,".."); + WSContentSend_P(HTTP_FORM_SDC_DIRd, npath,path, ".."); } char *ep; while (true) { - File entry=dir.openNextFile(); + File entry = dir.openNextFile(); if (!entry) { break; } // esp32 returns path here, shorten to filename - ep=(char*)entry.name(); + ep = (char*)entry.name(); if (*ep=='/') ep++; char *lcp = strrchr(ep,'/'); if (lcp) { - ep=lcp+1; + ep = lcp + 1; } //AddLog_P2(LOG_LEVEL_INFO, PSTR("entry: %s"),ep); - time_t tm=entry.getLastWrite(); + time_t tm = entry.getLastWrite(); char tstr[24]; strftime(tstr, 22, "%d-%m-%Y - %H:%M:%S ", localtime(&tm)); - char *pp=path; - if (!*(pp+1)) pp++; - char *cp=name; + char *pp = path; + if (!*(pp + 1)) pp++; + char *cp = name; // osx formatted disks contain a lot of stuff we dont want if (reject((char*)ep)) goto fclose; - for (uint8_t cnt=0;cnt1) { - strcat(path,"/"); + strcat(path, "/"); } - strcat(path,ep); - ListDir(path,depth+4); - path[plen]=0; + strcat(path, ep); + ListDir(path, depth + 4); + path[plen] = 0; } else { - snprintf_P(npath,sizeof(npath),HTTP_FORM_SDC_HREF,WiFi.localIP().toString().c_str(),pp,ep); - WSContentSend_P(HTTP_FORM_SDC_DIRb,npath,ep,name,tstr,entry.size()); + snprintf_P(npath, sizeof(npath), HTTP_FORM_SDC_HREF, WiFi.localIP().toString().c_str(), pp,ep); + WSContentSend_P(HTTP_FORM_SDC_DIRb, npath, ep, name, tstr, entry.size()); } fclose: entry.close(); @@ -4576,18 +4831,18 @@ void ListDir(char *path, uint8_t depth) { char path[48]; void Script_FileUploadConfiguration(void) { - uint8_t depth=0; + uint8_t depth = 0; - strcpy(path,"/"); + strcpy(path, "/"); if (!HttpCheckPriviledgedAccess()) { return; } if (Webserver->hasArg("download")) { String stmp = Webserver->arg("download"); - char *cp=(char*)stmp.c_str(); + char *cp = (char*)stmp.c_str(); if (DownloadFile(cp)) { // is directory - strcpy(path,cp); + strcpy(path, cp); } } @@ -4596,9 +4851,14 @@ void Script_FileUploadConfiguration(void) { WSContentSend_P(HTTP_FORM_FILE_UPLOAD,D_SDCARD_DIR); WSContentSend_P(HTTP_FORM_FILE_UPG, D_SCRIPT_UPLOAD); #ifdef SDCARD_DIR + char ts[16]; + char fs[16]; + form1000(get_fsinfo(0), ts, '.'); + form1000(get_fsinfo(1), fs, '.'); + WSContentSend_P(HTTP_FORM_FILE_UPGc, ts, fs); WSContentSend_P(HTTP_FORM_SDC_DIRa); if (glob_script_mem.script_sd_found) { - ListDir(path,depth); + ListDir(path, depth); } WSContentSend_P(HTTP_FORM_SDC_DIRc); #endif @@ -4629,15 +4889,15 @@ void script_upload(void) { char npath[48]; #if defined(ESP32) && defined(USE_SCRIPT_FATFS) && USE_SCRIPT_FATFS==-1 //sprintf(npath,"/%s",upload.filename.c_str()); - sprintf(npath,"%s/%s",path,upload.filename.c_str()); + sprintf(npath, "%s/%s", path, upload.filename.c_str()); #else - sprintf(npath,"%s/%s",path,upload.filename.c_str()); + sprintf(npath, "%s/%s", path, upload.filename.c_str()); #endif fsp->remove(npath); - upload_file=fsp->open(npath,FILE_WRITE); - if (!upload_file) Web.upload_error=1; + upload_file = fsp->open(npath, FILE_WRITE); + if (!upload_file) Web.upload_error = 1; } else if(upload.status == UPLOAD_FILE_WRITE) { - if (upload_file) upload_file.write(upload.buf,upload.currentSize); + if (upload_file) upload_file.write(upload.buf, upload.currentSize); } else if(upload.status == UPLOAD_FILE_END) { if (upload_file) upload_file.close(); if (Web.upload_error) { @@ -4658,7 +4918,7 @@ uint8_t DownloadFile(char *file) { return 0; } - download_file=fsp->open(file,FILE_READ); + download_file = fsp->open(file, FILE_READ); if (!download_file) { AddLog_P(LOG_LEVEL_INFO,PSTR("could not open file")); return 0; @@ -4669,20 +4929,20 @@ uint8_t DownloadFile(char *file) { return 1; } - uint32_t flen=download_file.size(); + uint32_t flen = download_file.size(); download_Client = Webserver->client(); Webserver->setContentLength(flen); char attachment[100]; char *cp; - for (uint8_t cnt=strlen(file); cnt>=0; cnt--) { + for (uint8_t cnt = strlen(file); cnt>=0; cnt--) { if (file[cnt]=='/') { - cp=&file[cnt+1]; + cp = &file[cnt + 1]; break; } } - snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=%s"),cp); + snprintf_P(attachment, sizeof(attachment), PSTR("attachment; filename=%s"), cp); Webserver->sendHeader(F("Content-Disposition"), attachment); WSSend(200, CT_STREAM, ""); @@ -4690,15 +4950,15 @@ uint8_t DownloadFile(char *file) { uint16_t bread; // transfer is about 150kb/s - uint8_t cnt=0; + uint8_t cnt = 0; while (download_file.available()) { - bread=download_file.read(buff,sizeof(buff)); - uint16_t bw=download_Client.write((const char*)buff,bread); + bread = download_file.read(buff, sizeof(buff)); + uint16_t bw = download_Client.write((const char*)buff, bread); if (!bw) break; cnt++; if (cnt>7) { - cnt=0; - if (glob_script_mem.script_loglevel&0x80) { + cnt = 0; + if (glob_script_mem.script_loglevel & 0x80) { // this indeed multitasks, but is slower 50 kB/s loop(); } @@ -4747,8 +5007,8 @@ void HandleScriptConfiguration(void) { #ifdef xSCRIPT_STRIP_COMMENTS - uint16_t ssize=glob_script_mem.script_size; - if (bitRead(Settings.rule_enabled, 1)) ssize*=2; + uint16_t ssize = glob_script_mem.script_size; + if (bitRead(Settings.rule_enabled, 1)) ssize *= 2; WSContentSend_P(HTTP_FORM_SCRIPT1,1,1,bitRead(Settings.rule_enabled,0) ? " checked" : "",ssize); #else WSContentSend_P(HTTP_FORM_SCRIPT1,1,1,bitRead(Settings.rule_enabled,0) ? " checked" : "",glob_script_mem.script_size); @@ -4763,10 +5023,10 @@ void HandleScriptConfiguration(void) { #ifdef USE_SCRIPT_FATFS if (glob_script_mem.script_sd_found) { WSContentSend_P(HTTP_FORM_SCRIPT1d); - if (glob_script_mem.flink[0][0]) WSContentSend_P(HTTP_FORM_SCRIPT1c,1,glob_script_mem.flink[0]); - if (glob_script_mem.flink[1][0]) WSContentSend_P(HTTP_FORM_SCRIPT1c,2,glob_script_mem.flink[1]); + if (glob_script_mem.flink[0][0]) WSContentSend_P(HTTP_FORM_SCRIPT1c, 1, glob_script_mem.flink[0]); + if (glob_script_mem.flink[1][0]) WSContentSend_P(HTTP_FORM_SCRIPT1c, 2, glob_script_mem.flink[1]); } -#endif +#endif //USE_SCRIPT_FATFS WSContentSend_P(HTTP_SCRIPT_FORM_END); WSContentSpaceButton(BUTTON_CONFIGURATION); @@ -4777,22 +5037,22 @@ void SaveScript(void) { #ifdef EEP_SCRIPT_SIZE if (glob_script_mem.flags&1) { - EEP_WRITE(0,EEP_SCRIPT_SIZE,glob_script_mem.script_ram); + EEP_WRITE(0, EEP_SCRIPT_SIZE, glob_script_mem.script_ram); } #endif // EEP_SCRIPT_SIZE #ifdef USE_SCRIPT_FATFS - if (glob_script_mem.flags&1) { + if (glob_script_mem.flags & 1) { fsp->remove(FAT_SCRIPT_NAME); - File file=fsp->open(FAT_SCRIPT_NAME,FILE_WRITE); - file.write((const uint8_t*)glob_script_mem.script_ram,FAT_SCRIPT_SIZE); + File file = fsp->open(FAT_SCRIPT_NAME, FILE_WRITE); + file.write((const uint8_t*)glob_script_mem.script_ram, FAT_SCRIPT_SIZE); file.close(); } #endif // USE_SCRIPT_FATFS #ifdef LITTLEFS_SCRIPT_SIZE if (glob_script_mem.flags&1) { - SaveFile("/script.txt",(uint8_t*)glob_script_mem.script_ram,LITTLEFS_SCRIPT_SIZE); + SaveFile("/script.txt", (uint8_t*)glob_script_mem.script_ram, LITTLEFS_SCRIPT_SIZE); } #endif // LITTLEFS_SCRIPT_SIZE } @@ -4800,9 +5060,9 @@ void SaveScript(void) { void ScriptSaveSettings(void) { if (Webserver->hasArg("c1")) { - bitWrite(Settings.rule_enabled,0,1); + bitWrite(Settings.rule_enabled, 0, 1); } else { - bitWrite(Settings.rule_enabled,0,0); + bitWrite(Settings.rule_enabled, 0, 0); } @@ -4810,42 +5070,42 @@ void ScriptSaveSettings(void) { if (*str.c_str()) { - str.replace("\r\n","\n"); - str.replace("\r","\n"); + str.replace("\r\n", "\n"); + str.replace("\r", "\n"); #ifdef xSCRIPT_STRIP_COMMENTS if (bitRead(Settings.rule_enabled, 1)) { - char *sp=(char*)str.c_str(); - char *sp1=sp; - char *dp=sp; - uint8_t flg=0; + char *sp = (char*)str.c_str(); + char *sp1 = sp; + char *dp = sp; + uint8_t flg = 0; while (*sp) { while (*sp==' ') sp++; - sp1=sp; - sp=strchr(sp,'\n'); + sp1 = sp; + sp = strchr(sp,'\n'); if (!sp) { - flg=1; + flg = 1; } else { - *sp=0; + *sp = 0; } if (*sp1!=';') { - uint8_t slen=strlen(sp1); + uint8_t slen = strlen(sp1); if (slen) { - strcpy(dp,sp1); - dp+=slen; - *dp++='\n'; + strcpy(dp, sp1); + dp += slen; + *dp++ = '\n'; } } if (flg) { - *dp=0; + *dp = 0; break; } sp++; } } -#endif +#endif //xSCRIPT_STRIP_COMMENTS - strlcpy(glob_script_mem.script_ram,str.c_str(), glob_script_mem.script_size); + strlcpy(glob_script_mem.script_ram, str.c_str(), glob_script_mem.script_size); if (glob_script_mem.script_ram[0]!='>' && glob_script_mem.script_ram[1]!='D') { AddLog_P2(LOG_LEVEL_INFO, PSTR("script error: must start with >D")); @@ -4860,11 +5120,16 @@ void ScriptSaveSettings(void) { } void SaveScriptEnd(void) { + +#ifdef USE_SCRIPT_GLOBVARS + Script_Stop_UDP(); +#endif //USE_SCRIPT_GLOBVARS + if (glob_script_mem.script_mem) { Scripter_save_pvars(); free(glob_script_mem.script_mem); - glob_script_mem.script_mem=0; - glob_script_mem.script_mem_size=0; + glob_script_mem.script_mem = 0; + glob_script_mem.script_mem_size = 0; } #ifdef USE_SCRIPT_COMPRESSION @@ -4879,20 +5144,23 @@ void SaveScriptEnd(void) { #endif // USE_SCRIPT_COMPRESSION if (bitRead(Settings.rule_enabled, 0)) { - int16_t res=Init_Scripter(); + + + + int16_t res = Init_Scripter(); if (res) { AddLog_P2(LOG_LEVEL_INFO, PSTR("script init error: %d"), res); return; } - Run_Scripter(">B\n",3,0); - Run_Scripter(">BS",3,0); + Run_Scripter(">B\n", 3, 0); + Run_Scripter(">BS", 3, 0); - fast_script=Run_Scripter(">F",-2,0); + fast_script = Run_Scripter(">F", -2, 0); } } -#endif +#endif // USE_WEBSERVER #if defined(USE_SCRIPT_HUE) && defined(USE_WEBSERVER) && defined(USE_EMULATION) && defined(USE_EMULATION_HUE) && defined(USE_LIGHT) @@ -5026,22 +5294,22 @@ break; void Script_HueStatus(String *response, uint16_t hue_devs) { if (hue_script[hue_devs].type=='p') { - *response+=FPSTR(SCRIPT_HUE_LIGHTS_STATUS_JSON2); - response->replace("{j1",hue_script[hue_devs].name); + *response += FPSTR(SCRIPT_HUE_LIGHTS_STATUS_JSON2); + response->replace("{j1", hue_script[hue_devs].name); response->replace("{j2", GetHueDeviceId(hue_devs)); - uint8_t pwr=glob_script_mem.fvars[hue_script[hue_devs].index[0]-1]; + uint8_t pwr = glob_script_mem.fvars[hue_script[hue_devs].index[0] - 1]; response->replace("{state}", (pwr ? "true" : "false")); return; } - *response+=FPSTR(SCRIPT_HUE_LIGHTS_STATUS_JSON1); - uint8_t pwr=glob_script_mem.fvars[hue_script[hue_devs].index[0]-1]; + *response += FPSTR(SCRIPT_HUE_LIGHTS_STATUS_JSON1); + uint8_t pwr = glob_script_mem.fvars[hue_script[hue_devs].index[0] - 1]; response->replace("{state}", (pwr ? "true" : "false")); String light_status = ""; if (hue_script[hue_devs].index[1]>0) { // bri light_status += "\"bri\":"; - uint32_t bri=glob_script_mem.fvars[hue_script[hue_devs].index[1]-1]; + uint32_t bri = glob_script_mem.fvars[hue_script[hue_devs].index[1] - 1]; if (bri > 254) bri = 254; if (bri < 1) bri = 1; light_status += String(bri); @@ -5049,7 +5317,7 @@ void Script_HueStatus(String *response, uint16_t hue_devs) { } if (hue_script[hue_devs].index[2]>0) { // hue - uint32_t hue=glob_script_mem.fvars[hue_script[hue_devs].index[2]-1]; + uint32_t hue = glob_script_mem.fvars[hue_script[hue_devs].index[2] - 1]; //hue = changeUIntScale(hue, 0, 359, 0, 65535); light_status += "\"hue\":"; light_status += String(hue); @@ -5057,7 +5325,7 @@ void Script_HueStatus(String *response, uint16_t hue_devs) { } if (hue_script[hue_devs].index[3]>0) { // sat - uint32_t sat=glob_script_mem.fvars[hue_script[hue_devs].index[3]-1] ; + uint32_t sat = glob_script_mem.fvars[hue_script[hue_devs].index[3] - 1] ; if (sat > 254) sat = 254; if (sat < 1) sat = 1; light_status += "\"sat\":"; @@ -5066,7 +5334,7 @@ void Script_HueStatus(String *response, uint16_t hue_devs) { } if (hue_script[hue_devs].index[4]>0) { // ct - uint32_t ct=glob_script_mem.fvars[hue_script[hue_devs].index[4]-1]; + uint32_t ct = glob_script_mem.fvars[hue_script[hue_devs].index[4] - 1]; light_status += "\"ct\":"; light_status += String(ct); light_status += ","; @@ -5101,7 +5369,7 @@ void Script_HueStatus(String *response, uint16_t hue_devs) { } response->replace("{light_status}", light_status); - response->replace("{j1",hue_script[hue_devs].name); + response->replace("{j1", hue_script[hue_devs].name); response->replace("{j2", GetHueDeviceId(hue_devs)); } @@ -5109,14 +5377,14 @@ void Script_HueStatus(String *response, uint16_t hue_devs) { void Script_Check_Hue(String *response) { if (!bitRead(Settings.rule_enabled, 0)) return; - uint8_t hue_script_found=Run_Scripter(">H",-2,0); + uint8_t hue_script_found = Run_Scripter(">H", -2, 0); if (hue_script_found!=99) return; char tmp[256]; - uint8_t hue_devs=0; - uint8_t vindex=0; + uint8_t hue_devs = 0; + uint8_t vindex = 0; char *cp; - char *lp=glob_script_mem.section_ptr+2; + char *lp = glob_script_mem.section_ptr + 2; while (lp) { SCRIPT_SKIP_SPACES while (*lp==SCRIPT_EOL) { @@ -5127,69 +5395,69 @@ void Script_Check_Hue(String *response) { } if (*lp!=';') { // check this line - Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp)); + Replace_Cmd_Vars(lp, 1, tmp, sizeof(tmp)); // check for hue defintions // NAME, TYPE , vars - cp=tmp; - cp=strchr(cp,','); + cp = tmp; + cp = strchr(cp,','); if (!cp) break; - *cp=0; + *cp = 0; // copy name - strlcpy(hue_script[hue_devs].name,tmp,HUE_DEV_NSIZE); + strlcpy(hue_script[hue_devs].name, tmp, HUE_DEV_NSIZE); cp++; while (*cp==' ') cp++; // get type - hue_script[hue_devs].type=*cp; + hue_script[hue_devs].type = *cp; - for (vindex=0;vindex0) *response+=",\""; - else *response+="\""; + if (hue_devs>0) *response += ",\""; + else *response += "\""; } - *response+=String(EncodeLightId(hue_devs+devices_present+1))+"\":"; - Script_HueStatus(response,hue_devs); + *response += String(EncodeLightId(hue_devs + devices_present + 1))+"\":"; + Script_HueStatus(response, hue_devs); //AddLog_P2(LOG_LEVEL_INFO, PSTR("Hue: %s - %d "),response->c_str(), hue_devs); } @@ -5252,49 +5520,58 @@ void Script_Handle_Hue(String *path) { bool resp = false; uint8_t device = DecodeLightId(atoi(path->c_str())); - uint8_t index = device-devices_present-1; + uint8_t index = device - devices_present - 1; if (Webserver->args()) { response = "["; - StaticJsonBuffer<400> jsonBuffer; - JsonObject &hue_json = jsonBuffer.parseObject(Webserver->arg((Webserver->args())-1)); - if (hue_json.containsKey("on")) { + JsonParser parser((char*) Webserver->arg((Webserver->args())-1).c_str()); + JsonParserObject root = parser.getRootObject(); + JsonParserToken hue_on = root[PSTR("on")]; + if (hue_on) { response += FPSTR(sHUE_LIGHT_RESPONSE_JSON); response.replace("{id", String(EncodeLightId(device))); response.replace("{cm", "on"); - bool on = hue_json["on"]; + bool on = hue_on.getBool(); if (on==false) { - glob_script_mem.fvars[hue_script[index].index[0]-1]=0; + glob_script_mem.fvars[hue_script[index].index[0] - 1] = 0; response.replace("{re", "false"); } else { - glob_script_mem.fvars[hue_script[index].index[0]-1]=1; + glob_script_mem.fvars[hue_script[index].index[0] - 1] = 1; response.replace("{re", "true"); } - glob_script_mem.type[hue_script[index].vindex[0]].bits.changed=1; + glob_script_mem.type[hue_script[index].vindex[0]].bits.changed = 1; resp = true; } - if (hue_json.containsKey("bri")) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. - tmp = hue_json["bri"]; - bri=tmp; + + parser.setCurrent(); + JsonParserToken hue_bri = root[PSTR("bri")]; + if (hue_bri) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. + tmp = hue_bri.getUInt(); + bri = tmp; if (254 <= bri) { bri = 255; } if (resp) { response += ","; } response += FPSTR(sHUE_LIGHT_RESPONSE_JSON); response.replace("{id", String(EncodeLightId(device))); response.replace("{cm", "bri"); response.replace("{re", String(tmp)); - glob_script_mem.fvars[hue_script[index].index[1]-1]=bri; - glob_script_mem.type[hue_script[index].vindex[1]].bits.changed=1; + glob_script_mem.fvars[hue_script[index].index[1] - 1] = bri; + glob_script_mem.type[hue_script[index].vindex[1]].bits.changed = 1; resp = true; } - if (hue_json.containsKey("xy")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). + + JsonParserToken hue_xy = root[PSTR("xy")]; + if (hue_xy) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). float x, y; - x = hue_json["xy"][0]; - y = hue_json["xy"][1]; - const String &x_str = hue_json["xy"][0]; - const String &y_str = hue_json["xy"][1]; + JsonParserArray arr_xy = JsonParserArray(hue_xy); + JsonParserToken tok_x = arr_xy[0]; + JsonParserToken tok_y = arr_xy[1]; + x = tok_x.getFloat(); + y = tok_y.getFloat(); + String x_str = tok_x.getStr(); + String y_str = tok_y.getStr(); uint8_t rr,gg,bb; LightStateClass::XyToRgb(x, y, &rr, &gg, &bb); LightStateClass::RgbToHsb(rr, gg, bb, &hue, &sat, nullptr); @@ -5303,49 +5580,54 @@ void Script_Handle_Hue(String *path) { response.replace("{id", String(device)); response.replace("{cm", "xy"); response.replace("{re", "[" + x_str + "," + y_str + "]"); - glob_script_mem.fvars[hue_script[index].index[2]-1]=hue; - glob_script_mem.type[hue_script[index].vindex[2]].bits.changed=1; - glob_script_mem.fvars[hue_script[index].index[3]-1]=sat; - glob_script_mem.type[hue_script[index].vindex[3]].bits.changed=1; + glob_script_mem.fvars[hue_script[index].index[2]-1] = hue; + glob_script_mem.type[hue_script[index].vindex[2]].bits.changed = 1; + glob_script_mem.fvars[hue_script[index].index[3]-1] = sat; + glob_script_mem.type[hue_script[index].vindex[3]].bits.changed = 1; resp = true; } - if (hue_json.containsKey("hue")) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. - tmp = hue_json["hue"]; + JsonParserToken hue_hue = root[PSTR("hue")]; + if (hue_hue) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. + tmp = hue_hue.getUInt(); //hue = changeUIntScale(tmp, 0, 65535, 0, 359); //tmp = changeUIntScale(hue, 0, 359, 0, 65535); - hue=tmp; + hue = tmp; if (resp) { response += ","; } response += FPSTR(sHUE_LIGHT_RESPONSE_JSON); response.replace("{id", String(EncodeLightId(device))); response.replace("{cm", "hue"); response.replace("{re", String(tmp)); - glob_script_mem.fvars[hue_script[index].index[2]-1]=hue; - glob_script_mem.type[hue_script[index].vindex[2]].bits.changed=1; + glob_script_mem.fvars[hue_script[index].index[2] - 1] = hue; + glob_script_mem.type[hue_script[index].vindex[2]].bits.changed = 1; resp = true; } - if (hue_json.containsKey("sat")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). - tmp = hue_json["sat"]; - sat=tmp; + + JsonParserToken hue_sat = root[PSTR("sat")]; + if (hue_sat) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). + tmp = hue_sat.getUInt(); + sat = tmp; if (254 <= sat) { sat = 255; } if (resp) { response += ","; } response += FPSTR(sHUE_LIGHT_RESPONSE_JSON); response.replace("{id", String(EncodeLightId(device))); response.replace("{cm", "sat"); response.replace("{re", String(tmp)); - glob_script_mem.fvars[hue_script[index].index[3]-1]=sat; - glob_script_mem.type[hue_script[index].vindex[3]].bits.changed=1; + glob_script_mem.fvars[hue_script[index].index[3] - 1] = sat; + glob_script_mem.type[hue_script[index].vindex[3]].bits.changed = 1; resp = true; } - if (hue_json.containsKey("ct")) { // Color temperature 153 (Cold) to 500 (Warm) - ct = hue_json["ct"]; + + JsonParserToken hue_ct = root[PSTR("ct")]; + if (hue_ct) { // Color temperature 153 (Cold) to 500 (Warm) + ct = hue_ct.getUInt(); if (resp) { response += ","; } response += FPSTR(sHUE_LIGHT_RESPONSE_JSON); response.replace("{id", String(EncodeLightId(device))); response.replace("{cm", "ct"); response.replace("{re", String(ct)); - glob_script_mem.fvars[hue_script[index].index[4]-1]=ct; - glob_script_mem.type[hue_script[index].vindex[4]].bits.changed=1; + glob_script_mem.fvars[hue_script[index].index[4] - 1] = ct; + glob_script_mem.type[hue_script[index].vindex[4]].bits.changed = 1; resp = true; } response += "]"; @@ -5356,7 +5638,7 @@ void Script_Handle_Hue(String *path) { AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE " Result (%s)"), response.c_str()); WSSend(code, CT_JSON, response); if (resp) { - Run_Scripter(">E",2,0); + Run_Scripter(">E", 2, 0); } } #endif // hue interface @@ -5369,30 +5651,30 @@ bool Script_SubCmd(void) { if (tasm_cmd_activ) return false; char command[CMDSZ]; - strlcpy(command,XdrvMailbox.topic,CMDSZ); - uint32_t pl=XdrvMailbox.payload; + strlcpy(command, XdrvMailbox.topic, CMDSZ); + uint32_t pl = XdrvMailbox.payload; char pld[64]; - strlcpy(pld,XdrvMailbox.data,sizeof(pld)); + strlcpy(pld, XdrvMailbox.data, sizeof(pld)); char cmdbuff[128]; - char *cp=cmdbuff; - *cp++='#'; - strcpy(cp,XdrvMailbox.topic); - uint8_t tlen=strlen(XdrvMailbox.topic); - cp+=tlen; + char *cp = cmdbuff; + *cp++ = '#'; + strcpy(cp, XdrvMailbox.topic); + uint8_t tlen = strlen(XdrvMailbox.topic); + cp += tlen; if (XdrvMailbox.index > 0) { - *cp++=XdrvMailbox.index|0x30; + *cp++ = XdrvMailbox.index | 0x30; tlen++; } if ((XdrvMailbox.payload>0) || (XdrvMailbox.data_len>0)) { - *cp++='('; - strncpy(cp,XdrvMailbox.data,XdrvMailbox.data_len); - cp+=XdrvMailbox.data_len; - *cp++=')'; - *cp=0; + *cp++ = '('; + strncpy(cp, XdrvMailbox.data,XdrvMailbox.data_len); + cp += XdrvMailbox.data_len; + *cp++ = ')'; + *cp = 0; } //toLog(cmdbuff); - uint32_t res=Run_Scripter(cmdbuff,tlen+1,0); + uint32_t res = Run_Scripter(cmdbuff, tlen + 1, 0); //AddLog_P2(LOG_LEVEL_INFO,">>%d",res); if (res) return false; else { @@ -5404,14 +5686,14 @@ bool Script_SubCmd(void) { } return true; } -#endif +#endif //USE_SCRIPT_SUB_COMMAND void execute_script(char *script) { - char *svd_sp=glob_script_mem.scriptptr; - strcat(script,"\n#"); - glob_script_mem.scriptptr=script; - Run_Scripter(">",1,0); - glob_script_mem.scriptptr=svd_sp; + char *svd_sp = glob_script_mem.scriptptr; + strcat(script, "\n#"); + glob_script_mem.scriptptr = script; + Run_Scripter(">", 1, 0); + glob_script_mem.scriptptr = svd_sp; } #define D_CMND_SCRIPT "Script" #define D_CMND_SUBSCRIBE "Subscribe" @@ -5441,44 +5723,44 @@ bool ScriptCommand(void) { break; #ifdef xSCRIPT_STRIP_COMMENTS case 2: - bitWrite(Settings.rule_enabled, 1,0); + bitWrite(Settings.rule_enabled, 1, 0); break; case 3: - bitWrite(Settings.rule_enabled, 1,1); + bitWrite(Settings.rule_enabled, 1, 1); break; -#endif +#endif //xSCRIPT_STRIP_COMMENTS } } else { if ('>' == XdrvMailbox.data[0]) { // execute script - snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"),command,XdrvMailbox.data); + snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"), command,XdrvMailbox.data); if (bitRead(Settings.rule_enabled, 0)) { - for (uint8_t count=0; count, [, ] String result = ScriptSubscribe(XdrvMailbox.data, XdrvMailbox.data_len); @@ -5486,7 +5768,7 @@ bool ScriptCommand(void) { } else if (CMND_UNSUBSCRIBE == command_code) { //MQTT Un-subscribe command. UnSubscribe String result = ScriptUnsubscribe(XdrvMailbox.data, XdrvMailbox.data_len); Response_P(S_JSON_COMMAND_SVALUE, command, result.c_str()); -#endif //SUPPORT_MQTT_EVENT +#endif //SUPPORT_MQTT_EVENT } return serviced; } @@ -5508,7 +5790,7 @@ void dateTime(uint16_t* date, uint16_t* time) { *time = xFAT_TIME(RtcTime.hour,RtcTime.minute,RtcTime.second); } -#endif +#endif //USE_SCRIPT_FATFS @@ -5519,7 +5801,7 @@ void dateTime(uint16_t* date, uint16_t* time) { #endif #ifndef MQTT_EVENT_JSIZE #define MQTT_EVENT_JSIZE 400 -#endif +#endif //SUPPORT_MQTT_EVENT /********************************************************************************************/ /* @@ -5556,30 +5838,32 @@ bool ScriptMqttData(void) if (event_item.Key.length() == 0) { //If did not specify Key value = sData; } else { //If specified Key, need to parse Key/Value from JSON data - StaticJsonBuffer jsonBuf; - JsonObject& jsonData = jsonBuf.parseObject(sData); + JsonParser parser((char*)sData.c_str()); + JsonParserObject jsonData = parser.getRootObject(); String key1 = event_item.Key; String key2; - if (!jsonData.success()) break; //Failed to parse JSON data, ignore this message. + if (!jsonData) break; //Failed to parse JSON data, ignore this message. int dot; if ((dot = key1.indexOf('.')) > 0) { key2 = key1.substring(dot+1); key1 = key1.substring(0, dot); - lkey=key2; - if (!jsonData[key1][key2].success()) break; //Failed to get the key/value, ignore this message. - value = (const char *)jsonData[key1][key2]; + lkey = key2; + JsonParserToken val = jsonData[key1.c_str()].getObject()[key2.c_str()]; + if (!val) break; //Failed to get the key/value, ignore this message. + value = val.getStr(); } else { - if (!jsonData[key1].success()) break; - value = (const char *)jsonData[key1]; - lkey=key1; + JsonParserToken val = jsonData[key1.c_str()]; + if (!val) break; + value = val.getStr(); + lkey = key1; } } value.trim(); char sbuffer[128]; - if (!strncmp(lkey.c_str(),"Epoch",5)) { - uint32_t ep=atoi(value.c_str())-(uint32_t)EPOCH_OFFSET; - snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=%d\n"), event_item.Event.c_str(),ep); + if (!strncmp(lkey.c_str(), "Epoch", 5)) { + uint32_t ep = atoi(value.c_str()) - (uint32_t)EPOCH_OFFSET; + snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=%d\n"), event_item.Event.c_str(), ep); } else { snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=\"%s\"\n"), event_item.Event.c_str(), value.c_str()); } @@ -5611,7 +5895,7 @@ String ScriptSubscribe(const char *data, int data_len) MQTT_Subscription subscription_item; String events; if (data_len > 0) { - char parameters[data_len+1]; + char parameters[data_len + 1]; memcpy(parameters, data, data_len); parameters[data_len] = '\0'; String event_name, topic, key; @@ -5632,7 +5916,7 @@ String ScriptSubscribe(const char *data, int data_len) //event_name.toUpperCase(); if (event_name.length() > 0 && topic.length() > 0) { //Search all subscriptions - for (uint32_t index=0; index < subscriptions.size(); index++) { + for (uint32_t index = 0; index < subscriptions.size(); index++) { if (subscriptions.get(index).Event.equals(event_name)) { //If find exists one, remove it. String stopic = subscriptions.get(index).Topic + "/#"; @@ -5665,7 +5949,7 @@ String ScriptSubscribe(const char *data, int data_len) } } else { //If did not specify the event name, list all subscribed event - for (uint32_t index=0; index < subscriptions.size(); index++) { + for (uint32_t index = 0; index < subscriptions.size(); index++) { subscription_item = subscriptions.get(index); events.concat(subscription_item.Event + "," + subscription_item.Topic + (subscription_item.Key.length()>0 ? "," : "") @@ -5795,15 +6079,15 @@ void ScriptGetSDCard(void) { if (!HttpCheckPriviledgedAccess()) { return; } String stmp = Webserver->uri(); - char *cp=strstr_P(stmp.c_str(),PSTR("/sdc/")); + char *cp = strstr_P(stmp.c_str(), PSTR("/sdc/")); // if (cp) Serial.printf(">>>%s\n",cp); if (cp) { #ifdef ESP32 - cp+=4; + cp += 4; #else - cp+=5; + cp += 5; #endif - if (strstr_P(cp,PSTR("scrdmp.bmp"))) { + if (strstr_P(cp, PSTR("scrdmp.bmp"))) { SendFile(cp); return; } else { @@ -5821,34 +6105,34 @@ extern uint8_t *buffer; void SendFile(char *fname) { char buff[512]; const char *mime; - uint8_t sflg=0; - char *jpg=strstr(fname,".jpg"); + uint8_t sflg = 0; + char *jpg = strstr(fname,".jpg"); if (jpg) { - mime="image/jpeg"; + mime = "image/jpeg"; } #ifdef USE_DISPLAY_DUMP - char *sbmp=strstr_P(fname,PSTR("scrdmp.bmp")); + char *sbmp = strstr_P(fname, PSTR("scrdmp.bmp")); if (sbmp) { - mime="image/bmp"; - sflg=1; + mime = "image/bmp"; + sflg = 1; } #endif // USE_DISPLAY_DUMP - char *bmp=strstr(fname,".bmp"); + char *bmp = strstr(fname, ".bmp"); if (bmp) { - mime="image/bmp"; + mime = "image/bmp"; } - char *html=strstr(fname,".html"); + char *html = strstr(fname, ".html"); if (html) { - mime="text/html"; + mime = "text/html"; } - char *txt=strstr(fname,".txt"); + char *txt = strstr(fname, ".txt"); if (txt) { - mime="text/plain"; + mime = "text/plain"; } - WSContentSend_P(HTTP_SCRIPT_MIMES,fname,mime); + WSContentSend_P(HTTP_SCRIPT_MIMES, fname, mime); if (sflg) { #ifdef USE_DISPLAY_DUMP @@ -5856,8 +6140,8 @@ char buff[512]; #define fileHeaderSize 14 #define infoHeaderSize 40 if (buffer) { - uint8_t *bp=buffer; - uint8_t *lbuf=(uint8_t*)calloc(Settings.display_width+2,3); + uint8_t *bp = buffer; + uint8_t *lbuf = (uint8_t*)calloc(Settings.display_width + 2, 3); uint8_t *lbp; uint8_t fileHeader[fileHeaderSize]; createBitmapFileHeader(Settings.display_height , Settings.display_width , fileHeader); @@ -5865,37 +6149,37 @@ char buff[512]; uint8_t infoHeader[infoHeaderSize]; createBitmapInfoHeader(Settings.display_height, Settings.display_width, infoHeader ); Webserver->client().write((uint8_t *)infoHeader, infoHeaderSize); - for (uint32_t lins=0; lins>1; + bits = bits>>1; } bp++; } - Webserver->client().write((const char*)lbuf, Settings.display_width*3); + Webserver->client().write((const char*)lbuf, Settings.display_width * 3); } if (lbuf) free(lbuf); Webserver->client().stop(); } #endif // USE_DISPLAY_DUMP } else { - File file=fsp->open(fname,FILE_READ); + File file = fsp->open(fname,FILE_READ); uint32_t siz = file.size(); - uint32_t len=sizeof(buff); + uint32_t len = sizeof(buff); while (siz > 0) { - if (len>siz) len=siz; - file.read((uint8_t *)buff,len ); + if (len>siz) len = siz; + file.read((uint8_t *)buff, len); Webserver->client().write((const char*)buff, len); siz -= len; } @@ -5930,7 +6214,7 @@ void ScriptFullWebpage(void) { } WSContentBegin(200, CT_HTML); - const char *title="Full Screen"; + const char *title = "Full Screen"; WSContentSend_P(HTTP_SCRIPT_FULLPAGE1, SettingsText(SET_DEVICENAME), title, fullpage_refresh); WSContentSend_P(HTTP_SCRIPT_FULLPAGE2, fullpage_refresh); //WSContentSend_P(PSTR("
")); @@ -5954,34 +6238,34 @@ void Script_Check_HTML_Setvars(void) { if (Webserver->hasArg("sv")) { String stmp = Webserver->arg("sv"); - Serial.printf("fwp has arg dv %s\n",stmp.c_str()); + Serial.printf("fwp has arg dv %s\n", stmp.c_str()); char cmdbuf[64]; - memset(cmdbuf,0,sizeof(cmdbuf)); - char *cp=cmdbuf; - *cp++='>'; - strncpy(cp,stmp.c_str(),sizeof(cmdbuf)-1); - char *cp1=strchr(cp,'_'); + memset(cmdbuf, 0, sizeof(cmdbuf)); + char *cp = cmdbuf; + *cp++ = '>'; + strncpy(cp, stmp.c_str(), sizeof(cmdbuf) - 1); + char *cp1 = strchr(cp, '_'); if (!cp1) return; - *cp1=0; + *cp1 = 0; char vname[32]; - strncpy(vname,cp,sizeof(vname)); - *cp1='='; + strncpy(vname, cp, sizeof(vname)); + *cp1 = '='; cp1++; struct T_INDEX ind; uint8_t vtype; - isvar(vname,&vtype,&ind,0,0,0); + isvar(vname, &vtype, &ind, 0, 0, 0); if (vtype!=NUM_RES && vtype&STYPE) { // string type must insert quotes - uint8_t tlen=strlen(cp1); - memmove(cp1+1,cp1,tlen); - *cp1='\"'; - *(cp1+tlen+1)='\"'; + uint8_t tlen = strlen(cp1); + memmove(cp1 + 1, cp1, tlen); + *cp1 = '\"'; + *(cp1 + tlen +1 ) = '\"'; } //toLog(cmdbuf); execute_script(cmdbuf); - Run_Scripter(">E",2,0); + Run_Scripter(">E", 2, 0); } } @@ -6048,7 +6332,9 @@ const char SCRIPT_MSG_GTABLEd[] PROGMEM = const char SCRIPT_MSG_GTABLEb[] PROGMEM = "]);" - "var options={%s" CHART_EXTRA_OPTIONS "};" + "var options={%s" CHART_EXTRA_OPTIONS "};"; + +const char SCRIPT_MSG_GTABLEbx[] PROGMEM = "var chart=new google.visualization.%s(document.getElementById('chart%1d'));" "chart.draw(data,options);}" "google.charts.setOnLoadCallback(drawChart);"; @@ -6084,63 +6370,64 @@ const char SCRIPT_MSG_GTE1[] PROGMEM = "'%s'"; #define MAX_GARRAY 4 + char *gc_get_arrays(char *lp, float **arrays, uint8_t *ranum, uint16_t *rentries, uint16_t *ipos) { struct T_INDEX ind; uint8_t vtype; -uint16 entries=0; -uint16_t cipos=0; +uint16 entries = 0; +uint16_t cipos = 0; - uint8_t anum=0; + uint8_t anum = 0; while (anum> 2 %d\n",len); if (fa && len>=entries) { if (!entries) { entries = len; } // add array to list - arrays[anum]=fa; + arrays[anum] = fa; anum++; } } else { // single numeric - arrays[anum]=&glob_script_mem.fvars[index]; + arrays[anum] = &glob_script_mem.fvars[index]; anum++; - entries=1; + entries = 1; } } else { - lp=lp1; + lp = lp1; break; } } } //Serial.printf(">> %d - %d - %d\n",anum,entries,(uint32_t)*arrays[0]); - *ranum=anum; - *rentries=entries; - *ipos=cipos; + *ranum = anum; + *rentries = entries; + *ipos = cipos; return lp; } char *gc_send_labels(char *lp,uint32_t anum) { WSContentSend_PD("["); - for (uint32_t cnt=0; cntw",-2,0); + web_script = Run_Scripter(">w", -2, 0); } else { - web_script=Run_Scripter(">W",-2,0); + web_script = Run_Scripter(">W", -2, 0); } if (web_script==99) { char tmp[256]; - uint8_t optflg=0; - uint8_t chartindex=1; - uint8_t google_libs=0; - char *lp=glob_script_mem.section_ptr+2; + uint8_t optflg = 0; + uint8_t chartindex = 1; + uint8_t google_libs = 0; + char *lp = glob_script_mem.section_ptr + 2; if (mc=='w') { while (*lp) { if (*lp=='\n') break; @@ -6194,175 +6481,175 @@ void ScriptWebShow(char mc) { } if (*lp!=';') { // send this line to web - Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp)); - char *lin=tmp; + Replace_Cmd_Vars(lp, 1, tmp, sizeof(tmp)); + char *lin = tmp; if ((!mc && (*lin!='$')) || (mc=='w' && (*lin!='$'))) { // normal web section if (*lin=='@') { lin++; - optflg=1; + optflg = 1; } else { - optflg=0; + optflg = 0; } // check for input elements - if (!strncmp(lin,"sl(",3)) { + if (!strncmp(lin, "sl(", 3)) { // insert slider sl(min max var left mid right) - char *lp=lin; + char *lp = lin; float min; - lp=GetNumericResult(lp+3,OPER_EQU,&min,0); + lp = GetNumericArgument(lp + 3, OPER_EQU, &min, 0); SCRIPT_SKIP_SPACES // arg2 float max; - lp=GetNumericResult(lp,OPER_EQU,&max,0); + lp = GetNumericArgument(lp, OPER_EQU, &max, 0); SCRIPT_SKIP_SPACES float val; - char *slp=lp; - lp=GetNumericResult(lp,OPER_EQU,&val,0); + char *slp = lp; + lp = GetNumericArgument(lp, OPER_EQU, &val, 0); SCRIPT_SKIP_SPACES char vname[16]; - ScriptGetVarname(vname,slp,sizeof(vname)); + ScriptGetVarname(vname, slp, sizeof(vname)); char left[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,left,0); + lp = GetStringArgument(lp, OPER_EQU, left, 0); SCRIPT_SKIP_SPACES char mid[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,mid,0); + lp = GetStringArgument(lp, OPER_EQU, mid, 0); SCRIPT_SKIP_SPACES char right[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,right,0); + lp = GetStringArgument(lp, OPER_EQU, right, 0); SCRIPT_SKIP_SPACES - WSContentSend_PD(SCRIPT_MSG_SLIDER,left,mid,right,(uint32_t)min,(uint32_t)max,(uint32_t)val,vname); + WSContentSend_PD(SCRIPT_MSG_SLIDER, left,mid, right, (uint32_t)min, (uint32_t)max, (uint32_t)val, vname); - } else if (!strncmp(lin,"ck(",3)) { - char *lp=lin+3; - char *slp=lp; + } else if (!strncmp(lin, "ck(", 3)) { + char *lp = lin + 3; + char *slp = lp; float val; - lp=GetNumericResult(lp,OPER_EQU,&val,0); + lp = GetNumericArgument(lp, OPER_EQU, &val, 0); SCRIPT_SKIP_SPACES char vname[16]; - ScriptGetVarname(vname,slp,sizeof(vname)); + ScriptGetVarname(vname, slp, sizeof(vname)); char label[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,label,0); + lp = GetStringArgument(lp, OPER_EQU, label, 0); const char *cp; uint8_t uval; if (val>0) { - cp="checked='checked'"; - uval=0; + cp = "checked='checked'"; + uval = 0; } else { - cp=""; - uval=1; + cp = ""; + uval = 1; } - WSContentSend_PD(SCRIPT_MSG_CHKBOX,label,(char*)cp,uval,vname); + WSContentSend_PD(SCRIPT_MSG_CHKBOX, label, (char*)cp, uval, vname); - } else if (!strncmp(lin,"bu(",3)) { - char *lp=lin+3; - uint8_t bcnt=0; - char *found=lin; + } else if (!strncmp(lin, "bu(", 3)) { + char *lp = lin + 3; + uint8_t bcnt = 0; + char *found = lin; while (bcnt<4) { - found=strstr(found,"bu("); + found = strstr(found, "bu("); if (!found) break; - found+=3; + found += 3; bcnt++; } - uint8_t proz=100/bcnt; - if (!optflg && bcnt>1) proz-=2; + uint8_t proz = 100 / bcnt; + if (!optflg && bcnt>1) proz -= 2; if (optflg) WSContentSend_PD(SCRIPT_MSG_BUT_START_TBL); else WSContentSend_PD(SCRIPT_MSG_BUT_START); - for (uint32_t cnt=0;cnt0) { - cp=ontxt; - uval=0; + cp = ontxt; + uval = 0; } else { - cp=offtxt; - uval=1; + cp = offtxt; + uval = 1; } if (bcnt>1 && cnt==bcnt-1) { - if (!optflg) proz+=2; + if (!optflg) proz += 2; } if (!optflg) { - WSContentSend_PD(SCRIPT_MSG_BUTTONa,proz,uval,vname,cp); + WSContentSend_PD(SCRIPT_MSG_BUTTONa, proz, uval, vname, cp); } else { - WSContentSend_PD(SCRIPT_MSG_BUTTONa_TBL,proz,uval,vname,cp); + WSContentSend_PD(SCRIPT_MSG_BUTTONa_TBL, proz, uval, vname, cp); } if (bcnt>1 && cnt%s
"),lin); + WSContentSend_PD(PSTR("
%s
"), lin); } else { - WSContentSend_PD(PSTR("{s}%s{e}"),lin); + WSContentSend_PD(PSTR("{s}%s{e}"), lin); } } } @@ -6375,58 +6662,67 @@ void ScriptWebShow(char mc) { lin++; exgc: char *lp; - if (!strncmp(lin,"gc(",3)) { + if (!strncmp(lin, "gc(", 3)) { // get google table - lp=lin+3; + lp = lin + 3; SCRIPT_SKIP_SPACES const char *type; const char *func; char options[312]; - uint8_t nanum=MAX_GARRAY; - uint8_t y2f=0; + uint8_t nanum = MAX_GARRAY; + uint8_t y2f = 0; + uint8_t tonly = 0; char ctype; - ctype=*lp; + ctype = *lp; lp++; - if (!(google_libs&GLIBS_MAIN)) { - google_libs|=GLIBS_MAIN; + if (!(google_libs & GLIBS_MAIN)) { + google_libs |= GLIBS_MAIN; WSContentSend_PD(SCRIPT_MSG_GTABLE); } switch (ctype) { case 'l': - type=PSTR("LineChart"); + type = PSTR("LineChart"); break; case 'b': - type=PSTR("BarChart"); + type = PSTR("BarChart"); break; case 'p': - type=PSTR("PieChart"); + type = PSTR("PieChart"); break; case 'g': - type=PSTR("Gauge"); - if (!(google_libs&GLIBS_GAUGE)) { - google_libs|=GLIBS_GAUGE; + type = PSTR("Gauge"); + if (!(google_libs & GLIBS_GAUGE)) { + google_libs |= GLIBS_GAUGE; WSContentSend_PD(SCRIPT_MSG_GAUGE); } break; case 't': - type=PSTR("Table"); - if (!(google_libs&GLIBS_TABLE)) { - google_libs|=GLIBS_TABLE; + type = PSTR("Table"); + if (!(google_libs & GLIBS_TABLE)) { + google_libs |= GLIBS_TABLE; WSContentSend_PD(SCRIPT_MSG_TABLE); } break; case 'T': - type=PSTR("Timeline"); - if (!(google_libs&GLIBS_TIMELINE)) { - google_libs|=GLIBS_TIMELINE; + type = PSTR("Timeline"); + if (!(google_libs & GLIBS_TIMELINE)) { + google_libs |= GLIBS_TIMELINE; WSContentSend_PD(SCRIPT_MSG_TIMELINE); } break; case 'h': - type=PSTR("Histogram"); + type = PSTR("Histogram"); break; case 'c': - type=PSTR("ColumnChart"); + type = PSTR("ColumnChart"); + break; + case 'C': + type = PSTR("ComboChart"); + break; + case 'e': + WSContentSend_PD(SCRIPT_MSG_GTABLEbx, type, chartindex); + chartindex++; + goto nextwebline; break; default: // error @@ -6435,24 +6731,28 @@ exgc: } if (ctype=='l' && *lp=='f') { lp++; - func=PSTR(",curveType:'function'"); + func = PSTR(",curveType:'function'"); } else { - func=""; + func = ""; } if (*lp=='2') { lp++; - nanum=2; - y2f=1; + nanum = 2; + y2f = 1; + } + if (*lp=='t') { + lp++; + tonly = 1; } SCRIPT_SKIP_SPACES //Serial.printf("type %d\n",ctype); float *arrays[MAX_GARRAY]; - uint8_t anum=0; - uint16_t entries=0; - uint16_t ipos=0; - lp=gc_get_arrays(lp, &arrays[0], &anum, &entries, &ipos); + uint8_t anum = 0; + uint16_t entries = 0; + uint16_t ipos = 0; + lp = gc_get_arrays(lp, &arrays[0], &anum, &entries, &ipos); if (anum>nanum) { goto nextwebline; @@ -6461,98 +6761,120 @@ exgc: //Serial.printf("arrays %d\n",anum); //Serial.printf("entries %d\n",entries); if (ctype=='T') { - if (anum && !(entries&1)) { + if (anum && !(entries & 1)) { WSContentSend_PD(SCRIPT_MSG_GTABLEa); WSContentSend_PD(SCRIPT_MSG_GTABLEd); char label[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,label,0); + lp = GetStringArgument(lp, OPER_EQU, label, 0); SCRIPT_SKIP_SPACES - for (uint32_t ind=0; ind=entries) todflg=entries-1; + int16_t divflg = 1; + int16_t todflg = -1; + if (!strncmp(label, "cnt", 3)) { + char *cp = &label[3]; + //todflg=atoi(&label[3]); + todflg = strtol(cp, &cp, 10); + if (todflg>=entries) todflg = entries - 1; + if (*cp=='/') { + cp++; + divflg = strtol(cp, &cp, 10); + } } else { - uint16 segments=1; - for (uint32_t cnt=0; cnt=entries) aind=entries-1; - for (uint32_t cnt=0; cnt=entries) aind = entries - 1; + for (uint32_t cnt = 0; cnt < entries; cnt++) { WSContentSend_PD("['"); char lbl[16]; if (todflg>=0) { - sprintf(lbl,"%d",todflg); + sprintf(lbl, "%d", todflg / divflg); todflg++; - if (todflg>=entries) { - todflg=0; + if (todflg >= entries) { + todflg = 0; } } else { - GetTextIndexed(lbl, sizeof(lbl), aind/divflg, label); + if (todflg==-1) { + GetTextIndexed(lbl, sizeof(lbl), aind / divflg, label); + } else { + // day,hours,mins + GetTextIndexed(lbl, sizeof(lbl), aind / divflg, label + 4); + sprintf(lbl, "%s-%02d", lbl, aind % divflg); + } } WSContentSend_PD(lbl); WSContentSend_PD("',"); - for (uint32_t ind=0; ind=entries) { - aind=0; + aind = 0; } } - + // table complete + if (tonly) { + WSContentSend_PD("]);"); + goto nextwebline; + } // get header char header[SCRIPT_MAXSSIZE]; - lp=GetStringResult(lp,OPER_EQU,header,0); + lp = GetStringArgument(lp, OPER_EQU, header, 0); SCRIPT_SKIP_SPACES switch (ctype) { case 't': - snprintf_P(options,sizeof(options),SCRIPT_MSG_GOPT2); + snprintf_P(options, sizeof(options), SCRIPT_MSG_GOPT2); break; default: - snprintf_P(options,sizeof(options),SCRIPT_MSG_GOPT1,header); + snprintf_P(options, sizeof(options), SCRIPT_MSG_GOPT1, header); break; } // check for 2 axis option @@ -6560,49 +6882,52 @@ exgc: // 2 y axes variant SCRIPT_SKIP_SPACES float max1; - lp=GetNumericResult(lp,OPER_EQU,&max1,0); + lp = GetNumericArgument(lp, OPER_EQU, &max1, 0); SCRIPT_SKIP_SPACES float max2; - lp=GetNumericResult(lp,OPER_EQU,&max2,0); + lp = GetNumericArgument(lp, OPER_EQU, &max2, 0); SCRIPT_SKIP_SPACES - snprintf_P(options,sizeof(options),SCRIPT_MSG_GOPT3,header,(uint32_t)max1,(uint32_t)max2,func); + snprintf_P(options, sizeof(options), SCRIPT_MSG_GOPT3, header, (uint32_t)max1, (uint32_t)max2, func); } else { SCRIPT_SKIP_SPACES - if (*lp!=')') { - float max1; - lp=GetNumericResult(lp,OPER_EQU,&max1,0); - SCRIPT_SKIP_SPACES - float max2; - lp=GetNumericResult(lp,OPER_EQU,&max2,0); - SCRIPT_SKIP_SPACES - snprintf_P(options,sizeof(options),SCRIPT_MSG_GOPT6,header,(uint32_t)max1,(uint32_t)max2,func); + if (ctype!='g') { + if (*lp!=')') { + float max1; + lp = GetNumericArgument(lp, OPER_EQU, &max1, 0); + SCRIPT_SKIP_SPACES + float max2; + lp = GetNumericArgument(lp, OPER_EQU, &max2, 0); + SCRIPT_SKIP_SPACES + snprintf_P(options, sizeof(options), SCRIPT_MSG_GOPT6, header, (uint32_t)max1, (uint32_t)max2, func); + } } } if (ctype=='g') { float yellowFrom; - lp=GetNumericResult(lp,OPER_EQU,&yellowFrom,0); + lp = GetNumericArgument(lp, OPER_EQU, &yellowFrom, 0); SCRIPT_SKIP_SPACES float redFrom; - lp=GetNumericResult(lp,OPER_EQU,&redFrom,0); + lp = GetNumericArgument(lp, OPER_EQU, &redFrom, 0); SCRIPT_SKIP_SPACES float maxValue; - lp=GetNumericResult(lp,OPER_EQU,&maxValue,0); + lp = GetNumericArgument(lp, OPER_EQU, &maxValue, 0); SCRIPT_SKIP_SPACES - float redTo=maxValue; - float yellowTo=redFrom; - snprintf_P(options,sizeof(options),SCRIPT_MSG_GAUGEOPT,(uint32_t)maxValue,(uint32_t)redFrom,(uint32_t)redTo, - (uint32_t)yellowFrom,(uint32_t)yellowTo); + float redTo = maxValue; + float yellowTo = redFrom; + snprintf_P(options, sizeof(options), SCRIPT_MSG_GAUGEOPT, (uint32_t)maxValue, (uint32_t)redFrom, (uint32_t)redTo, + (uint32_t)yellowFrom, (uint32_t)yellowTo); } } - WSContentSend_PD(SCRIPT_MSG_GTABLEb,options,type,chartindex); + WSContentSend_PD(SCRIPT_MSG_GTABLEb, options); + WSContentSend_PD(SCRIPT_MSG_GTABLEbx, type, chartindex); chartindex++; } else { - WSContentSend_PD(PSTR("%s"),lin); + WSContentSend_PD(PSTR("%s"), lin); } #else lin++; - WSContentSend_PD(PSTR("%s"),lin); + WSContentSend_PD(PSTR("%s"), lin); } else { // WSContentSend_PD(PSTR("%s"),lin); #endif //USE_GOOGLE_CHARTS @@ -6626,10 +6951,10 @@ nextwebline: #ifdef USE_SENDMAIL void script_send_email_body(void(*func)(char *)) { -uint8_t msect=Run_Scripter(">m",-2,0); +uint8_t msect = Run_Scripter(">m", -2, 0); if (msect==99) { char tmp[256]; - char *lp=glob_script_mem.section_ptr+2; + char *lp = glob_script_mem.section_ptr + 2; while (lp) { while (*lp==SCRIPT_EOL) { lp++; @@ -6639,7 +6964,7 @@ uint8_t msect=Run_Scripter(">m",-2,0); } if (*lp!=';') { // send this line to smtp - Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp)); + Replace_Cmd_Vars(lp, 1, tmp, sizeof(tmp)); //client->println(tmp); func(tmp); } @@ -6656,14 +6981,14 @@ uint8_t msect=Run_Scripter(">m",-2,0); func((char*)"*"); } } -#endif +#endif //USE_SENDMAIL #ifdef USE_SCRIPT_JSON_EXPORT void ScriptJsonAppend(void) { - uint8_t web_script=Run_Scripter(">J",-2,0); + uint8_t web_script = Run_Scripter(">J", -2, 0); if (web_script==99) { char tmp[256]; - char *lp=glob_script_mem.section_ptr+2; + char *lp = glob_script_mem.section_ptr + 2; while (lp) { while (*lp==SCRIPT_EOL) { lp++; @@ -6673,8 +6998,8 @@ void ScriptJsonAppend(void) { } if (*lp!=';') { // send this line to mqtt - Replace_Cmd_Vars(lp,1,tmp,sizeof(tmp)); - ResponseAppend_P(PSTR("%s"),tmp); + Replace_Cmd_Vars(lp, 1, tmp, sizeof(tmp)); + ResponseAppend_P(PSTR("%s"), tmp); } if (*lp==SCRIPT_EOL) { lp++; @@ -6690,7 +7015,7 @@ void ScriptJsonAppend(void) { bool RulesProcessEvent(char *json_event) { - if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">E",2,json_event); + if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">E", 2, json_event); return true; } @@ -6703,7 +7028,7 @@ bool RulesProcessEvent(char *json_event) { #ifndef STASK_PRIO #define STASK_PRIO 1 -#endif +#endif //ESP32 #if 1 @@ -6724,7 +7049,7 @@ void script_task1(void *arg) { //if (time<=esp32_tasks[0].task_timer) {vTaskDelay( pdMS_TO_TICKS( time ) ); } delay(esp32_tasks[0].task_timer); if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">t1",3,0); + Run_Scripter(">t1", 3, 0); } } } @@ -6740,7 +7065,7 @@ void script_task2(void *arg) { //if (time<=esp32_tasks[1].task_timer) {vTaskDelay( pdMS_TO_TICKS( time ) ); } delay(esp32_tasks[1].task_timer); if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">t2",3,0); + Run_Scripter(">t2", 3, 0); } } } @@ -6769,14 +7094,14 @@ TaskHandle_t task_t2; void script_task1(void *arg) { while (1) { delay(task_timer1); - Run_Scripter(">t1",3,0); + Run_Scripter(">t1", 3, 0); } } void script_task2(void *arg) { while (1) { delay(task_timer2); - Run_Scripter(">t2",3,0); + Run_Scripter(">t2", 3, 0); } } @@ -6805,12 +7130,12 @@ uint32_t scripter_create_task(uint32_t num, uint32_t time, uint32_t core) { #include "WiFiClientSecureLightBearSSL.h" #else #include -#endif +#endif //ESP8266 // get tesla powerwall info page json string uint32_t call2https(const char *host, const char *path) { if (global_state.wifi_down) return 1; - uint32_t status=0; + uint32_t status = 0; #ifdef ESP32 WiFiClientSecure *httpsClient; httpsClient = new WiFiClientSecure; @@ -6821,8 +7146,7 @@ uint32_t call2https(const char *host, const char *path) { httpsClient->setTimeout(1500); - int retry = 0; - String result; + uint32_t retry = 0; while ((!httpsClient->connect(host, 443)) && (retry < 5)) { delay(100); retry++; @@ -6842,6 +7166,7 @@ uint32_t call2https(const char *host, const char *path) { break; } } + String result; while (httpsClient->available()) { String line = httpsClient->readStringUntil('\n'); if (line!="") { @@ -6850,20 +7175,19 @@ uint32_t call2https(const char *host, const char *path) { } httpsClient->stop(); delete httpsClient; - Run_Scripter(">jp",3,(char*)result.c_str()); + Run_Scripter(">jp", 3, (char*)result.c_str()); return 0; } - #endif // SCRIPT_GET_HTTPS_JP -void cpy2lf(char *dst,uint32_t dstlen, char *src) { +void cpy2lf(char *dst, uint32_t dstlen, char *src) { for (uint32_t cnt=0; cnt0) glob_script_mem.script_ram[len_decompressed]=0; + if (len_decompressed>0) glob_script_mem.script_ram[len_decompressed] = 0; // indicates scripter use compression bitWrite(Settings.rule_once, 6, 1); //AddLog_P2(LOG_LEVEL_INFO, PSTR("decompressed script len %d"),len_decompressed); @@ -6908,29 +7232,29 @@ bool Xdrv10(uint8_t function) #endif // USE_SCRIPT_COMPRESSION #ifdef USE_BUTTON_EVENT - for (uint32_t cnt=0;cnt=0 AddLog_P(LOG_LEVEL_INFO,PSTR("FATFS mount OK!")); //fsp->dateTimeCallback(dateTime); - glob_script_mem.script_sd_found=1; + glob_script_mem.script_sd_found = 1; char *script; - script=(char*)calloc(FAT_SCRIPT_SIZE+4,1); + script = (char*)calloc(FAT_SCRIPT_SIZE + 4, 1); if (!script) break; - glob_script_mem.script_ram=script; - glob_script_mem.script_size=FAT_SCRIPT_SIZE; + glob_script_mem.script_ram = script; + glob_script_mem.script_size = FAT_SCRIPT_SIZE; if (fsp->exists(FAT_SCRIPT_NAME)) { - File file=fsp->open(FAT_SCRIPT_NAME,FILE_READ); - file.read((uint8_t*)script,FAT_SCRIPT_SIZE); + File file = fsp->open(FAT_SCRIPT_NAME, FILE_READ); + file.read((uint8_t*)script, FAT_SCRIPT_SIZE); file.close(); } - script[FAT_SCRIPT_SIZE-1]=0; + script[FAT_SCRIPT_SIZE - 1] = 0; // use rules storage for permanent vars - glob_script_mem.script_pram=(uint8_t*)Settings.rules[0]; - glob_script_mem.script_pram_size=MAX_SCRIPT_SIZE; + glob_script_mem.script_pram = (uint8_t*)Settings.rules[0]; + glob_script_mem.script_pram_size = MAX_SCRIPT_SIZE; - glob_script_mem.flags=1; + glob_script_mem.flags = 1; } else { AddLog_P(LOG_LEVEL_INFO,PSTR("FATFS mount failed!")); - glob_script_mem.script_sd_found=0; + glob_script_mem.script_sd_found = 0; } #endif // USE_SCRIPT_FATFS @@ -6997,46 +7321,46 @@ bool Xdrv10(uint8_t function) #else // lfs on esp8266 fsp = &LittleFS; -#endif +#endif //ESP32 char *script; - script=(char*)calloc(LITTLEFS_SCRIPT_SIZE+4,1); + script = (char*)calloc(LITTLEFS_SCRIPT_SIZE + 4, 1); if (!script) break; - LoadFile("/script.txt",(uint8_t*)script,LITTLEFS_SCRIPT_SIZE); + LoadFile("/script.txt", (uint8_t*)script, LITTLEFS_SCRIPT_SIZE); - glob_script_mem.script_ram=script; - glob_script_mem.script_size=LITTLEFS_SCRIPT_SIZE; - script[LITTLEFS_SCRIPT_SIZE-1]=0; + glob_script_mem.script_ram = script; + glob_script_mem.script_size = LITTLEFS_SCRIPT_SIZE; + script[LITTLEFS_SCRIPT_SIZE-1] = 0; // use rules storage for permanent vars - glob_script_mem.script_pram=(uint8_t*)Settings.rules[0]; - glob_script_mem.script_pram_size=MAX_SCRIPT_SIZE; - glob_script_mem.flags=1; + glob_script_mem.script_pram = (uint8_t*)Settings.rules[0]; + glob_script_mem.script_pram_size = MAX_SCRIPT_SIZE; + glob_script_mem.flags = 1; #endif // LITTLEFS_SCRIPT_SIZE // a valid script MUST start with >D if (glob_script_mem.script_ram[0]!='>' && glob_script_mem.script_ram[1]!='D') { // clr all - memset(glob_script_mem.script_ram,0,glob_script_mem.script_size); + memset(glob_script_mem.script_ram, 0 ,glob_script_mem.script_size); strcpy_P(glob_script_mem.script_ram, PSTR(">D\nscript error must start with >D")); bitWrite(Settings.rule_enabled, 0, 0); } // assure permanent memory is 4 byte aligned - { uint32_t ptr=(uint32_t)glob_script_mem.script_pram; - ptr&=0xfffffffc; - ptr+=4; - glob_script_mem.script_pram=(uint8_t*)ptr; - glob_script_mem.script_pram_size-=4; + { uint32_t ptr = (uint32_t)glob_script_mem.script_pram; + ptr &= 0xfffffffc; + ptr += 4; + glob_script_mem.script_pram = (uint8_t*)ptr; + glob_script_mem.script_pram_size -= 4; } if (bitRead(Settings.rule_enabled, 0)) Init_Scripter(); break; case FUNC_INIT: if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">B\n",3,0); - fast_script=Run_Scripter(">F",-2,0); + Run_Scripter(">B\n", 3, 0); + fast_script = Run_Scripter(">F", -2, 0); #if defined(USE_SCRIPT_HUE) && defined(USE_WEBSERVER) && defined(USE_EMULATION) && defined(USE_EMULATION_HUE) && defined(USE_LIGHT) Script_Check_Hue(0); -#endif +#endif //USE_SCRIPT_HUE } break; case FUNC_EVERY_100_MSECOND: @@ -7050,18 +7374,18 @@ bool Xdrv10(uint8_t function) break; case FUNC_SET_POWER: #ifdef SCRIPT_POWER_SECTION - if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">P",2,0); + if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">P", 2, 0); #else if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">E",2,0); - result=event_handeled; + Run_Scripter(">E", 2, 0); + result = event_handeled; } -#endif +#endif //SCRIPT_POWER_SECTION break; case FUNC_RULES_PROCESS: if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">E",2,mqtt_data); - result=event_handeled; + Run_Scripter(">E", 2, mqtt_data); + result = event_handeled; } break; #ifdef USE_WEBSERVER @@ -7073,11 +7397,11 @@ bool Xdrv10(uint8_t function) if (bitRead(Settings.rule_enabled, 0)) { ScriptWebShow('$'); #ifdef SCRIPT_FULL_WEBPAGE - uint8_t web_script=Run_Scripter(">w",-2,0); + uint8_t web_script = Run_Scripter(">w", -2, 0); if (web_script==99) { char bname[48]; - cpy2lf(bname,sizeof(bname),glob_script_mem.section_ptr+3); - WSContentSend_PD(HTTP_WEB_FULL_DISPLAY,bname); + cpy2lf(bname, sizeof(bname), glob_script_mem.section_ptr + 3); + WSContentSend_PD(HTTP_WEB_FULL_DISPLAY, bname); Webserver->on("/sfd", ScriptFullWebpage); #ifdef USE_SCRIPT_FATFS Webserver->onNotFound(ScriptGetSDCard); @@ -7090,24 +7414,24 @@ bool Xdrv10(uint8_t function) case FUNC_WEB_ADD_HANDLER: Webserver->on("/" WEB_HANDLE_SCRIPT, HandleScriptConfiguration); Webserver->on("/ta",HTTP_POST, HandleScriptTextareaConfiguration); - Webserver->on("/exs", HTTP_POST,[]() { Webserver->sendHeader("Location","/exs");Webserver->send(303);},script_upload_start); - Webserver->on("/exs", HTTP_GET,ScriptExecuteUploadSuccess); + Webserver->on("/exs", HTTP_POST,[]() { Webserver->sendHeader("Location","/exs");Webserver->send(303);}, script_upload_start); + Webserver->on("/exs", HTTP_GET, ScriptExecuteUploadSuccess); #ifdef USE_SCRIPT_FATFS - Webserver->on("/u3", HTTP_POST,[]() { Webserver->sendHeader("Location","/u3");Webserver->send(303);},script_upload); - Webserver->on("/u3", HTTP_GET,ScriptFileUploadSuccess); - Webserver->on("/upl", HTTP_GET,Script_FileUploadConfiguration); -#endif + Webserver->on("/u13", HTTP_POST,[]() { Webserver->sendHeader("Location","/u13");Webserver->send(303);}, script_upload); + Webserver->on("/u13", HTTP_GET, ScriptFileUploadSuccess); + Webserver->on("/upl", HTTP_GET, Script_FileUploadConfiguration); +#endif //USE_SCRIPT_FATFS break; #endif // USE_WEBSERVER case FUNC_SAVE_BEFORE_RESTART: if (bitRead(Settings.rule_enabled, 0)) { - Run_Scripter(">R",2,0); + Run_Scripter(">R", 2, 0); Scripter_save_pvars(); } #ifdef USE_SCRIPT_GLOBVARS Script_Stop_UDP(); -#endif +#endif //USE_SCRIPT_GLOBVARS break; #ifdef SUPPORT_MQTT_EVENT case FUNC_MQTT_DATA: @@ -7136,18 +7460,18 @@ bool Xdrv10(uint8_t function) case FUNC_BUTTON_PRESSED: if (bitRead(Settings.rule_enabled, 0)) { if ((script_button[XdrvMailbox.index]&1)!=(XdrvMailbox.payload&1)) { - script_button[XdrvMailbox.index]=XdrvMailbox.payload; - Run_Scripter(">b",2,0); + script_button[XdrvMailbox.index] = XdrvMailbox.payload; + Run_Scripter(">b", 2, 0); } } break; -#endif +#endif //USE_BUTTON_EVENT #ifdef USE_SCRIPT_GLOBVARS case FUNC_LOOP: Script_PollUdp(); break; -#endif +#endif //USE_SCRIPT_GLOBVARS } return result; diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index ef9d25be9..1c2cf7fad 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -29,6 +29,7 @@ const char kHAssJsonSensorTypes[] PROGMEM = D_JSON_REACTIVE_POWERUSAGE "|" D_JSON_TODAY "|" D_JSON_TOTAL "|" D_JSON_VOLTAGE "|" D_JSON_WEIGHT "|" D_JSON_YESTERDAY "|" D_JSON_CO2 "|" D_JSON_ECO2 "|" D_JSON_TVOC "|" D_COLOR_RED "|" D_COLOR_GREEN "|" D_COLOR_BLUE"|" D_CCT "|" D_PROXIMITY "|Ambient|"; + const char kHAssJsonSensorUnits[] PROGMEM = "||||" "VA|%|A|Cm|Hz|%|LX|" @@ -44,7 +45,7 @@ const char kHAssJsonSensorDevCla[] PROGMEM = "dev_cla\":\"power|dev_cla\":\"power|dev_cla\":\"power|ic\":\"mdi:alpha-v-circle-outline|ic\":\"mdi:scale|dev_cla\":\"power|" "ic\":\"mdi:molecule-co2|ic\":\"mdi:molecule-co2|ic\":\"mdi:air-filter|" "ic\":\"mdi:palette|ic\":\"mdi:palette|ic\":\"mdi:palette|ic\":\"mdi:temperature-kelvin|ic\":\"mdi:ruler|dev_cla\":\"illuminance|"; - //"ic\":\"mdi:weather-windy|ic\":\"mdi:weather-windy|ic\":\"mdi:weather-windy|ic\":\"mdi:weather-windy|" + // List of sensors ready for discovery const char HASS_DISCOVER_BASE[] PROGMEM = @@ -58,8 +59,8 @@ const char HASS_DISCOVER_SENSOR[] PROGMEM = const char HASS_DISCOVER_SENSOR_LWT[] PROGMEM = ",\"avty_t\":\"%s\"," // tele/dualr2/LWT - "\"pl_avail\":\"" D_ONLINE "\"," // Online - "\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline + "\"pl_avail\":\"" MQTT_LWT_ONLINE "\"," // Online + "\"pl_not_avail\":\"" MQTT_LWT_OFFLINE "\""; // Offline const char HASS_DISCOVER_RELAY[] PROGMEM = ",\"cmd_t\":\"%s\"," // cmnd/dualr2/POWER2 @@ -174,6 +175,100 @@ uint8_t hass_init_step = 0; uint8_t hass_mode = 0; int hass_tele_period = 0; +// NEW DISCOVERY + +const char HASS_DISCOVER_DEVICE[] PROGMEM = // Basic parameters for Discovery + "{\"ip\":\"%s\"," // IP Address + "\"dn\":\"%s\"," // Device Name + "\"fn\":[%s]," // Friendly Names + "\"hn\":\"%s\"," // Host Name + "\"mac\":\"%s\"," // Full MAC as Device id + "\"md\":\"%s\"," // Module Name + "\"ofln\":\"" MQTT_LWT_OFFLINE "\"," // Payload Offline + "\"onln\":\"" MQTT_LWT_ONLINE "\"," // Payload Online + "\"state\":[\"%s\",\"%s\",\"%s\",\"%s\"]," // State text for "OFF","ON","TOGGLE","HOLD" + "\"sw\":\"%s\"," // Software Version + "\"t\":\"%s\"," // Topic + "\"ft\":\"%s\"," // Ful Topic + "\"tp\":[\"%s\",\"%s\",\"%s\"]," // Topics for command, stat and tele + "\"rl\":[%s],\"swc\":[%s],\"btn\":[%s]," // Inputs / Outputs + "\"so\":{\"11\":%d,\"13\":%d,\"17\":%d,\"20\":%d," // SetOptions + "\"30\":%d,\"37\":%d,\"68\":%d,\"73\":%d,\"80\":%d}," + "\"lt_st\":%d,\"ver\":1}"; // Light SubType, and Discovery version + +void NewHAssDiscovery(void) +{ + char stopic[TOPSZ]; + char stemp1[TOPSZ]; + char stemp2[200]; + char stemp3[TOPSZ]; + char stemp4[TOPSZ]; + char stemp5[TOPSZ]; + char unique_id[30]; + char *state_topic = stemp1; + + stemp2[0] = '\0'; + uint32_t maxfn = (devices_present > MAX_FRIENDLYNAMES) ? MAX_FRIENDLYNAMES : (!devices_present) ? 1 : devices_present; + for (uint32_t i = 0; i < MAX_FRIENDLYNAMES; i++) { + char fname[TOPSZ]; + snprintf_P(fname, sizeof(fname), PSTR("\"%s\""), EscapeJSONString(SettingsText(SET_FRIENDLYNAME1 +i)).c_str()); + snprintf_P(stemp2, sizeof(stemp2), PSTR("%s%s%s"), stemp2, (i > 0 ? "," : ""), (i < maxfn) ? fname : "null"); + } + + stemp3[0] = '\0'; + uint32_t maxrl = (devices_present > MAX_RELAYS) ? MAX_RELAYS : (!devices_present) ? 1 : devices_present; + for (uint32_t i = 0; i < MAX_RELAYS; i++) { + snprintf_P(stemp3, sizeof(stemp3), PSTR("%s%s%d"), stemp3, (i > 0 ? "," : ""), (i < maxrl) ? (Settings.flag.hass_light ? 2 : 1) : 0); + } + + stemp4[0] = '\0'; + // Enable Discovery for Switches only if SwitchTopic is set to a custom name + auto discover_switches = (KeyTopicActive(1) && strcmp(SettingsText(SET_MQTT_SWITCH_TOPIC), mqtt_topic)); + for (uint32_t i = 0; i < MAX_SWITCHES; i++) { + snprintf_P(stemp4, sizeof(stemp4), PSTR("%s%s%d"), stemp4, (i > 0 ? "," : ""), (PinUsed(GPIO_SWT1, i) & discover_switches) ? Settings.switchmode[i] : -1); + } + + stemp5[0] = '\0'; + // Enable Discovery for Buttons only if SetOption73 is enabled + for (uint32_t i = 0; i < MAX_KEYS; i++) { + snprintf_P(stemp5, sizeof(stemp5), PSTR("%s%s%d"), stemp5, (i > 0 ? "," : ""), (PinUsed(GPIO_KEY1, i) & Settings.flag3.mqtt_buttons)); + } + + mqtt_data[0] = '\0'; // Clear retained message + + // Full 12 chars MAC address as ID + String mac_address = WiFi.macAddress(); + mac_address.replace(":", ""); + //String mac_part = mac_address.substring(0); + snprintf_P(unique_id, sizeof(unique_id), PSTR("%s"), mac_address.c_str()); + + //snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/discovery/%s/config"), unique_id); + snprintf_P(stopic, sizeof(stopic), PSTR("tasmota/discovery/%s/config"), unique_id); + GetTopic_P(state_topic, TELE, mqtt_topic, PSTR(D_RSLT_HASS_STATE)); + + // Send empty message if new discovery is disabled + masterlog_level = 4; // Hide topic on clean and remove use weblog 4 to see it + if (!Settings.flag.hass_discovery) { + Response_P(HASS_DISCOVER_DEVICE, WiFi.localIP().toString().c_str(), SettingsText(SET_DEVICENAME), + stemp2, my_hostname, unique_id, ModuleName().c_str(), GetStateText(0), GetStateText(1), GetStateText(2), GetStateText(3), + my_version, mqtt_topic, MQTT_FULLTOPIC, SUB_PREFIX, PUB_PREFIX, PUB_PREFIX2, stemp3, stemp4, stemp5, Settings.flag.button_swap, + Settings.flag.button_single, Settings.flag.decimal_text, Settings.flag.not_power_linked, Settings.flag.hass_light, + light_controller.isCTRGBLinked(), Settings.flag3.pwm_multi_channels, Settings.flag3.mqtt_buttons, Settings.flag3.shutter_mode, Light.subtype); + } + MqttPublish(stopic, true); + + if (!Settings.flag.hass_discovery) { + snprintf_P(stopic, sizeof(stopic), PSTR("tasmota/discovery/%s/sensors"), unique_id); + Response_P(PSTR("{")); + MqttShowSensor(); + ResponseAppend_P(PSTR(",\"ver\":1}")); + MqttPublish(stopic, true); + } + masterlog_level = 0; // Restore WebLog state +} + +// NEW DISCOVERY + void TryResponseAppend_P(const char *format, ...) { va_list args; @@ -216,6 +311,7 @@ void HAssAnnounceRelayLight(void) bool TuyaMod = false; // Controls Tuya MCU modules bool PwmMod = false; // Controls PWM_DIMMER module bool FanMod = false; // Controls SONOFF_IFAN0X modules + uint8_t ShowTopic; // Used to hide/unhide a topic during Discovery to spare some cpu load uint8_t dimmer = 1; uint8_t valid_relay = 0; @@ -261,6 +357,8 @@ void HAssAnnounceRelayLight(void) TuyaDim = TuyaGetDpId((TUYA_MCU_FUNC_DIMMER) + active_device - 1); #endif //USE_TUYA_MCU + masterlog_level = ShowTopic = 4; // Hide topic on clean and remove use weblog 4 to see it + bool RelayX = PinUsed(GPIO_REL1, i-1) || (valid_relay >= i) || (TuyaRel > 0 && TuyaMod) || (TuyaRelInv > 0 && TuyaMod); // Check if the gpio is configured as Relay or force it for Sonoff DUAL R1 with MCU and Tuya MCU is_topic_light = Settings.flag.hass_light && RelayX || light_type && !RelayX || PwmMod || (TuyaDim > 0 && TuyaMod); // SetOption30 - Enforce HAss autodiscovery as light mqtt_data[0] = '\0'; // Clear retained message @@ -289,7 +387,9 @@ void HAssAnnounceRelayLight(void) char *command_topic = stemp1; char *state_topic = stemp2; char *availability_topic = stemp3; - masterlog_level = 0; + + ShowTopic = 0; + if (i > MAX_FRIENDLYNAMES) { snprintf_P(name, sizeof(name), PSTR("%s %d"), SettingsText(SET_FRIENDLYNAME1), i-1); } else { @@ -363,8 +463,8 @@ void HAssAnnounceRelayLight(void) TryResponseAppend_P(PSTR("}")); } } + masterlog_level = ShowTopic; MqttPublish(stopic, true); - masterlog_level = 4; } } @@ -377,7 +477,7 @@ void HAssAnnouncerTriggers(uint8_t device, uint8_t present, uint8_t key, uint8_t char stemp2[TOPSZ]; char unique_id[30]; char trigger2[8]; - + uint8_t ShowTopic; // Used to hide/unhide a topic during Discovery to spare some cpu load mqtt_data[0] = '\0'; // Clear retained message for (uint8_t i = trg_start; i <= trg_end; i++) { @@ -385,6 +485,8 @@ void HAssAnnouncerTriggers(uint8_t device, uint8_t present, uint8_t key, uint8_t snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_%s_%d_%s"), ESP_getChipId(), key ? "SW" : "BTN", device + 1, key ? GetStateText(i) : trigger2); snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/device_automation/%s/config"), unique_id); + masterlog_level = ShowTopic = 4; // Hide topic on clean and remove use weblog 4 to see it + if (Settings.flag.hass_discovery && present) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59) char name[TOPSZ]; // friendlyname(33) + " " + "BTN" + " " + index char value_template[33]; @@ -392,7 +494,7 @@ void HAssAnnouncerTriggers(uint8_t device, uint8_t present, uint8_t key, uint8_t char *state_topic = stemp1; char *availability_topic = stemp2; char jsoname[8]; - masterlog_level = 0; + ShowTopic = 0; // Show the new generated topic GetPowerDevice(value_template, device + 1, sizeof(value_template), key + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by SetOption26 - Switch between POWER or POWER1 snprintf_P(jsoname, sizeof(jsoname), PSTR("%s%d"), key ? "SWITCH" : "BUTTON", device + 1); @@ -420,8 +522,8 @@ void HAssAnnouncerTriggers(uint8_t device, uint8_t present, uint8_t key, uint8_t } } } + masterlog_level = ShowTopic; MqttPublish(stopic, true); - masterlog_level = 4; } } @@ -431,14 +533,18 @@ void HAssAnnouncerBinSensors(uint8_t device, uint8_t present, uint8_t dual, uint char stemp1[TOPSZ]; char stemp2[TOPSZ]; char unique_id[30]; + uint8_t ShowTopic; // Used to hide/unhide a topic during Discovery to spare some cpu load + mqtt_data[0] = '\0'; // Clear retained message + masterlog_level = 4; // Hide topic on clean and remove use weblog 4 to see it + snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_SW_%d"), ESP_getChipId(), device + 1); snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/binary_sensor/%s/config"), unique_id); + masterlog_level = ShowTopic = 4; // Hide topic on clean and remove use weblog 4 to see it if (Settings.flag.hass_discovery && present ) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59) - masterlog_level = 0; if (!toggle || dual) { char name[TOPSZ]; // friendlyname(33) + " " + "BTN" + " " + index char value_template[33]; @@ -447,6 +553,8 @@ void HAssAnnouncerBinSensors(uint8_t device, uint8_t present, uint8_t dual, uint char *availability_topic = stemp2; char jsoname[8]; + ShowTopic = 0; + GetPowerDevice(value_template, device + 1, sizeof(value_template), 1 + Settings.flag.device_index_enable); // Force index for Switch 1, Index on Button1 is controlled by SetOption26 - Switch between POWER or POWER1 snprintf_P(jsoname, sizeof(jsoname), PSTR("SWITCH%d"), device + 1); GetTopic_P(state_topic, STAT, mqtt_topic, jsoname); @@ -471,8 +579,9 @@ void HAssAnnouncerBinSensors(uint8_t device, uint8_t present, uint8_t dual, uint TryResponseAppend_P(PSTR("}")); } } + masterlog_level = ShowTopic; MqttPublish(stopic, true); - masterlog_level = 4; + } void HAssAnnounceSwitches(void) @@ -594,7 +703,7 @@ void HAssAnnounceButtons(void) } } -void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const char *MultiSubName, uint8_t subqty, uint8_t subidx, uint8_t nested, const char* SubKey) +void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const char *MultiSubName, uint8_t subqty, bool nested, const char* SubKey) { char stopic[TOPSZ]; char stemp1[TOPSZ]; @@ -608,21 +717,20 @@ void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const NoAlNumToUnderscore(subname, MultiSubName); //Replace all non alphaumeric characters to '_' to avoid topic name issues snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_%s_%s"), ESP_getChipId(), sensorname, subname); snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/sensor/%s/config"), unique_id); - if (Settings.flag.hass_discovery) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59) char name[TOPSZ]; // friendlyname(33) + " " + sensorname(20?) + " " + sensortype(20?) char prefix[TOPSZ]; char *state_topic = stemp1; char *availability_topic = stemp2; - //bool LwtSensor = MQTT_LWT_DISCOVERY; + masterlog_level = 0; // Show the new generated topic GetTopic_P(state_topic, TELE, mqtt_topic, PSTR(D_RSLT_SENSOR)); snprintf_P(name, sizeof(name), PSTR("%s %s %s"), SettingsText(SET_DEVICENAME), sensorname, MultiSubName); GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT); Response_P(HASS_DISCOVER_BASE, name, state_topic); - #ifdef DEEPSLEEP_LWT_HA_DISCOVERY +#ifdef DEEPSLEEP_LWT_HA_DISCOVERY TryResponseAppend_P(HASS_DISCOVER_SENSOR_LWT, availability_topic); #else if (Settings.deepsleep == 0) @@ -648,30 +756,19 @@ void HAssAnnounceSensor(const char *sensorname, const char *subsensortype, const case 3: snprintf_P(param1, sizeof(param1), PSTR("%s"), PressureUnit().c_str()); break; - // case 4: // Speed. Default to km/h if not set to have a graph representation under HAss - // case 5: - // case 6: - // case 7: - // if (Settings.flag2.speed_conversion == 0) { - // snprintf_P(param1, sizeof(param1), PSTR("km/h")); - // } else { - // snprintf_P(param1, sizeof(param1), PSTR("%s"), SpeedUnit().c_str()); - // } - // break; } char param2[50]; GetTextIndexed(param2, sizeof(param2), sensor_index, kHAssJsonSensorDevCla); TryResponseAppend_P(HASS_DISCOVER_SENSOR, param1, param2, sensorname, subsensortype); - if (subidx) { - TryResponseAppend_P(PSTR("[%d]"), subqty -1); - } } else { TryResponseAppend_P(HASS_DISCOVER_SENSOR, " ", "ic\":\"mdi:eye", sensorname, subsensortype); } - if (nested) { - TryResponseAppend_P(PSTR("['%s']"), SubKey); - } + + if (nested) { TryResponseAppend_P(PSTR("['%s']"), SubKey); } + + if (subqty != 0) { TryResponseAppend_P(PSTR("[%d]"), subqty -1); } + TryResponseAppend_P(PSTR("}}\"}")); } MqttPublish(stopic, true); @@ -687,57 +784,66 @@ void HAssAnnounceSensors(void) tele_period = 2; // Do not allow HA updates during next function call XsnsNextCall(FUNC_JSON_APPEND, hass_xsns_index); // ,"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089} tele_period = tele_period_save; + size_t sensordata_len = strlen(mqtt_data); + char sensordata[sensordata_len+2]; // dynamically adjust the size + strcpy(sensordata, mqtt_data); // we can use strcpy since the buffer has the right size - char sensordata[512]; // Copy because we need to write to mqtt_data - strlcpy(sensordata, mqtt_data, sizeof(sensordata)); + // ******************* JSON TEST ******************* + // char sensordata[512]; + // snprintf_P(sensordata, sizeof(sensordata), PSTR("{\"ENERGY\":{\"TotalStartTime\":\"2018-11-23T15:33:47\",\"ExportTariff\":[0.000,0.017],\"Speed\":{\"Act\":\"NE\"}}}")); + // size_t sensordata_len = strlen(sensordata); + // ******************* JSON TEST ******************* - if (strlen(sensordata)) + if (sensordata_len > 0) { + // // We replace the leader ',' with '{' sensordata[0] = '{'; - snprintf_P(sensordata, sizeof(sensordata), PSTR("%s}"), sensordata); // {"INA219":{"Voltage":4.494,"Current":0.020,"Power":0.089}} - // USE THE FOLLOWING LINE TO TEST JSON - //snprintf_P(sensordata, sizeof(sensordata), PSTR("{\"APDS9960\":{\"Red\":282,\"Green\":252,\"Blue\":196,\"Ambient\":169,\"CCT\":4217,\"Proximity\":9}}")); - //snprintf_P(sensordata, sizeof(sensordata), PSTR("{\"ENERGY\":{\"TotalStartTime\":\"2018-11-23T15:33:47\",\"Total\":0.017,\"TotalTariff\":[0.000,0.017],\"Yesterday\":0.000,\"Today\":0.002,\"ExportActive\":0.000,\"ExportTariff\":[0.000,0.000],\"Period\":0.00,\"Power\":0.00,\"ApparentPower\":7.84,\"ReactivePower\":-7.21,\"Factor\":0.39,\"Frequency\":50.0,\"Voltage\":234.31,\"Current\":0.039,\"ImportActive\":12.580,\"ImportReactive\":0.002,\"ExportReactive\":39.131,\"PhaseAngle\":290.45}}")); + // // and we add a trailing '}' after the last '}' + sensordata[sensordata_len] = '}'; + sensordata[sensordata_len+1] = '\0'; - StaticJsonBuffer<500> jsonBuffer; - JsonObject &root = jsonBuffer.parseObject(sensordata); - if (!root.success()) + JsonParser parser(sensordata); + JsonParserObject root = parser.getRootObject(); + if (!root) { - AddLog_P2(LOG_LEVEL_ERROR, PSTR("%s '%s'"), kHAssError3, sensordata); + AddLog_P2(LOG_LEVEL_ERROR, PSTR("%s '%s' (ERR1)"), kHAssError3, sensordata); continue; } - for (auto sensor : root) + for (auto sensor_key : root) { - const char *sensorname = sensor.key; - JsonObject &sensors = sensor.value.as(); - if (!sensors.success()) + // sensor is of type JsonParserKey + const char *sensorname = sensor_key.getStr(); + JsonParserObject sensors = sensor_key.getValue().getObject(); + + if (!sensors) { - AddLog_P2(LOG_LEVEL_ERROR, PSTR("%s '%s'"), kHAssError3, sensordata); + AddLog_P2(LOG_LEVEL_ERROR, PSTR("%s '%s' (ERR2)"), kHAssError3, sensorname); continue; } - for (auto subsensor : sensors) + for (auto subsensor_key_token : sensors) { - if (subsensor.value.is()) { + const char * subsensor_key = subsensor_key_token.getStr(); + JsonParserToken subsensor = subsensor_key_token.getValue(); + if (subsensor.isObject()) { // If there is a nested json on sensor data, second level entitites will be created - char NestedName[20]; + JsonParserObject subsensors = subsensor.getObject(); char NewSensorName[20]; - snprintf_P(NestedName, sizeof(NestedName), PSTR("%s"), subsensor.key); - JsonObject& subsensors = subsensor.value.as(); - for (auto subsensor : subsensors) { - snprintf_P(NewSensorName, sizeof(NewSensorName), PSTR("%s %s"), NestedName, subsensor.key); - HAssAnnounceSensor(sensorname, NestedName, NewSensorName, 0, 0, 1, subsensor.key); + for (auto subsensor2_key : subsensors) { + snprintf_P(NewSensorName, sizeof(NewSensorName), PSTR("%s %s"), subsensor_key, subsensor2_key.getStr()); + HAssAnnounceSensor(sensorname, subsensor_key, NewSensorName, 0, 1, subsensor2_key.getStr()); } - } else if (subsensor.value.is()) { + } else if (subsensor.isArray()) { // If there is more than a value on sensor data, 'n' entitites will be created - JsonArray& subsensors = subsensor.value.as(); + JsonParserArray subsensors = subsensor.getArray(); uint8_t subqty = subsensors.size(); char MultiSubName[20]; for (int i = 1; i <= subqty; i++) { - snprintf_P(MultiSubName, sizeof(MultiSubName), PSTR("%s %d"), subsensor.key, i); - HAssAnnounceSensor(sensorname, subsensor.key, MultiSubName, i, 1, 0, subsensor.key); + snprintf_P(MultiSubName, sizeof(MultiSubName), PSTR("%s %d"), subsensor_key, i); + HAssAnnounceSensor(sensorname, subsensor_key, MultiSubName, i, 0, subsensor_key); } - } else { HAssAnnounceSensor(sensorname, subsensor.key, subsensor.key, 0, 0, 0, subsensor.key);} + } else { + HAssAnnounceSensor(sensorname, subsensor_key, subsensor_key, 0, 0, subsensor_key);} } } } @@ -752,15 +858,18 @@ void HAssAnnounceShutters(void) char stemp1[TOPSZ]; char stemp2[TOPSZ]; char unique_id[30]; + uint8_t ShowTopic; // Used to hide/unhide a topic during Discovery to spare some cpu load for (uint32_t i = 0; i < MAX_SHUTTERS; i++) { mqtt_data[0] = '\0'; // Clear retained message + masterlog_level = ShowTopic = 4; // Hide topic on clean and remove use weblog 4 to see it + snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_SHT_%d"), ESP_getChipId(), i + 1); snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/cover/%s/config"), unique_id); if (Settings.flag.hass_discovery && Settings.flag3.shutter_mode && Settings.shutter_startrelay[i] > 0 && Settings.shutter_startrelay[i] <= MAX_RELAYS) { - masterlog_level = 0; + ShowTopic = 0; // Show the new generated topic if (i > MAX_FRIENDLYNAMES) { snprintf_P(stemp1, sizeof(stemp1), PSTR("%s Shutter %d"), SettingsText(SET_DEVICENAME), i + 1); } else { @@ -783,8 +892,8 @@ void HAssAnnounceShutters(void) TryResponseAppend_P(PSTR("}")); } + masterlog_level = ShowTopic; MqttPublish(stopic, true); - masterlog_level = 4; } #endif } @@ -795,9 +904,11 @@ void HAssAnnounceDeviceInfoAndStatusSensor(void) char stemp1[TOPSZ]; char stemp2[TOPSZ]; char unique_id[30]; + uint8_t ShowTopic; // Used to hide/unhide a topic during Discovery to spare some cpu load + // Announce sensor mqtt_data[0] = '\0'; // Clear retained message - + masterlog_level = ShowTopic = 4; // Hide topic on clean and remove use weblog 4 to see it // Clear or Set topic snprintf_P(unique_id, sizeof(unique_id), PSTR("%06X_status"), ESP_getChipId()); snprintf_P(stopic, sizeof(stopic), PSTR(HOME_ASSISTANT_DISCOVERY_PREFIX "/sensor/%s/config"), unique_id); @@ -808,7 +919,7 @@ void HAssAnnounceDeviceInfoAndStatusSensor(void) char prefix[TOPSZ]; char *state_topic = stemp1; char *availability_topic = stemp2; - masterlog_level = 0; + ShowTopic = 0; // Show the new generated topic snprintf_P(name, sizeof(name), PSTR("%s status"), SettingsText(SET_DEVICENAME)); GetTopic_P(state_topic, TELE, mqtt_topic, PSTR(D_RSLT_HASS_STATE)); GetTopic_P(availability_topic, TELE, mqtt_topic, S_LWT); @@ -820,10 +931,12 @@ void HAssAnnounceDeviceInfoAndStatusSensor(void) ModuleName().c_str(), my_version, my_image); TryResponseAppend_P(PSTR("}")); } + masterlog_level = ShowTopic; MqttPublish(stopic, true); + if (!Settings.flag.hass_discovery) { masterlog_level = 0; - AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_LOG "Home Assistant Discovery disabled. ")); + AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_LOG "Home Assistant MQTT Discovery disabled.")); } } @@ -843,18 +956,18 @@ void HAssDiscovery(void) { // Configure Tasmota for default Home Assistant parameters to keep discovery message as short as possible if (Settings.flag.hass_discovery) - { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59) - Settings.flag.mqtt_response = 0; // SetOption4 - Switch between MQTT RESULT or COMMAND - Response always as RESULT and not as uppercase command - Settings.flag.decimal_text = 1; // SetOption17 - Switch between decimal or hexadecimal output - Respond with decimal color values - Settings.flag3.hass_tele_on_power = 1; // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT - send tele/STATE message as stat/RESULT - // the purpose of that is so that if HA is restarted, state in HA will be correct within one teleperiod otherwise state - // will not be correct until the device state is changed this is why in the patterns for switch and light, we tell HA to trigger on STATE, not RESULT. - //Settings.light_scheme = 0; // To just control color it needs to be Scheme 0 (on hold due to new light configuration) + { // SetOption19 - Control Home Assistant automatic discovery (See SetOption59) + Settings.flag.mqtt_response = 0; // SetOption4 - Switch between MQTT RESULT or COMMAND - Response always as RESULT and not as uppercase command + Settings.flag.decimal_text = 1; // SetOption17 - Switch between decimal or hexadecimal output - Respond with decimal color values + Settings.flag3.hass_tele_on_power = 1; // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT - send tele/STATE message as stat/RESULT + // the purpose of that is so that if HA is restarted, state in HA will be correct within one teleperiod otherwise state + // will not be correct until the device state is changed this is why in the patterns for switch and light, we tell HA to trigger on STATE, not RESULT. + //Settings.light_scheme = 0; // To just control color it needs to be Scheme 0 (on hold due to new light configuration) } if (Settings.flag.hass_discovery || (1 == hass_mode)) { // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59) - masterlog_level = 4; + hass_mode = 2; // Send info about buttons HAssAnnounceButtons(); @@ -872,8 +985,8 @@ void HAssDiscovery(void) // Send info about status sensor HAssAnnounceDeviceInfoAndStatusSensor(); - - masterlog_level = Settings.weblog_level; + masterlog_level = 0; // Restores weblog level + hass_mode = 3; } } @@ -885,10 +998,7 @@ void HAssDiscover(void) void HAssAnyKey(void) { - if (!Settings.flag.hass_discovery) - { - return; - } // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59) + if (!Settings.flag.hass_discovery) { return; } // SetOption19 - Control Home Assistantautomatic discovery (See SetOption59) uint32_t key = (XdrvMailbox.payload >> 16) & 0xFF; // 0 = Button, 1 = Switch uint32_t device = XdrvMailbox.payload & 0xFF; // Device number or 1 if more Buttons than Devices @@ -933,14 +1043,14 @@ bool HAssMqttLWT(void) if (Settings.flag.hass_discovery && (strncasecmp_P(XdrvMailbox.data, PSTR("online"), strlen("online")) == 0) && (XdrvMailbox.data_len == 6)) { MqttPublishTeleState(); return true; - } + } else { return false; } } void HassLwtSubscribe(bool hasslwt) { char htopic[TOPSZ]; snprintf_P(htopic, sizeof(htopic), PSTR(HOME_ASSISTANT_LWT_TOPIC)); - if (hasslwt && Settings.flag.hass_discovery) { + if (hasslwt && (Settings.flag.hass_discovery)) { MqttSubscribe(htopic); } else { MqttUnsubscribe(htopic); } } @@ -983,6 +1093,9 @@ bool Xdrv12(uint8_t function) case FUNC_MQTT_INIT: hass_mode = 0; // Discovery only if Settings.flag.hass_discovery is set hass_init_step = 2; // Delayed discovery + if (!Settings.flag.hass_discovery) { + NewHAssDiscovery(); + } break; case FUNC_MQTT_SUBSCRIBE: diff --git a/tasmota/xdrv_13_display.ino b/tasmota/xdrv_13_display.ino index 3a87826f3..1042cc463 100644 --- a/tasmota/xdrv_13_display.ino +++ b/tasmota/xdrv_13_display.ino @@ -506,7 +506,7 @@ void DisplayText(void) } } break; -#endif +#endif // USE_SCRIPT_FATFS case 'h': // hor line to var = atoiv(cp, &temp); @@ -624,12 +624,19 @@ void DisplayText(void) } } break; - case 'T': + case 'T': { + uint8_t param1 = RtcTime.day_of_month; + uint8_t param2 = RtcTime.month; + if (*cp=='U') { + cp++; + param1 = RtcTime.month; + param2 = RtcTime.day_of_month; + } if (dp < (linebuf + DISPLAY_BUFFER_COLS) -8) { - snprintf_P(dp, 9, PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%02d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year%2000); + snprintf_P(dp, 9, PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%02d"), param1, param2, RtcTime.year%2000); dp += 8; } - break; + break; } case 'd': // force draw grafics buffer if (renderer) renderer->Updateframe(); @@ -696,7 +703,7 @@ void DisplayText(void) Restore_graph(temp,bbuff); break; } -#endif +#endif // USE_SCRIPT_FATFS { int16_t num,gxp,gyp,gxs,gys,dec,icol; float ymin,ymax; var=atoiv(cp,&num); @@ -744,7 +751,7 @@ void DisplayText(void) AddValue(num,temp); } break; -#endif +#endif // USE_GRAPH #ifdef USE_AWATCH case 'w': @@ -752,7 +759,7 @@ void DisplayText(void) cp += var; DrawAClock(temp); break; -#endif +#endif // USE_AWATCH #ifdef USE_TOUCH_BUTTONS case 'b': @@ -834,12 +841,13 @@ void DisplayText(void) buttons[num]->vpower.is_pushbutton=0; } if (dflg) buttons[num]->xdrawButton(buttons[num]->vpower.on_off); + buttons[num]->vpower.disable=!dflg; } } } } break; -#endif +#endif // USE_TOUCH_BUTTONS default: // unknown escape Response_P(PSTR("Unknown Escape")); @@ -1157,50 +1165,45 @@ void DisplayAnalyzeJson(char *topic, char *json) // tele/wemos5/SENSOR {"Time":"2017-09-20T11:53:53","SHT1X":{"Temperature":20.1,"Humidity":58.9},"HTU21":{"Temperature":20.7,"Humidity":58.5},"BMP280":{"Temperature":21.6,"Pressure":1020.3},"TempUnit":"C"} // tele/th1/SENSOR {"Time":"2017-09-20T11:54:48","DS18B20":{"Temperature":49.7},"TempUnit":"C"} - -// char jsonStr[MESSZ]; -// strlcpy(jsonStr, json, sizeof(jsonStr)); // Save original before destruction by JsonObject String jsonStr = json; // Move from stack to heap to fix watchdogs (20180626) - StaticJsonBuffer<1024> jsonBuf; - JsonObject &root = jsonBuf.parseObject(jsonStr); - if (root.success()) { + JsonParser parser((char*)jsonStr.c_str()); + JsonParserObject root = parser.getRootObject(); + if (root) { // did JSON parsing went ok? - const char *unit; - unit = root[D_JSON_TEMPERATURE_UNIT]; + const char *unit = root.getStr(PSTR(D_JSON_TEMPERATURE_UNIT), nullptr); // nullptr if not found if (unit) { snprintf_P(disp_temp, sizeof(disp_temp), PSTR("%s"), unit); // C or F } - unit = root[D_JSON_PRESSURE_UNIT]; + unit = root.getStr(PSTR(D_JSON_PRESSURE_UNIT), nullptr); // nullptr if not found if (unit) { snprintf_P(disp_pres, sizeof(disp_pres), PSTR("%s"), unit); // hPa or mmHg } - - for (JsonObject::iterator it = root.begin(); it != root.end(); ++it) { - JsonVariant value = it->value; - if (value.is()) { - JsonObject& Object2 = value; - for (JsonObject::iterator it2 = Object2.begin(); it2 != Object2.end(); ++it2) { - JsonVariant value2 = it2->value; - if (value2.is()) { - JsonObject& Object3 = value2; - for (JsonObject::iterator it3 = Object3.begin(); it3 != Object3.end(); ++it3) { - const char* value = it3->value; - if (value != nullptr) { // "DHT11":{"Temperature":null,"Humidity":null} - ignore null as it will raise exception 28 - DisplayJsonValue(topic, it->key, it3->key, value); // Sensor 56% + for (auto key1 : root) { + JsonParserToken value1 = key1.getValue(); + if (value1.isObject()) { + JsonParserObject Object2 = value1.getObject(); + for (auto key2 : Object2) { + JsonParserToken value2 = key2.getValue(); + if (value2.isObject()) { + JsonParserObject Object3 = value2.getObject(); + for (auto key3 : Object3) { + const char* value3 = key3.getValue().getStr(nullptr); + if (value3 != nullptr) { // "DHT11":{"Temperature":null,"Humidity":null} - ignore null as it will raise exception 28 + DisplayJsonValue(topic, key1.getStr(), key3.getStr(), value3); // Sensor 56% } } } else { - const char* value = it2->value; + const char* value = value2.getStr(nullptr); if (value != nullptr) { - DisplayJsonValue(topic, it->key, it2->key, value); // Sensor 56% + DisplayJsonValue(topic, key1.getStr(), key2.getStr(), value); // Sensor 56% } } } } else { - const char* value = it->value; + const char* value = value1.getStr(nullptr); if (value != nullptr) { - DisplayJsonValue(topic, it->key, it->key, value); // Topic 56% + DisplayJsonValue(topic, key1.getStr(), key1.getStr(), value); // Topic 56% } } } @@ -1530,8 +1533,8 @@ void CmndDisplayRows(void) bool jpg2rgb888(const uint8_t *src, size_t src_len, uint8_t * out, jpg_scale_t scale); char get_jpeg_size(unsigned char* data, unsigned int data_size, unsigned short *width, unsigned short *height); void rgb888_to_565(uint8_t *in, uint16_t *out, uint32_t len); -#endif -#endif +#endif // JPEG_PICTS +#endif // ESP32 #if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) extern FS *fsp; @@ -1626,7 +1629,7 @@ void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) { #endif // ESP32 } } -#endif +#endif // USE_SCRIPT_FATFS #ifdef USE_AWATCH #define MINUTE_REDUCT 4 @@ -1663,7 +1666,7 @@ void DrawAClock(uint16_t rad) { temp=((float)RtcTime.minute*(pi/30.0)-(pi/2.0)); renderer->writeLine(disp_xpos, disp_ypos,disp_xpos+(frad-MINUTE_REDUCT)*cosf(temp),disp_ypos+(frad-MINUTE_REDUCT)*sinf(temp), fg_color); } -#endif +#endif // USE_AWATCH #ifdef USE_GRAPH @@ -1938,7 +1941,7 @@ void Restore_graph(uint8_t num, char *path) { fp.close(); RedrawGraph(num,1); } -#endif +#endif // USE_SCRIPT_FATFS void RedrawGraph(uint8_t num, uint8_t flags) { uint16_t index=num%NUM_GRAPHS; @@ -2050,16 +2053,13 @@ void AddValue(uint8_t num,float fval) { #ifdef USE_FT5206 +#include // touch panel controller #undef FT5206_address #define FT5206_address 0x38 -#include FT5206_Class *touchp; TP_Point pLoc; - - -extern VButton *buttons[]; bool FT5206_found; bool Touch_Init(TwoWire &i2c) { @@ -2088,6 +2088,7 @@ uint32_t Touch_Status(uint32_t sel) { } } + #ifdef USE_TOUCH_BUTTONS void Touch_MQTT(uint8_t index, const char *cp) { ResponseTime_P(PSTR(",\"FT5206\":{\"%s%d\":\"%d\"}}"), cp, index+1, buttons[index]->vpower.on_off); @@ -2184,6 +2185,7 @@ uint8_t vbutt=0; pLoc.y = 0; } } + #endif // USE_TOUCH_BUTTONS #endif // USE_FT5206 diff --git a/tasmota/xdrv_16_tuyamcu.ino b/tasmota/xdrv_16_tuyamcu.ino index b4b9a5ff4..c58dc2130 100644 --- a/tasmota/xdrv_16_tuyamcu.ino +++ b/tasmota/xdrv_16_tuyamcu.ino @@ -1,7 +1,7 @@ /* xdrv_16_tuyamcu.ino - Tuya MCU support for Tasmota - Copyright (C) 2020 digiblur, Joel Stein and Theo Arends + Copyright (C) 2020 Federico Leoni, digiblur, Joel Stein and Theo Arends This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -55,56 +55,32 @@ TasmotaSerial *TuyaSerial = nullptr; struct TUYA { - uint16_t new_dim = 0; // Tuya dimmer value temp - bool ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction - uint8_t cmd_status = 0; // Current status of serial-read - uint8_t cmd_checksum = 0; // Checksum of tuya command - uint8_t data_len = 0; // Data lenght of command + uint16_t Levels[5] = {0,0,0,0,0}; // Array to store the values of TuyaMCU channels + uint16_t Snapshot[5] = {0,0,0,0,0}; // Array to store a snapshot of Tasmota actual values for channels + char HSBColor[13]; // Stores HSB Color string in Hex format + uint16_t CTMin = 153; // Minimum CT level allowed - When SetOption82 is enabled will default to 200 + uint16_t CTMax = 500; // Maximum CT level allowed - When SetOption82 is enabled will default to 380 + bool ModeSet = false; // Controls 0 - Single Tone light, 1 - RGB Light + uint8_t FanState = 0; // Stores the current fan speed + bool SuspendTopic = false; // Used to reduce the load at init time or when polling the configuraton on demand + uint32_t ignore_topic_timeout = 0; // Suppress the /STAT topic (if enabled) to avoid data overflow until the configuration is over + bool ignore_dim = false; // Flag to skip serial send to prevent looping when processing inbound states from the faceplate interaction + uint8_t cmd_status = 0; // Current status of serial-read + uint8_t cmd_checksum = 0; // Checksum of tuya command + uint8_t data_len = 0; // Data lenght of command uint8_t wifi_state = -2; // Keep MCU wifi-status in sync with WifiState() - uint8_t heartbeat_timer = 0; // 10 second heartbeat timer for tuya module + uint8_t heartbeat_timer = 0; // 10 second heartbeat timer for tuya module #ifdef USE_ENERGY_SENSOR - uint32_t lastPowerCheckTime = 0; // Time when last power was checked + uint32_t lastPowerCheckTime = 0; // Time when last power was checked #endif // USE_ENERGY_SENSOR - char *buffer = nullptr; // Serial receive buffer - int byte_counter = 0; // Index in serial receive buffer - bool low_power_mode = false; // Normal or Low power mode protocol - bool send_success_next_second = false; // Second command success in low power mode - uint32_t ignore_dimmer_cmd_timeout = 0;// Time until which received dimmer commands should be ignored + char *buffer = nullptr; // Serial receive buffer + int byte_counter = 0; // Index in serial receive buffer + bool low_power_mode = false; // Normal or Low power mode protocol + bool send_success_next_second = false; // Second command success in low power mode + uint32_t ignore_dimmer_cmd_timeout = 0; // Time until which received dimmer commands should be ignored + bool ignore_tuyareceived = false; // When a modeset changes ignore stat } Tuya; - -// enum TuyaSupportedFunctions { -// TUYA_MCU_FUNC_NONE, -// TUYA_MCU_FUNC_SWT1 = 1, // Buttons -// TUYA_MCU_FUNC_SWT2, -// TUYA_MCU_FUNC_SWT3, -// TUYA_MCU_FUNC_SWT4, -// TUYA_MCU_FUNC_REL1 = 11, // Relays -// TUYA_MCU_FUNC_REL2, -// TUYA_MCU_FUNC_REL3, -// TUYA_MCU_FUNC_REL4, -// TUYA_MCU_FUNC_REL5, -// TUYA_MCU_FUNC_REL6, -// TUYA_MCU_FUNC_REL7, -// TUYA_MCU_FUNC_REL8, -// TUYA_MCU_FUNC_DIMMER = 21, -// TUYA_MCU_FUNC_POWER = 31, -// TUYA_MCU_FUNC_CURRENT, -// TUYA_MCU_FUNC_VOLTAGE, -// TUYA_MCU_FUNC_BATTERY_STATE, -// TUYA_MCU_FUNC_BATTERY_PERCENTAGE, -// TUYA_MCU_FUNC_REL1_INV = 41, // Inverted Relays -// TUYA_MCU_FUNC_REL2_INV, -// TUYA_MCU_FUNC_REL3_INV, -// TUYA_MCU_FUNC_REL4_INV, -// TUYA_MCU_FUNC_REL5_INV, -// TUYA_MCU_FUNC_REL6_INV, -// TUYA_MCU_FUNC_REL7_INV, -// TUYA_MCU_FUNC_REL8_INV, -// TUYA_MCU_FUNC_LOWPOWER_MODE = 51, -// TUYA_MCU_FUNC_LAST = 255 -// }; - const char kTuyaCommand[] PROGMEM = "|" // No prefix D_CMND_TUYA_MCU "|" D_CMND_TUYA_MCU_SEND_STATE; @@ -112,8 +88,48 @@ void (* const TuyaCommand[])(void) PROGMEM = { &CmndTuyaMcu, &CmndTuyaSend }; -/* +/*********************************************************************************************\ + * Web Interface +\*********************************************************************************************/ +bool IsModuleTuya(void) +{ + return ((TUYA_DIMMER == my_module_type) || (SK03_TUYA == my_module_type)); +} +bool AsModuleTuyaMS(void) // ModeSet Layout +{ + return ((light_type > LT_RGB) && TuyaGetDpId(TUYA_MCU_FUNC_MODESET) != 0); +} + +bool IsTuyaFanCtrl(void) // Fan Speed Controller Layout +{ + return ((TuyaGetDpId(TUYA_MCU_FUNC_FAN3) != 0) || (TuyaGetDpId(TUYA_MCU_FUNC_FAN4) != 0) || + (TuyaGetDpId(TUYA_MCU_FUNC_FAN5) != 0) || (TuyaGetDpId(TUYA_MCU_FUNC_FAN6) != 0)); +} + +bool TuyaModeSet(void) // ModeSet Status +{ + return Tuya.ModeSet; +} + +uint8_t TuyaFanSpeeds(void) // Number of Fan Speeds for WebUI +{ + uint8_t FanSpeeds = 0; + for (uint32_t i = 0; i <= 3; i++) { + if (TuyaGetDpId(TUYA_MCU_FUNC_FAN3 + i) != 0) { + FanSpeeds = i + 2; + } + } + return FanSpeeds; +} + +uint8_t TuyaFanState(void) // Fan Speed Status +{ + return Tuya.FanState; +} +// Web Interface + +/* TuyaSend dpId,data TuyaSend0 -> Sends TUYA_CMD_QUERY_STATE @@ -122,16 +138,20 @@ TuyaSend2 11,100 -> Sends integer (Type 2) data 100 to dpId 11 (Max data length TuyaSend2 11,0xAABBCCDD -> Sends 4 bytes (Type 2) data to dpId 11 (Max data length 4 bytes) TuyaSend3 11,ThisIsTheData -> Sends the supplied string (Type 3) to dpId 11 ( Max data length not-known) TuyaSend4 11,1 -> Sends enum (Type 4) data 0/1/2/3/4/5 to dpId 11 (Max data length 1 bytes) - */ - void CmndTuyaSend(void) { - if (XdrvMailbox.index > 4) { + if (XdrvMailbox.index > 4 && XdrvMailbox.index < 8) { return; } if (XdrvMailbox.index == 0) { - TuyaRequestState(); + TuyaRequestState(0); + } else if (XdrvMailbox.index == 8) { + TuyaRequestState(8); + } else if (XdrvMailbox.index == 9) { // TuyaSend Topic Toggle + if (Settings.tuyamcu_topic) { Settings.tuyamcu_topic = 0; } else { Settings.tuyamcu_topic = 1; } + AddLog_P2(LOG_LEVEL_INFO, PSTR("TYA: TuyaMCU Stat Topic %s"), (Settings.tuyamcu_topic ? PSTR("enabled") : PSTR("disabled"))); + } else { if (XdrvMailbox.data_len > 0) { char *p; @@ -161,11 +181,7 @@ void CmndTuyaSend(void) { ResponseCmndDone(); } -/* - -TuyaMcu fnid,dpid - -*/ +// TuyaMcu fnid,dpid void CmndTuyaMcu(void) { if (XdrvMailbox.data_len > 0) { @@ -178,12 +194,28 @@ void CmndTuyaMcu(void) { } if (TuyaFuncIdValid(parm[0])) { + // TuyaAddMcuFunc(parm[0], parm[1]); + // restart_flag = 2; + // } else { + // AddLog_P2(LOG_LEVEL_ERROR, PSTR("TYA: TuyaMcu Invalid function id=%d"), parm[0]); + // } + bool DualDim; + if (TUYA_MCU_FUNC_DIMMER2 == parm[0] && parm[1] != 0) { + if (TuyaGetDpId(TUYA_MCU_FUNC_DIMMER) != 0) { DualDim = true; } + } else if (TUYA_MCU_FUNC_DIMMER == parm[0] && parm[1] != 0) { + if (TuyaGetDpId(TUYA_MCU_FUNC_DIMMER2) != 0) { DualDim = true; } + } else if ((TUYA_MCU_FUNC_DIMMER == parm[0] && parm[1] == 0) || (TUYA_MCU_FUNC_DIMMER2 == parm[0] && parm[1] == 0)) { DualDim = false; }; + if (DualDim) { + if (TuyaGetDpId(TUYA_MCU_FUNC_CT) != 0) { TuyaAddMcuFunc(TUYA_MCU_FUNC_CT, 0); } // If the second dimmer is enabled CT, RGB or WHITE function must be removed + if (TuyaGetDpId(TUYA_MCU_FUNC_RGB) != 0) { TuyaAddMcuFunc(TUYA_MCU_FUNC_RGB, 0); } + if (TuyaGetDpId(TUYA_MCU_FUNC_WHITE) != 0) { TuyaAddMcuFunc(TUYA_MCU_FUNC_WHITE, 0); } + Settings.flag3.pwm_multi_channels = 1; + } else { Settings.flag3.pwm_multi_channels = 0; } TuyaAddMcuFunc(parm[0], parm[1]); restart_flag = 2; } else { AddLog_P2(LOG_LEVEL_ERROR, PSTR("TYA: TuyaMcu Invalid function id=%d"), parm[0]); } - } Response_P(PSTR("{\"" D_CMND_TUYA_MCU "\":[")); @@ -249,11 +281,13 @@ void UpdateDevices() { inline bool TuyaFuncIdValid(uint8_t fnId) { return (fnId >= TUYA_MCU_FUNC_SWT1 && fnId <= TUYA_MCU_FUNC_SWT4) || - (fnId >= TUYA_MCU_FUNC_REL1 && fnId <= TUYA_MCU_FUNC_REL8) || - fnId == TUYA_MCU_FUNC_DIMMER || - (fnId >= TUYA_MCU_FUNC_POWER && fnId <= TUYA_MCU_FUNC_VOLTAGE) || - (fnId >= TUYA_MCU_FUNC_REL1_INV && fnId <= TUYA_MCU_FUNC_REL8_INV) || - (fnId == TUYA_MCU_FUNC_LOWPOWER_MODE); + (fnId >= TUYA_MCU_FUNC_REL1 && fnId <= TUYA_MCU_FUNC_REL8) || + (fnId >= TUYA_MCU_FUNC_DIMMER && fnId <= TUYA_MCU_FUNC_REPORT2) || + (fnId >= TUYA_MCU_FUNC_POWER && fnId <= TUYA_MCU_FUNC_BATTERY_PERCENTAGE) || + (fnId >= TUYA_MCU_FUNC_REL1_INV && fnId <= TUYA_MCU_FUNC_REL8_INV) || + (fnId >= TUYA_MCU_FUNC_FAN3 && fnId <= TUYA_MCU_FUNC_FAN6) || + (fnId >= TUYA_MCU_FUNC_MOTOR_DIR && fnId <= TUYA_MCU_FUNC_DUMMY) || + (fnId == TUYA_MCU_FUNC_LOWPOWER_MODE); } uint8_t TuyaGetFuncId(uint8_t dpid) { @@ -376,38 +410,137 @@ bool TuyaSetPower(void) bool TuyaSetChannels(void) { - LightSerialDuty(((uint8_t*)XdrvMailbox.data)[0]); - //delay(20); // Hack when power is off and dimmer is set then both commands go too soon to Serial out. + uint16_t hue, TuyaData; + uint8_t sat, bri; + uint8_t TuyaIdx = 0; + char hex_char[13]; + bool noupd = false; + bool LightMode = TuyaGetDpId(TUYA_MCU_FUNC_MODESET) != 0; + uint8_t idx = 0; + snprintf_P(hex_char, sizeof(hex_char), PSTR("000000000000")); + + if (LT_SERIAL1 == light_type) { + Tuya.Snapshot[0] = light_state.getDimmer(); + } + if (LT_SERIAL2 == light_type || LT_RGBWC == light_type) { + idx = 1; + if (LT_SERIAL2 == light_type && Settings.flag3.pwm_multi_channels && (TuyaGetDpId(TUYA_MCU_FUNC_DIMMER2) != 0)) { + // Special setup for dual dimmer (like the MOES 2 Way Dimmer) emulating 2 PWM channels + Tuya.Snapshot[0] = changeUIntScale(Light.current_color[0], 0, 255, 0, 100); + Tuya.Snapshot[1] = changeUIntScale(Light.current_color[1], 0, 255, 0, 100); + } else { // CT Light or RGBWC + light_state.getCTRange(&Tuya.CTMin, &Tuya.CTMax); // SetOption82 - Reduce the CT range from 153..500 to 200..380 to accomodate with Alexa range + Tuya.Snapshot[0] = light_state.getDimmer(); + Tuya.Snapshot[1] = light_state.getCT(); + } + } + if (LT_RGBW == light_type) { + idx = 1; + Tuya.Snapshot[0] = light_state.getDimmer(1); + Tuya.Snapshot[1] = light_state.getDimmer(2); + } + + if (light_type > LT_BASIC) { + + if (LT_RGB != light_type) { + for (uint8_t i = 0; i <= idx; i++) { + if (Tuya.Snapshot[i] != Tuya.Levels[i]) { + if (i == 0 && LightMode && Tuya.ModeSet ) { noupd = true;} + if (!noupd) { + LightSerialDuty(Tuya.Snapshot[i], &hex_char[0], i+1); + Tuya.Levels[i] = Tuya.Snapshot[i]; + } + noupd = false; + } + } + } + + if (light_type >= LT_RGB) { + + light_state.getHSB(&hue, &sat, &bri); + sat = changeUIntScale(sat, 0, 255, 0, 100); + bri = changeUIntScale(bri, 0, 255, 0, 100); + if (hue != Tuya.Snapshot[2] || sat != Tuya.Snapshot[3] || bri != Tuya.Snapshot[4]) { + if ((LightMode && Tuya.ModeSet) || LT_RGB == light_type) { + snprintf_P(hex_char, sizeof(hex_char), PSTR("%04X%04X%04X"), hue, sat * 10, bri * 10); // Create a TuyaMCU readable RGB payload + LightSerialDuty(0, &hex_char[0], 3); + memcpy_P(Tuya.HSBColor, hex_char, strlen(hex_char)); + Tuya.Snapshot[2] = hue; + Tuya.Snapshot[3] = sat; + Tuya.Snapshot[4] = bri; + } + } + } + } return true; } -void LightSerialDuty(uint16_t duty) +void LightSerialDuty(uint16_t duty, char *hex_char, uint8_t TuyaIdx) { uint8_t dpid = TuyaGetDpId(TUYA_MCU_FUNC_DIMMER); - if (duty > 0 && !Tuya.ignore_dim && TuyaSerial && dpid > 0) { - duty = changeUIntScale(duty, 0, 255, 0, Settings.dimmer_hw_max); - if (duty < Settings.dimmer_hw_min) { duty = Settings.dimmer_hw_min; } // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself - if (Tuya.new_dim != duty) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim value=%d (id=%d)"), duty, dpid); - Tuya.ignore_dimmer_cmd_timeout = millis() + 250; // Ignore serial received dim commands for the next 250ms - TuyaSendValue(dpid, duty); + bool CTLight = false; + + if (TuyaIdx > 0 && TuyaIdx <= 2) { + + if (TuyaIdx == 2) { + if (!Settings.flag3.pwm_multi_channels) { + CTLight = true; + dpid = TuyaGetDpId(TUYA_MCU_FUNC_CT); + } else { dpid = TuyaGetDpId(TUYA_MCU_FUNC_DIMMER2); } } - } else if (dpid > 0) { - Tuya.ignore_dim = false; // reset flag - duty = changeUIntScale(duty, 0, 255, 0, Settings.dimmer_hw_max); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim skipped value=%d"), duty); // due to 0 or already set - } else { - AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Cannot set dimmer. Dimmer Id unknown")); // + + if (duty > 0 && !Tuya.ignore_dim && TuyaSerial && dpid > 0) { + if (TuyaIdx == 2 && CTLight) { + duty = changeUIntScale(duty, Tuya.CTMin, Tuya.CTMax, Settings.dimmer_hw_max, 0); + } else { duty = changeUIntScale(duty, 0, 100, 0, Settings.dimmer_hw_max); } + + if (duty < Settings.dimmer_hw_min) { duty = Settings.dimmer_hw_min; } // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself + Tuya.ignore_dimmer_cmd_timeout = millis() + 250; // Ignore serial received dim commands for the next 250ms + if (Tuya.ModeSet && (TuyaGetDpId(TUYA_MCU_FUNC_MODESET) != 0) && light_type > LT_RGB) { + TuyaSendEnum(TuyaGetDpId(TUYA_MCU_FUNC_MODESET), 0); + } + TuyaSendValue(dpid, duty); + + } else if (dpid > 0 && TuyaIdx <= 2) { + + Tuya.ignore_dim = false; // reset flag + + if (TuyaIdx == 2 && CTLight) { + duty = changeUIntScale(duty, Tuya.CTMin, Tuya.CTMax, Settings.dimmer_hw_max, 0); + } else { + duty = changeUIntScale(duty, 0, 100, 0, Settings.dimmer_hw_max); + } + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim skipped value %d for dpid %d"), duty, dpid); // due to 0 or already set + } else { + AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Cannot set dimmer. Dimmer Id unknown")); // + } + } + + if (TuyaIdx == 3) { + dpid = TuyaGetDpId(TUYA_MCU_FUNC_RGB); + if (!Tuya.ModeSet && (TuyaGetDpId(TUYA_MCU_FUNC_MODESET) != 0) && light_type > LT_RGB) { + TuyaSendEnum(TuyaGetDpId(TUYA_MCU_FUNC_MODESET), 1); + } + TuyaSendString(dpid, hex_char); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: TX RGB hex %s to dpId %d"), hex_char, dpid); } } -void TuyaRequestState(void) +void TuyaRequestState(uint8_t state_type) { if (TuyaSerial) { // Get current status of MCU AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Read MCU state")); - - TuyaSendCmd(TUYA_CMD_QUERY_STATE); + Tuya.SuspendTopic = true; + Tuya.ignore_topic_timeout = millis() + 1000; // suppress /STAT topic for 1000ms to limit data + switch (state_type) { + case 0: + TuyaSendCmd(TUYA_CMD_QUERY_STATE); + break; + case 8: + TuyaSendCmd(TUYA_CMD_QUERY_PRODUCT); + break; + } } } @@ -452,25 +585,64 @@ void TuyaProcessStatePacket(void) { SwitchHandler(1); } } - } else if (Tuya.buffer[dpidStart + 1] == 2) { // Data Type 2 bool tuya_energy_enabled = (XNRG_32 == energy_flg); uint16_t packetValue = Tuya.buffer[dpidStart + 6] << 8 | Tuya.buffer[dpidStart + 7]; - if (fnId == TUYA_MCU_FUNC_DIMMER) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), packetValue); - Tuya.new_dim = changeUIntScale(packetValue, 0, Settings.dimmer_hw_max, 0, 100); - if (Tuya.ignore_dimmer_cmd_timeout < millis()) { - if ((power || Settings.flag3.tuya_apply_o20) && // SetOption54 - Apply SetOption20 settings to Tuya device - (Tuya.new_dim > 0) && (abs(Tuya.new_dim - Settings.light_dimmer) > 1)) { - Tuya.ignore_dim = true; + uint8_t dimIndex; + if ((fnId == TUYA_MCU_FUNC_FAN3) || (fnId == TUYA_MCU_FUNC_FAN4) || + (fnId == TUYA_MCU_FUNC_FAN5) || (fnId == TUYA_MCU_FUNC_FAN6)) { + Tuya.FanState = packetValue; + } - snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER "3 %d"), Tuya.new_dim ); + if ((fnId == TUYA_MCU_FUNC_DIMMER) || (fnId == TUYA_MCU_FUNC_REPORT1)) { + dimIndex = 0; + } + + if (fnId == TUYA_MCU_FUNC_DIMMER2 || fnId == TUYA_MCU_FUNC_REPORT2 || fnId == TUYA_MCU_FUNC_CT) { + dimIndex = 1; + if (Settings.flag3.pwm_multi_channels) { + Tuya.Levels[dimIndex] = changeUIntScale(packetValue, 0, Settings.dimmer_hw_max, 0, 100); + } else { + Tuya.Levels[dimIndex] = changeUIntScale(packetValue, 0, Settings.dimmer_hw_max, Tuya.CTMax, Tuya.CTMin); + } + } + + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX value %d from dpId %d "), packetValue, Tuya.buffer[dpidStart]); + + if (Tuya.ignore_dimmer_cmd_timeout < millis()) { + + if ((power || Settings.flag3.tuya_apply_o20) && ((Tuya.Levels[dimIndex] > 0) && (Tuya.Levels[dimIndex] != Tuya.Snapshot[dimIndex]))) { // SetOption54 - Apply SetOption20 settings to Tuya device + + Tuya.ignore_dim = true; + skip_light_fade = true; + + if ((fnId == TUYA_MCU_FUNC_DIMMER) || (fnId == TUYA_MCU_FUNC_REPORT1)) { + if (Settings.flag3.pwm_multi_channels && (abs(Tuya.Levels[0] - changeUIntScale(Light.current_color[0], 0, 255, 0, 100))) > 1) { + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_CHANNEL "1 %d"), Tuya.Levels[0]); + } else { + if ((abs(Tuya.Levels[0] - light_state.getDimmer())) > 1) { snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER "3 %d"), Tuya.Levels[0]); } + } + ExecuteCommand(scmnd, SRC_SWITCH); + } + + if (((fnId == TUYA_MCU_FUNC_DIMMER2) || (fnId == TUYA_MCU_FUNC_REPORT2)) && + Settings.flag3.pwm_multi_channels && (abs(Tuya.Levels[1] - changeUIntScale(Light.current_color[1], 0, 255, 0, 100))) > 1) { + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_CHANNEL "2 %d"), Tuya.Levels[1]); + ExecuteCommand(scmnd, SRC_SWITCH); + } + + if ((fnId == TUYA_MCU_FUNC_CT) && (abs(Tuya.Levels[1] - light_state.getCT())) > 1) { + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_COLORTEMPERATURE " %d"), Tuya.Levels[1]); + ExecuteCommand(scmnd, SRC_SWITCH); + } + + if ((fnId == TUYA_MCU_FUNC_WHITE) && (abs(Tuya.Levels[1] - light_state.getDimmer(2))) > 1) { + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_WHITE " %d"), Tuya.Levels[1]); ExecuteCommand(scmnd, SRC_SWITCH); } } } - #ifdef USE_ENERGY_SENSOR else if (tuya_energy_enabled && fnId == TUYA_MCU_FUNC_VOLTAGE) { Energy.voltage[0] = (float)packetValue / 10; @@ -489,14 +661,40 @@ void TuyaProcessStatePacket(void) { Tuya.lastPowerCheckTime = Rtc.utc_time; } #endif // USE_ENERGY_SENSOR - } - // } else { - // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Unknown FnId=%s for dpId=%s"), fnId, Tuya.buffer[6]); - dpidStart += dpDataLen + 4; + else if (Tuya.buffer[dpidStart + 1] == 3) { // Data Type 3 + const unsigned char *dpData = (unsigned char*)&Tuya.buffer[dpidStart + 4]; + if ((TuyaGetDpId(TUYA_MCU_FUNC_RGB) != 0) && dpDataLen == 12) { //Decode the RGB hex and transmit HSBCOLOR command + + //Tuya.ignore_dim = true; + char RgbData[13]; + snprintf_P(RgbData, sizeof(RgbData), PSTR("%.*s"), dpDataLen, dpData); + char HSB1[5], HSB2[5], HSB3[5]; + snprintf_P(HSB1, sizeof(HSB1), PSTR("%.4s\n"), &RgbData[0]); + snprintf_P(HSB2, sizeof(HSB2), PSTR("%.4s\n"), &RgbData[4]); + snprintf_P(HSB3, sizeof(HSB3), PSTR("%.4s\n"), &RgbData[8]); + + if ((Tuya.Snapshot[2] != ((int)strtol(HSB1, NULL, 16)) || + Tuya.Snapshot[3] != ((int)strtol(HSB2, NULL, 16)) / 10 || Tuya.Snapshot[4] !=((int)strtol(HSB3, NULL, 16)) / 10)) { + + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_HSBCOLOR " %d,%d,%d"), ((int)strtol(HSB1, NULL, 16)), + ((int)strtol(HSB2, NULL, 16)) / 10, ((int)strtol(HSB3, NULL, 16)) / 10); + ExecuteCommand(scmnd, SRC_SWITCH); + + memcpy_P(Tuya.HSBColor, RgbData, strlen(RgbData)); + } + } + } + else if (Tuya.buffer[dpidStart + 1] == 4) { // Data Type 4 + const unsigned char *dpData = (unsigned char*)&Tuya.buffer[dpidStart + 4]; + if (fnId == TUYA_MCU_FUNC_MODESET) { // toggle light type + Tuya.ModeSet = dpData[0]; + Tuya.Levels[3] = dpData[0]; + } + } + dpidStart += dpDataLen + 4; } } - void TuyaLowPowerModePacketProcess(void) { switch (Tuya.buffer[3]) { case TUYA_CMD_QUERY_PRODUCT: @@ -579,7 +777,7 @@ void TuyaNormalPowerModePacketProcess(void) restart_flag = 2; } } - TuyaRequestState(); + TuyaRequestState(0); break; default: @@ -615,14 +813,33 @@ bool TuyaModuleSelected(void) } } - if (!relaySet) { + if (!relaySet && TuyaGetDpId(TUYA_MCU_FUNC_DUMMY) == 0) { //by default the first relay is created automatically the dummy let remove it if not needed TuyaAddMcuFunc(TUYA_MCU_FUNC_REL1, 1); devices_present++; SettingsSaveAll(); } + // Possible combinations for Lights: + // 0: NONE = LT_BASIC + // 1: DIMMER = LT_SERIAL1 - Common one channel dimmer + // 2: DIMMER, DIMMER2 = LT_SERIAL2 - Two channels dimmer (special setup used with SetOption68) + // 3: DIMMER, CT = LT_SERIAL2 - Dimmable light and White Color Temperature + // 4: DIMMER, RGB = LT_RGB - RGB Light + // 5: DIMMER, RGB, CT = LT_RGBWC - RGB LIght and White Color Temperature + // 6: DIMMER, RGB, WHITE = LT_RGBW - RGB LIght and White + if (TuyaGetDpId(TUYA_MCU_FUNC_DIMMER) != 0) { - light_type = LT_SERIAL1; + if (TuyaGetDpId(TUYA_MCU_FUNC_RGB) != 0) { + if (TuyaGetDpId(TUYA_MCU_FUNC_CT) != 0) { + light_type = LT_RGBWC; + } else if (TuyaGetDpId(TUYA_MCU_FUNC_WHITE) != 0) { + light_type = LT_RGBW; + } else { light_type = LT_RGB; } + } else if (TuyaGetDpId(TUYA_MCU_FUNC_CT) != 0 || TuyaGetDpId(TUYA_MCU_FUNC_DIMMER2) != 0) { + if (TuyaGetDpId(TUYA_MCU_FUNC_RGB) != 0) { + light_type = LT_RGBWC; + } else { light_type = LT_SERIAL2; } + } else { light_type = LT_SERIAL1; } } else { light_type = LT_BASIC; } @@ -647,9 +864,11 @@ void TuyaInit(void) if (TuyaSerial->begin(baudrate)) { if (TuyaSerial->hardwareSerial()) { ClaimSerial(); } // Get MCU Configuration - AddLog_P(LOG_LEVEL_DEBUG, PSTR("TYA: Request MCU configuration at %d baud rate")); - + Tuya.SuspendTopic = true; + Tuya.ignore_topic_timeout = millis() + 1000; // suppress /STAT topic for 1000ms to avoid data overflow + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Request MCU configuration at %d bps"), baudrate); TuyaSendCmd(TUYA_CMD_QUERY_PRODUCT); + } } Tuya.heartbeat_timer = 0; // init heartbeat timer when dimmer init is done @@ -687,17 +906,24 @@ void TuyaSerialInput(void) char hex_char[(Tuya.byte_counter * 2) + 2]; uint16_t len = Tuya.buffer[4] << 8 | Tuya.buffer[5]; + Response_P(PSTR("{\"" D_JSON_TUYA_MCU_RECEIVED "\":{\"Data\":\"%s\",\"Cmnd\":%d"), ToHex_P((unsigned char*)Tuya.buffer, Tuya.byte_counter, hex_char, sizeof(hex_char)), Tuya.buffer[3]); + uint16_t DataVal = 0; + uint8_t dpId = 0; + uint8_t dpDataType = 0; + char DataStr[13]; + if (len > 0) { ResponseAppend_P(PSTR(",\"CmndData\":\"%s\""), ToHex_P((unsigned char*)&Tuya.buffer[6], len, hex_char, sizeof(hex_char))); if (TUYA_CMD_STATE == Tuya.buffer[3]) { //55 AA 03 07 00 0D 01 04 00 01 02 02 02 00 04 00 00 00 1A 40 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 uint8_t dpidStart = 6; + //snprintf_P(DataStr, sizeof(DataStr), PSTR("000000000000")); while (dpidStart + 4 < Tuya.byte_counter) { - uint8_t dpId = Tuya.buffer[dpidStart]; - uint8_t dpDataType = Tuya.buffer[dpidStart + 1]; + dpId = Tuya.buffer[dpidStart]; + dpDataType = Tuya.buffer[dpidStart + 1]; uint16_t dpDataLen = Tuya.buffer[dpidStart + 2] << 8 | Tuya.buffer[dpidStart + 3]; const unsigned char *dpData = (unsigned char*)&Tuya.buffer[dpidStart + 4]; const char *dpHexData = ToHex_P(dpData, dpDataLen, hex_char, sizeof(hex_char)); @@ -706,15 +932,20 @@ void TuyaSerialInput(void) ResponseAppend_P(PSTR(",\"DpType%uId%u\":"), dpDataType, dpId); if (TUYA_TYPE_BOOL == dpDataType && dpDataLen == 1) { ResponseAppend_P(PSTR("%u"), dpData[0]); + DataVal = dpData[0]; } else if (TUYA_TYPE_VALUE == dpDataType && dpDataLen == 4) { uint32_t dpValue = (uint32_t)dpData[0] << 24 | (uint32_t)dpData[1] << 16 | (uint32_t)dpData[2] << 8 | (uint32_t)dpData[3] << 0; ResponseAppend_P(PSTR("%u"), dpValue); + DataVal = dpValue; } else if (TUYA_TYPE_STRING == dpDataType) { ResponseAppend_P(PSTR("\"%.*s\""), dpDataLen, dpData); + snprintf_P(DataStr, sizeof(DataStr), PSTR("%.*s"), dpDataLen, dpData); } else if (TUYA_TYPE_ENUM == dpDataType && dpDataLen == 1) { ResponseAppend_P(PSTR("%u"), dpData[0]); + DataVal = dpData[0]; } else { ResponseAppend_P(PSTR("\"0x%s\""), dpHexData); + snprintf_P(DataStr, sizeof(DataStr), PSTR("%s"), dpHexData); } } @@ -727,7 +958,6 @@ void TuyaSerialInput(void) } } } - ResponseAppend_P(PSTR("}}")); if (Settings.flag3.tuya_serial_mqtt_publish) { // SetOption66 - Enable TuyaMcuReceived messages over Mqtt @@ -737,6 +967,17 @@ void TuyaSerialInput(void) } XdrvRulesProcess(); + if (dpId != 0 && Settings.tuyamcu_topic) { // Publish a /STAT Topic ready to use for any home automation system + if (!Tuya.SuspendTopic) { + char scommand[10]; + snprintf_P(scommand, sizeof(scommand), PSTR("TuyaSend%d"), dpDataType); + + if (dpDataType != 3 && dpDataType != 5) { Response_P(PSTR("%d,%u"), dpId, DataVal); } + else { Response_P(PSTR("%d,%s"), dpId, DataStr); } + MqttPublishPrefixTopic_P(STAT, (PSTR("%s"), scommand)); + } + } + if (!Tuya.low_power_mode) { TuyaNormalPowerModePacketProcess(); } else { @@ -747,7 +988,7 @@ void TuyaSerialInput(void) Tuya.cmd_status = 0; Tuya.cmd_checksum = 0; Tuya.data_len = 0; - } // read additional packets from TUYA + } // read additional packets from TUYA else if (Tuya.byte_counter < TUYA_BUFFER_SIZE -1) { // add char to string if it still fits Tuya.buffer[Tuya.byte_counter++] = serial_in_byte; Tuya.cmd_checksum += serial_in_byte; @@ -821,6 +1062,7 @@ void TuyaSetTime(void) { #endif //USE_TUYA_TIME #ifdef USE_ENERGY_SENSOR + /*********************************************************************************************\ * Energy Interface \*********************************************************************************************/ @@ -872,6 +1114,7 @@ bool Xdrv16(uint8_t function) result = TuyaButtonPressed(); break; case FUNC_EVERY_SECOND: + TuyaSetChannels(); if (TuyaSerial && Tuya.wifi_state != TuyaGetTuyaWifiState()) { TuyaSetWifiLed(); } if (!Tuya.low_power_mode) { Tuya.heartbeat_timer++; @@ -887,10 +1130,11 @@ bool Xdrv16(uint8_t function) } else { TuyaSendLowPowerSuccessIfNeeded(); } + if (Tuya.ignore_topic_timeout < millis()) { Tuya.SuspendTopic = false; } break; - case FUNC_SET_CHANNELS: - result = TuyaSetChannels(); - break; + // case FUNC_SET_CHANNELS: + // result = TuyaSetChannels(); + // break; case FUNC_COMMAND: result = DecodeCommand(kTuyaCommand, TuyaCommand); break; diff --git a/tasmota/xdrv_17_rcswitch.ino b/tasmota/xdrv_17_rcswitch.ino index ac4111e83..af3dc1bba 100644 --- a/tasmota/xdrv_17_rcswitch.ino +++ b/tasmota/xdrv_17_rcswitch.ino @@ -104,18 +104,16 @@ void CmndRfSend(void) int repeat = 10; int pulse = 350; - char dataBufUc[XdrvMailbox.data_len + 1]; - UpperCase(dataBufUc, XdrvMailbox.data); - StaticJsonBuffer<150> jsonBuf; // ArduinoJSON entry used to calculate jsonBuf: JSON_OBJECT_SIZE(5) + 40 = 134 - JsonObject &root = jsonBuf.parseObject(dataBufUc); - if (root.success()) { + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (root) { // RFsend {"data":0x501014,"bits":24,"protocol":1,"repeat":10,"pulse":350} char parm_uc[10]; - data = strtoul(root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_DATA))], nullptr, 0); // Allow decimal (5246996) and hexadecimal (0x501014) input - bits = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_BITS))]; - protocol = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PROTOCOL))]; - repeat = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_REPEAT))]; - pulse = root[UpperCase_P(parm_uc, PSTR(D_JSON_RF_PULSE))]; + data = root.getUInt(PSTR(D_JSON_RF_DATA), data); + bits = root.getUInt(PSTR(D_JSON_RF_BITS), bits); + protocol = root.getInt(PSTR(D_JSON_RF_PROTOCOL), protocol); + repeat = root.getInt(PSTR(D_JSON_RF_REPEAT), repeat); + pulse = root.getInt(PSTR(D_JSON_RF_PULSE), pulse); } else { // RFsend data, bits, protocol, repeat, pulse char *p; diff --git a/tasmota/xdrv_20_hue.ino b/tasmota/xdrv_20_hue.ino index 974ebbb78..7c2e29395 100644 --- a/tasmota/xdrv_20_hue.ino +++ b/tasmota/xdrv_20_hue.ino @@ -569,10 +569,12 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { if (Webserver->args()) { response = "["; - StaticJsonBuffer<300> jsonBuffer; - JsonObject &hue_json = jsonBuffer.parseObject(Webserver->arg((Webserver->args())-1)); - if (hue_json.containsKey("on")) { - on = hue_json["on"]; + JsonParser parser((char*) Webserver->arg((Webserver->args())-1).c_str()); + JsonParserObject root = parser.getRootObject(); + + JsonParserToken hue_on = root[PSTR("on")]; + if (hue_on) { + on = hue_on.getBool(); snprintf_P(buf, buf_size, PSTR("{\"success\":{\"/lights/%d/state/on\":%s}}"), device_id, on ? "true" : "false"); @@ -587,15 +589,6 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } } else { #endif -/* - switch(on) - { - case false : ExecuteCommandPower(device, POWER_OFF, SRC_HUE); - break; - case true : ExecuteCommandPower(device, POWER_ON, SRC_HUE); - break; - } -*/ ExecuteCommandPower(device, (on) ? POWER_ON : POWER_OFF, SRC_HUE); response += buf; resp = true; @@ -619,8 +612,10 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } prev_x_str[0] = prev_y_str[0] = 0; // reset xy string - if (hue_json.containsKey("bri")) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. - bri = hue_json["bri"]; + parser.setCurrent(); + JsonParserToken hue_bri = root[PSTR("bri")]; + if (hue_bri) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. + bri = hue_bri.getUInt(); prev_bri = bri; // store command value if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -634,15 +629,19 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } resp = true; } + // handle xy before Hue/Sat // If the request contains both XY and HS, we wan't to give priority to HS - if (hue_json.containsKey("xy")) { - float x = hue_json["xy"][0]; - float y = hue_json["xy"][1]; - const String &x_str = hue_json["xy"][0]; - const String &y_str = hue_json["xy"][1]; - x_str.toCharArray(prev_x_str, sizeof(prev_x_str)); - y_str.toCharArray(prev_y_str, sizeof(prev_y_str)); + parser.setCurrent(); + JsonParserToken hue_xy = root[PSTR("xy")]; + if (hue_xy) { + JsonParserArray arr_xy = JsonParserArray(hue_xy); + JsonParserToken tok_x = arr_xy[0]; + JsonParserToken tok_y = arr_xy[1]; + float x = tok_x.getFloat(); + float y = tok_y.getFloat(); + strlcpy(prev_x_str, tok_x.getStr(), sizeof(prev_x_str)); + strlcpy(prev_y_str, tok_y.getStr(), sizeof(prev_y_str)); uint8_t rr,gg,bb; LightStateClass::XyToRgb(x, y, &rr, &gg, &bb); LightStateClass::RgbToHsb(rr, gg, bb, &hue, &sat, nullptr); @@ -658,8 +657,11 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { resp = true; change = true; } - if (hue_json.containsKey("hue")) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. - hue = hue_json["hue"]; + + parser.setCurrent(); + JsonParserToken hue_hue = root[PSTR("hue")]; + if (hue_hue) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. + hue = hue_hue.getUInt(); prev_hue = hue; if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -674,8 +676,11 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } resp = true; } - if (hue_json.containsKey("sat")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). - sat = hue_json["sat"]; + + parser.setCurrent(); + JsonParserToken hue_sat = root[PSTR("sat")]; + if (hue_sat) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). + sat = hue_sat.getUInt(); prev_sat = sat; // store command value if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -690,8 +695,11 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } resp = true; } - if (hue_json.containsKey("ct")) { // Color temperature 153 (Cold) to 500 (Warm) - ct = hue_json["ct"]; + + parser.setCurrent(); + JsonParserToken hue_ct = root[PSTR("ct")]; + if (hue_ct) { // Color temperature 153 (Cold) to 500 (Warm) + ct = hue_ct.getUInt(); prev_ct = ct; // store commande value if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -704,6 +712,7 @@ void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) { } resp = true; } + if (change) { #ifdef USE_SHUTTER if (ShutterState(device)) { diff --git a/tasmota/xdrv_23_zigbee_1_headers.ino b/tasmota/xdrv_23_zigbee_1_headers.ino index bb7555757..3ccb54672 100644 --- a/tasmota/xdrv_23_zigbee_1_headers.ino +++ b/tasmota/xdrv_23_zigbee_1_headers.ino @@ -37,42 +37,7 @@ public: }; void ZigbeeZCLSend_Raw(const ZigbeeZCLSendMessage &zcl); - -// get the result as a string (const char*) and nullptr if there is no field or the string is empty -const char * getCaseInsensitiveConstCharNull(const JsonObject &json, const char *needle) { - const JsonVariant &val = GetCaseInsensitive(json, needle); - if (&val) { - const char *val_cs = val.as(); - if (strlen(val_cs)) { - return val_cs; - } - } - return nullptr; -} - -// Get an JSON attribute, with case insensitive key search starting with *needle -JsonVariant &startsWithCaseInsensitive(const JsonObject &json, const char *needle) { - // key can be in PROGMEM - if ((nullptr == &json) || (nullptr == needle) || (0 == pgm_read_byte(needle))) { - return *(JsonVariant*)nullptr; - } - - String needle_s(needle); - needle_s.toLowerCase(); - - for (auto kv : json) { - String key_s(kv.key); - key_s.toLowerCase(); - JsonVariant &value = kv.value; - - if (key_s.startsWith(needle_s)) { - return value; - } - } - // if not found - return *(JsonVariant*)nullptr; -} - +bool ZbAppendWriteBuf(SBuffer & buf, const Z_attribute & attr, bool prepend_status_ok = false); uint32_t parseHex(const char **data, size_t max_len = 8) { uint32_t ret = 0; diff --git a/tasmota/xdrv_23_zigbee_1z_libs.ino b/tasmota/xdrv_23_zigbee_1z_libs.ino index 0590cff81..8b64baa5b 100644 --- a/tasmota/xdrv_23_zigbee_1z_libs.ino +++ b/tasmota/xdrv_23_zigbee_1z_libs.ino @@ -64,6 +64,46 @@ uint16_t Z_GetLastGroup(void) { return gZbLastMessage.groupaddr; } uint16_t Z_GetLastCluster(void) { return gZbLastMessage.cluster; } uint8_t Z_GetLastEndpoint(void) { return gZbLastMessage.endpoint; } +/*********************************************************************************************\ + * + * Class for attribute array of values + * This is a helper function to generate a clean list of unsigned ints + * +\*********************************************************************************************/ + +class Z_json_array { +public: + + Z_json_array(): val("[]") {} // start with empty array + void add(uint32_t uval32) { + // remove trailing ']' + val.remove(val.length()-1); + if (val.length() > 1) { // if not empty, prefix with comma + val += ','; + } + val += uval32; + val += ']'; + } + void addStrRaw(const char * sval) { + // remove trailing ']' + val.remove(val.length()-1); + if (val.length() > 1) { // if not empty, prefix with comma + val += ','; + } + val += sval; + val += ']'; + } + void addStr(const char * sval) { + addStrRaw(EscapeJSONString(sval).c_str()); + } + String &toString(void) { + return val; + } + +private : + String val; +}; + /*********************************************************************************************\ * * Class for single attribute @@ -79,7 +119,10 @@ enum class Za_type : uint8_t { Za_float, // float 32, uses fval // non-nummericals Za_raw, // bytes buffer, uses bval - Za_str // string, uses sval + Za_str, // string, uses sval + // sub_objects + Za_obj, // json sub-object + Za_arr, // array sub-object (string add-only) }; class Z_attribute { @@ -90,22 +133,26 @@ public: struct { uint16_t cluster; uint16_t attr_id; - } id; - char * key; + } id; + char * key; } key; // attribute value union { - uint32_t uval32; - int32_t ival32; - float fval; - SBuffer* bval; - char* sval; + uint32_t uval32; + int32_t ival32; + float fval; + SBuffer* bval; + char* sval; + class Z_attribute_list * objval; + class Z_json_array * arrval; } val; - Za_type type; // uint8_t in size, type of attribute, see above - bool key_is_str; // is the key a string? - bool key_is_pmem; // is the string in progmem, so we don't need to make a copy - bool val_str_raw; // if val is String, it is raw JSON and should not be escaped - uint8_t key_suffix; // append a suffix to key (default is 1, explicitly output if >1) + Za_type type; // uint8_t in size, type of attribute, see above + bool key_is_str; // is the key a string? + bool key_is_pmem; // is the string in progmem, so we don't need to make a copy + bool val_str_raw; // if val is String, it is raw JSON and should not be escaped + uint8_t key_suffix; // append a suffix to key (default is 1, explicitly output if >1) + uint8_t attr_type; // [opt] type of the attribute, default to Zunk (0xFF) + uint8_t attr_multiplier; // [opt] multiplier for attribute, defaults to 0x01 (no change) // Constructor with all defaults Z_attribute(): @@ -115,7 +162,9 @@ public: key_is_str(false), key_is_pmem(false), val_str_raw(false), - key_suffix(1) + key_suffix(1), + attr_type(0xFF), + attr_multiplier(1) {}; Z_attribute(const Z_attribute & rhs) { @@ -135,386 +184,67 @@ public: } // free any allocated memoruy for values - void freeVal(void) { - switch (type) { - case Za_type::Za_raw: - if (val.bval) { delete val.bval; val.bval = nullptr; } - break; - case Za_type::Za_str: - if (val.sval) { delete[] val.sval; val.sval = nullptr; } - break; - } - } + void freeVal(void); + // free any allocated memoruy for keys - void freeKey(void) { - if (key_is_str && key.key && !key_is_pmem) { delete[] key.key; } - key.key = nullptr; - } + void freeKey(void); // set key name - void setKeyName(const char * _key, bool pmem = false) { - freeKey(); - key_is_str = true; - key_is_pmem = pmem; - if (pmem) { - key.key = (char*) _key; - } else { - setKeyName(_key, nullptr); - } - } + void setKeyName(const char * _key, bool pmem = false); // provide two entries and concat - void setKeyName(const char * _key, const char * _key2) { - freeKey(); - key_is_str = true; - key_is_pmem = false; - if (_key) { - size_t key_len = strlen_P(_key); - if (_key2) { - key_len += strlen_P(_key2); - } - key.key = new char[key_len+1]; - strcpy_P(key.key, _key); - if (_key2) { - strcat_P(key.key, _key2); - } - } - } + void setKeyName(const char * _key, const char * _key2); - void setKeyId(uint16_t cluster, uint16_t attr_id) { - freeKey(); - key_is_str = false; - key.id.cluster = cluster; - key.id.attr_id = attr_id; - } + void setKeyId(uint16_t cluster, uint16_t attr_id); // Setters - void setNone(void) { - freeVal(); // free any previously allocated memory - val.uval32 = 0; - type = Za_type::Za_none; - } - void setUInt(uint32_t _val) { - freeVal(); // free any previously allocated memory - val.uval32 = _val; - type = Za_type::Za_uint; - } - void setBool(bool _val) { - freeVal(); // free any previously allocated memory - val.uval32 = _val; - type = Za_type::Za_bool; - } - void setInt(int32_t _val) { - freeVal(); // free any previously allocated memory - val.ival32 = _val; - type = Za_type::Za_int; - } - void setFloat(float _val) { - freeVal(); // free any previously allocated memory - val.fval = _val; - type = Za_type::Za_float; - } + void setNone(void); + void setUInt(uint32_t _val); + void setBool(bool _val); + void setInt(int32_t _val); + void setFloat(float _val); - void setBuf(const SBuffer &buf, size_t index, size_t len) { - freeVal(); - if (len) { - val.bval = new SBuffer(len); - val.bval->addBuffer(buf.buf(index), len); - } - type = Za_type::Za_raw; - } + void setBuf(const SBuffer &buf, size_t index, size_t len); // set the string value // PMEM argument is allowed // string will be copied, so it can be changed later // nullptr is allowed and considered as empty string // Note: memory is allocated only if string is non-empty - void setStr(const char * _val) { - freeVal(); // free any previously allocated memory - val_str_raw = false; - // val.sval is always nullptr after freeVal() - if (_val) { - size_t len = strlen_P(_val); - if (len) { - val.sval = new char[len+1]; - strcpy_P(val.sval, _val); - } - } - type = Za_type::Za_str; - } + void setStr(const char * _val); inline void setStrRaw(const char * _val) { setStr(_val); val_str_raw = true; } + Z_attribute_list & newAttrList(void); + Z_json_array & newJsonArray(void); + inline bool isNum(void) const { return (type >= Za_type::Za_bool) && (type <= Za_type::Za_float); } + inline bool isNone(void) const { return (type == Za_type::Za_none);} // get num values - float getFloat(void) const { - switch (type) { - case Za_type::Za_bool: - case Za_type::Za_uint: return (float) val.uval32; - case Za_type::Za_int: return (float) val.ival32; - case Za_type::Za_float: return val.fval; - default: return 0.0f; - } - } - - int32_t getInt(void) const { - switch (type) { - case Za_type::Za_bool: - case Za_type::Za_uint: return (int32_t) val.uval32; - case Za_type::Za_int: return val.ival32; - case Za_type::Za_float: return (int32_t) val.fval; - default: return 0; - } - } - - uint32_t getUInt(void) const { - switch (type) { - case Za_type::Za_bool: - case Za_type::Za_uint: return val.uval32; - case Za_type::Za_int: return (uint32_t) val.ival32; - case Za_type::Za_float: return (uint32_t) val.fval; - default: return 0; - } - } - - bool getBool(void) const { - switch (type) { - case Za_type::Za_bool: - case Za_type::Za_uint: return val.uval32 ? true : false; - case Za_type::Za_int: return val.ival32 ? true : false; - case Za_type::Za_float: return val.fval ? true : false; - default: return false; - } - } - - const SBuffer * getRaw(void) const { - if (Za_type::Za_raw == type) { return val.bval; } - return nullptr; - } + float getFloat(void) const; + int32_t getInt(void) const; + uint32_t getUInt(void) const; + bool getBool(void) const; + const SBuffer * getRaw(void) const; // always return a point to a string, if not defined then empty string. // Never returns nullptr - const char * getStr(void) const { - if (Za_type::Za_str == type) { return val.sval; } - return ""; - } + const char * getStr(void) const; - bool equalsKey(const Z_attribute & attr2, bool ignore_key_suffix = false) const { - // check if keys are equal - if (key_is_str != attr2.key_is_str) { return false; } - if (key_is_str) { - if (strcmp_PP(key.key, attr2.key.key)) { return false; } - } else { - if ((key.id.cluster != attr2.key.id.cluster) || - (key.id.attr_id != attr2.key.id.attr_id)) { return false; } - } - if (!ignore_key_suffix) { - if (key_suffix != attr2.key_suffix) { return false; } - } - return true; - } + bool equalsKey(const Z_attribute & attr2, bool ignore_key_suffix = false) const; + bool equalsKey(uint16_t cluster, uint16_t attr_id, uint8_t suffix = 0) const; + bool equalsKey(const char * name, uint8_t suffix = 0) const; + bool equalsVal(const Z_attribute & attr2) const; + bool equals(const Z_attribute & attr2) const; - bool equalsKey(uint16_t cluster, uint16_t attr_id, uint8_t suffix = 0) const { - if (!key_is_str) { - if ((key.id.cluster == cluster) && (key.id.attr_id == attr_id)) { - if (suffix) { - if (key_suffix == suffix) { return true; } - } else { - return true; - } - } - } - return false; - } - - bool equalsKey(const char * name, uint8_t suffix = 0) const { - if (key_is_str) { - if (0 == strcmp_PP(key.key, name)) { - if (suffix) { - if (key_suffix == suffix) { return true; } - } else { - return true; - } - } - } - return false; - } - - bool equalsVal(const Z_attribute & attr2) const { - if (type != attr2.type) { return false; } - if ((type >= Za_type::Za_bool) && (type <= Za_type::Za_float)) { - // numerical value - if (val.uval32 != attr2.val.uval32) { return false; } - } else if (type == Za_type::Za_raw) { - // compare 2 Static buffers - return equalsSBuffer(val.bval, attr2.val.bval); - } else if (type == Za_type::Za_str) { - // if (val_str_raw != attr2.val_str_raw) { return false; } - if (strcmp_PP(val.sval, attr2.val.sval)) { return false; } - } - return true; - } - - bool equals(const Z_attribute & attr2) const { - return equalsKey(attr2) && equalsVal(attr2); - } - - String toString(bool prefix_comma = false) const { - String res(""); - if (prefix_comma) { res += ','; } - res += '"'; - // compute the attribute name - if (key_is_str) { - if (key.key) { res += EscapeJSONString(key.key); } - else { res += F("null"); } // shouldn't happen - if (key_suffix > 1) { - res += key_suffix; - } - } else { - char attr_name[12]; - snprintf_P(attr_name, sizeof(attr_name), PSTR("%04X/%04X"), key.id.cluster, key.id.attr_id); - res += attr_name; - if (key_suffix > 1) { - res += '+'; - res += key_suffix; - } - } - res += F("\":"); - // value part - switch (type) { - case Za_type::Za_none: - res += "null"; - break; - case Za_type::Za_bool: - res += val.uval32 ? F("true") : F("false"); - break; - case Za_type::Za_uint: - res += val.uval32; - break; - case Za_type::Za_int: - res += val.ival32; - break; - case Za_type::Za_float: - { - String fstr(val.fval, 2); - size_t last = fstr.length() - 1; - // remove trailing zeros - while (fstr[last] == '0') { - fstr.remove(last--); - } - // remove trailing dot - if (fstr[last] == '.') { - fstr.remove(last); - } - res += fstr; - } - break; - case Za_type::Za_raw: - res += '"'; - if (val.bval) { - size_t blen = val.bval->len(); - // print as HEX - char hex[2*blen+1]; - ToHex_P(val.bval->getBuffer(), blen, hex, sizeof(hex)); - res += hex; - } - res += '"'; - break; - case Za_type::Za_str: - if (val_str_raw) { - if (val.sval) { res += val.sval; } - } else { - res += '"'; - if (val.sval) { - res += EscapeJSONString(val.sval); // escape JSON chars - } - res += '"'; - } - break; - } - - return res; - } + String toString(bool prefix_comma = false) const; // copy value from one attribute to another, without changing its type - void copyVal(const Z_attribute & rhs) { - freeVal(); - // copy value - val.uval32 = 0x00000000; - type = rhs.type; - if (rhs.isNum()) { - val.uval32 = rhs.val.uval32; - } else if (rhs.type == Za_type::Za_raw) { - if (rhs.val.bval) { - val.bval = new SBuffer(rhs.val.bval->len()); - val.bval->addBuffer(*(rhs.val.bval)); - } - } else if (rhs.type == Za_type::Za_str) { - if (rhs.val.sval) { - size_t s_len = strlen_P(rhs.val.sval); - val.sval = new char[s_len+1]; - strcpy_P(val.sval, rhs.val.sval); - } - } - val_str_raw = rhs.val_str_raw; - } + void copyVal(const Z_attribute & rhs); protected: - void deepCopy(const Z_attribute & rhs) { - // copy key - if (!rhs.key_is_str) { - key.id.cluster = rhs.key.id.cluster; - key.id.attr_id = rhs.key.id.attr_id; - } else { - if (rhs.key_is_pmem) { - key.key = rhs.key.key; // PMEM, don't copy - } else { - key.key = nullptr; - if (rhs.key.key) { - size_t key_len = strlen_P(rhs.key.key); - if (key_len) { - key.key = new char[key_len+1]; - strcpy_P(key.key, rhs.key.key); - } - } - } - } - key_is_str = rhs.key_is_str; - key_is_pmem = rhs.key_is_pmem; - key_suffix = rhs.key_suffix; - // copy value - copyVal(rhs); - // don't touch next pointer - } -}; - -/*********************************************************************************************\ - * - * Class for attribute array of values - * This is a helper function to generate a clean list of unsigned ints - * -\*********************************************************************************************/ - -class Z_json_array { -public: - - Z_json_array(): val("[]") {} // start with empty array - void add(uint32_t uval32) { - // remove trailing ']' - val.remove(val.length()-1); - if (val.length() > 1) { // if not empty, prefix with comma - val += ','; - } - val += uval32; - val += ']'; - } - String &toString(void) { - return val; - } - -private : - String val; + void deepCopy(const Z_attribute & rhs); }; /*********************************************************************************************\ @@ -606,6 +336,406 @@ public: bool mergeList(const Z_attribute_list &list2); }; +/*********************************************************************************************\ + * + * Implementation for Z_attribute + * +\*********************************************************************************************/ + +// free any allocated memoruy for keys +void Z_attribute::freeKey(void) { + if (key_is_str && key.key && !key_is_pmem) { delete[] key.key; } + key.key = nullptr; +} + +// set key name +void Z_attribute::setKeyName(const char * _key, bool pmem) { + freeKey(); + key_is_str = true; + key_is_pmem = pmem; + if (pmem) { + key.key = (char*) _key; + } else { + setKeyName(_key, nullptr); + } +} +// provide two entries and concat +void Z_attribute::setKeyName(const char * _key, const char * _key2) { + freeKey(); + key_is_str = true; + key_is_pmem = false; + if (_key) { + size_t key_len = strlen_P(_key); + if (_key2) { + key_len += strlen_P(_key2); + } + key.key = new char[key_len+1]; + strcpy_P(key.key, _key); + if (_key2) { + strcat_P(key.key, _key2); + } + } +} + +void Z_attribute::setKeyId(uint16_t cluster, uint16_t attr_id) { + freeKey(); + key_is_str = false; + key.id.cluster = cluster; + key.id.attr_id = attr_id; +} + +// Setters +void Z_attribute::setNone(void) { + freeVal(); // free any previously allocated memory + val.uval32 = 0; + type = Za_type::Za_none; +} +void Z_attribute::setUInt(uint32_t _val) { + freeVal(); // free any previously allocated memory + val.uval32 = _val; + type = Za_type::Za_uint; +} +void Z_attribute::setBool(bool _val) { + freeVal(); // free any previously allocated memory + val.uval32 = _val; + type = Za_type::Za_bool; +} +void Z_attribute::setInt(int32_t _val) { + freeVal(); // free any previously allocated memory + val.ival32 = _val; + type = Za_type::Za_int; +} +void Z_attribute::setFloat(float _val) { + freeVal(); // free any previously allocated memory + val.fval = _val; + type = Za_type::Za_float; +} + +void Z_attribute::setBuf(const SBuffer &buf, size_t index, size_t len) { + freeVal(); + if (len) { + val.bval = new SBuffer(len); + val.bval->addBuffer(buf.buf(index), len); + } + type = Za_type::Za_raw; +} + +// set the string value +// PMEM argument is allowed +// string will be copied, so it can be changed later +// nullptr is allowed and considered as empty string +// Note: memory is allocated only if string is non-empty +void Z_attribute::setStr(const char * _val) { + freeVal(); // free any previously allocated memory + val_str_raw = false; + // val.sval is always nullptr after freeVal() + if (_val) { + size_t len = strlen_P(_val); + if (len) { + val.sval = new char[len+1]; + strcpy_P(val.sval, _val); + } + } + type = Za_type::Za_str; +} + +Z_attribute_list & Z_attribute::newAttrList(void) { + freeVal(); + val.objval = new Z_attribute_list(); + type = Za_type::Za_obj; + return *val.objval; +} + +Z_json_array & Z_attribute::newJsonArray(void) { + freeVal(); + val.arrval = new Z_json_array(); + type = Za_type::Za_arr; + return *val.arrval; +} + +// get num values +float Z_attribute::getFloat(void) const { + switch (type) { + case Za_type::Za_bool: + case Za_type::Za_uint: return (float) val.uval32; + case Za_type::Za_int: return (float) val.ival32; + case Za_type::Za_float: return val.fval; + default: return 0.0f; + } +} + +int32_t Z_attribute::getInt(void) const { + switch (type) { + case Za_type::Za_bool: + case Za_type::Za_uint: return (int32_t) val.uval32; + case Za_type::Za_int: return val.ival32; + case Za_type::Za_float: return (int32_t) val.fval; + default: return 0; + } +} + +uint32_t Z_attribute::getUInt(void) const { + switch (type) { + case Za_type::Za_bool: + case Za_type::Za_uint: return val.uval32; + case Za_type::Za_int: return (uint32_t) val.ival32; + case Za_type::Za_float: return (uint32_t) val.fval; + default: return 0; + } +} + +bool Z_attribute::getBool(void) const { + switch (type) { + case Za_type::Za_bool: + case Za_type::Za_uint: return val.uval32 ? true : false; + case Za_type::Za_int: return val.ival32 ? true : false; + case Za_type::Za_float: return val.fval ? true : false; + default: return false; + } +} + +const SBuffer * Z_attribute::getRaw(void) const { + if (Za_type::Za_raw == type) { return val.bval; } + return nullptr; +} + +// always return a point to a string, if not defined then empty string. +// Never returns nullptr +const char * Z_attribute::getStr(void) const { + if (Za_type::Za_str == type) { return val.sval; } + return ""; +} + +bool Z_attribute::equalsKey(const Z_attribute & attr2, bool ignore_key_suffix) const { + // check if keys are equal + if (key_is_str != attr2.key_is_str) { return false; } + if (key_is_str) { + if (strcmp_PP(key.key, attr2.key.key)) { return false; } + } else { + if ((key.id.cluster != attr2.key.id.cluster) || + (key.id.attr_id != attr2.key.id.attr_id)) { return false; } + } + if (!ignore_key_suffix) { + if (key_suffix != attr2.key_suffix) { return false; } + } + return true; +} + +bool Z_attribute::equalsKey(uint16_t cluster, uint16_t attr_id, uint8_t suffix) const { + if (!key_is_str) { + if ((key.id.cluster == cluster) && (key.id.attr_id == attr_id)) { + if (suffix) { + if (key_suffix == suffix) { return true; } + } else { + return true; + } + } + } + return false; +} + +bool Z_attribute::equalsKey(const char * name, uint8_t suffix) const { + if (key_is_str) { + if (0 == strcmp_PP(key.key, name)) { + if (suffix) { + if (key_suffix == suffix) { return true; } + } else { + return true; + } + } + } + return false; +} + +bool Z_attribute::equalsVal(const Z_attribute & attr2) const { + if (type != attr2.type) { return false; } + if ((type >= Za_type::Za_bool) && (type <= Za_type::Za_float)) { + // numerical value + if (val.uval32 != attr2.val.uval32) { return false; } + } else if (type == Za_type::Za_raw) { + // compare 2 Static buffers + return equalsSBuffer(val.bval, attr2.val.bval); + } else if (type == Za_type::Za_str) { + // if (val_str_raw != attr2.val_str_raw) { return false; } + if (strcmp_PP(val.sval, attr2.val.sval)) { return false; } + } else if (type == Za_type::Za_obj) { + return false; // TODO for now we'll assume sub-objects are always different + } else if (type == Za_type::Za_arr) { + return false; // TODO for now we'll assume sub-objects are always different + } + return true; +} + +bool Z_attribute::equals(const Z_attribute & attr2) const { + return equalsKey(attr2) && equalsVal(attr2); +} + +String Z_attribute::toString(bool prefix_comma) const { + String res(""); + if (prefix_comma) { res += ','; } + res += '"'; + // compute the attribute name + if (key_is_str) { + if (key.key) { res += EscapeJSONString(key.key); } + else { res += F("null"); } // shouldn't happen + if (key_suffix > 1) { + res += key_suffix; + } + } else { + char attr_name[12]; + snprintf_P(attr_name, sizeof(attr_name), PSTR("%04X/%04X"), key.id.cluster, key.id.attr_id); + res += attr_name; + if (key_suffix > 1) { + res += '+'; + res += key_suffix; + } + } + res += F("\":"); + // value part + switch (type) { + case Za_type::Za_none: + res += "null"; + break; + case Za_type::Za_bool: + res += val.uval32 ? F("true") : F("false"); + break; + case Za_type::Za_uint: + res += val.uval32; + break; + case Za_type::Za_int: + res += val.ival32; + break; + case Za_type::Za_float: + { + String fstr(val.fval, 2); + size_t last = fstr.length() - 1; + // remove trailing zeros + while (fstr[last] == '0') { + fstr.remove(last--); + } + // remove trailing dot + if (fstr[last] == '.') { + fstr.remove(last); + } + res += fstr; + } + break; + case Za_type::Za_raw: + res += '"'; + if (val.bval) { + size_t blen = val.bval->len(); + // print as HEX + char hex[2*blen+1]; + ToHex_P(val.bval->getBuffer(), blen, hex, sizeof(hex)); + res += hex; + } + res += '"'; + break; + case Za_type::Za_str: + if (val_str_raw) { + if (val.sval) { res += val.sval; } + } else { + res += '"'; + if (val.sval) { + res += EscapeJSONString(val.sval); // escape JSON chars + } + res += '"'; + } + break; + case Za_type::Za_obj: + res += '{'; + if (val.objval) { + res += val.objval->toString(); + } + res += '}'; + break; + case Za_type::Za_arr: + if (val.arrval) { + res += val.arrval->toString(); + } else { + res += "[]"; + } + break; + } + + return res; +} + +// copy value from one attribute to another, without changing its type +void Z_attribute::copyVal(const Z_attribute & rhs) { + freeVal(); + // copy value + val.uval32 = 0x00000000; + type = rhs.type; + if (rhs.isNum()) { + val.uval32 = rhs.val.uval32; + } else if (rhs.type == Za_type::Za_raw) { + if (rhs.val.bval) { + val.bval = new SBuffer(rhs.val.bval->len()); + val.bval->addBuffer(*(rhs.val.bval)); + } + } else if (rhs.type == Za_type::Za_str) { + if (rhs.val.sval) { + size_t s_len = strlen_P(rhs.val.sval); + val.sval = new char[s_len+1]; + strcpy_P(val.sval, rhs.val.sval); + } + } + val_str_raw = rhs.val_str_raw; +} + +// free any allocated memoruy for values +void Z_attribute::freeVal(void) { + switch (type) { + case Za_type::Za_raw: + if (val.bval) { delete val.bval; val.bval = nullptr; } + break; + case Za_type::Za_str: + if (val.sval) { delete[] val.sval; val.sval = nullptr; } + break; + case Za_type::Za_obj: + if (val.objval) { delete val.objval; val.objval = nullptr; } + break; + case Za_type::Za_arr: + if (val.arrval) { delete val.arrval; val.arrval = nullptr; } + break; + } +} + +void Z_attribute::deepCopy(const Z_attribute & rhs) { + // copy key + if (!rhs.key_is_str) { + key.id.cluster = rhs.key.id.cluster; + key.id.attr_id = rhs.key.id.attr_id; + } else { + if (rhs.key_is_pmem) { + key.key = rhs.key.key; // PMEM, don't copy + } else { + key.key = nullptr; + if (rhs.key.key) { + size_t key_len = strlen_P(rhs.key.key); + if (key_len) { + key.key = new char[key_len+1]; + strcpy_P(key.key, rhs.key.key); + } + } + } + } + key_is_str = rhs.key_is_str; + key_is_pmem = rhs.key_is_pmem; + key_suffix = rhs.key_suffix; + attr_type = rhs.attr_type; + attr_multiplier = rhs.attr_multiplier; + // copy value + copyVal(rhs); + // don't touch next pointer +} + +/*********************************************************************************************\ + * + * Implementation for Z_attribute_list + * +\*********************************************************************************************/ // add a cluster/attr_id attribute at the end of the list Z_attribute & Z_attribute_list::addAttribute(uint16_t cluster, uint16_t attr_id, uint8_t suffix) { Z_attribute & attr = addToLast(); diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index 8ad36f861..506962f3a 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -36,6 +36,10 @@ public: char * manufacturerId; char * modelId; char * friendlyName; + // _defer_last_time : what was the last time an outgoing message is scheduled + // this is designed for flow control and avoid messages to be lost or unanswered + uint32_t defer_last_message_sent; + uint8_t endpoints[endpoints_max]; // static array to limit memory consumption, list of endpoints until 0x00 or end of array // Used for attribute reporting Z_attribute_list attr_list; @@ -68,9 +72,13 @@ public: int16_t temperature; // temperature in 1/10th of Celsius, 0x8000 if unknown uint16_t pressure; // air pressure in hPa, 0xFFFF if unknown uint8_t humidity; // humidity in percent, 0..100, 0xFF if unknown - // powe plug data + // power plug data uint16_t mains_voltage; // AC voltage int16_t mains_power; // Active power + uint32_t last_seen; // Last seen time (epoch) + // thermostat + int16_t temperature_target; // settings for the temparature + uint8_t th_setpoint; // percentage of heat/cool in percent // Constructor with all defaults Z_Device(uint16_t _shortaddr = BAD_SHORTADDR, uint64_t _longaddr = 0x00): @@ -78,6 +86,7 @@ public: manufacturerId(nullptr), modelId(nullptr), friendlyName(nullptr), + defer_last_message_sent(0), endpoints{ 0, 0, 0, 0, 0, 0, 0, 0 }, attr_list(), shortaddr(_shortaddr), @@ -98,7 +107,10 @@ public: pressure(0xFFFF), humidity(0xFF), mains_voltage(0xFFFF), - mains_power(-0x8000) + mains_power(-0x8000), + last_seen(0), + temperature_target(-0x8000), + th_setpoint(0xFF) { }; inline bool valid(void) const { return BAD_SHORTADDR != shortaddr; } // is the device known, valid and found? @@ -123,6 +135,10 @@ public: inline bool validTemperature(void) const { return -0x8000 != temperature; } inline bool validPressure(void) const { return 0xFFFF != pressure; } inline bool validHumidity(void) const { return 0xFF != humidity; } + inline bool validLastSeen(void) const { return 0x0 != last_seen; } + + inline bool validTemperatureTarget(void) const { return -0x8000 != temperature_target; } + inline bool validThSetpoint(void) const { return 0xFF != th_setpoint; } inline bool validMainsVoltage(void) const { return 0xFFFF != mains_voltage; } inline bool validMainsPower(void) const { return -0x8000 != mains_power; } @@ -145,21 +161,29 @@ public: * Structures for deferred callbacks \*********************************************************************************************/ -typedef int32_t (*Z_DeviceTimer)(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value); +typedef void (*Z_DeviceTimer)(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value); // Category for Deferred actions, this allows to selectively remove active deferred or update them typedef enum Z_Def_Category { - Z_CAT_NONE = 0, // no category, it will happen anyways + Z_CAT_ALWAYS = 0, // no category, it will happen whatever new timers + // Below will clear any event in the same category for the same address (shortaddr / groupaddr) + Z_CLEAR_DEVICE = 0x01, Z_CAT_READ_ATTR, // Attribute reporting, either READ_ATTRIBUTE or REPORT_ATTRIBUTE, we coalesce all attributes reported if we can Z_CAT_VIRTUAL_OCCUPANCY, // Creation of a virtual attribute, typically after a time-out. Ex: Aqara presence sensor Z_CAT_REACHABILITY, // timer set to measure reachability of device, i.e. if we don't get an answer after 1s, it is marked as unreachable (for Alexa) - Z_CAT_READ_0006, // Read 0x0006 cluster - Z_CAT_READ_0008, // Read 0x0008 cluster - Z_CAT_READ_0102, // Read 0x0300 cluster - Z_CAT_READ_0300, // Read 0x0300 cluster + Z_CAT_PERMIT_JOIN, // timer to signal the end of the PermitJoin period + // Below will clear based on device + cluster pair. + Z_CLEAR_DEVICE_CLUSTER, + Z_CAT_READ_CLUSTER, + // Below will clear based on device + cluster + endpoint + Z_CLEAR_DEVICE_CLUSTER_ENDPOINT, + Z_CAT_EP_DESC, // read endpoint descriptor to gather clusters + Z_CAT_BIND, // send auto-binding to coordinator + Z_CAT_CONFIG_ATTR, // send a config attribute reporting request + Z_CAT_READ_ATTRIBUTE, // read a single attribute } Z_Def_Category; -const uint32_t Z_CAT_REACHABILITY_TIMEOUT = 1000; // 1000 ms or 1s +const uint32_t Z_CAT_REACHABILITY_TIMEOUT = 2000; // 1000 ms or 1s typedef struct Z_Deferred { // below are per device timers, used for example to query the new state of the device @@ -235,6 +259,7 @@ public: void setReachable(uint16_t shortaddr, bool reachable); void setLQI(uint16_t shortaddr, uint8_t lqi); + void setLastSeenNow(uint16_t shortaddr); // uint8_t getLQI(uint16_t shortaddr) const; void setBatteryPercent(uint16_t shortaddr, uint8_t bp); uint8_t getBatteryPercent(uint16_t shortaddr) const; @@ -245,7 +270,7 @@ public: // Dump json String dumpLightState(uint16_t shortaddr) const; String dump(uint32_t dump_mode, uint16_t status_shortaddr = 0) const; - int32_t deviceRestore(const JsonObject &json); + int32_t deviceRestore(JsonParserObject json); // General Zigbee device profile support void setZbProfile(uint16_t shortaddr, uint8_t zb_profile); @@ -258,8 +283,9 @@ public: bool isHueBulbHidden(uint16_t shortaddr) const ; // Timers - void resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uint8_t category); + void resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uint8_t category, uint16_t cluster = 0xFFFF, uint8_t endpoint = 0xFF); void setTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_ms, uint16_t cluster, uint8_t endpoint, uint8_t category, uint32_t value, Z_DeviceTimer func); + void queueTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_ms, uint16_t cluster, uint8_t endpoint, uint8_t category, uint32_t value, Z_DeviceTimer func); void runTimer(void); // Append or clear attributes Json structure @@ -617,6 +643,17 @@ void Z_Devices::setLQI(uint16_t shortaddr, uint8_t lqi) { getShortAddr(shortaddr).lqi = lqi; } +void Z_Devices::setLastSeenNow(uint16_t shortaddr) { + if (shortaddr == localShortAddr) { return; } + // Only update time if after 2020-01-01 0000. + // Fixes issue where zigbee device pings before WiFi/NTP has set utc_time + // to the correct time, and "last seen" calculations are based on the + // pre-corrected last_seen time and the since-corrected utc_time. + if (Rtc.utc_time < 1577836800) { return; } + getShortAddr(shortaddr).last_seen = Rtc.utc_time; +} + + void Z_Devices::setBatteryPercent(uint16_t shortaddr, uint8_t bp) { getShortAddr(shortaddr).batterypercent = bp; } @@ -723,12 +760,17 @@ bool Z_Devices::isHueBulbHidden(uint16_t shortaddr) const { // Deferred actions // Parse for a specific category, of all deferred for a device if category == 0xFF -void Z_Devices::resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uint8_t category) { +// Only with specific cluster number or for all clusters if cluster == 0xFFFF +void Z_Devices::resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uint8_t category, uint16_t cluster, uint8_t endpoint) { // iterate the list of deferred, and remove any linked to the shortaddr for (auto & defer : _deferred) { if ((defer.shortaddr == shortaddr) && (defer.groupaddr == groupaddr)) { if ((0xFF == category) || (defer.category == category)) { - _deferred.remove(&defer); + if ((0xFFFF == cluster) || (defer.cluster == cluster)) { + if ((0xFF == endpoint) || (defer.endpoint == endpoint)) { + _deferred.remove(&defer); + } + } } } } @@ -737,8 +779,8 @@ void Z_Devices::resetTimersForDevice(uint16_t shortaddr, uint16_t groupaddr, uin // Set timer for a specific device void Z_Devices::setTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_ms, uint16_t cluster, uint8_t endpoint, uint8_t category, uint32_t value, Z_DeviceTimer func) { // First we remove any existing timer for same device in same category, except for category=0x00 (they need to happen anyway) - if (category) { // if category == 0, we leave all previous - resetTimersForDevice(shortaddr, groupaddr, category); // remove any cluster + if (category >= Z_CLEAR_DEVICE) { // if category == 0, we leave all previous timers + resetTimersForDevice(shortaddr, groupaddr, category, category >= Z_CLEAR_DEVICE_CLUSTER ? cluster : 0xFFFF, category >= Z_CLEAR_DEVICE_CLUSTER_ENDPOINT ? endpoint : 0xFF); // remove any cluster } // Now create the new timer @@ -753,6 +795,21 @@ void Z_Devices::setTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_m func }; } +// Set timer after the already queued events +// I.e. the wait_ms is not counted from now, but from the last event queued, which is 'now' or in the future +void Z_Devices::queueTimer(uint16_t shortaddr, uint16_t groupaddr, uint32_t wait_ms, uint16_t cluster, uint8_t endpoint, uint8_t category, uint32_t value, Z_DeviceTimer func) { + Z_Device & device = getShortAddr(shortaddr); + uint32_t now_millis = millis(); + if (TimeReached(device.defer_last_message_sent)) { + device.defer_last_message_sent = now_millis; + } + // defer_last_message_sent equals now or a value in the future + device.defer_last_message_sent += wait_ms; + + // for queueing we don't clear the backlog, so we force category to Z_CAT_ALWAYS + setTimer(shortaddr, groupaddr, (device.defer_last_message_sent - now_millis), cluster, endpoint, Z_CAT_ALWAYS, value, func); +} + // Run timer at each tick // WARNING: don't set a new timer within a running timer, this causes memory corruption void Z_Devices::runTimer(void) { @@ -853,9 +910,20 @@ void Z_Devices::jsonPublishFlush(uint16_t shortaddr) { attr_list.reset(); // clear the attributes if (Settings.flag4.zigbee_distinct_topics) { - char subtopic[16]; - snprintf_P(subtopic, sizeof(subtopic), PSTR("%04X/" D_RSLT_SENSOR), shortaddr); - MqttPublishPrefixTopic_P(TELE, subtopic, Settings.flag.mqtt_sensor_retain); + if (Settings.flag4.zb_topic_fname && fname) { + //Clean special characters and check size of friendly name + char stemp[TOPSZ]; + strlcpy(stemp, (!strlen(fname)) ? MQTT_TOPIC : fname, sizeof(stemp)); + MakeValidMqtt(0, stemp); + //Create topic with Prefix3 and cleaned up friendly name + char frtopic[TOPSZ]; + snprintf_P(frtopic, sizeof(frtopic), PSTR("%s/%s/" D_RSLT_SENSOR), SettingsText(SET_MQTTPREFIX3), stemp); + MqttPublish(frtopic, Settings.flag.mqtt_sensor_retain); + } else { + char subtopic[16]; + snprintf_P(subtopic, sizeof(subtopic), PSTR("%04X/" D_RSLT_SENSOR), shortaddr); + MqttPublishPrefixTopic_P(TELE, subtopic, Settings.flag.mqtt_sensor_retain); + } } else { MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); } @@ -882,7 +950,7 @@ void Z_Devices::clean(void) { // - a number 0..99, the index number in ZigbeeStatus // - a friendly name, between quotes, example: "Room_Temp" uint16_t Z_Devices::parseDeviceParam(const char * param, bool short_must_be_known) const { - if (nullptr == param) { return 0; } + if (nullptr == param) { return BAD_SHORTADDR; } size_t param_len = strlen(param); char dataBuf[param_len + 1]; strcpy(dataBuf, param); @@ -918,54 +986,50 @@ uint16_t Z_Devices::parseDeviceParam(const char * param, bool short_must_be_know // Display the tracked status for a light String Z_Devices::dumpLightState(uint16_t shortaddr) const { - DynamicJsonBuffer jsonBuffer; - JsonObject& json = jsonBuffer.createObject(); + Z_attribute_list attr_list; char hex[8]; const Z_Device & device = findShortAddr(shortaddr); - if (foundDevice(device)) { - const char * fname = getFriendlyName(shortaddr); + const char * fname = getFriendlyName(shortaddr); + bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname? + snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), shortaddr); - bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname? - - snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), shortaddr); - - JsonObject& dev = use_fname ? json.createNestedObject((char*) fname) // casting (char*) forces a copy - : json.createNestedObject(hex); - if (use_fname) { - dev[F(D_JSON_ZIGBEE_DEVICE)] = hex; - } else if (fname) { - dev[F(D_JSON_ZIGBEE_NAME)] = (char*) fname; - } - - // expose the last known status of the bulb, for Hue integration - dev[F(D_JSON_ZIGBEE_LIGHT)] = getHueBulbtype(shortaddr); // sign extend, 0xFF changed as -1 - // dump all known values - dev[F("Reachable")] = device.getReachable(); // TODO TODO - if (device.validPower()) { dev[F("Power")] = device.getPower(); } - if (device.validDimmer()) { dev[F("Dimmer")] = device.dimmer; } - if (device.validColormode()) { dev[F("Colormode")] = device.colormode; } - if (device.validCT()) { dev[F("CT")] = device.ct; } - if (device.validSat()) { dev[F("Sat")] = device.sat; } - if (device.validHue()) { dev[F("Hue")] = device.hue; } - if (device.validX()) { dev[F("X")] = device.x; } - if (device.validY()) { dev[F("Y")] = device.y; } + attr_list.addAttribute(F(D_JSON_ZIGBEE_DEVICE)).setStr(hex); + if (fname) { + attr_list.addAttribute(F(D_JSON_ZIGBEE_NAME)).setStr(fname); } - String payload = ""; - payload.reserve(200); - json.printTo(payload); - return payload; + if (foundDevice(device)) { + // expose the last known status of the bulb, for Hue integration + attr_list.addAttribute(F(D_JSON_ZIGBEE_LIGHT)).setInt(getHueBulbtype(shortaddr)); // sign extend, 0xFF changed as -1 + // dump all known values + attr_list.addAttribute(F("Reachable")).setBool(device.getReachable()); + if (device.validPower()) { attr_list.addAttribute(F("Power")).setUInt(device.getPower()); } + if (device.validDimmer()) { attr_list.addAttribute(F("Dimmer")).setUInt(device.dimmer); } + if (device.validColormode()) { attr_list.addAttribute(F("Colormode")).setUInt(device.colormode); } + if (device.validCT()) { attr_list.addAttribute(F("CT")).setUInt(device.ct); } + if (device.validSat()) { attr_list.addAttribute(F("Sat")).setUInt(device.sat); } + if (device.validHue()) { attr_list.addAttribute(F("Hue")).setUInt(device.hue); } + if (device.validX()) { attr_list.addAttribute(F("X")).setUInt(device.x); } + if (device.validY()) { attr_list.addAttribute(F("Y")).setUInt(device.y); } + } + + Z_attribute_list attr_list_root; + Z_attribute * attr_root; + if (use_fname) { + attr_root = &attr_list_root.addAttribute(fname); + } else { + attr_root = &attr_list_root.addAttribute(hex); + } + attr_root->setStrRaw(attr_list.toString(true).c_str()); + return attr_list_root.toString(true); } // Dump the internal memory of Zigbee devices // Mode = 1: simple dump of devices addresses -// Mode = 2: simple dump of devices addresses and names -// Mode = 3: Mode 2 + also dump the endpoints, profiles and clusters +// Mode = 2: simple dump of devices addresses and names, endpoints, light String Z_Devices::dump(uint32_t dump_mode, uint16_t status_shortaddr) const { - DynamicJsonBuffer jsonBuffer; - JsonArray& json = jsonBuffer.createArray(); - JsonArray& devices = json; + Z_json_array json_arr; for (const auto & device : _devices) { uint16_t shortaddr = device.shortaddr; @@ -974,44 +1038,41 @@ String Z_Devices::dump(uint32_t dump_mode, uint16_t status_shortaddr) const { // ignore non-current device, if device specified if ((BAD_SHORTADDR != status_shortaddr) && (status_shortaddr != shortaddr)) { continue; } - JsonObject& dev = devices.createNestedObject(); + Z_attribute_list attr_list; snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), shortaddr); - dev[F(D_JSON_ZIGBEE_DEVICE)] = hex; + attr_list.addAttribute(F(D_JSON_ZIGBEE_DEVICE)).setStr(hex); if (device.friendlyName > 0) { - dev[F(D_JSON_ZIGBEE_NAME)] = (char*) device.friendlyName; + attr_list.addAttribute(F(D_JSON_ZIGBEE_NAME)).setStr(device.friendlyName); } if (2 <= dump_mode) { hex[0] = '0'; // prefix with '0x' hex[1] = 'x'; Uint64toHex(device.longaddr, &hex[2], 64); - dev[F("IEEEAddr")] = hex; + attr_list.addAttribute(F("IEEEAddr")).setStr(hex); if (device.modelId) { - dev[F(D_JSON_MODEL D_JSON_ID)] = device.modelId; + attr_list.addAttribute(F(D_JSON_MODEL D_JSON_ID)).setStr(device.modelId); } int8_t bulbtype = getHueBulbtype(shortaddr); if (bulbtype >= 0) { - dev[F(D_JSON_ZIGBEE_LIGHT)] = bulbtype; // sign extend, 0xFF changed as -1 + attr_list.addAttribute(F(D_JSON_ZIGBEE_LIGHT)).setInt(bulbtype); // sign extend, 0xFF changed as -1 } if (device.manufacturerId) { - dev[F("Manufacturer")] = device.manufacturerId; + attr_list.addAttribute(F("Manufacturer")).setStr(device.manufacturerId); } - JsonArray& dev_endpoints = dev.createNestedArray(F("Endpoints")); + Z_json_array arr_ep; for (uint32_t i = 0; i < endpoints_max; i++) { uint8_t endpoint = device.endpoints[i]; if (0x00 == endpoint) { break; } - - snprintf_P(hex, sizeof(hex), PSTR("0x%02X"), endpoint); - dev_endpoints.add(hex); + arr_ep.add(endpoint); } + attr_list.addAttribute(F("Endpoints")).setStrRaw(arr_ep.toString().c_str()); } + json_arr.addStrRaw(attr_list.toString(true).c_str()); } - String payload = ""; - payload.reserve(200); - json.printTo(payload); - return payload; + return json_arr.toString(); } // Restore a single device configuration based on json export @@ -1023,7 +1084,7 @@ String Z_Devices::dump(uint32_t dump_mode, uint16_t status_shortaddr) const { // <0 : Error // // Ex: {"Device":"0x5ADF","Name":"IKEA_Light","IEEEAddr":"0x90FD9FFFFE03B051","ModelId":"TRADFRI bulb E27 WS opal 980lm","Manufacturer":"IKEA of Sweden","Endpoints":["0x01","0xF2"]} -int32_t Z_Devices::deviceRestore(const JsonObject &json) { +int32_t Z_Devices::deviceRestore(JsonParserObject json) { // params uint16_t device = 0x0000; // 0x0000 is coordinator so considered invalid @@ -1031,56 +1092,38 @@ int32_t Z_Devices::deviceRestore(const JsonObject &json) { const char * modelid = nullptr; const char * manufid = nullptr; const char * friendlyname = nullptr; - int8_t bulbtype = 0xFF; + int8_t bulbtype = -1; size_t endpoints_len = 0; // read mandatory "Device" - const JsonVariant &val_device = GetCaseInsensitive(json, PSTR("Device")); - if (nullptr != &val_device) { - device = strToUInt(val_device); + JsonParserToken val_device = json[PSTR("Device")]; + if (val_device) { + device = (uint32_t) val_device.getUInt(device); } else { return -1; // missing "Device" attribute } - // read "IEEEAddr" 64 bits in format "0x0000000000000000" - const JsonVariant &val_ieeeaddr = GetCaseInsensitive(json, PSTR("IEEEAddr")); - if (nullptr != &val_ieeeaddr) { - ieeeaddr = strtoull(val_ieeeaddr.as(), nullptr, 0); - } - - // read "Name" - friendlyname = getCaseInsensitiveConstCharNull(json, PSTR("Name")); - - // read "ModelId" - modelid = getCaseInsensitiveConstCharNull(json, PSTR("ModelId")); - - // read "Manufacturer" - manufid = getCaseInsensitiveConstCharNull(json, PSTR("Manufacturer")); - - // read "Light" - const JsonVariant &val_bulbtype = GetCaseInsensitive(json, PSTR(D_JSON_ZIGBEE_LIGHT)); - if (nullptr != &val_bulbtype) { bulbtype = strToUInt(val_bulbtype);; } + ieeeaddr = json.getULong(PSTR("IEEEAddr"), ieeeaddr); // read "IEEEAddr" 64 bits in format "0x0000000000000000" + friendlyname = json.getStr(PSTR("Name"), nullptr); // read "Name" + modelid = json.getStr(PSTR("ModelId"), nullptr); + manufid = json.getStr(PSTR("Manufacturer"), nullptr); + JsonParserToken tok_bulbtype = json[PSTR(D_JSON_ZIGBEE_LIGHT)]; // update internal device information updateDevice(device, ieeeaddr); if (modelid) { setModelId(device, modelid); } if (manufid) { setManufId(device, manufid); } if (friendlyname) { setFriendlyName(device, friendlyname); } - if (&val_bulbtype) { setHueBulbtype(device, bulbtype); } + if (tok_bulbtype) { setHueBulbtype(device, tok_bulbtype.getInt()); } // read "Endpoints" - const JsonVariant &val_endpoints = GetCaseInsensitive(json, PSTR("Endpoints")); - if ((nullptr != &val_endpoints) && (val_endpoints.is())) { - const JsonArray &arr_ep = val_endpoints.as(); - endpoints_len = arr_ep.size(); + JsonParserToken val_endpoints = json[PSTR("Endpoints")]; + if (val_endpoints.isArray()) { + JsonParserArray arr_ep = JsonParserArray(val_endpoints); clearEndpoints(device); // clear even if array is empty - if (endpoints_len) { - for (auto ep_elt : arr_ep) { - uint8_t ep = strToUInt(ep_elt); - if (ep) { - addEndpoint(device, ep); - } - } + for (auto ep_elt : arr_ep) { + uint8_t ep = ep_elt.getUInt(); + if (ep) { addEndpoint(device, ep); } } } diff --git a/tasmota/xdrv_23_zigbee_3_hue.ino b/tasmota/xdrv_23_zigbee_3_hue.ino index 2accf849c..1f4b18e19 100644 --- a/tasmota/xdrv_23_zigbee_3_hue.ino +++ b/tasmota/xdrv_23_zigbee_3_hue.ino @@ -218,10 +218,12 @@ void ZigbeeHandleHue(uint16_t shortaddr, uint32_t device_id, String &response) { if (Webserver->args()) { response = "["; - StaticJsonBuffer<300> jsonBuffer; - JsonObject &hue_json = jsonBuffer.parseObject(Webserver->arg((Webserver->args())-1)); - if (hue_json.containsKey("on")) { - on = hue_json["on"]; + JsonParser parser((char*) Webserver->arg((Webserver->args())-1).c_str()); + JsonParserObject root = parser.getRootObject(); + + JsonParserToken hue_on = root[PSTR("on")]; + if (hue_on) { + on = hue_on.getBool(); snprintf_P(buf, buf_size, PSTR("{\"success\":{\"/lights/%d/state/on\":%s}}"), device_id, on ? "true" : "false"); @@ -235,8 +237,10 @@ void ZigbeeHandleHue(uint16_t shortaddr, uint32_t device_id, String &response) { resp = true; } - if (hue_json.containsKey("bri")) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. - bri = hue_json["bri"]; + parser.setCurrent(); + JsonParserToken hue_bri = root[PSTR("bri")]; + if (hue_bri) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. + bri = hue_bri.getUInt(); prev_bri = bri; // store command value if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -252,13 +256,16 @@ void ZigbeeHandleHue(uint16_t shortaddr, uint32_t device_id, String &response) { } // handle xy before Hue/Sat // If the request contains both XY and HS, we wan't to give priority to HS - if (hue_json.containsKey("xy")) { - float x = hue_json["xy"][0]; - float y = hue_json["xy"][1]; - const String &x_str = hue_json["xy"][0]; - const String &y_str = hue_json["xy"][1]; - x_str.toCharArray(prev_x_str, sizeof(prev_x_str)); - y_str.toCharArray(prev_y_str, sizeof(prev_y_str)); + parser.setCurrent(); + JsonParserToken hue_xy = root[PSTR("xy")]; + if (hue_xy) { + JsonParserArray arr_xy = JsonParserArray(hue_xy); + JsonParserToken tok_x = arr_xy[0]; + JsonParserToken tok_y = arr_xy[1]; + float x = tok_x.getFloat(); + float y = tok_y.getFloat(); + strlcpy(prev_x_str, tok_x.getStr(), sizeof(prev_x_str)); + strlcpy(prev_y_str, tok_y.getStr(), sizeof(prev_y_str)); if (resp) { response += ","; } snprintf_P(buf, buf_size, PSTR("{\"success\":{\"/lights/%d/state/xy\":[%s,%s]}}"), @@ -270,8 +277,11 @@ void ZigbeeHandleHue(uint16_t shortaddr, uint32_t device_id, String &response) { ZigbeeHueXY(shortaddr, xi, yi); } bool huesat_changed = false; - if (hue_json.containsKey("hue")) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. - hue = hue_json["hue"]; + + parser.setCurrent(); + JsonParserToken hue_hue = root[PSTR("hue")]; + if (hue_hue) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. + hue = hue_hue.getUInt(); prev_hue = hue; if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -285,8 +295,11 @@ void ZigbeeHandleHue(uint16_t shortaddr, uint32_t device_id, String &response) { } resp = true; } - if (hue_json.containsKey("sat")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). - sat = hue_json["sat"]; + + parser.setCurrent(); + JsonParserToken hue_sat = root[PSTR("sat")]; + if (hue_sat) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). + sat = hue_sat.getUInt(); prev_sat = sat; // store command value if (resp) { response += ","; } snprintf_P(buf, buf_size, @@ -303,8 +316,11 @@ void ZigbeeHandleHue(uint16_t shortaddr, uint32_t device_id, String &response) { } resp = true; } - if (hue_json.containsKey("ct")) { // Color temperature 153 (Cold) to 500 (Warm) - ct = hue_json["ct"]; + + parser.setCurrent(); + JsonParserToken hue_ct = root[PSTR("ct")]; + if (hue_ct) { // Color temperature 153 (Cold) to 500 (Warm) + ct = hue_ct.getUInt(); prev_ct = ct; // store commande value if (resp) { response += ","; } snprintf_P(buf, buf_size, diff --git a/tasmota/xdrv_23_zigbee_5__constants.ino b/tasmota/xdrv_23_zigbee_5__constants.ino index 12a299a5f..7d35d7862 100644 --- a/tasmota/xdrv_23_zigbee_5__constants.ino +++ b/tasmota/xdrv_23_zigbee_5__constants.ino @@ -259,6 +259,22 @@ const char Z_strings[] PROGMEM = "DecelerationTimeLift" "\x00" "IntermediateSetpointsLift" "\x00" "IntermediateSetpointsTilt" "\x00" + "LocalTemperature" "\x00" + "OutdoorTemperature" "\x00" + "PICoolingDemand" "\x00" + "PIHeatingDemand" "\x00" + "LocalTemperatureCalibration" "\x00" + "OccupiedCoolingSetpoint" "\x00" + "OccupiedHeatingSetpoint" "\x00" + "UnoccupiedCoolingSetpoint" "\x00" + "UnoccupiedHeatingSetpoint" "\x00" + "RemoteSensing" "\x00" + "ControlSequenceOfOperation" "\x00" + "SystemMode" "\x00" + "TRVMode" "\x00" + "SetValvePosition" "\x00" + "EurotronicErrors" "\x00" + "CurrentTemperatureSetPoint" "\x00" "Hue" "\x00" "Sat" "\x00" "RemainingTime" "\x00" @@ -300,7 +316,6 @@ const char Z_strings[] PROGMEM = "TemperatureMinMeasuredValue" "\x00" "TemperatureMaxMeasuredValue" "\x00" "TemperatureTolerance" "\x00" - "PressureUnit" "\x00" "Pressure" "\x00" "PressureMinMeasuredValue" "\x00" "PressureMaxMeasuredValue" "\x00" @@ -310,6 +325,7 @@ const char Z_strings[] PROGMEM = "PressureMaxScaledValue" "\x00" "PressureScaledTolerance" "\x00" "PressureScale" "\x00" + "SeaPressure" "\x00" "FlowRate" "\x00" "FlowMinMeasuredValue" "\x00" "FlowMaxMeasuredValue" "\x00" @@ -620,179 +636,195 @@ enum Z_offsets { Zo_DecelerationTimeLift = 3144, Zo_IntermediateSetpointsLift = 3165, Zo_IntermediateSetpointsTilt = 3191, - Zo_Hue = 3217, - Zo_Sat = 3221, - Zo_RemainingTime = 3225, - Zo_X = 3239, - Zo_Y = 3241, - Zo_DriftCompensation = 3243, - Zo_CompensationText = 3261, - Zo_CT = 3278, - Zo_ColorMode = 3281, - Zo_NumberOfPrimaries = 3291, - Zo_Primary1X = 3309, - Zo_Primary1Y = 3319, - Zo_Primary1Intensity = 3329, - Zo_Primary2X = 3347, - Zo_Primary2Y = 3357, - Zo_Primary2Intensity = 3367, - Zo_Primary3X = 3385, - Zo_Primary3Y = 3395, - Zo_Primary3Intensity = 3405, - Zo_WhitePointX = 3423, - Zo_WhitePointY = 3435, - Zo_ColorPointRX = 3447, - Zo_ColorPointRY = 3460, - Zo_ColorPointRIntensity = 3473, - Zo_ColorPointGX = 3494, - Zo_ColorPointGY = 3507, - Zo_ColorPointGIntensity = 3520, - Zo_ColorPointBX = 3541, - Zo_ColorPointBY = 3554, - Zo_ColorPointBIntensity = 3567, - Zo_Illuminance = 3588, - Zo_IlluminanceMinMeasuredValue = 3600, - Zo_IlluminanceMaxMeasuredValue = 3628, - Zo_IlluminanceTolerance = 3656, - Zo_IlluminanceLightSensorType = 3677, - Zo_IlluminanceLevelStatus = 3704, - Zo_IlluminanceTargetLevel = 3727, - Zo_Temperature = 3750, - Zo_TemperatureMinMeasuredValue = 3762, - Zo_TemperatureMaxMeasuredValue = 3790, - Zo_TemperatureTolerance = 3818, - Zo_PressureUnit = 3839, - Zo_Pressure = 3852, - Zo_PressureMinMeasuredValue = 3861, - Zo_PressureMaxMeasuredValue = 3886, - Zo_PressureTolerance = 3911, - Zo_PressureScaledValue = 3929, - Zo_PressureMinScaledValue = 3949, - Zo_PressureMaxScaledValue = 3972, - Zo_PressureScaledTolerance = 3995, - Zo_PressureScale = 4019, - Zo_FlowRate = 4033, - Zo_FlowMinMeasuredValue = 4042, - Zo_FlowMaxMeasuredValue = 4063, - Zo_FlowTolerance = 4084, - Zo_Humidity = 4098, - Zo_HumidityMinMeasuredValue = 4107, - Zo_HumidityMaxMeasuredValue = 4132, - Zo_HumidityTolerance = 4157, - Zo_Occupancy = 4175, - Zo_OccupancySensorType = 4185, - Zo_ZoneState = 4205, - Zo_ZoneType = 4215, - Zo_ZoneStatus = 4224, - Zo_CurrentSummDelivered = 4235, - Zo_CompanyName = 4256, - Zo_MeterTypeID = 4268, - Zo_DataQualityID = 4280, - Zo_CustomerName = 4294, - Zo_Model = 4307, - Zo_PartNumber = 4313, - Zo_ProductRevision = 4324, - Zo_SoftwareRevision = 4340, - Zo_UtilityName = 4357, - Zo_POD = 4369, - Zo_AvailablePower = 4373, - Zo_PowerThreshold = 4388, - Zo_RMSVoltage = 4403, - Zo_RMSCurrent = 4414, - Zo_ActivePower = 4425, - Zo_NumberOfResets = 4437, - Zo_PersistentMemoryWrites = 4452, - Zo_LastMessageLQI = 4475, - Zo_LastMessageRSSI = 4490, - Zo_Identify = 4506, - Zo_xxxx = 4515, - Zo_IdentifyQuery = 4520, - Zo_AddGroup = 4534, - Zo_xxxx00 = 4543, - Zo_ViewGroup = 4550, - Zo_GetGroup = 4560, - Zo_01xxxx = 4569, - Zo_GetAllGroups = 4576, - Zo_00 = 4589, - Zo_RemoveGroup = 4592, - Zo_RemoveAllGroups = 4604, - Zo_ViewScene = 4620, - Zo_xxxxyy = 4630, - Zo_RemoveScene = 4637, - Zo_RemoveAllScenes = 4649, - Zo_RecallScene = 4665, - Zo_GetSceneMembership = 4677, - Zo_PowerOffEffect = 4696, - Zo_xxyy = 4711, - Zo_PowerOnRecall = 4716, - Zo_PowerOnTimer = 4730, - Zo_xxyyyyzzzz = 4743, - Zo_xx0A00 = 4754, - Zo_DimmerUp = 4761, - Zo_00190200 = 4770, - Zo_DimmerDown = 4779, - Zo_01190200 = 4790, - Zo_DimmerStop = 4799, - Zo_ResetAlarm = 4810, - Zo_xxyyyy = 4821, - Zo_ResetAllAlarms = 4828, - Zo_xx000A00 = 4843, - Zo_HueSat = 4852, - Zo_xxyy0A00 = 4859, - Zo_Color = 4868, - Zo_xxxxyyyy0A00 = 4874, - Zo_xxxx0A00 = 4887, - Zo_ShutterOpen = 4896, - Zo_ShutterClose = 4908, - Zo_ShutterStop = 4921, - Zo_ShutterLift = 4933, - Zo_xx = 4945, - Zo_ShutterTilt = 4948, - Zo_Shutter = 4960, - Zo_DimmerMove = 4968, - Zo_xx0A = 4979, - Zo_DimmerStepUp = 4984, - Zo_00xx0A00 = 4997, - Zo_DimmerStepDown = 5006, - Zo_01xx0A00 = 5021, - Zo_DimmerStep = 5030, - Zo_xx190A00 = 5041, - Zo_01 = 5050, - Zo_HueMove = 5053, - Zo_xx19 = 5061, - Zo_HueStepUp = 5066, - Zo_HueStepDown = 5076, - Zo_03xx0A00 = 5088, - Zo_HueStep = 5097, - Zo_SatMove = 5105, - Zo_SatStep = 5113, - Zo_xx190A = 5121, - Zo_ColorMove = 5128, - Zo_xxxxyyyy = 5138, - Zo_ColorStep = 5147, - Zo_ColorTempMoveUp = 5157, - Zo_01xxxx000000000000 = 5173, - Zo_ColorTempMoveDown = 5192, - Zo_03xxxx000000000000 = 5210, - Zo_ColorTempMoveStop = 5229, - Zo_00xxxx000000000000 = 5247, - Zo_ColorTempMove = 5266, - Zo_xxyyyy000000000000 = 5280, - Zo_ColorTempStepUp = 5299, - Zo_01xxxx0A0000000000 = 5315, - Zo_ColorTempStepDown = 5334, - Zo_03xxxx0A0000000000 = 5352, - Zo_ColorTempStep = 5371, - Zo_xxyyyy0A0000000000 = 5385, - Zo_ArrowClick = 5404, - Zo_ArrowHold = 5415, - Zo_ArrowRelease = 5425, - Zo_ZoneStatusChange = 5438, - Zo_xxxxyyzz = 5455, - Zo_xxyyzzzz = 5464, - Zo_AddScene = 5473, - Zo_xxyyyyzz = 5482, - Zo_StoreScene = 5491, + Zo_LocalTemperature = 3217, + Zo_OutdoorTemperature = 3234, + Zo_PICoolingDemand = 3253, + Zo_PIHeatingDemand = 3269, + Zo_LocalTemperatureCalibration = 3285, + Zo_OccupiedCoolingSetpoint = 3313, + Zo_OccupiedHeatingSetpoint = 3337, + Zo_UnoccupiedCoolingSetpoint = 3361, + Zo_UnoccupiedHeatingSetpoint = 3387, + Zo_RemoteSensing = 3413, + Zo_ControlSequenceOfOperation = 3427, + Zo_SystemMode = 3454, + Zo_TRVMode = 3465, + Zo_SetValvePosition = 3473, + Zo_EurotronicErrors = 3490, + Zo_CurrentTemperatureSetPoint = 3507, + Zo_Hue = 3534, + Zo_Sat = 3538, + Zo_RemainingTime = 3542, + Zo_X = 3556, + Zo_Y = 3558, + Zo_DriftCompensation = 3560, + Zo_CompensationText = 3578, + Zo_CT = 3595, + Zo_ColorMode = 3598, + Zo_NumberOfPrimaries = 3608, + Zo_Primary1X = 3626, + Zo_Primary1Y = 3636, + Zo_Primary1Intensity = 3646, + Zo_Primary2X = 3664, + Zo_Primary2Y = 3674, + Zo_Primary2Intensity = 3684, + Zo_Primary3X = 3702, + Zo_Primary3Y = 3712, + Zo_Primary3Intensity = 3722, + Zo_WhitePointX = 3740, + Zo_WhitePointY = 3752, + Zo_ColorPointRX = 3764, + Zo_ColorPointRY = 3777, + Zo_ColorPointRIntensity = 3790, + Zo_ColorPointGX = 3811, + Zo_ColorPointGY = 3824, + Zo_ColorPointGIntensity = 3837, + Zo_ColorPointBX = 3858, + Zo_ColorPointBY = 3871, + Zo_ColorPointBIntensity = 3884, + Zo_Illuminance = 3905, + Zo_IlluminanceMinMeasuredValue = 3917, + Zo_IlluminanceMaxMeasuredValue = 3945, + Zo_IlluminanceTolerance = 3973, + Zo_IlluminanceLightSensorType = 3994, + Zo_IlluminanceLevelStatus = 4021, + Zo_IlluminanceTargetLevel = 4044, + Zo_Temperature = 4067, + Zo_TemperatureMinMeasuredValue = 4079, + Zo_TemperatureMaxMeasuredValue = 4107, + Zo_TemperatureTolerance = 4135, + Zo_Pressure = 4156, + Zo_PressureMinMeasuredValue = 4165, + Zo_PressureMaxMeasuredValue = 4190, + Zo_PressureTolerance = 4215, + Zo_PressureScaledValue = 4233, + Zo_PressureMinScaledValue = 4253, + Zo_PressureMaxScaledValue = 4276, + Zo_PressureScaledTolerance = 4299, + Zo_PressureScale = 4323, + Zo_SeaPressure = 4337, + Zo_FlowRate = 4349, + Zo_FlowMinMeasuredValue = 4358, + Zo_FlowMaxMeasuredValue = 4379, + Zo_FlowTolerance = 4400, + Zo_Humidity = 4414, + Zo_HumidityMinMeasuredValue = 4423, + Zo_HumidityMaxMeasuredValue = 4448, + Zo_HumidityTolerance = 4473, + Zo_Occupancy = 4491, + Zo_OccupancySensorType = 4501, + Zo_ZoneState = 4521, + Zo_ZoneType = 4531, + Zo_ZoneStatus = 4540, + Zo_CurrentSummDelivered = 4551, + Zo_CompanyName = 4572, + Zo_MeterTypeID = 4584, + Zo_DataQualityID = 4596, + Zo_CustomerName = 4610, + Zo_Model = 4623, + Zo_PartNumber = 4629, + Zo_ProductRevision = 4640, + Zo_SoftwareRevision = 4656, + Zo_UtilityName = 4673, + Zo_POD = 4685, + Zo_AvailablePower = 4689, + Zo_PowerThreshold = 4704, + Zo_RMSVoltage = 4719, + Zo_RMSCurrent = 4730, + Zo_ActivePower = 4741, + Zo_NumberOfResets = 4753, + Zo_PersistentMemoryWrites = 4768, + Zo_LastMessageLQI = 4791, + Zo_LastMessageRSSI = 4806, + Zo_Identify = 4822, + Zo_xxxx = 4831, + Zo_IdentifyQuery = 4836, + Zo_AddGroup = 4850, + Zo_xxxx00 = 4859, + Zo_ViewGroup = 4866, + Zo_GetGroup = 4876, + Zo_01xxxx = 4885, + Zo_GetAllGroups = 4892, + Zo_00 = 4905, + Zo_RemoveGroup = 4908, + Zo_RemoveAllGroups = 4920, + Zo_ViewScene = 4936, + Zo_xxxxyy = 4946, + Zo_RemoveScene = 4953, + Zo_RemoveAllScenes = 4965, + Zo_RecallScene = 4981, + Zo_GetSceneMembership = 4993, + Zo_PowerOffEffect = 5012, + Zo_xxyy = 5027, + Zo_PowerOnRecall = 5032, + Zo_PowerOnTimer = 5046, + Zo_xxyyyyzzzz = 5059, + Zo_xx0A00 = 5070, + Zo_DimmerUp = 5077, + Zo_00190200 = 5086, + Zo_DimmerDown = 5095, + Zo_01190200 = 5106, + Zo_DimmerStop = 5115, + Zo_ResetAlarm = 5126, + Zo_xxyyyy = 5137, + Zo_ResetAllAlarms = 5144, + Zo_xx000A00 = 5159, + Zo_HueSat = 5168, + Zo_xxyy0A00 = 5175, + Zo_Color = 5184, + Zo_xxxxyyyy0A00 = 5190, + Zo_xxxx0A00 = 5203, + Zo_ShutterOpen = 5212, + Zo_ShutterClose = 5224, + Zo_ShutterStop = 5237, + Zo_ShutterLift = 5249, + Zo_xx = 5261, + Zo_ShutterTilt = 5264, + Zo_Shutter = 5276, + Zo_DimmerMove = 5284, + Zo_xx0A = 5295, + Zo_DimmerStepUp = 5300, + Zo_00xx0A00 = 5313, + Zo_DimmerStepDown = 5322, + Zo_01xx0A00 = 5337, + Zo_DimmerStep = 5346, + Zo_xx190A00 = 5357, + Zo_01 = 5366, + Zo_HueMove = 5369, + Zo_xx19 = 5377, + Zo_HueStepUp = 5382, + Zo_HueStepDown = 5392, + Zo_03xx0A00 = 5404, + Zo_HueStep = 5413, + Zo_SatMove = 5421, + Zo_SatStep = 5429, + Zo_xx190A = 5437, + Zo_ColorMove = 5444, + Zo_xxxxyyyy = 5454, + Zo_ColorStep = 5463, + Zo_ColorTempMoveUp = 5473, + Zo_01xxxx000000000000 = 5489, + Zo_ColorTempMoveDown = 5508, + Zo_03xxxx000000000000 = 5526, + Zo_ColorTempMoveStop = 5545, + Zo_00xxxx000000000000 = 5563, + Zo_ColorTempMove = 5582, + Zo_xxyyyy000000000000 = 5596, + Zo_ColorTempStepUp = 5615, + Zo_01xxxx0A0000000000 = 5631, + Zo_ColorTempStepDown = 5650, + Zo_03xxxx0A0000000000 = 5668, + Zo_ColorTempStep = 5687, + Zo_xxyyyy0A0000000000 = 5701, + Zo_ArrowClick = 5720, + Zo_ArrowHold = 5731, + Zo_ArrowRelease = 5741, + Zo_ZoneStatusChange = 5754, + Zo_xxxxyyzz = 5771, + Zo_xxyyzzzz = 5780, + Zo_AddScene = 5789, + Zo_xxyyyyzz = 5798, + Zo_StoreScene = 5807, }; diff --git a/tasmota/xdrv_23_zigbee_5_converters.ino b/tasmota/xdrv_23_zigbee_5_converters.ino index 1155a6f2e..217a003c4 100644 --- a/tasmota/xdrv_23_zigbee_5_converters.ino +++ b/tasmota/xdrv_23_zigbee_5_converters.ino @@ -106,16 +106,16 @@ enum Cx_cluster_short { Cx0000, Cx0001, Cx0002, Cx0003, Cx0004, Cx0005, Cx0006, Cx0007, Cx0008, Cx0009, Cx000A, Cx000B, Cx000C, Cx000D, Cx000E, Cx000F, Cx0010, Cx0011, Cx0012, Cx0013, Cx0014, Cx001A, Cx0020, Cx0100, - Cx0101, Cx0102, Cx0300, Cx0400, Cx0401, Cx0402, Cx0403, Cx0404, - Cx0405, Cx0406, Cx0500, Cx0702, Cx0B01, Cx0B04, Cx0B05, + Cx0101, Cx0102, Cx0201, Cx0300, Cx0400, Cx0401, Cx0402, Cx0403, + Cx0404, Cx0405, Cx0406, Cx0500, Cx0702, Cx0B01, Cx0B04, Cx0B05, }; const uint16_t Cx_cluster[] PROGMEM = { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x001A, 0x0020, 0x0100, - 0x0101, 0x0102, 0x0300, 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, - 0x0405, 0x0406, 0x0500, 0x0702, 0x0B01, 0x0B04, 0x0B05, + 0x0101, 0x0102, 0x0201, 0x0300, 0x0400, 0x0401, 0x0402, 0x0403, + 0x0404, 0x0405, 0x0406, 0x0500, 0x0702, 0x0B01, 0x0B04, 0x0B05, }; uint16_t CxToCluster(uint8_t cx) { @@ -124,6 +124,16 @@ uint16_t CxToCluster(uint8_t cx) { } return 0xFFFF; } + +uint8_t ClusterToCx(uint16_t cluster) { + for (uint8_t i=0; icluster_short)); + uint16_t conv_attr_id = pgm_read_word(&converter->attribute); + + if ((conv_cluster == cluster) && (conv_attr_id == attr_id)) { + if (multiplier) { *multiplier = pgm_read_byte(&converter->multiplier); } + if (attr_type) { *attr_type = pgm_read_byte(&converter->type); } + return (const __FlashStringHelper*) (Z_strings + pgm_read_word(&converter->name_offset)); + } + } + return nullptr; +} + class ZCLFrame { public: @@ -613,6 +662,7 @@ public: void parseReportAttributes(Z_attribute_list& attr_list); void generateSyntheticAttributes(Z_attribute_list& attr_list); + void computeSyntheticAttributes(Z_attribute_list& attr_list); void generateCallBacks(Z_attribute_list& attr_list); void parseReadAttributes(Z_attribute_list& attr_list); void parseReadAttributesResponse(Z_attribute_list& attr_list); @@ -1044,6 +1094,9 @@ void ZCLFrame::parseReportAttributes(Z_attribute_list& attr_list) { } } +// +// Extract attributes hidden in other compound attributes +// void ZCLFrame::generateSyntheticAttributes(Z_attribute_list& attr_list) { // scan through attributes and apply specific converters for (auto &attr : attr_list) { @@ -1068,6 +1121,48 @@ void ZCLFrame::generateSyntheticAttributes(Z_attribute_list& attr_list) { } } +// +// Compute new attributes based on the standard set +// Note: both function are now split to compute on extracted attributes +// +void ZCLFrame::computeSyntheticAttributes(Z_attribute_list& attr_list) { + // scan through attributes and apply specific converters + for (auto &attr : attr_list) { + if (attr.key_is_str) { continue; } // pass if key is a name + uint32_t ccccaaaa = (attr.key.id.cluster << 16) | attr.key.id.attr_id; + + switch (ccccaaaa) { // 0xccccaaaa . c=cluster, a=attribute + case 0x00010020: // BatteryVoltage + if (attr_list.countAttribute(0x0001,0x0021) == 0) { // if it does not already contain BatteryPercentage + uint32_t mv = attr.getUInt()*100; + attr_list.addAttribute(0x0001, 0x0021).setUInt(toPercentageCR2032(mv) * 2); + } + break; + case 0x02010008: // Pi Heating Demand - solve Eutotronic bug + { + const char * manufacturer_c = zigbee_devices.getManufacturerId(_srcaddr); // null if unknown + String manufacturerId((char*) manufacturer_c); + if (manufacturerId.equals(F("Eurotronic"))) { + // Eurotronic does not report 0..100 but 0..255, including 255 which is normally an ivalid value + uint8_t valve = attr.getUInt(); + if (attr.isNone()) { valve = 255; } + uint8_t valve_100 = changeUIntScale(valve, 0, 255, 0, 100); + attr.setUInt(valve_100); + } + } + break; + case 0x04030000: // Pressure + { + int16_t pressure = attr.getInt(); + int16_t pressure_sealevel = (pressure / FastPrecisePow(1.0 - ((float)Settings.altitude / 44330.0f), 5.255f)) - 21.6f; + attr_list.addAttribute(0x0403, 0xFF00).setInt(pressure_sealevel); + // We create a synthetic attribute 0403/FF00 to indicate sea level + } + break; + } + } +} + // Set deferred callbacks for Occupancy // TODO make delay a parameter void ZCLFrame::generateCallBacks(Z_attribute_list& attr_list) { @@ -1095,35 +1190,30 @@ void ZCLFrame::generateCallBacks(Z_attribute_list& attr_list) { // Set timers to read back values. // If it's a device address, also set a timer for reachability test void sendHueUpdate(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint = 0) { - int32_t z_cat = -1; - uint32_t wait_ms = 0; + uint32_t wait_ms = 0xFFFF; switch (cluster) { case 0x0006: - z_cat = Z_CAT_READ_0006; wait_ms = 200; // wait 0.2 s break; case 0x0008: - z_cat = Z_CAT_READ_0008; wait_ms = 1050; // wait 1.0 s break; case 0x0102: - z_cat = Z_CAT_READ_0102; wait_ms = 10000; // wait 10.0 s break; case 0x0300: - z_cat = Z_CAT_READ_0300; wait_ms = 1050; // wait 1.0 s break; default: break; } - if (z_cat >= 0) { + if (0xFFFF != wait_ms) { if ((BAD_SHORTADDR != shortaddr) && (0 == endpoint)) { endpoint = zigbee_devices.findFirstEndpoint(shortaddr); } if ((BAD_SHORTADDR == shortaddr) || (endpoint)) { // send if group address or endpoint is known - zigbee_devices.setTimer(shortaddr, groupaddr, wait_ms, cluster, endpoint, z_cat, 0 /* value */, &Z_ReadAttrCallback); + zigbee_devices.queueTimer(shortaddr, groupaddr, wait_ms, cluster, endpoint, Z_CAT_READ_CLUSTER, 0 /* value */, &Z_ReadAttrCallback); if (BAD_SHORTADDR != shortaddr) { // reachability test is not possible for group addresses, since we don't know the list of devices in the group zigbee_devices.setTimer(shortaddr, groupaddr, wait_ms + Z_CAT_REACHABILITY_TIMEOUT, cluster, endpoint, Z_CAT_REACHABILITY, 0 /* value */, &Z_Unreachable); } @@ -1170,16 +1260,27 @@ void ZCLFrame::parseReadAttributes(Z_attribute_list& attr_list) { // ZCL_CONFIGURE_REPORTING_RESPONSE void ZCLFrame::parseConfigAttributes(Z_attribute_list& attr_list) { - uint32_t i = 0; uint32_t len = _payload.len(); - uint8_t status = _payload.get8(i); - Z_attribute_list attr_config_response; - attr_config_response.addAttribute(F("Status")).setUInt(status); - attr_config_response.addAttribute(F("StatusMsg")).setStr(getZigbeeStatusMessage(status).c_str()); + Z_attribute_list attr_config_list; + for (uint32_t i=0; len >= i+4; i+=4) { + uint8_t status = _payload.get8(i); + uint16_t attr_id = _payload.get8(i+2); + + Z_attribute_list attr_config_response; + attr_config_response.addAttribute(F("Status")).setUInt(status); + attr_config_response.addAttribute(F("StatusMsg")).setStr(getZigbeeStatusMessage(status).c_str()); + + const __FlashStringHelper* attr_name = zigbeeFindAttributeById(_cluster_id, attr_id, nullptr, nullptr); + if (attr_name) { + attr_config_list.addAttribute(attr_name).setStrRaw(attr_config_response.toString(true).c_str()); + } else { + attr_config_list.addAttribute(_cluster_id, attr_id).setStrRaw(attr_config_response.toString(true).c_str()); + } + } Z_attribute &attr_1 = attr_list.addAttribute(F("ConfigResponse")); - attr_1.setStrRaw(attr_config_response.toString(true).c_str()); + attr_1.setStrRaw(attr_config_list.toString(true).c_str()); } // ZCL_READ_REPORTING_CONFIGURATION_RESPONSE @@ -1459,7 +1560,7 @@ void ZCLFrame::syntheticAqaraCubeOrButton(class Z_attribute_list &attr_list, cla // presentValue = x + 128 = 180º flip to side x on top // presentValue = x + 256 = push/slide cube while side x is on top // presentValue = x + 512 = double tap while side x is on top - } else if (modelId.startsWith(F("lumi.remote"))) { // only for Aqara button + } else if (modelId.startsWith(F("lumi.remote")) || modelId.startsWith(F("lumi.sensor_switch"))) { // only for Aqara buttons WXKG11LM & WXKG12LM int32_t val = attr.getInt(); const __FlashStringHelper *aqara_click = F("click"); const __FlashStringHelper *aqara_action = F("action"); @@ -1474,8 +1575,17 @@ void ZCLFrame::syntheticAqaraCubeOrButton(class Z_attribute_list &attr_list, cla case 2: attr_list.addAttribute(aqara_click).setStr(PSTR("double")); break; + case 16: + attr_list.addAttribute(aqara_action).setStr(PSTR("hold")); + break; + case 17: + attr_list.addAttribute(aqara_action).setStr(PSTR("release")); + break; + case 18: + attr_list.addAttribute(aqara_action).setStr(PSTR("shake")); + break; case 255: - attr_list.addAttribute(aqara_click).setStr(PSTR("release")); + attr_list.addAttribute(aqara_action).setStr(PSTR("release")); break; default: attr_list.addAttribute(aqara_click).setUInt(val); @@ -1534,11 +1644,10 @@ void ZCLFrame::syntheticAqaraVibration(class Z_attribute_list &attr_list, class } /// Publish a message for `"Occupancy":0` when the timer expired -int32_t Z_OccupancyCallback(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { +void Z_OccupancyCallback(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { Z_attribute_list attr_list; attr_list.addAttribute(F(OCCUPANCY)).setUInt(0); zigbee_devices.jsonPublishNow(shortaddr, attr_list); - return 0; // Fix GCC 10.1 warning } // ====================================================================== @@ -1593,6 +1702,11 @@ void ZCLFrame::postProcessAttributes(uint16_t shortaddr, Z_attribute_list& attr_ case 0x00060000: case 0x00068000: device.setPower(attr.getBool()); break; case 0x00080000: device.dimmer = uval16; break; + case 0x02010000: device.temperature = fval * 10 + 0.5f; break; + case 0x02010008: device.th_setpoint = uval16; break; + case 0x02010012: device.temperature_target = fval * 10 + 0.5f; break; + case 0x02010007: device.th_setpoint = uval16; break; + case 0x02010011: device.temperature_target = fval * 10 + 0.5f; break; case 0x03000000: device.hue = changeUIntScale(uval16, 0, 254, 0, 360); break; case 0x03000001: device.sat = uval16; break; case 0x03000003: device.x = uval16; break; @@ -1600,6 +1714,7 @@ void ZCLFrame::postProcessAttributes(uint16_t shortaddr, Z_attribute_list& attr_ case 0x03000007: device.ct = uval16; break; case 0x03000008: device.colormode = uval16; break; case 0x04020000: device.temperature = fval * 10 + 0.5f; break; + case 0x0403FF00: device.pressure = fval + 0.5f; break; // give priority to sea level case 0x04030000: device.pressure = fval + 0.5f; break; case 0x04050000: device.humidity = fval + 0.5f; break; case 0x0B040505: device.mains_voltage = uval16; break; @@ -1616,4 +1731,78 @@ void ZCLFrame::postProcessAttributes(uint16_t shortaddr, Z_attribute_list& attr_ } } +// +// Given an attribute string, retrieve all attribute details. +// Input: the attribute has a key name, either: / or /% or "" +// Ex: "0008/0000", "0008/0000%20", "Dimmer" +// Use: +// Z_attribute attr; +// attr.setKeyName("0008/0000%20") +// if (Z_parseAttributeKey(attr)) { +// // ok +// } +// +// Output: +// The `attr` attribute has the correct cluster, attr_id, attr_type, attr_multiplier +// Note: the attribute value is unchanged and unparsed +// +// Note: if the type is specified in the key, the multiplier is not applied, no conversion happens +bool Z_parseAttributeKey(class Z_attribute & attr) { + // check if the name has the format "XXXX/YYYY" where XXXX is the cluster, YYYY the attribute id + // alternative "XXXX/YYYY%ZZ" where ZZ is the type (for unregistered attributes) + if (attr.key_is_str) { + const char * key = attr.key.key; + char * delimiter = strchr(key, '/'); + char * delimiter2 = strchr(key, '%'); + if (delimiter) { + uint16_t attr_id = 0xFFFF; + uint16_t cluster_id = 0xFFFF; + uint8_t type_id = Zunk; + + cluster_id = strtoul(key, &delimiter, 16); + if (!delimiter2) { + attr_id = strtoul(delimiter+1, nullptr, 16); + } else { + attr_id = strtoul(delimiter+1, &delimiter2, 16); + type_id = strtoul(delimiter2+1, nullptr, 16); + } + attr.setKeyId(cluster_id, attr_id); + attr.attr_type = type_id; + } + } + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("cluster_id = 0x%04X, attr_id = 0x%04X"), cluster_id, attr_id); + + // do we already know the type, i.e. attribute and cluster are also known + if (Zunk == attr.attr_type) { + // scan attributes to find by name, and retrieve type + for (uint32_t i = 0; i < ARRAY_SIZE(Z_PostProcess); i++) { + const Z_AttributeConverter *converter = &Z_PostProcess[i]; + bool match = false; + uint16_t local_attr_id = pgm_read_word(&converter->attribute); + uint16_t local_cluster_id = CxToCluster(pgm_read_byte(&converter->cluster_short)); + uint8_t local_type_id = pgm_read_byte(&converter->type); + int8_t local_multiplier = pgm_read_byte(&converter->multiplier); + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Try cluster = 0x%04X, attr = 0x%04X, type_id = 0x%02X"), local_cluster_id, local_attr_id, local_type_id); + + if (!attr.key_is_str) { + if ((attr.key.id.cluster == local_cluster_id) && (attr.key.id.attr_id == local_attr_id)) { + attr.attr_type = local_type_id; + break; + } + } else if (pgm_read_word(&converter->name_offset)) { + const char * key = attr.key.key; + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Comparing '%s' with '%s'"), attr_name, converter->name); + if (0 == strcasecmp_P(key, Z_strings + pgm_read_word(&converter->name_offset))) { + // match + attr.setKeyId(local_cluster_id, local_attr_id); + attr.attr_type = local_type_id; + attr.attr_multiplier = local_multiplier; + break; + } + } + } + } + return (Zunk != attr.attr_type) ? true : false; +} + #endif // USE_ZIGBEE diff --git a/tasmota/xdrv_23_zigbee_6_commands.ino b/tasmota/xdrv_23_zigbee_6_commands.ino index a32f637c4..947e2ffe0 100644 --- a/tasmota/xdrv_23_zigbee_6_commands.ino +++ b/tasmota/xdrv_23_zigbee_6_commands.ino @@ -148,7 +148,7 @@ const uint8_t CLUSTER_0009[] = { ZLE(0x0000) }; // AlarmCount const uint8_t CLUSTER_0300[] = { ZLE(0x0000), ZLE(0x0001), ZLE(0x0003), ZLE(0x0004), ZLE(0x0007), ZLE(0x0008) }; // Hue, Sat, X, Y, CT, ColorMode // This callback is registered after a cluster specific command and sends a read command for the same cluster -int32_t Z_ReadAttrCallback(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { +void Z_ReadAttrCallback(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { size_t attrs_len = 0; const uint8_t* attrs = nullptr; @@ -188,16 +188,14 @@ int32_t Z_ReadAttrCallback(uint16_t shortaddr, uint16_t groupaddr, uint16_t clus attrs, attrs_len })); } - return 0; // Fix GCC 10.1 warning } // This callback is registered after a an attribute read command was made to a light, and fires if we don't get any response after 1000 ms -int32_t Z_Unreachable(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { +void Z_Unreachable(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { if (BAD_SHORTADDR != shortaddr) { zigbee_devices.setReachable(shortaddr, false); // mark device as reachable } - return 0; // Fix GCC 10.1 warning } // returns true if char is 'x', 'y' or 'z' @@ -349,6 +347,10 @@ void convertClusterSpecific(class Z_attribute_list &attr_list, uint16_t cluster, if ((0 != xyz.z) && (0xFF != xyz.z)) { attr_list.addAttribute(command_name, PSTR("Zone")).setUInt(xyz.z); } + // for now convert alamrs 1 and 2 to Occupancy + // TODO we may only do this conversion to ZoneType == 0x000D 'Motion Sensor' + // Occupancy is 0406/0000 of type Zmap8 + attr_list.addAttribute(0x0406, 0x0000).setUInt((xyz.x) & 0x01 ? 1 : 0); break; case 0x00040000: case 0x00040001: diff --git a/tasmota/xdrv_23_zigbee_7_statemachine.ino b/tasmota/xdrv_23_zigbee_7_statemachine.ino index 5a6ab4e29..a529edd0e 100644 --- a/tasmota/xdrv_23_zigbee_7_statemachine.ino +++ b/tasmota/xdrv_23_zigbee_7_statemachine.ino @@ -28,6 +28,7 @@ const uint8_t ZIGBEE_STATUS_STARTING = 3; // Starting CC2530 as co const uint8_t ZIGBEE_STATUS_PERMITJOIN_CLOSE = 20; // Disable PermitJoin const uint8_t ZIGBEE_STATUS_PERMITJOIN_OPEN_60 = 21; // Enable PermitJoin for 60 seconds const uint8_t ZIGBEE_STATUS_PERMITJOIN_OPEN_XX = 22; // Enable PermitJoin until next boot +const uint8_t ZIGBEE_STATUS_PERMITJOIN_ERROR = 23; // Enable PermitJoin until next boot const uint8_t ZIGBEE_STATUS_DEVICE_ANNOUNCE = 30; // Device announces its address const uint8_t ZIGBEE_STATUS_NODE_DESC = 31; // Node descriptor const uint8_t ZIGBEE_STATUS_ACTIVE_EP = 32; // Endpoints descriptor @@ -648,7 +649,9 @@ ZBM(ZBS_SET_NETWORKS, EZSP_setConfigurationValue, 0x00 /*high*/, EZSP_CONFIG ZBM(ZBS_SET_PACKET_BUF, EZSP_setConfigurationValue, 0x00 /*high*/, EZSP_CONFIG_PACKET_BUFFER_COUNT, 0xFF, 0x00) // 530001FF00 ZBM(ZBR_SET_OK, EZSP_setConfigurationValue, 0x00 /*high*/, 0x00 /*ok*/) // 530000 -ZBM(ZBR_SET_OK2, 0x00, 0x00 /*high*/, 0x00 /*ok*/) // 000000 - TODO why does setting EZSP_CONFIG_PACKET_BUFFER_COUNT has a different response? +// There is a bug in v6.7 where the response if 000000 instead of 530000 +// If we detect the version to be v6.7, the first byte is changed to 00 +ZBR(ZBR_SET_OK2, EZSP_setConfigurationValue, 0x00 /*high*/, 0x00 /*ok*/) // Read some configuration values // ZBM(ZBS_GET_APS_UNI, EZSP_getConfigurationValue, 0x00 /*high*/, EZSP_CONFIG_APS_UNICAST_MESSAGE_COUNT) // 520003 @@ -681,7 +684,7 @@ ZBM(ZBS_SET_CONCENTRATOR, EZSP_setConcentrator, 0x00 /*high*/, 0x00 /*false*/, 0 ZBM(ZBR_SET_CONCENTRATOR, EZSP_setConcentrator, 0x00 /*high*/, 0x00 /*ok*/) // 100000 // setInitialSecurityState -#define EZ_SECURITY_MODE EMBER_TRUST_CENTER_GLOBAL_LINK_KEY | EMBER_PRECONFIGURED_NETWORK_KEY_MODE | EMBER_HAVE_NETWORK_KEY | EMBER_HAVE_PRECONFIGURED_KEY | EMBER_NO_FRAME_COUNTER_RESET +#define EZ_SECURITY_MODE EMBER_TRUST_CENTER_GLOBAL_LINK_KEY | EMBER_PRECONFIGURED_NETWORK_KEY_MODE | EMBER_HAVE_NETWORK_KEY | EMBER_HAVE_PRECONFIGURED_KEY ZBR(ZBS_SET_SECURITY, EZSP_setInitialSecurityState, 0x00 /*high*/, Z_B0(EZ_SECURITY_MODE), Z_B1(EZ_SECURITY_MODE), // preConfiguredKey diff --git a/tasmota/xdrv_23_zigbee_8_parsers.ino b/tasmota/xdrv_23_zigbee_8_parsers.ino index d800eaf71..2ce98cd01 100644 --- a/tasmota/xdrv_23_zigbee_8_parsers.ino +++ b/tasmota/xdrv_23_zigbee_8_parsers.ino @@ -166,21 +166,22 @@ int32_t EZ_RouteError(int32_t res, const class SBuffer &buf) { int32_t EZ_PermitJoinRsp(int32_t res, const class SBuffer &buf) { uint8_t status = buf.get8(2); - Response_P(PSTR("{\"" D_JSON_ZIGBEE_STATE "\":{" - "\"Status\":%d,\"Message\":\"%s"), - (0 == status) ? ZIGBEE_STATUS_PERMITJOIN_OPEN_60 : ZIGBEE_STATUS_PERMITJOIN_CLOSE, - (0 == status) ? PSTR("Pairing mode enabled") : PSTR("Pairing mode error") - ); - if (status) { - ResponseAppend_P("0x%02X", status); + if (status) { // only report if there is an error + Response_P(PSTR("{\"" D_JSON_ZIGBEE_STATE "\":{\"Status\":23,\"Message\":\"Pairing mode error 0x%02X\"}}"), status); + MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEE_STATE)); } - ResponseAppend_P(PSTR("\"}}")); - - MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEE_STATE)); - return -1; } +// +// Special case: EZSP does not send an event for PermitJoin end, so we generate a synthetic one +// +void Z_PermitJoinDisable(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { + Response_P(PSTR("{\"" D_JSON_ZIGBEE_STATE "\":{\"Status\":20,\"Message\":\"Pairing mode disabled\"}}")); + MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEE_STATE)); +} + + // // Received MessageSentHandler // @@ -395,6 +396,10 @@ int32_t EZ_ReceiveCheckVersion(int32_t res, class SBuffer &buf) { MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEE_STATE)); if (0x08 == protocol_version) { + if ((stack_version & 0xFF00) == 0x6700) { + // If v6.7 there is a bug so we need to change the response + ZBW(ZBR_SET_OK2, 0x00, 0x00 /*high*/, 0x00 /*ok*/) + } return 0; // protocol v8 is ok } else { return ZIGBEE_LABEL_UNSUPPORTED_VERSION; // abort @@ -533,7 +538,11 @@ int32_t Z_ReceiveActiveEp(int32_t res, const class SBuffer &buf) { #endif for (uint32_t i = 0; i < activeEpCount; i++) { - zigbee_devices.addEndpoint(nwkAddr, activeEpList[i]); + uint8_t ep = activeEpList[i]; + zigbee_devices.addEndpoint(nwkAddr, ep); + if ((i < 4) && (ep < 0x10)) { + zigbee_devices.queueTimer(nwkAddr, 0 /* groupaddr */, 1500, ep /* fake cluster as ep */, ep, Z_CAT_EP_DESC, 0 /* value */, &Z_SendSimpleDescReq); + } } Response_P(PSTR("{\"" D_JSON_ZIGBEE_STATE "\":{" @@ -546,7 +555,132 @@ int32_t Z_ReceiveActiveEp(int32_t res, const class SBuffer &buf) { ResponseAppend_P(PSTR("]}}")); MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEEZCL_RECEIVED)); - Z_SendAFInfoRequest(nwkAddr); // probe for ModelId and ManufId + Z_SendDeviceInfoRequest(nwkAddr); // probe for ModelId and ManufId + + return -1; +} + +// list of clusters that need bindings +const uint8_t Z_bindings[] PROGMEM = { + Cx0001, Cx0006, Cx0008, Cx0300, + Cx0400, Cx0402, Cx0403, Cx0405, Cx0406, + Cx0500, +}; + +int32_t Z_ClusterToCxBinding(uint16_t cluster) { + uint8_t cx = ClusterToCx(cluster); + for (uint32_t i=0; i= 0) { + bitSet(cluster_map, found_cx); + bitSet(cluster_in_map, found_cx); + } + } + // scan out clusters + for (uint32_t i=0; i= 0) { + bitSet(cluster_map, found_cx); + } + } + + // if IAS device, request the device type + if (bitRead(cluster_map, Z_ClusterToCxBinding(0x0500))) { + // send a read command to cluster 0x0500, attribute 0x0001 (ZoneType) - to read the type of sensor + zigbee_devices.queueTimer(shortaddr, 0 /* groupaddr */, 2000, 0x0500, endpoint, Z_CAT_READ_ATTRIBUTE, 0x0001, &Z_SendSingleAttributeRead); + } + + // enqueue bind requests + for (uint32_t i=0; i 0) { ResponseAppend_P(PSTR(",")); } + ResponseAppend_P(PSTR("\"0x%04X\""), buf.get16(numInIndex + i*2)); + } + ResponseAppend_P(PSTR("],\"OutClusters\":[")); + for (uint32_t i = 0; i < numOutCluster; i++) { + if (i > 0) { ResponseAppend_P(PSTR(",")); } + ResponseAppend_P(PSTR("\"0x%04X\""), buf.get16(numOutIndex + i*2)); + } + ResponseAppend_P(PSTR("]}}")); + MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEEZCL_RECEIVED)); + XdrvRulesProcess(); + } return -1; } @@ -831,7 +965,7 @@ int32_t Z_MgmtBindRsp(int32_t res, const class SBuffer &buf) { //uint64_t srcaddr = buf.get16(idx); // unused uint8_t srcep = buf.get8(idx + 8); - uint8_t cluster = buf.get16(idx + 9); + uint16_t cluster = buf.get16(idx + 9); uint8_t addrmode = buf.get8(idx + 11); uint16_t group = 0x0000; uint64_t dstaddr = 0; @@ -960,9 +1094,31 @@ void Z_SendActiveEpReq(uint16_t shortaddr) { } // -// Send AF Info Request +// Probe the clusters_out on the first endpoint // -void Z_SendAFInfoRequest(uint16_t shortaddr) { +// Send ZDO_SIMPLE_DESC_REQ to get full list of supported Clusters for a specific endpoint +void Z_SendSimpleDescReq(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { +#ifdef USE_ZIGBEE_ZNP + uint8_t SimpleDescReq[] = { Z_SREQ | Z_ZDO, ZDO_SIMPLE_DESC_REQ, // 2504 + Z_B0(shortaddr), Z_B1(shortaddr), Z_B0(shortaddr), Z_B1(shortaddr), + endpoint }; + ZigbeeZNPSend(SimpleDescReq, sizeof(SimpleDescReq)); +#endif +#ifdef USE_ZIGBEE_EZSP + uint8_t SimpleDescReq[] = { Z_B0(shortaddr), Z_B1(shortaddr), endpoint }; + EZ_SendZDO(shortaddr, ZDO_SIMPLE_DESC_REQ, SimpleDescReq, sizeof(SimpleDescReq)); +#endif +} + + +// +// Send AF Info Request +// Queue requests for the device +// 1. Request for 'ModelId' and 'Manufacturer': 0000/0005, 0000/0006 +// 2. Auto-bind to coordinator: +// Iterate among +// +void Z_SendDeviceInfoRequest(uint16_t shortaddr) { uint8_t endpoint = zigbee_devices.findFirstEndpoint(shortaddr); if (0x00 == endpoint) { endpoint = 0x01; } // if we don't know the endpoint, try 0x01 uint8_t transacid = zigbee_devices.getNextSeqNumber(shortaddr); @@ -983,6 +1139,170 @@ void Z_SendAFInfoRequest(uint16_t shortaddr) { })); } +// +// Send sing attribute read request in Timer +// +void Z_SendSingleAttributeRead(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { + uint8_t transacid = zigbee_devices.getNextSeqNumber(shortaddr); + uint8_t InfoReq[2] = { Z_B0(value), Z_B1(value) }; // list of single attribute + + ZigbeeZCLSend_Raw(ZigbeeZCLSendMessage({ + shortaddr, + 0x0000, /* group */ + cluster /*cluster*/, + endpoint, + ZCL_READ_ATTRIBUTES, + 0x0000, /* manuf */ + false /* not cluster specific */, + true /* response */, + transacid, /* zcl transaction id */ + InfoReq, sizeof(InfoReq) + })); +} + +// +// Auto-bind some clusters to the coordinator's endpoint 0x01 +// +void Z_AutoBind(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { + uint64_t srcLongAddr = zigbee_devices.getDeviceLongAddr(shortaddr); + + AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "auto-bind `ZbBind {\"Device\":\"0x%04X\",\"Endpoint\":%d,\"Cluster\":\"0x%04X\"}`"), + shortaddr, endpoint, cluster); +#ifdef USE_ZIGBEE_ZNP + SBuffer buf(34); + buf.add8(Z_SREQ | Z_ZDO); + buf.add8(ZDO_BIND_REQ); + buf.add16(shortaddr); + buf.add64(srcLongAddr); + buf.add8(endpoint); + buf.add16(cluster); + buf.add8(Z_Addr_IEEEAddress); // DstAddrMode - 0x03 = ADDRESS_64_BIT + buf.add64(localIEEEAddr); + buf.add8(0x01); // toEndpoint + + ZigbeeZNPSend(buf.getBuffer(), buf.len()); +#endif // USE_ZIGBEE_ZNP + +#ifdef USE_ZIGBEE_EZSP + SBuffer buf(24); + + // ZDO message payload (see Zigbee spec 2.4.3.2.2) + buf.add64(srcLongAddr); + buf.add8(endpoint); + buf.add16(cluster); + buf.add8(Z_Addr_IEEEAddress); // DstAddrMode - 0x03 = ADDRESS_64_BIT + buf.add64(localIEEEAddr); + buf.add8(0x01); // toEndpoint + + EZ_SendZDO(shortaddr, ZDO_BIND_REQ, buf.buf(), buf.len()); +#endif // USE_ZIGBEE_EZSP +} + +// +// Auto-bind some clusters to the coordinator's endpoint 0x01 +// + +// the structure below indicates which attributes need to be configured for attribute reporting +typedef struct Z_autoAttributeReporting_t { + uint16_t cluster; + uint16_t attr_id; + uint16_t min_interval; // minimum interval in seconds (consecutive reports won't happen before this value) + uint16_t max_interval; // maximum interval in seconds (attribut will always be reported after this interval) + float report_change; // for non discrete attributes, the value change that triggers a report +} Z_autoAttributeReporting_t; + +// Note the attribute must be registered in the converter list, used to retrieve the type of the attribute +const Z_autoAttributeReporting_t Z_autoAttributeReporting[] PROGMEM = { + { 0x0001, 0x0020, 15*60, 15*60, 0.1 }, // BatteryVoltage + { 0x0001, 0x0021, 15*60, 15*60, 1 }, // BatteryPercentage + { 0x0006, 0x0000, 1, 60*60, 0 }, // Power + { 0x0008, 0x0000, 1, 60*60, 5 }, // Dimmer + { 0x0300, 0x0000, 1, 60*60, 5 }, // Hue + { 0x0300, 0x0001, 1, 60*60, 5 }, // Sat + { 0x0300, 0x0003, 1, 60*60, 100 }, // X + { 0x0300, 0x0004, 1, 60*60, 100 }, // Y + { 0x0300, 0x0007, 1, 60*60, 5 }, // CT + { 0x0300, 0x0008, 1, 60*60, 0 }, // ColorMode + { 0x0400, 0x0000, 10, 60*60, 5 }, // Illuminance (5 lux) + { 0x0402, 0x0000, 30, 60*60, 0.2 }, // Temperature (0.2 °C) + { 0x0403, 0x0000, 30, 60*60, 1 }, // Pressure (1 hPa) + { 0x0405, 0x0000, 30, 60*60, 1.0 }, // Humidity (1 %) + { 0x0406, 0x0000, 10, 60*60, 0 }, // Occupancy + { 0x0500, 0x0002, 1, 60*60, 0 }, // ZoneStatus +}; + +// +// Called by Device Auto-config +// Configures default values for the most common Attribute Rerporting configurations +// +// Note: must be of type `Z_DeviceTimer` +void Z_AutoConfigReportingForCluster(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { + // Buffer, max 12 bytes per attribute + SBuffer buf(12*6); + + + Response_P(PSTR("ZbSend {\"Device\":\"0x%04X\",\"Config\":{"), shortaddr); + + boolean comma = false; + for (uint32_t i=0; i 0) { + AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "auto-bind `%s`"), mqtt_data); + ZigbeeZCLSend_Raw(ZigbeeZCLSendMessage({ + shortaddr, + 0x0000, /* group */ + cluster /*cluster*/, + endpoint, + ZCL_CONFIGURE_REPORTING, + 0x0000, /* manuf */ + false /* not cluster specific */, + false /* no response */, + zigbee_devices.getNextSeqNumber(shortaddr), /* zcl transaction id */ + buf.buf(), buf.len() + })); + } +} // // Handle trustCenterJoinHandler @@ -1033,7 +1353,8 @@ void Z_IncomingMessage(class ZCLFrame &zcl_received) { // log the packet details zcl_received.log(); - zigbee_devices.setLQI(srcaddr, linkquality); // EFR32 has a different scale for LQI + zigbee_devices.setLQI(srcaddr, linkquality != 0xFF ? linkquality : 0xFE); // EFR32 has a different scale for LQI + zigbee_devices.setLastSeenNow(srcaddr); char shortaddr[8]; snprintf_P(shortaddr, sizeof(shortaddr), PSTR("0x%04X"), srcaddr); @@ -1075,6 +1396,7 @@ void Z_IncomingMessage(class ZCLFrame &zcl_received) { } zcl_received.generateSyntheticAttributes(attr_list); + zcl_received.computeSyntheticAttributes(attr_list); zcl_received.generateCallBacks(attr_list); // set deferred callbacks, ex: Occupancy zcl_received.postProcessAttributes(srcaddr, attr_list); @@ -1177,6 +1499,7 @@ int32_t EZ_IncomingMessage(int32_t res, const class SBuffer &buf) { // ZDO request // Report LQI zigbee_devices.setLQI(srcaddr, linkquality); + zigbee_devices.setLastSeenNow(srcaddr); // Since ZDO messages start with a sequence number, we skip it // but we add the source address in the last 2 bytes SBuffer zdo_buf(buf.get8(20) - 1 + 2); @@ -1189,6 +1512,8 @@ int32_t EZ_IncomingMessage(int32_t res, const class SBuffer &buf) { return Z_ReceiveActiveEp(res, zdo_buf); case ZDO_IEEE_addr_rsp: return Z_ReceiveIEEEAddr(res, zdo_buf); + case ZDO_Simple_Desc_rsp: + return Z_ReceiveSimpleDesc(res, zdo_buf); case ZDO_Bind_rsp: return Z_BindRsp(res, zdo_buf); case ZDO_Unbind_rsp: @@ -1279,9 +1604,8 @@ int32_t EZ_Recv_Default(int32_t res, const class SBuffer &buf) { \*********************************************************************************************/ // Publish the received values once they have been coalesced -int32_t Z_PublishAttributes(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { +void Z_PublishAttributes(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) { zigbee_devices.jsonPublishFlush(shortaddr); - return 1; } /*********************************************************************************************\ @@ -1363,6 +1687,7 @@ const Z_Dispatcher Z_DispatchTable[] PROGMEM = { { { Z_AREQ | Z_ZDO, ZDO_PERMIT_JOIN_IND }, &ZNP_ReceivePermitJoinStatus }, // 45CB { { Z_AREQ | Z_ZDO, ZDO_NODE_DESC_RSP }, &ZNP_ReceiveNodeDesc }, // 4582 { { Z_AREQ | Z_ZDO, ZDO_ACTIVE_EP_RSP }, &Z_ReceiveActiveEp }, // 4585 + { { Z_AREQ | Z_ZDO, ZDO_SIMPLE_DESC_RSP}, &Z_ReceiveSimpleDesc}, // 4584 { { Z_AREQ | Z_ZDO, ZDO_IEEE_ADDR_RSP }, &Z_ReceiveIEEEAddr }, // 4581 { { Z_AREQ | Z_ZDO, ZDO_BIND_RSP }, &Z_BindRsp }, // 45A1 { { Z_AREQ | Z_ZDO, ZDO_UNBIND_RSP }, &Z_UnbindRsp }, // 45A2 @@ -1413,11 +1738,11 @@ void Z_Query_Bulb(uint16_t shortaddr, uint32_t &wait_ms) { uint8_t endpoint = zigbee_devices.findFirstEndpoint(shortaddr); if (endpoint) { // send only if we know the endpoint - zigbee_devices.setTimer(shortaddr, 0 /* groupaddr */, wait_ms, 0x0006, endpoint, Z_CAT_NONE, 0 /* value */, &Z_ReadAttrCallback); + zigbee_devices.setTimer(shortaddr, 0 /* groupaddr */, wait_ms, 0x0006, endpoint, Z_CAT_READ_CLUSTER, 0 /* value */, &Z_ReadAttrCallback); wait_ms += inter_message_ms; - zigbee_devices.setTimer(shortaddr, 0 /* groupaddr */, wait_ms, 0x0008, endpoint, Z_CAT_NONE, 0 /* value */, &Z_ReadAttrCallback); + zigbee_devices.setTimer(shortaddr, 0 /* groupaddr */, wait_ms, 0x0008, endpoint, Z_CAT_READ_CLUSTER, 0 /* value */, &Z_ReadAttrCallback); wait_ms += inter_message_ms; - zigbee_devices.setTimer(shortaddr, 0 /* groupaddr */, wait_ms, 0x0300, endpoint, Z_CAT_NONE, 0 /* value */, &Z_ReadAttrCallback); + zigbee_devices.setTimer(shortaddr, 0 /* groupaddr */, wait_ms, 0x0300, endpoint, Z_CAT_READ_CLUSTER, 0 /* value */, &Z_ReadAttrCallback); wait_ms += inter_message_ms; zigbee_devices.setTimer(shortaddr, 0, wait_ms + Z_CAT_REACHABILITY_TIMEOUT, 0, endpoint, Z_CAT_REACHABILITY, 0 /* value */, &Z_Unreachable); wait_ms += 1000; // wait 1 second between devices @@ -1451,20 +1776,21 @@ int32_t Z_State_Ready(uint8_t value) { // // Mostly used for routers/end-devices // json: holds the attributes in JSON format -void Z_AutoResponder(uint16_t srcaddr, uint16_t cluster, uint8_t endpoint, const uint16_t *attr_list, size_t attr_len) { - DynamicJsonBuffer jsonBuffer; - JsonObject& json_out = jsonBuffer.createObject(); +void Z_AutoResponder(uint16_t srcaddr, uint16_t cluster, uint8_t endpoint, const uint16_t *attr_list_ids, size_t attr_len) { + Z_attribute_list attr_list; for (uint32_t i=0; i 0xFEFF) ? uxy[i] : 0xFEFF; } - if (0x0000 == attr) { json_out[F("Hue")] = changeUIntScale(hue, 0, 360, 0, 254); } - if (0x0001 == attr) { json_out[F("Sat")] = changeUIntScale(sat, 0, 255, 0, 254); } - if (0x0003 == attr) { json_out[F("X")] = uxy[0]; } - if (0x0004 == attr) { json_out[F("Y")] = uxy[1]; } - if (0x0007 == attr) { json_out[F("CT")] = LightGetColorTemp(); } + if (0x0000 == attr_id) { attr.setUInt(changeUIntScale(hue, 0, 360, 0, 254)); } + if (0x0001 == attr_id) { attr.setUInt(changeUIntScale(sat, 0, 255, 0, 254)); } + if (0x0003 == attr_id) { attr.setUInt(uxy[0]); } + if (0x0004 == attr_id) { attr.setUInt(uxy[1]); } + if (0x0007 == attr_id) { attr.setUInt(LightGetColorTemp()); } } break; #endif case 0x000A0000: // Time - json_out[F("Time")] = (Rtc.utc_time > (60 * 60 * 24 * 365 * 10)) ? Rtc.utc_time - 946684800 : Rtc.utc_time; + attr.setUInt((Rtc.utc_time > (60 * 60 * 24 * 365 * 10)) ? Rtc.utc_time - 946684800 : Rtc.utc_time); break; case 0x000AFF00: // TimeEpoch - Tasmota specific - json_out[F("TimeEpoch")] = Rtc.utc_time; + attr.setUInt(Rtc.utc_time); break; case 0x000A0001: // TimeStatus - json_out[F("TimeStatus")] = (Rtc.utc_time > (60 * 60 * 24 * 365 * 10)) ? 0x02 : 0x00; // if time is beyond 2010 then we are synchronized + attr.setUInt((Rtc.utc_time > (60 * 60 * 24 * 365 * 10)) ? 0x02 : 0x00); // if time is beyond 2010 then we are synchronized break; case 0x000A0002: // TimeZone - json_out[F("TimeZone")] = Settings.toffset[0] * 60; + attr.setUInt(Settings.toffset[0] * 60); break; case 0x000A0007: // LocalTime // TODO take DST - json_out[F("LocalTime")] = Settings.toffset[0] * 60 + ((Rtc.utc_time > (60 * 60 * 24 * 365 * 10)) ? Rtc.utc_time - 946684800 : Rtc.utc_time); + attr.setUInt(Settings.toffset[0] * 60 + ((Rtc.utc_time > (60 * 60 * 24 * 365 * 10)) ? Rtc.utc_time - 946684800 : Rtc.utc_time)); break; } + if (!attr.isNone()) { + Z_parseAttributeKey(attr); + attr_list.addAttribute(cluster, attr_id) = attr; + } } - if (json_out.size() > 0) { + SBuffer buf(200); + for (const auto & attr : attr_list) { + if (!ZbAppendWriteBuf(buf, attr, true)) { // true = need status indicator in Read Attribute Responses + return; // error + } + } + + if (buf.len() > 0) { // we have a non-empty output // log first - String msg(""); - msg.reserve(100); - json_out.printTo(msg); AddLog_P2(LOG_LEVEL_INFO, PSTR("ZIG: Auto-responder: ZbSend {\"Device\":\"0x%04X\"" ",\"Cluster\":\"0x%04X\"" ",\"Endpoint\":%d" ",\"Response\":%s}" ), srcaddr, cluster, endpoint, - msg.c_str()); + attr_list.toString().c_str()); // send - const JsonVariant &json_out_v = json_out; - ZbSendReportWrite(json_out_v, srcaddr, 0 /* group */,cluster, endpoint, 0 /* manuf */, ZCL_READ_ATTRIBUTES_RESPONSE); + // all good, send the packet + uint8_t seq = zigbee_devices.getNextSeqNumber(srcaddr); + ZigbeeZCLSend_Raw(ZigbeeZCLSendMessage({ + srcaddr, + 0x0000, + cluster /*cluster*/, + endpoint, + ZCL_READ_ATTRIBUTES_RESPONSE, + 0x0000, /* manuf */ + false /* not cluster specific */, + false /* no response */, + seq, /* zcl transaction id */ + buf.getBuffer(), buf.len() + })); } } diff --git a/tasmota/xdrv_23_zigbee_9_serial.ino b/tasmota/xdrv_23_zigbee_9_serial.ino index f5056c58c..c14c7f248 100644 --- a/tasmota/xdrv_23_zigbee_9_serial.ino +++ b/tasmota/xdrv_23_zigbee_9_serial.ino @@ -595,9 +595,12 @@ int32_t ZigbeeProcessInputEZSP(class SBuffer &buf) { case EZSP_getNetworkParameters: // 2800 case EZSP_sendUnicast: // 3400 case EZSP_sendBroadcast: // 3600 + case EZSP_sendMulticast: // 3800 case EZSP_messageSentHandler: // 3F00 + case EZSP_incomingMessageHandler: // 4500 case EZSP_setConfigurationValue: // 5300 case EZSP_setPolicy: // 5500 + case 0x0059: // 5900 - supposedly removed by still happening case EZSP_setMulticastTableEntry: // 6400 case EZSP_setInitialSecurityState: // 6800 case EZSP_getCurrentSecurityState: // 6900 diff --git a/tasmota/xdrv_23_zigbee_9a_upload.ino b/tasmota/xdrv_23_zigbee_9a_upload.ino index fc7defb15..e3c233152 100644 --- a/tasmota/xdrv_23_zigbee_9a_upload.ino +++ b/tasmota/xdrv_23_zigbee_9a_upload.ino @@ -99,6 +99,8 @@ char ZigbeeUploadFlashRead(void) { * XModem protocol \*********************************************************************************************/ +// Number of milliseconds to wait before prompt is received +const uint32_t XMODEM_FLUSH_DELAY = 1000; // Number of seconds until giving up hope of receiving sync packets from host. const uint8_t XMODEM_SYNC_TIMEOUT = 30; // Number of times we try to send a packet to the host until we give up sending.. @@ -109,6 +111,7 @@ const uint8_t XMODEM_PACKET_SIZE = 128; struct XMODEM { uint32_t timeout = 0; uint32_t delay = 0; + uint32_t flush_delay = 0xFFFFFFFF; uint32_t filepos = 0; int crcBuf = 0; uint8_t packetNo = 1; @@ -215,21 +218,43 @@ bool ZigbeeUploadBootloaderPrompt(void) { // Scripts that interact with the bootloader should use only the “BL >” prompt to determine // when the bootloader is ready for input. While current menu options should remain functionally // unchanged, the menu title and options text is liable to change, and new options might be added. + + uint8_t serial_buffer[255]; + uint32_t buf_len = 0; + while (ZigbeeSerial->available()) { yield(); char bootloader_byte = ZigbeeSerial->read(); - switch (ZbUpload.byte_counter) { - case 0: - if ('B' == bootloader_byte) { ZbUpload.byte_counter++; } break; - case 1: - if ('L' == bootloader_byte) { ZbUpload.byte_counter++; } break; - case 2: - if (' ' == bootloader_byte) { ZbUpload.byte_counter++; } break; - case 3: - if ('>' == bootloader_byte) { ZbUpload.byte_counter++; } + + if (((uint8_t)bootloader_byte >=0) && (buf_len < sizeof(serial_buffer) -2)) { + serial_buffer[buf_len++] = bootloader_byte; + } + + if (ZbUpload.byte_counter != 4) { + switch (ZbUpload.byte_counter) { + case 0: + if ('B' == bootloader_byte) { ZbUpload.byte_counter++; } break; + case 1: + if ('L' == bootloader_byte) { ZbUpload.byte_counter++; } break; + case 2: + if (' ' == bootloader_byte) { ZbUpload.byte_counter++; } break; + case 3: + if ('>' == bootloader_byte) { + ZbUpload.byte_counter++; + XModem.flush_delay = millis() + XMODEM_FLUSH_DELAY; + XModem.delay = XModem.flush_delay + XMODEM_FLUSH_DELAY; + } + } } } - return (4 == ZbUpload.byte_counter); + + if (buf_len) { + char hex_char[256]; + ToHex_P(serial_buffer, buf_len, hex_char, 256); + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("XMD: Rcvd %s"), hex_char); + } + + return ((4 == ZbUpload.byte_counter) && (millis() > XModem.flush_delay)); } bool ZigbeeUploadXmodem(void) { @@ -261,7 +286,7 @@ bool ZigbeeUploadXmodem(void) { if (EZSP_Serial.to_send == EZSP_Serial.to_end) { ZbUpload.bootloader = ZBU_SOFTWARE_RESET; XModem.timeout = millis() + (10 * 1000); // Allow 10 seconds to receive EBL prompt - XModem.delay = millis() + 500; + XModem.delay = millis() + (2 * XMODEM_FLUSH_DELAY); ZbUpload.byte_counter = 0; ZbUpload.ota_step = ZBU_PROMPT; } @@ -271,7 +296,7 @@ bool ZigbeeUploadXmodem(void) { ZbUpload.bootloader = ZBU_HARDWARE_RESET; ZigbeeUploadSetBootloader(0); // Reboot MCU EFR32 which returns below text XModem.timeout = millis() + (30 * 1000); // Allow 30 seconds to receive EBL prompt - XModem.delay = millis() + 500; + XModem.delay = millis() + (2 * XMODEM_FLUSH_DELAY); ZbUpload.byte_counter = 0; ZbUpload.ota_step = ZBU_PROMPT; break; @@ -292,7 +317,7 @@ bool ZigbeeUploadXmodem(void) { AddLog_P2(LOG_LEVEL_DEBUG, PSTR("XMD: Init bootloader")); ZigbeeUploadSetBootloader(0); // Reboot MCU EFR32 which returns below text XModem.timeout = millis() + (30 * 1000); // Allow 30 seconds to receive EBL prompt - XModem.delay = millis() + 500; + XModem.delay = millis() + (2 * XMODEM_FLUSH_DELAY); ZbUpload.byte_counter = 0; ZbUpload.ota_step = ZBU_PROMPT; break; @@ -304,12 +329,12 @@ bool ZigbeeUploadXmodem(void) { return true; } #endif // ZIGBEE_BOOTLOADER_SOFTWARE_RESET_FIRST - else if (!ZigbeeSerial->available()) { + else if (!ZigbeeSerial->available() && (millis() < XModem.flush_delay)) { // The target device’s bootloader sends output over its serial port after it receives a // carriage return from the source device if (millis() > XModem.delay) { ZigbeeSerial->write(XM_CR); - XModem.delay = millis() + 500; + XModem.delay = millis() + (2 * XMODEM_FLUSH_DELAY); } } else { // After the bootloader receives a carriage return from the target device, it displays a menu diff --git a/tasmota/xdrv_23_zigbee_A_impl.ino b/tasmota/xdrv_23_zigbee_A_impl.ino index fff151af6..7bacb43d9 100644 --- a/tasmota/xdrv_23_zigbee_A_impl.ino +++ b/tasmota/xdrv_23_zigbee_A_impl.ino @@ -97,19 +97,6 @@ void ZigbeeInit(void) * Commands \*********************************************************************************************/ -uint32_t strToUInt(const JsonVariant &val) { - // if the string starts with 0x, it is considered Hex, otherwise it is an int - if (val.is()) { - return val.as(); - } else { - if (val.is()) { - String sval = val.as(); - return strtoull(sval.c_str(), nullptr, 0); - } - } - return 0; // couldn't parse anything -} - #ifdef USE_ZIGBEE_ZNP // Do a factory reset of the CC2530 const unsigned char ZIGBEE_FACTORY_RESET[] PROGMEM = @@ -212,9 +199,37 @@ void ZbApplyMultiplier(double &val_d, int8_t multiplier) { } } +// +// Send Attribute Write, apply mutlipliers before +// +bool ZbAppendWriteBuf(SBuffer & buf, const Z_attribute & attr, bool prepend_status_ok) { + double val_d = attr.getFloat(); + const char * val_str = attr.getStr(); + + if (attr.key_is_str) { return false; } + if (attr.isNum() && (1 != attr.attr_multiplier)) { + ZbApplyMultiplier(val_d, attr.attr_multiplier); + } + + // push the value in the buffer + buf.add16(attr.key.id.attr_id); // prepend with attribute identifier + if (prepend_status_ok) { + buf.add8(Z_SUCCESS); // status OK = 0x00 + } + buf.add8(attr.attr_type); // prepend with attribute type + int32_t res = encodeSingleAttribute(buf, val_d, val_str, attr.attr_type); + if (res < 0) { + // remove the attribute type we just added + // buf.setLen(buf.len() - (operation == ZCL_READ_ATTRIBUTES_RESPONSE ? 4 : 3)); + AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Unsupported attribute type %04X/%04X '0x%02X'"), attr.key.id.cluster, attr.key.id.attr_id, attr.attr_type); + return false; + } + return true; +} + // Parse "Report", "Write", "Response" or "Condig" attribute // Operation is one of: ZCL_REPORT_ATTRIBUTES (0x0A), ZCL_WRITE_ATTRIBUTES (0x02) or ZCL_READ_ATTRIBUTES_RESPONSE (0x01) -void ZbSendReportWrite(const JsonObject &val_pubwrite, uint16_t device, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint16_t manuf, uint8_t operation) { +void ZbSendReportWrite(class JsonParserToken val_pubwrite, uint16_t device, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint16_t manuf, uint8_t operation) { SBuffer buf(200); // buffer to store the binary output of attibutes if (nullptr == XdrvMailbox.command) { @@ -222,158 +237,89 @@ void ZbSendReportWrite(const JsonObject &val_pubwrite, uint16_t device, uint16_t } // iterate on keys - for (JsonObject::const_iterator it=val_pubwrite.begin(); it!=val_pubwrite.end(); ++it) { - const char *key = it->key; - const JsonVariant &value = it->value; + for (auto key : val_pubwrite.getObject()) { + JsonParserToken value = key.getValue(); - uint16_t attr_id = 0xFFFF; - uint16_t cluster_id = 0xFFFF; - uint8_t type_id = Znodata; - int8_t multiplier = 1; // multiplier to adjust the key value - double val_d = 0; // I try to avoid `double` but this type capture both float and (u)int32_t without prevision loss - const char* val_str = ""; // variant as string - - // check if the name has the format "XXXX/YYYY" where XXXX is the cluster, YYYY the attribute id - // alternative "XXXX/YYYY%ZZ" where ZZ is the type (for unregistered attributes) - char * delimiter = strchr(key, '/'); - char * delimiter2 = strchr(key, '%'); - if (delimiter) { - cluster_id = strtoul(key, &delimiter, 16); - if (!delimiter2) { - attr_id = strtoul(delimiter+1, nullptr, 16); - } else { - attr_id = strtoul(delimiter+1, &delimiter2, 16); - type_id = strtoul(delimiter2+1, nullptr, 16); - } - } - // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("cluster_id = 0x%04X, attr_id = 0x%04X"), cluster_id, attr_id); - - // do we already know the type, i.e. attribute and cluster are also known - if (Znodata == type_id) { - // scan attributes to find by name, and retrieve type - for (uint32_t i = 0; i < ARRAY_SIZE(Z_PostProcess); i++) { - const Z_AttributeConverter *converter = &Z_PostProcess[i]; - bool match = false; - uint16_t local_attr_id = pgm_read_word(&converter->attribute); - uint16_t local_cluster_id = CxToCluster(pgm_read_byte(&converter->cluster_short)); - uint8_t local_type_id = pgm_read_byte(&converter->type); - int8_t local_multiplier = pgm_read_byte(&converter->multiplier); - // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Try cluster = 0x%04X, attr = 0x%04X, type_id = 0x%02X"), local_cluster_id, local_attr_id, local_type_id); - - if (delimiter) { - if ((cluster_id == local_cluster_id) && (attr_id == local_attr_id)) { - type_id = local_type_id; - break; - } - } else if (pgm_read_word(&converter->name_offset)) { - // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Comparing '%s' with '%s'"), attr_name, converter->name); - if (0 == strcasecmp_P(key, Z_strings + pgm_read_word(&converter->name_offset))) { - // match - cluster_id = local_cluster_id; - attr_id = local_attr_id; - type_id = local_type_id; - multiplier = local_multiplier; - break; - } - } - } - } - - // Buffer ready, do some sanity checks - // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("cluster_id = 0x%04X, attr_id = 0x%04X, type_id = 0x%02X"), cluster_id, attr_id, type_id); - if ((0xFFFF == attr_id) || (0xFFFF == cluster_id)) { - Response_P(PSTR("{\"%s\":\"%s'%s'\"}"), XdrvMailbox.command, PSTR("Unknown attribute "), key); - return; - } - if (Znodata == type_id) { - Response_P(PSTR("{\"%s\":\"%s'%s'\"}"), XdrvMailbox.command, PSTR("Unknown attribute type for attribute "), key); - return; - } - - if (0xFFFF == cluster) { - cluster = cluster_id; // set the cluster for this packet - } else if (cluster != cluster_id) { - ResponseCmndChar_P(PSTR("No more than one cluster id per command")); - return; - } - - // //////////////////////////////////////////////////////////////////////////////// - // Split encoding depending on message - if (operation != ZCL_CONFIGURE_REPORTING) { - // apply multiplier if needed - val_d = value.as(); - val_str = value.as(); - ZbApplyMultiplier(val_d, multiplier); - - // push the value in the buffer - buf.add16(attr_id); // prepend with attribute identifier - if (operation == ZCL_READ_ATTRIBUTES_RESPONSE) { - buf.add8(Z_SUCCESS); // status OK = 0x00 - } - buf.add8(type_id); // prepend with attribute type - int32_t res = encodeSingleAttribute(buf, val_d, val_str, type_id); - if (res < 0) { - // remove the attribute type we just added - // buf.setLen(buf.len() - (operation == ZCL_READ_ATTRIBUTES_RESPONSE ? 4 : 3)); - Response_P(PSTR("{\"%s\":\"%s'%s' 0x%02X\"}"), XdrvMailbox.command, PSTR("Unsupported attribute type "), key, type_id); + Z_attribute attr; + attr.setKeyName(key.getStr()); + if (Z_parseAttributeKey(attr)) { + // Buffer ready, do some sanity checks + if (0xFFFF == cluster) { + cluster = attr.key.id.cluster; // set the cluster for this packet + } else if (cluster != attr.key.id.cluster) { + ResponseCmndChar_P(PSTR("No more than one cluster id per command")); return; } + } else { + if (attr.key_is_str) { + Response_P(PSTR("{\"%s\":\"%s'%s'\"}"), XdrvMailbox.command, PSTR("Unknown attribute "), key); + return; + } + if (Zunk == attr.attr_type) { + Response_P(PSTR("{\"%s\":\"%s'%s'\"}"), XdrvMailbox.command, PSTR("Unknown attribute type for attribute "), key); + return; + } + } + + if (value.isStr()) { + attr.setStr(value.getStr()); + } else if (value.isNum()) { + attr.setFloat(value.getFloat()); + } + + double val_d = 0; // I try to avoid `double` but this type capture both float and (u)int32_t without prevision loss + const char* val_str = ""; // variant as string + //////////////////////////////////////////////////////////////////////////////// + // Split encoding depending on message + if (operation != ZCL_CONFIGURE_REPORTING) { + if (!ZbAppendWriteBuf(buf, attr, operation == ZCL_READ_ATTRIBUTES_RESPONSE)) { + return; // error + } } else { // //////////////////////////////////////////////////////////////////////////////// // ZCL_CONFIGURE_REPORTING - if (!value.is()) { + if (!value.isObject()) { ResponseCmndChar_P(PSTR("Config requires JSON objects")); return; } - JsonObject &attr_config = value.as(); + JsonParserObject attr_config = value.getObject(); bool attr_direction = false; - const JsonVariant &val_attr_direction = GetCaseInsensitive(attr_config, PSTR("DirectionReceived")); - if (nullptr != &val_attr_direction) { - uint32_t dir = strToUInt(val_attr_direction); - if (dir) { - attr_direction = true; - } - } + uint32_t dir = attr_config.getUInt(PSTR("DirectionReceived"), 0); + if (dir) { attr_direction = true; } // read MinInterval and MaxInterval, default to 0xFFFF if not specified - uint16_t attr_min_interval = 0xFFFF; - uint16_t attr_max_interval = 0xFFFF; - const JsonVariant &val_attr_min = GetCaseInsensitive(attr_config, PSTR("MinInterval")); - if (nullptr != &val_attr_min) { attr_min_interval = strToUInt(val_attr_min); } - const JsonVariant &val_attr_max = GetCaseInsensitive(attr_config, PSTR("MaxInterval")); - if (nullptr != &val_attr_max) { attr_max_interval = strToUInt(val_attr_max); } + uint16_t attr_min_interval = attr_config.getUInt(PSTR("MinInterval"), 0xFFFF); + uint16_t attr_max_interval = attr_config.getUInt(PSTR("MaxInterval"), 0xFFFF); // read ReportableChange - const JsonVariant &val_attr_rc = GetCaseInsensitive(attr_config, PSTR("ReportableChange")); - if (nullptr != &val_attr_rc) { - val_d = val_attr_rc.as(); - val_str = val_attr_rc.as(); - ZbApplyMultiplier(val_d, multiplier); + JsonParserToken val_attr_rc = attr_config[PSTR("ReportableChange")]; + if (val_attr_rc) { + val_d = val_attr_rc.getFloat(); + val_str = val_attr_rc.getStr(); + ZbApplyMultiplier(val_d, attr.attr_multiplier); } // read TimeoutPeriod - uint16_t attr_timeout = 0x0000; - const JsonVariant &val_attr_timeout = GetCaseInsensitive(attr_config, PSTR("TimeoutPeriod")); - if (nullptr != &val_attr_timeout) { attr_timeout = strToUInt(val_attr_timeout); } + uint16_t attr_timeout = attr_config.getUInt(PSTR("TimeoutPeriod"), 0x0000); - bool attr_discrete = Z_isDiscreteDataType(type_id); + bool attr_discrete = Z_isDiscreteDataType(attr.attr_type); // all fields are gathered, output the butes into the buffer, ZCL 2.5.7.1 // common bytes buf.add8(attr_direction ? 0x01 : 0x00); - buf.add16(attr_id); + buf.add16(attr.key.id.attr_id); if (attr_direction) { buf.add16(attr_timeout); } else { - buf.add8(type_id); + buf.add8(attr.attr_type); buf.add16(attr_min_interval); buf.add16(attr_max_interval); if (!attr_discrete) { - int32_t res = encodeSingleAttribute(buf, val_d, val_str, type_id); + int32_t res = encodeSingleAttribute(buf, val_d, val_str, attr.attr_type); if (res < 0) { - Response_P(PSTR("{\"%s\":\"%s'%s' 0x%02X\"}"), XdrvMailbox.command, PSTR("Unsupported attribute type "), key, type_id); + Response_P(PSTR("{\"%s\":\"%s'%s' 0x%02X\"}"), XdrvMailbox.command, PSTR("Unsupported attribute type "), key, attr.attr_type); return; } } @@ -405,37 +351,36 @@ void ZbSendReportWrite(const JsonObject &val_pubwrite, uint16_t device, uint16_t } // Parse the "Send" attribute and send the command -void ZbSendSend(const JsonVariant &val_cmd, uint16_t device, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint16_t manuf) { +void ZbSendSend(class JsonParserToken val_cmd, uint16_t device, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint16_t manuf) { uint8_t cmd = 0; String cmd_str = ""; // the actual low-level command, either specified or computed - const char *cmd_s; // pointer to payload string + const char *cmd_s = ""; // pointer to payload string bool clusterSpecific = true; static char delim[] = ", "; // delimiters for parameters // probe the type of the argument // If JSON object, it's high level commands // If String, it's a low level command - if (val_cmd.is()) { + if (val_cmd.isObject()) { // we have a high-level command - const JsonObject &cmd_obj = val_cmd.as(); + JsonParserObject cmd_obj = val_cmd.getObject(); int32_t cmd_size = cmd_obj.size(); if (cmd_size > 1) { Response_P(PSTR("Only 1 command allowed (%d)"), cmd_size); return; } else if (1 == cmd_size) { // We have exactly 1 command, parse it - JsonObject::const_iterator it = cmd_obj.begin(); // just get the first key/value - String key = it->key; - const JsonVariant& value = it->value; + JsonParserKey key = cmd_obj.getFirstElement(); + JsonParserToken value = key.getValue(); uint32_t x = 0, y = 0, z = 0; uint16_t cmd_var; uint16_t local_cluster_id; - const __FlashStringHelper* tasmota_cmd = zigbeeFindCommand(key.c_str(), &local_cluster_id, &cmd_var); + const __FlashStringHelper* tasmota_cmd = zigbeeFindCommand(key.getStr(), &local_cluster_id, &cmd_var); if (tasmota_cmd) { cmd_str = tasmota_cmd; } else { - Response_P(PSTR("Unrecognized zigbee command: %s"), key.c_str()); + Response_P(PSTR("Unrecognized zigbee command: %s"), key.getStr()); return; } // check cluster @@ -447,13 +392,17 @@ void ZbSendSend(const JsonVariant &val_cmd, uint16_t device, uint16_t groupaddr, } // parse the JSON value, depending on its type fill in x,y,z - if (value.is()) { - x = value.as() ? 1 : 0; - } else if (value.is()) { - x = value.as(); + if (value.isNum()) { + x = value.getUInt(); // automatic conversion to 0/1 + // if (value.is()) { + // // x = value.as() ? 1 : 0; + // } else if + // } else if (value.is()) { + // x = value.as(); } else { // if non-bool or non-int, trying char* - const char *s_const = value.as(); + const char *s_const = value.getStr(nullptr); + // const char *s_const = value.as(); if (s_const != nullptr) { char s[strlen(s_const)+1]; strcpy(s, s_const); @@ -488,14 +437,13 @@ void ZbSendSend(const JsonVariant &val_cmd, uint16_t device, uint16_t groupaddr, } else { // we have zero command, pass through until last error for missing command } - } else if (val_cmd.is()) { + } else if (val_cmd.isStr()) { // low-level command - cmd_str = val_cmd.as(); // Now parse the string to extract cluster, command, and payload // Parse 'cmd' in the form "AAAA_BB/CCCCCCCC" or "AAAA!BB/CCCCCCCC" // where AA is the cluster number, BBBB the command number, CCCC... the payload // First delimiter is '_' for a global command, or '!' for a cluster specific command - const char * data = cmd_str.c_str(); + const char * data = val_cmd.getStr(); uint16_t local_cluster_id = parseHex(&data, 4); // check cluster @@ -534,7 +482,7 @@ void ZbSendSend(const JsonVariant &val_cmd, uint16_t device, uint16_t groupaddr, // Parse the "Send" attribute and send the command -void ZbSendRead(const JsonVariant &val_attr, uint16_t device, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint16_t manuf, uint8_t operation) { +void ZbSendRead(JsonParserToken val_attr, uint16_t device, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint16_t manuf, uint8_t operation) { // ZbSend {"Device":"0xF289","Cluster":0,"Endpoint":3,"Read":5} // ZbSend {"Device":"0xF289","Cluster":"0x0000","Endpoint":"0x0003","Read":"0x0005"} // ZbSend {"Device":"0xF289","Cluster":0,"Endpoint":3,"Read":[5,6,7,4]} @@ -554,30 +502,30 @@ void ZbSendRead(const JsonVariant &val_attr, uint16_t device, uint16_t groupaddr attr_item_offset = 1; } - uint16_t val = strToUInt(val_attr); - if (val_attr.is()) { - const JsonArray& attr_arr = val_attr.as(); + if (val_attr.isArray()) { + // value is an array [] + JsonParserArray attr_arr = val_attr.getArray(); attrs_len = attr_arr.size() * attr_item_len; attrs = (uint8_t*) calloc(attrs_len, 1); uint32_t i = 0; for (auto value : attr_arr) { - uint16_t val = strToUInt(value); + uint16_t val = value.getUInt(); i += attr_item_offset; attrs[i++] = val & 0xFF; attrs[i++] = val >> 8; i += attr_item_len - 2 - attr_item_offset; // normally 0 } - } else if (val_attr.is()) { - const JsonObject& attr_obj = val_attr.as(); + } else if (val_attr.isObject()) { + // value is an object {} + JsonParserObject attr_obj = val_attr.getObject(); attrs_len = attr_obj.size() * attr_item_len; attrs = (uint8_t*) calloc(attrs_len, 1); uint32_t actual_attr_len = 0; // iterate on keys - for (JsonObject::const_iterator it=attr_obj.begin(); it!=attr_obj.end(); ++it) { - const char *key = it->key; - const JsonVariant &value = it->value; // we don't need the value here, only keys are relevant + for (auto key : attr_obj) { + JsonParserToken value = key.getValue(); bool found = false; // scan attributes to find by name, and retrieve type @@ -588,11 +536,11 @@ void ZbSendRead(const JsonVariant &val_attr, uint16_t device, uint16_t groupaddr uint16_t local_cluster_id = CxToCluster(pgm_read_byte(&converter->cluster_short)); // uint8_t local_type_id = pgm_read_byte(&converter->type); - if ((pgm_read_word(&converter->name_offset)) && (0 == strcasecmp_P(key, Z_strings + pgm_read_word(&converter->name_offset)))) { + if ((pgm_read_word(&converter->name_offset)) && (0 == strcasecmp_P(key.getStr(), Z_strings + pgm_read_word(&converter->name_offset)))) { // match name // check if there is a conflict with cluster // TODO - if (!value && attr_item_offset) { + if (!(value.getBool()) && attr_item_offset) { // If value is false (non-default) then set direction to 1 (for ReadConfig) attrs[actual_attr_len] = 0x01; } @@ -619,10 +567,14 @@ void ZbSendRead(const JsonVariant &val_attr, uint16_t device, uint16_t groupaddr attrs_len = actual_attr_len; } else { - attrs_len = attr_item_len; - attrs = (uint8_t*) calloc(attrs_len, 1); - attrs[0 + attr_item_offset] = val & 0xFF; // little endian - attrs[1 + attr_item_offset] = val >> 8; + // value is a literal + if (0xFFFF != cluster) { + uint16_t val = val_attr.getUInt(); + attrs_len = attr_item_len; + attrs = (uint8_t*) calloc(attrs_len, 1); + attrs[0 + attr_item_offset] = val & 0xFF; // little endian + attrs[1 + attr_item_offset] = val >> 8; + } } if (attrs_len > 0) { @@ -672,9 +624,9 @@ void CmndZbSend(void) { // ZbSend { "device":"0x1234", "endpoint":"0x03", "send":{"Color":"1,2"} } // ZbSend { "device":"0x1234", "endpoint":"0x03", "send":{"Color":"0x1122,0xFFEE"} } if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - DynamicJsonBuffer jsonBuf; - const JsonObject &json = jsonBuf.parseObject((const char*) XdrvMailbox.data); - if (!json.success()) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } // params uint16_t device = BAD_SHORTADDR; // BAD_SHORTADDR is broadcast, so considered invalid @@ -685,15 +637,15 @@ void CmndZbSend(void) { // parse "Device" and "Group" - const JsonVariant &val_device = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_DEVICE)); - if (nullptr != &val_device) { - device = zigbee_devices.parseDeviceParam(val_device.as()); + JsonParserToken val_device = root[PSTR(D_CMND_ZIGBEE_DEVICE)]; + if (val_device) { + device = zigbee_devices.parseDeviceParam(val_device.getStr()); if (BAD_SHORTADDR == device) { ResponseCmndChar_P(PSTR("Invalid parameter")); return; } } if (BAD_SHORTADDR == device) { // if not found, check if we have a group - const JsonVariant &val_group = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_GROUP)); - if (nullptr != &val_group) { - groupaddr = strToUInt(val_group); + JsonParserToken val_group = root[PSTR(D_CMND_ZIGBEE_GROUP)]; + if (val_group) { + groupaddr = val_group.getUInt(); } else { // no device nor group ResponseCmndChar_P(PSTR("Unknown device")); return; @@ -703,12 +655,9 @@ void CmndZbSend(void) { // Note: groupaddr == 0 is valid // read other parameters - const JsonVariant &val_cluster = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_CLUSTER)); - if (nullptr != &val_cluster) { cluster = strToUInt(val_cluster); } - const JsonVariant &val_endpoint = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_ENDPOINT)); - if (nullptr != &val_endpoint) { endpoint = strToUInt(val_endpoint); } - const JsonVariant &val_manuf = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_MANUF)); - if (nullptr != &val_manuf) { manuf = strToUInt(val_manuf); } + cluster = root.getUInt(PSTR(D_CMND_ZIGBEE_CLUSTER), cluster); + endpoint = root.getUInt(PSTR(D_CMND_ZIGBEE_ENDPOINT), endpoint); + manuf = root.getUInt(PSTR(D_CMND_ZIGBEE_MANUF), manuf); // infer endpoint if (BAD_SHORTADDR == device) { @@ -724,61 +673,61 @@ void CmndZbSend(void) { // from here endpoint is valid and non-zero // cluster may be already specified or 0xFFFF - const JsonVariant &val_cmd = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_SEND)); - const JsonVariant &val_read = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_READ)); - const JsonVariant &val_write = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_WRITE)); - const JsonVariant &val_publish = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_REPORT)); - const JsonVariant &val_response = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_RESPONSE)); - const JsonVariant &val_read_config = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_READ_CONFIG)); - const JsonVariant &val_config = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_CONFIG)); - uint32_t multi_cmd = (nullptr != &val_cmd) + (nullptr != &val_read) + (nullptr != &val_write) + (nullptr != &val_publish) - + (nullptr != &val_response) + (nullptr != &val_read_config) + (nullptr != &val_config); + JsonParserToken val_cmd = root[PSTR(D_CMND_ZIGBEE_SEND)]; + JsonParserToken val_read = root[PSTR(D_CMND_ZIGBEE_READ)]; + JsonParserToken val_write = root[PSTR(D_CMND_ZIGBEE_WRITE)]; + JsonParserToken val_publish = root[PSTR(D_CMND_ZIGBEE_REPORT)]; + JsonParserToken val_response = root[PSTR(D_CMND_ZIGBEE_RESPONSE)]; + JsonParserToken val_read_config = root[PSTR(D_CMND_ZIGBEE_READ_CONFIG)]; + JsonParserToken val_config = root[PSTR(D_CMND_ZIGBEE_CONFIG)]; + uint32_t multi_cmd = ((bool)val_cmd) + ((bool)val_read) + ((bool)val_write) + ((bool)val_publish) + + ((bool)val_response) + ((bool)val_read_config) + ((bool)val_config); if (multi_cmd > 1) { ResponseCmndChar_P(PSTR("Can only have one of: 'Send', 'Read', 'Write', 'Report', 'Reponse', 'ReadConfig' or 'Config'")); return; } // from here we have one and only one command - if (nullptr != &val_cmd) { + if (val_cmd) { // "Send":{...commands...} // we accept either a string or a JSON object ZbSendSend(val_cmd, device, groupaddr, cluster, endpoint, manuf); - } else if (nullptr != &val_read) { + } else if (val_read) { // "Read":{...attributes...}, "Read":attribute or "Read":[...attributes...] // we accept eitehr a number, a string, an array of numbers/strings, or a JSON object ZbSendRead(val_read, device, groupaddr, cluster, endpoint, manuf, ZCL_READ_ATTRIBUTES); - } else if (nullptr != &val_write) { + } else if (val_write) { // only KSON object - if (!val_write.is()) { + if (!val_write.isObject()) { ResponseCmndChar_P(PSTR("Missing parameters")); return; } // "Write":{...attributes...} ZbSendReportWrite(val_write, device, groupaddr, cluster, endpoint, manuf, ZCL_WRITE_ATTRIBUTES); - } else if (nullptr != &val_publish) { + } else if (val_publish) { // "Publish":{...attributes...} // only KSON object - if (!val_publish.is()) { + if (!val_publish.isObject()) { ResponseCmndChar_P(PSTR("Missing parameters")); return; } ZbSendReportWrite(val_publish, device, groupaddr, cluster, endpoint, manuf, ZCL_REPORT_ATTRIBUTES); - } else if (nullptr != &val_response) { + } else if (val_response) { // "Report":{...attributes...} // only KSON object - if (!val_response.is()) { + if (!val_response.isObject()) { ResponseCmndChar_P(PSTR("Missing parameters")); return; } ZbSendReportWrite(val_response, device, groupaddr, cluster, endpoint, manuf, ZCL_READ_ATTRIBUTES_RESPONSE); - } else if (nullptr != &val_read_config) { + } else if (val_read_config) { // "ReadConfg":{...attributes...}, "ReadConfg":attribute or "ReadConfg":[...attributes...] // we accept eitehr a number, a string, an array of numbers/strings, or a JSON object ZbSendRead(val_read_config, device, groupaddr, cluster, endpoint, manuf, ZCL_READ_REPORTING_CONFIGURATION); - } else if (nullptr != &val_config) { + } else if (val_config) { // "Config":{...attributes...} // only JSON object - if (!val_config.is()) { + if (!val_config.isObject()) { ResponseCmndChar_P(PSTR("Missing parameters")); return; } @@ -798,61 +747,57 @@ void ZbBindUnbind(bool unbind) { // false = bind, true = unbind // local endpoint is always 1, IEEE addresses are calculated if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - DynamicJsonBuffer jsonBuf; - const JsonObject &json = jsonBuf.parseObject((const char*) XdrvMailbox.data); - if (!json.success()) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } // params - uint16_t srcDevice = BAD_SHORTADDR; // BAD_SHORTADDR is broadcast, so considered invalid + uint16_t srcDevice = BAD_SHORTADDR; // BAD_SHORTADDR is broadcast, so considered invalid uint16_t dstDevice = BAD_SHORTADDR; // BAD_SHORTADDR is broadcast, so considered invalid uint64_t dstLongAddr = 0; uint8_t endpoint = 0x00; // 0x00 is invalid for the src endpoint - uint8_t toendpoint = 0x00; // 0x00 is invalid for the dst endpoint + uint8_t toendpoint = 0x01; // default dest endpoint to 0x01 uint16_t toGroup = 0x0000; // group address uint16_t cluster = 0; // 0xFFFF is invalid uint32_t group = 0xFFFFFFFF; // 16 bits values, otherwise 0xFFFFFFFF is unspecified // Information about source device: "Device", "Endpoint", "Cluster" // - the source endpoint must have a known IEEE address - const JsonVariant &val_device = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_DEVICE)); - if (nullptr != &val_device) { - srcDevice = zigbee_devices.parseDeviceParam(val_device.as()); - } - if ((nullptr == &val_device) || (BAD_SHORTADDR == srcDevice)) { ResponseCmndChar_P(PSTR("Unknown source device")); return; } + srcDevice = zigbee_devices.parseDeviceParam(root.getStr(PSTR(D_CMND_ZIGBEE_DEVICE), nullptr)); + if (BAD_SHORTADDR == srcDevice) { ResponseCmndChar_P(PSTR("Unknown source device")); return; } // check if IEEE address is known uint64_t srcLongAddr = zigbee_devices.getDeviceLongAddr(srcDevice); if (0 == srcLongAddr) { ResponseCmndChar_P(PSTR("Unknown source IEEE address")); return; } // look for source endpoint - const JsonVariant &val_endpoint = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_ENDPOINT)); - if (nullptr != &val_endpoint) { endpoint = strToUInt(val_endpoint); } - else { endpoint = zigbee_devices.findFirstEndpoint(srcDevice); } + endpoint = root.getUInt(PSTR(D_CMND_ZIGBEE_ENDPOINT), endpoint); + if (0 == endpoint) { endpoint = zigbee_devices.findFirstEndpoint(srcDevice); } // look for source cluster - const JsonVariant &val_cluster = GetCaseInsensitive(json, PSTR(D_CMND_ZIGBEE_CLUSTER)); - if (nullptr != &val_cluster) { - cluster = strToUInt(val_cluster); // first convert as number + JsonParserToken val_cluster = root[PSTR(D_CMND_ZIGBEE_CLUSTER)]; + if (val_cluster) { + cluster = val_cluster.getUInt(cluster); // first convert as number if (0 == cluster) { - zigbeeFindAttributeByName(val_cluster.as(), &cluster, nullptr, nullptr); + zigbeeFindAttributeByName(val_cluster.getStr(), &cluster, nullptr, nullptr); } } // Or Group Address - we don't need a dstEndpoint in this case - const JsonVariant &to_group = GetCaseInsensitive(json, PSTR("ToGroup")); - if (nullptr != &to_group) { toGroup = strToUInt(to_group); } + JsonParserToken to_group = root[PSTR("ToGroup")]; + if (to_group) { toGroup = to_group.getUInt(toGroup); } // Either Device address // In this case the following parameters are mandatory // - "ToDevice" and the device must have a known IEEE address // - "ToEndpoint" - const JsonVariant &dst_device = GetCaseInsensitive(json, PSTR("ToDevice")); + JsonParserToken dst_device = root[PSTR("ToDevice")]; // If no target is specified, we default to coordinator 0x0000 - if ((nullptr == &to_group) && (nullptr == &dst_device)) { + if ((!to_group) && (!dst_device)) { dstDevice = 0x0000; } - if ((nullptr != &dst_device) || (BAD_SHORTADDR != dstDevice)) { + if ((dst_device) || (BAD_SHORTADDR != dstDevice)) { if (BAD_SHORTADDR == dstDevice) { - dstDevice = zigbee_devices.parseDeviceParam(dst_device.as()); + dstDevice = zigbee_devices.parseDeviceParam(dst_device.getStr(nullptr)); if (BAD_SHORTADDR == dstDevice) { ResponseCmndChar_P(PSTR("Invalid parameter")); return; } } if (0x0000 == dstDevice) { @@ -862,14 +807,12 @@ void ZbBindUnbind(bool unbind) { // false = bind, true = unbind } if (0 == dstLongAddr) { ResponseCmndChar_P(PSTR("Unknown dest IEEE address")); return; } - const JsonVariant &val_toendpoint = GetCaseInsensitive(json, PSTR("ToEndpoint")); - if (nullptr != &val_toendpoint) { toendpoint = strToUInt(val_toendpoint); } - else { toendpoint = 0x01; } // default to endpoint 1 + toendpoint = root.getUInt(PSTR("ToEndpoint"), toendpoint); } // make sure we don't have conflicting parameters - if (&to_group && dstLongAddr) { ResponseCmndChar_P(PSTR("Cannot have both \"ToDevice\" and \"ToGroup\"")); return; } - if (!&to_group && !dstLongAddr) { ResponseCmndChar_P(PSTR("Missing \"ToDevice\" or \"ToGroup\"")); return; } + if (to_group && dstLongAddr) { ResponseCmndChar_P(PSTR("Cannot have both \"ToDevice\" and \"ToGroup\"")); return; } + if (!to_group && !dstLongAddr) { ResponseCmndChar_P(PSTR("Missing \"ToDevice\" or \"ToGroup\"")); return; } #ifdef USE_ZIGBEE_ZNP SBuffer buf(34); @@ -1121,38 +1064,32 @@ void CmndZbSave(void) { // ZbRestore {"Device":"0x5ADF","Name":"Petite_Lampe","IEEEAddr":"0x90FD9FFFFE03B051","ModelId":"TRADFRI bulb E27 WS opal 980lm","Manufacturer":"IKEA of Sweden","Endpoints":["0x01","0xF2"]} void CmndZbRestore(void) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - DynamicJsonBuffer jsonBuf; - const JsonVariant json_parsed = jsonBuf.parse((const char*) XdrvMailbox.data); // const to force a copy of parameter - const JsonVariant * json = &json_parsed; // root of restore, to be changed if needed - bool success = false; + JsonParser parser(XdrvMailbox.data); + JsonParserToken root = parser.getRoot(); - // check if parsing succeeded - if (json_parsed.is()) { - success = json_parsed.as().success(); - } else if (json_parsed.is()) { - success = json_parsed.as().success(); - } - if (!success) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } + if (!parser || !(root.isObject() || root.isArray())) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } // Check is root contains `ZbStatus` key, if so change the root - const JsonVariant * zbstatus = &startsWithCaseInsensitive(*json, PSTR("ZbStatus")); - if (nullptr != zbstatus) { - json = zbstatus; + JsonParserToken zbstatus = root.getObject().findStartsWith(PSTR("ZbStatus")); + if (zbstatus) { + root = zbstatus; } // check if the root is an array - if (json->is()) { - const JsonArray& arr = json->as(); - for (auto elt : arr) { + if (root.isArray()) { + JsonParserArray arr = JsonParserArray(root); + for (const auto elt : arr) { // call restore on each item - int32_t res = zigbee_devices.deviceRestore(elt); - if (res < 0) { - ResponseCmndChar_P(PSTR("Restore failed")); - return; + if (elt.isObject()) { + int32_t res = zigbee_devices.deviceRestore(JsonParserObject(elt)); + if (res < 0) { + ResponseCmndChar_P(PSTR("Restore failed")); + return; + } } } - } else if (json->is()) { - int32_t res = zigbee_devices.deviceRestore(*json); + } else if (root.isObject()) { + int32_t res = zigbee_devices.deviceRestore(JsonParserObject(root)); if (res < 0) { ResponseCmndChar_P(PSTR("Restore failed")); return; @@ -1215,6 +1152,15 @@ void CmndZbPermitJoin(void) { buf.add8(duration); buf.add8(0x01); // TC_Significance - This field shall always have a value of 1, indicating a request to change the Trust Center policy. If a frame is received with a value of 0, it shall be treated as having a value of 1. EZ_SendZDO(0xFFFC, ZDO_Mgmt_Permit_Joining_req, buf.buf(), buf.len()); + + // Set Timer after the end of the period, and reset a non-expired previous timer + if (duration > 0) { + // Log pairing mode enabled + Response_P(PSTR("{\"" D_JSON_ZIGBEE_STATE "\":{\"Status\":21,\"Message\":\"Pairing mode enabled\"}}")); + MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEE_STATE)); + } + // always register timer for disable, might happen at next tick + zigbee_devices.setTimer(0x0000 /* coordinator */, 0 /* group addr*/, duration * 1000, 0, 0 /* endpoint */, Z_CAT_PERMIT_JOIN, 0 /* value */, &Z_PermitJoinDisable); #endif // USE_ZIGBEE_EZSP ResponseCmndDone(); @@ -1282,30 +1228,26 @@ void CmndZbConfig(void) { // if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } RemoveAllSpaces(XdrvMailbox.data); if (strlen(XdrvMailbox.data) > 0) { - DynamicJsonBuffer jsonBuf; - const JsonObject &json = jsonBuf.parseObject((const char*) XdrvMailbox.data); - if (!json.success()) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } - + JsonParser parser(XdrvMailbox.data); + JsonParserObject root = parser.getRootObject(); + if (!root) { ResponseCmndChar_P(PSTR(D_JSON_INVALID_JSON)); return; } // Channel - const JsonVariant &val_channel = GetCaseInsensitive(json, PSTR("Channel")); - if (nullptr != &val_channel) { zb_channel = strToUInt(val_channel); } + + zb_channel = root.getUInt(PSTR("Channel"), zb_channel); + zb_pan_id = root.getUInt(PSTR("PanID"), zb_pan_id); + zb_ext_panid = root.getULong(PSTR("ExtPanID"), zb_ext_panid); + zb_precfgkey_l = root.getULong(PSTR("KeyL"), zb_precfgkey_l); + zb_precfgkey_h = root.getULong(PSTR("KeyH"), zb_precfgkey_h); + zb_txradio_dbm = root.getUInt(PSTR("TxRadio"), zb_txradio_dbm); + if (zb_channel < 11) { zb_channel = 11; } if (zb_channel > 26) { zb_channel = 26; } - // PanID - const JsonVariant &val_pan_id = GetCaseInsensitive(json, PSTR("PanID")); - if (nullptr != &val_pan_id) { zb_pan_id = strToUInt(val_pan_id); } - // ExtPanID - const JsonVariant &val_ext_pan_id = GetCaseInsensitive(json, PSTR("ExtPanID")); - if (nullptr != &val_ext_pan_id) { zb_ext_panid = strtoull(val_ext_pan_id.as(), nullptr, 0); } - // KeyL - const JsonVariant &val_key_l = GetCaseInsensitive(json, PSTR("KeyL")); - if (nullptr != &val_key_l) { zb_precfgkey_l = strtoull(val_key_l.as(), nullptr, 0); } - // KeyH - const JsonVariant &val_key_h = GetCaseInsensitive(json, PSTR("KeyH")); - if (nullptr != &val_key_h) { zb_precfgkey_h = strtoull(val_key_h.as(), nullptr, 0); } - // TxRadio dBm - const JsonVariant &val_txradio = GetCaseInsensitive(json, PSTR("TxRadio")); - if (nullptr != &val_txradio) { zb_txradio_dbm = strToUInt(val_txradio); } + // if network key is zero, we generate a truly random key with a hardware generator from ESP + if ((0 == zb_precfgkey_l) && (0 == zb_precfgkey_h)) { + AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "generating random Zigbee network key")); + zb_precfgkey_l = (uint64_t)HwRandom() << 32 | HwRandom(); + zb_precfgkey_h = (uint64_t)HwRandom() << 32 | HwRandom(); + } // Check if a parameter was changed after all if ( (zb_channel != Settings.zb_channel) || @@ -1367,8 +1309,37 @@ extern "C" { return 1; } } -} + +// Convert seconds to a string representing days, hours or minutes present in the n-value. +// The string will contain the most coarse time only, rounded down (61m == 01h, 01h37m == 01h). +// Inputs: +// - n: uint32_t representing some number of seconds +// - result: a buffer of suitable size (7 bytes would represent the entire solution space +// for UINT32_MAX including the trailing null-byte, or "49710d") +// - result_len: A numeric value representing the total length of the result buffer +// Returns: +// - The number of characters that would have been written were result sufficiently large +// - negatve number on encoding error from snprintf +// + int convert_seconds_to_dhm(uint32_t n, char *result, size_t result_len){ + char fmtstr[] = "%02dmhd"; // Don't want this in progmem, because we mutate it. + uint32_t conversions[3] = {24 * 3600, 3600, 60}; + uint32_t value; + for(int i = 0; i < 3; ++i) { + value = n / conversions[i]; + if(value > 0) { + fmtstr[4] = fmtstr[6-i]; + break; + } + n = n % conversions[i]; + } + + // Null-terminate the string at the last "valid" index, removing any excess zero values. + fmtstr[5] = '\0'; + return snprintf(result, result_len, fmtstr, value); + } +} void ZigbeeShow(bool json) { if (json) { @@ -1379,12 +1350,22 @@ void ZigbeeShow(bool json) if (!zigbee_num) { return; } if (zigbee_num > 255) { zigbee_num = 255; } - // Calculate fixed column width for best visual result (Theos opinion) - const uint8_t px_batt = 30; // Battery icon is 20px, add 10px as separator - const uint8_t px_lqi = (strlen(D_LQI) + 4) * 10; // LQI 254 = 70px - WSContentSend_P(PSTR("{t}")); // Terminate current two column table and open new table - WSContentSend_P(PSTR("")); + WSContentSend_P(PSTR( + "" + )); // sort elements by name, then by id uint8_t sorted_idx[zigbee_num]; @@ -1393,44 +1374,69 @@ void ZigbeeShow(bool json) } qsort(sorted_idx, zigbee_num, sizeof(sorted_idx[0]), device_cmp); + uint32_t now = Rtc.utc_time; + for (uint32_t i = 0; i < zigbee_num; i++) { const Z_Device &device = zigbee_devices.devicesAt(sorted_idx[i]); uint16_t shortaddr = device.shortaddr; - { // exxplicit scope to free up stack allocated strings - char *name = (char*) device.friendlyName; - char sdevice[33]; - if (nullptr == name) { - snprintf_P(sdevice, sizeof(sdevice), PSTR(D_DEVICE " 0x%04X"), shortaddr); - name = sdevice; - } + char *name = (char*) device.friendlyName; - char slqi[8]; - snprintf_P(slqi, sizeof(slqi), PSTR("-")); - if (device.validLqi()) { - snprintf_P(slqi, sizeof(slqi), PSTR("%d"), device.lqi); - } - - char sbatt[64]; - snprintf_P(sbatt, sizeof(sbatt), PSTR(" ")); - if (device.validBatteryPercent()) { - snprintf_P(sbatt, sizeof(sbatt), PSTR(""), device.batterypercent, changeUIntScale(device.batterypercent, 0, 100, 0, 14)); - } - - if (!i) { // First row needs style info - WSContentSend_PD(PSTR("%s%s" D_LQI " %s{e}"), - name, px_batt, sbatt, px_lqi, slqi); - } else { // Following rows don't need style info so reducing ajax package - WSContentSend_PD(PSTR("%s%s" D_LQI " %s{e}"), name, sbatt, slqi); - } + char sdevice[33]; + if (nullptr == name) { + snprintf_P(sdevice, sizeof(sdevice), PSTR(D_DEVICE " 0x%04X"), shortaddr); + name = sdevice; } - // Sensor + char sbatt[64]; + snprintf_P(sbatt, sizeof(sbatt), PSTR(" ")); + if (device.validBatteryPercent()) { + snprintf_P(sbatt, sizeof(sbatt), + PSTR(""), + device.batterypercent, changeUIntScale(device.batterypercent, 0, 100, 0, 14) + ); + } + + uint32_t num_bars = 0; + + char slqi[4]; + slqi[0] = '-'; + slqi[1] = '\0'; + if (device.validLqi()){ + num_bars = changeUIntScale(device.lqi, 0, 254, 0, 4); + snprintf_P(slqi, sizeof(slqi), PSTR("%d"), device.lqi); + } + + WSContentSend_PD(PSTR( + "" + "%s" // name + "%s" // sbatt (Battery Indicator) + "
" // slqi + ), name, sbatt, slqi); + + if(device.validLqi()) { + for(uint32_t j = 0; j < 4; ++j) { + WSContentSend_PD(PSTR(""), j, (num_bars < j) ? PSTR(" o30") : PSTR("")); + } + } + char dhm[16]; // len("🕗" + "49710d" + '\0') == 16 + snprintf_P(dhm, sizeof(dhm), PSTR(" ")); + if(device.validLastSeen()){ + snprintf_P(dhm, sizeof(dhm), PSTR("🕗")); + convert_seconds_to_dhm(now - device.last_seen, &dhm[9], 7); + } + + WSContentSend_PD(PSTR( + "
" // Close LQI + "%s{e}" // dhm (Last Seen) + ), dhm ); + + // Sensors bool temperature_ok = device.validTemperature(); bool humidity_ok = device.validHumidity(); bool pressure_ok = device.validPressure(); if (temperature_ok || humidity_ok || pressure_ok) { - WSContentSend_P(PSTR("┆")); + WSContentSend_P(PSTR("┆")); if (temperature_ok) { char buf[12]; dtostrf(device.temperature / 10.0f, 3, 1, buf); @@ -1442,6 +1448,7 @@ void ZigbeeShow(bool json) if (pressure_ok) { WSContentSend_P(PSTR(" ⛅ %d hPa"), device.pressure); } + WSContentSend_P(PSTR("{e}")); } @@ -1450,7 +1457,7 @@ void ZigbeeShow(bool json) if (power_ok) { uint8_t channels = device.getLightChannels(); if (0xFF == channels) { channels = 5; } // if number of channel is unknown, display all known attributes - WSContentSend_P(PSTR("┆ %s"), device.getPower() ? PSTR(D_ON) : PSTR(D_OFF)); + WSContentSend_P(PSTR("┆ %s"), device.getPower() ? PSTR(D_ON) : PSTR(D_OFF)); if (device.validDimmer() && (channels >= 1)) { WSContentSend_P(PSTR(" 🔅 %d%%"), changeUIntScale(device.dimmer,0,254,0,100)); } @@ -1477,6 +1484,7 @@ void ZigbeeShow(bool json) WSContentSend_P(PSTR(" %dW"), device.mains_power); } } + WSContentSend_P(PSTR("{e}")); } } diff --git a/tasmota/xdrv_24_buzzer.ino b/tasmota/xdrv_24_buzzer.ino index 10232cc52..60f158d6f 100644 --- a/tasmota/xdrv_24_buzzer.ino +++ b/tasmota/xdrv_24_buzzer.ino @@ -32,6 +32,7 @@ struct BUZZER { uint8_t inverted = 0; // Buzzer inverted flag (1 = (0 = On, 1 = Off)) uint8_t count = 0; // Number of buzzes uint8_t mode = 0; // Buzzer mode (0 = regular, 1 = infinite, 2 = follow LED) + uint8_t freq_mode = 0; // Output mode (0 = regular, 1 = using frequency output) uint8_t set[2]; uint8_t duration; uint8_t state = 0; @@ -39,9 +40,28 @@ struct BUZZER { /*********************************************************************************************/ -void BuzzerOff(void) +void BuzzerSet(uint8_t state) { - DigitalWrite(GPIO_BUZZER, 0, Buzzer.inverted); // Buzzer Off + if (Buzzer.inverted) { + state = !state; + } + + if (Buzzer.freq_mode == 1) { + static uint8_t last_state = 0; + if (last_state != state) { + if (state) { + analogWrite(Pin(GPIO_BUZZER, 0), Settings.pwm_range / 2); // set 50% duty cycle for frequency output + } + else { + analogWrite(Pin(GPIO_BUZZER, 0), 0); // set 0% (or 100% for inverted PWM) duty cycle which turns off frequency output either way + } + last_state = state; + } + } + else { + DigitalWrite(GPIO_BUZZER, 0, state); // Buzzer On/Off + } + } //void BuzzerBeep(uint32_t count = 1, uint32_t on = 1, uint32_t off = 1, uint32_t tune = 0, uint32_t mode = 0); @@ -69,11 +89,19 @@ void BuzzerBeep(uint32_t count, uint32_t on, uint32_t off, uint32_t tune, uint32 } Buzzer.count = count * 2; // Start buzzer - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("BUZ: %d(%d),%d,%d,0x%08X(0x%08X)"), count, Buzzer.count, on, off, tune, Buzzer.tune); + // We can use PWM mode for buzzer output if enabled. + if (Settings.flag4.buzzer_freq_mode) { // SetOption111 - Enable frequency output mode for buzzer + Buzzer.freq_mode = 1; + } + else { + Buzzer.freq_mode = 0; + } + + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("BUZ: %d(%d),%d,%d,0x%08X(0x%08X),%d"), count, Buzzer.count, on, off, tune, Buzzer.tune, Buzzer.freq_mode); Buzzer.enable = (Buzzer.count > 0); if (!Buzzer.enable) { - BuzzerOff(); + BuzzerSet(0); } } @@ -81,7 +109,7 @@ void BuzzerSetStateToLed(uint32_t state) { if (Buzzer.enable && (2 == Buzzer.mode)) { Buzzer.state = (state != 0); - DigitalWrite(GPIO_BUZZER, 0, (Buzzer.inverted) ? !Buzzer.state : Buzzer.state); + BuzzerSet(Buzzer.state); } } @@ -113,7 +141,7 @@ void BuzzerInit(void) { if (PinUsed(GPIO_BUZZER)) { pinMode(Pin(GPIO_BUZZER), OUTPUT); - BuzzerOff(); + BuzzerSet(0); } else { Buzzer.active = false; } @@ -140,7 +168,7 @@ void BuzzerEvery100mSec(void) Buzzer.duration = Buzzer.set[Buzzer.state]; } } - DigitalWrite(GPIO_BUZZER, 0, (Buzzer.inverted) ? !Buzzer.state : Buzzer.state); + BuzzerSet(Buzzer.state); } else { Buzzer.enable = false; } diff --git a/tasmota/xdrv_27_shutter.ino b/tasmota/xdrv_27_shutter.ino index e5a5353f6..44bbb5429 100644 --- a/tasmota/xdrv_27_shutter.ino +++ b/tasmota/xdrv_27_shutter.ino @@ -1,5 +1,5 @@ /* - xdrv_27_shutter.ino - Shutter/Blind support for Tasmota + xdrv_27_Shutter[i].ino - Shutter/Blind support for Tasmota Copyright (C) 2020 Stefan Bode @@ -23,29 +23,44 @@ \*********************************************************************************************/ #define XDRV_27 27 +#ifndef SHUTTER_STEPPER + #define SHUTTER_STEPPER +#endif #define D_SHUTTER "SHUTTER" const uint16_t MOTOR_STOP_TIME = 500; // in mS -const uint8_t steps_per_second = 20; // FUNC_EVERY_50_MSECOND +const uint16_t RESOLUTION = 1000; // incresed to 1000 in 8.5 to ramp servos +const uint8_t STEPS_PER_SECOND = 20; // FUNC_EVERY_50_MSECOND +const uint16_t pwm_max = 500; +const uint16_t pwm_min = 90; uint8_t calibrate_pos[6] = {0,30,50,70,90,100}; uint16_t messwerte[5] = {30,50,70,90,100}; -uint16_t last_execute_step; -enum ShutterModes { SHT_OFF_OPEN__OFF_CLOSE, SHT_OFF_ON__OPEN_CLOSE, SHT_PULSE_OPEN__PULSE_CLOSE, SHT_OFF_ON__OPEN_CLOSE_STEPPER,}; +int32_t velocity_max = 0; +int32_t velocity_change_per_step_max = 0; +int32_t min_runtime_ms = 0; +int32_t current_stop_way = 0; +int32_t next_possible_stop_position = 0; +int32_t toBeAcc = 0; + + +const uint8_t MAX_MODES = 7; +enum Shutterposition_mode {SHT_UNDEF, SHT_TIME, SHT_TIME_UP_DOWN, SHT_TIME_GARAGE, SHT_COUNTER, SHT_PWM_VALUE, SHT_PWM_TIME,}; +enum Shutterswitch_mode {SHT_SWITCH, SHT_PULSE,}; enum ShutterButtonStates { SHT_NOT_PRESSED, SHT_PRESSED_MULTI, SHT_PRESSED_HOLD, SHT_PRESSED_IMMEDIATE, SHT_PRESSED_EXT_HOLD, SHT_PRESSED_MULTI_SIMULTANEOUS, SHT_PRESSED_HOLD_SIMULTANEOUS, SHT_PRESSED_EXT_HOLD_SIMULTANEOUS,}; const char kShutterCommands[] PROGMEM = D_PRFX_SHUTTER "|" D_CMND_SHUTTER_OPEN "|" D_CMND_SHUTTER_CLOSE "|" D_CMND_SHUTTER_TOGGLE "|" D_CMND_SHUTTER_TOGGLEDIR "|" D_CMND_SHUTTER_STOP "|" D_CMND_SHUTTER_POSITION "|" - D_CMND_SHUTTER_OPENTIME "|" D_CMND_SHUTTER_CLOSETIME "|" D_CMND_SHUTTER_RELAY "|" + D_CMND_SHUTTER_OPENTIME "|" D_CMND_SHUTTER_CLOSETIME "|" D_CMND_SHUTTER_RELAY "|" D_CMND_SHUTTER_MODE "|" D_CMND_SHUTTER_PWMRANGE "|" D_CMND_SHUTTER_SETHALFWAY "|" D_CMND_SHUTTER_SETCLOSE "|" D_CMND_SHUTTER_SETOPEN "|" D_CMND_SHUTTER_INVERT "|" D_CMND_SHUTTER_CLIBRATION "|" D_CMND_SHUTTER_MOTORDELAY "|" D_CMND_SHUTTER_FREQUENCY "|" D_CMND_SHUTTER_BUTTON "|" D_CMND_SHUTTER_LOCK "|" D_CMND_SHUTTER_ENABLEENDSTOPTIME "|" D_CMND_SHUTTER_INVERTWEBBUTTONS "|" D_CMND_SHUTTER_STOPOPEN "|" D_CMND_SHUTTER_STOPCLOSE "|" D_CMND_SHUTTER_STOPTOGGLE "|" D_CMND_SHUTTER_STOPTOGGLEDIR "|" D_CMND_SHUTTER_STOPPOSITION; void (* const ShutterCommand[])(void) PROGMEM = { &CmndShutterOpen, &CmndShutterClose, &CmndShutterToggle, &CmndShutterToggleDir, &CmndShutterStop, &CmndShutterPosition, - &CmndShutterOpenTime, &CmndShutterCloseTime, &CmndShutterRelay, + &CmndShutterOpenTime, &CmndShutterCloseTime, &CmndShutterRelay, &CmndShutterMode, &CmndShutterPwmRange, &CmndShutterSetHalfway, &CmndShutterSetClose, &CmndShutterSetOpen, &CmndShutterInvert, &CmndShutterCalibration , &CmndShutterMotorDelay, &CmndShutterFrequency, &CmndShutterButton, &CmndShutterLock, &CmndShutterEnableEndStopTime, &CmndShutterInvertWebButtons, &CmndShutterStopOpen, &CmndShutterStopClose, &CmndShutterStopToggle, &CmndShutterStopToggleDir, &CmndShutterStopPosition}; @@ -58,53 +73,88 @@ void (* const ShutterCommand[])(void) PROGMEM = { Ticker TickerShutter; struct SHUTTER { - power_t mask = 0; // bit mask with 11 at the position of relays that belong to at least ONE shutter - power_t old_power = 0; // preserve old bitmask for power to extract the relay that changes. - power_t switched_relay = 0; // bitmatrix that contain the relays that was lastly changed. - uint32_t time[MAX_SHUTTERS]; // operating time of the shutter in 0.05sec - int32_t open_max[MAX_SHUTTERS]; // max value on maximum open calculated - int32_t target_position[MAX_SHUTTERS]; // position to go to - int32_t start_position[MAX_SHUTTERS]; // position before a movement is started. init at start - int32_t real_position[MAX_SHUTTERS]; // value between 0 and Shutter.open_max - uint16_t open_time[MAX_SHUTTERS]; // duration to open the shutter. 112 = 11.2sec - uint16_t close_time[MAX_SHUTTERS]; // duration to close the shutter. 112 = 11.2sec - uint16_t close_velocity[MAX_SHUTTERS]; // in relation to open velocity. higher value = faster - int8_t direction[MAX_SHUTTERS]; // 1 == UP , 0 == stop; -1 == down - int8_t lastdirection[MAX_SHUTTERS]; // last direction (1 == UP , -1 == down) - uint8_t mode = 0; // operation mode definition. see enum type above SHT_OFF_OPEN__OFF_CLOSE, SHT_OFF_ON__OPEN_CLOSE, SHT_PULSE_OPEN__PULSE_CLOSE - int16_t motordelay[MAX_SHUTTERS]; // initial motorstarttime in 0.05sec. - int16_t pwm_frequency[MAX_SHUTTERS]; // frequency of PWN for stepper motors - uint16_t max_pwm_frequency = 1000; // maximum of PWM frequency for openig the shutter. depend on the motor and drivers - uint16_t max_close_pwm_frequency[MAX_SHUTTERS];// maximum of PWM frequency for closeing the shutter. depend on the motor and drivers - uint8_t skip_relay_change; // avoid overrun at endstops - int32_t accelerator[MAX_SHUTTERS]; // speed of ramp-up, ramp down of shutter - uint8_t start_reported = 0; -} Shutter; + uint32_t time; // operating time of the shutter in 0.05sec + int32_t open_max; // max value on maximum open calculated + int32_t target_position; // position to go to + int32_t start_position; // position before a movement is started. init at start + int32_t real_position; // value between 0 and Shutter[i].open_max + uint16_t open_time; // duration to open the Shutter[i]. 112 = 11.2sec + uint16_t close_time; // duration to close the Shutter[i]. 112 = 11.2sec + uint16_t close_velocity; // in relation to open velocity. higher value = faster + int8_t direction; // 1 == UP , 0 == stop; -1 == down + int8_t lastdirection; // last direction (1 == UP , -1 == down) + uint8_t switch_mode; // how to switch relays: SHT_SWITCH, SHT_PULSE + int16_t motordelay; // initial motorstarttime in 0.05sec. Also uses for ramp at steppers and servos + int16_t pwm_velocity; // frequency of PWN for stepper motors or PWM duty cycle change for PWM servo + uint16_t pwm_value; // dutyload of PWM 0..1023 on ESP8266 + uint16_t close_velocity_max; // maximum of PWM change during closeing. Defines velocity on opening. Steppers and Servos only + int32_t accelerator; // speed of ramp-up, ramp down of shutters with velocity control. Steppers and Servos only +} Shutter[MAX_SHUTTERS]; + +struct SHUTTERGLOBAL { + power_t RelayShutterMask = 0; // bit mask with 11 at the position of relays that belong to at least ONE shutter + power_t RelayOldMask = 0; // bitmatrix that contain the last known state of all relays. Required to detemine the manual changed relay. + power_t RelayCurrentMask = 0; // bitmatrix that contain the current state of all relays + uint8_t position_mode = 0; // how to calculate actual position: SHT_TIME, SHT_COUNTER, SHT_PWM_VALUE, SHT_PWM_TIME + uint8_t skip_relay_change; // avoid overrun at endstops + uint8_t start_reported = 0; // indicates of the shutter start was reported through MQTT JSON + uint16_t open_velocity_max = 1000; // maximum of PWM change during opening. Defines velocity on opening. Steppers and Servos only +} ShutterGlobal; + +#define SHT_DIV_ROUND(__A, __B) (((__A) + (__B)/2) / (__B)) void ShutterLogPos(uint32_t i) { char stemp2[10]; - dtostrfd((float)Shutter.time[i] / steps_per_second, 2, stemp2); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Shutter%d Real %d, Start %d, Stop %d, Dir %d, Delay %d, Rtc %s [s], Freq %d"), - i+1, Shutter.real_position[i], Shutter.start_position[i], Shutter.target_position[i], Shutter.direction[i], Shutter.motordelay[i], stemp2, Shutter.pwm_frequency[i]); + dtostrfd((float)Shutter[i].time / STEPS_PER_SECOND, 2, stemp2); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Shutter %d Real %d, Start %d, Stop %d, Dir %d, Delay %d, Rtc %s [s], Freq %d, PWM %d"), + i+1, Shutter[i].real_position, Shutter[i].start_position, Shutter[i].target_position, Shutter[i].direction, Shutter[i].motordelay, stemp2, Shutter[i].pwm_velocity, Shutter[i].pwm_value); +} + +void ExecuteCommandPowerShutter(uint32_t device, uint32_t state, uint32_t source) +{ + // first implementation for virtual relays. Avoid switching relay numbers that do not exist. + if (device <= devices_present) ExecuteCommandPower(device,state,source); +} + +void ShutterUpdateVelocity(uint8_t i) +{ + // No Logging allowed. Part of RTC Timer + // will be calles through RTC every 50ms. + Shutter[i].pwm_velocity += Shutter[i].accelerator; + Shutter[i].pwm_velocity = tmax(0,tmin(Shutter[i].direction==1 ? ShutterGlobal.open_velocity_max : Shutter[i].close_velocity_max,Shutter[i].pwm_velocity)); } void ShutterRtc50mS(void) { + // No Logging allowed. RTC Timer for (uint8_t i = 0; i < shutters_present; i++) { - Shutter.time[i]++; - if (Shutter.accelerator[i]) { - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: accelerator i=%d -> %d"),i, Shutter.accelerator[i]); - Shutter.pwm_frequency[i] += Shutter.accelerator[i]; - Shutter.pwm_frequency[i] = tmax(0,tmin(Shutter.direction[i]==1 ? Shutter.max_pwm_frequency : Shutter.max_close_pwm_frequency[i],Shutter.pwm_frequency[i])); - analogWriteFreq(Shutter.pwm_frequency[i]); - analogWrite(Pin(GPIO_PWM1, i), 50); - } + if (Shutter[i].direction) { + // update position data before increasing counter + Shutter[i].real_position = ShutterCalculatePosition(i); + Shutter[i].time++; + ShutterCalculateAccelerator(i); + switch (ShutterGlobal.position_mode) { + case SHT_PWM_VALUE: + ShutterUpdateVelocity(i); + Shutter[i].real_position += Shutter[i].direction > 0 ? Shutter[i].pwm_velocity : (Shutter[i].direction < 0 ? -Shutter[i].pwm_velocity : 0); + Shutter[i].pwm_value = SHT_DIV_ROUND((Settings.shutter_pwmrange[1][i]-Settings.shutter_pwmrange[0][i]) * Shutter[i].real_position , Shutter[i].open_max)+Settings.shutter_pwmrange[0][i]; + analogWrite(Pin(GPIO_PWM1, i), Shutter[i].pwm_value); + break; + + case SHT_COUNTER: + if (Shutter[i].accelerator) { + //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: accelerator i=%d -> %d"),i, Shutter[i].accelerator); + ShutterUpdateVelocity(i); + analogWriteFreq(Shutter[i].pwm_velocity); + analogWrite(Pin(GPIO_PWM1, i), 50); + } + break; + } + } // if (Shutter[i].direction) } } -#define SHT_DIV_ROUND(__A, __B) (((__A) + (__B)/2) / (__B)) - int32_t ShutterPercentToRealPosition(uint32_t percent, uint32_t index) { if (Settings.shutter_set50percent[index] != 50) { @@ -120,17 +170,17 @@ int32_t ShutterPercentToRealPosition(uint32_t percent, uint32_t index) } } } - for (uint32_t i = 0; i < 5; i++) { - if ((percent * 10) >= Settings.shuttercoeff[i][index]) { - realpos = SHT_DIV_ROUND(Shutter.open_max[index] * calibrate_pos[i+1], 100); + for (uint32_t k = 0; k < 5; k++) { + if ((percent * 10) >= Settings.shuttercoeff[k][index]) { + realpos = SHT_DIV_ROUND(Shutter[index].open_max * calibrate_pos[k+1], 100); //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Realposition TEMP1: %d, %% %d, coeff %d"), realpos, percent, Settings.shuttercoeff[i][index]); } else { - if (0 == i) { - realpos = SHT_DIV_ROUND(SHT_DIV_ROUND(percent * Shutter.open_max[index] * calibrate_pos[i+1], Settings.shuttercoeff[i][index]), 10); + if (0 == k) { + realpos = SHT_DIV_ROUND(SHT_DIV_ROUND(percent * Shutter[index].open_max * calibrate_pos[k+1], Settings.shuttercoeff[k][index]), 10); } else { //uint16_t addon = ( percent*10 - Settings.shuttercoeff[i-1][index] ) * Shutter_Open_Max[index] * (calibrate_pos[i+1] - calibrate_pos[i]) / (Settings.shuttercoeff[i][index] -Settings.shuttercoeff[i-1][index]) / 100; //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Realposition TEMP2: %d, %% %d, coeff %d"), addon, (calibrate_pos[i+1] - calibrate_pos[i]), (Settings.shuttercoeff[i][index] -Settings.shuttercoeff[i-1][index])); - realpos += SHT_DIV_ROUND(SHT_DIV_ROUND((percent*10 - Settings.shuttercoeff[i-1][index] ) * Shutter.open_max[index] * (calibrate_pos[i+1] - calibrate_pos[i]), Settings.shuttercoeff[i][index] - Settings.shuttercoeff[i-1][index]), 100); + realpos += SHT_DIV_ROUND(SHT_DIV_ROUND((percent*10 - Settings.shuttercoeff[k-1][index] ) * Shutter[index].open_max * (calibrate_pos[k+1] - calibrate_pos[k]), Settings.shuttercoeff[k][index] - Settings.shuttercoeff[k-1][index]), 100); } break; } @@ -146,18 +196,18 @@ uint8_t ShutterRealToPercentPosition(int32_t realpos, uint32_t index) } else { uint16_t realpercent; - for (uint32_t i = 0; i < 5; i++) { - if (realpos >= Shutter.open_max[index] * calibrate_pos[i+1] / 100) { - realpercent = SHT_DIV_ROUND(Settings.shuttercoeff[i][index], 10); + for (uint32_t j = 0; j < 5; j++) { + if (realpos >= Shutter[index].open_max * calibrate_pos[j+1] / 100) { + realpercent = SHT_DIV_ROUND(Settings.shuttercoeff[j][index], 10); //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Realpercent TEMP1: %d, %% %d, coeff %d"), realpercent, realpos, Shutter_Open_Max[index] * calibrate_pos[i+1] / 100); } else { - if (0 == i) { - realpercent = SHT_DIV_ROUND(SHT_DIV_ROUND((realpos - SHT_DIV_ROUND(Shutter.open_max[index] * calibrate_pos[i], 100)) * 10 * Settings.shuttercoeff[i][index], calibrate_pos[i+1]), Shutter.open_max[index]); + if (0 == j) { + realpercent = SHT_DIV_ROUND(SHT_DIV_ROUND((realpos - SHT_DIV_ROUND(Shutter[index].open_max * calibrate_pos[j], 100)) * 10 * Settings.shuttercoeff[j][index], calibrate_pos[j+1]), Shutter[index].open_max); } else { //uint16_t addon = ( realpos - (Shutter_Open_Max[index] * calibrate_pos[i] / 100) ) * 10 * (Settings.shuttercoeff[i][index] - Settings.shuttercoeff[i-1][index]) / (calibrate_pos[i+1] - calibrate_pos[i])/ Shutter_Open_Max[index]; //uint16_t addon = ( percent*10 - Settings.shuttercoeff[i-1][index] ) * Shutter_Open_Max[index] * (calibrate_pos[i+1] - calibrate_pos[i]) / (Settings.shuttercoeff[i][index] -Settings.shuttercoeff[i-1][index]) / 100; //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Realpercent TEMP2: %d, delta %d, %% %d, coeff %d"), addon,( realpos - (Shutter_Open_Max[index] * calibrate_pos[i] / 100) ) , (calibrate_pos[i+1] - calibrate_pos[i])* Shutter_Open_Max[index]/100, (Settings.shuttercoeff[i][index] -Settings.shuttercoeff[i-1][index])); - realpercent += SHT_DIV_ROUND(SHT_DIV_ROUND((realpos - SHT_DIV_ROUND(Shutter.open_max[index] * calibrate_pos[i], 100)) * 10 * (Settings.shuttercoeff[i][index] - Settings.shuttercoeff[i-1][index]), (calibrate_pos[i+1] - calibrate_pos[i])), Shutter.open_max[index]) ; + realpercent += SHT_DIV_ROUND(SHT_DIV_ROUND((realpos - SHT_DIV_ROUND(Shutter[index].open_max * calibrate_pos[j], 100)) * 10 * (Settings.shuttercoeff[j][index] - Settings.shuttercoeff[j-1][index]), (calibrate_pos[j+1] - calibrate_pos[j])), Shutter[index].open_max) ; } break; } @@ -169,14 +219,14 @@ uint8_t ShutterRealToPercentPosition(int32_t realpos, uint32_t index) void ShutterInit(void) { shutters_present = 0; - Shutter.mask = 0; + ShutterGlobal.RelayShutterMask = 0; //Initialize to get relay that changed - Shutter.old_power = power; - bool relay_in_interlock = false; + ShutterGlobal.RelayOldMask = power; + // if shutter 4 is unused if (Settings.shutter_startrelay[MAX_SHUTTERS -1] == 0) { - Shutter.max_pwm_frequency = Settings.shuttercoeff[4][3] > 0 ? Settings.shuttercoeff[4][3] : Shutter.max_pwm_frequency; + ShutterGlobal.open_velocity_max = Settings.shuttercoeff[4][3] > 0 ? Settings.shuttercoeff[4][3] : ShutterGlobal.open_velocity_max; } for (uint32_t i = 0; i < MAX_SHUTTERS; i++) { // set startrelay to 1 on first init, but only to shutter 1. 90% usecase @@ -184,68 +234,89 @@ void ShutterInit(void) if (Settings.shutter_startrelay[i] && (Settings.shutter_startrelay[i] < 9)) { shutters_present++; - // Determine shutter types - Shutter.mask |= 3 << (Settings.shutter_startrelay[i] -1) ; + // Add the two relays to the mask to knaw they belong to shutters + ShutterGlobal.RelayShutterMask |= 3 << (Settings.shutter_startrelay[i] -1) ; - for (uint32_t j = 0; j < MAX_INTERLOCKS * Settings.flag.interlock; j++) { // CMND_INTERLOCK - Enable/disable interlock - //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Interlock state i=%d %d, flag %d, , shuttermask %d, maskedIL %d"),i, Settings.interlock[i], Settings.flag.interlock,Shutter.mask, Settings.interlock[i]&Shutter.mask); - if (Settings.interlock[j] && (Settings.interlock[j] & Shutter.mask)) { - //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Relay in Interlock group")); - relay_in_interlock = true; - } + // All shutters must have same mode. Switch OR Pulse. N + switch (Settings.pulse_timer[i]) { + case 0: + Shutter[i].switch_mode = SHT_SWITCH; + break; + default: + Shutter[i].switch_mode = SHT_PULSE; + break; } - if (relay_in_interlock) { - if (Settings.pulse_timer[i] > 0) { - Shutter.mode = SHT_PULSE_OPEN__PULSE_CLOSE; + + if (Settings.shutter_mode == SHT_UNDEF) { + bool relay_in_interlock = false; + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: mode undef.. calculate...")); + + for (uint32_t j = 0; j < MAX_INTERLOCKS * Settings.flag.interlock; j++) { // CMND_INTERLOCK - Enable/disable interlock + //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Interlock state i=%d %d, flag %d, , shuttermask %d, maskedIL %d"),i, Settings.interlock[i], Settings.flag.interlock,ShutterGlobal.RelayShutterMask, Settings.interlock[i]&ShutterGlobal.RelayShutterMask); + if (Settings.interlock[j] && (Settings.interlock[j] & ShutterGlobal.RelayShutterMask)) { + //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Relay in Interlock group")); + relay_in_interlock = true; + } + } + + if (relay_in_interlock) { + ShutterGlobal.position_mode = SHT_TIME; } else { - Shutter.mode = SHT_OFF_OPEN__OFF_CLOSE; + ShutterGlobal.position_mode = SHT_TIME_UP_DOWN; + if (PinUsed(GPIO_PWM1, i) && PinUsed(GPIO_CNTR1, i)) { + ShutterGlobal.position_mode = SHT_COUNTER; + } } + } else { - Shutter.mode = SHT_OFF_ON__OPEN_CLOSE; - if (PinUsed(GPIO_PWM1, i) && PinUsed(GPIO_CNTR1, i)) { - Shutter.mode = SHT_OFF_ON__OPEN_CLOSE_STEPPER; - Shutter.pwm_frequency[i] = 0; - Shutter.accelerator[i] = 0; - analogWriteFreq(Shutter.pwm_frequency[i]); - analogWrite(Pin(GPIO_PWM1, i), 0); -// ExecuteCommandPower(Settings.shutter_startrelay[i]+2, 0, SRC_SHUTTER); - } + ShutterGlobal.position_mode = Settings.shutter_mode; } + // main function for stepper and servos to control velocity and acceleration. TickerShutter.attach_ms(50, ShutterRtc50mS ); + // default the 50 percent should not have any impact without changing it. set to 60 Settings.shutter_set50percent[i] = (Settings.shutter_set50percent[i] > 0) ? Settings.shutter_set50percent[i] : 50; // use 10 sec. as default to allow everybody to play without deep initialize - Shutter.open_time[i] = (Settings.shutter_opentime[i] > 0) ? Settings.shutter_opentime[i] : 100; - Shutter.close_time[i] = (Settings.shutter_closetime[i] > 0) ? Settings.shutter_closetime[i] : 100; + Shutter[i].open_time = Settings.shutter_opentime[i] = (Settings.shutter_opentime[i] > 0) ? Settings.shutter_opentime[i] : 100; + Shutter[i].close_time = Settings.shutter_closetime[i] = (Settings.shutter_closetime[i] > 0) ? Settings.shutter_closetime[i] : 100; - // Update Calculation 20 because time interval is 0.05 sec - Shutter.open_max[i] = 200 * Shutter.open_time[i]; - Shutter.close_velocity[i] = Shutter.open_max[i] / Shutter.close_time[i] / 2 ; - Shutter.max_close_pwm_frequency[i] = Shutter.max_pwm_frequency*Shutter.open_time[i] / Shutter.close_time[i]; - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d Closefreq: %d"),i, Shutter.max_close_pwm_frequency[i]); + // Update Calculation 20 because time interval is 0.05 sec ans time is in 0.1sec + Shutter[i].open_max = STEPS_PER_SECOND * RESOLUTION * Shutter[i].open_time / 10; + Shutter[i].close_velocity = Shutter[i].open_max / Shutter[i].close_time / 2 ; // calculate a ramp slope at the first 5 percent to compensate that shutters move with down part later than the upper part if (Settings.shutter_set50percent[i] != 50) { - Settings.shuttercoeff[1][i] = Shutter.open_max[i] * (100 - Settings.shutter_set50percent[i] ) / 5000; - Settings.shuttercoeff[0][i] = Shutter.open_max[i] - (Settings.shuttercoeff[1][i] * 100); + Settings.shuttercoeff[1][i] = Shutter[i].open_max * (100 - Settings.shutter_set50percent[i] ) / 5000; + Settings.shuttercoeff[0][i] = Shutter[i].open_max - (Settings.shuttercoeff[1][i] * 100); Settings.shuttercoeff[2][i] = (Settings.shuttercoeff[0][i] + 5 * Settings.shuttercoeff[1][i]) / 5; } - Shutter.mask |= 3 << (Settings.shutter_startrelay[i] -1); + ShutterGlobal.RelayShutterMask |= 3 << (Settings.shutter_startrelay[i] -1); - Shutter.real_position[i] = ShutterPercentToRealPosition(Settings.shutter_position[i], i); - //Shutter.real_position[i] = Settings.shutter_position[i] <= 5 ? Settings.shuttercoeff[2][i] * Settings.shutter_position[i] : Settings.shuttercoeff[1][i] * Settings.shutter_position[i] + Settings.shuttercoeff[0,i]; - Shutter.start_position[i] = Shutter.target_position[i] = Shutter.real_position[i]; - Shutter.motordelay[i] = Settings.shutter_motordelay[i]; - Shutter.lastdirection[i] = (50 < Settings.shutter_position[i]) ? 1 : -1; + Shutter[i].real_position = ShutterPercentToRealPosition(Settings.shutter_position[i], i); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT%d: Init. Pos: %d,inverted %d, locked %d, end stop time enabled %d, webButtons inverted %d, shuttermode %d"), - i+1, Shutter.real_position[i], - (Settings.shutter_options[i]&1) ? 1 : 0, (Settings.shutter_options[i]&2) ? 1 : 0, (Settings.shutter_options[i]&4) ? 1 : 0, (Settings.shutter_options[i]&8) ? 1 : 0, Shutter.mode); + Shutter[i].start_position = Shutter[i].target_position = Shutter[i].real_position; + Shutter[i].motordelay = Settings.shutter_motordelay[i]; + Shutter[i].lastdirection = (50 < Settings.shutter_position[i]) ? 1 : -1; + + switch (ShutterGlobal.position_mode) { + case SHT_PWM_VALUE: + ShutterGlobal.open_velocity_max = RESOLUTION; + // Initiate pwm range with defaults if not already set. + Settings.shutter_pwmrange[0][i] = Settings.shutter_pwmrange[0][i] > 0 ? Settings.shutter_pwmrange[0][i] : pwm_min; + Settings.shutter_pwmrange[1][i] = Settings.shutter_pwmrange[1][i] > 0 ? Settings.shutter_pwmrange[1][i] : pwm_max; + break; + } + Shutter[i].close_velocity_max = ShutterGlobal.open_velocity_max*Shutter[i].open_time / Shutter[i].close_time; + + //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d Openvel %d, Closevel: %d"),i, ShutterGlobal.open_velocity_max, Shutter[i].close_velocity_max); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT%d: Init. Pos: %d,inverted %d, locked %d, end stop time enabled %d, webButtons inverted %d"), + i+1, Shutter[i].real_position, + (Settings.shutter_options[i]&1) ? 1 : 0, (Settings.shutter_options[i]&2) ? 1 : 0, (Settings.shutter_options[i]&4) ? 1 : 0, (Settings.shutter_options[i]&8) ? 1 : 0); } else { - // terminate loop at first INVALID shutter. + // terminate loop at first INVALID Shutter[i]. break; } ShutterLimitRealAndTargetPositions(i); @@ -264,30 +335,135 @@ void ShutterReportPosition(bool always, uint32_t index) n = index+1; } for (i; i < n; i++) { - //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Shutter %d: Real Pos: %d"), i+1,Shutter.real_position[i]); - uint32_t position = ShutterRealToPercentPosition(Shutter.real_position[i], i); - if (Shutter.direction[i] != 0) { + //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Shutter %d: Real Pos: %d"), i+1,Shutter[i].real_position); + uint32_t position = ShutterRealToPercentPosition(Shutter[i].real_position, i); + if (Shutter[i].direction != 0) { rules_flag.shutter_moving = 1; ShutterLogPos(i); } if (i && index == MAX_SHUTTERS) { ResponseAppend_P(PSTR(",")); } - uint32_t target = ShutterRealToPercentPosition(Shutter.target_position[i], i); - ResponseAppend_P(JSON_SHUTTER_POS, i+1, (Settings.shutter_options[i] & 1) ? 100-position : position, Shutter.direction[i],(Settings.shutter_options[i] & 1) ? 100-target : target ); + uint32_t target = ShutterRealToPercentPosition(Shutter[i].target_position, i); + ResponseAppend_P(JSON_SHUTTER_POS, i+1, (Settings.shutter_options[i] & 1) ? 100-position : position, Shutter[i].direction,(Settings.shutter_options[i] & 1) ? 100-target : target ); } ResponseJsonEnd(); if (always || (rules_flag.shutter_moving)) { MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_PRFX_SHUTTER)); // RulesProcess() now re-entry protected } - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: rules_flag.shutter_moving: %d, moved %d"), rules_flag.shutter_moving, rules_flag.shutter_moved); - } void ShutterLimitRealAndTargetPositions(uint32_t i) { - if (Shutter.real_position[i]<0) Shutter.real_position[i] = 0; - if (Shutter.real_position[i]>Shutter.open_max[i]) Shutter.real_position[i] = Shutter.open_max[i]; - if (Shutter.target_position[i]<0) Shutter.target_position[i] = 0; - if (Shutter.target_position[i]>Shutter.open_max[i]) Shutter.target_position[i] = Shutter.open_max[i]; + if (Shutter[i].real_position<0) Shutter[i].real_position = 0; + if (Shutter[i].real_position>Shutter[i].open_max) Shutter[i].real_position = Shutter[i].open_max; + if (Shutter[i].target_position<0) Shutter[i].target_position = 0; + if (Shutter[i].target_position>Shutter[i].open_max) Shutter[i].target_position = Shutter[i].open_max; +} + +void ShutterCalculateAccelerator(uint8_t i) +{ + // No Logging allowed. Part of RTC Timer + if (Shutter[i].direction != 0) { + switch (ShutterGlobal.position_mode) { + case SHT_COUNTER: + case SHT_PWM_VALUE: + // calculate max velocity allowed in this direction + velocity_max = Shutter[i].direction == 1 ? ShutterGlobal.open_velocity_max : Shutter[i].close_velocity_max; + // calculate max change of velocyty based on the defined motordelay in steps + velocity_change_per_step_max = velocity_max / (Shutter[i].motordelay>0 ? Shutter[i].motordelay : 1); + // minimumtime required from current velocity to stop + min_runtime_ms = Shutter[i].pwm_velocity * 1000 / STEPS_PER_SECOND / velocity_change_per_step_max; + // decellartion way from current velocity + current_stop_way = (min_runtime_ms * (Shutter[i].pwm_velocity+velocity_change_per_step_max)/100 - Shutter[i].pwm_velocity)*RESOLUTION/ShutterGlobal.open_velocity_max * Shutter[i].direction ; + next_possible_stop_position = Shutter[i].real_position + current_stop_way ; + toBeAcc = 0; + // ensure that the accelerotor kicks in at least one step BEFORE it is to late and a hard stop required. + if (Shutter[i].accelerator < 0 || (next_possible_stop_position * Shutter[i].direction) +RESOLUTION*Shutter[i].pwm_velocity/ShutterGlobal.open_velocity_max>= Shutter[i].target_position * Shutter[i].direction ) { + // 10 times the deviation is the p-value of this simple p-regulator + toBeAcc = 100+(Shutter[i].direction*(next_possible_stop_position-Shutter[i].target_position)*velocity_max/Shutter[i].pwm_velocity*10/RESOLUTION); + Shutter[i].accelerator = - tmin(tmax( velocity_change_per_step_max*toBeAcc/100 , (velocity_change_per_step_max*9/10)), (velocity_change_per_step_max*11/10)); + } else if ( Shutter[i].accelerator > 0 && Shutter[i].pwm_velocity == velocity_max) { + Shutter[i].accelerator = 0; + } + break; + } + } +} + +void ShutterDecellerateForStop(uint8_t i) +{ + switch (ShutterGlobal.position_mode) { + case SHT_PWM_VALUE: + case SHT_COUNTER: + int16_t missing_steps; + Shutter[i].accelerator = -(ShutterGlobal.open_velocity_max / (Shutter[i].motordelay>4 ? (Shutter[i].motordelay*11)/10 : 4) ); + while (Shutter[i].pwm_velocity > -2*Shutter[i].accelerator ) { + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: velocity: %ld, delta: %d"), Shutter[i].pwm_velocity, Shutter[i].accelerator ); + //Shutter[i].pwm_velocity = tmax(Shutter[i].pwm_velocity-Shutter[i].accelerator , 0); + // Control will be done in RTC Ticker. + delay(50); + } + if (ShutterGlobal.position_mode == SHT_COUNTER){ + missing_steps = ((Shutter[i].target_position-Shutter[i].start_position)*Shutter[i].direction*ShutterGlobal.open_velocity_max/RESOLUTION/STEPS_PER_SECOND) - RtcSettings.pulse_counter[i]; + //prepare for stop PWM + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Remain steps %d, counter %d, freq %d"), missing_steps, RtcSettings.pulse_counter[i] ,Shutter[i].pwm_velocity); + Shutter[i].accelerator = 0; + Shutter[i].pwm_velocity = Shutter[i].pwm_velocity > 250 ? 250 : Shutter[i].pwm_velocity; + analogWriteFreq(Shutter[i].pwm_velocity); + analogWrite(Pin(GPIO_PWM1, i), 50); + Shutter[i].pwm_velocity = 0; + analogWriteFreq(Shutter[i].pwm_velocity); + while (RtcSettings.pulse_counter[i] < (uint32_t)(Shutter[i].target_position-Shutter[i].start_position)*Shutter[i].direction*ShutterGlobal.open_velocity_max/RESOLUTION/STEPS_PER_SECOND) { + delay(1); + } + analogWrite(Pin(GPIO_PWM1, i), 0); // removed with 8.3 because of reset caused by watchog + Shutter[i].real_position = ShutterCalculatePosition(i); + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Real %d, pulsecount %d, start %d"), Shutter[i].real_position,RtcSettings.pulse_counter[i], Shutter[i].start_position); + + } + Shutter[i].direction = 0; + Shutter[i].pwm_velocity = 0; + break; + } +} + +void ShutterPowerOff(uint8_t i) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Stop Shutter %d. Switchmode %d"), i,Shutter[i].switch_mode); + ShutterDecellerateForStop(i); + if (Shutter[i].direction !=0) { + Shutter[i].direction = 0; + delay(MOTOR_STOP_TIME); + } + switch (Shutter[i].switch_mode) { + case SHT_SWITCH: + if ((1 << (Settings.shutter_startrelay[i]-1)) & power) { + ExecuteCommandPowerShutter(Settings.shutter_startrelay[i], 0, SRC_SHUTTER); + } + if ((1 << (Settings.shutter_startrelay[i])) & power) { + ExecuteCommandPowerShutter(Settings.shutter_startrelay[i]+1, 0, SRC_SHUTTER); + } + break; + case SHT_PULSE: + uint8_t cur_relay = Settings.shutter_startrelay[i] + (Shutter[i].direction == 1 ? 0 : (uint8_t)(ShutterGlobal.position_mode == SHT_TIME)) ; + // we have a momentary switch here. Needs additional pulse on same relay after the end + if ((SRC_PULSETIMER == last_source || SRC_SHUTTER == last_source || SRC_WEBGUI == last_source)) { + ExecuteCommandPowerShutter(cur_relay, 1, SRC_SHUTTER); + // switch off direction relay to make it power less + if ((1 << (Settings.shutter_startrelay[i])) & power) { + ExecuteCommandPowerShutter(Settings.shutter_startrelay[i]+1, 0, SRC_SHUTTER); + } + } else { + last_source = SRC_SHUTTER; + } + break; + } + // Store current PWM value to ensure proper position after reboot. + switch (ShutterGlobal.position_mode) { + case SHT_PWM_VALUE: + char scmnd[20]; + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_PWM " %d" ),Shutter[i].pwm_value); + ExecuteCommand(scmnd, SRC_BUTTON); + break; + } } void ShutterUpdatePosition(void) @@ -295,113 +471,33 @@ void ShutterUpdatePosition(void) char scommand[CMDSZ]; char stopic[TOPSZ]; - for (uint32_t i = 0; i < shutters_present; i++) { - if (Shutter.direction[i] != 0) { - int32_t stop_position_delta = 20; - // Calculate position with counter. Much more accurate and no need for motordelay workaround - // adding some steps to stop early - Shutter.real_position[i] = ShutterCounterBasedPosition(i); - if (!Shutter.start_reported) { + if (Shutter[i].direction != 0) { + if (!ShutterGlobal.start_reported) { ShutterReportPosition(true, i); XdrvRulesProcess(); - Shutter.start_reported = 1; + ShutterGlobal.start_reported = 1; } + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: time: %d, toBeAcc %d, current_stop_way %d,vel_cur %d, vel_max %d, act_vel_change %d, min_runtime_ms %d, act.pos %d, next_stop %d, target: %d, max_vel_change %d, dir: %d"),Shutter[i].time,toBeAcc,current_stop_way, + Shutter[i].pwm_velocity,velocity_max, Shutter[i].accelerator,min_runtime_ms,Shutter[i].real_position, next_possible_stop_position,Shutter[i].target_position,velocity_change_per_step_max,Shutter[i].direction); - if (Shutter.mode == SHT_OFF_ON__OPEN_CLOSE_STEPPER) { - int32_t max_frequency = Shutter.direction[i] == 1 ? Shutter.max_pwm_frequency : Shutter.max_close_pwm_frequency[i]; - int32_t max_freq_change_per_sec = Shutter.max_pwm_frequency*steps_per_second / (Shutter.motordelay[i]>0 ? Shutter.motordelay[i] : 1); - int32_t min_runtime_ms = Shutter.pwm_frequency[i]*1000 / max_freq_change_per_sec; - int32_t velocity = Shutter.direction[i] == 1 ? 100 : Shutter.close_velocity[i]; - int32_t minstopway = min_runtime_ms * velocity / 100 * Shutter.pwm_frequency[i] / max_frequency * Shutter.direction[i] ; - int32_t next_possible_stop = Shutter.real_position[i] + minstopway ; - stop_position_delta =200 * Shutter.pwm_frequency[i]/max_frequency + Shutter.direction[i] * (next_possible_stop - Shutter.target_position[i]); - - //Shutter.accelerator[i] = tmin(tmax(max_freq_change_per_sec*(100-(Shutter.direction[i]*(Shutter.target_position[i]-next_possible_stop) ))/2000 , max_freq_change_per_sec*9/200), max_freq_change_per_sec*11/200); - //int32_t act_freq_change = max_freq_change_per_sec/20; - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: time: %d, velocity %d, minstopway %d,cur_freq %d, max_frequency %d, act_freq_change %d, min_runtime_ms %d, act.pos %d, next_stop %d, target: %d"),Shutter.time[i],velocity,minstopway, - Shutter.pwm_frequency[i],max_frequency, Shutter.accelerator[i],min_runtime_ms,Shutter.real_position[i], next_possible_stop,Shutter.target_position[i]); - - if (Shutter.accelerator[i] < 0 || next_possible_stop * Shutter.direction[i] > (Shutter.target_position[i]- (100 * Shutter.direction[i])) * Shutter.direction[i] ) { - - Shutter.accelerator[i] = - tmin(tmax(max_freq_change_per_sec*(100-(Shutter.direction[i]*(Shutter.target_position[i]-next_possible_stop) ))/2000 , max_freq_change_per_sec*9/200), max_freq_change_per_sec*11/200); - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Ramp down: acc: %d"), Shutter.accelerator[i]); - } else if ( Shutter.accelerator[i] > 0 && Shutter.pwm_frequency[i] == max_frequency) { - Shutter.accelerator[i] = 0; - } - } else { - Shutter.real_position[i] = Shutter.start_position[i] + ( (Shutter.time[i] - Shutter.motordelay[i]) * (Shutter.direction[i] > 0 ? 100 : -Shutter.close_velocity[i])); - } - if ( Shutter.real_position[i] * Shutter.direction[i] + stop_position_delta >= Shutter.target_position[i] * Shutter.direction[i] ) { - // calculate relay number responsible for current movement. - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Stop Condition detected: real: %d, Target: %d, direction: %d"),Shutter.real_position[i], Shutter.target_position[i],Shutter.direction[i]); - uint8_t cur_relay = Settings.shutter_startrelay[i] + (Shutter.direction[i] == 1 ? 0 : 1) ; - int16_t missing_steps; - - switch (Shutter.mode) { - case SHT_PULSE_OPEN__PULSE_CLOSE: - // we have a momentary switch here. Needs additional pulse on same relay after the end - if (SRC_PULSETIMER == last_source || SRC_SHUTTER == last_source || SRC_WEBGUI == last_source) { - ExecuteCommandPower(cur_relay, 1, SRC_SHUTTER); - } else { - last_source = SRC_SHUTTER; - } - break; - case SHT_OFF_ON__OPEN_CLOSE_STEPPER: - missing_steps = ((Shutter.target_position[i]-Shutter.start_position[i])*Shutter.direction[i]*Shutter.max_pwm_frequency/2000) - RtcSettings.pulse_counter[i]; - //prepare for stop PWM - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Remain steps %d, counter %d, freq %d"), missing_steps, RtcSettings.pulse_counter[i] ,Shutter.pwm_frequency[i]); - Shutter.accelerator[i] = 0; - Shutter.pwm_frequency[i] = Shutter.pwm_frequency[i] > 250 ? 250 : Shutter.pwm_frequency[i]; - analogWriteFreq(Shutter.pwm_frequency[i]); - analogWrite(Pin(GPIO_PWM1, i), 50); - Shutter.pwm_frequency[i] = 0; - analogWriteFreq(Shutter.pwm_frequency[i]); - while (RtcSettings.pulse_counter[i] < (uint32_t)(Shutter.target_position[i]-Shutter.start_position[i])*Shutter.direction[i]*Shutter.max_pwm_frequency/2000) { - delay(1); - } - analogWrite(Pin(GPIO_PWM1, i), 0); // removed with 8.3 because of reset caused by watchog -// ExecuteCommandPower(Settings.shutter_startrelay[i]+2, 0, SRC_SHUTTER); - Shutter.real_position[i] = ShutterCounterBasedPosition(i); - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Real %d, pulsecount %d, start %d"), Shutter.real_position[i],RtcSettings.pulse_counter[i], Shutter.start_position[i]); - - if ((1 << (Settings.shutter_startrelay[i]-1)) & power) { - ExecuteCommandPower(Settings.shutter_startrelay[i], 0, SRC_SHUTTER); - ExecuteCommandPower(Settings.shutter_startrelay[i]+1, 0, SRC_SHUTTER); - } - break; - case SHT_OFF_ON__OPEN_CLOSE: - if ((1 << (Settings.shutter_startrelay[i]-1)) & power) { - ExecuteCommandPower(Settings.shutter_startrelay[i], 0, SRC_SHUTTER); - ExecuteCommandPower(Settings.shutter_startrelay[i]+1, 0, SRC_SHUTTER); - } - break; - case SHT_OFF_OPEN__OFF_CLOSE: - // avoid switching OFF a relay already OFF - if ((1 << (cur_relay-1)) & power) { - // Relay is on and need to be switched off. - ExecuteCommandPower(cur_relay, 0, SRC_SHUTTER); - } - break; + if ( Shutter[i].real_position * Shutter[i].direction >= Shutter[i].target_position * Shutter[i].direction || Shutter[i].pwm_velocity0 ? Shutter.motordelay[i] : 1); - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Ramp up: %d"), Shutter.accelerator[i]); + Shutter[i].pwm_velocity = 0; + switch (ShutterGlobal.position_mode) { +#ifdef SHUTTER_STEPPER + case SHT_COUNTER: + analogWriteFreq(Shutter[i].pwm_velocity); + analogWrite(Pin(GPIO_PWM1, i), 0); + RtcSettings.pulse_counter[i] = 0; + break; +#endif } - Shutter.target_position[i] = target_pos; - Shutter.start_position[i] = Shutter.real_position[i]; - Shutter.time[i] = 0; - Shutter.skip_relay_change = 0; - Shutter.direction[i] = direction; + Shutter[i].accelerator = ShutterGlobal.open_velocity_max / (Shutter[i].motordelay>0 ? Shutter[i].motordelay : 1); + Shutter[i].target_position = target_pos; + Shutter[i].start_position = Shutter[i].real_position; + Shutter[i].time = 0; + ShutterGlobal.skip_relay_change = 0; + Shutter[i].direction = direction; rules_flag.shutter_moving = 1; rules_flag.shutter_moved = 0; - Shutter.start_reported = 0; - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: real %d, start %d, counter %d, max_freq %d, dir %d, freq %d"),Shutter.real_position[i], Shutter.start_position[i] ,RtcSettings.pulse_counter[i],Shutter.max_pwm_frequency , Shutter.direction[i] ,Shutter.max_pwm_frequency ); + ShutterGlobal.start_reported = 0; + //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: real %d, start %d, counter %d,freq_max %d, dir %d, freq %d"),Shutter[i].real_position, Shutter[i].start_position ,RtcSettings.pulse_counter[i],ShutterGlobal.open_velocity_max , Shutter[i].direction ,ShutterGlobal.open_velocity_max ); } - //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Start shutter: %d from %d to %d in directin %d"), i, Shutter.start_position[i], Shutter.target_position[i], Shutter.direction[i]); + //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Start shutter: %d from %d to %d in direction %d"), i, Shutter[i].start_position, Shutter[i].target_position, Shutter[i].direction); } -void ShutterWaitForMotorStop(uint32_t i) +int32_t ShutterCalculatePosition(uint32_t i) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Wait for Motorstop..")); - if ((SHT_OFF_ON__OPEN_CLOSE == Shutter.mode) || (SHT_OFF_ON__OPEN_CLOSE_STEPPER == Shutter.mode)) { - if (SHT_OFF_ON__OPEN_CLOSE_STEPPER == Shutter.mode) { - //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Frequency change %d"), Shutter.pwm_frequency); - while (Shutter.pwm_frequency[i] > 0) { - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Frequency: %ld, delta: %d"), Shutter.pwm_frequency[i], (int32_t)((Shutter.direction[i] == 1 ? Shutter.max_pwm_frequency : Shutter.max_close_pwm_frequency[i])/(Shutter.motordelay[i]+1)) ); - Shutter.pwm_frequency[i] = tmax(Shutter.pwm_frequency[i]-((Shutter.direction[i] == 1 ? Shutter.max_pwm_frequency : Shutter.max_close_pwm_frequency[i])/(Shutter.motordelay[i]+1)) , 0); - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Frequency: %ld"), Shutter.pwm_frequency[i]); - analogWriteFreq(Shutter.pwm_frequency[i]); - analogWrite(Pin(GPIO_PWM1, i), 50); - delay(50); + // No Logging allowed. Part of RTC Timer + if (Shutter[i].direction != 0) { + switch (ShutterGlobal.position_mode) { + case SHT_COUNTER: + return ((int32_t)RtcSettings.pulse_counter[i]*Shutter[i].direction*STEPS_PER_SECOND / ShutterGlobal.open_velocity_max * RESOLUTION)+Shutter[i].start_position; + break; + case SHT_TIME: + case SHT_TIME_UP_DOWN: + case SHT_TIME_GARAGE: + return Shutter[i].start_position + ( (Shutter[i].time - Shutter[i].motordelay) * (Shutter[i].direction > 0 ? RESOLUTION : -Shutter[i].close_velocity)); + break; + case SHT_PWM_TIME: + break; + case SHT_PWM_VALUE: + return Shutter[i].real_position; + break; + default: + break; } - analogWrite(Pin(GPIO_PWM1, i), 0); -// ExecuteCommandPower(Settings.shutter_startrelay[i]+2, 0, SRC_SHUTTER); - Shutter.real_position[i] = ShutterCounterBasedPosition(i); } else { - ExecuteCommandPower(Settings.shutter_startrelay[i], 0, SRC_SHUTTER); - delay(MOTOR_STOP_TIME); + return Shutter[i].real_position; } - } else { - delay(MOTOR_STOP_TIME); - } -} - -int32_t ShutterCounterBasedPosition(uint32_t i) -{ - return ((int32_t)RtcSettings.pulse_counter[i]*Shutter.direction[i]*2000 / Shutter.max_pwm_frequency)+Shutter.start_position[i]; } void ShutterRelayChanged(void) { - // Shutter.switched_relay = binary relay that was recently changed and cause an Action + // ShutterGlobal.RelayCurrentMask = binary relay that was recently changed and cause an Action // powerstate_local = binary powermatrix and relays from shutter: 0..3 // relays_changed = bool if one of the relays that belong to the shutter changed not by shutter or pulsetimer char stemp1[10]; for (uint32_t i = 0; i < shutters_present; i++) { power_t powerstate_local = (power >> (Settings.shutter_startrelay[i] -1)) & 3; - //uint8 manual_relays_changed = ((Shutter.switched_relay >> (Settings.shutter_startrelay[i] -1)) & 3) && SRC_IGNORE != last_source && SRC_SHUTTER != last_source && SRC_PULSETIMER != last_source ; - uint8 manual_relays_changed = ((Shutter.switched_relay >> (Settings.shutter_startrelay[i] -1)) & 3) && SRC_SHUTTER != last_source && SRC_PULSETIMER != last_source ; - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: source: %s, powerstate_local %ld, Shutter.switched_relay %d, manual change %d"), i+1, GetTextIndexed(stemp1, sizeof(stemp1), last_source, kCommandSource), powerstate_local,Shutter.switched_relay,manual_relays_changed); + // SRC_IGNORE added because INTERLOCK function bite causes this as last source for changing the relay. + //uint8 manual_relays_changed = ((ShutterGlobal.RelayCurrentMask >> (Settings.shutter_startrelay[i] -1)) & 3) && SRC_IGNORE != last_source && SRC_SHUTTER != last_source && SRC_PULSETIMER != last_source ; + uint8 manual_relays_changed = ((ShutterGlobal.RelayCurrentMask >> (Settings.shutter_startrelay[i] -1)) & 3) && SRC_SHUTTER != last_source && SRC_PULSETIMER != last_source ; + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: source: %s, powerstate_local %ld, ShutterGlobal.RelayCurrentMask %d, manual change %d"), i+1, GetTextIndexed(stemp1, sizeof(stemp1), last_source, kCommandSource), powerstate_local,ShutterGlobal.RelayCurrentMask,manual_relays_changed); if (manual_relays_changed) { - //Shutter.skip_relay_change = true; + //ShutterGlobal.skip_relay_change = true; ShutterLimitRealAndTargetPositions(i); - if (Shutter.mode == SHT_OFF_ON__OPEN_CLOSE || Shutter.mode == SHT_OFF_ON__OPEN_CLOSE_STEPPER) { - ShutterWaitForMotorStop(i); - switch (powerstate_local) { - case 1: - ShutterStartInit(i, 1, Shutter.open_max[i]); - break; - case 3: - ShutterStartInit(i, -1, 0); - break; - default: - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Switch OFF motor."),i); - Shutter.target_position[i] = Shutter.real_position[i]; - } - } else { - if (Shutter.direction[i] != 0 && (!powerstate_local || (powerstate_local && Shutter.mode == SHT_PULSE_OPEN__PULSE_CLOSE))) { - Shutter.target_position[i] = Shutter.real_position[i]; - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Switch OFF motor. Target: %ld, source: %s, powerstate_local %ld, Shutter.switched_relay %d, manual change %d"), i+1, Shutter.target_position[i], GetTextIndexed(stemp1, sizeof(stemp1), last_source, kCommandSource), powerstate_local,Shutter.switched_relay,manual_relays_changed); - } else { - last_source = SRC_SHUTTER; // avoid switch off in the next loop - if (powerstate_local == 2) { // testing on CLOSE relay, if ON - // close with relay two - ShutterWaitForMotorStop(i); - ShutterStartInit(i, -1, 0); - } else { - // opens with relay one - ShutterWaitForMotorStop(i); - ShutterStartInit(i, 1, Shutter.open_max[i]); - } - } - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Target: %ld, powerstatelocal %d"), i+1, Shutter.target_position[i], powerstate_local); - } - } - } + switch (Shutter[i].switch_mode ) { + case SHT_PULSE: + if (Shutter[i].direction != 0 && powerstate_local) { + Shutter[i].target_position = Shutter[i].real_position; + powerstate_local = 0; + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Switch OFF motor. Target: %ld, source: %s, powerstate_local %ld, ShutterGlobal.RelayCurrentMask %d, manual change %d"), i+1, Shutter[i].target_position, GetTextIndexed(stemp1, sizeof(stemp1), last_source, kCommandSource), powerstate_local,ShutterGlobal.RelayCurrentMask,manual_relays_changed); + } + break; + default: + last_source = SRC_SHUTTER; // avoid switch off in the next loop + if (Shutter[i].direction != 0 ) ShutterPowerOff(i); + } + switch (ShutterGlobal.position_mode) { + // enum Shutterposition_mode {SHT_TIME, SHT_TIME_UP_DOWN, SHT_TIME_GARAGE, SHT_COUNTER, SHT_PWM_VALUE, SHT_PWM_TIME,}; + case SHT_TIME_UP_DOWN: + case SHT_COUNTER: + case SHT_PWM_VALUE: + case SHT_PWM_TIME: + ShutterPowerOff(i); + switch (powerstate_local) { + case 1: + ShutterStartInit(i, 1, Shutter[i].open_max); + break; + case 3: + ShutterStartInit(i, -1, 0); + break; + default: + //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Switch OFF motor."),i); + Shutter[i].target_position = Shutter[i].real_position; + } + break; + case SHT_TIME: + switch (powerstate_local) { + case 1: + ShutterStartInit(i, 1, Shutter[i].open_max); + break; + case 2: + ShutterStartInit(i, -1, 0); + break; + default: + //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Switch OFF motor."),i); + Shutter[i].target_position = Shutter[i].real_position; + } + break; + case SHT_TIME_GARAGE: + switch (powerstate_local) { + case 1: + ShutterStartInit(i, Shutter[i].lastdirection*-1 , Shutter[i].lastdirection == 1 ? 0 : Shutter[i].open_max); + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d Garage. NewTarget %d"), i, Shutter[i].target_position); + break; + default: + Shutter[i].target_position = Shutter[i].real_position; + } + + + } // switch (ShutterGlobal.position_mode) + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Shutter %d: Target: %ld, powerstatelocal %d"), i+1, Shutter[i].target_position, powerstate_local); + } // if (manual_relays_changed) + } // for (uint32_t i = 0; i < shutters_present; i++) } bool ShutterButtonIsSimultaneousHold(uint32_t button_index, uint32_t shutter_index) { @@ -552,7 +673,7 @@ void ShutterButtonHandler(void) buttonState = SHT_PRESSED_MULTI; press_index = 1; } else { - if ((Shutter.direction[shutter_index]) && (Button.press_counter[button_index]==0)) { + if ((Shutter[shutter_index].direction) && (Button.press_counter[button_index]==0)) { buttonState = SHT_PRESSED_IMMEDIATE; press_index = 1; Button.press_counter[button_index] = 99; // Remember to discard further action for press & hold within button timings @@ -682,7 +803,7 @@ void ShutterButtonHandler(void) } else { uint8_t position = (Settings.shutter_button[button_index]>>(6*pos_press_index + 2)) & 0x03f; if (position) { - if (Shutter.direction[shutter_index]) { + if (Shutter[shutter_index].direction) { XdrvMailbox.payload = XdrvMailbox.index; CmndShutterStop(); } else { @@ -723,22 +844,22 @@ void ShutterSetPosition(uint32_t device, uint32_t position) { char svalue[32]; // Command and number parameter snprintf_P(svalue, sizeof(svalue), PSTR(D_PRFX_SHUTTER D_CMND_SHUTTER_POSITION "%d %d"), device, position); - ExecuteCommand(svalue, SRC_IGNORE); + ExecuteCommand(svalue, SRC_SHUTTER); } void ShutterToggle(bool dir) { - //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Payload toggle: %d, i %d"), XdrvMailbox.payload, XdrvMailbox.index); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Payload toggle: %d, i %d, dir %d"), XdrvMailbox.payload, XdrvMailbox.index, dir); if ((1 == XdrvMailbox.index) && (XdrvMailbox.payload != -99)) { XdrvMailbox.index = XdrvMailbox.payload; } if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { uint32_t index = XdrvMailbox.index-1; if (dir) { - XdrvMailbox.payload = (Shutter.lastdirection[index] > 0) ? 0 : 100; + XdrvMailbox.payload = (Shutter[index].lastdirection > 0) ? 0 : 100; } else { - XdrvMailbox.payload = (50 < ShutterRealToPercentPosition(Shutter.real_position[index], index)) ? 0 : 100; + XdrvMailbox.payload = (50 < ShutterRealToPercentPosition(Shutter[index].real_position, index)) ? 0 : 100; } XdrvMailbox.data_len = 0; last_source = SRC_WEBGUI; @@ -765,7 +886,7 @@ void CmndShutterStopOpen(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { uint32_t index = XdrvMailbox.index-1; - if (Shutter.direction[index]) { + if (Shutter[index].direction) { CmndShutterStop(); } else { CmndShutterOpen(); @@ -789,7 +910,7 @@ void CmndShutterStopClose(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { uint32_t index = XdrvMailbox.index-1; - if (Shutter.direction[index]) { + if (Shutter[index].direction) { CmndShutterStop(); } else { CmndShutterClose(); @@ -811,7 +932,7 @@ void CmndShutterStopToggle(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { uint32_t index = XdrvMailbox.index-1; - if (Shutter.direction[index]) { + if (Shutter[index].direction) { CmndShutterStop(); } else { CmndShutterToggle(); @@ -823,7 +944,7 @@ void CmndShutterStopToggleDir(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { uint32_t index = XdrvMailbox.index-1; - if (Shutter.direction[index]) { + if (Shutter[index].direction) { CmndShutterStop(); } else { CmndShutterToggleDir(); @@ -839,13 +960,12 @@ void CmndShutterStop(void) XdrvMailbox.index = XdrvMailbox.payload; } uint32_t i = XdrvMailbox.index -1; - if (Shutter.direction[i] != 0) { + if (Shutter[i].direction != 0) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Stop moving %d: dir: %d"), XdrvMailbox.index, Shutter.direction[i]); - // set stop position 10 steps ahead (0.5sec to allow normal stop) - int32_t temp_realpos = Shutter.start_position[i] + ( (Shutter.time[i]+10) * (Shutter.direction[i] > 0 ? 100 : -Shutter.close_velocity[i])); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Stop moving %d: dir: %d"), XdrvMailbox.index, Shutter[i].direction); + + int32_t temp_realpos = ShutterCalculatePosition(i); XdrvMailbox.payload = ShutterRealToPercentPosition(temp_realpos, i); - //XdrvMailbox.payload = Settings.shuttercoeff[2][i] * 5 > temp_realpos ? temp_realpos / Settings.shuttercoeff[2][i] : (temp_realpos-Settings.shuttercoeff[0,i]) / Settings.shuttercoeff[1][i]; last_source = SRC_WEBGUI; CmndShutterPosition(); } else { @@ -871,11 +991,11 @@ void CmndShutterPosition(void) // special handling fo UP,DOWN,TOGGLE,STOP command comming with payload -99 if ((XdrvMailbox.data_len > 1) && (XdrvMailbox.payload <= 0)) { //UpperCase(XdrvMailbox.data, XdrvMailbox.data); - if (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_UP) || !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_OPEN) || ((Shutter.direction[index]==0) && !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPOPEN))) { + if (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_UP) || !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_OPEN) || ((Shutter[index].direction==0) && !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPOPEN))) { CmndShutterOpen(); return; } - if (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_DOWN) || !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_CLOSE) || ((Shutter.direction[index]==0) && !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPCLOSE))) { + if (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_DOWN) || !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_CLOSE) || ((Shutter[index].direction==0) && !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPCLOSE))) { CmndShutterClose(); return; } @@ -887,70 +1007,76 @@ void CmndShutterPosition(void) CmndShutterToggleDir(); return; } - if (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOP) || ((Shutter.direction[index]) && (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPOPEN) || !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPCLOSE)))) { + if (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOP) || ((Shutter[index].direction) && (!strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPOPEN) || !strcasecmp(XdrvMailbox.data,D_CMND_SHUTTER_STOPCLOSE)))) { XdrvMailbox.payload = -99; CmndShutterStop(); return; } } - int8_t target_pos_percent = (XdrvMailbox.payload < 0) ? (XdrvMailbox.payload == -99 ? ShutterRealToPercentPosition(Shutter.real_position[index], index) : 0) : ((XdrvMailbox.payload > 100) ? 100 : XdrvMailbox.payload); + int8_t target_pos_percent = (XdrvMailbox.payload < 0) ? (XdrvMailbox.payload == -99 ? ShutterRealToPercentPosition(Shutter[index].real_position, index) : 0) : ((XdrvMailbox.payload > 100) ? 100 : XdrvMailbox.payload); // webgui still send also on inverted shutter the native position. target_pos_percent = ((Settings.shutter_options[index] & 1) && (SRC_WEBGUI != last_source)) ? 100 - target_pos_percent : target_pos_percent; if (XdrvMailbox.payload != -99) { //target_pos_percent = (Settings.shutter_options[index] & 1) ? 100 - target_pos_percent : target_pos_percent; - Shutter.target_position[index] = ShutterPercentToRealPosition(target_pos_percent, index); - //Shutter.accelerator[index] = Shutter.max_pwm_frequency / ((Shutter.motordelay[index] > 0) ? Shutter.motordelay[index] : 1); - //Shutter.target_position[index] = XdrvMailbox.payload < 5 ? Settings.shuttercoeff[2][index] * XdrvMailbox.payload : Settings.shuttercoeff[1][index] * XdrvMailbox.payload + Settings.shuttercoeff[0,index]; - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: lastsource %d:, real %d, target %d, payload %d"), last_source, Shutter.real_position[index] ,Shutter.target_position[index],target_pos_percent); + Shutter[index].target_position = ShutterPercentToRealPosition(target_pos_percent, index); + //Shutter[i].accelerator[index] = ShutterGlobal.open_velocity_max / ((Shutter[i].motordelay[index] > 0) ? Shutter[i].motordelay[index] : 1); + //Shutter[i].target_position[index] = XdrvMailbox.payload < 5 ? Settings.shuttercoeff[2][index] * XdrvMailbox.payload : Settings.shuttercoeff[1][index] * XdrvMailbox.payload + Settings.shuttercoeff[0,index]; + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: lastsource %d:, real %d, target %d, payload %d"), last_source, Shutter[index].real_position ,Shutter[index].target_position,target_pos_percent); } - if ( (target_pos_percent >= 0) && (target_pos_percent <= 100) && abs(Shutter.target_position[index] - Shutter.real_position[index] ) / Shutter.close_velocity[index] > 2) { + if ( (target_pos_percent >= 0) && (target_pos_percent <= 100) && abs(Shutter[index].target_position - Shutter[index].real_position ) / Shutter[index].close_velocity > 2) { if (Settings.shutter_options[index] & 4) { - if (0 == target_pos_percent) Shutter.target_position[index] -= 1 * 2000; - if (100 == target_pos_percent) Shutter.target_position[index] += 1 * 2000; + if (0 == target_pos_percent) Shutter[index].target_position -= 1 * RESOLUTION * STEPS_PER_SECOND; + if (100 == target_pos_percent) Shutter[index].target_position += 1 * RESOLUTION * STEPS_PER_SECOND; } - int8_t new_shutterdirection = Shutter.real_position[index] < Shutter.target_position[index] ? 1 : -1; - if (Shutter.direction[index] == -new_shutterdirection) { - // direction need to be changed. on momentary switches first stop the Shutter - if (SHT_PULSE_OPEN__PULSE_CLOSE == Shutter.mode) { - // code for momentary shutters only small switch on to stop Shutter - ExecuteCommandPower(Settings.shutter_startrelay[index] + ((new_shutterdirection == 1) ? 0 : 1), 1, SRC_SHUTTER); - delay(100); - } else { - if (SHT_OFF_OPEN__OFF_CLOSE == Shutter.mode) { - ExecuteCommandPower(Settings.shutter_startrelay[index] + ((new_shutterdirection == 1) ? 1 : 0), 0, SRC_SHUTTER); - ShutterWaitForMotorStop(index); - } - } + int8_t new_shutterdirection = Shutter[index].real_position < Shutter[index].target_position ? 1 : -1; + if (Shutter[index].direction == -new_shutterdirection) { + ShutterPowerOff(index); } - if (Shutter.direction[index] != new_shutterdirection) { - if ((SHT_OFF_ON__OPEN_CLOSE == Shutter.mode) || (SHT_OFF_ON__OPEN_CLOSE_STEPPER == Shutter.mode)) { - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Delay5 5s, xdrv %d"), XdrvMailbox.payload); - ShutterWaitForMotorStop(index); - ExecuteCommandPower(Settings.shutter_startrelay[index], 0, SRC_SHUTTER); - ShutterStartInit(index, new_shutterdirection, Shutter.target_position[index]); - if (Shutter.skip_relay_change == 0) { - // Code for shutters with circuit safe configuration, switch the direction Relay - ExecuteCommandPower(Settings.shutter_startrelay[index] +1, new_shutterdirection == 1 ? 0 : 1, SRC_SHUTTER); - // power on - ExecuteCommandPower(Settings.shutter_startrelay[index], 1, SRC_SHUTTER); - if (SHT_OFF_ON__OPEN_CLOSE_STEPPER == Shutter.mode) { - ExecuteCommandPower(Settings.shutter_startrelay[index]+2, 1, SRC_SHUTTER); + if (Shutter[index].direction != new_shutterdirection) { + ShutterStartInit(index, new_shutterdirection, Shutter[index].target_position); + switch (ShutterGlobal.position_mode) { + case SHT_COUNTER: + case SHT_PWM_TIME: + case SHT_PWM_VALUE: + case SHT_TIME_UP_DOWN: + if (!ShutterGlobal.skip_relay_change) { + // Code for shutters with circuit safe configuration, switch the direction Relay + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index] +1, new_shutterdirection == 1 ? 0 : 1, SRC_SHUTTER); + // power on + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index], 1, SRC_SHUTTER); } - } - } else { - // now start the motor for the right direction, work for momentary and normal shutters. - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT: Start in dir %d"), Shutter.direction[index]); - ShutterStartInit(index, new_shutterdirection, Shutter.target_position[index]); - if (Shutter.skip_relay_change == 0) { - ExecuteCommandPower(Settings.shutter_startrelay[index] + (new_shutterdirection == 1 ? 0 : 1), 1, SRC_SHUTTER); - } - //AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Delay6 5s, xdrv %d"), XdrvMailbox.payload); - } - Shutter.switched_relay = 0; - } + if (ShutterGlobal.position_mode != SHT_TIME_UP_DOWN) ExecuteCommandPowerShutter(Settings.shutter_startrelay[index]+2, 1, SRC_SHUTTER); + break; + case SHT_TIME: + if (!ShutterGlobal.skip_relay_change) { + if ( (power >> (Settings.shutter_startrelay[index] -1)) & 3 > 0 ) { + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index] + (new_shutterdirection == 1 ? 1 : 0), Shutter[index].switch_mode == SHT_SWITCH ? 0 : 1, SRC_SHUTTER); + } + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index] + (new_shutterdirection == 1 ? 0 : 1), 1, SRC_SHUTTER); + } + break; + case SHT_TIME_GARAGE: + if (!ShutterGlobal.skip_relay_change) { + if (new_shutterdirection == Shutter[index].lastdirection) { + AddLog_P2(LOG_LEVEL_INFO, PSTR("SHT: Garage not move in this direction: %d"), Shutter[index].switch_mode == SHT_PULSE); + for (uint8_t k=0 ; k <= (uint8_t)(Shutter[index].switch_mode == SHT_PULSE) ; k++) { + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index], 1, SRC_SHUTTER); + delay(500); + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index], 0, SRC_SHUTTER); + delay(500); + } + // reset shutter time to avoid 2 seconds above count as runtime + Shutter[index].time = 0; + } // if (new_shutterdirection == Shutter[i].lastdirection[index]) + ExecuteCommandPowerShutter(Settings.shutter_startrelay[index], 1, SRC_SHUTTER); + } // if (!ShutterGlobal.skip_relay_change) + break; + } // switch (ShutterGlobal.position_mode) + ShutterGlobal.RelayCurrentMask = 0; + } // if (Shutter[i].direction[index] != new_shutterdirection) } else { - target_pos_percent = ShutterRealToPercentPosition(Shutter.real_position[index], index); + target_pos_percent = ShutterRealToPercentPosition(Shutter[index].real_position, index); ShutterReportPosition(true, index); } XdrvMailbox.index = index +1; // Fix random index for ShutterClose @@ -968,7 +1094,7 @@ void CmndShutterStopPosition(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { uint32_t index = XdrvMailbox.index-1; - if (Shutter.direction[index]) { + if (Shutter[index].direction) { XdrvMailbox.payload = -99; CmndShutterStop(); } else { @@ -1006,24 +1132,34 @@ void CmndShutterMotorDelay(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { if (XdrvMailbox.data_len > 0) { - Settings.shutter_motordelay[XdrvMailbox.index -1] = (uint16_t)(steps_per_second * CharToFloat(XdrvMailbox.data)); + Settings.shutter_motordelay[XdrvMailbox.index -1] = (uint16_t)(STEPS_PER_SECOND * CharToFloat(XdrvMailbox.data)); ShutterInit(); } char time_chr[10]; - dtostrfd((float)(Settings.shutter_motordelay[XdrvMailbox.index -1]) / steps_per_second, 2, time_chr); + dtostrfd((float)(Settings.shutter_motordelay[XdrvMailbox.index -1]) / STEPS_PER_SECOND, 2, time_chr); ResponseCmndIdxChar(time_chr); } } +void CmndShutterMode(void) +{ + if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= MAX_MODES)) { + ShutterGlobal.position_mode = XdrvMailbox.payload; + Settings.shutter_mode = XdrvMailbox.payload; + ShutterInit(); + } + ResponseCmndNumber(ShutterGlobal.position_mode); +} + void CmndShutterRelay(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_SHUTTERS)) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 64)) { Settings.shutter_startrelay[XdrvMailbox.index -1] = XdrvMailbox.payload; if (XdrvMailbox.payload > 0) { - Shutter.mask |= 3 << (XdrvMailbox.payload - 1); + ShutterGlobal.RelayShutterMask |= 3 << (XdrvMailbox.payload - 1); } else { - Shutter.mask ^= 3 << (Settings.shutter_startrelay[XdrvMailbox.index -1] - 1); + ShutterGlobal.RelayShutterMask ^= 3 << (Settings.shutter_startrelay[XdrvMailbox.index -1] - 1); } Settings.shutter_startrelay[XdrvMailbox.index -1] = XdrvMailbox.payload; ShutterInit(); @@ -1184,21 +1320,19 @@ void CmndShutterSetHalfway(void) void CmndShutterFrequency(void) { if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= 20000)) { - Shutter.max_pwm_frequency = XdrvMailbox.payload; + ShutterGlobal.open_velocity_max = XdrvMailbox.payload; if (shutters_present < 4) { - Settings.shuttercoeff[4][3] = Shutter.max_pwm_frequency; + Settings.shuttercoeff[4][3] = ShutterGlobal.open_velocity_max; } ShutterInit(); - ResponseCmndNumber(XdrvMailbox.payload); // ???? - } else { - ResponseCmndNumber(Shutter.max_pwm_frequency); } + ResponseCmndNumber(ShutterGlobal.open_velocity_max); } void CmndShutterSetClose(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { - Shutter.real_position[XdrvMailbox.index -1] = 0; + Shutter[XdrvMailbox.index -1].real_position = 0; ShutterStartInit(XdrvMailbox.index -1, 0, 0); Settings.shutter_position[XdrvMailbox.index -1] = 0; ResponseCmndIdxChar(D_CONFIGURATION_RESET); @@ -1208,22 +1342,40 @@ void CmndShutterSetClose(void) void CmndShutterSetOpen(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { - Shutter.real_position[XdrvMailbox.index -1] = Shutter.open_max[XdrvMailbox.index -1]; - ShutterStartInit(XdrvMailbox.index -1, 0, Shutter.open_max[XdrvMailbox.index -1]); + Shutter[XdrvMailbox.index -1].real_position = Shutter[XdrvMailbox.index -1].open_max; + ShutterStartInit(XdrvMailbox.index -1, 0, Shutter[XdrvMailbox.index -1].open_max); Settings.shutter_position[XdrvMailbox.index -1] = 100; ResponseCmndIdxChar(D_CONFIGURATION_RESET); } } -void CmndShutterInvert(void) +void CmndShutterPwmRange(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { - if (XdrvMailbox.payload == 0) { - Settings.shutter_options[XdrvMailbox.index -1] &= ~(1); - } else if (XdrvMailbox.payload == 1) { - Settings.shutter_options[XdrvMailbox.index -1] |= (1); + if (XdrvMailbox.data_len > 0) { + uint8_t i = 0; + char *str_ptr; + + char data_copy[strlen(XdrvMailbox.data) +1]; + strncpy(data_copy, XdrvMailbox.data, sizeof(data_copy)); // Duplicate data as strtok_r will modify it. + // Loop through the data string, splitting on ' ' seperators. + for (char *str = strtok_r(data_copy, " ", &str_ptr); str && i < 2; str = strtok_r(nullptr, " ", &str_ptr), i++) { + uint16_t field = atoi(str); + // The fields in a data string can only range from 1-30000. + // and following value must be higher than previous one + if ((field <= 0) || (field > 1023)) { + break; + } + Settings.shutter_pwmrange[i][XdrvMailbox.index -1] = field; + } + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("SHT%d: Init1. pwmmin %d, pwmmax %d"), XdrvMailbox.index , Settings.shutter_pwmrange[0][XdrvMailbox.index -1], Settings.shutter_pwmrange[1][XdrvMailbox.index -1]); + ShutterInit(); + ResponseCmndIdxChar(XdrvMailbox.data); + } else { + char setting_chr[30] = "0"; + snprintf_P(setting_chr, sizeof(setting_chr), PSTR("Shutter %d: min:%d max:%d"), XdrvMailbox.index, Settings.shutter_pwmrange[0][XdrvMailbox.index -1], Settings.shutter_pwmrange[1][XdrvMailbox.index -1]); + ResponseCmndIdxChar(setting_chr); } - ResponseCmndIdxNumber((Settings.shutter_options[XdrvMailbox.index -1] & 1) ? 1 : 0); } } @@ -1231,7 +1383,7 @@ void CmndShutterCalibration(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { if (XdrvMailbox.data_len > 0) { - uint32_t i = 0; + uint8_t i = 0; char *str_ptr; char data_copy[strlen(XdrvMailbox.data) +1]; @@ -1260,38 +1412,31 @@ void CmndShutterCalibration(void) } } -void CmndShutterLock(void) { +void ShutterOptionsSetHelper(uint16_t option){ if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { if (XdrvMailbox.payload == 0) { - Settings.shutter_options[XdrvMailbox.index -1] &= ~(2); + Settings.shutter_options[XdrvMailbox.index -1] &= ~(option); } else if (XdrvMailbox.payload == 1) { - Settings.shutter_options[XdrvMailbox.index -1] |= (2); + Settings.shutter_options[XdrvMailbox.index -1] |= (option); } - ResponseCmndIdxNumber((Settings.shutter_options[XdrvMailbox.index -1] & 2) ? 1 : 0); + ResponseCmndIdxNumber((Settings.shutter_options[XdrvMailbox.index -1] & option) ? 1 : 0); } } +void CmndShutterInvert(void) { + ShutterOptionsSetHelper(1); +} + +void CmndShutterLock(void) { + ShutterOptionsSetHelper(2); +} + void CmndShutterEnableEndStopTime(void) { - if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { - if (XdrvMailbox.payload == 0) { - Settings.shutter_options[XdrvMailbox.index -1] &= ~(4); - } else if (XdrvMailbox.payload == 1) { - Settings.shutter_options[XdrvMailbox.index -1] |= (4); - } - ResponseCmndIdxNumber((Settings.shutter_options[XdrvMailbox.index -1] & 4) ? 1 : 0); - } + ShutterOptionsSetHelper(4); } -void CmndShutterInvertWebButtons(void) -{ - if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= shutters_present)) { - if (XdrvMailbox.payload == 0) { - Settings.shutter_options[XdrvMailbox.index -1] &= ~(8); - } else if (XdrvMailbox.payload == 1) { - Settings.shutter_options[XdrvMailbox.index -1] |= (8); - } - ResponseCmndIdxNumber((Settings.shutter_options[XdrvMailbox.index -1] & 8) ? 1 : 0); - } +void CmndShutterInvertWebButtons(void) { + ShutterOptionsSetHelper(8); } /*********************************************************************************************\ @@ -1321,10 +1466,10 @@ bool Xdrv27(uint8_t function) case FUNC_JSON_APPEND: for (uint8_t i = 0; i < shutters_present; i++) { uint8_t position = (Settings.shutter_options[i] & 1) ? 100 - Settings.shutter_position[i] : Settings.shutter_position[i]; - uint8_t target = (Settings.shutter_options[i] & 1) ? 100 - ShutterRealToPercentPosition(Shutter.target_position[i], i) : ShutterRealToPercentPosition(Shutter.target_position[i], i); + uint8_t target = (Settings.shutter_options[i] & 1) ? 100 - ShutterRealToPercentPosition(Shutter[i].target_position, i) : ShutterRealToPercentPosition(Shutter[i].target_position, i); ResponseAppend_P(","); - ResponseAppend_P(JSON_SHUTTER_POS, i+1, position, Shutter.direction[i],target); + ResponseAppend_P(JSON_SHUTTER_POS, i+1, position, Shutter[i].direction,target); #ifdef USE_DOMOTICZ if ((0 == tele_period) && (0 == i)) { DomoticzSensor(DZ_SHUTTER, position); @@ -1335,25 +1480,25 @@ bool Xdrv27(uint8_t function) case FUNC_SET_POWER: char stemp1[10]; // extract the number of the relay that was switched and save for later in Update Position. - Shutter.switched_relay = XdrvMailbox.index ^ Shutter.old_power; - AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Switched relay: %d by %s"), Shutter.switched_relay,GetTextIndexed(stemp1, sizeof(stemp1), last_source, kCommandSource)); + ShutterGlobal.RelayCurrentMask = XdrvMailbox.index ^ ShutterGlobal.RelayOldMask; + AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Switched relay: %d by %s"), ShutterGlobal.RelayCurrentMask,GetTextIndexed(stemp1, sizeof(stemp1), last_source, kCommandSource)); ShutterRelayChanged(); - Shutter.old_power = XdrvMailbox.index; + ShutterGlobal.RelayOldMask = XdrvMailbox.index; break; case FUNC_SET_DEVICE_POWER: - if (Shutter.skip_relay_change ) { + if (ShutterGlobal.skip_relay_change ) { uint8_t i; for (i = 0; i < devices_present; i++) { - if (Shutter.switched_relay &1) { + if (ShutterGlobal.RelayCurrentMask &1) { break; } - Shutter.switched_relay >>= 1; + ShutterGlobal.RelayCurrentMask >>= 1; } //AddLog_P2(LOG_LEVEL_ERROR, PSTR("SHT: skip relay change: %d"),i+1); result = true; - Shutter.skip_relay_change = 0; + ShutterGlobal.skip_relay_change = 0; AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("SHT: Skipping switch off relay %d"),i); - ExecuteCommandPower(i+1, 0, SRC_SHUTTER); + ExecuteCommandPowerShutter(i+1, 0, SRC_SHUTTER); } break; case FUNC_BUTTON_PRESSED: diff --git a/tasmota/xdrv_29_deepsleep.ino b/tasmota/xdrv_29_deepsleep.ino index 84b30f740..bb1a7744c 100644 --- a/tasmota/xdrv_29_deepsleep.ino +++ b/tasmota/xdrv_29_deepsleep.ino @@ -130,7 +130,7 @@ void DeepSleepPrepare(void) Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup); MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_STATUS)); -// Response_P(S_OFFLINE); +// Response_P(S_LWT_OFFLINE); // MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic } diff --git a/tasmota/xdrv_39_thermostat.ino b/tasmota/xdrv_39_thermostat.ino index 8e863822d..4a29ad36b 100644 --- a/tasmota/xdrv_39_thermostat.ino +++ b/tasmota/xdrv_39_thermostat.ino @@ -1328,17 +1328,14 @@ void ThermostatDebug(uint8_t ctr_output) #endif // DEBUG_THERMOSTAT void ThermostatGetLocalSensor(uint8_t ctr_output) { - DynamicJsonBuffer jsonBuffer; - JsonObject& root = jsonBuffer.parseObject((const char*)mqtt_data); - if (root.success()) { - const char* value_c = root[THERMOSTAT_SENSOR_NAME]["Temperature"]; - if (value_c != NULL && strlen(value_c) > 0 && (isdigit(value_c[0]) || (value_c[0] == '-' && isdigit(value_c[1])) ) ) { - int16_t value; + JsonParser parser(mqtt_data); + JsonParserObject root = parser.getRootObject(); + if (root) { + JsonParserToken value_token = root[PSTR(THERMOSTAT_SENSOR_NAME)].getObject()[PSTR("Temperature")]; + if (value_token.isNum()) { + int16_t value = value_token.getFloat() * 10; if (Thermostat[ctr_output].status.temp_format == TEMP_FAHRENHEIT) { - value = (int16_t)ThermostatFahrenheitToCelsius((int32_t)(CharToFloat(value_c) * 10), TEMP_CONV_ABSOLUTE); - } - else { - value = (int16_t)(CharToFloat(value_c) * 10); + value = ThermostatFahrenheitToCelsius(value, TEMP_CONV_ABSOLUTE); } if ( (value >= -1000) && (value <= 1000) diff --git a/tasmota/xdrv_40_telegram.ino b/tasmota/xdrv_40_telegram.ino index 92fca7c90..9834d3b30 100644 --- a/tasmota/xdrv_40_telegram.ino +++ b/tasmota/xdrv_40_telegram.ino @@ -76,7 +76,7 @@ bool TelegramInit(void) { if (!telegramClient) { telegramClient = new BearSSL::WiFiClientSecure_light(tls_rx_size, tls_tx_size); #ifdef USE_MQTT_TLS_CA_CERT - telegramClient->setTrustAnchor(&GoDaddyCAG2_TA); + telegramClient->setTrustAnchor(&GoDaddyCAG2_TA, 1); #else telegramClient->setPubKeyFingerprint(Telegram_Fingerprint, Telegram_Fingerprint, false); // check server fingerprint #endif @@ -225,15 +225,22 @@ void TelegramAnalizeMessage(void) { for (uint32_t i = 1; i < Telegram.message[0][0].toInt() +1; i++) { Telegram.message[i][5] = ""; - DynamicJsonBuffer jsonBuffer; - JsonObject &root = jsonBuffer.parseObject(Telegram.message[i][0]); - if (root.success()) { - Telegram.message[i][0] = root["update_id"].as(); - Telegram.message[i][1] = root["message"]["from"]["id"].as(); - Telegram.message[i][2] = root["message"]["from"]["first_name"].as(); - Telegram.message[i][3] = root["message"]["from"]["last_name"].as(); - Telegram.message[i][4] = root["message"]["chat"]["id"].as(); - Telegram.message[i][5] = root["message"]["text"].as(); + String buf = Telegram.message[i][0]; // we need to keep a copy of the buffer + JsonParser parser((char*)buf.c_str()); + JsonParserObject root = parser.getRootObject(); + if (root) { + Telegram.message[i][0] = root["update_id"].getStr(); + Telegram.message[i][1] = root["message"].getObject()["from"].getObject()["id"].getStr(); + Telegram.message[i][2] = root["message"].getObject()["from"].getObject()["first_name"].getStr(); + Telegram.message[i][3] = root["message"].getObject()["from"].getObject()["last_name"].getStr(); + Telegram.message[i][4] = root["message"].getObject()["chat"].getObject()["id"].getStr(); + Telegram.message[i][5] = root["message"].getObject()["text"].getStr(); + // Telegram.message[i][0] = root["update_id"].as(); + // Telegram.message[i][1] = root["message"]["from"]["id"].as(); + // Telegram.message[i][2] = root["message"]["from"]["first_name"].as(); + // Telegram.message[i][3] = root["message"]["from"]["last_name"].as(); + // Telegram.message[i][4] = root["message"]["chat"]["id"].as(); + // Telegram.message[i][5] = root["message"]["text"].as(); } int id = Telegram.message[Telegram.message[0][0].toInt()][0].toInt() +1; diff --git a/tasmota/xdrv_42_i2s_audio.ino b/tasmota/xdrv_42_i2s_audio.ino index 77492992b..4a3bd91b0 100644 --- a/tasmota/xdrv_42_i2s_audio.ino +++ b/tasmota/xdrv_42_i2s_audio.ino @@ -52,14 +52,9 @@ AudioFileSourceFS *file; AudioOutputI2S *out; AudioFileSourceID3 *id3; AudioGeneratorMP3 *decoder = NULL; +void *mp3ram = NULL; -#ifdef USE_WEBRADIO -AudioFileSourceICYStream *ifile = NULL; -AudioFileSourceBuffer *buff = NULL; -char wr_title[64]; -//char status[64]; - #ifdef ESP8266 const int preallocateBufferSize = 5*1024; const int preallocateCodecSize = 29192; // MP3 codec max mem needed @@ -69,6 +64,12 @@ const int preallocateCodecSize = 29192; // MP3 codec max mem needed //const int preallocateCodecSize = 85332; // AAC+SBR codec max mem needed #endif +#ifdef USE_WEBRADIO +AudioFileSourceICYStream *ifile = NULL; +AudioFileSourceBuffer *buff = NULL; +char wr_title[64]; +//char status[64]; + void *preallocateBuffer = NULL; void *preallocateCodec = NULL; uint32_t retryms = 0; @@ -210,6 +211,12 @@ void I2S_Init(void) { is2_volume=10; out->SetGain(((float)is2_volume/100.0)*4.0); out->stop(); + mp3ram = nullptr; + +#ifdef ESP32 + if (psramFound()) { + mp3ram = heap_caps_malloc(preallocateCodecSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + } #ifdef USE_WEBRADIO if (psramFound()) { @@ -223,6 +230,7 @@ void I2S_Init(void) { //Serial.printf_P(PSTR("FATAL ERROR: Unable to preallocate %d bytes for app\n"), preallocateBufferSize+preallocateCodecSize); } #endif // USE_WEBRADIO +#endif // ESP32 } #ifdef ESP32 @@ -285,7 +293,7 @@ void Webradio(const char *url) { retryms = millis() + 2000; } - xTaskCreatePinnedToCore(mp3_task2, "MP3", 8192, NULL, 3, &mp3_task_h, 1); + xTaskCreatePinnedToCore(mp3_task2, "MP3-2", 8192, NULL, 3, &mp3_task_h, 1); } void mp3_task2(void *arg){ @@ -366,7 +374,12 @@ void Play_mp3(const char *path) { file = new AudioFileSourceFS(*fsp,path); id3 = new AudioFileSourceID3(file); - mp3 = new AudioGeneratorMP3(); + + if (mp3ram) { + mp3 = new AudioGeneratorMP3(mp3ram, preallocateCodecSize); + } else { + mp3 = new AudioGeneratorMP3(); + } mp3->begin(id3, out); if (I2S_Task) { diff --git a/tasmota/xdrv_43_mlx90640.ino b/tasmota/xdrv_43_mlx90640.ino new file mode 100644 index 000000000..88832acc6 --- /dev/null +++ b/tasmota/xdrv_43_mlx90640.ino @@ -0,0 +1,626 @@ +/* + xdrv_43_mlx90640.ino - MLX90640 support for Tasmota + + Copyright (C) 2020 Christian Baars and Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + + -------------------------------------------------------------------------------------------- + Version yyyymmdd Action Description + -------------------------------------------------------------------------------------------- + 0.9.0.0 20200827 started - based on https://github.com/melexis/mlx90640-library +*/ + +#ifdef USE_I2C +#ifdef USE_MLX90640 + +#define MLX90640_ADDRESS 0x33 +#define MLX90640_POI_NUM 6 //some parts of the JS are hardcoded for 6!! + +/*********************************************************************************************\ +* MLX90640 +\*********************************************************************************************/ + +#define XDRV_43 43 +#define XI2C_53 53 // See I2CDEVICES.md +#include + +const char MLX90640type[] PROGMEM = "MLX90640"; + +#ifdef USE_WEBSERVER +#define WEB_HANDLE_MLX90640 "mlx" +const char HTTP_BTN_MENU_MLX90640[] PROGMEM = "

"; +#endif // USE_WEBSERVER + +struct { + uint32_t type:1; + uint32_t ready:1; + uint32_t dumpedEE:1; + uint32_t extractedParams:1; + paramsMLX90640 *params; + float Ta; + uint16_t Frame[834]; + float To[768]; + uint8_t pois[2*MLX90640_POI_NUM] = {2,1, 30,1, 10,12, 22,12, 2,23, 30,23}; // {x1,y1,x2,y2,...,x6,y6} +} MLX90640; + +/*********************************************************************************************\ + * commands +\*********************************************************************************************/ + +#define D_CMND_MLX90640 "MLX" + +const char S_JSON_MLX90640_COMMAND_NVALUE[] PROGMEM = "{\"" D_CMND_MLX90640 "%s\":%d}"; +const char S_JSON_MLX90640_COMMAND[] PROGMEM = "{\"" D_CMND_MLX90640 "%s\"}"; +const char kMLX90640_Commands[] PROGMEM = "POI"; + +enum MLX90640_Commands { // commands useable in console or rules + CMND_MLX90640_POI // MLXPOIn xxyy - set POI number n to x,y + }; + +/************************************************************************\ + * Web GUI +\************************************************************************/ +#ifdef USE_WEBSERVER + +#ifdef USE_UNISHOX_COMPRESSION +const size_t HTTP_MLX90640_1_SNS_SIZE = 389; +const char HTTP_MLX90640_1_SNS_COMPRESSED[] PROGMEM = "\x3D\x3C\x1F\xF4\x65\x2A\x2B\x32\x18\xCF\x87\xDD\x33\x65\x1D\x86\xBB\x33\xB0\x41" + "\xA4\x7D\x9F\x81\xE7\x7A\x90\xDB\x18\x7C\x3B\xA6\x76\x10\xB6\x75\x1B\x0E\x43\xA8" + "\x8C\x8E\x43\xA8\x8D\x87\x28\xEA\x23\x23\x94\x77\x8F\x87\xE1\x02\x0D\x13\xAC\xD8" + "\x72\x1D\xE3\xD6\x77\x48\xC8\xE5\x1D\x64\x6C\x39\x47\x78\xEC\x3B\xA4\x64\x72\x1D" + "\x64\x6C\x39\x0E\xF1\xDB\x23\x61\xCA\x3C\x10\x20\xE3\x3A\x36\xC7\x9A\x3E\x2E\x63" + "\xE8\xB4\x6D\x8F\x33\xC1\x9D\xFD\x07\x7C\x67\x7E\x3A\x83\xA3\x61\xD4\x3D\xF1\x0F" + "\x06\x77\xF4\x3C\x43\x0D\x87\x50\xCC\xD3\xE1\xEF\x1E\xF9\xE0\xCE\xFE\xBE\x56\x7C" + "\x3D\xE3\xDF\x3C\x18\x17\xC1\xD6\xE7\x21\xE7\x44\x37\x05\xF9\x90\xCC\xF1\xDD\x04" + "\x2C\x65\x33\x3A\x3B\xC8\xF6\x82\x0E\x87\xF6\x1D\x23\xE0\x21\x66\x87\x41\xE7\x44" + "\x3B\x05\xF0\x9B\xC3\xC4\x18\x5A\xFA\x8B\xEC\x3A\x3B\xA7\x78\xF0\x67\x7F\x46\xC4" + "\x7C\x4C\xCE\x8E\x81\x85\xAF\xA8\x8D\x87\x5F\xD8\x74\x74\x09\x98\xA3\xC6\x98\x3B" + "\xA6\xC3\xF0\xE5\xD3\x3B\xC7\xB4\x8D\x87\xC3\x97\x11\xE0\xF7\x17\xDD\x0B\xFF\x23" + "\xDA\x6C\x3C\xD1\x0D\xBA\x14\x74\x30\x16\x67\xCE\xE8\xDB\x18\x77\x4D\x87\x51\xC6" + "\x75\x5D\x33\xA9\x9D\x57\x0E\x88\xEF\x1D\xE3\xA8\x8C\x81\x32\xF9\xDD\x04\x5D\x04" + "\x8C\x91\xD6\xBE\xC3\xA3\xA5\x60\xC3\xBC\x75\x1C\x67\x55\x63\x3A\x99\xD5\x56\x74" + "\x47\x78\xEF\x1E\xE3\xC1\xEE"; +#define HTTP_MLX90640_1_SNS Decompress(HTTP_MLX90640_1_SNS_COMPRESSED,HTTP_MLX90640_1_SNS_SIZE).c_str() +#else +const char HTTP_MLX90640_1_SNS[] PROGMEM = + "" +; +#endif //USE_UNISHOX_COMPRESSION +#ifdef USE_UNISHOX_COMPRESSION +const size_t HTTP_MLX90640_4b_SNS_SIZE = 418; +const char HTTP_MLX90640_4b_SNS_COMPRESSED[] PROGMEM = "\x3D\x07\x60\x86\x4B\x38\x2C\xB1\x0F\x87\xDF\x9D\x0B\x18\x77\x4E\xF1\xE0\xFB\x3F" + "\x0F\x40\xEF\x8C\xEF\xCB\x44\x3E\x1F\x63\x42\x36\x1F\x68\x7F\x44\xA1\x47\xC3\xEC" + "\xE5\xE3\x3E\xCE\xE1\x0A\x7A\x3C\x2A\x2B\x8F\x87\xD9\xCA\xC6\x7D\x9F\x87\xA1\xD8" + "\x40\x83\x83\x9F\x87\xA0\x9A\x66\x7E\x1E\x87\x60\x9A\x66\x7E\x1E\x9E\x61\x30\xE9" + "\x68\x87\xC3\xEC\x66\x69\x04\x7D\xAC\xE0\xC5\x5F\x0F\x33\xE1\xF6\x37\x3C\x77\x4E" + "\xF1\xF6\x7E\x1E\x98\x32\xB7\x39\x19\xD8\x42\xD9\xF0\xFB\x38\xCF\xB3\xF0\x88\x61" + "\x61\x69\xD6\x72\x1E\x87\x61\x02\x0D\x40\x4B\xB8\x72\x10\x20\xDC\x39\x44\x0A\x77" + "\x0E\x51\x02\x0D\xC3\x96\x40\xA7\x70\xE5\x90\x20\xDC\x39\x84\x0A\x77\x0E\x61\x02" + "\x0D\xC3\x9A\x40\xA7\x70\xE6\x90\x20\xDC\x39\xC4\x08\xB7\x0E\xC0\x41\xE1\x2A\x01" + "\xFC\x3D\x04\xD3\x30\x41\xE2\x0C\xE4\x3E\xC8\x10\xF8\x5B\x13\x4C\xCF\xC2\x18\x58" + "\x5A\x75\x9C\x67\x99\xDC\x3D\x0B\xC3\x2F\x96\x88\x7C\x3E\xEC\xE4\x3E\xCF\xC3\xD0" + "\xEC\x2F\x0C\xBE\x3F\x26\x3B\x32\xF2\x0D\x1D\xDF\x3E\xF6\x7C\xEF\x02\x2E\x1E\x08" + "\x39\x11\xCA\x20\x44\xC8\x8E\xC1\xD8\x21\x91\xF8"; +#define HTTP_MLX90640_4b_SNS Decompress(HTTP_MLX90640_4b_SNS_COMPRESSED,HTTP_MLX90640_4b_SNS_SIZE).c_str() +#else +const char HTTP_MLX90640_4b_SNS[] PROGMEM = + "" + "" + "
" + "" + "
" + "
POI-0: °C (sensor)
" + "
" + "" + ; +#endif //USE_UNISHOX_COMPRESSION +void MLX90640UpdateGUI(void){ + WSContentStart_P("mlx"); + WSContentSendStyle(); + WSContentSend_P(HTTP_MLX90640_1_SNS); + WSContentSend_P(HTTP_MLX90640_2a_SNS); + WSContentSend_P(HTTP_MLX90640_2b_SNS); + WSContentSend_P(HTTP_MLX90640_3a_SNS); + WSContentSend_P(HTTP_MLX90640_3b_SNS); + WSContentSend_P(HTTP_MLX90640_4a_SNS); + WSContentSend_P(HTTP_MLX90640_4b_SNS); + WSContentSpaceButton(BUTTON_MAIN); + WSContentStop(); +} + +void MLX90640HandleWebGuiResponse(void){ + char tmp[(MLX90640_POI_NUM*2)+4]; + WebGetArg("ul", tmp, sizeof(tmp)); // update line + if (strlen(tmp)) { + uint8_t _line = atoi(tmp); + // AddLog_P2(LOG_LEVEL_DEBUG, "MLX90640: send line %u", _line); + float _buf[65]; + if(_line==0){_buf[0]=1000+MLX90640.Ta;} //ambient temperature modulation hack + else{_buf[0]=(float)_line;} + memcpy((char*)&_buf[1],(char*)&MLX90640.To[_line*64],64*4); + Webserver->send(200,PSTR("application/octet-stream"),(const char*)&_buf,65*4); + return; + } + WebGetArg("up", tmp, sizeof(tmp)); // update POI to browser + if (strlen(tmp)==1) { + Webserver->send(200,PSTR("application/octet-stream"),(const char*)&MLX90640.pois,MLX90640_POI_NUM*2); + return; + } + else if (strlen(tmp)>2) { // receive updated POI from browser + uint32_t _poi = atoi(tmp); + uint32_t _poiNum = (_poi-(_poi%10000))/10000; + MLX90640.pois[_poiNum*2] = (_poi%10000)/100; + MLX90640.pois[(_poiNum*2)+1] = _poi%100; + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RAW: %u, POI-%u: x: %u, y: %u"),_poi,_poiNum,MLX90640.pois[_poiNum],MLX90640.pois[_poiNum+1]); + for(int i = 0;i(MLX90640_POI_NUM-1)&&XdrvMailbox.index<1) return false; + _idx = (XdrvMailbox.index-1)*2; + if (XdrvMailbox.data_len > 0) { + uint32_t _coord = TextToInt(XdrvMailbox.data); + MLX90640.pois[_idx] = (_coord%10000)/100; + if(MLX90640.pois[_idx]>31) MLX90640.pois[_idx]=31; + MLX90640.pois[_idx+1] = _coord%100; + if(MLX90640.pois[_idx+1]>23) MLX90640.pois[_idx+1]=23; + } + AddLog_P2(LOG_LEVEL_INFO, PSTR("POI-%u = x:%u,y:%u"),XdrvMailbox.index,MLX90640.pois[_idx],MLX90640.pois[_idx+1]); + Response_P(S_JSON_MLX90640_COMMAND_NVALUE, command, XdrvMailbox.payload); + break; + default: + // else for Unknown command + serviced = false; + break; + } + } else { + return false; + } + return serviced; +} + +/************************************************************************\ + * Init +\************************************************************************/ +void MLX90640init() +{ + if (MLX90640.type || !I2cSetDevice(MLX90640_ADDRESS)) { return; } + + Wire.setClock(400000); + int status = -1; + if(!MLX90640.dumpedEE){ + status = MLX90640_DumpEE(MLX90640_ADDRESS, MLX90640.Frame); + if (status != 0){ + AddLog_P2(LOG_LEVEL_INFO, PSTR("Failed to load system parameters")); + } + else { + AddLog_P2(LOG_LEVEL_INFO, PSTR("MLX90640: started")); + MLX90640.type = true; + } + MLX90640.params = new paramsMLX90640; + } +} + +/************************************************************************\ + * Run loop +\************************************************************************/ +void MLX90640every100msec(){ + static uint32_t _job = 0; + int status; + uint32_t _time; + + if(!MLX90640.extractedParams){ + static uint32_t _chunk = 0; + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: will read chunk: %u"), _chunk); + _time = millis(); + status = MLX90640_ExtractParameters(MLX90640.Frame, MLX90640.params, _chunk); + if (status == 0){ + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: parameter received after: %u msec, status: %u"), TimePassedSince(_time), status); + } + if (_chunk == 5) MLX90640.extractedParams = true; + _chunk++; + return; + } + + switch(_job){ + case 0: + if(MLX90640_SynchFrame(MLX90640_ADDRESS)!=0){ + _job=-1; + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: frame not ready")); + break; + } + // _time = millis(); + status = MLX90640_GetFrameData(MLX90640_ADDRESS, MLX90640.Frame); + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: got frame 0 in %u msecs, status: %i"), TimePassedSince(_time), status); + break; + case 1: + MLX90640.Ta = MLX90640_GetTa(MLX90640.Frame, MLX90640.params); + break; + case 2: + // _time = millis(); + MLX90640_CalculateTo(MLX90640.Frame, MLX90640.params, 0.95f, MLX90640.Ta - 8, MLX90640.To, 0); + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: calculated temperatures in %u msecs"), TimePassedSince(_time)); + break; + case 5: + if(MLX90640_SynchFrame(MLX90640_ADDRESS)!=0){ + _job=4; + break; + } + // _time = millis(); + status = MLX90640_GetFrameData(MLX90640_ADDRESS, MLX90640.Frame); + // // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: got frame 1 in %u msecs, status: %i"), TimePassedSince(_time), status); + break; + case 7: + // _time = millis(); + MLX90640_CalculateTo(MLX90640.Frame, MLX90640.params, 0.95f, MLX90640.Ta - 8, MLX90640.To, 1); + // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MLX90640: calculated temperatures in %u msecs"), TimePassedSince(_time)); + break; + default: + break; + } + _job++; + if(_job>10) _job=0; +} + + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +void MLX90640Show(uint8_t json) +{ + char amb_tstr[FLOATSZ]; + dtostrfd(MLX90640.Ta, Settings.flag2.temperature_resolution, amb_tstr); + if (json) { + ResponseAppend_P(PSTR(",\"MLX90640\":{\"" D_JSON_TEMPERATURE "\":[%s"), amb_tstr); + for(int i = 0;ion("/mlx", MLX90640HandleWebGui); + break; +#endif // USE_WEBSERVER + case FUNC_COMMAND: + result = MLX90640Cmd(); + break; + } + } + return result; +} + +#endif // USE_MLX90640_SENSOR +#endif // USE_I2C diff --git a/tasmota/xdrv_83_esp32watch.ino b/tasmota/xdrv_83_esp32watch.ino index 2f759aa9a..6a58974c0 100644 --- a/tasmota/xdrv_83_esp32watch.ino +++ b/tasmota/xdrv_83_esp32watch.ino @@ -64,6 +64,7 @@ struct TTGO_globs { bool bma_double_click = false; bool bma_click = false; bool bma_button = false; + bool power_ok = false; } ttgo_globs; @@ -115,15 +116,19 @@ void TTGO_Init(void) { ttgo_globs.bma->enableWakeupInterrupt(true); ttgo_globs.bma->enableAnyNoMotionInterrupt(true); ttgo_globs.bma->enableAccel(); -#endif +#endif // USE_BMA423 } void initPower(void) { int ret = ttgo_globs.ttgo_power->begin(axpReadBytes, axpWriteBytes); if (ret == AXP_FAIL) { //DBGX("AXP Power begin failed"); + // Serial.printf("AXP202 failed\n" ); } else { I2cSetActiveFound(AXP202_SLAVE_ADDRESS, "AXP202"); + ttgo_globs.power_ok = true; + // Serial.printf("AXP202 OK\n" ); + //Change the button boot time to 4 seconds ttgo_globs.ttgo_power->setShutdownTime(AXP_POWER_OFF_TIME_4S); // Turn off the charging instructions, there should be no @@ -162,6 +167,7 @@ void initPower(void) { portYIELD_FROM_ISR (); } }, FALLING); + } } @@ -205,6 +211,8 @@ const char HTTP_TTGO_BMA[] PROGMEM = void TTGO_WebShow(uint32_t json) { + if (ttgo_globs.power_ok == false) return; + TTGO_GetADC(); char vstring[32]; @@ -276,8 +284,9 @@ int32_t ttgo_sleeptime; ttgo_sleeptime = stime; +#ifdef USE_DISPLAY DisplayOnOff(0); - +#endif if (ttgo_sleeptime>=0) { // ligh sleep mode WifiShutdown(); @@ -311,7 +320,9 @@ int32_t ttgo_sleeptime; if (ttgo_sleeptime) { ttgo_globs.lenergy = false; rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M); +#ifdef USE_DISPLAY DisplayOnOff(1); +#endif } else { while (ttgo_globs.lenergy == true) { TTGO_loop(0); @@ -332,7 +343,9 @@ uint8_t data; if (ttgo_globs.lenergy) { ttgo_globs.lenergy = false; rtc_clk_cpu_freq_set(RTC_CPU_FREQ_240M); +#ifdef USE_DISPLAY DisplayOnOff(1); +#endif } #ifdef USE_BMA423 diff --git a/tasmota/xnrg_07_ade7953.ino b/tasmota/xnrg_07_ade7953.ino index 3d7b405d5..beaea9635 100644 --- a/tasmota/xnrg_07_ade7953.ino +++ b/tasmota/xnrg_07_ade7953.ino @@ -199,7 +199,8 @@ void Ade7953EnergyEverySecond(void) void Ade7953DrvInit(void) { - if (PinUsed(GPIO_ADE7953_IRQ)) { // Irq on GPIO16 is not supported... + if (PinUsed(GPIO_ADE7953_IRQ)) { // Irq on GPIO16 is not supported... + pinMode(Pin(GPIO_ADE7953_IRQ), INPUT); // Related to resetPins() - Must be set to input delay(100); // Need 100mS to init ADE7953 if (I2cSetDevice(ADE7953_ADDR)) { if (HLW_PREF_PULSE == Settings.energy_power_calibration) { diff --git a/tasmota/xsns_05_ds18x20.ino b/tasmota/xsns_05_ds18x20.ino index 361847932..e828c55d6 100644 --- a/tasmota/xsns_05_ds18x20.ino +++ b/tasmota/xsns_05_ds18x20.ino @@ -17,6 +17,7 @@ along with this program. If not, see . */ +#ifdef ESP8266 #ifdef USE_DS18x20 /*********************************************************************************************\ * DS18B20 - Temperature - Multiple sensors @@ -547,3 +548,4 @@ bool Xsns05(uint8_t function) } #endif // USE_DS18x20 +#endif // ESP8266 diff --git a/tasmota/xsns_05_ds18x20_esp32.ino b/tasmota/xsns_05_ds18x20_esp32.ino new file mode 100644 index 000000000..1d622448c --- /dev/null +++ b/tasmota/xsns_05_ds18x20_esp32.ino @@ -0,0 +1,254 @@ +/* + xsns_05_ds18x20_esp32.ino - DS18x20 temperature sensor support for Tasmota + + Copyright (C) 2020 Heiko Krupp and Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifdef ESP32 +#ifdef USE_DS18x20 +/*********************************************************************************************\ + * DS18B20 - Temperature - Multiple sensors +\*********************************************************************************************/ + +#define XSNS_05 5 + +#define DS18S20_CHIPID 0x10 // +/-0.5C 9-bit +#define DS1822_CHIPID 0x22 // +/-2C 12-bit +#define DS18B20_CHIPID 0x28 // +/-0.5C 12-bit +#define MAX31850_CHIPID 0x3B // +/-0.25C 14-bit + +#define W1_SKIP_ROM 0xCC +#define W1_CONVERT_TEMP 0x44 +#define W1_READ_SCRATCHPAD 0xBE + +#define DS18X20_MAX_SENSORS 8 + +const char kDs18x20Types[] PROGMEM = "DS18x20|DS18S20|DS1822|DS18B20|MAX31850"; + +uint8_t ds18x20_chipids[] = { 0, DS18S20_CHIPID, DS1822_CHIPID, DS18B20_CHIPID, MAX31850_CHIPID }; + +uint8_t ds18x20_address[DS18X20_MAX_SENSORS][8]; +uint8_t ds18x20_index[DS18X20_MAX_SENSORS]; +uint8_t ds18x20_valid[DS18X20_MAX_SENSORS]; +uint8_t ds18x20_sensors = 0; +char ds18x20_types[12]; + +/********************************************************************************************/ + +#include + +OneWire *ds = nullptr; + +void Ds18x20Init(void) { + ds = new OneWire(Pin(GPIO_DSB)); + + Ds18x20Search(); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSORS_FOUND " %d"), ds18x20_sensors); +} + +void Ds18x20Search(void) { + uint8_t num_sensors=0; + uint8_t sensor = 0; + + ds->reset_search(); + for (num_sensors = 0; num_sensors < DS18X20_MAX_SENSORS; num_sensors) { + if (!ds->search(ds18x20_address[num_sensors])) { + ds->reset_search(); + break; + } + // If CRC Ok and Type DS18S20, DS1822, DS18B20 or MAX31850 + if ((OneWire::crc8(ds18x20_address[num_sensors], 7) == ds18x20_address[num_sensors][7]) && + ((ds18x20_address[num_sensors][0]==DS18S20_CHIPID) || + (ds18x20_address[num_sensors][0]==DS1822_CHIPID) || + (ds18x20_address[num_sensors][0]==DS18B20_CHIPID) || + (ds18x20_address[num_sensors][0]==MAX31850_CHIPID))) { + num_sensors++; + } + } + for (uint32_t i = 0; i < num_sensors; i++) { + ds18x20_index[i] = i; + } + for (uint32_t i = 0; i < num_sensors; i++) { + for (uint32_t j = i + 1; j < num_sensors; j++) { + if (uint32_t(ds18x20_address[ds18x20_index[i]]) > uint32_t(ds18x20_address[ds18x20_index[j]])) { + std::swap(ds18x20_index[i], ds18x20_index[j]); + } + } + } + ds18x20_sensors = num_sensors; +} + +void Ds18x20Convert(void) { + ds->reset(); + ds->write(W1_SKIP_ROM); // Address all Sensors on Bus + ds->write(W1_CONVERT_TEMP); // start conversion, no parasite power on at the end +// delay(750); // 750ms should be enough for 12bit conv +} + +bool Ds18x20Read(uint8_t sensor, float &t) +{ + uint8_t data[12]; + int8_t sign = 1; + + t = NAN; + + uint8_t index = ds18x20_index[sensor]; + if (ds18x20_valid[index]) { ds18x20_valid[index]--; } + + ds->reset(); + ds->select(ds18x20_address[index]); + ds->write(W1_READ_SCRATCHPAD); // Read Scratchpad + + for (uint32_t i = 0; i < 9; i++) { + data[i] = ds->read(); + } + if (OneWire::crc8(data, 8) == data[8]) { + switch(ds18x20_address[index][0]) { + case DS18S20_CHIPID: { + int16_t tempS = (((data[1] << 8) | (data[0] & 0xFE)) << 3) | ((0x10 - data[6]) & 0x0F); + t = ConvertTemp(tempS * 0.0625 - 0.250); + ds18x20_valid[index] = SENSOR_MAX_MISS; + return true; + } + case DS1822_CHIPID: + case DS18B20_CHIPID: { + uint16_t temp12 = (data[1] << 8) + data[0]; + if (temp12 > 2047) { + temp12 = (~temp12) +1; + sign = -1; + } + t = ConvertTemp(sign * temp12 * 0.0625); // Divide by 16 + ds18x20_valid[index] = SENSOR_MAX_MISS; + return true; + } + case MAX31850_CHIPID: { + int16_t temp14 = (data[1] << 8) + (data[0] & 0xFC); + t = ConvertTemp(temp14 * 0.0625); // Divide by 16 + ds18x20_valid[index] = SENSOR_MAX_MISS; + return true; + } + } + } + AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DSB D_SENSOR_CRC_ERROR)); + return false; +} + +void Ds18x20Name(uint8_t sensor) +{ + uint8_t index = sizeof(ds18x20_chipids); + while (index) { + if (ds18x20_address[ds18x20_index[sensor]][0] == ds18x20_chipids[index]) { + break; + } + index--; + } + GetTextIndexed(ds18x20_types, sizeof(ds18x20_types), index, kDs18x20Types); + if (ds18x20_sensors > 1) { + snprintf_P(ds18x20_types, sizeof(ds18x20_types), PSTR("%s%c%d"), ds18x20_types, IndexSeparator(), sensor +1); + } +} + +/********************************************************************************************/ + +void Ds18x20EverySecond(void) +{ + if (!ds18x20_sensors) { return; } + + if (uptime & 1) { + // 2mS +// Ds18x20Search(); // Check for changes in sensors number + Ds18x20Convert(); // Start Conversion, takes up to one second + } else { + float t; + for (uint32_t i = 0; i < ds18x20_sensors; i++) { + // 12mS per device + if (!Ds18x20Read(i, t)) { // Read temperature + Ds18x20Name(i); + AddLogMissed(ds18x20_types, ds18x20_valid[ds18x20_index[i]]); + } + } + } +} + +void Ds18x20Show(bool json) +{ + float t; + + uint8_t dsxflg = 0; + for (uint32_t i = 0; i < ds18x20_sensors; i++) { + if (Ds18x20Read(i, t)) { // Check if read failed + char temperature[33]; + dtostrfd(t, Settings.flag2.temperature_resolution, temperature); + + Ds18x20Name(i); + + if (json) { + char address[17]; + for (uint32_t j = 0; j < 6; j++) { + sprintf(address+2*j, "%02X", ds18x20_address[ds18x20_index[i]][6-j]); // Skip sensor type and crc + } + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ID "\":\"%s\",\"" D_JSON_TEMPERATURE "\":%s}"), ds18x20_types, address, temperature); + dsxflg++; +#ifdef USE_DOMOTICZ + if ((0 == tele_period) && (1 == dsxflg)) { + DomoticzSensor(DZ_TEMP, temperature); + } +#endif // USE_DOMOTICZ +#ifdef USE_KNX + if ((0 == tele_period) && (1 == dsxflg)) { + KnxSensor(KNX_TEMPERATURE, t); + } +#endif // USE_KNX +#ifdef USE_WEBSERVER + } else { + WSContentSend_PD(HTTP_SNS_TEMP, ds18x20_types, temperature, TempUnit()); +#endif // USE_WEBSERVER + } + } + } +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +bool Xsns05(uint8_t function) +{ + bool result = false; + + if (PinUsed(GPIO_DSB)) { + switch (function) { + case FUNC_INIT: + Ds18x20Init(); + break; + case FUNC_EVERY_SECOND: + Ds18x20EverySecond(); + break; + case FUNC_JSON_APPEND: + Ds18x20Show(1); + break; +#ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Ds18x20Show(0); + break; +#endif // USE_WEBSERVER + } + } + return result; +} + +#endif // USE_DS18x20 +#endif // ESP32 diff --git a/tasmota/xsns_09_bmp.ino b/tasmota/xsns_09_bmp.ino index 9e3ee59b4..8e1bd074a 100644 --- a/tasmota/xsns_09_bmp.ino +++ b/tasmota/xsns_09_bmp.ino @@ -613,15 +613,17 @@ void BmpShow(bool json) void BMP_EnterSleep(void) { - for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { - switch (bmp_sensors[bmp_idx].bmp_type) { - case BMP180_CHIPID: - case BMP280_CHIPID: - case BME280_CHIPID: - I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BMP_REGISTER_RESET, BMP_CMND_RESET); - break; - default: - break; + if (DeepSleepEnabled()) { + for (uint32_t bmp_idx = 0; bmp_idx < bmp_count; bmp_idx++) { + switch (bmp_sensors[bmp_idx].bmp_type) { + case BMP180_CHIPID: + case BMP280_CHIPID: + case BME280_CHIPID: + I2cWrite8(bmp_sensors[bmp_idx].bmp_address, BMP_REGISTER_RESET, BMP_CMND_RESET); + break; + default: + break; + } } } } diff --git a/tasmota/xsns_16_tsl2561.ino b/tasmota/xsns_16_tsl2561.ino index 99c243329..573fc3b93 100644 --- a/tasmota/xsns_16_tsl2561.ino +++ b/tasmota/xsns_16_tsl2561.ino @@ -37,6 +37,8 @@ Tsl2561 Tsl(Wire); uint8_t tsl2561_type = 0; uint8_t tsl2561_valid = 0; uint32_t tsl2561_milliLux = 0; +uint32_t tsl2561_full = 0; +uint32_t tsl2561_ir = 0; char tsl2561_types[] = "TSL2561"; bool Tsl2561Read(void) @@ -47,15 +49,18 @@ bool Tsl2561Read(void) bool gain; Tsl2561::exposure_t exposure; uint16_t scaledFull, scaledIr; - uint32_t full, ir; if (Tsl.on()) { if (Tsl.id(id) && Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) - && Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) - && Tsl2561Util::milliLux(full, ir, tsl2561_milliLux, Tsl2561::packageCS(id))) { + && Tsl2561Util::normalizedLuminosity(gain, exposure, tsl2561_full = scaledFull, tsl2561_ir = scaledIr)) { + if (! Tsl2561Util::milliLux(tsl2561_full, tsl2561_ir, tsl2561_milliLux, Tsl2561::packageCS(id))) { + tsl2561_milliLux = 0; + } } else{ tsl2561_milliLux = 0; + tsl2561_full = 0; + tsl2561_ir = 0; } } tsl2561_valid = SENSOR_MAX_MISS; @@ -94,8 +99,8 @@ void Tsl2561Show(bool json) { if (tsl2561_valid) { if (json) { - ResponseAppend_P(PSTR(",\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u}"), - tsl2561_milliLux / 1000, tsl2561_milliLux % 1000); + ResponseAppend_P(PSTR(",\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u,\"IR\":%u,\"Broadband\":%u}"), + tsl2561_milliLux / 1000, tsl2561_milliLux % 1000, tsl2561_ir, tsl2561_full); #ifdef USE_DOMOTICZ if (0 == tele_period) { DomoticzSensor(DZ_ILLUMINANCE, (tsl2561_milliLux + 500) / 1000); } #endif // USE_DOMOTICZ diff --git a/tasmota/xsns_40_pn532.ino b/tasmota/xsns_40_pn532.ino index 3c29d4c82..8fd4e450d 100644 --- a/tasmota/xsns_40_pn532.ino +++ b/tasmota/xsns_40_pn532.ino @@ -495,7 +495,7 @@ void PN532_ScanForTag(void) #endif // USE_PN532_DATA_FUNCTION #ifdef USE_PN532_DATA_FUNCTION - ResponseTime_P(PSTR(",\"PN532\":{\"UID\":\"%s\", \"DATA\":\"%s\"}}"), uids, card_datas); + ResponseTime_P(PSTR(",\"PN532\":{\"UID\":\"%s\", \"" D_JSON_DATA "\":\"%s\"}}"), uids, card_datas); #else ResponseTime_P(PSTR(",\"PN532\":{\"UID\":\"%s\"}}"), uids); #endif // USE_PN532_DATA_FUNCTION diff --git a/tasmota/xsns_61_MI_NRF24.ino b/tasmota/xsns_61_MI_NRF24.ino index 44456a6a7..e9d2841ea 100644 --- a/tasmota/xsns_61_MI_NRF24.ino +++ b/tasmota/xsns_61_MI_NRF24.ino @@ -20,7 +20,8 @@ -------------------------------------------------------------------------------------------- Version yyyymmdd Action Description -------------------------------------------------------------------------------------------- - + 0.9.8.1 20200918 integrate - add MHOC303, ATC-custom FW, allow lower case commands + --- 0.9.8.0 20200705 integrate - add YEE-RC, NLIGHT and MJYD2S, add NRFUSE --- 0.9.7.0 20200624 integrate - fix BEARSSL-decryption, remove MBEDTLS, prepare night light sensors @@ -84,8 +85,10 @@ #define MJYD2S 8 #define YEERC 9 #define MHOC401 10 +#define MHOC303 11 +#define ATC 12 -#define MI_TYPES 10 //count this manually +#define MI_TYPES 12 //count this manually #define D_CMND_NRF "NRF" @@ -121,7 +124,9 @@ const uint16_t kMINRFDeviceID[MI_TYPES]={ 0x0098, // Flora 0x03dd, // NLIGHT 0x07f6, // MJYD2S 0x0153, // yee-rc - 0x0387 // MHO-C401 + 0x0387, // MHO-C401 + 0x06d3, // MHO-C303 + 0x0a1c // ATC -> this is a fake ID }; const char kMINRFDeviceType1[] PROGMEM = "Flora"; @@ -134,7 +139,9 @@ const char kMINRFDeviceType7[] PROGMEM = "NLIGHT"; const char kMINRFDeviceType8[] PROGMEM = "MJYD2S"; const char kMINRFDeviceType9[] PROGMEM = "YEERC"; const char kMINRFDeviceType10[] PROGMEM = "MHOC401"; -const char * kMINRFDeviceType[] PROGMEM = {kMINRFDeviceType1,kMINRFDeviceType2,kMINRFDeviceType3,kMINRFDeviceType4,kMINRFDeviceType5,kMINRFDeviceType6,kMINRFDeviceType7,kMINRFDeviceType8,kMINRFDeviceType9,kMINRFDeviceType10}; +const char kMINRFDeviceType11[] PROGMEM = "MHOC303"; +const char kMINRFDeviceType12[] PROGMEM = "ATC"; +const char * kMINRFDeviceType[] PROGMEM = {kMINRFDeviceType1,kMINRFDeviceType2,kMINRFDeviceType3,kMINRFDeviceType4,kMINRFDeviceType5,kMINRFDeviceType6,kMINRFDeviceType7,kMINRFDeviceType8,kMINRFDeviceType9,kMINRFDeviceType10,kMINRFDeviceType11,kMINRFDeviceType12}; // PDU's or different channels 37-39 const uint32_t kMINRFFloPDU[3] = {0x3eaa857d,0xef3b8730,0x71da7b46}; @@ -146,11 +153,13 @@ const uint32_t kMINRFCGGPDU[3] = {0x4760cd6e,0xdbcc0cdb,0x33048dfd}; const uint32_t kMINRFCGDPDU[3] = {0x5da0d752,0xc10c16e7,0x29c497c1}; // const uint32_t kMINRFNLIPDU[3] = {0x4760C56E,0xDBCC04DB,0x0330485FD}; //NLIGHT const uint32_t kMINRFYRCPDU[3] = {0x216D63E2,0x5C3DD47E,0x0A5D0E96}; //yee-rc - 50 30 +const uint32_t kMINRFATCPDU[3] = {0xA6E4D00A,0xD0CDAD5A,0x8B03FB3A}; //ATC // start-LSFR for different channels 37-39 const uint8_t kMINRFlsfrList_A[3] = {0x4b,0x17,0x23}; // Flora, LYWSD02 const uint8_t kMINRFlsfrList_B[3] = {0x21,0x72,0x43}; // MJ_HT_V1, LYWSD03, CGx const uint8_t kMINRFlsfrList_C[3] = {0x38,0x25,0x2e}; // yee-rc +const uint8_t kMINRFlsfrList_D[3] = {0x26,0x23,0x20}; // ATC #pragma pack(1) // important!! @@ -224,6 +233,15 @@ struct mjysd02_Packet_t{ uint8_t data[18]; }; +struct ATCPacket_t{ + uint8_t MAC[6]; + int16_t temp; //sadly this is in wrong endianess + uint8_t hum; + uint8_t batPer; + uint16_t batMV; + uint8_t frameCnt; +}; + union mi_bindKey_t{ struct{ uint8_t key[16]; @@ -363,7 +381,7 @@ bool MINRFinitBLE(uint8_t _mode) MINRFchangePacketModeTo(_mode); return true; } - // DEBUG_SENSOR_LOG(PSTR("MINRF chip NOT !!!! connected")); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF chip NOT !!!! connected")); return false; } @@ -406,35 +424,38 @@ bool MINRFreceivePacket(void) MINRFswapbuf((uint8_t*)&MINRF.buffer, sizeof(MINRF.buffer) ); // MINRF_LOG_BUFFER(); - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: _lsfrlist: %x, chan: %u, mode: %u"),_lsfrlist[MINRF.currentChan],MINRF.currentChan, MINRF.packetMode); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: _lsfrlist: %x, chan: %u, mode: %u"),_lsfrlist[MINRF.currentChan],MINRF.currentChan, MINRF.packetMode); switch (MINRF.packetMode) { - case 0: case 7: case 8: + case 0: case NLIGHT: case MJYD2S: MINRFwhiten((uint8_t *)&MINRF.buffer, sizeof(MINRF.buffer), MINRF.channel[MINRF.currentChan] | 0x40); // "BEACON" mode, "NLIGHT" mode, "MJYD2S" mode break; - case 1: case 3: + case FLORA: case LYWSD02: case MHOC303: MINRFwhiten((uint8_t *)&MINRF.buffer, sizeof(MINRF.buffer), kMINRFlsfrList_A[MINRF.currentChan]); // "flora" mode, "LYWSD02" mode break; - case 2: case 4: case 5: case 6: case MHOC401: + case MJ_HT_V1: case LYWSD03: case CGG1: case CGD1: case MHOC401: MINRFwhiten((uint8_t *)&MINRF.buffer, sizeof(MINRF.buffer), kMINRFlsfrList_B[MINRF.currentChan]); // "MJ_HT_V1" mode, LYWSD03" mode, "CGG1" mode, "CGD1" mode break; - case 9: + case YEERC: MINRFwhiten((uint8_t *)&MINRF.buffer, sizeof(MINRF.buffer), kMINRFlsfrList_C[MINRF.currentChan]); // "YEE-RC" mode break; + case ATC: + MINRFwhiten((uint8_t *)&MINRF.buffer, sizeof(MINRF.buffer), kMINRFlsfrList_D[MINRF.currentChan]); // ATC + break; } - // DEBUG_SENSOR_LOG(PSTR("MINRF: LSFR:%x"),_lsfr); + // DEBUG_SENSOR_LOG(PSTR("NRF: LSFR:%x"),_lsfr); // if (_lsfr>254) _lsfr=0; } - // DEBUG_SENSOR_LOG(PSTR("MINRF: did read FIFO")); + // DEBUG_SENSOR_LOG(PSTR("NRF: did read FIFO")); return true; } // #ifdef DEBUG_TASMOTA_SENSOR void MINRFshowBuffer(uint8_t (&buf)[32]){ // we use this only for the 32-byte-FIFO-buffer, so 32 is hardcoded - // DEBUG_SENSOR_LOG(PSTR("MINRF: Buffer: %c %c %c %c %c %c %c %c" + // DEBUG_SENSOR_LOG(PSTR("NRF: Buffer: %c %c %c %c %c %c %c %c" // " %c %c %c %c %c %c %c %c" // " %c %c %c %c %c %c %c %c" // " %c %c %c %c %c %c %c %c") - DEBUG_SENSOR_LOG(PSTR("MINRF: Buffer: %02x %02x %02x %02x %02x %02x %02x %02x " + DEBUG_SENSOR_LOG(PSTR("NRF: Buffer: %02x %02x %02x %02x %02x %02x %02x %02x " "%02x %02x %02x %02x %02x %02x %02x %02x " "%02x %02x %02x %02x %02x %02x %02x %02x " "%02x %02x %02x %02x %02x %02x %02x %02x ") @@ -511,7 +532,7 @@ void MINRFhandleScan(void){ MINRFscanResult.erase(std::remove_if(MINRFscanResult.begin(), MINRFscanResult.end(), [&i](scan_entry_t e) { - if(e.showedUp>2) AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: Beacon %02u: %02X%02X%02X%02X%02X%02X Cid: %04X Svc: %04X UUID: %04X"),i,e.MAC[0],e.MAC[1],e.MAC[2],e.MAC[3],e.MAC[4],e.MAC[5],e.cid,e.svc,e.uuid); + if(e.showedUp>2) AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: Beacon %02u: %02X%02X%02X%02X%02X%02X Cid: %04X Svc: %04X UUID: %04X"),i,e.MAC[0],e.MAC[1],e.MAC[2],e.MAC[3],e.MAC[4],e.MAC[5],e.cid,e.svc,e.uuid); i++; return ((e.showedUp < 3)); }), @@ -524,7 +545,7 @@ void MINRFhandleScan(void){ for(uint32_t i=0; i30) break; uint32_t ADtype = _buf[i+1]; - // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: Size: %u AD: %x i:%u"), size, ADtype,i); + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: Size: %u AD: %x i:%u"), size, ADtype,i); if (size+i>32+offset) size=32-i+offset-2; if (size>30) break; char _stemp[(size*2)]; uint32_t backupSize; switch(ADtype){ case 0x01: - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: Flags: %02x"), _buf[i+2]); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: Flags: %02x"), _buf[i+2]); break; case 0x02: case 0x03: entry->uuid = _buf[i+3]*256 + _buf[i+2]; - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: UUID: %04x"), entry->uuid); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: UUID: %04x"), entry->uuid); success = true; break; case 0x08: case 0x09: backupSize = _buf[i+size+1]; _buf[i+size+1] = 0; - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: Name: %s"), (char*)&_buf[i+2]); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: Name: %s"), (char*)&_buf[i+2]); success = true; _buf[i+size+1] = backupSize; break; case 0x0a: - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: TxPow: %02u"), _buf[i+2]); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: TxPow: %02u"), _buf[i+2]); break; case 0xff: entry->cid = _buf[i+3]*256 + _buf[i+2]; - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: Cid: %04x"), entry->cid); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: Cid: %04x"), entry->cid); ToHex_P((unsigned char*)&_buf+i+4,size-3,_stemp,(size*2)); AddLog_P2(LOG_LEVEL_DEBUG,PSTR("%s"),_stemp); success = true; break; case 0x16: entry->svc = _buf[i+3]*256 + _buf[i+2]; - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: Svc: %04x"), entry->svc); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: Svc: %04x"), entry->svc); ToHex_P((unsigned char*)&_buf+i+4,size-3,_stemp,(size*2)); AddLog_P2(LOG_LEVEL_DEBUG,PSTR("%s"),_stemp); success = true; @@ -712,7 +733,7 @@ int MINRFdecryptPacket(char *_buf){ br_ccm_run(&ctx, 0, output, sizeof(packet->payload.cipher)); ret = br_ccm_check_tag(&ctx, packet->payload.tag); - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("BEARSSL: Err:%i, Decrypted : %02x %02x %02x %02x %02x "), ret, output[0],output[1],output[2],output[3],output[4]); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: Err:%i, Decrypted : %02x %02x %02x %02x %02x "), ret, output[0],output[1],output[2],output[3],output[4]); memcpy((uint8_t*)(packet->payload.cipher)+1,output,sizeof(packet->payload.cipher)); return ret; } @@ -833,6 +854,7 @@ void MINRFAddKey(char* payload){ */ void MINRFKeyMACStringToBytes(char* _string,uint8_t _keyMAC[]) { //uppercase uint32_t index = 0; + UpperCase(_string,_string); while (index < 44) { char c = _string[index]; uint8_t value = 0; @@ -843,9 +865,9 @@ void MINRFKeyMACStringToBytes(char* _string,uint8_t _keyMAC[]) { //uppercase _keyMAC[(index/2)] += value << (((index + 1) % 2) * 4); index++; } - DEBUG_SENSOR_LOG(PSTR("MINRF: %s to:"),_string); - DEBUG_SENSOR_LOG(PSTR("MINRF: key-array: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"),_keyMAC[0],_keyMAC[1],_keyMAC[2],_keyMAC[3],_keyMAC[4],_keyMAC[5],_keyMAC[6],_keyMAC[7],_keyMAC[8],_keyMAC[9],_keyMAC[10],_keyMAC[11],_keyMAC[12],_keyMAC[13],_keyMAC[14],_keyMAC[15]); - DEBUG_SENSOR_LOG(PSTR("MINRF: MAC-array: %02X%02X%02X%02X%02X%02X"),_keyMAC[16],_keyMAC[17],_keyMAC[18],_keyMAC[19],_keyMAC[20],_keyMAC[21]); + DEBUG_SENSOR_LOG(PSTR("NRF: %s to:"),_string); + DEBUG_SENSOR_LOG(PSTR("NRF: key-array: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"),_keyMAC[0],_keyMAC[1],_keyMAC[2],_keyMAC[3],_keyMAC[4],_keyMAC[5],_keyMAC[6],_keyMAC[7],_keyMAC[8],_keyMAC[9],_keyMAC[10],_keyMAC[11],_keyMAC[12],_keyMAC[13],_keyMAC[14],_keyMAC[15]); + DEBUG_SENSOR_LOG(PSTR("NRF: MAC-array: %02X%02X%02X%02X%02X%02X"),_keyMAC[16],_keyMAC[17],_keyMAC[18],_keyMAC[19],_keyMAC[20],_keyMAC[21]); } #endif //USE_MI_DECRYPTION /** @@ -856,6 +878,7 @@ void MINRFKeyMACStringToBytes(char* _string,uint8_t _keyMAC[]) { //uppercase */ void MINRFMACStringToBytes(char* _string, uint8_t _MAC[]) { //uppercase uint32_t index = 0; + UpperCase(_string,_string); while (index < 12) { char c = _string[index]; uint8_t value = 0; @@ -866,7 +889,7 @@ void MINRFMACStringToBytes(char* _string, uint8_t _MAC[]) { //uppercase _MAC[(index/2)] += value << (((index + 1) % 2) * 4); index++; } - // DEBUG_SENSOR_LOG(PSTR("MINRF: %s to MAC-array: %02X%02X%02X%02X%02X%02X"),_string,_MAC[0],_MAC[1],_MAC[2],_MAC[3],_MAC[4],_MAC[5]); + // DEBUG_SENSOR_LOG(PSTR("NRF: %s to MAC-array: %02X%02X%02X%02X%02X%02X"),_string,_MAC[0],_MAC[1],_MAC[2],_MAC[3],_MAC[4],_MAC[5]); } /** @@ -876,7 +899,7 @@ void MINRFMACStringToBytes(char* _string, uint8_t _MAC[]) { //uppercase void MINRFcomputefirstUsedPacketMode(void){ for (uint32_t i = 0; iMI_TYPES) MINRF.firstUsedPacketMode=0; break; @@ -913,34 +936,37 @@ void MINRFchangePacketModeTo(uint8_t _mode) { case 0: // normal BLE advertisement NRF24radio.openReadingPipe(0,0x6B7D9171); // advertisement address: 0x8E89BED6 (bit-reversed -> 0x6B7D9171) break; - case 1: // special flora packet + case FLORA: // special flora packet NRF24radio.openReadingPipe(0,kMINRFFloPDU[_nextchannel]); // 95 fe 71 20 -> flora break; - case 2: // special MJ_HT_V1 packet + case MJ_HT_V1: // special MJ_HT_V1 packet NRF24radio.openReadingPipe(0,kMINRFMJPDU[_nextchannel]); // 95 fe 50 20 -> MJ_HT_V1 break; - case 3: // special LYWSD02 packet + case LYWSD02: case MHOC303: // special LYWSD02 packet NRF24radio.openReadingPipe(0,kMINRFL2PDU[_nextchannel]);// 95 fe 70 20 -> LYWSD02 break; - case 4: case MHOC401: // special LYWSD03 packet, MHOC401 has the same + case LYWSD03: case MHOC401: // special LYWSD03 packet, MHOC401 has the same NRF24radio.openReadingPipe(0,kMINRFL3PDU[_nextchannel]);// 95 fe 58 58 -> LYWSD03 (= encrypted data message) break; - case 5: // special CGG1 packet + case CGG1: // special CGG1 packet NRF24radio.openReadingPipe(0,kMINRFCGGPDU[_nextchannel]); // 95 fe 50 30 -> CGG1 break; - case 6: // special CGD1 packet + case CGD1: // special CGD1 packet NRF24radio.openReadingPipe(0,kMINRFCGDPDU[_nextchannel]); // cd fd 08 0c -> CGD1 break; - case 7: case 8:// MAC based LIGHT packet + case NLIGHT: case MJYD2S:// MAC based LIGHT packet if (MIBLElights.size()==0) break; NRF24radio.openReadingPipe(0,MIBLElights[MINRF.activeLight].PDU[_nextchannel]); // computed from MAC -> NLIGHT and MJYSD2S MINRF.activeLight++; break; - case 9: // YEE-RC packet + case YEERC: // YEE-RC packet NRF24radio.openReadingPipe(0,kMINRFYRCPDU[_nextchannel]);// 95 fe 50 30 -> YEE-RC break; + case ATC: + NRF24radio.openReadingPipe(0,kMINRFATCPDU[_nextchannel]);// 10 16 1a 18 -> ATC + break; } - // DEBUG_SENSOR_LOG(PSTR("MINRF: Change Mode to %u"),_mode); + // DEBUG_SENSOR_LOG(PSTR("NRF: Change Mode to %u"),_mode); MINRF.packetMode = _mode; } @@ -953,27 +979,27 @@ void MINRFchangePacketModeTo(uint8_t _mode) { */ uint32_t MINRFgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type){ - DEBUG_SENSOR_LOG(PSTR("MINRF: will test ID-type: %x"), _type); + DEBUG_SENSOR_LOG(PSTR("NRF: will test ID-type: %x"), _type); bool _success = false; for (uint32_t i=0;itype,MINRF.buffer.miBeacon.type); + DEBUG_SENSOR_LOG(PSTR("NRF: %u %u %u"),_slot,_sensorVec->type,MINRF.buffer.miBeacon.type); float _tempFloat; int decryptRet; @@ -1082,7 +1108,7 @@ void MINRFhandleMiBeaconPacket(void){ switch(MINRF.buffer.miBeacon.type){ case 0x1: if(MINRF.buffer.miBeacon.counter==_sensorVec->lastCnt) break; - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: YEE-RC button: %u Long: %u"), MINRF.buffer.miBeacon.Btn.num, MINRF.buffer.miBeacon.Btn.longPress); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: YEE-RC button: %u Long: %u"), MINRF.buffer.miBeacon.Btn.num, MINRF.buffer.miBeacon.Btn.longPress); _sensorVec->lastCnt=MINRF.buffer.miBeacon.counter; _sensorVec->btn=MINRF.buffer.miBeacon.Btn.num + (MINRF.buffer.miBeacon.Btn.longPress/2)*6; _sensorVec->shallSendMQTT = 1; @@ -1154,7 +1180,7 @@ void MINRFhandleMiBeaconPacket(void){ void MINRFhandleCGD1Packet(void){ // no MiBeacon MINRFreverseMAC(MINRF.buffer.CGDPacket.MAC); uint32_t _slot = MINRFgetSensorSlot(MINRF.buffer.CGDPacket.MAC, 0x0576); // This must be hard-coded, no object-id in Cleargrass-packet - DEBUG_SENSOR_LOG(PSTR("MINRF: Sensor slot: %u"), _slot); + DEBUG_SENSOR_LOG(PSTR("NRF: Sensor slot: %u"), _slot); if(_slot==0xff) return; switch (MINRF.buffer.CGDPacket.mode){ @@ -1179,7 +1205,7 @@ void MINRFhandleCGD1Packet(void){ // no MiBeacon } break; default: - DEBUG_SENSOR_LOG(PSTR("MINRF: unexpected CGD1-packet")); + DEBUG_SENSOR_LOG(PSTR("NRF: unexpected CGD1-packet")); MINRF_LOG_BUFFER(MINRF.buffer.raw); } } @@ -1188,10 +1214,10 @@ void MINRFhandleNlightPacket(void){ // no MiBeacon uint32_t offset = 6; uint8_t _buf[32+offset]; MINRFrecalcBuffer((uint8_t*)&_buf,offset); - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: NLIGHT: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"),_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6],_buf[7],_buf[8],_buf[9],_buf[10],_buf[11],_buf[12],_buf[13],_buf[14],_buf[15],_buf[16],_buf[17],_buf[18]); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: NLIGHT: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"),_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6],_buf[7],_buf[8],_buf[9],_buf[10],_buf[11],_buf[12],_buf[13],_buf[14],_buf[15],_buf[16],_buf[17],_buf[18]); uint32_t _frame_PID = _buf[15]<<24 | _buf[16]<<16 | _buf[17]<<8 | _buf[18]; if(_frame_PID!=0x4030dd03) return; // invalid packet - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: NLIGHT:%x"),_frame_PID); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: NLIGHT:%x"),_frame_PID); uint32_t _idx = MINRF.activeLight-1; if((millis() - MIBLElights[_idx].lastTime)<1500) return; if(_buf[19]!=MIBLElights[_idx].lastCnt){ @@ -1199,7 +1225,7 @@ void MINRFhandleNlightPacket(void){ // no MiBeacon MIBLElights[_idx].events++; MIBLElights[_idx].shallSendMQTT = 1; MIBLElights[_idx].lastTime = millis(); - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: NLIGHT %u: events: %u, Cnt:%u"), _idx,MIBLElights[_idx].events, MIBLElights[_idx].lastCnt); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: NLIGHT %u: events: %u, Cnt:%u"), _idx,MIBLElights[_idx].events, MIBLElights[_idx].lastCnt); } } @@ -1209,20 +1235,20 @@ void MINRFhandleMJYD2SPacket(void){ // no MiBeacon MINRFrecalcBuffer((uint8_t*)&_buf,offset); mjysd02_Packet_t *_packet = (mjysd02_Packet_t*)&_buf; if(_packet->PID!=0x07f6) return; // invalid packet - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: MJYD2S: %02u %04x %04x %04x %02x"),_packet->payloadSize,_packet->UUID,_packet->frameCtrl,_packet->PID,_packet->frameCnt); - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: PAYLOAD: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"),_packet->data[0],_packet->data[1],_packet->data[2],_packet->data[3],_packet->data[4],_packet->data[5],_packet->data[6],_packet->data[7],_packet->data[8],_packet->data[9],_packet->data[10],_packet->data[11],_packet->data[12],_packet->data[13],_packet->data[14],_packet->data[15],_packet->data[16],_packet->data[17]); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: MJYD2S: %02u %04x %04x %04x %02x"),_packet->payloadSize,_packet->UUID,_packet->frameCtrl,_packet->PID,_packet->frameCnt); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: PAYLOAD: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"),_packet->data[0],_packet->data[1],_packet->data[2],_packet->data[3],_packet->data[4],_packet->data[5],_packet->data[6],_packet->data[7],_packet->data[8],_packet->data[9],_packet->data[10],_packet->data[11],_packet->data[12],_packet->data[13],_packet->data[14],_packet->data[15],_packet->data[16],_packet->data[17]); uint32_t _idx = MINRF.activeLight-1; switch(_packet->frameCtrl){ case 0x5910: if(_packet->frameCnt!=MIBLElights[_idx].lastCnt){ - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: MJYD2S after motion:%x"),_packet->frameCnt); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: MJYD2S after motion:%x"),_packet->frameCnt); MIBLElights[_idx].lastCnt = _packet->frameCnt; if(millis()-MIBLElights[_idx].lastTime>120000){ MIBLElights[_idx].eventType = 1; MIBLElights[_idx].events++; MIBLElights[_idx].shallSendMQTT = 1; MIBLElights[_idx].lastTime = millis(); - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: MJYD2S secondary PIR")); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: MJYD2S secondary PIR")); } } break; @@ -1238,7 +1264,7 @@ void MINRFhandleMJYD2SPacket(void){ // no MiBeacon if(millis()-MIBLElights[_idx].lastTime>1000){ MIBLElights[_idx].eventType = 1; //PIR MIBLElights[_idx].shallSendMQTT = 1; - AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: MJYD2S primary PIR")); + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: MJYD2S primary PIR")); MIBLElights[_idx].events++; } MIBLElights[_idx].lastTime = millis(); @@ -1259,23 +1285,23 @@ void MINRFhandleMJYD2SPacket(void){ // no MiBeacon MIBLElights[_idx].NMT = output[6]<<24 | output[5]<<16 | output[4]<<8 | output[3]; MIBLElights[_idx].eventType = 3; // NMT 0, 120, 300, 600, 1800, ... seconds MIBLElights[_idx].shallSendMQTT = 1; - // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("MINRF: MJYD2S NMT: %u"), MIBLElights[_idx].NMT ); + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("NRF: MJYD2S NMT: %u"), MIBLElights[_idx].NMT ); break; } } } - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: NLIGHT:%x"),_frame_PID); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: NLIGHT:%x"),_frame_PID); } void MINRFhandleLightPacket(void){ switch(MIBLElights[MINRF.activeLight-1].type){ case NLIGHT: - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: NLIGHT!!")); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: NLIGHT!!")); MINRFhandleNlightPacket(); break; case MJYD2S: - // AddLog_P2(LOG_LEVEL_INFO,PSTR("MINRF: MJYD2S !!")); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: MJYD2S !!")); MINRFhandleMJYD2SPacket(); break; } @@ -1285,7 +1311,7 @@ void MINRFhandleLightPacket(void){ void MINRFaddLight(uint8_t _MAC[], uint8_t _type){ // no MiBeacon for(uint32_t i=0; iMAC, 0x0a1c); // This must be a hard-coded fake ID + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("known %s at slot %u"), kMINRFDeviceType[MIBLEsensors[_slot].type-1],_slot); + // AddLog_P2(LOG_LEVEL_INFO,PSTR("NRF: ATC: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"),MINRF.buffer.raw[0],MINRF.buffer.raw[1],MINRF.buffer.raw[2],MINRF.buffer.raw[3],MINRF.buffer.raw[4],MINRF.buffer.raw[5],MINRF.buffer.raw[6],MINRF.buffer.raw[7],MINRF.buffer.raw[8],MINRF.buffer.raw[9],MINRF.buffer.raw[10],MINRF.buffer.raw[11]); + if(_slot==0xff) return; + + MIBLEsensors.at(_slot).temp = (float)(__builtin_bswap16(_packet->temp))/10.0f; + MIBLEsensors.at(_slot).hum = (float)_packet->hum; + MIBLEsensors.at(_slot).bat = _packet->batPer; + + MIBLEsensors[_slot].shallSendMQTT = 1; } /*********************************************************************************************\ @@ -1309,14 +1349,15 @@ void MINRFaddLight(uint8_t _MAC[], uint8_t _type){ // no MiBeacon void MINRF_EVERY_50_MSECOND() { // Every 50mseconds if(MINRF.timer>6000){ // happens every 6000/20 = 300 seconds - DEBUG_SENSOR_LOG(PSTR("MINRF: check for FAKE sensors")); + DEBUG_SENSOR_LOG(PSTR("NRF: check for FAKE sensors")); MINRFpurgeFakeSensors(); MINRF.timer=0; } MINRF.timer++; if (!MINRFreceivePacket()){ - // DEBUG_SENSOR_LOG(PSTR("MINRF: nothing received")); + // DEBUG_SENSOR_LOG(PSTR("NRF: nothing received")); + // if (MINRF.packetMode==ATC) AddLog_P2(LOG_LEVEL_INFO,PSTR("no ATC..")); } else { @@ -1327,7 +1368,7 @@ void MINRF_EVERY_50_MSECOND() { // Every 50mseconds } else MINRFhandleScan(); break; - case FLORA: case MJ_HT_V1: case LYWSD02: case CGG1: case LYWSD03: case YEERC: case MHOC401: + case FLORA: case MJ_HT_V1: case LYWSD02: case CGG1: case LYWSD03: case YEERC: case MHOC401: case MHOC303: MINRFhandleMiBeaconPacket(); break; case CGD1: @@ -1336,6 +1377,9 @@ void MINRF_EVERY_50_MSECOND() { // Every 50mseconds case NLIGHT: //case MJYD2S: MINRFhandleLightPacket(); break; + case ATC: + MINRFhandleATCPacket(); + break; default: break; } @@ -1534,7 +1578,7 @@ void MINRFShow(bool json) if (json) { for (uint32_t i = 0; i < MIBLEsensors.size(); i++) { if(MIBLEsensors[i].showedUp < 3){ - DEBUG_SENSOR_LOG(PSTR("MINRF: sensor not fully registered yet")); + DEBUG_SENSOR_LOG(PSTR("NRF: sensor not fully registered yet")); if(MIBLEsensors[i].type != YEERC) break; // send every RC code, even if there is a potentially false MAC } switch(MIBLEsensors[i].type){ @@ -1647,7 +1691,7 @@ void MINRFShow(bool json) WSContentSend_PD(HTTP_NRF24NEW, NRF24type, NRF24.chipType, i+1,stemp,MINRF.confirmedSensors); for (i ; igetAddress().toString().c_str(),advertisedDevice->getServiceData().length()); - if (advertisedDevice->getServiceData().length() == 0) { - // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("No Xiaomi Device: %s Buffer: %u"),advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData().length()); + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("Advertised Device: %s Buffer: %u"),advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData(0).length()); + if (advertisedDevice->getServiceDataCount() == 0) { + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("No Xiaomi Device: %s Buffer: %u"),advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData(0).length()); MI32Scan->erase(advertisedDevice->getAddress()); return; } - uint16_t uuid = advertisedDevice->getServiceDataUUID().getNative()->u16.value; + uint16_t uuid = advertisedDevice->getServiceDataUUID(0).getNative()->u16.value; // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("UUID: %x"),uuid); uint8_t addr[6]; memcpy(addr,advertisedDevice->getAddress().getNative(),6); @@ -350,14 +366,17 @@ class MI32AdvCallbacks: public NimBLEAdvertisedDeviceCallbacks { } // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("RSSI: %d"),rssi); // actually i never got a 0xffff if(uuid==0xfe95) { - MI32ParseResponse((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, rssi); + MI32ParseResponse((char*)advertisedDevice->getServiceData(0).data(),advertisedDevice->getServiceData(0).length(), addr, rssi); } else if(uuid==0xfdcd) { - MI32parseCGD1Packet((char*)advertisedDevice->getServiceData().data(),advertisedDevice->getServiceData().length(), addr, rssi); + MI32parseCGD1Packet((char*)advertisedDevice->getServiceData(0).data(),advertisedDevice->getServiceData(0).length(), addr, rssi); + } + else if(uuid==0x181a) { //ATC + MI32ParseATCPacket((char*)advertisedDevice->getServiceData(0).data(),advertisedDevice->getServiceData(0).length(), addr, rssi); } else { + // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("No Xiaomi Device: %x: %s Buffer: %u"), uuid, advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData(0).length()); MI32Scan->erase(advertisedDevice->getAddress()); - // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("No Xiaomi Device: %s Buffer: %u"),advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData().length()); } }; }; @@ -404,6 +423,7 @@ void MI32_ReverseMAC(uint8_t _mac[]){ void MI32AddKey(char* payload){ mi_bindKey_t keyMAC; memset(keyMAC.buf,0,sizeof(keyMAC)); + UpperCase(payload,payload); MI32KeyMACStringToBytes(payload,keyMAC.buf); bool unknownKey = true; for(uint32_t i=0; igetResults().getCount()); + // DEBUG_SENSOR_LOG(PSTR("%s: Scan Cache Length: %u"),D_CMND_MI32, MI32Scan->getResults().getCount()); MI32Scan->setInterval(70); MI32Scan->setWindow(50); MI32Scan->setAdvertisedDeviceCallbacks(&MI32ScanCallbacks,true); MI32Scan->setActiveScan(false); - MI32Scan->start(0, MI32scanEndedCB, true); // never stop scanning, will pause automaically while connecting + MI32Scan->start(0, MI32scanEndedCB, true); // never stop scanning, will pause automatically while connecting uint32_t timer = 0; for(;;){ @@ -841,7 +861,7 @@ bool MI32connectLYWSD03forNotification(){ } if (pChr){ if(pChr->canNotify()) { - if(pChr->subscribe(true,false,MI32notifyCB)) { + if(pChr->subscribe(true,MI32notifyCB,false)) { return true; } } @@ -1251,6 +1271,25 @@ if (MIBLEsensors[_slot].type==NLIGHT){ MI32.mode.shallTriggerTele = 1; } +void MI32ParseATCPacket(char * _buf, uint32_t length, uint8_t addr[6], int rssi){ + ATCPacket_t *_packet = (ATCPacket_t*)_buf; + uint32_t _slot = MIBLEgetSensorSlot(_packet->MAC, 0x0a1c, _packet->frameCnt); // This must be a hard-coded fake ID + AddLog_P2(LOG_LEVEL_DEBUG,PSTR("%s at slot %u"), kMI32DeviceType[MIBLEsensors[_slot].type-1],_slot); + if(_slot==0xff) return; + + MIBLEsensors[_slot].rssi=rssi; + + MIBLEsensors.at(_slot).temp = (float)(__builtin_bswap16(_packet->temp))/10.0f; + MIBLEsensors.at(_slot).hum = (float)_packet->hum; + MIBLEsensors[_slot].eventType.tempHum = 1; + MIBLEsensors.at(_slot).bat = _packet->batPer; + MIBLEsensors[_slot].eventType.bat = 1; + + MIBLEsensors[_slot].shallSendMQTT = 1; + MI32.mode.shallTriggerTele = 1; + +} + void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int rssi){ // no MiBeacon uint8_t _addr[6]; memcpy(_addr,addr,6); @@ -1568,7 +1607,7 @@ bool MI32Cmd(void) { \*********************************************************************************************/ const char HTTP_MI32[] PROGMEM = "{s}MI ESP32 {m}%u%s / %u{e}"; -const char HTTP_MI32_SERIAL[] PROGMEM = "{s}%s %s{m}%02x:%02x:%02x:%02x:%02x:%02x%{e}"; +const char HTTP_MI32_MAC[] PROGMEM = "{s}%s %s{m}%s{e}"; const char HTTP_RSSI[] PROGMEM = "{s}%s " D_RSSI "{m}%d dBm{e}"; const char HTTP_BATTERY[] PROGMEM = "{s}%s" " Battery" "{m}%u %%{e}"; const char HTTP_LASTBUTTON[] PROGMEM = "{s}%s Last Button{m}%u {e}"; @@ -1580,6 +1619,15 @@ const char HTTP_MI32_HL[] PROGMEM = "{s}
{m}
{e}"; void MI32Show(bool json) { if (json) { +#ifdef USE_HOME_ASSISTANT + bool _noSummarySave = MI32.option.noSummary; + bool _minimalSummarySave = MI32.option.minimalSummary; + if(hass_mode==2){ + MI32.option.noSummary = false; + MI32.option.minimalSummary = false; + } +#endif //USE_HOME_ASSISTANT + if(!MI32.mode.triggeredTele){ MI32.mode.shallClearResults=1; if(MI32.option.noSummary) return; // no message at TELEPERIOD @@ -1599,7 +1647,11 @@ void MI32Show(bool json) bool tempHumSended = false; if(MIBLEsensors[i].feature.tempHum){ if(MIBLEsensors[i].eventType.tempHum || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ - if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)) { + if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp) +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { ResponseAppend_P(PSTR(",")); ResponseAppendTHD(MIBLEsensors[i].temp, MIBLEsensors[i].hum); tempHumSended = true; @@ -1608,7 +1660,11 @@ void MI32Show(bool json) } if(MIBLEsensors[i].feature.temp && !tempHumSended){ if(MIBLEsensors[i].eventType.temp || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate) { - if (!isnan(MIBLEsensors[i].temp)) { + if (!isnan(MIBLEsensors[i].temp) +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { char temperature[FLOATSZ]; dtostrfd(MIBLEsensors[i].temp, Settings.flag2.temperature_resolution, temperature); ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s"), temperature); @@ -1617,7 +1673,11 @@ void MI32Show(bool json) } if(MIBLEsensors[i].feature.hum && !tempHumSended){ if(MIBLEsensors[i].eventType.hum || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate) { - if (!isnan(MIBLEsensors[i].hum)) { + if (!isnan(MIBLEsensors[i].hum) +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { char hum[FLOATSZ]; dtostrfd(MIBLEsensors[i].hum, Settings.flag2.humidity_resolution, hum); ResponseAppend_P(PSTR(",\"" D_JSON_HUMIDITY "\":%s"), hum); @@ -1626,27 +1686,43 @@ void MI32Show(bool json) } if (MIBLEsensors[i].feature.lux){ if(MIBLEsensors[i].eventType.lux || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ - if (MIBLEsensors[i].lux!=0x0ffffff) { // this is the error code -> no lux + if (MIBLEsensors[i].lux!=0x0ffffff +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { // this is the error code -> no lux ResponseAppend_P(PSTR(",\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux); } } } if (MIBLEsensors[i].feature.moist){ if(MIBLEsensors[i].eventType.moist || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ - if (MIBLEsensors[i].moisture!=0xff) { + if (MIBLEsensors[i].moisture!=0xff +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { ResponseAppend_P(PSTR(",\"" D_JSON_MOISTURE "\":%u"), MIBLEsensors[i].moisture); } } } if (MIBLEsensors[i].feature.fert){ if(MIBLEsensors[i].eventType.fert || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ - if (MIBLEsensors[i].fertility!=0xffff) { + if (MIBLEsensors[i].fertility!=0xffff +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { ResponseAppend_P(PSTR(",\"Fertility\":%u"), MIBLEsensors[i].fertility); } } } if (MIBLEsensors[i].feature.Btn){ - if(MIBLEsensors[i].eventType.Btn){ + if(MIBLEsensors[i].eventType.Btn +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ){ ResponseAppend_P(PSTR(",\"Btn\":%u"),MIBLEsensors[i].Btn); } } @@ -1674,7 +1750,11 @@ void MI32Show(bool json) } if (MIBLEsensors[i].feature.bat){ if(MIBLEsensors[i].eventType.bat || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ - if (MIBLEsensors[i].bat != 0x00) { // this is the error code -> no battery + if (MIBLEsensors[i].bat != 0x00 +#ifdef USE_HOME_ASSISTANT + ||(hass_mode==2) +#endif //USE_HOME_ASSISTANT + ) { // this is the error code -> no battery ResponseAppend_P(PSTR(",\"Battery\":%u"), MIBLEsensors[i].bat); } } @@ -1688,11 +1768,16 @@ void MI32Show(bool json) MIBLEsensors[i].eventType.raw = 0; if(MIBLEsensors[i].shallSendMQTT==1){ MIBLEsensors[i].shallSendMQTT = 0; - break; + continue; } } MI32.mode.triggeredTele = 0; - // ResponseAppend_P(PSTR("}")); +#ifdef USE_HOME_ASSISTANT + if(hass_mode==2){ + MI32.option.noSummary = _noSummarySave; + MI32.option.minimalSummary = _minimalSummarySave; + } +#endif //USE_HOME_ASSISTANT #ifdef USE_WEBSERVER } else { static uint16_t _page = 0; @@ -1711,7 +1796,9 @@ void MI32Show(bool json) WSContentSend_PD(HTTP_MI32, i+1,stemp,MIBLEsensors.size()); for (i; iMAC,MIBLEsensors.at(_slot).MAC,6)!=0) return; // data corruption + MIBLEsensors.at(_slot).temp = (float)(__builtin_bswap16(_packet->temp))/10.0f; + MIBLEsensors.at(_slot).hum = (float)_packet->hum; + MIBLEsensors.at(_slot).bat = _packet->batPer; + MIBLEsensors[_slot].shallSendMQTT = 1; +} + +char* HM10ParseResponse(char *buf, uint16_t bufsize) { if (!strncmp(buf,"HMSoft",6)) { //8 const char* _fw = "000"; memcpy((void *)_fw,(void *)(buf+8),3); HM10.firmware = atoi(_fw); DEBUG_SENSOR_LOG(PSTR("%s: Firmware: %d"),D_CMND_HM10, HM10.firmware); - return; + return buf; } - char * _pos = strstr(buf, "ISA:"); + char * _pos = nullptr; + uint32_t _idx = 0; + char _subStr[] = "SA:"; + while(_pos = (char*) memchr(buf+_idx, 'I', 60)){ //strstr() does miss too much + _idx=_pos-buf; + if(memcmp(&_pos+1,_subStr,3)){ + break; + } + } if(_pos) { uint8_t _newMacArray[6] = {0}; memcpy((void *)_newMacArray,(void *)(_pos+4),6); uint32_t _rssi = 255- (uint8_t)(_pos[11]); - // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("rssi: %u"),(255- (uint8_t)(_pos[11]))); HM10_ReverseMAC(_newMacArray); DEBUG_SENSOR_LOG(PSTR("%s: MAC-array: %02x%02x%02x%02x%02x%02x"),D_CMND_HM10,_newMacArray[0],_newMacArray[1],_newMacArray[2],_newMacArray[3],_newMacArray[4],_newMacArray[5]); uint16_t _type=0xffff; - for (uint32_t idx =10;idx64) return _pos+12; + else return nullptr; } else if (strstr(buf, "LOST")){ + HM10.current_task_delay = 0; HM10.mode.connected = false; } + else if (strstr(buf, "CONNF")){ + HM10.mode.connected = false; + HM10.current_task_delay = 0; + } else if (strstr(buf, "CONN")){ HM10.current_task_delay = 0; } else { DEBUG_SENSOR_LOG(PSTR("%s: empty response"),D_CMND_HM10); + return buf; } + return _pos; } void HM10readHT_LY(char *_buf){ - // AddLog_P2(LOG_LEVEL_DEBUG,PSTR("%s: raw data: %x%x%x%x%x%x%x"),D_CMND_HM10,_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6]); + // AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)_buf,7); if(_buf[0]==0x4f && _buf[1]==0x4b) return; // "OK" if(_buf[0] != 0 && _buf[1] != 0){ - memcpy(&LYWSD0x_HT,(void *)_buf,sizeof(LYWSD0x_HT)); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: T * 100: %u, H: %u"),D_CMND_HM10,LYWSD0x_HT.temp,LYWSD0x_HT.hum); + LYWSD0x_HT_t *packet = (LYWSD0x_HT_t*)_buf; + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: T * 100: %u, H: %u"),D_CMND_HM10,packet->temp,packet->hum); uint32_t _slot = HM10.state.sensor; DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot); static float _tempFloat; - _tempFloat=(float)(LYWSD0x_HT.temp)/100.0f; + _tempFloat=(float)(packet->temp)/100.0f; if(_tempFloat<60){ MIBLEsensors[_slot].temp=_tempFloat; HM10.mode.awaiting = none; HM10.current_task_delay = 0; MIBLEsensors[_slot].showedUp=255; // this sensor is real } - _tempFloat=(float)LYWSD0x_HT.hum; + _tempFloat=(float)packet->hum; if(_tempFloat<100){ MIBLEsensors[_slot].hum = _tempFloat; DEBUG_SENSOR_LOG(PSTR("LYWSD0x: hum updated")); } MIBLEsensors[_slot].eventType.tempHum = 1; if (MIBLEsensors[_slot].type == LYWSD03MMC || MIBLEsensors[_slot].type == MHOC401){ - MIBLEsensors[_slot].bat = ((float)LYWSD0x_HT.volt-2100.0f)/12.0f; + MIBLEsensors[_slot].bat = ((float)packet->volt-2100.0f)/12.0f; MIBLEsensors[_slot].eventType.bat = 1; } MIBLEsensors[_slot].shallSendMQTT = 1; @@ -730,20 +777,20 @@ void HM10readHT_CGD1(char *_buf){ if(_buf[0]==0x4f && _buf[1]==0x4b) return; // "OK" if(_buf[0] == 0){ if(_buf[1]==0 && _buf[2]==0 && _buf[3]==0 && _buf[4]==0) return; - memcpy(&CGD1_HT,(void *)_buf,5); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: T * 100: %u, H * 100: %u"),D_CMND_HM10,CGD1_HT.temp,CGD1_HT.hum); + CGD1_HT_t *_packet = (CGD1_HT_t*)_buf; + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: T * 100: %u, H * 100: %u"),D_CMND_HM10,_packet->temp,_packet->hum); uint32_t _slot = HM10.state.sensor; DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot); static float _tempFloat; - _tempFloat=(float)(CGD1_HT.temp)/100.0f; + _tempFloat=(float)(_packet->temp)/100.0f; if(_tempFloat<60){ MIBLEsensors[_slot].temp=_tempFloat; HM10.mode.awaiting = none; HM10.current_task_delay = 0; MIBLEsensors[_slot].showedUp=255; // this sensor is real } - _tempFloat=(float)CGD1_HT.hum/100.0f; + _tempFloat=(float)_packet->hum/100.0f; if(_tempFloat<100){ MIBLEsensors[_slot].hum = _tempFloat; DEBUG_SENSOR_LOG(PSTR("CGD1: hum updated")); @@ -784,23 +831,21 @@ void HM10readHT_MJ_HT_V1(char *_buf){ } void HM10readTLMF(char *_buf){ - DEBUG_SENSOR_LOG(PSTR("%s: raw data: %x%x%x%x%x%x%x"),D_CMND_HM10,_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6]); - if(_buf[0]==0x4f && _buf[1]==0x4b) return; // "OK" - if(_buf[0] != 0 || _buf[1] != 0){ // this will lose 0.0 degree, but it is not possible to measure a successful reading - memcpy(&Flora_TLMF,(void *)_buf,10); - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: T * 10: %u, L: %u, M: %u, F: %u"),D_CMND_HM10,Flora_TLMF.temp,Flora_TLMF.lux,Flora_TLMF.moist,Flora_TLMF.fert); + AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)_buf,16); + Flora_TLMF_t *_packet = (Flora_TLMF_t*)_buf; + if(_packet->ID==0xFB003C02){ // this is a magic word ... hopefully independent of FW version + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: T * 10: %u, L: %u, M: %u, F: %u"),D_CMND_HM10,_packet->temp,_packet->lux,_packet->moist,_packet->fert); uint32_t _slot = HM10.state.sensor; - DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot); + MIBLEsensors[_slot].showedUp=255; // this sensor is real + static float _tempFloat; - _tempFloat=(float)(Flora_TLMF.temp)/10.0f; - if(_tempFloat<60){ - MIBLEsensors[_slot].temp=_tempFloat; - MIBLEsensors[_slot].showedUp=255; // this sensor is real - } - MIBLEsensors[_slot].lux = Flora_TLMF.lux; - MIBLEsensors[_slot].moisture = Flora_TLMF.moist; - MIBLEsensors[_slot].fertility = Flora_TLMF.fert; + _tempFloat=(float)(_packet->temp)/10.0f; + MIBLEsensors[_slot].temp=_tempFloat; + + MIBLEsensors[_slot].lux = _packet->lux; + MIBLEsensors[_slot].moisture = _packet->moist; + MIBLEsensors[_slot].fertility = _packet->fert; MIBLEsensors[_slot].eventType.temp = 1; MIBLEsensors[_slot].eventType.lux = 1; MIBLEsensors[_slot].eventType.moist = 1; @@ -842,12 +887,10 @@ bool HM10readBat(char *_buf){ bool HM10SerialHandleFeedback(){ // every 50 milliseconds bool success = false; uint32_t i = 0; - static char ret[HM10_MAX_RX_BUF] = {0}; while(HM10Serial->available()) { - // delay(0); if(iread(); + HM10.rxBuffer[i] = HM10Serial->read(); } i++; success = true; @@ -864,45 +907,48 @@ bool HM10SerialHandleFeedback(){ // every 50 milliseconds HM10triggerTele(); } return success; - } + } switch (HM10.mode.awaiting){ case bat: if (HM10.mode.connected) { - if (HM10readBat(ret)){ + if (HM10readBat(HM10.rxBuffer)){ HM10.mode.awaiting = none; HM10.current_task_delay = 0; } } break; case tempHumLY: - if (HM10.mode.connected) HM10readHT_LY(ret); + if (HM10.mode.connected) HM10readHT_LY(HM10.rxBuffer); break; case tempHumCGD1: - if (HM10.mode.connected) HM10readHT_CGD1(ret); + if (HM10.mode.connected) HM10readHT_CGD1(HM10.rxBuffer); break; case TLMF: - if (HM10.mode.connected) HM10readTLMF(ret); + if (HM10.mode.connected) HM10readTLMF(HM10.rxBuffer); break; case discScan: if(success) { - HM10ParseResponse(ret,i); + char *_src = HM10ParseResponse(HM10.rxBuffer,i); + if(_src){ + HM10ParseResponse(_src,i-(_src-HM10.rxBuffer)); // try a second parse + } } break; case tempHumMJ: - if (HM10.mode.connected) HM10readHT_MJ_HT_V1(ret); + if (HM10.mode.connected) HM10readHT_MJ_HT_V1(HM10.rxBuffer); break; case none: if(success) { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: response: %s"),D_CMND_HM10, (char *)ret); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s: response: %s"),D_CMND_HM10, (char *)HM10.rxBuffer); // for(uint32_t j = 0; j. +*/ + +#ifdef USE_I2C +#ifdef USE_VL53L1X +/*********************************************************************************************\ + * VL53L1X + * + * Source: + * + * I2C Address: 0x29 +\*********************************************************************************************/ + +#define XSNS_77 77 +#define XI2C_54 54 // See I2CDEVICES.md + +#include "VL53L1X.h" +VL53L1X vl53l1x = VL53L1X(); // create object copy + +#define VL53L1X_ADDRESS 0x29 + +struct { + bool ready = false; + uint16_t distance = 0; +} vl53l1x_sensors; + +/********************************************************************************************/ + +void Vl53l1Detect(void) { + if (!I2cSetDevice(VL53L1X_ADDRESS)) { return; } + if (!vl53l1x.init()) { return; } + + I2cSetActiveFound(vl53l1x.getAddress(), "VL53L1X"); + vl53l1x.setTimeout(500); + vl53l1x.setDistanceMode(VL53L1X::Long); // could be Short, Medium, Long + vl53l1x.setMeasurementTimingBudget(140000); + vl53l1x.startContinuous(50); + vl53l1x_sensors.ready = true; +} + +#ifdef USE_WEBSERVER +const char HTTP_SNS_VL53L1X[] PROGMEM = + "{s}VL53L1X " D_DISTANCE "{m}%d" D_UNIT_MILLIMETER "{e}"; // {s} = , {m} = , {e} = +#endif // USE_WEBSERVER + +void Vl53l1Every_250MSecond(void) { + // every 250 ms + uint16_t dist = vl53l1x.read(); + if (!dist || dist > 4000) { + dist = 9999; + } + vl53l1x_sensors.distance = dist; +} + +#ifdef USE_DOMOTICZ +void Vl53l1Every_Second(void) { + char distance[FLOATSZ]; + dtostrfd((float)vl53l1x_sensors.distance / 10, 1, distance); + DomoticzSensor(DZ_ILLUMINANCE, distance); +} +#endif // USE_DOMOTICZ + +void Vl53l1Show(bool json) { + if (json) { +#ifdef USE_DOMOTICZ + if (0 == tele_period) { + Vl53l1Every_Second(); + } +#endif // USE_DOMOTICZ + ResponseAppend_P(PSTR(",\"VL53L1X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l1x_sensors.distance); +#ifdef USE_WEBSERVER + } else { + WSContentSend_PD(HTTP_SNS_VL53L1X, vl53l1x_sensors.distance); +#endif + } +} + +/*********************************************************************************************\ + * Interface +\*********************************************************************************************/ + +bool Xsns77(uint8_t function) +{ + if (!I2cEnabled(XI2C_54)) { return false; } + bool result = false; + + if (FUNC_INIT == function) { + Vl53l1Detect(); + } + else if (vl53l1x_sensors.ready) { + switch (function) { + case FUNC_EVERY_250_MSECOND: + Vl53l1Every_250MSecond(); + break; +#ifdef USE_DOMOTICZ + case FUNC_EVERY_SECOND: + Vl53l1Every_Second(); + break; +#endif // USE_DOMOTICZ + case FUNC_JSON_APPEND: + Vl53l1Show(1); + break; +#ifdef USE_WEBSERVER + case FUNC_WEB_SENSOR: + Vl53l1Show(0); + break; +#endif // USE_WEBSERVER + } + } + return result; +} + +#endif // USE_VL53L1X +#endif // USE_I2C diff --git a/tools/decode-status.py b/tools/decode-status.py index 26f7fe900..f1c2c8302 100755 --- a/tools/decode-status.py +++ b/tools/decode-status.py @@ -163,7 +163,9 @@ a_setoption = [[ "Select virtual White as (0) Warm or (1) Cold", "Enable Teleinfo telemetry into Tasmota Energy MQTT (0) or Teleinfo only (1)", "Force gen1 Alexa mode", - "","","","" + "Disable Zigbee auto-config when pairing new devices", + "Use frequency output for buzzer pin instead of on/off signal", + "","" ],[ "","","","", "","","","", @@ -227,7 +229,7 @@ a_features = [[ "USE_VEML7700","USE_MCP9808","USE_BL0940","USE_TELEGRAM", "USE_HP303B","USE_TCP_BRIDGE","USE_TELEINFO","USE_LMT01", "USE_PROMETHEUS","USE_IEM3000","USE_DYP","USE_I2S_AUDIO", - "","","","", + "USE_MLX90640","USE_VL53L1X","","", "","USE_TTGO_WATCH","USE_ETHERNET","USE_WEBCAM" ],[ "","","","", @@ -265,7 +267,7 @@ else: obj = json.load(fp) def StartDecode(): - print ("\n*** decode-status.py v20200906 by Theo Arends and Jacek Ziolkowski ***") + print ("\n*** decode-status.py v20200915 by Theo Arends and Jacek Ziolkowski ***") # print("Decoding\n{}".format(obj)) diff --git a/tools/fw_zbbridge/ncp-uart-sw-6.8.0.1_115200.ota b/tools/fw_zbbridge/ncp-uart-sw-6.8.0.1_115200.ota new file mode 100644 index 000000000..cac52942e Binary files /dev/null and b/tools/fw_zbbridge/ncp-uart-sw-6.8.0.1_115200.ota differ diff --git a/tools/fw_zbbridge/readme.txt b/tools/fw_zbbridge/readme.txt index 38049cf62..da6716cdd 100644 --- a/tools/fw_zbbridge/readme.txt +++ b/tools/fw_zbbridge/readme.txt @@ -1,2 +1,15 @@ -The ncp-uart-sw_6.7.6_115200.ota is for EZSP v8 compatible hosts -The ncp-uart-sw_6.5.5_115200.ota its for older like current ZHA +# EmberZNet NCP UART EZSP firmware + +## EmberZNet NCP UART EZSP firmware signed for Sonoff ZBBridge + +- `ncp-uart-sw_6.7.6_115200.ota` - recommended stable version for EZSP v6, EZSP v7, and EZSP v8 compatible hosts. +- `ncp-uart-sw-6.8.0.1_115200.ota` - latest cutting-edge version. largely untested and only for experimentation with EZSP v8 compatible hosts. +- `ncp-uart-sw_6.5.5_115200.ota` - legacy version for EZSP v4, EZSP v5, EZSP v6, or EZSP v7 compatible hosts. + +## EmberZNet and EZSP Protocol Versions + +Silicon Labs do not currently have a consolidated list of changes by EmberZNet SDK or EZSP protocol version. The EZSP additions, changes and deletions have only ever been listed in the [Zigbee EmberZNet Release Notes](https://www.silabs.com/search#q=Zigbee%20EmberZNet%20Release%20Notes&t=All&sort=relevancy) (EmberZNet SDK) under the "New items" section as well as the matching UG100 EZSP Reference Guide included with each EmberZNet SDK release. + +The largest change was between EZSP v4 (first added in EmberZNet 4.7.2 SDK) and EZSP v5 that was added in EmberZNet 5.9.0 SDK which requires the Legacy Frame ID 0xFF. The change from EZSP v5 to EZSP v6 was done in EmberZNet 6.0.0 SDK. The change from EZSP v6 to EZSP v7 was in EmberZNet 6.4.0 SDK. EmberZNet 6.7.0 SDK added EZSP v8 (and Secure EZSP Protocol Version 2). + +Perhaps more important to know is that EZSP v5, v6 and v7 (EmberZNet 6.6.x.x) use the same framing format, but EmberZNet 6.7.x.x/EZSP v8 introduced new framing format and expanded command id field from 8 bits to 16 bits. diff --git a/uncrustify.cfg b/uncrustify.cfg new file mode 100644 index 000000000..fbd5cab07 --- /dev/null +++ b/uncrustify.cfg @@ -0,0 +1,2807 @@ +# Uncrustify-0.69.0-1-72de5fdb + +# +# General options +# + +# The type of line endings. +# +# Default: auto +newlines = auto # lf/crlf/cr/auto + +# The original size of tabs in the input. +# +# Default: 8 +input_tab_size = 8 # unsigned number + +# The size of tabs in the output (only used if align_with_tabs=true). +# +# Default: 8 +output_tab_size = 8 # unsigned number + +# The ASCII value of the string escape char, usually 92 (\) or (Pawn) 94 (^). +# +# Default: 92 +string_escape_char = 92 # unsigned number + +# Alternate string escape char (usually only used for Pawn). +# Only works right before the quote char. +string_escape_char2 = 0 # unsigned number + +# Replace tab characters found in string literals with the escape sequence \t +# instead. +string_replace_tab_chars = false # true/false + +# Allow interpreting '>=' and '>>=' as part of a template in code like +# 'void f(list>=val);'. If true, 'assert(x<0 && y>=3)' will be broken. +# Improvements to template detection may make this option obsolete. +tok_split_gte = false # true/false + +# Specify the marker used in comments to disable processing of part of the +# file. +# +# Default: *INDENT-OFF* +disable_processing_cmt = " *INDENT-OFF*" # string + +# Specify the marker used in comments to (re)enable processing in a file. +# +# Default: *INDENT-ON* +enable_processing_cmt = " *INDENT-ON*" # string + +# Enable parsing of digraphs. +enable_digraphs = false # true/false + +# Add or remove the UTF-8 BOM (recommend 'remove'). +utf8_bom = ignore # ignore/add/remove/force + +# If the file contains bytes with values between 128 and 255, but is not +# UTF-8, then output as UTF-8. +utf8_byte = false # true/false + +# Force the output encoding to UTF-8. +utf8_force = false # true/false + +# +# Spacing options +# + +# Add or remove space around non-assignment symbolic operators ('+', '/', '%', +# '<<', and so forth). +sp_arith = add # ignore/add/remove/force + +# Add or remove space around arithmetic operators '+' and '-'. +# +# Overrides sp_arith. +sp_arith_additive = ignore # ignore/add/remove/force + +# Add or remove space around assignment operator '=', '+=', etc. +sp_assign = add # ignore/add/remove/force + +# Add or remove space around '=' in C++11 lambda capture specifications. +# +# Overrides sp_assign. +sp_cpp_lambda_assign = ignore # ignore/add/remove/force + +# Add or remove space after the capture specification in C++11 lambda. +sp_cpp_lambda_paren = ignore # ignore/add/remove/force + +# Add or remove space around assignment operator '=' in a prototype. +# +# If set to ignore, use sp_assign. +sp_assign_default = ignore # ignore/add/remove/force + +# Add or remove space before assignment operator '=', '+=', etc. +# +# Overrides sp_assign. +sp_before_assign = ignore # ignore/add/remove/force + +# Add or remove space after assignment operator '=', '+=', etc. +# +# Overrides sp_assign. +sp_after_assign = ignore # ignore/add/remove/force + +# Add or remove space in 'NS_ENUM ('. +sp_enum_paren = ignore # ignore/add/remove/force + +# Add or remove space around assignment '=' in enum. +sp_enum_assign = add # ignore/add/remove/force + +# Add or remove space before assignment '=' in enum. +# +# Overrides sp_enum_assign. +sp_enum_before_assign = ignore # ignore/add/remove/force + +# Add or remove space after assignment '=' in enum. +# +# Overrides sp_enum_assign. +sp_enum_after_assign = ignore # ignore/add/remove/force + +# Add or remove space around assignment ':' in enum. +sp_enum_colon = ignore # ignore/add/remove/force + +# Add or remove space around preprocessor '##' concatenation operator. +# +# Default: add +sp_pp_concat = add # ignore/add/remove/force + +# Add or remove space after preprocessor '#' stringify operator. +# Also affects the '#@' charizing operator. +sp_pp_stringify = add # ignore/add/remove/force + +# Add or remove space before preprocessor '#' stringify operator +# as in '#define x(y) L#y'. +sp_before_pp_stringify = ignore # ignore/add/remove/force + +# Add or remove space around boolean operators '&&' and '||'. +sp_bool = add # ignore/add/remove/force + +# Add or remove space around compare operator '<', '>', '==', etc. +sp_compare = add # ignore/add/remove/force + +# Add or remove space inside '(' and ')'. +sp_inside_paren = remove # ignore/add/remove/force + +# Add or remove space between nested parentheses, i.e. '((' vs. ') )'. +sp_paren_paren = remove # ignore/add/remove/force + +# Add or remove space between back-to-back parentheses, i.e. ')(' vs. ') ('. +sp_cparen_oparen = ignore # ignore/add/remove/force + +# Whether to balance spaces inside nested parentheses. +sp_balance_nested_parens = false # true/false + +# Add or remove space between ')' and '{'. +sp_paren_brace = add # ignore/add/remove/force + +# Add or remove space between nested braces, i.e. '{{' vs '{ {'. +sp_brace_brace = ignore # ignore/add/remove/force + +# Add or remove space before pointer star '*'. +sp_before_ptr_star = add # ignore/add/remove/force + +# Add or remove space before pointer star '*' that isn't followed by a +# variable name. If set to 'ignore', sp_before_ptr_star is used instead. +sp_before_unnamed_ptr_star = add # ignore/add/remove/force + +# Add or remove space between pointer stars '*'. +sp_between_ptr_star = remove # ignore/add/remove/force + +# Add or remove space after pointer star '*', if followed by a word. +sp_after_ptr_star = remove # ignore/add/remove/force + +# Add or remove space after pointer caret '^', if followed by a word. +sp_after_ptr_block_caret = ignore # ignore/add/remove/force + +# Add or remove space after pointer star '*', if followed by a qualifier. +sp_after_ptr_star_qualifier = ignore # ignore/add/remove/force + +# Add or remove space after a pointer star '*', if followed by a function +# prototype or function definition. +sp_after_ptr_star_func = add # ignore/add/remove/force + +# Add or remove space after a pointer star '*', if followed by an open +# parenthesis, as in 'void* (*)(). +sp_ptr_star_paren = ignore # ignore/add/remove/force + +# Add or remove space before a pointer star '*', if followed by a function +# prototype or function definition. +sp_before_ptr_star_func = remove # ignore/add/remove/force + +# Add or remove space before a reference sign '&'. +sp_before_byref = remove # ignore/add/remove/force + +# Add or remove space before a reference sign '&' that isn't followed by a +# variable name. If set to 'ignore', sp_before_byref is used instead. +sp_before_unnamed_byref = remove # ignore/add/remove/force + +# Add or remove space after reference sign '&', if followed by a word. +sp_after_byref = add # ignore/add/remove/force + +# Add or remove space after a reference sign '&', if followed by a function +# prototype or function definition. +sp_after_byref_func = add # ignore/add/remove/force + +# Add or remove space before a reference sign '&', if followed by a function +# prototype or function definition. +sp_before_byref_func = remove # ignore/add/remove/force + +# Add or remove space between type and word. +# +# Default: force +sp_after_type = add # ignore/add/remove/force + +# Add or remove space between 'decltype(...)' and word. +sp_after_decltype = ignore # ignore/add/remove/force + +# (D) Add or remove space before the parenthesis in the D constructs +# 'template Foo(' and 'class Foo('. +sp_before_template_paren = ignore # ignore/add/remove/force + +# Add or remove space between 'template' and '<'. +# If set to ignore, sp_before_angle is used. +sp_template_angle = ignore # ignore/add/remove/force + +# Add or remove space before '<'. +sp_before_angle = remove # ignore/add/remove/force + +# Add or remove space inside '<' and '>'. +sp_inside_angle = remove # ignore/add/remove/force + +# Add or remove space inside '<>'. +sp_inside_angle_empty = ignore # ignore/add/remove/force + +# Add or remove space between '>' and ':'. +sp_angle_colon = ignore # ignore/add/remove/force + +# Add or remove space after '<>'. +sp_after_angle = remove # ignore/add/remove/force + +# Add or remove space between '>' and '(' as found in 'new List(foo);'. +sp_angle_paren = remove # ignore/add/remove/force + +# Add or remove space between '>' and '()' as found in 'new List();'. +sp_angle_paren_empty = ignore # ignore/add/remove/force + +# Add or remove space between '>' and a word as in 'List m;' or +# 'template static ...'. +sp_angle_word = remove # ignore/add/remove/force + +# Add or remove space between '>' and '>' in '>>' (template stuff). +# +# Default: add +sp_angle_shift = add # ignore/add/remove/force + +# (C++11) Permit removal of the space between '>>' in 'foo >'. Note +# that sp_angle_shift cannot remove the space without this option. +sp_permit_cpp11_shift = false # true/false + +# Add or remove space before '(' of control statements ('if', 'for', 'switch', +# 'while', etc.). +sp_before_sparen = add # ignore/add/remove/force + +# Add or remove space inside '(' and ')' of control statements. +sp_inside_sparen = remove # ignore/add/remove/force + +# Add or remove space after '(' of control statements. +# +# Overrides sp_inside_sparen. +sp_inside_sparen_open = ignore # ignore/add/remove/force + +# Add or remove space before ')' of control statements. +# +# Overrides sp_inside_sparen. +sp_inside_sparen_close = ignore # ignore/add/remove/force + +# Add or remove space after ')' of control statements. +sp_after_sparen = ignore # ignore/add/remove/force + +# Add or remove space between ')' and '{' of of control statements. +sp_sparen_brace = add # ignore/add/remove/force + +# (D) Add or remove space between 'invariant' and '('. +sp_invariant_paren = ignore # ignore/add/remove/force + +# (D) Add or remove space after the ')' in 'invariant (C) c'. +sp_after_invariant_paren = ignore # ignore/add/remove/force + +# Add or remove space before empty statement ';' on 'if', 'for' and 'while'. +sp_special_semi = remove # ignore/add/remove/force + +# Add or remove space before ';'. +# +# Default: remove +sp_before_semi = remove # ignore/add/remove/force + +# Add or remove space before ';' in non-empty 'for' statements. +sp_before_semi_for = remove # ignore/add/remove/force + +# Add or remove space before a semicolon of an empty part of a for statement. +sp_before_semi_for_empty = remove # ignore/add/remove/force + +# Add or remove space after ';', except when followed by a comment. +# +# Default: add +sp_after_semi = add # ignore/add/remove/force + +# Add or remove space after ';' in non-empty 'for' statements. +# +# Default: force +sp_after_semi_for = force # ignore/add/remove/force + +# Add or remove space after the final semicolon of an empty part of a for +# statement, as in 'for ( ; ; )'. +sp_after_semi_for_empty = remove # ignore/add/remove/force + +# Add or remove space before '[' (except '[]'). +sp_before_square = remove # ignore/add/remove/force + +# Add or remove space before '[]'. +sp_before_squares = remove # ignore/add/remove/force + +# Add or remove space before C++17 structured bindings. +sp_cpp_before_struct_binding = ignore # ignore/add/remove/force + +# Add or remove space inside a non-empty '[' and ']'. +sp_inside_square = remove # ignore/add/remove/force + +# (OC) Add or remove space inside a non-empty Objective-C boxed array '@[' and +# ']'. If set to ignore, sp_inside_square is used. +sp_inside_square_oc_array = ignore # ignore/add/remove/force + +# Add or remove space after ',', i.e. 'a,b' vs. 'a, b'. +sp_after_comma = add # ignore/add/remove/force + +# Add or remove space before ','. +# +# Default: remove +sp_before_comma = remove # ignore/add/remove/force + +# (C#) Add or remove space between ',' and ']' in multidimensional array type +# like 'int[,,]'. +sp_after_mdatype_commas = ignore # ignore/add/remove/force + +# (C#) Add or remove space between '[' and ',' in multidimensional array type +# like 'int[,,]'. +sp_before_mdatype_commas = ignore # ignore/add/remove/force + +# (C#) Add or remove space between ',' in multidimensional array type +# like 'int[,,]'. +sp_between_mdatype_commas = ignore # ignore/add/remove/force + +# Add or remove space between an open parenthesis and comma, +# i.e. '(,' vs. '( ,'. +# +# Default: force +sp_paren_comma = force # ignore/add/remove/force + +# Add or remove space before the variadic '...' when preceded by a +# non-punctuator. +sp_before_ellipsis = ignore # ignore/add/remove/force + +# Add or remove space between a type and '...'. +sp_type_ellipsis = ignore # ignore/add/remove/force + +# (D) Add or remove space between a type and '?'. +sp_type_question = ignore # ignore/add/remove/force + +# Add or remove space between ')' and '...'. +sp_paren_ellipsis = ignore # ignore/add/remove/force + +# Add or remove space between ')' and a qualifier such as 'const'. +sp_paren_qualifier = ignore # ignore/add/remove/force + +# Add or remove space between ')' and 'noexcept'. +sp_paren_noexcept = ignore # ignore/add/remove/force + +# Add or remove space after class ':'. +sp_after_class_colon = add # ignore/add/remove/force + +# Add or remove space before class ':'. +sp_before_class_colon = add # ignore/add/remove/force + +# Add or remove space after class constructor ':'. +sp_after_constr_colon = ignore # ignore/add/remove/force + +# Add or remove space before class constructor ':'. +sp_before_constr_colon = ignore # ignore/add/remove/force + +# Add or remove space before case ':'. +# +# Default: remove +sp_before_case_colon = remove # ignore/add/remove/force + +# Add or remove space between 'operator' and operator sign. +sp_after_operator = remove # ignore/add/remove/force + +# Add or remove space between the operator symbol and the open parenthesis, as +# in 'operator ++('. +sp_after_operator_sym = remove # ignore/add/remove/force + +# Overrides sp_after_operator_sym when the operator has no arguments, as in +# 'operator *()'. +sp_after_operator_sym_empty = ignore # ignore/add/remove/force + +# Add or remove space after C/D cast, i.e. 'cast(int)a' vs. 'cast(int) a' or +# '(int)a' vs. '(int) a'. +sp_after_cast = remove # ignore/add/remove/force + +# Add or remove spaces inside cast parentheses. +sp_inside_paren_cast = remove # ignore/add/remove/force + +# Add or remove space between the type and open parenthesis in a C++ cast, +# i.e. 'int(exp)' vs. 'int (exp)'. +sp_cpp_cast_paren = remove # ignore/add/remove/force + +# Add or remove space between 'sizeof' and '('. +sp_sizeof_paren = remove # ignore/add/remove/force + +# Add or remove space between 'sizeof' and '...'. +sp_sizeof_ellipsis = ignore # ignore/add/remove/force + +# Add or remove space between 'sizeof...' and '('. +sp_sizeof_ellipsis_paren = ignore # ignore/add/remove/force + +# Add or remove space between 'decltype' and '('. +sp_decltype_paren = ignore # ignore/add/remove/force + +# (Pawn) Add or remove space after the tag keyword. +sp_after_tag = ignore # ignore/add/remove/force + +# Add or remove space inside enum '{' and '}'. +sp_inside_braces_enum = add # ignore/add/remove/force + +# Add or remove space inside struct/union '{' and '}'. +sp_inside_braces_struct = add # ignore/add/remove/force + +# (OC) Add or remove space inside Objective-C boxed dictionary '{' and '}' +sp_inside_braces_oc_dict = ignore # ignore/add/remove/force + +# Add or remove space after open brace in an unnamed temporary +# direct-list-initialization. +sp_after_type_brace_init_lst_open = ignore # ignore/add/remove/force + +# Add or remove space before close brace in an unnamed temporary +# direct-list-initialization. +sp_before_type_brace_init_lst_close = ignore # ignore/add/remove/force + +# Add or remove space inside an unnamed temporary direct-list-initialization. +sp_inside_type_brace_init_lst = ignore # ignore/add/remove/force + +# Add or remove space inside '{' and '}'. +sp_inside_braces = add # ignore/add/remove/force + +# Add or remove space inside '{}'. +sp_inside_braces_empty = remove # ignore/add/remove/force + +# Add or remove space between return type and function name. A minimum of 1 +# is forced except for pointer return types. +sp_type_func = add # ignore/add/remove/force + +# Add or remove space between type and open brace of an unnamed temporary +# direct-list-initialization. +sp_type_brace_init_lst = ignore # ignore/add/remove/force + +# Add or remove space between function name and '(' on function declaration. +sp_func_proto_paren = remove # ignore/add/remove/force + +# Add or remove space between function name and '()' on function declaration +# without parameters. +sp_func_proto_paren_empty = ignore # ignore/add/remove/force + +# Add or remove space between function name and '(' on function definition. +sp_func_def_paren = remove # ignore/add/remove/force + +# Add or remove space between function name and '()' on function definition +# without parameters. +sp_func_def_paren_empty = ignore # ignore/add/remove/force + +# Add or remove space inside empty function '()'. +sp_inside_fparens = remove # ignore/add/remove/force + +# Add or remove space inside function '(' and ')'. +sp_inside_fparen = remove # ignore/add/remove/force + +# Add or remove space inside the first parentheses in a function type, as in +# 'void (*x)(...)'. +sp_inside_tparen = ignore # ignore/add/remove/force + +# Add or remove space between the ')' and '(' in a function type, as in +# 'void (*x)(...)'. +sp_after_tparen_close = ignore # ignore/add/remove/force + +# Add or remove space between ']' and '(' when part of a function call. +sp_square_fparen = remove # ignore/add/remove/force + +# Add or remove space between ')' and '{' of function. +sp_fparen_brace = add # ignore/add/remove/force + +# Add or remove space between ')' and '{' of s function call in object +# initialization. +# +# Overrides sp_fparen_brace. +sp_fparen_brace_initializer = ignore # ignore/add/remove/force + +# (Java) Add or remove space between ')' and '{{' of double brace initializer. +sp_fparen_dbrace = ignore # ignore/add/remove/force + +# Add or remove space between function name and '(' on function calls. +sp_func_call_paren = remove # ignore/add/remove/force + +# Add or remove space between function name and '()' on function calls without +# parameters. If set to 'ignore' (the default), sp_func_call_paren is used. +sp_func_call_paren_empty = ignore # ignore/add/remove/force + +# Add or remove space between the user function name and '(' on function +# calls. You need to set a keyword to be a user function in the config file, +# like: +# set func_call_user tr _ i18n +sp_func_call_user_paren = remove # ignore/add/remove/force + +# Add or remove space inside user function '(' and ')'. +sp_func_call_user_inside_fparen = ignore # ignore/add/remove/force + +# Add or remove space between nested parentheses with user functions, +# i.e. '((' vs. '( ('. +sp_func_call_user_paren_paren = ignore # ignore/add/remove/force + +# Add or remove space between a constructor/destructor and the open +# parenthesis. +sp_func_class_paren = remove # ignore/add/remove/force + +# Add or remove space between a constructor without parameters or destructor +# and '()'. +sp_func_class_paren_empty = ignore # ignore/add/remove/force + +# Add or remove space between 'return' and '('. +sp_return_paren = add # ignore/add/remove/force + +# Add or remove space between 'return' and '{'. +sp_return_brace = ignore # ignore/add/remove/force + +# Add or remove space between '__attribute__' and '('. +sp_attribute_paren = remove # ignore/add/remove/force + +# Add or remove space between 'defined' and '(' in '#if defined (FOO)'. +sp_defined_paren = remove # ignore/add/remove/force + +# Add or remove space between 'throw' and '(' in 'throw (something)'. +sp_throw_paren = remove # ignore/add/remove/force + +# Add or remove space between 'throw' and anything other than '(' as in +# '@throw [...];'. +sp_after_throw = ignore # ignore/add/remove/force + +# Add or remove space between 'catch' and '(' in 'catch (something) { }'. +# If set to ignore, sp_before_sparen is used. +sp_catch_paren = ignore # ignore/add/remove/force + +# (OC) Add or remove space between '@catch' and '(' +# in '@catch (something) { }'. If set to ignore, sp_catch_paren is used. +sp_oc_catch_paren = ignore # ignore/add/remove/force + +# (OC) Add or remove space between class name and '(' +# in '@interface className(categoryName):BaseClass' +sp_oc_classname_paren = ignore # ignore/add/remove/force + +# (D) Add or remove space between 'version' and '(' +# in 'version (something) { }'. If set to ignore, sp_before_sparen is used. +sp_version_paren = ignore # ignore/add/remove/force + +# (D) Add or remove space between 'scope' and '(' +# in 'scope (something) { }'. If set to ignore, sp_before_sparen is used. +sp_scope_paren = ignore # ignore/add/remove/force + +# Add or remove space between 'super' and '(' in 'super (something)'. +# +# Default: remove +sp_super_paren = remove # ignore/add/remove/force + +# Add or remove space between 'this' and '(' in 'this (something)'. +# +# Default: remove +sp_this_paren = remove # ignore/add/remove/force + +# Add or remove space between a macro name and its definition. +sp_macro = add # ignore/add/remove/force + +# Add or remove space between a macro function ')' and its definition. +sp_macro_func = remove # ignore/add/remove/force + +# Add or remove space between 'else' and '{' if on the same line. +sp_else_brace = add # ignore/add/remove/force + +# Add or remove space between '}' and 'else' if on the same line. +sp_brace_else = add # ignore/add/remove/force + +# Add or remove space between '}' and the name of a typedef on the same line. +sp_brace_typedef = add # ignore/add/remove/force + +# Add or remove space before the '{' of a 'catch' statement, if the '{' and +# 'catch' are on the same line, as in 'catch (decl) {'. +sp_catch_brace = add # ignore/add/remove/force + +# (OC) Add or remove space before the '{' of a '@catch' statement, if the '{' +# and '@catch' are on the same line, as in '@catch (decl) {'. +# If set to ignore, sp_catch_brace is used. +sp_oc_catch_brace = ignore # ignore/add/remove/force + +# Add or remove space between '}' and 'catch' if on the same line. +sp_brace_catch = add # ignore/add/remove/force + +# (OC) Add or remove space between '}' and '@catch' if on the same line. +# If set to ignore, sp_brace_catch is used. +sp_oc_brace_catch = ignore # ignore/add/remove/force + +# Add or remove space between 'finally' and '{' if on the same line. +sp_finally_brace = add # ignore/add/remove/force + +# Add or remove space between '}' and 'finally' if on the same line. +sp_brace_finally = add # ignore/add/remove/force + +# Add or remove space between 'try' and '{' if on the same line. +sp_try_brace = add # ignore/add/remove/force + +# Add or remove space between get/set and '{' if on the same line. +sp_getset_brace = add # ignore/add/remove/force + +# Add or remove space between a variable and '{' for C++ uniform +# initialization. +# +# Default: add +sp_word_brace = add # ignore/add/remove/force + +# Add or remove space between a variable and '{' for a namespace. +# +# Default: add +sp_word_brace_ns = add # ignore/add/remove/force + +# Add or remove space before the '::' operator. +sp_before_dc = remove # ignore/add/remove/force + +# Add or remove space after the '::' operator. +sp_after_dc = remove # ignore/add/remove/force + +# (D) Add or remove around the D named array initializer ':' operator. +sp_d_array_colon = ignore # ignore/add/remove/force + +# Add or remove space after the '!' (not) unary operator. +# +# Default: remove +sp_not = remove # ignore/add/remove/force + +# Add or remove space after the '~' (invert) unary operator. +# +# Default: remove +sp_inv = remove # ignore/add/remove/force + +# Add or remove space after the '&' (address-of) unary operator. This does not +# affect the spacing after a '&' that is part of a type. +# +# Default: remove +sp_addr = remove # ignore/add/remove/force + +# Add or remove space around the '.' or '->' operators. +# +# Default: remove +sp_member = remove # ignore/add/remove/force + +# Add or remove space after the '*' (dereference) unary operator. This does +# not affect the spacing after a '*' that is part of a type. +# +# Default: remove +sp_deref = remove # ignore/add/remove/force + +# Add or remove space after '+' or '-', as in 'x = -5' or 'y = +7'. +# +# Default: remove +sp_sign = remove # ignore/add/remove/force + +# Add or remove space between '++' and '--' the word to which it is being +# applied, as in '(--x)' or 'y++;'. +# +# Default: remove +sp_incdec = remove # ignore/add/remove/force + +# Add or remove space before a backslash-newline at the end of a line. +# +# Default: add +sp_before_nl_cont = remove # ignore/add/remove/force + +# (OC) Add or remove space after the scope '+' or '-', as in '-(void) foo;' +# or '+(int) bar;'. +sp_after_oc_scope = remove # ignore/add/remove/force + +# (OC) Add or remove space after the colon in message specs, +# i.e. '-(int) f:(int) x;' vs. '-(int) f: (int) x;'. +sp_after_oc_colon = remove # ignore/add/remove/force + +# (OC) Add or remove space before the colon in message specs, +# i.e. '-(int) f: (int) x;' vs. '-(int) f : (int) x;'. +sp_before_oc_colon = remove # ignore/add/remove/force + +# (OC) Add or remove space after the colon in immutable dictionary expression +# 'NSDictionary *test = @{@"foo" :@"bar"};'. +sp_after_oc_dict_colon = ignore # ignore/add/remove/force + +# (OC) Add or remove space before the colon in immutable dictionary expression +# 'NSDictionary *test = @{@"foo" :@"bar"};'. +sp_before_oc_dict_colon = ignore # ignore/add/remove/force + +# (OC) Add or remove space after the colon in message specs, +# i.e. '[object setValue:1];' vs. '[object setValue: 1];'. +sp_after_send_oc_colon = add # ignore/add/remove/force + +# (OC) Add or remove space before the colon in message specs, +# i.e. '[object setValue:1];' vs. '[object setValue :1];'. +sp_before_send_oc_colon = remove # ignore/add/remove/force + +# (OC) Add or remove space after the (type) in message specs, +# i.e. '-(int)f: (int) x;' vs. '-(int)f: (int)x;'. +sp_after_oc_type = remove # ignore/add/remove/force + +# (OC) Add or remove space after the first (type) in message specs, +# i.e. '-(int) f:(int)x;' vs. '-(int)f:(int)x;'. +sp_after_oc_return_type = ignore # ignore/add/remove/force + +# (OC) Add or remove space between '@selector' and '(', +# i.e. '@selector(msgName)' vs. '@selector (msgName)'. +# Also applies to '@protocol()' constructs. +sp_after_oc_at_sel = ignore # ignore/add/remove/force + +# (OC) Add or remove space between '@selector(x)' and the following word, +# i.e. '@selector(foo) a:' vs. '@selector(foo)a:'. +sp_after_oc_at_sel_parens = ignore # ignore/add/remove/force + +# (OC) Add or remove space inside '@selector' parentheses, +# i.e. '@selector(foo)' vs. '@selector( foo )'. +# Also applies to '@protocol()' constructs. +sp_inside_oc_at_sel_parens = ignore # ignore/add/remove/force + +# (OC) Add or remove space before a block pointer caret, +# i.e. '^int (int arg){...}' vs. ' ^int (int arg){...}'. +sp_before_oc_block_caret = ignore # ignore/add/remove/force + +# (OC) Add or remove space after a block pointer caret, +# i.e. '^int (int arg){...}' vs. '^ int (int arg){...}'. +sp_after_oc_block_caret = ignore # ignore/add/remove/force + +# (OC) Add or remove space between the receiver and selector in a message, +# as in '[receiver selector ...]'. +sp_after_oc_msg_receiver = ignore # ignore/add/remove/force + +# (OC) Add or remove space after '@property'. +sp_after_oc_property = ignore # ignore/add/remove/force + +# (OC) Add or remove space between '@synchronized' and the open parenthesis, +# i.e. '@synchronized(foo)' vs. '@synchronized (foo)'. +sp_after_oc_synchronized = ignore # ignore/add/remove/force + +# Add or remove space around the ':' in 'b ? t : f'. +sp_cond_colon = add # ignore/add/remove/force + +# Add or remove space before the ':' in 'b ? t : f'. +# +# Overrides sp_cond_colon. +sp_cond_colon_before = ignore # ignore/add/remove/force + +# Add or remove space after the ':' in 'b ? t : f'. +# +# Overrides sp_cond_colon. +sp_cond_colon_after = ignore # ignore/add/remove/force + +# Add or remove space around the '?' in 'b ? t : f'. +sp_cond_question = add # ignore/add/remove/force + +# Add or remove space before the '?' in 'b ? t : f'. +# +# Overrides sp_cond_question. +sp_cond_question_before = ignore # ignore/add/remove/force + +# Add or remove space after the '?' in 'b ? t : f'. +# +# Overrides sp_cond_question. +sp_cond_question_after = ignore # ignore/add/remove/force + +# In the abbreviated ternary form '(a ?: b)', add or remove space between '?' +# and ':'. +# +# Overrides all other sp_cond_* options. +sp_cond_ternary_short = ignore # ignore/add/remove/force + +# Fix the spacing between 'case' and the label. Only 'ignore' and 'force' make +# sense here. +sp_case_label = ignore # ignore/add/remove/force + +# (D) Add or remove space around the D '..' operator. +sp_range = ignore # ignore/add/remove/force + +# Add or remove space after ':' in a Java/C++11 range-based 'for', +# as in 'for (Type var : expr)'. +sp_after_for_colon = ignore # ignore/add/remove/force + +# Add or remove space before ':' in a Java/C++11 range-based 'for', +# as in 'for (Type var : expr)'. +sp_before_for_colon = ignore # ignore/add/remove/force + +# (D) Add or remove space between 'extern' and '(' as in 'extern (C)'. +sp_extern_paren = ignore # ignore/add/remove/force + +# Add or remove space after the opening of a C++ comment, +# i.e. '// A' vs. '//A'. +sp_cmt_cpp_start = add # ignore/add/remove/force + +# If true, space is added with sp_cmt_cpp_start will be added after doxygen +# sequences like '///', '///<', '//!' and '//!<'. +sp_cmt_cpp_doxygen = false # true/false + +# If true, space is added with sp_cmt_cpp_start will be added after Qt +# translator or meta-data comments like '//:', '//=', and '//~'. +sp_cmt_cpp_qttr = false # true/false + +# Add or remove space between #else or #endif and a trailing comment. +sp_endif_cmt = ignore # ignore/add/remove/force + +# Add or remove space after 'new', 'delete' and 'delete[]'. +sp_after_new = ignore # ignore/add/remove/force + +# Add or remove space between 'new' and '(' in 'new()'. +sp_between_new_paren = ignore # ignore/add/remove/force + +# Add or remove space between ')' and type in 'new(foo) BAR'. +sp_after_newop_paren = ignore # ignore/add/remove/force + +# Add or remove space inside parenthesis of the new operator +# as in 'new(foo) BAR'. +sp_inside_newop_paren = ignore # ignore/add/remove/force + +# Add or remove space after the open parenthesis of the new operator, +# as in 'new(foo) BAR'. +# +# Overrides sp_inside_newop_paren. +sp_inside_newop_paren_open = ignore # ignore/add/remove/force + +# Add or remove space before the close parenthesis of the new operator, +# as in 'new(foo) BAR'. +# +# Overrides sp_inside_newop_paren. +sp_inside_newop_paren_close = ignore # ignore/add/remove/force + +# Add or remove space before a trailing or embedded comment. +sp_before_tr_emb_cmt = ignore # ignore/add/remove/force + +# Number of spaces before a trailing or embedded comment. +sp_num_before_tr_emb_cmt = 0 # unsigned number + +# (Java) Add or remove space between an annotation and the open parenthesis. +sp_annotation_paren = ignore # ignore/add/remove/force + +# If true, vbrace tokens are dropped to the previous token and skipped. +sp_skip_vbrace_tokens = false # true/false + +# Add or remove space after 'noexcept'. +sp_after_noexcept = ignore # ignore/add/remove/force + +# Add or remove space after '_'. +sp_vala_after_translation = ignore # ignore/add/remove/force + +# If true, a is inserted after #define. +force_tab_after_define = false # true/false + +# +# Indenting options +# + +# The number of columns to indent per level. Usually 2, 3, 4, or 8. +# +# Default: 8 +indent_columns = 2 # unsigned number + +# The continuation indent. If non-zero, this overrides the indent of '(', '[' +# and '=' continuation indents. Negative values are OK; negative value is +# absolute and not increased for each '(' or '[' level. +# +# For FreeBSD, this is set to 4. +indent_continue = 0 # number + +# The continuation indent, only for class header line(s). If non-zero, this +# overrides the indent of 'class' continuation indents. +indent_continue_class_head = 0 # unsigned number + +# Whether to indent empty lines (i.e. lines which contain only spaces before +# the newline character). +indent_single_newlines = false # true/false + +# The continuation indent for func_*_param if they are true. If non-zero, this +# overrides the indent. +indent_param = 0 # unsigned number + +# How to use tabs when indenting code. +# +# 0: Spaces only +# 1: Indent with tabs to brace level, align with spaces (default) +# 2: Indent and align with tabs, using spaces when not on a tabstop +# +# Default: 1 +indent_with_tabs = 0 # unsigned number + +# Whether to indent comments that are not at a brace level with tabs on a +# tabstop. Requires indent_with_tabs=2. If false, will use spaces. +indent_cmt_with_tabs = false # true/false + +# Whether to indent strings broken by '\' so that they line up. +indent_align_string = true # true/false + +# The number of spaces to indent multi-line XML strings. +# Requires indent_align_string=true. +indent_xml_string = 0 # unsigned number + +# Spaces to indent '{' from level. +indent_brace = 0 # unsigned number + +# Whether braces are indented to the body level. +indent_braces = false # true/false + +# Whether to disable indenting function braces if indent_braces=true. +indent_braces_no_func = false # true/false + +# Whether to disable indenting class braces if indent_braces=true. +indent_braces_no_class = false # true/false + +# Whether to disable indenting struct braces if indent_braces=true. +indent_braces_no_struct = false # true/false + +# Whether to indent based on the size of the brace parent, +# i.e. 'if' => 3 spaces, 'for' => 4 spaces, etc. +indent_brace_parent = false # true/false + +# Whether to indent based on the open parenthesis instead of the open brace +# in '({\n'. +indent_paren_open_brace = false # true/false + +# (C#) Whether to indent the brace of a C# delegate by another level. +indent_cs_delegate_brace = false # true/false + +# (C#) Whether to indent a C# delegate (to handle delegates with no brace) by +# another level. +indent_cs_delegate_body = false # true/false + +# Whether to indent the body of a 'namespace'. +indent_namespace = false # true/false + +# Whether to indent only the first namespace, and not any nested namespaces. +# Requires indent_namespace=true. +indent_namespace_single_indent = false # true/false + +# The number of spaces to indent a namespace block. +# If set to zero, use the value indent_columns +indent_namespace_level = 0 # unsigned number + +# If the body of the namespace is longer than this number, it won't be +# indented. Requires indent_namespace=true. 0 means no limit. +indent_namespace_limit = 0 # unsigned number + +# Whether the 'extern "C"' body is indented. +indent_extern = false # true/false + +# Whether the 'class' body is indented. +indent_class = true # true/false + +# Whether to indent the stuff after a leading base class colon. +indent_class_colon = true # true/false + +# Whether to indent based on a class colon instead of the stuff after the +# colon. Requires indent_class_colon=true. +indent_class_on_colon = false # true/false + +# Whether to indent the stuff after a leading class initializer colon. +indent_constr_colon = false # true/false + +# Virtual indent from the ':' for member initializers. +# +# Default: 2 +indent_ctor_init_leading = 2 # unsigned number + +# Additional indent for constructor initializer list. +# Negative values decrease indent down to the first column. +indent_ctor_init = 0 # number + +# Whether to indent 'if' following 'else' as a new block under the 'else'. +# If false, 'else\nif' is treated as 'else if' for indenting purposes. +indent_else_if = false # true/false + +# Amount to indent variable declarations after a open brace. +# +# <0: Relative +# >=0: Absolute +indent_var_def_blk = 0 # number + +# Whether to indent continued variable declarations instead of aligning. +indent_var_def_cont = false # true/false + +# Whether to indent continued shift expressions ('<<' and '>>') instead of +# aligning. Set align_left_shift=false when enabling this. +indent_shift = false # true/false + +# Whether to force indentation of function definitions to start in column 1. +indent_func_def_force_col1 = false # true/false + +# Whether to indent continued function call parameters one indent level, +# rather than aligning parameters under the open parenthesis. +indent_func_call_param = false # true/false + +# Same as indent_func_call_param, but for function definitions. +indent_func_def_param = false # true/false + +# Same as indent_func_call_param, but for function prototypes. +indent_func_proto_param = false # true/false + +# Same as indent_func_call_param, but for class declarations. +indent_func_class_param = false # true/false + +# Same as indent_func_call_param, but for class variable constructors. +indent_func_ctor_var_param = false # true/false + +# Same as indent_func_call_param, but for template parameter lists. +indent_template_param = false # true/false + +# Double the indent for indent_func_xxx_param options. +# Use both values of the options indent_columns and indent_param. +indent_func_param_double = false # true/false + +# Indentation column for standalone 'const' qualifier on a function +# prototype. +indent_func_const = 0 # unsigned number + +# Indentation column for standalone 'throw' qualifier on a function +# prototype. +indent_func_throw = 0 # unsigned number + +# The number of spaces to indent a continued '->' or '.'. +# Usually set to 0, 1, or indent_columns. +indent_member = 0 # unsigned number + +# Whether lines broken at '.' or '->' should be indented by a single indent. +# The indent_member option will not be effective if this is set to true. +indent_member_single = false # true/false + +# Spaces to indent single line ('//') comments on lines before code. +indent_sing_line_comments = 0 # unsigned number + +# Whether to indent trailing single line ('//') comments relative to the code +# instead of trying to keep the same absolute column. +indent_relative_single_line_comments = false # true/false + +# Spaces to indent 'case' from 'switch'. Usually 0 or indent_columns. +indent_switch_case = 2 # unsigned number + +# Whether to indent preprocessor statements inside of switch statements. +# +# Default: true +indent_switch_pp = true # true/false + +# Spaces to shift the 'case' line, without affecting any other lines. +# Usually 0. +indent_case_shift = 0 # unsigned number + +# Spaces to indent '{' from 'case'. By default, the brace will appear under +# the 'c' in case. Usually set to 0 or indent_columns. Negative values are OK. +indent_case_brace = 0 # number + +# Whether to indent comments found in first column. +indent_col1_comment = true # true/false + +# Whether to indent multi string literal in first column. +indent_col1_multi_string_literal = false # true/false + +# How to indent goto labels. +# +# >0: Absolute column where 1 is the leftmost column +# <=0: Subtract from brace indent +# +# Default: 1 +indent_label = 1 # number + +# Same as indent_label, but for access specifiers that are followed by a +# colon. +# +# Default: 1 +indent_access_spec = 1 # number + +# Whether to indent the code after an access specifier by one level. +# If true, this option forces 'indent_access_spec=0'. +indent_access_spec_body = false # true/false + +# If an open parenthesis is followed by a newline, whether to indent the next +# line so that it lines up after the open parenthesis (not recommended). +indent_paren_nl = false # true/false + +# How to indent a close parenthesis after a newline. +# +# 0: Indent to body level (default) +# 1: Align under the open parenthesis +# 2: Indent to the brace level +indent_paren_close = 0 # unsigned number + +# Whether to indent the open parenthesis of a function definition, +# if the parenthesis is on its own line. +indent_paren_after_func_def = false # true/false + +# Whether to indent the open parenthesis of a function declaration, +# if the parenthesis is on its own line. +indent_paren_after_func_decl = false # true/false + +# Whether to indent the open parenthesis of a function call, +# if the parenthesis is on its own line. +indent_paren_after_func_call = false # true/false + +# Whether to indent a comma when inside a parenthesis. +# If true, aligns under the open parenthesis. +indent_comma_paren = false # true/false + +# Whether to indent a Boolean operator when inside a parenthesis. +# If true, aligns under the open parenthesis. +indent_bool_paren = false # true/false + +# Whether to indent a semicolon when inside a for parenthesis. +# If true, aligns under the open for parenthesis. +indent_semicolon_for_paren = false # true/false + +# Whether to align the first expression to following ones +# if indent_bool_paren=true. +indent_first_bool_expr = false # true/false + +# Whether to align the first expression to following ones +# if indent_semicolon_for_paren=true. +indent_first_for_expr = false # true/false + +# If an open square is followed by a newline, whether to indent the next line +# so that it lines up after the open square (not recommended). +indent_square_nl = false # true/false + +# (ESQL/C) Whether to preserve the relative indent of 'EXEC SQL' bodies. +indent_preserve_sql = false # true/false + +# Whether to align continued statements at the '='. If false or if the '=' is +# followed by a newline, the next line is indent one tab. +# +# Default: true +indent_align_assign = true # true/false + +# Whether to align continued statements at the '('. If false or the '(' is +# followed by a newline, the next line indent is one tab. +# +# Default: true +indent_align_paren = true # true/false + +# (OC) Whether to indent Objective-C blocks at brace level instead of usual +# rules. +indent_oc_block = false # true/false + +# (OC) Indent for Objective-C blocks in a message relative to the parameter +# name. +# +# =0: Use indent_oc_block rules +# >0: Use specified number of spaces to indent +indent_oc_block_msg = 0 # unsigned number + +# (OC) Minimum indent for subsequent parameters +indent_oc_msg_colon = 0 # unsigned number + +# (OC) Whether to prioritize aligning with initial colon (and stripping spaces +# from lines, if necessary). +# +# Default: true +indent_oc_msg_prioritize_first_colon = true # true/false + +# (OC) Whether to indent blocks the way that Xcode does by default +# (from the keyword if the parameter is on its own line; otherwise, from the +# previous indentation level). Requires indent_oc_block_msg=true. +indent_oc_block_msg_xcode_style = false # true/false + +# (OC) Whether to indent blocks from where the brace is, relative to a +# message keyword. Requires indent_oc_block_msg=true. +indent_oc_block_msg_from_keyword = false # true/false + +# (OC) Whether to indent blocks from where the brace is, relative to a message +# colon. Requires indent_oc_block_msg=true. +indent_oc_block_msg_from_colon = false # true/false + +# (OC) Whether to indent blocks from where the block caret is. +# Requires indent_oc_block_msg=true. +indent_oc_block_msg_from_caret = false # true/false + +# (OC) Whether to indent blocks from where the brace caret is. +# Requires indent_oc_block_msg=true. +indent_oc_block_msg_from_brace = false # true/false + +# When indenting after virtual brace open and newline add further spaces to +# reach this minimum indent. +indent_min_vbrace_open = 0 # unsigned number + +# Whether to add further spaces after regular indent to reach next tabstop +# when identing after virtual brace open and newline. +indent_vbrace_open_on_tabstop = false # true/false + +# How to indent after a brace followed by another token (not a newline). +# true: indent all contained lines to match the token +# false: indent all contained lines to match the brace +# +# Default: true +indent_token_after_brace = true # true/false + +# Whether to indent the body of a C++11 lambda. +indent_cpp_lambda_body = false # true/false + +# (C#) Whether to indent a 'using' block if no braces are used. +# +# Default: true +indent_using_block = true # true/false + +# How to indent the continuation of ternary operator. +# +# 0: Off (default) +# 1: When the `if_false` is a continuation, indent it under `if_false` +# 2: When the `:` is a continuation, indent it under `?` +indent_ternary_operator = 0 # unsigned number + +# If true, the indentation of the chunks after a `return new` sequence will be set at return indentation column. +indent_off_after_return_new = false # true/false + +# If true, the tokens after return are indented with regular single indentation. By default (false) the indentation is after the return token. +indent_single_after_return = false # true/false + +# Whether to ignore indent and alignment for 'asm' blocks (i.e. assume they +# have their own indentation). +indent_ignore_asm_block = false # true/false + +# +# Newline adding and removing options +# + +# Whether to collapse empty blocks between '{' and '}'. +nl_collapse_empty_body = true # true/false + +# Don't split one-line braced assignments, as in 'foo_t f = { 1, 2 };'. +nl_assign_leave_one_liners = false # true/false + +# Don't split one-line braced statements inside a 'class xx { }' body. +nl_class_leave_one_liners = false # true/false + +# Don't split one-line enums, as in 'enum foo { BAR = 15 };' +nl_enum_leave_one_liners = false # true/false + +# Don't split one-line get or set functions. +nl_getset_leave_one_liners = false # true/false + +# (C#) Don't split one-line property get or set functions. +nl_cs_property_leave_one_liners = false # true/false + +# Don't split one-line function definitions, as in 'int foo() { return 0; }'. +nl_func_leave_one_liners = false # true/false + +# Don't split one-line C++11 lambdas, as in '[]() { return 0; }'. +nl_cpp_lambda_leave_one_liners = false # true/false + +# Don't split one-line if/else statements, as in 'if(...) b++;'. +nl_if_leave_one_liners = false # true/false + +# Don't split one-line while statements, as in 'while(...) b++;'. +nl_while_leave_one_liners = false # true/false + +# Don't split one-line for statements, as in 'for(...) b++;'. +nl_for_leave_one_liners = false # true/false + +# (OC) Don't split one-line Objective-C messages. +nl_oc_msg_leave_one_liner = false # true/false + +# (OC) Add or remove newline between method declaration and '{'. +nl_oc_mdef_brace = ignore # ignore/add/remove/force + +# (OC) Add or remove newline between Objective-C block signature and '{'. +nl_oc_block_brace = ignore # ignore/add/remove/force + +# (OC) Add or remove newline between '@interface' and '{'. +nl_oc_interface_brace = ignore # ignore/add/remove/force + +# (OC) Add or remove newline between '@implementation' and '{'. +nl_oc_implementation_brace = ignore # ignore/add/remove/force + +# Add or remove newlines at the start of the file. +nl_start_of_file = ignore # ignore/add/remove/force + +# The minimum number of newlines at the start of the file (only used if +# nl_start_of_file is 'add' or 'force'). +nl_start_of_file_min = 0 # unsigned number + +# Add or remove newline at the end of the file. +nl_end_of_file = add # ignore/add/remove/force + +# The minimum number of newlines at the end of the file (only used if +# nl_end_of_file is 'add' or 'force'). +nl_end_of_file_min = 1 # unsigned number + +# Add or remove newline between '=' and '{'. +nl_assign_brace = ignore # ignore/add/remove/force + +# (D) Add or remove newline between '=' and '['. +nl_assign_square = ignore # ignore/add/remove/force + +# Add or remove newline between '[]' and '{'. +nl_tsquare_brace = ignore # ignore/add/remove/force + +# (D) Add or remove newline after '= ['. Will also affect the newline before +# the ']'. +nl_after_square_assign = ignore # ignore/add/remove/force + +# Add or remove newline between a function call's ')' and '{', as in +# 'list_for_each(item, &list) { }'. +nl_fcall_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'enum' and '{'. +nl_enum_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'enum' and 'class'. +nl_enum_class = ignore # ignore/add/remove/force + +# Add or remove newline between 'enum class' and the identifier. +nl_enum_class_identifier = ignore # ignore/add/remove/force + +# Add or remove newline between 'enum class' type and ':'. +nl_enum_identifier_colon = ignore # ignore/add/remove/force + +# Add or remove newline between 'enum class identifier :' and type. +nl_enum_colon_type = ignore # ignore/add/remove/force + +# Add or remove newline between 'struct and '{'. +nl_struct_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'union' and '{'. +nl_union_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'if' and '{'. +nl_if_brace = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and 'else'. +nl_brace_else = ignore # ignore/add/remove/force + +# Add or remove newline between 'else if' and '{'. If set to ignore, +# nl_if_brace is used instead. +nl_elseif_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'else' and '{'. +nl_else_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'else' and 'if'. +nl_else_if = ignore # ignore/add/remove/force + +# Add or remove newline before 'if'/'else if' closing parenthesis. +nl_before_if_closing_paren = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and 'finally'. +nl_brace_finally = ignore # ignore/add/remove/force + +# Add or remove newline between 'finally' and '{'. +nl_finally_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'try' and '{'. +nl_try_brace = ignore # ignore/add/remove/force + +# Add or remove newline between get/set and '{'. +nl_getset_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'for' and '{'. +nl_for_brace = ignore # ignore/add/remove/force + +# Add or remove newline before the '{' of a 'catch' statement, as in +# 'catch (decl) {'. +nl_catch_brace = ignore # ignore/add/remove/force + +# (OC) Add or remove newline before the '{' of a '@catch' statement, as in +# '@catch (decl) {'. If set to ignore, nl_catch_brace is used. +nl_oc_catch_brace = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and 'catch'. +nl_brace_catch = ignore # ignore/add/remove/force + +# (OC) Add or remove newline between '}' and '@catch'. If set to ignore, +# nl_brace_catch is used. +nl_oc_brace_catch = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and ']'. +nl_brace_square = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and ')' in a function invocation. +nl_brace_fparen = ignore # ignore/add/remove/force + +# Add or remove newline between 'while' and '{'. +nl_while_brace = ignore # ignore/add/remove/force + +# (D) Add or remove newline between 'scope (x)' and '{'. +nl_scope_brace = ignore # ignore/add/remove/force + +# (D) Add or remove newline between 'unittest' and '{'. +nl_unittest_brace = ignore # ignore/add/remove/force + +# (D) Add or remove newline between 'version (x)' and '{'. +nl_version_brace = ignore # ignore/add/remove/force + +# (C#) Add or remove newline between 'using' and '{'. +nl_using_brace = ignore # ignore/add/remove/force + +# Add or remove newline between two open or close braces. Due to general +# newline/brace handling, REMOVE may not work. +nl_brace_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'do' and '{'. +nl_do_brace = ignore # ignore/add/remove/force + +# Add or remove newline between '}' and 'while' of 'do' statement. +nl_brace_while = ignore # ignore/add/remove/force + +# Add or remove newline between 'switch' and '{'. +nl_switch_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'synchronized' and '{'. +nl_synchronized_brace = ignore # ignore/add/remove/force + +# Add a newline between ')' and '{' if the ')' is on a different line than the +# if/for/etc. +# +# Overrides nl_for_brace, nl_if_brace, nl_switch_brace, nl_while_switch and +# nl_catch_brace. +nl_multi_line_cond = false # true/false + +# Force a newline in a define after the macro name for multi-line defines. +nl_multi_line_define = false # true/false + +# Whether to add a newline before 'case', and a blank line before a 'case' +# statement that follows a ';' or '}'. +nl_before_case = false # true/false + +# Whether to add a newline after a 'case' statement. +nl_after_case = false # true/false + +# Add or remove newline between a case ':' and '{'. +# +# Overrides nl_after_case. +nl_case_colon_brace = ignore # ignore/add/remove/force + +# Add or remove newline between ')' and 'throw'. +nl_before_throw = ignore # ignore/add/remove/force + +# Add or remove newline between 'namespace' and '{'. +nl_namespace_brace = remove # ignore/add/remove/force + +# Add or remove newline between 'template<>' and whatever follows. +nl_template_class = ignore # ignore/add/remove/force + +# Add or remove newline between 'class' and '{'. +nl_class_brace = remove # ignore/add/remove/force + +# Add or remove newline before or after (depending on pos_class_comma, +# may not be IGNORE) each',' in the base class list. +nl_class_init_args = add # ignore/add/remove/force + +# Add or remove newline after each ',' in the constructor member +# initialization. Related to nl_constr_colon, pos_constr_colon and +# pos_constr_comma. +nl_constr_init_args = ignore # ignore/add/remove/force + +# Add or remove newline before first element, after comma, and after last +# element, in 'enum'. +nl_enum_own_lines = ignore # ignore/add/remove/force + +# Add or remove newline between return type and function name in a function +# definition. +nl_func_type_name = ignore # ignore/add/remove/force + +# Add or remove newline between return type and function name inside a class +# definition. If set to ignore, nl_func_type_name or nl_func_proto_type_name +# is used instead. +nl_func_type_name_class = ignore # ignore/add/remove/force + +# Add or remove newline between class specification and '::' +# in 'void A::f() { }'. Only appears in separate member implementation (does +# not appear with in-line implementation). +nl_func_class_scope = ignore # ignore/add/remove/force + +# Add or remove newline between function scope and name, as in +# 'void A :: f() { }'. +nl_func_scope_name = ignore # ignore/add/remove/force + +# Add or remove newline between return type and function name in a prototype. +nl_func_proto_type_name = ignore # ignore/add/remove/force + +# Add or remove newline between a function name and the opening '(' in the +# declaration. +nl_func_paren = ignore # ignore/add/remove/force + +# Overrides nl_func_paren for functions with no parameters. +nl_func_paren_empty = ignore # ignore/add/remove/force + +# Add or remove newline between a function name and the opening '(' in the +# definition. +nl_func_def_paren = ignore # ignore/add/remove/force + +# Overrides nl_func_def_paren for functions with no parameters. +nl_func_def_paren_empty = ignore # ignore/add/remove/force + +# Add or remove newline between a function name and the opening '(' in the +# call. +nl_func_call_paren = ignore # ignore/add/remove/force + +# Overrides nl_func_call_paren for functions with no parameters. +nl_func_call_paren_empty = ignore # ignore/add/remove/force + +# Add or remove newline after '(' in a function declaration. +nl_func_decl_start = ignore # ignore/add/remove/force + +# Add or remove newline after '(' in a function definition. +nl_func_def_start = ignore # ignore/add/remove/force + +# Overrides nl_func_decl_start when there is only one parameter. +nl_func_decl_start_single = ignore # ignore/add/remove/force + +# Overrides nl_func_def_start when there is only one parameter. +nl_func_def_start_single = ignore # ignore/add/remove/force + +# Whether to add a newline after '(' in a function declaration if '(' and ')' +# are in different lines. If false, nl_func_decl_start is used instead. +nl_func_decl_start_multi_line = false # true/false + +# Whether to add a newline after '(' in a function definition if '(' and ')' +# are in different lines. If false, nl_func_def_start is used instead. +nl_func_def_start_multi_line = false # true/false + +# Add or remove newline after each ',' in a function declaration. +nl_func_decl_args = add # ignore/add/remove/force + +# Add or remove newline after each ',' in a function definition. +nl_func_def_args = ignore # ignore/add/remove/force + +# Whether to add a newline after each ',' in a function declaration if '(' +# and ')' are in different lines. If false, nl_func_decl_args is used instead. +nl_func_decl_args_multi_line = false # true/false + +# Whether to add a newline after each ',' in a function definition if '(' +# and ')' are in different lines. If false, nl_func_def_args is used instead. +nl_func_def_args_multi_line = false # true/false + +# Add or remove newline before the ')' in a function declaration. +nl_func_decl_end = ignore # ignore/add/remove/force + +# Add or remove newline before the ')' in a function definition. +nl_func_def_end = ignore # ignore/add/remove/force + +# Overrides nl_func_decl_end when there is only one parameter. +nl_func_decl_end_single = ignore # ignore/add/remove/force + +# Overrides nl_func_def_end when there is only one parameter. +nl_func_def_end_single = ignore # ignore/add/remove/force + +# Whether to add a newline before ')' in a function declaration if '(' and ')' +# are in different lines. If false, nl_func_decl_end is used instead. +nl_func_decl_end_multi_line = false # true/false + +# Whether to add a newline before ')' in a function definition if '(' and ')' +# are in different lines. If false, nl_func_def_end is used instead. +nl_func_def_end_multi_line = false # true/false + +# Add or remove newline between '()' in a function declaration. +nl_func_decl_empty = ignore # ignore/add/remove/force + +# Add or remove newline between '()' in a function definition. +nl_func_def_empty = ignore # ignore/add/remove/force + +# Add or remove newline between '()' in a function call. +nl_func_call_empty = ignore # ignore/add/remove/force + +# Whether to add a newline after '(' in a function call, +# has preference over nl_func_call_start_multi_line. +nl_func_call_start = ignore # ignore/add/remove/force + +# Whether to add a newline after '(' in a function call if '(' and ')' are in +# different lines. +nl_func_call_start_multi_line = false # true/false + +# Whether to add a newline after each ',' in a function call if '(' and ')' +# are in different lines. +nl_func_call_args_multi_line = false # true/false + +# Whether to add a newline before ')' in a function call if '(' and ')' are in +# different lines. +nl_func_call_end_multi_line = false # true/false + +# (OC) Whether to put each Objective-C message parameter on a separate line. +# See nl_oc_msg_leave_one_liner. +nl_oc_msg_args = false # true/false + +# Add or remove newline between function signature and '{'. +nl_fdef_brace = ignore # ignore/add/remove/force + +# Add or remove newline between function signature and '{', +# if signature ends with ')'. Overrides nl_fdef_brace. +nl_fdef_brace_cond = ignore # ignore/add/remove/force + +# Add or remove newline between C++11 lambda signature and '{'. +nl_cpp_ldef_brace = ignore # ignore/add/remove/force + +# Add or remove newline between 'return' and the return expression. +nl_return_expr = ignore # ignore/add/remove/force + +# Whether to add a newline after semicolons, except in 'for' statements. +nl_after_semicolon = false # true/false + +# (Java) Add or remove newline between the ')' and '{{' of the double brace +# initializer. +nl_paren_dbrace_open = ignore # ignore/add/remove/force + +# Whether to add a newline after the type in an unnamed temporary +# direct-list-initialization. +nl_type_brace_init_lst = ignore # ignore/add/remove/force + +# Whether to add a newline after the open brace in an unnamed temporary +# direct-list-initialization. +nl_type_brace_init_lst_open = ignore # ignore/add/remove/force + +# Whether to add a newline before the close brace in an unnamed temporary +# direct-list-initialization. +nl_type_brace_init_lst_close = ignore # ignore/add/remove/force + +# Whether to add a newline after '{'. This also adds a newline before the +# matching '}'. +nl_after_brace_open = false # true/false + +# Whether to add a newline between the open brace and a trailing single-line +# comment. Requires nl_after_brace_open=true. +nl_after_brace_open_cmt = false # true/false + +# Whether to add a newline after a virtual brace open with a non-empty body. +# These occur in un-braced if/while/do/for statement bodies. +nl_after_vbrace_open = false # true/false + +# Whether to add a newline after a virtual brace open with an empty body. +# These occur in un-braced if/while/do/for statement bodies. +nl_after_vbrace_open_empty = false # true/false + +# Whether to add a newline after '}'. Does not apply if followed by a +# necessary ';'. +nl_after_brace_close = false # true/false + +# Whether to add a newline after a virtual brace close, +# as in 'if (foo) a++; return;'. +nl_after_vbrace_close = false # true/false + +# Add or remove newline between the close brace and identifier, +# as in 'struct { int a; } b;'. Affects enumerations, unions and +# structures. If set to ignore, uses nl_after_brace_close. +nl_brace_struct_var = ignore # ignore/add/remove/force + +# Whether to alter newlines in '#define' macros. +nl_define_macro = true # true/false + +# Whether to alter newlines between consecutive parenthesis closes. The number +# of closing parentheses in a line will depend on respective open parenthesis +# lines. +nl_squeeze_paren_close = false # true/false + +# Whether to remove blanks after '#ifxx' and '#elxx', or before '#elxx' and +# '#endif'. Does not affect top-level #ifdefs. +nl_squeeze_ifdef = false # true/false + +# Makes the nl_squeeze_ifdef option affect the top-level #ifdefs as well. +nl_squeeze_ifdef_top_level = false # true/false + +# Add or remove blank line before 'if'. +nl_before_if = add # ignore/add/remove/force + +# Add or remove blank line after 'if' statement. Add/Force work only if the +# next token is not a closing brace. +nl_after_if = ignore # ignore/add/remove/force + +# Add or remove blank line before 'for'. +nl_before_for = add # ignore/add/remove/force + +# Add or remove blank line after 'for' statement. +nl_after_for = ignore # ignore/add/remove/force + +# Add or remove blank line before 'while'. +nl_before_while = add # ignore/add/remove/force + +# Add or remove blank line after 'while' statement. +nl_after_while = ignore # ignore/add/remove/force + +# Add or remove blank line before 'switch'. +nl_before_switch = add # ignore/add/remove/force + +# Add or remove blank line after 'switch' statement. +nl_after_switch = ignore # ignore/add/remove/force + +# Add or remove blank line before 'synchronized'. +nl_before_synchronized = ignore # ignore/add/remove/force + +# Add or remove blank line after 'synchronized' statement. +nl_after_synchronized = ignore # ignore/add/remove/force + +# Add or remove blank line before 'do'. +nl_before_do = add # ignore/add/remove/force + +# Add or remove blank line after 'do/while' statement. +nl_after_do = ignore # ignore/add/remove/force + +# Whether to put a blank line before 'return' statements, unless after an open +# brace. +nl_before_return = false # true/false + +# Whether to put a blank line after 'return' statements, unless followed by a +# close brace. +nl_after_return = false # true/false + +# Whether to double-space commented-entries in 'struct'/'union'/'enum'. +nl_ds_struct_enum_cmt = true # true/false + +# Whether to force a newline before '}' of a 'struct'/'union'/'enum'. +# (Lower priority than eat_blanks_before_close_brace.) +nl_ds_struct_enum_close_brace = true # true/false + +# Add or remove newline before or after (depending on pos_class_colon) a class +# colon, as in 'class Foo : public Bar'. +nl_class_colon = ignore # ignore/add/remove/force + +# Add or remove newline around a class constructor colon. The exact position +# depends on nl_constr_init_args, pos_constr_colon and pos_constr_comma. +nl_constr_colon = ignore # ignore/add/remove/force + +# Whether to collapse a two-line namespace, like 'namespace foo\n{ decl; }' +# into a single line. If true, prevents other brace newline rules from turning +# such code into four lines. +nl_namespace_two_to_one_liner = false # true/false + +# Whether to remove a newline in simple unbraced if statements, turning them +# into one-liners, as in 'if(b)\n i++;' => 'if(b) i++;'. +nl_create_if_one_liner = true # true/false + +# Whether to remove a newline in simple unbraced for statements, turning them +# into one-liners, as in 'for (...)\n stmt;' => 'for (...) stmt;'. +nl_create_for_one_liner = true # true/false + +# Whether to remove a newline in simple unbraced while statements, turning +# them into one-liners, as in 'while (expr)\n stmt;' => 'while (expr) stmt;'. +nl_create_while_one_liner = true # true/false + +# Whether to collapse a function definition whose body (not counting braces) +# is only one line so that the entire definition (prototype, braces, body) is +# a single line. +nl_create_func_def_one_liner = false # true/false + +# Whether to split one-line simple unbraced if statements into two lines by +# adding a newline, as in 'if(b) i++;'. +nl_split_if_one_liner = false # true/false + +# Whether to split one-line simple unbraced for statements into two lines by +# adding a newline, as in 'for (...) stmt;'. +nl_split_for_one_liner = false # true/false + +# Whether to split one-line simple unbraced while statements into two lines by +# adding a newline, as in 'while (expr) stmt;'. +nl_split_while_one_liner = false # true/false + +# +# Blank line options +# + +# The maximum number of consecutive newlines (3 = 2 blank lines). +nl_max = 3 # unsigned number + +# The maximum number of consecutive newlines in a function. +nl_max_blank_in_func = 0 # unsigned number + +# The number of newlines before a function prototype. +nl_before_func_body_proto = 0 # unsigned number + +# The number of newlines before a multi-line function definition. +nl_before_func_body_def = 0 # unsigned number + +# The number of newlines before a class constructor/destructor prototype. +nl_before_func_class_proto = 0 # unsigned number + +# The number of newlines before a class constructor/destructor definition. +nl_before_func_class_def = 0 # unsigned number + +# The number of newlines after a function prototype. +nl_after_func_proto = 0 # unsigned number + +# The number of newlines after a function prototype, if not followed by +# another function prototype. +nl_after_func_proto_group = 0 # unsigned number + +# The number of newlines after a class constructor/destructor prototype. +nl_after_func_class_proto = 0 # unsigned number + +# The number of newlines after a class constructor/destructor prototype, +# if not followed by another constructor/destructor prototype. +nl_after_func_class_proto_group = 0 # unsigned number + +# Whether one-line method definitions inside a class body should be treated +# as if they were prototypes for the purposes of adding newlines. +# +# Requires nl_class_leave_one_liners=true. Overrides nl_before_func_body_def +# and nl_before_func_class_def for one-liners. +nl_class_leave_one_liner_groups = false # true/false + +# The number of newlines after '}' of a multi-line function body. +nl_after_func_body = 2 # unsigned number + +# The number of newlines after '}' of a multi-line function body in a class +# declaration. Also affects class constructors/destructors. +# +# Overrides nl_after_func_body. +nl_after_func_body_class = 0 # unsigned number + +# The number of newlines after '}' of a single line function body. Also +# affects class constructors/destructors. +# +# Overrides nl_after_func_body and nl_after_func_body_class. +nl_after_func_body_one_liner = 2 # unsigned number + +# The number of blank lines after a block of variable definitions at the top +# of a function body. +# +# 0 = No change (default). +nl_func_var_def_blk = 1 # unsigned number + +# The number of newlines before a block of typedefs. If nl_after_access_spec +# is non-zero, that option takes precedence. +# +# 0 = No change (default). +nl_typedef_blk_start = 0 # unsigned number + +# The number of newlines after a block of typedefs. +# +# 0 = No change (default). +nl_typedef_blk_end = 0 # unsigned number + +# The maximum number of consecutive newlines within a block of typedefs. +# +# 0 = No change (default). +nl_typedef_blk_in = 0 # unsigned number + +# The number of newlines before a block of variable definitions not at the top +# of a function body. If nl_after_access_spec is non-zero, that option takes +# precedence. +# +# 0 = No change (default). +nl_var_def_blk_start = 0 # unsigned number + +# The number of newlines after a block of variable definitions not at the top +# of a function body. +# +# 0 = No change (default). +nl_var_def_blk_end = 0 # unsigned number + +# The maximum number of consecutive newlines within a block of variable +# definitions. +# +# 0 = No change (default). +nl_var_def_blk_in = 0 # unsigned number + +# The minimum number of newlines before a multi-line comment. +# Doesn't apply if after a brace open or another multi-line comment. +nl_before_block_comment = 2 # unsigned number + +# The minimum number of newlines before a single-line C comment. +# Doesn't apply if after a brace open or other single-line C comments. +nl_before_c_comment = 2 # unsigned number + +# The minimum number of newlines before a CPP comment. +# Doesn't apply if after a brace open or other CPP comments. +nl_before_cpp_comment = 2 # unsigned number + +# Whether to force a newline after a multi-line comment. +nl_after_multiline_comment = true # true/false + +# Whether to force a newline after a label's colon. +nl_after_label_colon = false # true/false + +# The number of newlines after '}' or ';' of a struct/enum/union definition. +nl_after_struct = 0 # unsigned number + +# The number of newlines before a class definition. +nl_before_class = 0 # unsigned number + +# The number of newlines after '}' or ';' of a class definition. +nl_after_class = 0 # unsigned number + +# The number of newlines before an access specifier label. This also includes +# the Qt-specific 'signals:' and 'slots:'. Will not change the newline count +# if after a brace open. +# +# 0 = No change (default). +nl_before_access_spec = 2 # unsigned number + +# The number of newlines after an access specifier label. This also includes +# the Qt-specific 'signals:' and 'slots:'. Will not change the newline count +# if after a brace open. +# +# 0 = No change (default). +# +# Overrides nl_typedef_blk_start and nl_var_def_blk_start. +nl_after_access_spec = 2 # unsigned number + +# The number of newlines between a function definition and the function +# comment, as in '// comment\n void foo() {...}'. +# +# 0 = No change (default). +nl_comment_func_def = 1 # unsigned number + +# The number of newlines after a try-catch-finally block that isn't followed +# by a brace close. +# +# 0 = No change (default). +nl_after_try_catch_finally = 1 # unsigned number + +# (C#) The number of newlines before and after a property, indexer or event +# declaration. +# +# 0 = No change (default). +nl_around_cs_property = 0 # unsigned number + +# (C#) The number of newlines between the get/set/add/remove handlers. +# +# 0 = No change (default). +nl_between_get_set = 0 # unsigned number + +# (C#) Add or remove newline between property and the '{'. +nl_property_brace = ignore # ignore/add/remove/force + +# The number of newlines after '{' of a namespace. This also adds newlines +# before the matching '}'. +# +# 0 = Apply eat_blanks_after_open_brace or eat_blanks_before_close_brace if +# applicable, otherwise no change. +# +# Overrides eat_blanks_after_open_brace and eat_blanks_before_close_brace. +nl_inside_namespace = 0 # unsigned number + +# Whether to remove blank lines after '{'. +eat_blanks_after_open_brace = true # true/false + +# Whether to remove blank lines before '}'. +eat_blanks_before_close_brace = true # true/false + +# How aggressively to remove extra newlines not in preprocessor. +# +# 0: No change (default) +# 1: Remove most newlines not handled by other config +# 2: Remove all newlines and reformat completely by config +nl_remove_extra_newlines = 0 # unsigned number + +# (Java) Add or remove newline after an annotation statement. Only affects +# annotations that are after a newline. +nl_after_annotation = ignore # ignore/add/remove/force + +# (Java) Add or remove newline between two annotations. +nl_between_annotation = ignore # ignore/add/remove/force + +# +# Positioning options +# + +# The position of arithmetic operators in wrapped expressions. +pos_arith = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of assignment in wrapped expressions. Do not affect '=' +# followed by '{'. +pos_assign = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of Boolean operators in wrapped expressions. +pos_bool = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of comparison operators in wrapped expressions. +pos_compare = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of conditional operators, as in the '?' and ':' of +# 'expr ? stmt : stmt', in wrapped expressions. +pos_conditional = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of the comma in wrapped expressions. +pos_comma = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of the comma in enum entries. +pos_enum_comma = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of the comma in the base class list if there is more than one +# line. Affects nl_class_init_args. +pos_class_comma = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of the comma in the constructor initialization list. +# Related to nl_constr_colon, nl_constr_init_args and pos_constr_colon. +pos_constr_comma = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of trailing/leading class colon, between class and base class +# list. Affects nl_class_colon. +pos_class_colon = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# The position of colons between constructor and member initialization. +# Related to nl_constr_colon, nl_constr_init_args and pos_constr_comma. +pos_constr_colon = ignore # ignore/break/force/lead/trail/join/lead_break/lead_force/trail_break/trail_force + +# +# Line splitting options +# + +# Try to limit code width to N columns. +code_width = 142 # unsigned number + +# Whether to fully split long 'for' statements at semi-colons. +ls_for_split_full = false # true/false + +# Whether to fully split long function prototypes/calls at commas. +# The option ls_code_width has priority over the option ls_func_split_full. +ls_func_split_full = true # true/false + +# Whether to split lines as close to code_width as possible and ignore some +# groupings. +# The option ls_code_width has priority over the option ls_func_split_full. +ls_code_width = false # true/false + +# +# Code alignment options (not left column spaces/tabs) +# + +# Whether to keep non-indenting tabs. +align_keep_tabs = false # true/false + +# Whether to use tabs for aligning. +align_with_tabs = false # true/false + +# Whether to bump out to the next tab when aligning. +align_on_tabstop = false # true/false + +# Whether to right-align numbers. +align_number_right = false # true/false + +# Whether to keep whitespace not required for alignment. +align_keep_extra_space = false # true/false + +# Whether to align variable definitions in prototypes and functions. +align_func_params = true # true/false + +# The span for aligning parameter definitions in function on parameter name. +# +# 0 = Don't align (default). +align_func_params_span = 0 # unsigned number + +# The threshold for aligning function parameter definitions. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_func_params_thresh = 0 # number + +# The gap for aligning function parameter definitions. +align_func_params_gap = 0 # unsigned number + +# The span for aligning constructor value. +# +# 0 = Don't align (default). +align_constr_value_span = 0 # unsigned number + +# The threshold for aligning constructor value. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_constr_value_thresh = 0 # number + +# The gap for aligning constructor value. +align_constr_value_gap = 0 # unsigned number + +# Whether to align parameters in single-line functions that have the same +# name. The function names must already be aligned with each other. +align_same_func_call_params = true # true/false + +# The span for aligning function-call parameters for single line functions. +# +# 0 = Don't align (default). +align_same_func_call_params_span = 0 # unsigned number + +# The threshold for aligning function-call parameters for single line +# functions. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_same_func_call_params_thresh = 0 # number + +# The span for aligning variable definitions. +# +# 0 = Don't align (default). +align_var_def_span = 1 # unsigned number + +# How to consider (or treat) the '*' in the alignment of variable definitions. +# +# 0: Part of the type 'void * foo;' (default) +# 1: Part of the variable 'void *foo;' +# 2: Dangling 'void *foo;' +# Dangling: the '*' will not be taken into account when aligning. +align_var_def_star_style = 2 # unsigned number + +# How to consider (or treat) the '&' in the alignment of variable definitions. +# +# 0: Part of the type 'long & foo;' (default) +# 1: Part of the variable 'long &foo;' +# 2: Dangling 'long &foo;' +# Dangling: the '&' will not be taken into account when aligning. +align_var_def_amp_style = 2 # unsigned number + +# The threshold for aligning variable definitions. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_var_def_thresh = 3 # number + +# The gap for aligning variable definitions. +align_var_def_gap = 1 # unsigned number + +# Whether to align the colon in struct bit fields. +align_var_def_colon = true # true/false + +# The gap for aligning the colon in struct bit fields. +align_var_def_colon_gap = 0 # unsigned number + +# Whether to align any attribute after the variable name. +align_var_def_attribute = true # true/false + +# Whether to align inline struct/enum/union variable definitions. +align_var_def_inline = true # true/false + +# The span for aligning on '=' in assignments. +# +# 0 = Don't align (default). +align_assign_span = 1 # unsigned number + +# The span for aligning on '=' in function prototype modifier. +# +# 0 = Don't align (default). +align_assign_func_proto_span = 0 # unsigned number + +# The threshold for aligning on '=' in assignments. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_assign_thresh = 0 # number + +# How to apply align_assign_span to function declaration "assignments", i.e. +# 'virtual void foo() = 0' or '~foo() = {default|delete}'. +# +# 0: Align with other assignments (default) +# 1: Align with each other, ignoring regular assignments +# 2: Don't align +align_assign_decl_func = 0 # unsigned number + +# The span for aligning on '=' in enums. +# +# 0 = Don't align (default). +align_enum_equ_span = 1 # unsigned number + +# The threshold for aligning on '=' in enums. +# Use a negative number for absolute thresholds. +# +# 0 = no limit (default). +align_enum_equ_thresh = 0 # number + +# The span for aligning class member definitions. +# +# 0 = Don't align (default). +align_var_class_span = 0 # unsigned number + +# The threshold for aligning class member definitions. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_var_class_thresh = 0 # number + +# The gap for aligning class member definitions. +align_var_class_gap = 0 # unsigned number + +# The span for aligning struct/union member definitions. +# +# 0 = Don't align (default). +align_var_struct_span = 1 # unsigned number + +# The threshold for aligning struct/union member definitions. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_var_struct_thresh = 0 # number + +# The gap for aligning struct/union member definitions. +align_var_struct_gap = 0 # unsigned number + +# The span for aligning struct initializer values. +# +# 0 = Don't align (default). +align_struct_init_span = 1 # unsigned number + +# The span for aligning single-line typedefs. +# +# 0 = Don't align (default). +align_typedef_span = 1 # unsigned number + +# The minimum space between the type and the synonym of a typedef. +align_typedef_gap = 0 # unsigned number + +# How to align typedef'd functions with other typedefs. +# +# 0: Don't mix them at all (default) +# 1: Align the open parenthesis with the types +# 2: Align the function type name with the other type names +align_typedef_func = 0 # unsigned number + +# How to consider (or treat) the '*' in the alignment of typedefs. +# +# 0: Part of the typedef type, 'typedef int * pint;' (default) +# 1: Part of type name: 'typedef int *pint;' +# 2: Dangling: 'typedef int *pint;' +# Dangling: the '*' will not be taken into account when aligning. +align_typedef_star_style = 2 # unsigned number + +# How to consider (or treat) the '&' in the alignment of typedefs. +# +# 0: Part of the typedef type, 'typedef int & intref;' (default) +# 1: Part of type name: 'typedef int &intref;' +# 2: Dangling: 'typedef int &intref;' +# Dangling: the '&' will not be taken into account when aligning. +align_typedef_amp_style = 2 # unsigned number + +# The span for aligning comments that end lines. +# +# 0 = Don't align (default). +align_right_cmt_span = 4 # unsigned number + +# Minimum number of columns between preceding text and a trailing comment in +# order for the comment to qualify for being aligned. Must be non-zero to have +# an effect. +align_right_cmt_gap = 0 # unsigned number + +# If aligning comments, whether to mix with comments after '}' and #endif with +# less than three spaces before the comment. +align_right_cmt_mix = false # true/false + +# Whether to only align trailing comments that are at the same brace level. +align_right_cmt_same_level = false # true/false + +# Minimum column at which to align trailing comments. Comments which are +# aligned beyond this column, but which can be aligned in a lesser column, +# may be "pulled in". +# +# 0 = Ignore (default). +align_right_cmt_at_col = 1 # unsigned number + +# The span for aligning function prototypes. +# +# 0 = Don't align (default). +align_func_proto_span = 3 # unsigned number + +# The threshold for aligning function prototypes. +# Use a negative number for absolute thresholds. +# +# 0 = No limit (default). +align_func_proto_thresh = 0 # number + +# Minimum gap between the return type and the function name. +align_func_proto_gap = 0 # unsigned number + +# Whether to align function prototypes on the 'operator' keyword instead of +# what follows. +align_on_operator = true # true/false + +# Whether to mix aligning prototype and variable declarations. If true, +# align_var_def_XXX options are used instead of align_func_proto_XXX options. +align_mix_var_proto = false # true/false + +# Whether to align single-line functions with function prototypes. +# Uses align_func_proto_span. +align_single_line_func = true # true/false + +# Whether to align the open brace of single-line functions. +# Requires align_single_line_func=true. Uses align_func_proto_span. +align_single_line_brace = true # true/false + +# Gap for align_single_line_brace. +align_single_line_brace_gap = 0 # unsigned number + +# (OC) The span for aligning Objective-C message specifications. +# +# 0 = Don't align (default). +align_oc_msg_spec_span = 0 # unsigned number + +# Whether to align macros wrapped with a backslash and a newline. This will +# not work right if the macro contains a multi-line comment. +align_nl_cont = true # true/false + +# Whether to align macro functions and variables together. +align_pp_define_together = false # true/false + +# The span for aligning on '#define' bodies. +# +# =0: Don't align (default) +# >0: Number of lines (including comments) between blocks +align_pp_define_span = 0 # unsigned number + +# The minimum space between label and value of a preprocessor define. +align_pp_define_gap = 0 # unsigned number + +# Whether to align lines that start with '<<' with previous '<<'. +# +# Default: true +align_left_shift = true # true/false + +# Whether to align text after 'asm volatile ()' colons. +align_asm_colon = false # true/false + +# (OC) Span for aligning parameters in an Objective-C message call +# on the ':'. +# +# 0 = Don't align. +align_oc_msg_colon_span = 0 # unsigned number + +# (OC) Whether to always align with the first parameter, even if it is too +# short. +align_oc_msg_colon_first = false # true/false + +# (OC) Whether to align parameters in an Objective-C '+' or '-' declaration +# on the ':'. +align_oc_decl_colon = false # true/false + +# +# Comment modification options +# + +# Try to wrap comments at N columns. +cmt_width = 140 # unsigned number + +# How to reflow comments. +# +# 0: No reflowing (apart from the line wrapping due to cmt_width) (default) +# 1: No touching at all +# 2: Full reflow +cmt_reflow_mode = 0 # unsigned number + +# Whether to convert all tabs to spaces in comments. If false, tabs in +# comments are left alone, unless used for indenting. +cmt_convert_tab_to_spaces = false # true/false + +# Whether to apply changes to multi-line comments, including cmt_width, +# keyword substitution and leading chars. +# +# Default: true +cmt_indent_multi = true # true/false + +# Whether to group c-comments that look like they are in a block. +cmt_c_group = false # true/false + +# Whether to put an empty '/*' on the first line of the combined c-comment. +cmt_c_nl_start = false # true/false + +# Whether to add a newline before the closing '*/' of the combined c-comment. +cmt_c_nl_end = false # true/false + +# Whether to change cpp-comments into c-comments. +cmt_cpp_to_c = false # true/false + +# Whether to group cpp-comments that look like they are in a block. Only +# meaningful if cmt_cpp_to_c=true. +cmt_cpp_group = false # true/false + +# Whether to put an empty '/*' on the first line of the combined cpp-comment +# when converting to a c-comment. +# +# Requires cmt_cpp_to_c=true and cmt_cpp_group=true. +cmt_cpp_nl_start = false # true/false + +# Whether to add a newline before the closing '*/' of the combined cpp-comment +# when converting to a c-comment. +# +# Requires cmt_cpp_to_c=true and cmt_cpp_group=true. +cmt_cpp_nl_end = false # true/false + +# Whether to put a star on subsequent comment lines. +cmt_star_cont = false # true/false + +# The number of spaces to insert at the start of subsequent comment lines. +cmt_sp_before_star_cont = 0 # unsigned number + +# The number of spaces to insert after the star on subsequent comment lines. +cmt_sp_after_star_cont = 0 # unsigned number + +# For multi-line comments with a '*' lead, remove leading spaces if the first +# and last lines of the comment are the same length. +# +# Default: true +cmt_multi_check_last = true # true/false + +# For multi-line comments with a '*' lead, remove leading spaces if the first +# and last lines of the comment are the same length AND if the length is +# bigger as the first_len minimum. +# +# Default: 4 +cmt_multi_first_len_minimum = 4 # unsigned number + +# Path to a file that contains text to insert at the beginning of a file if +# the file doesn't start with a C/C++ comment. If the inserted text contains +# '$(filename)', that will be replaced with the current file's name. +cmt_insert_file_header = "" # string + +# Path to a file that contains text to insert at the end of a file if the +# file doesn't end with a C/C++ comment. If the inserted text contains +# '$(filename)', that will be replaced with the current file's name. +cmt_insert_file_footer = "" # string + +# Path to a file that contains text to insert before a function definition if +# the function isn't preceded by a C/C++ comment. If the inserted text +# contains '$(function)', '$(javaparam)' or '$(fclass)', these will be +# replaced with, respectively, the name of the function, the javadoc '@param' +# and '@return' stuff, or the name of the class to which the member function +# belongs. +cmt_insert_func_header = "" # string + +# Path to a file that contains text to insert before a class if the class +# isn't preceded by a C/C++ comment. If the inserted text contains '$(class)', +# that will be replaced with the class name. +cmt_insert_class_header = "" # string + +# Path to a file that contains text to insert before an Objective-C message +# specification, if the method isn't preceded by a C/C++ comment. If the +# inserted text contains '$(message)' or '$(javaparam)', these will be +# replaced with, respectively, the name of the function, or the javadoc +# '@param' and '@return' stuff. +cmt_insert_oc_msg_header = "" # string + +# Whether a comment should be inserted if a preprocessor is encountered when +# stepping backwards from a function name. +# +# Applies to cmt_insert_oc_msg_header, cmt_insert_func_header and +# cmt_insert_class_header. +cmt_insert_before_preproc = false # true/false + +# Whether a comment should be inserted if a function is declared inline to a +# class definition. +# +# Applies to cmt_insert_func_header. +# +# Default: true +cmt_insert_before_inlines = true # true/false + +# Whether a comment should be inserted if the function is a class constructor +# or destructor. +# +# Applies to cmt_insert_func_header. +cmt_insert_before_ctor_dtor = false # true/false + +# +# Code modifying options (non-whitespace) +# + +# Add or remove braces on a single-line 'do' statement. +mod_full_brace_do = add # ignore/add/remove/force + +# Add or remove braces on a single-line 'for' statement. +mod_full_brace_for = add # ignore/add/remove/force + +# (Pawn) Add or remove braces on a single-line function definition. +mod_full_brace_function = ignore # ignore/add/remove/force + +# Add or remove braces on a single-line 'if' statement. Braces will not be +# removed if the braced statement contains an 'else'. +mod_full_brace_if = add # ignore/add/remove/force + +# Whether to enforce that all blocks of an 'if'/'else if'/'else' chain either +# have, or do not have, braces. If true, braces will be added if any block +# needs braces, and will only be removed if they can be removed from all +# blocks. +# +# Overrides mod_full_brace_if. +mod_full_brace_if_chain = false # true/false + +# Whether to add braces to all blocks of an 'if'/'else if'/'else' chain. +# If true, mod_full_brace_if_chain will only remove braces from an 'if' that +# does not have an 'else if' or 'else'. +mod_full_brace_if_chain_only = false # true/false + +# Add or remove braces on single-line 'while' statement. +mod_full_brace_while = add # ignore/add/remove/force + +# Add or remove braces on single-line 'using ()' statement. +mod_full_brace_using = ignore # ignore/add/remove/force + +# Don't remove braces around statements that span N newlines +mod_full_brace_nl = 1 # unsigned number + +# Whether to prevent removal of braces from 'if'/'for'/'while'/etc. blocks +# which span multiple lines. +# +# Affects: +# mod_full_brace_for +# mod_full_brace_if +# mod_full_brace_if_chain +# mod_full_brace_if_chain_only +# mod_full_brace_while +# mod_full_brace_using +# +# Does not affect: +# mod_full_brace_do +# mod_full_brace_function +mod_full_brace_nl_block_rem_mlcond = false # true/false + +# Add or remove unnecessary parenthesis on 'return' statement. +mod_paren_on_return = remove # ignore/add/remove/force + +# (Pawn) Whether to change optional semicolons to real semicolons. +mod_pawn_semicolon = false # true/false + +# Whether to fully parenthesize Boolean expressions in 'while' and 'if' +# statement, as in 'if (a && b > c)' => 'if (a && (b > c))'. +mod_full_paren_if_bool = true # true/false + +# Whether to remove superfluous semicolons. +mod_remove_extra_semicolon = true # true/false + +# If a function body exceeds the specified number of newlines and doesn't have +# a comment after the close brace, a comment will be added. +mod_add_long_function_closebrace_comment = 0 # unsigned number + +# If a namespace body exceeds the specified number of newlines and doesn't +# have a comment after the close brace, a comment will be added. +mod_add_long_namespace_closebrace_comment = 0 # unsigned number + +# If a class body exceeds the specified number of newlines and doesn't have a +# comment after the close brace, a comment will be added. +mod_add_long_class_closebrace_comment = 0 # unsigned number + +# If a switch body exceeds the specified number of newlines and doesn't have a +# comment after the close brace, a comment will be added. +mod_add_long_switch_closebrace_comment = 0 # unsigned number + +# If an #ifdef body exceeds the specified number of newlines and doesn't have +# a comment after the #endif, a comment will be added. +mod_add_long_ifdef_endif_comment = 1 # unsigned number + +# If an #ifdef or #else body exceeds the specified number of newlines and +# doesn't have a comment after the #else, a comment will be added. +mod_add_long_ifdef_else_comment = 1 # unsigned number + +# Whether to sort consecutive single-line 'import' statements. +mod_sort_import = false # true/false + +# (C#) Whether to sort consecutive single-line 'using' statements. +mod_sort_using = false # true/false + +# Whether to sort consecutive single-line '#include' statements (C/C++) and +# '#import' statements (Objective-C). Be aware that this has the potential to +# break your code if your includes/imports have ordering dependencies. +mod_sort_include = false # true/false + +# Whether to move a 'break' that appears after a fully braced 'case' before +# the close brace, as in 'case X: { ... } break;' => 'case X: { ... break; }'. +mod_move_case_break = true # true/false + +# Add or remove braces around a fully braced case statement. Will only remove +# braces if there are no variable declarations in the block. +mod_case_brace = ignore # ignore/add/remove/force + +# Whether to remove a void 'return;' that appears as the last statement in a +# function. +mod_remove_empty_return = true # true/false + +# Add or remove the comma after the last value of an enumeration. +mod_enum_last_comma = ignore # ignore/add/remove/force + +# (OC) Whether to organize the properties. If true, properties will be +# rearranged according to the mod_sort_oc_property_*_weight factors. +mod_sort_oc_properties = false # true/false + +# (OC) Weight of a class property modifier. +mod_sort_oc_property_class_weight = 0 # number + +# (OC) Weight of 'atomic' and 'nonatomic'. +mod_sort_oc_property_thread_safe_weight = 0 # number + +# (OC) Weight of 'readwrite' when organizing properties. +mod_sort_oc_property_readwrite_weight = 0 # number + +# (OC) Weight of a reference type specifier ('retain', 'copy', 'assign', +# 'weak', 'strong') when organizing properties. +mod_sort_oc_property_reference_weight = 0 # number + +# (OC) Weight of getter type ('getter=') when organizing properties. +mod_sort_oc_property_getter_weight = 0 # number + +# (OC) Weight of setter type ('setter=') when organizing properties. +mod_sort_oc_property_setter_weight = 0 # number + +# (OC) Weight of nullability type ('nullable', 'nonnull', 'null_unspecified', +# 'null_resettable') when organizing properties. +mod_sort_oc_property_nullability_weight = 0 # number + +# +# Preprocessor options +# + +# Add or remove indentation of preprocessor directives inside #if blocks +# at brace level 0 (file-level). +pp_indent = ignore # ignore/add/remove/force + +# Whether to indent #if/#else/#endif at the brace level. If false, these are +# indented from column 1. +pp_indent_at_level = false # true/false + +# Specifies the number of columns to indent preprocessors per level +# at brace level 0 (file-level). If pp_indent_at_level=false, also specifies +# the number of columns to indent preprocessors per level +# at brace level > 0 (function-level). +# +# Default: 1 +pp_indent_count = 1 # unsigned number + +# Add or remove space after # based on pp_level of #if blocks. +pp_space = add # ignore/add/remove/force + +# Sets the number of spaces per level added with pp_space. +pp_space_count = 0 # unsigned number + +# The indent for '#region' and '#endregion' in C# and '#pragma region' in +# C/C++. Negative values decrease indent down to the first column. +pp_indent_region = 0 # number + +# Whether to indent the code between #region and #endregion. +pp_region_indent_code = false # true/false + +# If pp_indent_at_level=true, sets the indent for #if, #else and #endif when +# not at file-level. Negative values decrease indent down to the first column. +# +# =0: Indent preprocessors using output_tab_size +# >0: Column at which all preprocessors will be indented +pp_indent_if = 0 # number + +# Whether to indent the code between #if, #else and #endif. +pp_if_indent_code = false # true/false + +# Whether to indent '#define' at the brace level. If false, these are +# indented from column 1. +pp_define_at_level = false # true/false + +# Whether to ignore the '#define' body while formatting. +pp_ignore_define_body = false # true/false + +# Whether to indent case statements between #if, #else, and #endif. +# Only applies to the indent of the preprocesser that the case statements +# directly inside of. +# +# Default: true +pp_indent_case = true # true/false + +# Whether to indent whole function definitions between #if, #else, and #endif. +# Only applies to the indent of the preprocesser that the function definition +# is directly inside of. +# +# Default: true +pp_indent_func_def = true # true/false + +# Whether to indent extern C blocks between #if, #else, and #endif. +# Only applies to the indent of the preprocesser that the extern block is +# directly inside of. +# +# Default: true +pp_indent_extern = true # true/false + +# Whether to indent braces directly inside #if, #else, and #endif. +# Only applies to the indent of the preprocesser that the braces are directly +# inside of. +# +# Default: true +pp_indent_brace = true # true/false + +# +# Sort includes options +# + +# The regex for include category with priority 0. +include_category_0 = "" # string + +# The regex for include category with priority 1. +include_category_1 = "" # string + +# The regex for include category with priority 2. +include_category_2 = "" # string + +# +# Use or Do not Use options +# + +# true: indent_func_call_param will be used (default) +# false: indent_func_call_param will NOT be used +# +# Default: true +use_indent_func_call_param = true # true/false + +# The value of the indentation for a continuation line is calculated +# differently if the statement is: +# - a declaration: your case with QString fileName ... +# - an assignment: your case with pSettings = new QSettings( ... +# +# At the second case the indentation value might be used twice: +# - at the assignment +# - at the function call (if present) +# +# To prevent the double use of the indentation value, use this option with the +# value 'true'. +# +# true: indent_continue will be used only once +# false: indent_continue will be used every time (default) +use_indent_continue_only_once = false # true/false + +# The value might be used twice: +# - at the assignment +# - at the opening brace +# +# To prevent the double use of the indentation value, use this option with the +# value 'true'. +# +# true: indentation will be used only once +# false: indentation will be used every time (default) +indent_cpp_lambda_only_once = false # true/false + +# Whether to apply special formatting for Qt SIGNAL/SLOT macros. Essentially, +# this tries to format these so that they match Qt's normalized form (i.e. the +# result of QMetaObject::normalizedSignature), which can slightly improve the +# performance of the QObject::connect call, rather than how they would +# otherwise be formatted. +# +# See options_for_QT.cpp for details. +# +# Default: true +use_options_overriding_for_qt_macros = true # true/false + +# +# Warn levels - 1: error, 2: warning (default), 3: note +# + +# (C#) Warning is given if doing tab-to-\t replacement and we have found one +# in a C# verbatim string literal. +# +# Default: 2 +warn_level_tabs_found_in_verbatim_string_literals = 2 # unsigned number + +# Meaning of the settings: +# Ignore - do not do any changes +# Add - makes sure there is 1 or more space/brace/newline/etc +# Force - makes sure there is exactly 1 space/brace/newline/etc, +# behaves like Add in some contexts +# Remove - removes space/brace/newline/etc +# +# +# - Token(s) can be treated as specific type(s) with the 'set' option: +# `set tokenType tokenString [tokenString...]` +# +# Example: +# `set BOOL __AND__ __OR__` +# +# tokenTypes are defined in src/token_enum.h, use them without the +# 'CT_' prefix: 'CT_BOOL' => 'BOOL' +# +# +# - Token(s) can be treated as type(s) with the 'type' option. +# `type tokenString [tokenString...]` +# +# Example: +# `type int c_uint_8 Rectangle` +# +# This can also be achieved with `set TYPE int c_uint_8 Rectangle` +# +# +# To embed whitespace in tokenStrings use the '\' escape character, or quote +# the tokenStrings. These quotes are supported: "'` +# +# +# - Support for the auto detection of languages through the file ending can be +# added using the 'file_ext' command. +# `file_ext langType langString [langString..]` +# +# Example: +# `file_ext CPP .ch .cxx .cpp.in` +# +# langTypes are defined in uncrusify_types.h in the lang_flag_e enum, use +# them without the 'LANG_' prefix: 'LANG_CPP' => 'CPP' +# +# +# - Custom macro-based indentation can be set up using 'macro-open', +# 'macro-else' and 'macro-close'. +# `(macro-open | macro-else | macro-close) tokenString` +# +# Example: +# `macro-open BEGIN_TEMPLATE_MESSAGE_MAP` +# `macro-open BEGIN_MESSAGE_MAP` +# `macro-close END_MESSAGE_MAP` +# +# +# option(s) with 'not default' value: 164 +#