-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEepromRingBuffer.cpp
146 lines (126 loc) · 4.04 KB
/
EepromRingBuffer.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
/**
EepromRingBuffer.cpp is part of EepromUtils.
Copyright (c) 2011 Lorenzo Flueckiger
EepromUtils is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EepromUtils 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with EepromUtils. If not, see <http://www.gnu.org/licenses/>.
*/
#include "EepromRingBuffer.h"
#include "SafeEeprom.h"
#ifdef SERIAL_DEBUG
#include "HardwareSerial.h"
#endif
#include <stdlib.h> // for exit
EepromRingBuffer::EepromRingBuffer(uint16_t startAddr,
uint16_t bufferSize,
size_t dataSize,
uint16_t indexEndurance)
: m_eepromIndex(startAddr, indexEndurance, sizeof(Indexes)),
m_bufferLength(bufferSize*dataSize),
m_dataSize(dataSize)
{
m_bufferStart = startAddr + m_eepromIndex.storageSize();
// Check if there is enough EEPROM
if ( startAddr + storageSize() > (E2END+1) ) {
exit(-1);
}
m_eepromIndex.readData((void *)&m_ramIndex);
if ( 0xFFFF == m_ramIndex.last ) {
// This buffer never existed before. Let's initialize it
clear();
}
}
void EepromRingBuffer::push(void *data)
{
#ifdef SERIAL_DEBUG
Serial.print("push: Current byte index = ");
Serial.print(m_ramIndex.last, DEC);
#endif
m_ramIndex.last = (m_ramIndex.last+m_dataSize) % m_bufferLength;
#ifdef SERIAL_DEBUG
Serial.print(" -> New byte index = ");
Serial.println(m_ramIndex.last, DEC);
#endif
SafeEeprom::write_block(m_bufferStart+m_ramIndex.last, data, m_dataSize);
m_eepromIndex.writeData((void *)&m_ramIndex);
}
void EepromRingBuffer::get(int index, void *data)
{
#ifdef SERIAL_DEBUG
Serial.print("-- get index=");
Serial.print(index, DEC);
#endif
// The if statment for positive and negative values of the index
// seems necessary because the module function did not generate
// the desired values for negative numbers!
if ( index < 0 ) {
index = (-m_dataSize*index) % m_bufferLength;
index = m_ramIndex.last + index;
if ( (uint16_t)index > m_bufferLength-1 ) index -= m_bufferLength;
}
else {
index = (m_dataSize*index) % m_bufferLength;
index = m_ramIndex.last - index;
if ( index < 0 ) index += m_bufferLength;
}
#ifdef SERIAL_DEBUG
Serial.print(" -> byte index=");
Serial.print(index, DEC);
Serial.print(" :: ");
#endif
SafeEeprom::read_block(m_bufferStart+index, data, m_dataSize);
}
void EepromRingBuffer::rotate(uint16_t steps)
{
#ifdef SERIAL_DEBUG
Serial.print("rotate (steps=");
Serial.print(steps, DEC);
Serial.print(") : Current byte index = ");
Serial.print(m_ramIndex.last, DEC);
#endif
if ( steps < bufferSize() ) {
m_ramIndex.last += m_dataSize;
for (uint16_t i=0; i<steps*m_dataSize; i++) {
m_ramIndex.last = m_ramIndex.last % m_bufferLength;
SafeEeprom::write_byte(m_bufferStart+m_ramIndex.last, 0xFF);
m_ramIndex.last++;
}
m_ramIndex.last -= m_dataSize;
m_ramIndex.last = m_ramIndex.last % m_bufferLength;
#ifdef SERIAL_DEBUG
Serial.print(" -> New byte index = ");
Serial.println(m_ramIndex.last, DEC);
#endif
m_eepromIndex.writeData((void *)&m_ramIndex);
}
else {
clear();
}
}
void EepromRingBuffer::clear()
{
for (uint16_t i=0; i<m_bufferLength; i++) {
SafeEeprom::write_byte(m_bufferStart+i, 0xFF);
}
m_ramIndex.last = 0;
m_eepromIndex.writeData((void *)&m_ramIndex);
}
uint16_t EepromRingBuffer::storageSize()
{
return m_eepromIndex.storageSize() + m_bufferLength;
}
uint16_t EepromRingBuffer::bufferSize()
{
return m_bufferLength / m_dataSize;
}
uint16_t EepromRingBuffer::currentIndex()
{
return m_ramIndex.last / m_dataSize;
}