Skip to content

Commit f5ed5e9

Browse files
committed
Vend2: add missing lib, rename Vend->Vend2 to match folder name
1 parent f9f3704 commit f5ed5e9

File tree

11 files changed

+636
-0
lines changed

11 files changed

+636
-0
lines changed

Vend2/Vend.ino Vend2/Vend2.ino

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Demonstrate how to use FlashStorage with an API that is similar to the EEPROM library.
3+
4+
This example code is in the public domain.
5+
6+
Written by A. Christian
7+
Edited 14 Oct 2016 by Cristian Maglie
8+
*/
9+
10+
// Include EEPROM-like API for FlashStorage
11+
#include <FlashAsEEPROM.h>
12+
13+
void setup() {
14+
Serial.begin(9600);
15+
16+
// If the EEPROM is empty then isValid() is false
17+
if (!EEPROM.isValid()) {
18+
19+
Serial.println("EEPROM is empty, writing some example data:");
20+
Serial.print("->");
21+
for (int i=0; i<20; i++) {
22+
EEPROM.write(i, 100+i);
23+
Serial.print(" ");
24+
Serial.print(100+i);
25+
}
26+
Serial.println();
27+
28+
// commit() saves all the changes to EEPROM, it must be called
29+
// every time the content of virtual EEPROM is changed to make
30+
// the change permanent.
31+
// This operation burns Flash write cycles and should not be
32+
// done too often. See readme for details:
33+
// https://github.com/cmaglie/FlashStorage#limited-number-of-writes
34+
EEPROM.commit();
35+
Serial.println("Done!");
36+
37+
Serial.print("After commit, calling isValid() returns ");
38+
Serial.println(EEPROM.isValid());
39+
40+
} else {
41+
42+
Serial.println("EEPROM has been written.");
43+
Serial.println("Here is the content of the first 20 bytes:");
44+
45+
Serial.print("->");
46+
for (int i=0; i<20; i++) {
47+
Serial.print(" ");
48+
Serial.print(EEPROM.read(i));
49+
}
50+
Serial.println();
51+
52+
}
53+
}
54+
55+
void loop() {
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Store and retrieve an integer value in Flash memory.
3+
The value is increased each time the board is restarted.
4+
5+
This example code is in the public domain.
6+
7+
Written 30 Apr 2015 by Cristian Maglie
8+
*/
9+
10+
#include <FlashStorage.h>
11+
12+
// Reserve a portion of flash memory to store an "int" variable
13+
// and call it "my_flash_store".
14+
FlashStorage(my_flash_store, int);
15+
16+
// Note: the area of flash memory reserved for the variable is
17+
// lost every time the sketch is uploaded on the board.
18+
19+
void setup() {
20+
SERIAL_PORT_MONITOR.begin(9600);
21+
22+
int number;
23+
24+
// Read the content of "my_flash_store" and assign it to "number"
25+
number = my_flash_store.read();
26+
27+
// Print the current number on the serial monitor
28+
SERIAL_PORT_MONITOR.println(number);
29+
30+
// Save into "my_flash_store" the number increased by 1 for the
31+
// next run of the sketch
32+
my_flash_store.write(number + 1);
33+
}
34+
35+
void loop() {
36+
// Do nothing...
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Store and retrieve structured data in Flash memory.
3+
4+
This example code is in the public domain.
5+
6+
Written 30 Apr 2015 by Cristian Maglie
7+
*/
8+
9+
#include <FlashStorage.h>
10+
11+
// Create a structure that is big enough to contain a name
12+
// and a surname. The "valid" variable is set to "true" once
13+
// the structure is filled with actual data for the first time.
14+
typedef struct {
15+
boolean valid;
16+
char name[100];
17+
char surname[100];
18+
} Person;
19+
20+
// Reserve a portion of flash memory to store a "Person" and
21+
// call it "my_flash_store".
22+
FlashStorage(my_flash_store, Person);
23+
24+
// Note: the area of flash memory reserved lost every time
25+
// the sketch is uploaded on the board.
26+
27+
void setup() {
28+
SERIAL_PORT_MONITOR.begin(9600);
29+
while (!SERIAL_PORT_MONITOR) { }
30+
31+
// Create a "Person" variable and call it "owner"
32+
Person owner;
33+
34+
// Read the content of "my_flash_store" into the "owner" variable
35+
owner = my_flash_store.read();
36+
37+
// If this is the first run the "valid" value should be "false"...
38+
if (owner.valid == false) {
39+
40+
// ...in this case we ask for user data.
41+
SERIAL_PORT_MONITOR.setTimeout(30000);
42+
SERIAL_PORT_MONITOR.println("Insert your name:");
43+
String name = SERIAL_PORT_MONITOR.readStringUntil('\n');
44+
SERIAL_PORT_MONITOR.println("Insert your surname:");
45+
String surname = SERIAL_PORT_MONITOR.readStringUntil('\n');
46+
47+
// Fill the "owner" structure with the data entered by the user...
48+
name.toCharArray(owner.name, 100);
49+
surname.toCharArray(owner.surname, 100);
50+
// set "valid" to true, so the next time we know that we
51+
// have valid data inside
52+
owner.valid = true;
53+
54+
// ...and finally save everything into "my_flash_store"
55+
my_flash_store.write(owner);
56+
57+
// Print a confirmation of the data inserted.
58+
SERIAL_PORT_MONITOR.println();
59+
SERIAL_PORT_MONITOR.print("Your name: ");
60+
SERIAL_PORT_MONITOR.println(owner.name);
61+
SERIAL_PORT_MONITOR.print("and your surname: ");
62+
SERIAL_PORT_MONITOR.println(owner.surname);
63+
SERIAL_PORT_MONITOR.println("have been saved. Thank you!");
64+
65+
} else {
66+
67+
// Say hello to the returning user!
68+
SERIAL_PORT_MONITOR.println();
69+
SERIAL_PORT_MONITOR.print("Hi ");
70+
SERIAL_PORT_MONITOR.print(owner.name);
71+
SERIAL_PORT_MONITOR.print(" ");
72+
SERIAL_PORT_MONITOR.print(owner.surname);
73+
SERIAL_PORT_MONITOR.println(", nice to see you again :-)");
74+
75+
}
76+
}
77+
78+
void loop() {
79+
// Do nothing...
80+
}
81+

libraries/FlashStorage/library.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "FlashStorage",
3+
"keywords": "storage,data",
4+
"description": "The FlashStorage library aims to provide a convenient way to store and retrieve user's data using the non-volatile flash memory of microcontrollers.",
5+
"authors":
6+
{
7+
"name": "Arduino",
8+
"email": "[email protected]",
9+
"url": "https://github.com/cmaglie/FlashStorage",
10+
"maintainer": true
11+
},
12+
"repository":
13+
{
14+
"type": "git",
15+
"url": "https://github.com/cmaglie/FlashStorage"
16+
},
17+
"version": "0.7.1",
18+
"frameworks": "arduino",
19+
"platforms": "atmelsam"
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=FlashStorage
2+
version=0.7.1
3+
author=Various
4+
maintainer=Arduino <[email protected]>
5+
sentence=The FlashStorage library aims to provide a convenient way to store and retrieve user's data using the non-volatile flash memory of microcontrollers.
6+
paragraph=Useful if the EEPROM is not available or too small. Currently, only ATSAMD21 cpu is supported (and consequently every board based on this cpu like the Arduino Zero or Aduino MKR1000).
7+
url=https://github.com/cmaglie/FlashStorage
8+
architectures=samd
9+
category=Data Storage

libraries/FlashStorage/readme.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# FlashStorage library for Arduino
2+
3+
The FlashStorage library aims to provide a convenient way to store and retrieve
4+
user's data using the non-volatile flash memory of microcontrollers.
5+
6+
The flash memory, due to it's properties, is generally used to store the firmware
7+
code, but it can also be used to store user data.
8+
9+
## Supported hardware
10+
11+
Currently, only ATSAMD21 cpu is supported (and consequently every board based
12+
on this cpu like the Arduino Zero or Aduino MKR1000).
13+
14+
## Limited number of writes
15+
16+
Flash memory has a limited amount of write cycles. Typical flash
17+
memory can perform about 10000 writes cycles to the same flash block
18+
before starting to "wear out" and begin to lose the ability to retain data.
19+
20+
So **BEWARE: IMPROPER USE OF THIS LIBRARY CAN QUICKLY AND PERMANENTLY
21+
DESTROY THE FLASH MEMORY OF YOUR MICRO**, in particular you should avoid to
22+
call the `write()` function too often and make sure that in the entire life
23+
of the micro the number of calls to `write` stay well below the above limit
24+
of 10000 (it's a good rule-of-thumb to keep that number in mind even if the
25+
manufacturer of the micro guarantees a bigger number of cycles).
26+
27+
The same caution must be taken if you're using the EEPROM API emulation (see
28+
below) with the `EEPROM.commit()` function.
29+
30+
## Usage
31+
32+
First of all you must declare a global `FlashStorage` object for each piece of
33+
data you intend to store in the flash memory.
34+
For example if you want to store the age of a person you must declare an
35+
`age_storage` like this:
36+
37+
```c++
38+
FlashStorage(age_storage, int);
39+
```
40+
41+
this instruction means "create a `FlashStorage` to store an `int` variable and call
42+
it `age_storage`". Now you can use `age_storage` as a place to safely store an integer:
43+
44+
```c++
45+
void readAndStoreUserAge() {
46+
Serial.println("Please enter your age:");
47+
String age = Serial.readStringUntil('\n');
48+
49+
age_storage.write(age.toInt()); // <-- save the age
50+
}
51+
```
52+
53+
after a reset of the microcontroller you can retrieve the stored age by using:
54+
55+
```c++
56+
int user_age = age_storage.read();
57+
```
58+
59+
### Using the alternative EEPROM-like API
60+
61+
If you include `FlashAsEEPROM.h` you'll get an EEPROM emulation with the internal flash memory.
62+
See [EmulateEEPROM](https://github.com/cmaglie/FlashStorage/tree/master/examples/EmulateEEPROM/EmulateEEPROM.ino) sketch for an example.
63+
64+
The API is very similar to the well known Arduino EEPROM.h API but with two additional functions:
65+
66+
* `EEPROM.isValid()` returns `true` if data in the EEPROM is valid or, in other words, if the data has been written at least once, otherwise EEPROM data is "undefined" and the function returns `false`.
67+
* `EEPROM.commit()` store the EEPROM data in flash. Use this with care: Every call writes the complete EEPROM data to flash. This will reduce the remaining flash-write-cycles. Don't call this method in a loop or [you will kill your flash soon](https://github.com/cmaglie/FlashStorage#limited-number-of-writes).
68+
69+
## License
70+
71+
This library is released under LGPL-2.1.
72+
73+
## FAQ
74+
75+
### Can I use a single FlashStorage object to store more stuff?
76+
77+
Yes, you can declare a `struct` with more fields and create a `FlashStorage` object to
78+
store the entire structure. See the [StoreNameAndSurname](https://github.com/cmaglie/FlashStorage/tree/master/examples/StoreNameAndSurname/StoreNameAndSurname.ino)
79+
sketch for an example on how to do it.
80+
81+
### The content of the FlashStorage is erased each time a new sketch is uploaded?
82+
83+
Yes, every time you upload a new sketch, the previous content of the FlashStorage is erased.
84+
85+
### Do you recommend to use FLASH instead of EEPROM?
86+
87+
No. If your micro provides an EEPROM it's almost always better to use that because
88+
it's a kind of memory designed with the specific purpose to store user data (it has a
89+
longer lifetime, number of write cycles, etc...).
90+
91+
In the absence of an EEPROM you can use this library to use a piece of the flash memory
92+
as an alternative to EEPROM. However, you must always keep in mind of it's limits.
93+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
EEPROM like API that uses Arduino Zero's flash memory.
3+
Written by A. Christian
4+
5+
Copyright (c) 2015-2016 Arduino LLC. All right reserved.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
See the GNU Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
#include "FlashAsEEPROM.h"
23+
24+
FlashStorage(eeprom_storage, EEPROM_EMULATION);
25+
26+
EEPROMClass::EEPROMClass(void) : _initialized(false), _dirty(false) {
27+
// Empty
28+
}
29+
30+
uint8_t EEPROMClass::read(int address)
31+
{
32+
if (!_initialized) init();
33+
return _eeprom.data[address];
34+
}
35+
36+
void EEPROMClass::update(int address, uint8_t value)
37+
{
38+
if (!_initialized) init();
39+
if (_eeprom.data[address] != value) {
40+
_dirty = true;
41+
_eeprom.data[address] = value;
42+
}
43+
}
44+
45+
void EEPROMClass::write(int address, uint8_t value)
46+
{
47+
update(address, value);
48+
}
49+
50+
void EEPROMClass::init()
51+
{
52+
_eeprom = eeprom_storage.read();
53+
if (!_eeprom.valid) {
54+
memset(_eeprom.data, 0xFF, EEPROM_EMULATION_SIZE);
55+
}
56+
_initialized = true;
57+
}
58+
59+
bool EEPROMClass::isValid()
60+
{
61+
if (!_initialized) init();
62+
return _eeprom.valid;
63+
}
64+
65+
void EEPROMClass::commit()
66+
{
67+
if (!_initialized) init();
68+
if (_dirty) {
69+
_eeprom.valid = true;
70+
eeprom_storage.write(_eeprom);
71+
}
72+
}
73+
74+
EEPROMClass EEPROM;

0 commit comments

Comments
 (0)