This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom_i2c.cpp
223 lines (200 loc) · 6.8 KB
/
eeprom_i2c.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "eeprom_i2c.h"
#include "Arduino.h"
/// <summary>
/// Initialised the EEPROM Object with the EEPROM Adress
/// </summary>
/// <param name="ideviceaddress">I2C Adress</param>
///
eeprom_i2c::eeprom_i2c(int ideviceaddress){
this->deviceaddress = ideviceaddress;
}
/// <summary>
// Write Data(Byte) to the eeprom(Array Write)
/// </summary>
/// <param name="_sEEPROMdataByte">Structur of sEEPROMdataByte with Items</param>
/// <param name="size">Size/Items from _sEEPROMdataByte[]</param>
///
void eeprom_i2c::write(sEEPROMdataByte _sEEPROMdataByte[], unsigned int size) {
for(byte i=0; i<size;i++){
this->write(_sEEPROMdataByte[i].blockAddress, _sEEPROMdataByte[i].data);
}
}
/// <summary>
// Write Data(Boolean) to the eeprom(Array Write)
/// </summary>
/// <param name="_sEEPROMdataBoolean">Structur of sEEPROMdataBoolean with Items</param>
/// <param name="size">Size/Items from _sEEPROMdataBoolean[]</param>
///
void eeprom_i2c::write(sEEPROMdataBoolean _sEEPROMdataBoolean[], unsigned int size) {
for(byte i=0; i<size;i++){
this->write(_sEEPROMdataBoolean[i].blockAddress, _sEEPROMdataBoolean[i].data);
}
}
/// <summary>
// Write Data(uunsigned Int) to the eeprom(Array Write)
/// </summary>
/// <param name="_sEEPROMdataUint">Structur of sEEPROMdataUint with Items</param>
/// <param name="size">Size/Items from _sEEPROMdataUint[]</param>
///
void eeprom_i2c::write(sEEPROMdataUint _sEEPROMdataUint[], unsigned int size) {
for(byte i=0; i<size;i++){
this->write(_sEEPROMdataUint[i].blockAddress, _sEEPROMdataUint[i].data);
}
}
/// <summary>
// Write Data(Byte) to the eeprom(Single Write)
/// </summary>
/// <param name="blockAddress">Adress of the the Block</param>
/// <param name="data">Data</param>
///
void eeprom_i2c::write(unsigned int blockAddress, byte data ) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddress);
Wire.write(data);
Wire.endTransmission();
delay(200);
}
/// <summary>
// Write Data(char) to the eeprom(Array Write)
/// </summary>
/// <param name="blockAddress">Adress of the the Block</param>
/// <param name="data">Char Array</param>
/// <param name="size">Size of the Char Array</param>
void eeprom_i2c::write(unsigned int blockAddress, char data[], byte size ) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddress);
for(int i=0; i<size; i++) {
Wire.write(data[i]);
}
Wire.endTransmission();
delay(200);
}
/// <summary>
// Write Data(unsigned Int) to the eeprom(Single Write)
/// </summary>
/// <param name="blockAddressStart">Adress of the the Block</param>
/// <param name="data">Data</param>
///
void eeprom_i2c::write(unsigned int blockAddressStart, unsigned int data) {
byte lowByte = (data & 0xFF);
byte highByte = ((data >> 8) & 0xFF);
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddressStart);
Wire.write(lowByte);
Wire.write(highByte);
Wire.endTransmission();
delay(200);
}
/// <summary>
// Write Data(Boolean) to the eeprom(Single Write)
/// </summary>
/// <param name="blockAddress">Adress of the the Block</param>
/// <param name="data">Data</param>
///
void eeprom_i2c::write( unsigned int blockAddress, bool data ) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddress);
Wire.write(data);
Wire.endTransmission();
delay(200);
}
/// <summary>
// Read Data(Byte) from the eeprom(Single Read)
/// </summary>
/// <param name="blockAddress">Adress of the the Block</param>
/// <param name="data">Data</param>
///
void eeprom_i2c::read(unsigned int blockAddress, byte &data) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddress);
Wire.endTransmission();
Wire.requestFrom(blockAddress, 1);
if(Wire.available()) {
data = Wire.read();
}
Wire.endTransmission();
delay(200);
}
/// <summary>
// Read Data(unsigned Int) from the eeprom(Single Read)
/// </summary>
/// <param name="blockAddressStart">Adress of the the Block</param>
/// <param name="data">Data</param>
///
void eeprom_i2c::read( unsigned int blockAddressStart, unsigned int &data) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddressStart); // MSB
Wire.endTransmission();
Wire.requestFrom(blockAddressStart,2);
if(Wire.available()) data = (unsigned int)Wire.read();
if(Wire.available()) data |= (((unsigned int)Wire.read()) << 8);
delay(200);
}
/// <summary>
// Read Data(Boolean) from the eeprom(Single Read)
/// </summary>
/// <param name="blockAddress">Adress of the the Block</param>
/// <param name="data">Data</param>
///
void eeprom_i2c::read(unsigned int blockAddress, bool &data) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddress);
Wire.endTransmission();
Wire.requestFrom(blockAddress, 1);
if(Wire.available()) {
data = bool (Wire.read());
}
Wire.endTransmission();
delay(200);
}
/// <summary>
// Read Data(char) to the eeprom(Array Write)
/// </summary>
/// <param name="blockAddress">Adress of the the Block</param>
/// <param name="data">Char Array</param>
/// <param name="size">Size of the Char Array</param>
void eeprom_i2c::read(unsigned int blockAddress, char data[], byte size) {
Wire.beginTransmission(deviceaddress);
Wire.write(blockAddress);
Wire.endTransmission();
Wire.requestFrom(blockAddress, size);
if(Wire.available()) {
for(int i=0; i<size; i++) {
data[i] = (char) Wire.read();
}
}
Wire.endTransmission();
}
/// <summary>
// Read Data(Byte) to the eeprom(Array Write)
/// </summary>
/// <param name="_sEEPROMdataByte">Structur of sEEPROMdataByte without Items. The Structur have to initalised with the right Number of Items before this Function is call.</param>
/// <param name="size">Size/Items from _sEEPROMdataByte[]</param>
///
void eeprom_i2c::read(sEEPROMdataByte _sEEPROMdataByte[], unsigned int size) {
for(byte i=0; i<size;i++){
this->read(_sEEPROMdataByte[i].blockAddress, _sEEPROMdataByte[i].data);
}
}
/// <summary>
// Read Data(Boolean) to the eeprom(Array Write)
/// </summary>
/// <param name="_sEEPROMdataBoolean">Structur of sEEPROMdataBoolean without Items. The Structur have to initalised with the right Number of Items before this Function is call.</param>
/// <param name="size">Size/Items from _sEEPROMdataBoolean[]</param>
///
void eeprom_i2c::read(sEEPROMdataBoolean _sEEPROMdataBoolean[], unsigned int size) {
for(byte i=0; i<size;i++){
this->read(_sEEPROMdataBoolean[i].blockAddress, _sEEPROMdataBoolean[i].data);
}
}
/// <summary>
// Read Data(unsigned Int) to the eeprom(Array Write)
/// </summary>
/// <param name="_sEEPROMdataUint">Structur of sEEPROMdataUint without Items. The Structur have to initalised with the right Number of Items before this Function is call.</param>
/// <param name="size">Size/Items from _sEEPROMdataUint[]</param>
///
void eeprom_i2c::read(sEEPROMdataUint _sEEPROMdataUint[], unsigned int size) {
for(byte i=0; i<size;i++){
this->read(_sEEPROMdataUint[i].blockAddress, _sEEPROMdataUint[i].data);
}
}