OOKwiz
on/off-keying for ESP32 and a variety of supported radio modules
Settings.h
Go to the documentation of this file.
1 #ifndef _SETTINGS_H_
2 #define _SETTINGS_H_
3 
4 #include <map>
5 #include <Arduino.h>
6 #include "serial_output.h"
7 #include "config.h"
8 
9 // Update same-named variable only if setting exists
10 #define SETTING(name) Settings::get(#name, name);
11 
12 // Same but now sets the already existing variable to the
13 // default if setting doesn't exist.
14 #define SETTING_WITH_DEFAULT(name, default) if (!Settings::get(#name, name)) name = default;
15 
16 // For use in functions that return a bool. Assumes there is
17 // a variable (int, long, String or float) of the same name
18 // as the setting, and sets that variable to the value in the
19 // setting. If no such setting found, will show ERROR and then
20 // will "return false;" out of the function you call this from.
21 #define SETTING_OR_ERROR(name) if (!Settings::get(#name, name)) {
22  ERROR("Mandatory setting '%s' missing.\n", #name);
23  return false;\
24 }
25 
26 #define MANDATORY(name) if (!Settings::isSet(#name)) {
27  ERROR("Mandatory setting '%s' missing.\n", #name);
28  return false;\
29 }
30 
31 class Settings {
32 
33 public:
34  Settings();
35  static bool set(const String &name, const String &value = "");
36 
37  template <typename T>
38  static bool set(const String &name, const T &value = "") {
39  return set(name, String(value));
40  }
41 
42  static bool unset(const String &name);
43  static bool get(const String &name, String &value);
44  static bool get(const String &name, float &value);
45  static bool get(const String &name, int &value);
46  static bool get(const String &name, long &value);
47  static String getString(const String &name, const String dflt = "");
48  static int getInt(const String &name, const long dflt = -1);
49  static long getLong(const String &name, const long dflt = -1);
50  static float getFloat(const String &name, const float dflt = -1);
51  static bool validName(const String &name);
52  static String list();
53  static bool fromList(String in);
54  static bool save(const String filename);
55  static bool load(const String filename);
56  static bool ls();
57  static bool rm(const String filename);
58  static bool fileExists(String filename);
59  static void zap();
60  static bool isSet(const String &name);
61 
62 private:
63  static std::map<String, String> store;
64 
65 };
66 
67 #endif