Version 1.2.

This commit is contained in:
GuruSR 2021-11-03 21:39:28 -04:00 committed by GitHub
parent 9581395215
commit 35aced28f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 951 additions and 171 deletions

308
src/ArduinoNvs.cpp Normal file
View File

@ -0,0 +1,308 @@
// ArduinoNvs.cpp
// Copyright (c) 2018 Sinai RnD
// Copyright (c) 2016-2017 TridentTD
// 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 "ArduinoNvs.h"
ArduinoNvs::ArduinoNvs()
{
}
bool ArduinoNvs::begin(String namespaceNvs)
{
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
DEBUG_PRINTLN("W: NVS. Cannot init flash mem");
if (err != ESP_ERR_NVS_NO_FREE_PAGES)
return false;
// erase and reinit
DEBUG_PRINTLN("NVS. Try reinit the partition");
const esp_partition_t *nvs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
if (nvs_partition == NULL)
return false;
err = esp_partition_erase_range(nvs_partition, 0, nvs_partition->size);
esp_err_t err = nvs_flash_init();
if (err)
return false;
DEBUG_PRINTLN("NVS. Partition re-formatted");
}
err = nvs_open(namespaceNvs.c_str(), NVS_READWRITE, &_nvs_handle);
if (err != ESP_OK)
return false;
return true;
}
void ArduinoNvs::close()
{
nvs_close(_nvs_handle);
}
bool ArduinoNvs::eraseAll(bool forceCommit)
{
esp_err_t err = nvs_erase_all(_nvs_handle);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::erase(String key, bool forceCommit)
{
esp_err_t err = nvs_erase_key(_nvs_handle, key.c_str());
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::commit()
{
esp_err_t err = nvs_commit(_nvs_handle);
if (err != ESP_OK)
return false;
return true;
}
bool ArduinoNvs::setInt(String key, uint8_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u8(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, int16_t value, bool forceCommit)
{
esp_err_t err = nvs_set_i16(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, uint16_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u16(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, int32_t value, bool forceCommit)
{
esp_err_t err = nvs_set_i32(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, uint32_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u32(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, int64_t value, bool forceCommit)
{
esp_err_t err = nvs_set_i64(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setInt(String key, uint64_t value, bool forceCommit)
{
esp_err_t err = nvs_set_u64(_nvs_handle, (char *)key.c_str(), value);
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setString(String key, String value, bool forceCommit)
{
esp_err_t err = nvs_set_str(_nvs_handle, (char *)key.c_str(), value.c_str());
if (err != ESP_OK)
return false;
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setBlob(String key, uint8_t *blob, size_t length, bool forceCommit)
{
DEBUG_PRINTF("ArduinoNvs::setObjct(): set obj addr = [0x%X], length = [%d]\n", (int32_t)blob, length);
if (length == 0)
return false;
esp_err_t err = nvs_set_blob(_nvs_handle, (char *)key.c_str(), blob, length);
if (err)
{
DEBUG_PRINTF("ArduinoNvs::setObjct(): err = [0x%X]\n", err);
return false;
}
return forceCommit ? commit() : true;
}
bool ArduinoNvs::setBlob(String key, std::vector<uint8_t> &blob, bool forceCommit)
{
return setBlob(key, &blob[0], blob.size(), forceCommit);
}
int64_t ArduinoNvs::getInt(String key, int64_t default_value)
{
uint8_t v_u8;
int16_t v_i16;
uint16_t v_u16;
int32_t v_i32;
uint32_t v_u32;
int64_t v_i64;
uint64_t v_u64;
esp_err_t err;
err = nvs_get_u8(_nvs_handle, (char *)key.c_str(), &v_u8);
if (err == ESP_OK)
return (int64_t)v_u8;
err = nvs_get_i16(_nvs_handle, (char *)key.c_str(), &v_i16);
if (err == ESP_OK)
return (int64_t)v_i16;
err = nvs_get_u16(_nvs_handle, (char *)key.c_str(), &v_u16);
if (err == ESP_OK)
return (int64_t)v_u16;
err = nvs_get_i32(_nvs_handle, (char *)key.c_str(), &v_i32);
if (err == ESP_OK)
return (int64_t)v_i32;
err = nvs_get_u32(_nvs_handle, (char *)key.c_str(), &v_u32);
if (err == ESP_OK)
return (int64_t)v_u32;
err = nvs_get_i64(_nvs_handle, (char *)key.c_str(), &v_i64);
if (err == ESP_OK)
return (int64_t)v_i64;
err = nvs_get_u64(_nvs_handle, (char *)key.c_str(), &v_u64);
if (err == ESP_OK)
return (int64_t)v_u64;
return default_value;
}
bool ArduinoNvs::getString(String key, String &res)
{
size_t required_size;
esp_err_t err;
err = nvs_get_str(_nvs_handle, key.c_str(), NULL, &required_size);
if (err)
return false;
char value[required_size];
err = nvs_get_str(_nvs_handle, key.c_str(), value, &required_size);
if (err)
return false;
res = value;
return true;
}
String ArduinoNvs::getString(String key)
{
String res;
bool ok = getString(key, res);
if (!ok)
return String();
return res;
}
size_t ArduinoNvs::getBlobSize(String key)
{
size_t required_size;
esp_err_t err = nvs_get_blob(_nvs_handle, key.c_str(), NULL, &required_size);
if (err)
{
if (err != ESP_ERR_NVS_NOT_FOUND) // key_not_found is not an error, just return size 0
DEBUG_PRINTF("ArduinoNvs::getBlobSize(): err = [0x%X]\n", err);
return 0;
}
return required_size;
}
bool ArduinoNvs::getBlob(String key, uint8_t *blob, size_t length)
{
if (length == 0)
return false;
size_t required_size = getBlobSize(key);
if (required_size == 0)
return false;
if (length < required_size)
return false;
esp_err_t err = nvs_get_blob(_nvs_handle, key.c_str(), blob, &required_size);
if (err)
{
DEBUG_PRINTF("ArduinoNvs::getBlob(): get object err = [0x%X]\n", err);
return false;
}
return true;
}
bool ArduinoNvs::getBlob(String key, std::vector<uint8_t> &blob)
{
size_t required_size = getBlobSize(key);
if (required_size == 0)
return false;
blob.resize(required_size);
esp_err_t err = nvs_get_blob(_nvs_handle, key.c_str(), &blob[0], &required_size);
if (err)
{
DEBUG_PRINTF("ArduinoNvs::getBlob(): get object err = [0x%X]\n", err);
return false;
}
return true;
}
std::vector<uint8_t> ArduinoNvs::getBlob(String key)
{
std::vector<uint8_t> res;
bool ok = getBlob(key, res);
if (!ok)
res.clear();
return res;
}
bool ArduinoNvs::setFloat(String key, float value, bool forceCommit)
{
return setBlob(key, (uint8_t *)&value, sizeof(float), forceCommit);
}
float ArduinoNvs::getFloat(String key, float default_value)
{
std::vector<uint8_t> res(sizeof(float));
if (!getBlob(key, res))
return default_value;
return *(float *)(&res[0]);
}
ArduinoNvs NVS;

92
src/ArduinoNvs.h Normal file
View File

@ -0,0 +1,92 @@
// ArduinoNvs.h
// Copyright (c) 2018 Sinai RnD
// Copyright (c) 2016-2017 TridentTD
// 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 __ARDUINO_NVS_H__
#define __ARDUINO_NVS_H__
#include <Arduino.h>
#include <vector>
extern "C" {
#include "esp_partition.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "nvs.h"
}
#ifndef ARDUINONVS_SILENT
#define ARDUINONVS_SILENT 0
#endif
#if ARDUINONVS_SILENT
#define DEBUG_PRINT(...) { }
#define DEBUG_PRINTLN(...) { }
#define DEBUG_PRINTF(fmt, args...) { }
#else
#define DEBUG_PRINTER Serial
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#define DEBUG_PRINTF(fmt, args...) { DEBUG_PRINTER.printf(fmt,## args); }
#endif
class ArduinoNvs {
public:
ArduinoNvs();
bool begin(String namespaceNvs = "storage");
void close();
bool eraseAll(bool forceCommit = true);
bool erase(String key, bool forceCommit = true);
bool setInt(String key, uint8_t value, bool forceCommit = true);
bool setInt(String key, int16_t value, bool forceCommit = true);
bool setInt(String key, uint16_t value, bool forceCommit = true);
bool setInt(String key, int32_t value, bool forceCommit = true);
bool setInt(String key, uint32_t value, bool forceCommit = true);
bool setInt(String key, int64_t value, bool forceCommit = true);
bool setInt(String key, uint64_t value, bool forceCommit = true);
bool setFloat(String key, float value, bool forceCommit = true);
bool setString(String key, String value, bool forceCommit = true);
bool setBlob(String key, uint8_t* blob, size_t length, bool forceCommit = true);
bool setBlob(String key, std::vector<uint8_t>& blob, bool forceCommit = true);
int64_t getInt(String key, int64_t default_value = 0); // In case of error, default_value will be returned
float getFloat(String key, float default_value = 0);
bool getString(String key, String& res);
String getString(String key);
size_t getBlobSize(String key); /// Returns the size of the stored blob
bool getBlob(String key, uint8_t* blob, size_t length); /// User should proivde enought memory to store the loaded blob. If length < than required size to store blob, function fails.
bool getBlob(String key, std::vector<uint8_t>& blob);
std::vector<uint8_t> getBlob(String key); /// Less eficient but more simple in usage implemetation of `getBlob()`
bool commit();
protected:
nvs_handle _nvs_handle;
};
extern ArduinoNvs NVS;
#endif

View File

@ -6,7 +6,7 @@
//display //display
#define SOFTWARE_VERSION_MAJOR 1 #define SOFTWARE_VERSION_MAJOR 1
#define SOFTWARE_VERSION_MINOR 1 #define SOFTWARE_VERSION_MINOR 2
#define SOFTWARE_VERSION_PATCH 0 #define SOFTWARE_VERSION_PATCH 0
#define HARDWARE_VERSION_MAJOR 1 #define HARDWARE_VERSION_MAJOR 1
#define HARDWARE_VERSION_MINOR 0 #define HARDWARE_VERSION_MINOR 0
@ -15,6 +15,7 @@
#define WiFi_AP_SSID "Watchy Connect" #define WiFi_AP_SSID "Watchy Connect"
#define WiFi_AP_PSWD "Watchy123" #define WiFi_AP_PSWD "Watchy123"
#define WiFi_AP_HIDE false #define WiFi_AP_HIDE false
#define WiFi_AP_MAXC 4
#define WiFi_SSID_MAX 32 #define WiFi_SSID_MAX 32
#define WiFi_PASS_MAX 63 #define WiFi_PASS_MAX 63
// Use instead of Watchy Connect (if necessary) // Use instead of Watchy Connect (if necessary)
@ -70,18 +71,22 @@
#define MENU_MODE 16 #define MENU_MODE 16
#define MENU_FEED 17 #define MENU_FEED 17
#define MENU_TRBO 18 #define MENU_TRBO 18
#define MENU_SCRN 19 #define MENU_DARK 19
#define MENU_SYNC 20 #define MENU_TRBL 20
#define MENU_WIFI 21 #define MENU_SYNC 21
#define MENU_OTAU 22 #define MENU_WIFI 22
#define MENU_OTAM 23 #define MENU_OTAU 23
#define MENU_RSET 24 #define MENU_OTAM 24
#define MENU_SCRN 25
#define MENU_RSET 26
#define MENU_TOFF 27 // Time Diff offset.
// Menu segments. // Menu segments.
#define MENU_INNORMAL 0 #define MENU_INNORMAL 0
#define MENU_INALARMS 1 #define MENU_INALARMS 1
#define MENU_INTIMERS 2 #define MENU_INTIMERS 2
#define MENU_INOPTIONS 3 #define MENU_INOPTIONS 3
#define MENU_INTROUBLE 4
// Button debounce. // Button debounce.
#define KEYPAUSE 333 #define KEYPAUSE 333

File diff suppressed because it is too large Load Diff

View File

@ -23,11 +23,7 @@
#include "icons.h" #include "icons.h"
#include "Olsen2POSIX.h" #include "Olsen2POSIX.h"
#include "ArduinoNvs.h"
//#include "esp_system.h"
//#include "nvs_flash.h"
//#include "nvs.h"
//#include "nvs_handle.hpp"
#include "aAntiCorona15pt7b.h" #include "aAntiCorona15pt7b.h"
#include "Bronova_Regular13pt7b.h" #include "Bronova_Regular13pt7b.h"
@ -60,16 +56,20 @@ class WatchyGSR{
void drawMenu(); void drawMenu();
void VibeTo(bool Mode); void VibeTo(bool Mode);
//void handleAccelerometer(); //void handleAccelerometer();
void GoDark();
void detectBattery(); void detectBattery();
void ProcessNTP(); void ProcessNTP();
void UpdateUTC(); void UpdateUTC();
void UpdateClock(); void UpdateClock();
void ManageTime();
void _rtcConfig(); void _rtcConfig();
void _bmaConfig(); void _bmaConfig();
static uint16_t _readRegister(uint8_t address, uint8_t reg, uint8_t *data, uint16_t len); static uint16_t _readRegister(uint8_t address, uint8_t reg, uint8_t *data, uint16_t len);
static uint16_t _writeRegister(uint8_t address, uint8_t reg, uint8_t *data, uint16_t len); static uint16_t _writeRegister(uint8_t address, uint8_t reg, uint8_t *data, uint16_t len);
String MakeTime(int Hour, int Minutes, bool& Alarm); String MakeTime(int Hour, int Minutes, bool& Alarm);
String MakeHour(uint8_t Hour); String MakeHour(uint8_t Hour);
String MakeSeconds(uint8_t Seconds);
String MakeTOD(uint8_t Hour, bool AddZeros);
String MakeMinutes(uint8_t Minutes); String MakeMinutes(uint8_t Minutes);
String MakeSteps(uint32_t uSteps); String MakeSteps(uint32_t uSteps);
void CheckAlarm(int I); void CheckAlarm(int I);
@ -79,14 +79,13 @@ class WatchyGSR{
uint8_t getToneTimes(uint8_t ToneIndex); uint8_t getToneTimes(uint8_t ToneIndex);
String getReduce(uint8_t Amount); String getReduce(uint8_t Amount);
void monitorSteps(); void monitorSteps();
// String GetStoredTimezone();
// virtual bool StoreTimezone(String Timezone);
uint8_t getButtonPins(); uint8_t getButtonPins();
uint8_t getButtonMaskToID(uint64_t HW); uint8_t getButtonMaskToID(uint64_t HW);
uint8_t getSwapped(uint8_t pIn); uint8_t getSwapped(uint8_t pIn);
void ScreenRefresh(); void ScreenRefresh();
void AskForWiFi(); void AskForWiFi();
void processWiFiRequest(); void processWiFiRequest();
String WiFiIndicator(uint8_t Index);
wl_status_t currentWiFi(); wl_status_t currentWiFi();
void endWiFi(); void endWiFi();
static void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); static void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
@ -98,8 +97,12 @@ class WatchyGSR{
void initZeros(); void initZeros();
String GetSettings(); String GetSettings();
void StoreSettings(String FromUser); void StoreSettings(String FromUser);
String RetrieveSettings();
void RecordSettings();
void SetTurbo(); void SetTurbo();
bool InTurbo(); bool InTurbo();
bool DarkWait();
bool Showing();
void DBug(String Value); void DBug(String Value);
String ToHex(uint64_t Value); String ToHex(uint64_t Value);
}; };