EEPROM-Storage Library
The EEPROM Storage library provides the ability to access variables stored in EEPROM just as if they were stored in RAM.
 
Loading...
Searching...
No Matches
EEPROM-Checksum.h
Go to the documentation of this file.
1// Copyright © 2017-2025 Daniel Porrey. All Rights Reserved.
2//
3// This file is part of the EEPROM-Storage library.
4//
5// EEPROM-Storage library is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// EEPROM-Storage library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with EEPROM-Storage library. If not,
17// see http://www.gnu.org/licenses/.
18//
19#pragma once
20#ifndef CHECKSUM_H
21#define CHECKSUM_H
22
28//
29// Cross-compatable with Arduino, GNU C++ for tests, and Particle.
30//
31#if defined(ARDUINO) && ARDUINO >= 100
32 #include <Arduino.h>
33#elif defined(PARTICLE)
34 #include <Particle.h>
35#endif
36
45template <typename T>
47{
48 public:
58 static byte get(byte* data, uint length)
59 {
60 byte returnValue = 0;
61
62 if (length == 1)
63 {
64 //
65 // For a single byte use the bit
66 // pattern 0xAA (10101010).
67 //
68 returnValue = 0xAA ^ data[0];
69 }
70 else
71 {
72 for (uint i = 0; i < length ; i++)
73 {
74 byte b = data[i];
75 returnValue ^= b;
76 }
77 }
78
79 //
80 // Do not let the checksum be UNSET_VALUE
81 //
82 if (returnValue == UNSET_VALUE)
83 {
84 returnValue <<= 1;
85 }
86
87 return returnValue;
88 }
89
99 static byte getEEPROM(uint address, uint length)
100 {
101 byte returnValue = 0;
102
103 if (length == 1)
104 {
105 //
106 // For a single byte use the bit
107 // pattern 0xAA (10101010).
108 //
109 returnValue = 0xAA ^ EEPROM[address];
110 }
111 else
112 {
113 for (uint i = 0; i < length ; i++)
114 {
115 byte b = EEPROM[address + i];
116 returnValue ^= b;
117 }
118 }
119
120 //
121 // Do not let the checksum be UNSET_VALUE
122 //
123 if (returnValue == UNSET_VALUE)
124 {
125 returnValue <<= 1;
126 }
127
128 return returnValue;
129 }
130
136 static byte get(T value)
137 {
138 //
139 // Get a pointer to the bytes in memory
140 // for the variable value.
141 //
142 byte *ptr = (byte*) &value;
143
144 //
145 // Calculate and return the checksum.
146 //
147 return Checksum<T>::get(ptr, sizeof(T));
148 }
149};
150#endif
#define UNSET_VALUE
Defines the default value used when clearing the EEPROM memory.
Definition EEPROM-Vars.h:39
Provides checksum calculation options.
static byte get(T value)
Calculates the checksum of the value of T.
static byte get(byte *data, uint length)
Calculate the checksum of a byte array.
static byte getEEPROM(uint address, uint length)
Calculates the checksum from data in the EEPROM.