-
Notifications
You must be signed in to change notification settings - Fork 1
/
I2C_ExtEEPROM.h
95 lines (74 loc) · 2.79 KB
/
I2C_ExtEEPROM.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* ============================================================================
* Written by Eric Paquot, 03/2019
* https://github.com/wEPac
* ============================================================================
*
* Inspired by tiny code parts got here and there
*
* Read/Write multiple bytes to and from an I2C EEPROM.
* Valid device ID address range is 0x00~0xFF.
* Valid address range is 0x0000~0xFFFF, no checking.
*
* Features:
* - Not the fastest way but a very tiny code size.
* - Able to manage objects
* - Able to exceed the limit of 31 bytes of the Wire library
* - Able to update instead of writing
*
* ============================================================================
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* ============================================================================*/
#ifndef I2C_ExtEEPROM_h
#define I2C_ExtEEPROM_h
#include <Arduino.h>
#include <Wire.h>
class I2C_ExtEEPROMClass
{
public:
I2C_ExtEEPROMClass();
void setClock(int clockFrequency);
void setID(byte addr_ID);
// to work on 1 byte
byte read(int addr);
void write(int addr, byte data);
void check(int addr, byte data);
// to 'get', 'put' and 'update' objects into and from EEPROM
template <typename T> T &get(int addr, T &value)
{
byte* p = (byte*)(void*)&value;
byte i = sizeof(value);
while(i--) *p++ = read(addr++);
};
template <typename T> const T &put(int addr, const T &value)
{
const byte* p = (const byte*)(const void*)&value;
byte i = sizeof(value);
while(i--) write(addr++, *p++);
};
template <typename T> const T &update(int addr, const T &value)
{
const byte* p = (const byte*)(const void*)&value;
byte i = sizeof(value);
while(i--) check(addr++, *p++);
};
private:
byte _addr_ID;
void _set(int addr);
};
extern I2C_ExtEEPROMClass ExtEEPROM;
#endif // I2C_ExtEEPROM_h