Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatQuadEnc.h
Go to the documentation of this file.
1#pragma once
2
3/*
4 Copyright 2020-2025 Broadwell Consulting Inc.
5
6"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
7 the United States. See SerialWombat.com for usage guidance.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26*/
27
28#include <stdint.h>
29#include "SerialWombat.h"
30
31
34
35 typedef enum
36{
37 QE_ONLOW_INT = 0,
38 QE_ONHIGH_INT = 1,
39 QE_ONBOTH_INT = 2,
40 QE_ONLOW_POLL = 4,
41 QE_ONHIGH_POLL = 5,
42 QE_ONBOTH_POLL = 6
43} QE_READ_MODE_t;
44
45
46
103
104 class SerialWombatQuadEnc :public SerialWombatPin
105{
106 public:
107
112 SerialWombatQuadEnc(SerialWombatChip& serialWombatChip) :SerialWombatPin(serialWombatChip)
113 {
114 _secondPin = 255;
115 }
116
127 void begin(uint8_t pin, uint8_t secondPin, uint16_t debounce_mS = 10, bool pullUpsEnabled = true, QE_READ_MODE_t readState = QE_ONBOTH_POLL)
128 {
129 _pin = pin;
130 _secondPin = secondPin;
131 _pinMode = PIN_MODE_QUADRATUREENCODER;
132 uint8_t tx[] = { 200,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(debounce_mS), _secondPin,(uint8_t)readState,pullUpsEnabled };
133 uint8_t rx[8];
134 _sw.sendPacket(tx, rx);
135 }
136
141 uint16_t read()
142 {
143 return _sw.readPublicData(_pin);
144 }
145
155 uint16_t read(uint16_t replacementValue)
156 {
157 return _sw.writePublicData(_pin, replacementValue);
158 }
159
164 void write(uint16_t value)
165 {
166 _sw.writePublicData(_pin, value);
167 }
168 private:
169
170 uint8_t _secondPin;
171};
172
176 class SerialWombatQuadEnc_18AB : public SerialWombatQuadEnc, public SerialWombatAbstractProcessedInput
177{
178 public:
179 SerialWombatQuadEnc_18AB(SerialWombatChip& serialWombat) :SerialWombatQuadEnc(serialWombat) , SerialWombatAbstractProcessedInput(serialWombat)
180 {}
181
182 int16_t writeMinMaxIncrementTargetPin(uint16_t min = 65535, uint16_t max = 0, uint16_t increment = 1, uint8_t targetPin = 255)
183 {
184
185 {
186 uint8_t tx[] = { 201,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(increment), 0x55,0x55,0x55 };
187 int16_t result = _sw.sendPacket(tx);
188 if (result < 0)
189 {
190 return(result);
191 }
192 }
193 {
194 uint8_t tx[] = { 202,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(min), SW_LE16(max),0x55 };
195 int16_t result = _sw.sendPacket(tx);
196 if (result < 0)
197 {
198 return(result);
199 }
200 }
201 {
202 uint8_t tx[] = { 203,_pin,PIN_MODE_QUADRATUREENCODER,targetPin,0x55,0x55,0x55,0x55 };
203 return _sw.sendPacket(tx);
204
205 }
206
207 }
208
209 int16_t writeFrequencyPeriodmS(uint16_t period)
210 {
211 uint8_t tx[] = { 204,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(period),0x55,0x55,0x55 };
212 return _sw.sendPacket(tx);
213
214 }
215
216 uint16_t readFrequency()
217 {
218 uint8_t tx[] = { 205,_pin,PIN_MODE_QUADRATUREENCODER,0x55,0x55,0x55,0x55,0x55 };
219 uint8_t rx[8];
220 int16_t result = _sw.sendPacket(tx,rx);
221 if (result < 0) return 0;
222
223 return ((uint16_t)(rx[3] + 256 * rx[4]));
224 }
225
226
231 uint8_t pin()
232 {
233 return SerialWombatPin::_pin;
234 }
235
239 uint8_t swPinModeNumber()
240 {
241 return SerialWombatPin::_pinMode;
242 }
243};
244
245
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_QUADRATUREENCODER
@ PIN_MODE_QUADRATUREENCODER
(5)
Definition SerialWombat.h:266
QE_READ_MODE_t
QE_READ_MODE_t
Definition SerialWombatQuadEnc.h:36
QE_ONLOW_INT
@ QE_ONLOW_INT
Interrupt driven, process on high to low transition.
Definition SerialWombatQuadEnc.h:37
QE_ONLOW_POLL
@ QE_ONLOW_POLL
1mS Polling, process on high to low transition
Definition SerialWombatQuadEnc.h:40
QE_ONBOTH_POLL
@ QE_ONBOTH_POLL
1mS Polling, process on low to high and high to low transition
Definition SerialWombatQuadEnc.h:42
QE_ONBOTH_INT
@ QE_ONBOTH_INT
Interrupt driven, process on low to high and high to low transition.
Definition SerialWombatQuadEnc.h:39
QE_ONHIGH_POLL
@ QE_ONHIGH_POLL
1mS Polling, process on low to high transition
Definition SerialWombatQuadEnc.h:41
QE_ONHIGH_INT
@ QE_ONHIGH_INT
Interrupt driven, process on low to high transition.
Definition SerialWombatQuadEnc.h:38
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::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
SerialWombatQuadEnc_18AB::writeMinMaxIncrementTargetPin
int16_t writeMinMaxIncrementTargetPin(uint16_t min=65535, uint16_t max=0, uint16_t increment=1, uint8_t targetPin=255)
Definition SerialWombatQuadEnc.h:182
SerialWombatQuadEnc_18AB::swPinModeNumber
uint8_t swPinModeNumber()
fulfills a virtual function requirement of SerialWombatAbstractProcessedInput
Definition SerialWombatQuadEnc.h:239
SerialWombatQuadEnc_18AB::readFrequency
uint16_t readFrequency()
Definition SerialWombatQuadEnc.h:216
SerialWombatQuadEnc_18AB::pin
uint8_t pin()
fulfills a virtual function requirement of SerialWombatAbstractProcessedInput
Definition SerialWombatQuadEnc.h:231
SerialWombatQuadEnc_18AB::SerialWombatQuadEnc_18AB
SerialWombatQuadEnc_18AB(SerialWombatChip &serialWombat)
Definition SerialWombatQuadEnc.h:179
SerialWombatQuadEnc_18AB::writeFrequencyPeriodmS
int16_t writeFrequencyPeriodmS(uint16_t period)
Definition SerialWombatQuadEnc.h:209
SerialWombatQuadEnc::read
uint16_t read(uint16_t replacementValue)
Read the quadrature encoder position from the Serial Wombat chip then set the position value.
Definition SerialWombatQuadEnc.h:155
SerialWombatQuadEnc::begin
void begin(uint8_t pin, uint8_t secondPin, uint16_t debounce_mS=10, bool pullUpsEnabled=true, QE_READ_MODE_t readState=QE_ONBOTH_POLL)
Initialization for SerialWombatQuadEnc that allows configuration of debounce time,...
Definition SerialWombatQuadEnc.h:127
SerialWombatQuadEnc::read
uint16_t read()
Read the quadrature encoder position from the Serial Wombat chip.
Definition SerialWombatQuadEnc.h:141
SerialWombatQuadEnc::SerialWombatQuadEnc
SerialWombatQuadEnc(SerialWombatChip &serialWombatChip)
Constructor for the SerialWombatQuadEnc class.
Definition SerialWombatQuadEnc.h:112
SerialWombatQuadEnc::write
void write(uint16_t value)
This function initializes the position of the encoder.
Definition SerialWombatQuadEnc.h:164