-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rotaryDecoder.cpp
224 lines (181 loc) · 3.77 KB
/
rotaryDecoder.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
//
// FILE: rotaryDecoder.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// DATE: 2021-05-08
// PURPOSE: Arduino library for a PCF8574 based rotary decoder
// URL: https://github.com/RobTillaart/rotaryDecoder
#include "rotaryDecoder.h"
/////////////////////////////////////////////////////
//
// CONSTRUCTORS
//
rotaryDecoder::rotaryDecoder(const int8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
}
bool rotaryDecoder::begin(uint8_t count)
{
_count = count;
if (_count > ROTDEC_MAX_COUNT) _count = ROTDEC_MAX_COUNT;
if (! isConnected()) return false;
return true;
}
bool rotaryDecoder::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t rotaryDecoder::getRECount()
{
return _count;
}
void rotaryDecoder::reset()
{
for (int i = 0 ; i < ROTDEC_MAX_COUNT; i++)
{
_lastPos[i] = 0;
_encoder[i] = 0;
}
_lastValue = 0;
}
uint8_t rotaryDecoder::readInitialState()
{
uint8_t value = read8();
_lastValue = value;
for (uint8_t i = 0; i < _count; i++)
{
_lastPos[i] = value & 0x03;
value >>= 2;
}
return _lastValue;
}
bool rotaryDecoder::checkChange()
{
uint8_t value = read8();
return (_lastValue != value);
}
bool rotaryDecoder::update()
{
uint8_t value = read8();
if (_lastValue == value)
{
return false;
}
_lastValue = value;
for (uint8_t i = 0; i < _count; i++, value >>= 2)
{
uint8_t currentPos = (value & 0x03);
uint8_t change = (_lastPos[i] << 2) | currentPos;
switch (change)
{
case 0b0001: // fall through..
case 0b0111:
case 0b1110:
case 0b1000:
_encoder[i]++;
break;
case 0b0010:
case 0b0100:
case 0b1101:
case 0b1011:
_encoder[i]--;
break;
}
_lastPos[i] = currentPos;
}
return true;
}
bool rotaryDecoder::updateSingle()
{
uint8_t value = read8();
if (_lastValue == value)
{
return false;
}
_lastValue = value;
for (uint8_t i = 0; i < _count; i++, value >>= 2)
{
uint8_t currentPos = (value & 0x03);
uint8_t change = (_lastPos[i] << 2) | currentPos;
switch (change)
{
case 0b0001: // fall through..
case 0b0111:
case 0b1110:
case 0b1000:
_encoder[i] += 1;
break;
case 0b0011:
case 0b0110:
case 0b1001:
case 0b1100:
_encoder[i] += 2;
break;
case 0b0010:
case 0b0100:
case 0b1101:
case 0b1011:
_encoder[i] += 3;
break;
}
_lastPos[i] = currentPos;
}
return true;
}
int32_t rotaryDecoder::getValue(uint8_t re)
{
if (re >= ROTDEC_MAX_COUNT) return 0;
return _encoder[re];
}
bool rotaryDecoder::setValue(uint8_t re, int32_t value)
{
if (re >= ROTDEC_MAX_COUNT) return false;
_encoder[re] = value;
return true;
}
/////////////////////////////////////////////////////
//
// READ - WRITE interface
//
uint8_t rotaryDecoder::read1(uint8_t pin)
{
uint8_t mask = 1 << pin;
uint8_t tmp = read8();
return (tmp & mask) > 0 ? HIGH : LOW;
}
bool rotaryDecoder::write1(uint8_t pin, uint8_t value)
{
uint8_t mask = 1 << pin;
uint8_t tmp = read8();
if (value == LOW) tmp &= ~mask;
else tmp |= mask;
return write8(tmp);
}
uint8_t rotaryDecoder::read8()
{
_wire->requestFrom(_address, (uint8_t)1);
uint8_t a = _wire->read();
return a;
}
bool rotaryDecoder::write8(uint8_t bitmask)
{
_wire->beginTransmission(_address);
_wire->write(bitmask);
return (_wire->endTransmission() == 0);
}
/////////////////////////////////////////////////////
//
// DEBUG
//
uint8_t rotaryDecoder::getLastPosition(uint8_t re)
{
if (re >= ROTDEC_MAX_COUNT) return 0;
return _lastPos[re];
}
/////////////////////////////////////////////////////
//
// PROTECTED
//
// -- END OF FILE --