Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombat18CapTouch.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2021-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 <stdint.h>
28#include "SerialWombat.h"
29#include "limits.h"
30
85 class SerialWombat18CapTouch :public SerialWombatAbstractButton, public SerialWombatPin, public SerialWombatAbstractProcessedInput
86{
87 public:
88
90 SerialWombat18CapTouch(SerialWombatChip& serialWombat):SerialWombatAbstractButton(),SerialWombatPin(serialWombat) , SerialWombatAbstractProcessedInput(serialWombat) { _sw = serialWombat; }
91
92
104 int16_t begin(uint8_t pin, uint16_t chargeTime, uint16_t delay = 10)
105 {
106 _pin = pin;
107 _pinMode = PIN_MODE_SW18AB_CAPTOUCH;
108
109 uint8_t tx[8] = { 200,_pin, 22, SW_LE16(chargeTime), SW_LE16(delay),0x55 };
110
111 return _sw.sendPacket(tx);
112 }
113
124 int16_t makeDigital(uint16_t touchLimit, uint16_t noTouchLimit)
125 {
126 return makeDigital(touchLimit, noTouchLimit, 1, 0, 0, 0);
127 }
128
143 int16_t makeDigital(uint16_t touchLimit, uint16_t noTouchLimit, uint16_t touchValue, uint16_t noTouchValue, bool invert,uint16_t debounceCount)
144 {
145 uint8_t tx1[8] = { 201,_pin,22,SW_LE16(touchLimit), SW_LE16(noTouchLimit),0x55 };
146 int16_t result = _sw.sendPacket(tx1);
147 if (result < 0)
148 {
149 return (result);
150 }
151
152 _trueOutput = touchValue;
153 _falseOutput = noTouchValue;
154 uint8_t tx2[8] = { 202,_pin,22,SW_LE16(touchValue), SW_LE16(noTouchValue),0x55 };
155 result = _sw.sendPacket(tx2);
156 if (result < 0)
157 {
158 return (result);
159 }
160 uint8_t tx3[8] = { 203,_pin,22,1, invert? (uint8_t)1: (uint8_t)0,SW_LE16(debounceCount),0x55 };
161 result = _sw.sendPacket(tx3);
162 if (result < 0)
163 {
164 return (result);
165 }
166 return(0);
167 }
168
169 int16_t makeAnalog()
170 {
171 uint8_t tx3[8] = { 203,_pin,22,0, 0x55,0x55,0x55,0x55 };
172 return _sw.sendPacket(tx3);
173 }
174
183 bool digitalRead()
184 {
185 uint8_t tx[8] = { 204,_pin,22,0,0x55,0x55,0x55,0x55 };
186 uint8_t rx[8];
187 if (_sw.sendPacket(tx, rx) >= 0)
188 {
189 return(rx[3] > 0);
190 }
191 return(false);
192 }
193
201 uint16_t readDurationInTrueState_mS()
202 {
203 uint8_t tx[8] = { 204,_pin,22,1,0x55,0x55,0x55,0x55 };
204 uint8_t rx[8];
205 _sw.sendPacket(tx, rx);
206
207 transitions += (256 * rx[5] + rx[4]);
208 if (rx[3] == 0)
209 {
210 return (0);
211 }
212 else
213 {
214 return(256 * rx[7] + rx[6]);
215 }
216 }
217
225 uint16_t readDurationInFalseState_mS()
226 {
227 uint8_t tx[8] = { 204,_pin,22,1,0x55,0x55,0x55,0x55 };
228 uint8_t rx[8];
229 _sw.sendPacket(tx, rx);
230
231 transitions += (256 * rx[5] + rx[4]);
232
233 if (rx[3] == 1)
234 {
235 return (0);
236 }
237 else
238 {
239 return(256 * rx[7] + rx[6]);
240 }
241 }
242
243
245 uint16_t transitions = 0;
246
256 bool readTransitionsState(bool resetTransitionCounts = true)
257 {
258 uint8_t tx[8] = { 204,_pin,22,1,(uint8_t)resetTransitionCounts,0x55,0x55,0x55 };
259 uint8_t rx[8];
260 _sw.sendPacket(tx, rx);
261 transitions = (256 * rx[5] + rx[4]);
262 return (rx[3] > 0);
263 }
264
265 uint8_t pin() { return _pin; }
266 uint8_t swPinModeNumber() { return _pinMode; }
267
268 private:
269
270 uint16_t _trueOutput = 1;
271 uint16_t _falseOutput = 1;
272};
SerialWombat.h
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition SerialWombat.h:46
PIN_MODE_SW18AB_CAPTOUCH
@ PIN_MODE_SW18AB_CAPTOUCH
(22)
Definition SerialWombat.h:280
SerialWombat18CapTouch::pin
uint8_t pin()
Used for inheritance.
Definition SerialWombat18CapTouch.h:265
SerialWombat18CapTouch::swPinModeNumber
uint8_t swPinModeNumber()
Used for ineheritance.
Definition SerialWombat18CapTouch.h:266
SerialWombat18CapTouch::digitalRead
bool digitalRead()
Returns the debounced state of the input.
Definition SerialWombat18CapTouch.h:183
SerialWombat18CapTouch::readDurationInTrueState_mS
uint16_t readDurationInTrueState_mS()
return the number of mS that the debounced input has been in true state
Definition SerialWombat18CapTouch.h:201
SerialWombat18CapTouch::makeDigital
int16_t makeDigital(uint16_t touchLimit, uint16_t noTouchLimit, uint16_t touchValue, uint16_t noTouchValue, bool invert, uint16_t debounceCount)
Make a cap touch behave like a digital button.
Definition SerialWombat18CapTouch.h:143
SerialWombat18CapTouch::makeDigital
int16_t makeDigital(uint16_t touchLimit, uint16_t noTouchLimit)
Make a cap touch behave like a digital button.
Definition SerialWombat18CapTouch.h:124
SerialWombat18CapTouch::readDurationInFalseState_mS
uint16_t readDurationInFalseState_mS()
return the number of mS that the debounced input has been in false state
Definition SerialWombat18CapTouch.h:225
SerialWombat18CapTouch::SerialWombat18CapTouch
SerialWombat18CapTouch(SerialWombatChip &serialWombat)
Instantiate a SerialWombat18CapTouch class on a specified Serial Wombat Chip.
Definition SerialWombat18CapTouch.h:90
SerialWombat18CapTouch::makeAnalog
int16_t makeAnalog()
Definition SerialWombat18CapTouch.h:169
SerialWombat18CapTouch::readTransitionsState
bool readTransitionsState(bool resetTransitionCounts=true)
Queries the number of transistions that have occured on the debounced input.
Definition SerialWombat18CapTouch.h:256
SerialWombat18CapTouch::begin
int16_t begin(uint8_t pin, uint16_t chargeTime, uint16_t delay=10)
Initialize the SerialWombat18CapTouch instance with a given charge Time in uS.
Definition SerialWombat18CapTouch.h:104
SerialWombat18CapTouch::transitions
uint16_t transitions
Number of transitions returned by last call to readTransitionsState()
Definition SerialWombat18CapTouch.h:245
SerialWombatAbstractButton::SerialWombatAbstractButton
SerialWombatAbstractButton()
Definition SerialWombatAbstractButton.h:44
SerialWombatAbstractProcessedInput::SerialWombatAbstractProcessedInput
SerialWombatAbstractProcessedInput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition SerialWombatAbstractProcessedInput.h:87
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:313
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:214
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:213
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:215