Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatDebouncedInput.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2020-2024 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27#include "SerialWombat.h"
28#include "limits.h"
29
32
74 class SerialWombatDebouncedInput:public SerialWombatAbstractButton , public SerialWombatPin
75{
76 public:
82 SerialWombatDebouncedInput(SerialWombatChip& serialWombatChip):SerialWombatAbstractButton(),SerialWombatPin(serialWombatChip){}
83
84
93 int16_t begin(uint8_t pin, uint16_t debounce_mS = 30, bool invert = true, bool usePullUp = true)
94 {
95 _pin = pin;
96 _pinMode = PIN_MODE_DEBOUNCE;
97 return( initPacketNoResponse(0,debounce_mS,(uint8_t)invert,0,(uint8_t)usePullUp));
98 }
99
108 bool digitalRead()
109 {
110 return (_sw.readPublicData(_pin) > 0);
111 }
112
113 /*
114 \brief return the number of mS that the debounced input has been in true state
115
116 Note that this value starts incrementing after the debounce period, not after the physical pin transition.
117
118 \return returns a value in mS which saturates at 65535. Returns 0 if currently false.
119 */
120 uint16_t readDurationInTrueState_mS()
121 {
122
123 uint8_t tx[8] = { 201,_pin,_pinMode,1,0x55,0x55,0x55,0x55 };
124 uint8_t rx[8];
125 _sw.sendPacket(tx,rx);
126
127 transitions += (256 * rx[5] + rx[4]);
128 if (rx[3] == 0)
129 {
130 return (0);
131 }
132 else
133 {
134 return(256 * rx[7] + rx[6]);
135 }
136 }
137
138 /*
139 \brief return the number of mS that the debounced input has been in false state
140
141 Note that this value starts incrementing after the debounce period, not after the physical pin transition.
142
143 \return returns a value in mS which saturates at 65535. Returns 0 if currently true.
144 */
145 uint16_t readDurationInFalseState_mS()
146 {
147
148 uint8_t tx[8] = { 201,_pin,_pinMode,1,0x55,0x55,0x55,0x55 };
149 uint8_t rx[8];
150 _sw.sendPacket(tx, rx);
151
152 transitions += (256 * rx[5] + rx[4]);
153
154 if (rx[3] == 1)
155 {
156 return (0);
157 }
158 else
159 {
160 return(256 * rx[7] + rx[6]);
161 }
162 }
163
164 /*
165 \brief Queries the number of transistions that have occured on the debounced input
166
167 This function queries the debounced input for current state and transitions since last call.
168 transition count is put in the global member transitions. The debounced input in the Serial
169 Wombat resets its count to zero after this call.
170
171 \return TRUE or FALSE, current status of debounced input
172 */
173 bool readTransitionsState(bool resetTransitionCounts = true)
174 {
175 uint8_t tx[8] = { 201,_pin,_pinMode,(uint8_t)resetTransitionCounts,0x55,0x55,0x55,0x55 };
176 uint8_t rx[8];
177 _sw.sendPacket(tx, rx);
178 transitions = (256 * rx[5] + rx[4]);
179 return (rx[3] > 0);
180 }
181
182 private:
183};
184
185
205 class SerialWombatButtonCounter
206{
207 public:
208
213 SerialWombatButtonCounter( SerialWombatAbstractButton& serialWombatDebouncedInput):_debouncedInput(serialWombatDebouncedInput)
214 {
215 _debouncedInput = serialWombatDebouncedInput;
216 }
217
231 void begin(int32_t* variableToIncrement,
232 int32_t slowIncrement = 1, uint32_t slow_mS_betweenIncrements = 250,
233 uint16_t slowToMediumTransition_mS = 1000,
234 int32_t mediumIncrement = 1, uint32_t medium_mS_betweenIncrements = 100,
235 uint16_t mediumToFastTransition_mS = 1000 ,
236 int32_t fastIncrement = 1, uint32_t fast_mS_betweenIncrements = 50)
237 {
238 _variableToIncrement = variableToIncrement;
239
240 _slowIncrement = slowIncrement;
241 _slow_mS_betweenIncrements = slow_mS_betweenIncrements;
242
243 _slowToMediumTransition_mS = slowToMediumTransition_mS;
244
245 _mediumIncrement = mediumIncrement;
246 _medium_mS_betweenIncrements = medium_mS_betweenIncrements;
247
248 _mediumToFastTransistion_mS = mediumToFastTransition_mS;
249
250 _fastIncrement = fastIncrement;
251 _fast_mS_betweenIncrements = fast_mS_betweenIncrements;
252
253 _lastPressDuration = 0;
254
255 }
256
257 bool update()
258 {
259 uint16_t pressDuration = _debouncedInput.readDurationInTrueState_mS();
260 int increments = 0;
261 bool incremented = false;
262 bool pressed = false;
263 if (pressDuration > 0)
264 {
265 if (_lastPressDuration >= pressDuration)
266 {
267 _lastPressDuration = 0;
268 }
269
270 if (pressDuration > _mediumToFastTransistion_mS)
271 {
272 // Increment fast
273 increments = (pressDuration - _lastPressDuration) / _fast_mS_betweenIncrements;
274 *_variableToIncrement += _fastIncrement * increments;
275 _lastPressDuration += _fast_mS_betweenIncrements * increments;
276 }
277 else if (pressDuration > _slowToMediumTransition_mS)
278 {
279 // Increment medium
280 increments = (pressDuration - _lastPressDuration) / _medium_mS_betweenIncrements;
281 *_variableToIncrement += _mediumIncrement * increments;
282 _lastPressDuration += _medium_mS_betweenIncrements * increments;
283 }
284 else
285 {
286 //Increment slow
287 increments = (pressDuration - _lastPressDuration) / _slow_mS_betweenIncrements;
288 *_variableToIncrement += _slowIncrement * increments;
289 _lastPressDuration += _slow_mS_betweenIncrements * increments;
290 incremented = increments > 0; // An increment happened
291 }
292 if (incremented)
293 {
294 _debouncedInput.transitions = 0; // Get rid of false->true transition so that final release doesn't cause and increment
295 }
296 pressed = true;
297 }
298 else
299 {
300 // Button isn't currently pressed. if there were other transitions, add them
301 _lastPressDuration = 0;
302 int presses = _debouncedInput.transitions / 2;
303 *_variableToIncrement += _slowIncrement * presses;
304 _debouncedInput.transitions -= presses * 2;
305 }
306
307 if (*_variableToIncrement > highLimit)
308 {
309 *_variableToIncrement = highLimit;
310 }
311 if (*_variableToIncrement < lowLimit)
312 {
313 *_variableToIncrement = lowLimit;
314 }
315
316 return (pressed);
317 }
318
320 long highLimit = LONG_MAX;
322 long lowLimit = LONG_MIN;
323
324 private:
325 SerialWombatAbstractButton& _debouncedInput;
326 int32_t* _variableToIncrement;
327
328 int32_t _slowIncrement;
329 uint32_t _slow_mS_betweenIncrements;
330
331 uint16_t _slowToMediumTransition_mS;
332
333 int32_t _mediumIncrement;
334 uint32_t _medium_mS_betweenIncrements;
335
336 uint16_t _mediumToFastTransistion_mS;
337
338 int32_t _fastIncrement;
339 uint32_t _fast_mS_betweenIncrements;
340
341 uint32_t _lastPressDuration;
342
343};
344
SerialWombat.h
PIN_MODE_DEBOUNCE
@ PIN_MODE_DEBOUNCE
(10)
Definition SerialWombat.h:262
SerialWombatAbstractButton
SerialWombat18CapTouch, SerialWombatDebouncedInput and SerialWombatMatrixButton inherit from this cla...
Definition SerialWombatAbstractButton.h:41
SerialWombatAbstractButton::transitions
uint16_t transitions
Number of transitions returned by last call to readTransitionsState()
Definition SerialWombatAbstractButton.h:72
SerialWombatAbstractButton::SerialWombatAbstractButton
SerialWombatAbstractButton()
Definition SerialWombatAbstractButton.h:44
SerialWombatButtonCounter::SerialWombatButtonCounter
SerialWombatButtonCounter(SerialWombatAbstractButton &serialWombatDebouncedInput)
Constructor for SerialWombatButtonCounter.
Definition SerialWombatDebouncedInput.h:213
SerialWombatButtonCounter::update
bool update()
Called periodically to query the SerialWombatDebouncedInput and update the variable.
Definition SerialWombatDebouncedInput.h:257
SerialWombatButtonCounter::highLimit
long highLimit
The variable will not increment above this limit.
Definition SerialWombatDebouncedInput.h:320
SerialWombatButtonCounter::lowLimit
long lowLimit
The variable will not decrement below this limit.
Definition SerialWombatDebouncedInput.h:322
SerialWombatButtonCounter::begin
void begin(int32_t *variableToIncrement, int32_t slowIncrement=1, uint32_t slow_mS_betweenIncrements=250, uint16_t slowToMediumTransition_mS=1000, int32_t mediumIncrement=1, uint32_t medium_mS_betweenIncrements=100, uint16_t mediumToFastTransition_mS=1000, int32_t fastIncrement=1, uint32_t fast_mS_betweenIncrements=50)
Definition SerialWombatDebouncedInput.h:231
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:300
SerialWombatDebouncedInput::digitalRead
bool digitalRead()
Returns the debounced state of the input.
Definition SerialWombatDebouncedInput.h:108
SerialWombatDebouncedInput::readTransitionsState
bool readTransitionsState(bool resetTransitionCounts=true)
Queries the number of transistions that have occured on the debounced input.
Definition SerialWombatDebouncedInput.h:173
SerialWombatDebouncedInput::begin
int16_t begin(uint8_t pin, uint16_t debounce_mS=30, bool invert=true, bool usePullUp=true)
Initialize a debounced input.
Definition SerialWombatDebouncedInput.h:93
SerialWombatDebouncedInput::readDurationInTrueState_mS
uint16_t readDurationInTrueState_mS()
return the number of mS that the input has been in true state
Definition SerialWombatDebouncedInput.h:120
SerialWombatDebouncedInput::readDurationInFalseState_mS
uint16_t readDurationInFalseState_mS()
return the number of mS that the input has been in false state
Definition SerialWombatDebouncedInput.h:145
SerialWombatDebouncedInput::SerialWombatDebouncedInput
SerialWombatDebouncedInput(SerialWombatChip &serialWombatChip)
Constructor for the SerialWombatDebouncedInput class.
Definition SerialWombatDebouncedInput.h:82
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:206
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::initPacketNoResponse
int16_t initPacketNoResponse(uint8_t packetNumber, uint8_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55, uint8_t param4=0x55)
Definition SerialWombatPin.h:131
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:205
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:121
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:207