forked from jhughes1010/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.ino
149 lines (133 loc) · 4.05 KB
/
eeprom.ino
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
#ifdef USE_EEPROM
#define EEPROM_I2C_ADDRESS 0x50
//========================================================================
// readEEPROM: Read historicalData from NVM
//========================================================================
void readEEPROM(struct rainfallData *rainfall)
{
int structSize;
byte buffer[200];
byte c;
bool nonZero = false;
int address = 0x0000;
structSize = sizeof(rainfallData);
memset(buffer, 0, sizeof(structSize));
//Send dummy write to set addpress register of NVM
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(address >> 8));
Wire.write((int)(address & 0xFF));
Wire.endTransmission();
//Read rainfall data
for (int location = 0; location < structSize; location++)
{
Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
delay(1);
while (Wire.available())
{
c = Wire.read();
}
if (c)
{
nonZero = true;
}
buffer[location] = c;
}
//If EEPROM contains non-zero values, restore data to historical structure
if (nonZero)
{
MonPrintf("\nRestoring rainfall structure from EEPROM\n");
memcpy(rainfall, buffer, structSize);
}
}
//========================================================================
// writeEEPROM: Conditional write of historicalData from NVM
// if rainfall has non-zero values, or needs updated to zero
// boot must be >1 in order for a write to take place, array will never
// be written on boot = 1
//========================================================================
void writeEEPROM(struct rainfallData *rainfall)
{
byte buffer[200];
int structSize;
int writePosition = 0;
int pageSize = 32;
int pageAddr = 0;
int page = 0;
structSize = sizeof(rainfallData);
memcpy(buffer, rainfall, structSize);
for (page = 0; (page * pageSize) < structSize; page++)
{
pageAddr = page * pageSize;
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(pageAddr >> 8));
Wire.write((int)(pageAddr & 0xFF));
MonPrintf("\npageAddr: %04x\n", pageAddr);
for (writePosition = pageAddr; writePosition < (pageAddr + pageSize); writePosition++)
{
if (writePosition < structSize)
{
//MonPrintf("position: % 02x: % 02x\n", writePosition, buffer[writePosition]);
Wire.write(buffer[writePosition]);
}
}
Wire.endTransmission();
delay(10);
MonPrintf("page write\n");
}
}
//========================================================================
// initEEPROM: Clear NVM to 0x00 where structure is stored
//========================================================================
void initEEPROM(void)
{
int structSize;
int x;
structSize = sizeof(rainfallData);
MonPrintf("sizeof int: %i\n", sizeof(int));
for (x = 0; x < structSize; x++)
{
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(x >> 8));
Wire.write((int)(x & 0xFF));
Wire.write(0x00);
Wire.endTransmission();
delay(10);
}
}
//========================================================================
// conditionalWriteEEPROM: Only write EEPROM if something has changed
//========================================================================
void conditionalWriteEEPROM(struct rainfallData *rainfall)
{
struct rainfallData historyBuffer;
bool match = true;
int structSize;
int x;
structSize = sizeof(rainfallData);
readEEPROM(&historyBuffer);
//compare EEPROM to RTC structure
//look at hourly totals for rainfall mismatch
for (int hour = 0; hour < 24; hour++)
{
if (historyBuffer.hourlyRainfall[hour] != rainfall->hourlyRainfall[hour])
{
//MonPrintf("Hourly rainfall: %i\n", rainfall->hourlyRainfall[hour]);
match = false;
}
}
//look at minute totals for rainfall mismatch
for (int hour = 0; hour < 12; hour++)
{
if (historyBuffer.current60MinRainfall[hour] != rainfall->current60MinRainfall[hour])
{
//MonPrintf("This hour rainfall: %i\n", rainfall->current60MinRainfall[hour]);
match = false;
}
}
//if mismatch exists, write EEPROM
if (!match)
{
writeEEPROM(rainfall);
}
}
#endif