forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartCounter.ino
72 lines (57 loc) · 1.85 KB
/
StartCounter.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
/*
* Microcontroller startup counter example with Portenta c33 kvstore library
* This simple example demonstrates using the KVStore library to store how many times
* the microcontroller has booted. The KVStore library is based on mbed OS KVStore library
*
* This example is based on Martin Sloup (Arcao) StartCounter example for arduino-esp32
*/
#include <KVStore.h>
#include <TDBStore.h>
#include <MBRBlockDevice.h>
auto root = BlockDevice::get_default_instance();
MBRBlockDevice bd(root, 3);
TDBStore kvstore(&bd);
void setup() {
Serial.begin(115200);
Serial.println();
while(!Serial);
// Init KVStore
if (kvstore.init() != KVSTORE_SUCCESS) {
Serial.println("Cannot initialize kvstore");
while(1) {};
}
// Remove all values stored in the kvstore
// kvstore.reset();
// Or remove the counter key only
// kvstore.remove("counter");
// Get the counter value, if it doesn't exist it returns KVSTORE_ERROR_ITEM_NOT_FOUND
unsigned int counter;
auto res = kvstore.get("counter", (void*)&counter, sizeof(counter));
if (res == KVSTORE_ERROR_ITEM_NOT_FOUND) {
counter = 0;
} else if (res == KVSTORE_SUCCESS) {
// Increase counter by 1
counter++;
} else {
Serial.print("Error getting counter from kvstore: ");
Serial.println(res);
}
// Print the counter to Serial Monitor
Serial.print("Current counter value: ");
Serial.println(counter);
// Store the updated counter value to the kvstore
if (kvstore.set("counter",(void*)&counter, sizeof(counter), 0) != KVSTORE_SUCCESS) {
Serial.println("Error setting counter from kvstore");
}
// Close the kvstore
if (kvstore.deinit() != KVSTORE_SUCCESS) {
Serial.println("Cannot deinitialize kvstore");
while(1) {};
}
// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(10000);
// Reset
NVIC_SystemReset();
}
void loop() {}