-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFishduinoMgr.h
More file actions
338 lines (283 loc) · 9.44 KB
/
FishduinoMgr.h
File metadata and controls
338 lines (283 loc) · 9.44 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/****************************************************************************
Copyright (c) 2015, Jac Goudsmit
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
/*
This module is an extension of the FishDuino module. It keeps track of the
inputs and outputs so that the main program doesn't have to do that. For
example, if one function of the main program controls a motor and another
function needs the input from one of the digital pins, neither of those
functions have to keep track of the entire interface state to do their
work.
The compromise is that whenever a function reads or writes an input or
output pin, there is a delay in getting the actual digital signals in and
out. The effects of this problem can be minimized by calling the update
function often enough, e.g. from your loop() function or even from a timer
interrupt. When using a timer interrupt, it should be possible to update
the interface so fast that PWM by bit-banging can be implemented.
*/
#ifndef _FISHDUINOMGR_H_
#define _FISHDUINOMGR_H_
/////////////////////////////////////////////////////////////////////////////
// INCLUDES
/////////////////////////////////////////////////////////////////////////////
#include "FishDuino.h"
/////////////////////////////////////////////////////////////////////////////
// FISHDUINO MANAGER
/////////////////////////////////////////////////////////////////////////////
class FishduinoMgr : public Fishduino
{
#ifndef NDEBUG
public:
#else
protected:
#endif
volatile byte m_outputs[MaxInterfaces]; // Digital outputs
volatile byte m_inputs[MaxInterfaces]; // Digital inputs
byte m_previnputs[MaxInterfaces];// Input cache
public:
//-------------------------------------------------------------------------
// Constructor
FishduinoMgr(
byte pin_datacountin,
byte pin_triggerx,
byte pin_triggery,
byte pin_dataout,
byte pin_clock,
byte pin_loadout,
byte pin_loadin,
byte num_interfaces = 1)
: Fishduino(
pin_datacountin,
pin_triggerx,
pin_triggery,
pin_dataout,
pin_clock,
pin_loadout,
pin_loadin,
num_interfaces)
{
Reset();
Update();
}
public:
//-------------------------------------------------------------------------
// Simpler constructor for when connected to consecutive Arduino pins
FishduinoMgr(
byte startpin = 2,
byte num_interfaces = 1)
: Fishduino(
startpin,
num_interfaces)
{
Reset();
Update();
}
public:
//-------------------------------------------------------------------------
// Reset the outputs
void
Reset()
{
memset((void*)m_outputs, 0, sizeof(m_outputs));
}
public:
//-------------------------------------------------------------------------
// Update the outputs from the internal data
void
UpdateOutputs()
{
SetOutputs((const byte *)m_outputs);
}
public:
//-------------------------------------------------------------------------
// Update the internal data from the inputs
//
// If the debounce count is given, the function keeps reading the inputs
// until it gets the same readings
void
UpdateInputs(
unsigned debouncecount = 0, // Need this many identical readings
unsigned delayus = 0) // Delay between readings (useconds)
{
unsigned counter = 0;
byte curinputs[MaxInterfaces];
bool init = false;
memcpy(m_previnputs, (const void *)m_inputs, sizeof(m_previnputs));
for (;;)
{
GetInputs((byte *)m_inputs);
if (counter >= debouncecount)
{
break;
}
if ((init) && (!memcmp(curinputs, (const void *)m_inputs, sizeof(curinputs))))
{
++counter;
}
else
{
memcpy(curinputs, (const void *)m_inputs, sizeof(curinputs));
counter = 0;
init = true;
}
if (delayus)
{
delayMicroseconds(delayus);
}
}
}
public:
//-------------------------------------------------------------------------
// Update the outputs and inputs
void
Update()
{
UpdateOutputs();
UpdateInputs();
}
public:
//-------------------------------------------------------------------------
// Set an output bit
bool // Returns new state of the output
SetOutputPin(
byte intindex, // Interface index, 0=first
byte pin, // Output pin number, 0=first(!)
bool value = true) // True=on, false=off
{
if ((intindex < MaxInterfaces) && (pin < 7))
{
if (value)
{
m_outputs[intindex] |= (1 << pin);
}
else
{
m_outputs[intindex] &= ~(1 << pin);
}
}
return value;
}
public:
//-------------------------------------------------------------------------
// Set multiple output bits
void
SetOutputMask(
byte intindex, // Interface index, 0=first
byte setmask, // Bits to set
byte resetmask) // Bits to reset
{
m_outputs[intindex] = (m_outputs[intindex] | setmask) & (~resetmask);
}
public:
//-------------------------------------------------------------------------
// Get an input bit
bool // Returns state of the given input
GetInputPin(
byte intindex, // Interface index, 0=first
byte pin) // Input pin, 0=first(!)
{
bool result = false;
if ((intindex < MaxInterfaces) && (pin < 7))
{
result = (0 != (m_inputs[intindex] & (1 << pin)));
}
return result;
}
public:
//-------------------------------------------------------------------------
// Get previous value of input bit (from before the most recent update)
bool // Returns state of given input
GetPrevInputPin(
byte intindex, // Interface index, 0=first
byte pin) // Input pin, 0=first(!)
{
bool result = false;
if ((intindex < MaxInterfaces) && (pin < 7))
{
result = (0 != (m_previnputs[intindex] & (1 << pin)));
}
return result;
}
public:
//-------------------------------------------------------------------------
// Get multiple input bits
byte // Returns state of entire interface
GetInputMask(
byte intindex) // Interface index, 0=first
{
byte result = 0;
if (intindex < MaxInterfaces)
{
result = m_inputs[intindex];
}
return result;
}
public:
//-------------------------------------------------------------------------
// Get multiple previous input bits
byte // Returns state of entire interface
GetPrevInputMask(
byte intindex) // Interface index, 0=first
{
byte result = 0;
if (intindex < MaxInterfaces)
{
result = m_previnputs[intindex];
}
return result;
}
public:
//-------------------------------------------------------------------------
// Get an output bit
bool // Returns state of the given output
GetOutputPin(
byte intindex, // Interface index, 0=first
byte pin) // Output pin, 0=first(!)
{
bool result = false;
if ((intindex < MaxInterfaces) && (pin < 7))
{
result = (0 != (m_outputs[intindex] & (1 << pin)));
}
return result;
}
public:
//-------------------------------------------------------------------------
// Get all outputs of an interface
byte // Returns state as bit pattern
GetOutputMask(
byte intindex) // Interface index, 0=first
{
byte result = 0;
if (intindex < MaxInterfaces)
{
result = m_outputs[intindex];
}
return result;
}
};
/////////////////////////////////////////////////////////////////////////////
// END
/////////////////////////////////////////////////////////////////////////////
#endif