-
Notifications
You must be signed in to change notification settings - Fork 21
/
mcp4xxx.cpp
276 lines (230 loc) · 5.43 KB
/
mcp4xxx.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "mcp4xxx.h"
namespace icecave {
namespace arduino {
MCP4XXX::MCP4XXX(byte select_pin, Pot pot, Resolution resolution, WiperConfiguration config)
: m_select_pin(select_pin)
, m_pot(pot)
, m_max_value(resolution + config) // Potentiometer configurations allow "resolution + 1" values, for setting the "full-scale" wiper position.
, m_select_nesting(0)
{
SPI.begin();
pinMode(m_select_pin, OUTPUT);
digitalWrite(m_select_pin, HIGH);
}
word MCP4XXX::max_value(void) const
{
return m_max_value;
}
bool MCP4XXX::increment(void)
{
return transfer(static_cast<Address>(m_pot), command_increment);
}
bool MCP4XXX::decrement(void)
{
return transfer(static_cast<Address>(m_pot), command_decrement);
}
bool MCP4XXX::set(word value)
{
return transfer(static_cast<Address>(m_pot), command_write, min(value, m_max_value));
}
bool MCP4XXX::get(word& value) const
{
return transfer(static_cast<Address>(m_pot), command_read, data_mask_word, value);
}
word MCP4XXX::get(void) const
{
word result = 0xFFFF;
get(result);
return result;
}
bool MCP4XXX::set_terminal_a_status(bool connected)
{
return set_tcon(tcon_term_a_mask, connected);
}
bool MCP4XXX::get_terminal_a_status(bool& connected) const
{
return get_tcon(tcon_term_a_mask, connected);
}
bool MCP4XXX::get_terminal_a_status(void) const
{
bool connected = false;
return get_terminal_a_status(connected)
&& connected;
}
bool MCP4XXX::set_terminal_b_status(bool connected)
{
return set_tcon(tcon_term_b_mask, connected);
}
bool MCP4XXX::get_terminal_b_status(bool& connected) const
{
return get_tcon(tcon_term_b_mask, connected);
}
bool MCP4XXX::get_terminal_b_status(void) const
{
bool connected = false;
return get_terminal_b_status(connected)
&& connected;
}
bool MCP4XXX::set_wiper_status(bool connected)
{
return set_tcon(tcon_wiper_mask, connected);
}
bool MCP4XXX::get_wiper_status(bool& connected) const
{
return get_tcon(tcon_wiper_mask, connected);
}
bool MCP4XXX::get_wiper_status(void) const
{
bool connected = false;
return get_wiper_status(connected)
&& connected;
}
bool MCP4XXX::set_shutdown_status(bool shutdown)
{
return set_tcon(tcon_shutdown_mask, !shutdown);
}
bool MCP4XXX::get_shutdown_status(bool& shutdown) const
{
bool enabled;
if (get_tcon(tcon_shutdown_mask, enabled))
{
shutdown = !enabled;
return true;
}
return false;
}
bool MCP4XXX::get_shutdown_status(void) const
{
bool shutdown = false;
return get_shutdown_status(shutdown)
&& shutdown;
}
bool MCP4XXX::get_hardware_shutdown_status(bool& shutdown) const
{
word result;
if (transfer(address_status, command_read, data_mask_word, result))
{
shutdown = result & status_shutdown_mask;
return true;
}
return false;
}
bool MCP4XXX::get_hardware_shutdown_status(void) const
{
bool shutdown = false;
return get_hardware_shutdown_status(shutdown)
&& shutdown;
}
void MCP4XXX::select(void) const
{
if (++m_select_nesting == 1)
{
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV128);
digitalWrite(m_select_pin, LOW);
}
}
void MCP4XXX::deselect(void) const
{
if (--m_select_nesting == 0)
{
digitalWrite(m_select_pin, HIGH);
}
}
byte MCP4XXX::build_command(Address address, Command command) const
{
return ((address << 4) & address_mask)
| ((command << 2) & command_mask)
| cmderr_mask;
}
word MCP4XXX::build_command(Address address, Command command, word data) const
{
return (build_command(address, command) << 8)
| (data & data_mask_word);
}
bool MCP4XXX::transfer(Address address, Command command) const
{
select();
byte result = SPI.transfer(build_command(address, command));
deselect();
return result & cmderr_mask;
}
bool MCP4XXX::transfer(Address address, Command command, word data) const
{
select();
word cmd = build_command(address, command, data);
bool valid = SPI.transfer(highByte(cmd)) & cmderr_mask;
if (valid)
{
SPI.transfer(lowByte(cmd));
}
deselect();
return valid;
}
bool MCP4XXX::transfer(Address address, Command command, word data, word& result) const
{
select();
word cmd = build_command(address, command, data);
byte high_byte = SPI.transfer(highByte(cmd));
bool valid = high_byte & cmderr_mask;
if (valid)
{
result = ((high_byte & data_mask) << 8)
| SPI.transfer(lowByte(cmd));
}
deselect();
return valid;
}
bool MCP4XXX::set_tcon(byte value)
{
// Note that the 9th (reserved) bit is always set to 1.
return transfer(address_tcon, command_write, 0x100 | value);
}
bool MCP4XXX::set_tcon(byte mask, bool value)
{
select();
// The values for pot #1 are 4 bits higher in the TCON register.
if (m_pot == pot_1)
mask <<= 4;
byte tcon;
bool success = get_tcon(tcon);
if (success)
{
if (value)
{
success = set_tcon(tcon | mask);
}
else
{
success = set_tcon(tcon & ~mask);
}
}
deselect();
return success;
}
bool MCP4XXX::get_tcon(byte& value) const
{
word result;
if (transfer(address_tcon, command_read, data_mask_word, result))
{
value = lowByte(result);
return true;
}
return false;
}
bool MCP4XXX::get_tcon(byte mask, bool& value) const
{
byte tcon;
if (get_tcon(tcon))
{
// The values for pot #1 are 4 bits higher in the TCON register.
if (m_pot == pot_1)
mask <<= 4;
value = tcon & mask;
return true;
}
return false;
}
} // end namespace arduino
} // end namespace icecave