forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58ef2a2
commit aed3c13
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() {} |