-
Notifications
You must be signed in to change notification settings - Fork 28
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
Showing
10 changed files
with
425 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,5 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
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,39 @@ | ||
|
||
This directory is intended for project header files. | ||
|
||
A header file is a file containing C declarations and macro definitions | ||
to be shared between several project source files. You request the use of a | ||
header file in your project source file (C, C++, etc) located in `src` folder | ||
by including it, with the C preprocessing directive `#include'. | ||
|
||
```src/main.c | ||
|
||
#include "header.h" | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
Including a header file produces the same results as copying the header file | ||
into each source file that needs it. Such copying would be time-consuming | ||
and error-prone. With a header file, the related declarations appear | ||
in only one place. If they need to be changed, they can be changed in one | ||
place, and programs that include the header file will automatically use the | ||
new version when next recompiled. The header file eliminates the labor of | ||
finding and changing all the copies as well as the risk that a failure to | ||
find one copy will result in inconsistencies within a program. | ||
|
||
In C, the usual convention is to give header files names that end with `.h'. | ||
It is most portable to use only letters, digits, dashes, and underscores in | ||
header file names, and at most one dot. | ||
|
||
Read more about using header files in official GCC documentation: | ||
|
||
* Include Syntax | ||
* Include Operation | ||
* Once-Only Headers | ||
* Computed Includes | ||
|
||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html |
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,18 @@ | ||
#pragma once | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
class SauvegardeDonnees { | ||
public: | ||
virtual bool sauvegarder() = 0; | ||
virtual bool charger() = 0; | ||
|
||
inline void donnees(const TypeDonneesASauvegarder& donnees) { | ||
this->m_donnees = donnees; | ||
} | ||
|
||
inline const TypeDonneesASauvegarder& donnees() const { | ||
return this->m_donnees; | ||
} | ||
private: | ||
TypeDonneesASauvegarder m_donnees; | ||
}; |
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,222 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include <CRC16.h> | ||
#include <EEPROM.h> | ||
|
||
#include "stringUtil.h" | ||
|
||
#include "SauvegardeDonnees.h" | ||
|
||
// Organisation EEPROM : | ||
// || signature app (2 octets) || adresse debut donnees (2 octets) || ... | ||
// || nombre ecritures (2 octets) || crc donnees utilisateur (2 octets) || donnees utilisateur (sizeof(TypeDonneesASauvegarder) octets) || ... || | ||
inline String padLeft(String p_texte, uint16_t p_tailleMin, | ||
char p_remplissage = ' '); | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
class SauvegardeDonneesEEPROM | ||
: public SauvegardeDonnees<TypeDonneesASauvegarder> { | ||
public: | ||
SauvegardeDonneesEEPROM(const uint16_t& signatureApp); | ||
virtual bool sauvegarder() override; | ||
virtual bool charger() override; | ||
virtual void initIfFirstTime(); | ||
virtual void effacerEEPROM(); | ||
|
||
static void afficherContenuEEPROM(); | ||
static uint16_t tailleDonnees(); | ||
|
||
private: | ||
static uint16_t adresseCompteurEcritures(const uint16_t& adresseDonnees); | ||
static uint16_t adresseCRCDonneesUtilisateur(const uint16_t& adresseDonnees); | ||
static uint16_t adresseDonneesUtilisateur(const uint16_t& adresseDonnees); | ||
|
||
const uint16_t SIGNATURE_APP; | ||
|
||
static const uint16_t ADRESSE_SIGNATURE_APP; | ||
static const uint16_t ADRESSE_ADRESSE_DEBUT_DONNEES; | ||
static const uint16_t ADRESSE_DEBUT_DONNEES_INITIALISATION; | ||
static const uint16_t MAX_ECRIRE_PAR_ADRESSE; | ||
}; | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
const uint16_t | ||
SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::ADRESSE_SIGNATURE_APP = 0; | ||
template <typename TypeDonneesASauvegarder> | ||
const uint16_t SauvegardeDonneesEEPROM< | ||
TypeDonneesASauvegarder>::ADRESSE_ADRESSE_DEBUT_DONNEES = 2; | ||
template <typename TypeDonneesASauvegarder> | ||
const uint16_t SauvegardeDonneesEEPROM< | ||
TypeDonneesASauvegarder>::ADRESSE_DEBUT_DONNEES_INITIALISATION = 4; | ||
template <typename TypeDonneesASauvegarder> | ||
const uint16_t | ||
SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::MAX_ECRIRE_PAR_ADRESSE = | ||
2; | ||
template <typename TypeDonneesASauvegarder> | ||
SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::SauvegardeDonneesEEPROM( | ||
const uint16_t& signatureApp) | ||
: SIGNATURE_APP(signatureApp) { | ||
; | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
uint16_t SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::tailleDonnees() { | ||
return sizeof(uint16_t) + sizeof(uint16_t) + sizeof(TypeDonneesASauvegarder); | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
uint16_t | ||
SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::adresseCompteurEcritures( | ||
const uint16_t& adresseDonnees) { | ||
return adresseDonnees; | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
uint16_t | ||
SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::adresseCRCDonneesUtilisateur( | ||
const uint16_t& adresseDonnees) { | ||
return adresseCompteurEcritures(adresseDonnees) + sizeof(uint16_t); | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
uint16_t | ||
SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::adresseDonneesUtilisateur( | ||
const uint16_t& adresseDonnees) { | ||
return adresseCRCDonneesUtilisateur(adresseDonnees) + sizeof(uint16_t); | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
bool SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::sauvegarder() { | ||
bool succes = true; | ||
uint16_t adresseDonnees = 0; | ||
uint16_t compteurEcritures = 0; | ||
|
||
this->initIfFirstTime(); | ||
EEPROM.get(ADRESSE_ADRESSE_DEBUT_DONNEES, adresseDonnees); | ||
EEPROM.get(adresseCompteurEcritures(adresseDonnees), compteurEcritures); | ||
|
||
if (compteurEcritures >= MAX_ECRIRE_PAR_ADRESSE) { | ||
adresseDonnees += tailleDonnees(); | ||
if (adresseDonnees + tailleDonnees() > EEPROM.length()) { | ||
succes = false; | ||
} | ||
compteurEcritures = 0; | ||
if (succes) { | ||
EEPROM.put(ADRESSE_ADRESSE_DEBUT_DONNEES, adresseDonnees); | ||
} | ||
} | ||
|
||
++compteurEcritures; | ||
if (succes) { | ||
CRC16 crc; | ||
crc.add((uint8_t*)&this->donnees(), sizeof(TypeDonneesASauvegarder)); | ||
|
||
EEPROM.put(adresseCompteurEcritures(adresseDonnees), compteurEcritures); | ||
EEPROM.put(adresseCRCDonneesUtilisateur(adresseDonnees), crc.calc()); | ||
EEPROM.put(adresseDonneesUtilisateur(adresseDonnees), this->donnees()); | ||
|
||
succes = this->charger(); | ||
CRC16 crc2; | ||
crc2.add((uint8_t*)&(this->donnees()), sizeof(TypeDonneesASauvegarder)); | ||
succes = crc2.calc() == crc.calc(); | ||
} | ||
|
||
return succes; | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
void SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::initIfFirstTime() { | ||
uint16_t signatureAppEEPROM = 0; | ||
EEPROM.get(ADRESSE_SIGNATURE_APP, signatureAppEEPROM); | ||
|
||
if (signatureAppEEPROM != this->SIGNATURE_APP) { | ||
this->effacerEEPROM(); | ||
EEPROM.put(ADRESSE_SIGNATURE_APP, this->SIGNATURE_APP); | ||
EEPROM.put(ADRESSE_ADRESSE_DEBUT_DONNEES, | ||
ADRESSE_DEBUT_DONNEES_INITIALISATION); | ||
} | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
bool SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::charger() { | ||
bool succes = true; | ||
uint16_t adresseDonnees = 0; | ||
uint16_t crcSauvegarde = 0; | ||
TypeDonneesASauvegarder donnees; | ||
|
||
this->initIfFirstTime(); | ||
EEPROM.get(ADRESSE_ADRESSE_DEBUT_DONNEES, adresseDonnees); | ||
EEPROM.get(adresseCRCDonneesUtilisateur(adresseDonnees), crcSauvegarde); | ||
EEPROM.get(adresseDonneesUtilisateur(adresseDonnees), donnees); | ||
|
||
CRC16 crc; | ||
crc.add((uint8_t*)&donnees, sizeof(TypeDonneesASauvegarder)); | ||
succes = crcSauvegarde == crc.calc(); | ||
|
||
if (succes) { | ||
this->donnees(donnees); | ||
} | ||
|
||
return succes; | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
void SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::effacerEEPROM() { | ||
for (uint16_t adresseEEPROM = 0; adresseEEPROM < EEPROM.length(); | ||
++adresseEEPROM) { | ||
EEPROM.write(adresseEEPROM, 0); | ||
} | ||
} | ||
|
||
template <typename TypeDonneesASauvegarder> | ||
void SauvegardeDonneesEEPROM<TypeDonneesASauvegarder>::afficherContenuEEPROM() { | ||
uint16_t adresseDonnees = 0; | ||
uint16_t compteurEcritures = 0; | ||
uint16_t signatureApp = 0; | ||
uint16_t crcDonneesUtilisateur = 0; | ||
|
||
EEPROM.get(ADRESSE_SIGNATURE_APP, signatureApp); | ||
EEPROM.get(ADRESSE_ADRESSE_DEBUT_DONNEES, adresseDonnees); | ||
EEPROM.get(adresseCompteurEcritures(adresseDonnees), compteurEcritures); | ||
EEPROM.get(adresseCRCDonneesUtilisateur(adresseDonnees), crcDonneesUtilisateur); | ||
|
||
Serial.println("Taille donnees : " + String(tailleDonnees())); | ||
Serial.println("Signature app : 0x" + padLeft(String(signatureApp, HEX), 4, '0')); | ||
Serial.println("Adresse debut donnees : 0x" + padLeft(String(adresseDonnees, HEX), 4, '0')); | ||
Serial.println("Adresse CRC donnees utilisateur : 0x" + padLeft(String(adresseCRCDonneesUtilisateur(adresseDonnees), HEX), 4, '0')); | ||
Serial.println("Adresse debut donnees utilisateur : 0x" + padLeft(String(adresseDonneesUtilisateur(adresseDonnees), HEX), 4, '0')); | ||
Serial.println("Compteur ecritures : " + String(compteurEcritures, DEC)); | ||
Serial.println("CRC donnees utilisateur : 0x" + padLeft(String(crcDonneesUtilisateur, HEX), 4, '0')); | ||
Serial.println(); | ||
|
||
Serial.println("Contenu de l'EEPROM :"); | ||
Serial.println("Capacite dump : " + String(EEPROM.length(), DEC)); | ||
|
||
uint16_t lineOffset = 16; | ||
Serial.print(padLeft("", 4)); | ||
for (uint16_t compt = 0; compt < lineOffset; ++compt) { | ||
Serial.print(" " + padLeft(String(compt, HEX), 2, '0')); | ||
} | ||
Serial.println(); | ||
|
||
for (uint16_t adresse = 0; adresse < EEPROM.length(); adresse += lineOffset) { | ||
Serial.print(padLeft(String((uint16_t)adresse, HEX), 4, '0')); | ||
for (uint16_t offset = 0; | ||
offset < lineOffset && adresse + offset < EEPROM.length(); ++offset) { | ||
Serial.print(" " + | ||
padLeft(String(EEPROM.read(adresse + offset), HEX), 2, '0')); | ||
} | ||
|
||
Serial.print(" | "); | ||
for (uint16_t offset = 0; | ||
offset < lineOffset && adresse + offset < EEPROM.length(); ++offset) { | ||
char character = (char)EEPROM.read(adresse + offset); | ||
Serial.print(isprint(character) ? character : ' '); | ||
} | ||
|
||
Serial.println(" |"); | ||
} | ||
|
||
Serial.println(); | ||
} |
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,12 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
|
||
inline String padLeft(String p_texte, uint16_t p_tailleMin, | ||
char p_remplissage) { | ||
while (p_texte.length() < p_tailleMin) { | ||
p_texte = p_remplissage + p_texte; | ||
} | ||
|
||
return p_texte; | ||
} |
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,46 @@ | ||
|
||
This directory is intended for project specific (private) libraries. | ||
PlatformIO will compile them to static libraries and link into executable file. | ||
|
||
The source code of each library should be placed in a an own separate directory | ||
("lib/your_library_name/[here are source files]"). | ||
|
||
For example, see a structure of the following two libraries `Foo` and `Bar`: | ||
|
||
|--lib | ||
| | | ||
| |--Bar | ||
| | |--docs | ||
| | |--examples | ||
| | |--src | ||
| | |- Bar.c | ||
| | |- Bar.h | ||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html | ||
| | | ||
| |--Foo | ||
| | |- Foo.c | ||
| | |- Foo.h | ||
| | | ||
| |- README --> THIS FILE | ||
| | ||
|- platformio.ini | ||
|--src | ||
|- main.c | ||
|
||
and a contents of `src/main.c`: | ||
``` | ||
#include <Foo.h> | ||
#include <Bar.h> | ||
|
||
int main (void) | ||
{ | ||
... | ||
} | ||
|
||
``` | ||
|
||
PlatformIO Library Dependency Finder will find automatically dependent | ||
libraries scanning project source files. | ||
|
||
More information about PlatformIO Library Dependency Finder | ||
- https://docs.platformio.org/page/librarymanager/ldf.html |
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,17 @@ | ||
; PlatformIO Project Configuration File | ||
; | ||
; Build options: build flags, source filter | ||
; Upload options: custom upload port, speed and extra flags | ||
; Library options: dependencies, extra library storages | ||
; Advanced options: extra scripting | ||
; | ||
; Please visit documentation for the other options and examples | ||
; https://docs.platformio.org/page/projectconf.html | ||
|
||
[env:uno] | ||
platform = atmelavr | ||
board = uno | ||
framework = arduino | ||
monitor_speed = 115200 | ||
lib_deps = | ||
robtillaart/CRC@^1.0.2 |
Oops, something went wrong.