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
141 int16_t makeDigital(uint16_t touchLimit, uint16_t noTouchLimit, uint16_t touchValue, uint16_t noTouchValue, bool invert,uint16_t debounceCount)
142 {
143 uint8_t tx1[8] = { 201,_pin,22,SW_LE16(touchLimit), SW_LE16(noTouchLimit),0x55 };
144 int16_t result = _sw.sendPacket(tx1);
145 if (result < 0)
146 {
147 return (result);
148 }
149
150 _trueOutput = touchValue;
151 _falseOutput = noTouchValue;
152 uint8_t tx2[8] = { 202,_pin,22,SW_LE16(touchValue), SW_LE16(noTouchValue),0x55 };
153 result = _sw.sendPacket(tx2);
154 if (result < 0)
155 {
156 return (result);
157 }
158 uint8_t tx3[8] = { 203,_pin,22,1, invert? (uint8_t)1: (uint8_t)0,SW_LE16(debounceCount),0x55 };
159 result = _sw.sendPacket(tx3);
160 if (result < 0)
161 {
162 return (result);
163 }
164 return(0);
165 }
166
167 int16_t makeAnalog()
168 {
169 uint8_t tx3[8] = { 203,_pin,22,0, 0x55,0x55,0x55,0x55 };
170 return _sw.sendPacket(tx3);
171 }
172
181 bool digitalRead()
182 {
183 uint8_t tx[8] = { 204,_pin,22,0,0x55,0x55,0x55,0x55 };
184 uint8_t rx[8];
185 if (_sw.sendPacket(tx, rx) >= 0)
186 {
187 return(rx[3] > 0);
188 }
189 return(false);
190 }
191
199 uint16_t readDurationInTrueState_mS()
200 {
201 uint8_t tx[8] = { 204,_pin,22,1,0x55,0x55,0x55,0x55 };
202 uint8_t rx[8];
203 _sw.sendPacket(tx, rx);
204
205 transitions += (256 * rx[5] + rx[4]);
206 if (rx[3] == 0)
207 {
208 return (0);
209 }
210 else
211 {
212 return(256 * rx[7] + rx[6]);
213 }
214 }
215
223 uint16_t readDurationInFalseState_mS()
224 {
225 uint8_t tx[8] = { 204,_pin,22,1,0x55,0x55,0x55,0x55 };
226 uint8_t rx[8];
227 _sw.sendPacket(tx, rx);
228
229 transitions += (256 * rx[5] + rx[4]);
230
231 if (rx[3] == 1)
232 {
233 return (0);
234 }
235 else
236 {
237 return(256 * rx[7] + rx[6]);
238 }
239 }
240
241
243 uint16_t transitions = 0;
244
254 bool readTransitionsState(bool resetTransitionCounts = true)
255 {
256 uint8_t tx[8] = { 204,_pin,22,1,(uint8_t)resetTransitionCounts,0x55,0x55,0x55 };
257 uint8_t rx[8];
258 _sw.sendPacket(tx, rx);
259 transitions = (256 * rx[5] + rx[4]);
260 return (rx[3] > 0);
261 }
262
263 uint8_t pin() { return _pin; }
264 uint8_t swPinModeNumber() { return _pinMode; }
265
266 private:
267
268 uint16_t _trueOutput = 1;
269 uint16_t _falseOutput = 1;
270};
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:43
PIN_MODE_SW18AB_CAPTOUCH
@ PIN_MODE_SW18AB_CAPTOUCH
(22)
Definition SerialWombat.h:272
SerialWombat18CapTouch::pin
uint8_t pin()
Used for inheritance.
Definition SerialWombat18CapTouch.h:263
SerialWombat18CapTouch::swPinModeNumber
uint8_t swPinModeNumber()
Used for ineheritance.
Definition SerialWombat18CapTouch.h:264
SerialWombat18CapTouch::digitalRead
bool digitalRead()
Returns the debounced state of the input.
Definition SerialWombat18CapTouch.h:181
SerialWombat18CapTouch::readDurationInTrueState_mS
uint16_t readDurationInTrueState_mS()
return the number of mS that the debounced input has been in true state
Definition SerialWombat18CapTouch.h:199
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:141
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:223
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:167
SerialWombat18CapTouch::readTransitionsState
bool readTransitionsState(bool resetTransitionCounts=true)
Queries the number of transistions that have occured on the debounced input.
Definition SerialWombat18CapTouch.h:254
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:243
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:300
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:206
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:205
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:207