forked from cinderblock/3-Phase-Controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripleBuffer.cpp
More file actions
126 lines (116 loc) · 3.12 KB
/
TripleBuffer.cpp
File metadata and controls
126 lines (116 loc) · 3.12 KB
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
/*
* File: TripleBuffer.cpp
* Author: Cameron
*
* Created on February 22, 2015, 5:58 PM
*/
#include <avr/interrupt.h>
#include "TripleBuffer.h"
template <u1 size, bool readInterrupt, bool writeInterrupt>
TripleBuffer<size, readInterrupt, writeInterrupt>::TripleBuffer() :state(State::A) {
}
template<u1 size, bool readInterrupt, bool writeInterrupt>
void TripleBuffer<size, readInterrupt, writeInterrupt>::reserveNewestBufferForReading() {
if (readInterrupt) cli();
switch (state) {
default: break;
case State::F: state = State::G; break;
case State::H: state = State::I; break;
case State::C:
case State::J: state = State::E; break;
case State::D:
case State::K: state = State::L; break;
case State::M: state = State::N; break;
case State::O: state = State::P; break;
}
if (readInterrupt) sei();
}
template<u1 size, bool readInterrupt, bool writeInterrupt>
void TripleBuffer<size, readInterrupt, writeInterrupt>::markNewestBuffer() {
if (writeInterrupt) cli();
switch (state) {
default: // WTF
case State::A: state = State::B; break;
case State::B:
case State::D: state = State::C; break;
case State::C: state = State::D; break;
case State::E:
case State::O: state = State::F; break;
case State::F:
case State::N: state = State::O; break;
case State::G:
case State::M: state = State::H; break;
case State::H:
case State::L: state = State::M; break;
case State::I:
case State::K: state = State::J; break;
case State::J:
case State::P: state = State::K; break;
}
if (writeInterrupt) sei();
}
template<u1 size, bool readInterrupt, bool writeInterrupt>
bool TripleBuffer<size, readInterrupt, writeInterrupt>::isNewData() {
// We don't need to lock anything here since we're only reading
// And because of the machine's states, even if the state changes before a read
// (and we just returned true), it will stay true until a read
switch (state) {
case State::C:
case State::D:
case State::F:
case State::H:
case State::J:
case State::K:
case State::M:
case State::O: return true;
// Prevents a warning
default: break;
};
return false;
}
template<u1 size, bool readInterrupt, bool writeInterrupt>
u1* TripleBuffer<size, readInterrupt, writeInterrupt>::getWriteBuffer() {
switch (state) {
case State::A: return nullptr;
case State::B:
case State::D:
case State::H:
case State::I:
case State::K:
case State::L: return rBuffer;
case State::C:
case State::E:
case State::J:
case State::O:
case State::P: return gBuffer;
case State::F:
case State::G:
case State::M:
case State::N: return bBuffer;
}
// WTF
return nullptr;
}
template<u1 size, bool readInterrupt, bool writeInterrupt>
u1* TripleBuffer<size, readInterrupt, writeInterrupt>::getReadBuffer() {
switch (state) {
case State::A:
case State::B:
case State::C:
case State::D: return nullptr;
case State::E:
case State::F:
case State::N:
case State::O: return rBuffer;
case State::G:
case State::H:
case State::L:
case State::M: return gBuffer;
case State::I:
case State::J:
case State::K:
case State::P: return bBuffer;
}
// WTF
return nullptr;
}