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
260 bool update()
261 {
262 uint16_t pressDuration = _debouncedInput.readDurationInTrueState_mS();
263 int increments = 0;
264 bool incremented = false;
265 bool pressed = false;
266 if (pressDuration > 0)
267 {
268 if (_lastPressDuration >= pressDuration)
269 {
270 _lastPressDuration = 0;
271 }
272
273 if (pressDuration > _mediumToFastTransistion_mS)
274 {
275 // Increment fast
276 increments = (pressDuration - _lastPressDuration) / _fast_mS_betweenIncrements;
277 *_variableToIncrement += _fastIncrement * increments;
278 _lastPressDuration += _fast_mS_betweenIncrements * increments;
279 }
280 else if (pressDuration > _slowToMediumTransition_mS)
281 {
282 // Increment medium
283 increments = (pressDuration - _lastPressDuration) / _medium_mS_betweenIncrements;
284 *_variableToIncrement += _mediumIncrement * increments;
285 _lastPressDuration += _medium_mS_betweenIncrements * increments;
286 }
287 else
288 {
289 //Increment slow
290 increments = (pressDuration - _lastPressDuration) / _slow_mS_betweenIncrements;
291 *_variableToIncrement += _slowIncrement * increments;
292 _lastPressDuration += _slow_mS_betweenIncrements * increments;
293 incremented = increments > 0; // An increment happened
294 }
295 if (incremented)
296 {
297 _debouncedInput.transitions = 0; // Get rid of false->true transition so that final release doesn't cause and increment
298 }
299 pressed = true;
300 }
301 else
302 {
303 // Button isn't currently pressed. if there were other transitions, add them
304 _lastPressDuration = 0;
305 int presses = _debouncedInput.transitions / 2;
306 *_variableToIncrement += _slowIncrement * presses;
307 _debouncedInput.transitions -= presses * 2;
308 }
309
310 if (*_variableToIncrement > highLimit)
311 {
312 *_variableToIncrement = highLimit;
313 }
314 if (*_variableToIncrement < lowLimit)
315 {
316 *_variableToIncrement = lowLimit;
317 }
318
319 return (pressed);
320 }
321
323 long highLimit = LONG_MAX;
325 long lowLimit = LONG_MIN;
326
327 private:
328 SerialWombatAbstractButton& _debouncedInput;
329 int32_t* _variableToIncrement;
330
331 int32_t _slowIncrement;
332 uint32_t _slow_mS_betweenIncrements;
333
334 uint16_t _slowToMediumTransition_mS;
335
336 int32_t _mediumIncrement;
337 uint32_t _medium_mS_betweenIncrements;
338
339 uint16_t _mediumToFastTransistion_mS;
340
341 int32_t _fastIncrement;
342 uint32_t _fast_mS_betweenIncrements;
343
344 uint32_t _lastPressDuration;
345
346};
347
SerialWombat.h
PIN_MODE_DEBOUNCE
@ PIN_MODE_DEBOUNCE
(10)
Definition SerialWombat.h:270
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:260
SerialWombatButtonCounter::highLimit
long highLimit
The variable will not increment above this limit.
Definition SerialWombatDebouncedInput.h:323
SerialWombatButtonCounter::lowLimit
long lowLimit
The variable will not decrement below this limit.
Definition SerialWombatDebouncedInput.h:325
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)
Initializes the SerialWombatButtonCounter.
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:313
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:214
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:130
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:213
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:120
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:215