OmEspHelpers
OmEeprom.h
1 #include <vector>
2 #include <string>
3 #include <stdint.h>
4 
5 #ifndef OMEEPROM_H
6 #define OMEEPROM_H
7 
8 #ifdef NOT_ARDUINO
9 #include <string>
10 #define String std::string
11 #else
12 #include "Arduino.h"
13 #endif
14 
15 typedef enum
16 {
17  OME_TYPE_STRING = 0,
18  OME_TYPE_INT = 1,
19  OME_TYPE_BYTES = 2, // bytes array
20 } EOmEepromFieldType;
21 
22 typedef enum
23 {
26  OME_GROUP_OTA = 201,
27  OME_GROUP_WIFI_SETUP = 202,
30  OME_FLAG_BONJOUR = 0x0100,
32  OME_FLAG_HUNDREDTHS = 0x0200,
33 } EOmEepromFlags;
34 
36 {
37 public:
38  const char *name = 0;
39  EOmEepromFieldType type = OME_TYPE_STRING; // 0 string, 1 int.
40  uint16_t length = 0; // string container size (1 more than max length) or int size 1,2,4,8.
41 
42  int omeFlags = 0;
43  const char *label = 0; // typically a prettier version of the field name
44  const char *description = 0;
45 
46  int offset = 0;
47 };
48 
51 {
52 public:
53  OmEepromClass();
54  static bool active;
55  OmEepromField *addField(const char *fieldName, EOmEepromFieldType type, uint8_t length, int omeFlags, const char *label);
56 
57  void begin(const char *signature = "x"); // signature is ignored.
58  void end();
59 
60  bool get(const char *fieldName, void *valueOut, int valueLength = -1); // value length if provided limits write length
61  bool put(const char *fieldName, const void *value, int valueLength = -1); // valueLength if provided limits bytes copied, pads with 0
63  int commit();
64 
66  void dumpState(const char *note = NULL);
67 
68  // And now, the friendlier API calls.
69 
71  char *addString(const char *fieldName, uint8_t length);
77  char *addString(const char *fieldName, uint8_t length, int omeFlags, const char *label);
78 
79  int8_t *addInt8(const char *fieldName, int omeFlags = 0, const char *label = NULL);
80  int16_t *addInt16(const char *fieldName, int omeFlags = 0, const char *label = NULL);
81  int32_t *addInt32(const char *fieldName, int omeFlags = 0, const char *label = NULL);
82 
83  void addBytes(const char *fieldName, uint8_t length, int omeFlags = 0, const char *label = NULL);
84 
85  void set(const char *fieldName, String stringValue);
86  void set(const char *fieldName, int32_t intValue);
87  bool set(const char *fieldName, int first, int count, uint8_t *bytes);
88 
89  String getString(const char *fieldName);
90  void setString(const char *fieldName, String value);
91  int getInt(const char *fieldName);
92  void getBytes(const char *fieldName, int first, int count, uint8_t *bytes);
93  uint8_t getByte(const char *fieldName, int index);
94 
95  int getFieldCount();
96  int getDataSize();
97  const char *getFieldName(int ix);
98  int getFieldLength(int ix);
99  int getFieldType(int ix);
100 
101  OmEepromField *findField(const char *fieldName);
102  OmEepromField *findField(int ix);
103 
105  void fieldFromString(const char *fieldName, String value);
106 
108  String fieldToString(const char *fieldName);
109 
110  bool verbose = false;
111 
112 private:
113 
114  std::vector<OmEepromField> fields;
115  bool didBegin = false;
116 
117  int dataSize = 0; // sum of signature & fields.
118  uint8_t *data = 0; // malloc'd by fields.
119 };
120 
121 extern OmEepromClass OmEeprom;
122 
123 #endif // OMEEPROM_H
OmEepromClass::dumpState
void dumpState(const char *note=NULL)
Print out the in-memory contents of the Eeprom and other misc. Helpful for debugging.
Definition: OmEeprom.cpp:378
OmEepromClass
Wrapper for eeprom, lets you structure fields and check signature.
Definition: OmEeprom.h:51
OmEepromClass::fieldFromString
void fieldFromString(const char *fieldName, String value)
set a value from a string. convert to int for int type.
OmEepromClass::fieldToString
String fieldToString(const char *fieldName)
retrieve value as string. convert from int for int type. (todo – format styles? flags?...
OmEepromClass::addString
char * addString(const char *fieldName, uint8_t length)
Add a string field, in group 0.
Definition: OmEeprom.cpp:425
OmEepromField
Definition: OmEeprom.h:36
OmEepromClass::commit
int commit()
Definition: OmEeprom.cpp:359
OmEepromClass::active
static bool active
Definition: OmEeprom.h:54