From a5e86804523d688d56718b9d6499a6d337952214 Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 15 Jan 2024 10:42:34 -0600 Subject: [PATCH] Added CustomNVSPartition Example To do: Add to list of Other Examples in documentation. --- .../CustomNVSPartition/CustomNVSPartition.ino | 134 ++++++++++++++++++ .../partitions.csv | 0 .../MaxAccessories/MaxAccessories.ino | 104 -------------- 3 files changed, 134 insertions(+), 104 deletions(-) create mode 100644 examples/Other Examples/CustomNVSPartition/CustomNVSPartition.ino rename examples/Other Examples/{MaxAccessories => CustomNVSPartition}/partitions.csv (100%) delete mode 100644 examples/Other Examples/MaxAccessories/MaxAccessories.ino diff --git a/examples/Other Examples/CustomNVSPartition/CustomNVSPartition.ino b/examples/Other Examples/CustomNVSPartition/CustomNVSPartition.ino new file mode 100644 index 0000000..2f97905 --- /dev/null +++ b/examples/Other Examples/CustomNVSPartition/CustomNVSPartition.ino @@ -0,0 +1,134 @@ +/********************************************************************************* + * MIT License + * + * Copyright (c) 2020-2023 Gregg E. Berman + * + * https://github.com/HomeSpan/HomeSpan + * + * 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. + * + ********************************************************************************/ + +// This example demonstrates the use of a custom Partition Scheme file: "partitions.csv" + +// During compilation, if a file with this EXACT name is placed in the sketch folder, +// the esptool performing the compilation will use the partition scheme found +// in "partitions.csv" regardless of what partition scheme you selected in the Arduino IDE. + +// Note if you change the partition scheme it is highly recommended that you fully erase the flash +// upon your next compile/upload by enabling the "Erase All Flash" option from the Arduino IDE menu. +// NOTE: remember to turn OFF this option after you've successully uploaded a sketch with the new +// partition scheme, else you will continue to erase everything saved in the NVS every time you upload +// a new sketch (which is likely NOT what you want to occur). + +// The main reason for wanting to create your own partition scheme is to expand the NVS space. +// All of the pre-configured partition scheme you can select from the Arduino IDE provide +// for 504 records of NVS space. This is usuall sufficient for most HomeSpan projects, but if +// you have a LOT of Accessories (as per below) AND you are saving their states in NVS, you can +// use up all the NVS space. If this occurs, HomeSpan will warn you of low NVS space upon boot-up. + +// The custom partition scheme included in this sketch folder solves this problem by eliminating +// the SPIFFs partition (which is generally not used by HomeSpan) and using this portion of the flash +// to provide an NVS space with 3906 records --- more than enough for even the largest projects. + +// For reference, in addition to HomeSpan's internal use of NVS (about 32 records), saving a +// numerical Characteristic consumes one additional NVS record, and saving a string Characteristic (of +// less than 32 characters) consumes two NVS records. Also, the ESP32 WiFi stack consumes about 130 +// additional NVS records once initialized. As such, the sketch below requires: + +// 32 records (internal HomeSpan use) +// + 320 records (80 Accessories * 4 saved numerical Characterstics) +// + 160 records (80 Accessories * 2 records per saved string Characterstic) +// + 130 records (with WiFi initialized) +// ---------------------------------------- +// = 642 NVS records needed (which exceeds the normal 504 limit, unless a custom partition scheme is used) + +// Note that once HomeSpan is paired with HomeKit, additional NVS records will be consumed to store the +// pairing information for each verified HomeKit Controller. + +/////////////////////////////////////////////////////////////////////////////////////////// + +#include "HomeSpan.h" + +#define MAX_LIGHTS 80 // configure for 80 Light Accessories + +struct RGB_Light : Service::LightBulb { + + Characteristic::On power{0,true}; // save these 4 numerical Characteristics (4*80 = 320 NVS records) + Characteristic::Hue H{0,true}; + Characteristic::Saturation S{0,true}; + Characteristic::Brightness V{0,true}; + + int lightNumber; + + RGB_Light(int n) : Service::LightBulb(){ + + lightNumber=n; + LOG0("Configured RGB Light-%0d\n",lightNumber); + } + + boolean update(){ + + if(power.updated()) + LOG0("Light-%d: Power=%s",lightNumber,power.getNewVal()?"ON":"OFF"); + + if(H.updated()) + LOG0("Light-%d: Hue=%d",lightNumber,H.getNewVal()); + + if(S.updated()) + LOG0("Light-%d: Saturation=%d",lightNumber,S.getNewVal()); + + if(V.updated()) + LOG0("Light-%d: Brightness=%d",lightNumber,V.getNewVal()); + + return(false); + + } +}; + +void setup() { + + Serial.begin(115200); + + homeSpan.begin(Category::Lighting,"HomeSpan Max"); + + new SpanAccessory(); + new Service::AccessoryInformation(); + new Characteristic::Identify(); + + for(int i=1;i<=MAX_LIGHTS;i++){ + char c[60]; + sprintf(c,"Light-%02d",i); + + new SpanAccessory(); + new Service::AccessoryInformation(); + new Characteristic::Identify(); + new Characteristic::Name(c,true); // save this string Characteristic (2*80 = 160 NVS records) + + new RGB_Light(i); + } + +} + +////////////////////////////////////// + +void loop(){ + + homeSpan.poll(); +} diff --git a/examples/Other Examples/MaxAccessories/partitions.csv b/examples/Other Examples/CustomNVSPartition/partitions.csv similarity index 100% rename from examples/Other Examples/MaxAccessories/partitions.csv rename to examples/Other Examples/CustomNVSPartition/partitions.csv diff --git a/examples/Other Examples/MaxAccessories/MaxAccessories.ino b/examples/Other Examples/MaxAccessories/MaxAccessories.ino deleted file mode 100644 index d485a06..0000000 --- a/examples/Other Examples/MaxAccessories/MaxAccessories.ino +++ /dev/null @@ -1,104 +0,0 @@ -/********************************************************************************* - * MIT License - * - * Copyright (c) 2020-2023 Gregg E. Berman - * - * https://github.com/HomeSpan/HomeSpan - * - * 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 "HomeSpan.h" - -#define MAX_LIGHTS 70 - -struct RGB_Light : Service::LightBulb { - - Characteristic::On power{0,true}; - Characteristic::Hue H{0,true}; - Characteristic::Saturation S{0,true}; - Characteristic::Brightness V{0,true}; - - int lightNumber; - - RGB_Light(int n) : Service::LightBulb(){ - - lightNumber=n; - - WEBLOG("Configured RGB Light-%d",lightNumber); - WEBLOG("Light-%d: Saved Power Setting = %s",lightNumber,power.getVal()?"ON":"OFF"); - WEBLOG("Light-%d: Saved Hue Setting = %d",lightNumber,H.getVal()); - WEBLOG("Light-%d: Saved Saturation Setting = %d",lightNumber,S.getVal()); - WEBLOG("Light-%d: Saved Brightness Setting = %d",lightNumber,V.getVal()); - - } - - boolean update(){ - - if(power.updated()) - WEBLOG("Light-%d: Power=%s",lightNumber,power.getNewVal()?"ON":"OFF"); - - if(H.updated()) - WEBLOG("Light-%d: Hue=%d",lightNumber,H.getNewVal()); - - if(S.updated()) - WEBLOG("Light-%d: Saturation=%d",lightNumber,S.getNewVal()); - - if(V.updated()) - WEBLOG("Light-%d: Brightness=%d",lightNumber,V.getNewVal()); - - return(true); - - } -}; - -void setup() { - - Serial.begin(115200); - - homeSpan.setLogLevel(2); - homeSpan.enableWebLog(500); - - homeSpan.begin(Category::Lighting,"HomeSpan Max"); - - new SpanAccessory(); - new Service::AccessoryInformation(); - new Characteristic::Identify(); - - for(int i=1;i<=MAX_LIGHTS;i++){ - char c[30]; - sprintf(c,"Light-%d",i); - - new SpanAccessory(); - new Service::AccessoryInformation(); - new Characteristic::Identify(); - new Characteristic::Name(c); - - new RGB_Light(i); - } - -} - -////////////////////////////////////// - -void loop(){ - - homeSpan.poll(); -}