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-2021 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
174 class SerialWombatQuadEnc_18AB : public SerialWombatQuadEnc, public SerialWombatAbstractProcessedInput
175{
176 public:
177 SerialWombatQuadEnc_18AB(SerialWombatChip& serialWombat) :SerialWombatQuadEnc(serialWombat) , SerialWombatAbstractProcessedInput(serialWombat)
178 {}
179
180 int16_t writeMinMaxIncrementTargetPin(uint16_t min = 65535, uint16_t max = 0, uint16_t increment = 1, uint8_t targetPin = 255)
181 {
182
183 {
184 uint8_t tx[] = { 201,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(increment), 0x55,0x55,0x55 };
185 int16_t result = _sw.sendPacket(tx);
186 if (result < 0)
187 {
188 return(result);
189 }
190 }
191 {
192 uint8_t tx[] = { 202,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(min), SW_LE16(max),0x55 };
193 int16_t result = _sw.sendPacket(tx);
194 if (result < 0)
195 {
196 return(result);
197 }
198 }
199 {
200 uint8_t tx[] = { 203,_pin,PIN_MODE_QUADRATUREENCODER,targetPin,0x55,0x55,0x55,0x55 };
201 return _sw.sendPacket(tx);
202
203 }
204
205 }
206
207 int16_t writeFrequencyPeriodmS(uint16_t period)
208 {
209 uint8_t tx[] = { 204,_pin,PIN_MODE_QUADRATUREENCODER,SW_LE16(period),0x55,0x55,0x55 };
210 return _sw.sendPacket(tx);
211
212 }
213
214 uint16_t readFrequency()
215 {
216 uint8_t tx[] = { 205,_pin,PIN_MODE_QUADRATUREENCODER,0x55,0x55,0x55,0x55,0x55 };
217 uint8_t rx[8];
218 int16_t result = _sw.sendPacket(tx,rx);
219 if (result < 0) return 0;
220
221 return ((uint16_t)(rx[3] + 256 * rx[4]));
222 }
223
224
229 uint8_t pin()
230 {
231 return SerialWombatPin::_pin;
232 }
233
237 uint8_t swPinModeNumber()
238 {
239 return SerialWombatPin::_pinMode;
240 }
241};
242
243
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:41
PIN_MODE_QUADRATUREENCODER
@ PIN_MODE_QUADRATUREENCODER
(5)
Definition SerialWombat.h:256
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:297
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:194
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:193
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:121
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:195
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:180
SerialWombatQuadEnc_18AB::swPinModeNumber
uint8_t swPinModeNumber()
fulfills a virtual function requirement of SerialWombatAbstractProcessedInput
Definition SerialWombatQuadEnc.h:237
SerialWombatQuadEnc_18AB::readFrequency
uint16_t readFrequency()
Definition SerialWombatQuadEnc.h:214
SerialWombatQuadEnc_18AB::pin
uint8_t pin()
fulfills a virtual function requirement of SerialWombatAbstractProcessedInput
Definition SerialWombatQuadEnc.h:229
SerialWombatQuadEnc_18AB::SerialWombatQuadEnc_18AB
SerialWombatQuadEnc_18AB(SerialWombatChip &serialWombat)
Definition SerialWombatQuadEnc.h:177
SerialWombatQuadEnc_18AB::writeFrequencyPeriodmS
int16_t writeFrequencyPeriodmS(uint16_t period)
Definition SerialWombatQuadEnc.h:207
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