123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #include "settings.h"
- using std::string;
- using std::map;
- namespace {
- typedef const char *const Key_t;
- template<class T>
- T read(const Key_t &key);
- struct Key {
- static constexpr Key_t NETWORK_SSID = "networkSSID";
- static constexpr Key_t NETWORK_PASSWORD = "networkPassword";
- static constexpr Key_t PAIRING_ID = "pairingId";
- static constexpr Key_t CALIBRATION_HIGH = "calibHigh";
- static constexpr Key_t CALIBRATION_LOW = "calibLow";
- };
- const map<string, uint64_t> defaultValuesInt = {
- {Key::CALIBRATION_LOW, 0},
- {Key::CALIBRATION_HIGH, 0}
- };
- const map<string, string> defaultValuesStr = {
- {Key::NETWORK_SSID, ""},
- {Key::NETWORK_PASSWORD, ""},
- {Key::PAIRING_ID, ""},
- };
- kbf::nvs::NVS *nvs;
- template<>
- bool read<bool>(Key_t const &key) {
- uint8_t value;
- if (!nvs->read(key, value)) {
- value = defaultValuesInt.at(key);
- }
- return value;
- }
- template<>
- int read<int>(Key_t const &key) {
- int value;
- if (!nvs->read(key, value)) {
- value = static_cast<bool>(defaultValuesInt.at(key));
- }
- return value;
- }
- template<>
- string read<string>(Key_t const &key) {
- string value;
- if (!nvs->read(key, value)) {
- value = defaultValuesStr.at(key);
- }
- return value;
- }
- }
- namespace dk::settings {
- void init() {
- delete nvs;
- nvs = new kbf::nvs::NVS("dk_settings");
- }
- Wifi wifi() {
- if (!nvs) init();
- return (Wifi) {.ssid = read<string>(Key::NETWORK_SSID), .password = read<string>(Key::NETWORK_PASSWORD)};
- }
- void setWifi(Wifi &value) {
- if (!nvs) init();
- nvs->write(Key::NETWORK_SSID, value.ssid);
- nvs->write(Key::NETWORK_PASSWORD, value.password);
- }
- string pairingId() {
- if (!nvs) init();
- return read<string>(Key::PAIRING_ID);
- }
- void setPairingId(string pairingId) {
- if (!nvs) init();
- nvs->write(Key::PAIRING_ID, pairingId);
- }
- Calibration calibration() {
- if (!nvs) init();
- return (Calibration) {.high = read<int>(Key::CALIBRATION_HIGH), .low = read<int>(Key::CALIBRATION_LOW)};
- }
- void setCalibration(Calibration &config) {
- if (!nvs) init();
- nvs->write(Key::CALIBRATION_HIGH, config.high);
- nvs->write(Key::CALIBRATION_LOW, config.low);
- }
- }
|