Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatDebouncedInput.h
Go to the documentation of this file.
1#pragma once
2/*
3Copyright 2020-2026 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6the United States. See SerialWombat.com for usage guidance.
7
8Permission 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
15The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18THE 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
80{
81public:
88
89
98 int16_t begin(uint8_t pin, uint16_t debounce_mS = 30, bool invert = true, bool usePullUp = true)
99 {
100 _pin = pin;
102 return( initPacketNoResponse(0,debounce_mS,(uint8_t)invert,0,(uint8_t)usePullUp));
103 }
104
114 {
115 return (_sw.readPublicData(_pin) > 0);
116 }
117
118 /*
119 @brief return the number of mS that the debounced input has been in true state
120
121 Note that this value starts incrementing after the debounce period, not after the physical pin transition.
122
123 @return returns a value in mS which saturates at 65535. Returns 0 if currently false.
124 */
126 {
127
128 uint8_t tx[8] = { 201,_pin,_pinMode,1,0x55,0x55,0x55,0x55 };
129 uint8_t rx[8];
130 _sw.sendPacket(tx,rx);
131
132 transitions += (256 * rx[5] + rx[4]);
133 if (rx[3] == 0)
134 {
135 return (0);
136 }
137 else
138 {
139 return(256 * rx[7] + rx[6]);
140 }
141 }
142
143 /*
144 @brief return the number of mS that the debounced input has been in false state
145
146 Note that this value starts incrementing after the debounce period, not after the physical pin transition.
147
148 @return returns a value in mS which saturates at 65535. Returns 0 if currently true.
149 */
151 {
152
153 uint8_t tx[8] = { 201,_pin,_pinMode,1,0x55,0x55,0x55,0x55 };
154 uint8_t rx[8];
155 _sw.sendPacket(tx, rx);
156
157 transitions += (256 * rx[5] + rx[4]);
158
159 if (rx[3] == 1)
160 {
161 return (0);
162 }
163 else
164 {
165 return(256 * rx[7] + rx[6]);
166 }
167 }
168
169 /*
170 @brief Queries the number of transistions that have occured on the debounced input
171
172 This function queries the debounced input for current state and transitions since last call.
173 transition count is put in the global member transitions. The debounced input in the Serial
174 Wombat resets its count to zero after this call.
175
176 @return TRUE or FALSE, current status of debounced input
177 */
178 bool readTransitionsState(bool resetTransitionCounts = true)
179 {
180 uint8_t tx[8] = { 201,_pin,_pinMode,(uint8_t)resetTransitionCounts,0x55,0x55,0x55,0x55 };
181 uint8_t rx[8];
182 _sw.sendPacket(tx, rx);
183 transitions = (256 * rx[5] + rx[4]);
184 return (rx[3] > 0);
185 }
186
202 int16_t setDoubleClickPeriod(uint16_t doubleClickPeriod_mS)
203 {
204 return(initPacketNoResponse(2, doubleClickPeriod_mS));
205 }
206
224 int16_t setOutputPin(uint8_t outputPin, uint16_t transitionOutputValue, uint16_t doubleClickOutputValue)
225 {
226 uint8_t tx[8] = { 203,_pin,_pinMode,outputPin,SW_LE16(transitionOutputValue),SW_LE16(doubleClickOutputValue) };
227 return(_sw.sendPacket(tx));
228 }
229
239 uint16_t readDoubleClickCount(bool resetDoubleClickCount = true)
240 {
241 uint8_t tx[8] = { 204,_pin,_pinMode,(uint8_t)resetDoubleClickCount,0x55,0x55,0x55,0x55 };
242 uint8_t rx[8];
243 _sw.sendPacket(tx, rx);
244 return(256 * rx[4] + rx[3]);
245 }
246
262 int16_t setToggleSimulation(bool enabled, uint16_t transitionCount = 0)
263 {
264 return(initPacketNoResponse(5, (uint8_t)enabled, (uint8_t)(transitionCount & 0xFF), (uint8_t)(transitionCount >> 8)));
265 }
266
267private:
268};
269
270
291{
292public:
293
298 SerialWombatButtonCounter( SerialWombatAbstractButton& serialWombatDebouncedInput):_debouncedInput(serialWombatDebouncedInput)
299 {
300 _debouncedInput = serialWombatDebouncedInput;
301 }
302
316 void begin(int32_t* variableToIncrement,
317 int32_t slowIncrement = 1, uint32_t slow_mS_betweenIncrements = 250,
318 uint16_t slowToMediumTransition_mS = 1000,
319 int32_t mediumIncrement = 1, uint32_t medium_mS_betweenIncrements = 100,
320 uint16_t mediumToFastTransition_mS = 1000 ,
321 int32_t fastIncrement = 1, uint32_t fast_mS_betweenIncrements = 50)
322 {
323 _variableToIncrement = variableToIncrement;
324
325 _slowIncrement = slowIncrement;
326 _slow_mS_betweenIncrements = slow_mS_betweenIncrements;
327
328 _slowToMediumTransition_mS = slowToMediumTransition_mS;
329
330 _mediumIncrement = mediumIncrement;
331 _medium_mS_betweenIncrements = medium_mS_betweenIncrements;
332
333 _mediumToFastTransistion_mS = mediumToFastTransition_mS;
334
335 _fastIncrement = fastIncrement;
336 _fast_mS_betweenIncrements = fast_mS_betweenIncrements;
337
338 _lastPressDuration = 0;
339
340 }
341
345 bool update()
346 {
347 uint16_t pressDuration = _debouncedInput.readDurationInTrueState_mS();
348 int increments = 0;
349 bool incremented = false;
350 bool pressed = false;
351 if (pressDuration > 0)
352 {
353 if (_lastPressDuration >= pressDuration)
354 {
355 _lastPressDuration = 0;
356 }
357
358 if (pressDuration > _mediumToFastTransistion_mS)
359 {
360 // Increment fast
361 increments = (pressDuration - _lastPressDuration) / _fast_mS_betweenIncrements;
362 *_variableToIncrement += _fastIncrement * increments;
363 _lastPressDuration += _fast_mS_betweenIncrements * increments;
364 }
365 else if (pressDuration > _slowToMediumTransition_mS)
366 {
367 // Increment medium
368 increments = (pressDuration - _lastPressDuration) / _medium_mS_betweenIncrements;
369 *_variableToIncrement += _mediumIncrement * increments;
370 _lastPressDuration += _medium_mS_betweenIncrements * increments;
371 }
372 else
373 {
374 //Increment slow
375 increments = (pressDuration - _lastPressDuration) / _slow_mS_betweenIncrements;
376 *_variableToIncrement += _slowIncrement * increments;
377 _lastPressDuration += _slow_mS_betweenIncrements * increments;
378 incremented = increments > 0; // An increment happened
379 }
380 if (incremented)
381 {
382 _debouncedInput.transitions = 0; // Get rid of false->true transition so that final release doesn't cause and increment
383 }
384 pressed = true;
385 }
386 else
387 {
388 // Button isn't currently pressed. if there were other transitions, add them
389 _lastPressDuration = 0;
390 int presses = _debouncedInput.transitions / 2;
391 *_variableToIncrement += _slowIncrement * presses;
392 _debouncedInput.transitions -= presses * 2;
393 }
394
395 if (*_variableToIncrement > highLimit)
396 {
397 *_variableToIncrement = highLimit;
398 }
399 if (*_variableToIncrement < lowLimit)
400 {
401 *_variableToIncrement = lowLimit;
402 }
403
404 return (pressed);
405 }
406
408 long highLimit = LONG_MAX;
410 long lowLimit = LONG_MIN;
411
412private:
413 SerialWombatAbstractButton& _debouncedInput;
414 int32_t* _variableToIncrement;
415
416 int32_t _slowIncrement;
417 uint32_t _slow_mS_betweenIncrements;
418
419 uint16_t _slowToMediumTransition_mS;
420
421 int32_t _mediumIncrement;
422 uint32_t _medium_mS_betweenIncrements;
423
424 uint16_t _mediumToFastTransistion_mS;
425
426 int32_t _fastIncrement;
427 uint32_t _fast_mS_betweenIncrements;
428
429 uint32_t _lastPressDuration;
430
431};
432
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
@ PIN_MODE_DEBOUNCE
(10)
SerialWombat18CapTouch, SerialWombatDebouncedInput and SerialWombatMatrixButton inherit from this cla...
uint16_t transitions
Number of transitions returned by last call to readTransitionsState()
SerialWombatButtonCounter(SerialWombatAbstractButton &serialWombatDebouncedInput)
Constructor for SerialWombatButtonCounter.
bool update()
Called periodically to query the SerialWombatDebouncedInput and update the variable.
long highLimit
The variable will not increment above this limit.
long lowLimit
The variable will not decrement below this limit.
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.
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
int16_t setDoubleClickPeriod(uint16_t doubleClickPeriod_mS)
Configure the maximum period used to recognize a double click.
bool digitalRead()
Returns the debounced state of the input.
bool readTransitionsState(bool resetTransitionCounts=true)
Queries the number of transistions that have occured on the debounced input.
int16_t setToggleSimulation(bool enabled, uint16_t transitionCount=0)
Enable or disable toggle-switch simulation in the pin's public data.
int16_t begin(uint8_t pin, uint16_t debounce_mS=30, bool invert=true, bool usePullUp=true)
Initialize a debounced input.
uint16_t readDurationInTrueState_mS()
return the number of mS that the input has been in true state
uint16_t readDoubleClickCount(bool resetDoubleClickCount=true)
Read the number of double clicks detected by the firmware.
uint16_t readDurationInFalseState_mS()
return the number of mS that the input has been in false state
int16_t setOutputPin(uint8_t outputPin, uint16_t transitionOutputValue, uint16_t doubleClickOutputValue)
Configure another Serial Wombat pin to receive values on button events.
SerialWombatDebouncedInput(SerialWombatChip &serialWombatChip)
Constructor for the SerialWombatDebouncedInput class.
SerialWombatChip & _sw
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
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)
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.