-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBastWAN_EEPROM_Prefs.ino
115 lines (109 loc) · 3.42 KB
/
BastWAN_EEPROM_Prefs.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#undef min
#undef max
#include <string>
#include <Wire.h>
using namespace std;
template class basic_string<char>; // https://github.com/esp8266/Arduino/issues/1136
// Required or the code won't compile!
namespace std _GLIBCXX_VISIBILITY(default) {
_GLIBCXX_BEGIN_NAMESPACE_VERSION
void __throw_bad_alloc() {}
}
#include "SparkFun_External_EEPROM.h"
// Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM
/*
You need to define the buffer lengths in SparkFun_External_EEPROM.h
Around line 56:
#elif defined(_VARIANT_ELECTRONICCATS_BASTWAN_)
#define I2C_BUFFER_LENGTH_RX SERIAL_BUFFER_SIZE
#define I2C_BUFFER_LENGTH_TX SERIAL_BUFFER_SIZE
*/
#include "ArduinoJson.h"
// Click here to get the library: http://librarymanager/All#ArduinoJson
ExternalEEPROM myMem;
unsigned char buf[97];
double myFreq = 868125000;
uint8_t mySF = 10, myBW = 8;
string deviceName = "My BastWAN";
void setup() {
SerialUSB.begin(115200);
SerialUSB.flush();
delay(3000);
Serial.println("\nEEPROM Prefs Example");
Serial.println("Buffer size: " + String(SERIAL_BUFFER_SIZE));
Wire.begin(SDA, SCL);
Wire.setClock(100000);
// bool begin(uint8_t deviceAddress = 0b1010000, TwoWire &wirePort = Wire);
// deviceAddress = 0x50
// flip switches on the breakout board to change it to between 0x51 and 0x57.
if (myMem.begin() == false) {
Serial.println("No memory detected. Freezing.");
while (1)
;
}
// To erase the prefs:
// memset(buf, 0, 97);
// myMem.write(0, buf, 32);
// myMem.write(32, buf + 32, 32);
// myMem.write(64, buf + 64, 32);
memset(buf, 0, 97);
myMem.read(0, buf, 32);
myMem.read(32, buf + 32, 32);
myMem.read(64, buf + 64, 32);
// Let's limit the JSON string size to 96 for now.
hexDump(96);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, buf);
if (error) {
SerialUSB.println(F("\ndeserializeJson() failed!"));
savePrefs();
}
myFreq = doc["myFreq"];
Serial.print("FQ: "); Serial.println(myFreq / 1e6);
mySF = doc["mySF"] = mySF;
Serial.print("SF: "); Serial.println(mySF);
myBW = doc["myBW"];
Serial.print("BW: "); Serial.println(myBW);
const char *x = doc["deviceName"];
deviceName = x;
Serial.print("Device Name: "); Serial.println(deviceName.c_str());
}
void loop() {
}
void hexDump(uint16_t len) {
String s = "|", t = "| |";
Serial.println(F(" |.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .a .b .c .d .e .f |"));
Serial.println(F(" +------------------------------------------------+ +----------------+"));
for (uint16_t i = 0; i < len; i += 16) {
for (uint8_t j = 0; j < 16; j++) {
if (i + j >= len) {
s = s + " "; t = t + " ";
} else {
char c = buf[i + j];
if (c < 16) s = s + "0";
s = s + String(c, HEX) + " ";
if (c < 32 || c > 127) t = t + ".";
else t = t + (String(c));
}
}
uint8_t index = i / 16;
Serial.print(index, HEX); Serial.write('.');
Serial.println(s + t + "|");
s = "|"; t = "| |";
}
Serial.println(F(" +------------------------------------------------+ +----------------+"));
}
void savePrefs() {
SerialUSB.println("Saving prefs:");
StaticJsonDocument<200> doc;
doc["myFreq"] = myFreq;
doc["mySF"] = mySF;
doc["myBW"] = myBW;
doc["deviceName"] = deviceName.c_str();
memset(buf, 0, 97);
serializeJson(doc, (char*)buf, 97);
hexDump(96);
myMem.write(0, buf, 32);
myMem.write(32, buf + 32, 32);
myMem.write(64, buf + 64, 32);
}