Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatPulseTimer.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2020-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
28#include <stdint.h>
29#include "SerialWombat.h"
30
31 class SerialWombat;
32
35 typedef enum
36{
37 SW_PULSETIMER_uS = 0,
38 SW_PULSETIMER_mS = 1
39} SerialWombatPulseTimerUnits;
40
81
82 class SerialWombatPulseTimer : public SerialWombatPin
83{
84 public:
89 SerialWombatPulseTimer(SerialWombatChip& serialWombat):SerialWombatPin(serialWombat)
90 {
91 _pinMode = PIN_MODE_PULSETIMER;
92 }
93
101 int16_t begin(uint8_t pin, SerialWombatPulseTimerUnits units = SW_PULSETIMER_uS, bool pullUpEnabled = false)
102 {
103 _pin = pin;
104 return initPacketNoResponse(0,(uint8_t)pullUpEnabled,(uint8_t)units);
105 }
106
112 void refresh()
113 {
114 refreshHighCountsLowCounts();
115 {
116 uint8_t tx[] = { 202,_pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
117 uint8_t rx[8];
118 _sw.sendPacket(tx, rx);
119 Pulses = rx[5] + 256 * rx[6];
120 MeasurementOverflowOccurred = rx[7];
121 }
122 }
123
130 void refreshHighCountsLowCounts()
131 {
132 uint8_t tx[] = { 201,_pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
133 uint8_t rx[8];
134 _sw.sendPacket(tx, rx);
135 HighCounts = rx[3] + 256 * rx[4];
136 LowCounts = rx[5] + 256 * rx[6];
137 }
138
139 /*
140 \brief Retreive the High counts and number of pulses Serial Wombat chip in a single transaction
141
142 This command will retreive consecutive high and low periods from the Serial Wombat chip.
143 It it not guaranteed which is the most recent.
144 */
145 void refreshHighCountsPulses()
146 {
147 uint8_t tx[] = { 202,_pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
148 uint8_t rx[8];
149 _sw.sendPacket(tx, rx);
150 HighCounts = rx[3] + 256 * rx[4];
151 Pulses = rx[5] + 256 * rx[6];
152 MeasurementOverflowOccurred = rx[7];
153 }
154
155 /*
156 \brief Retreives the most recent Counts in the configured units for the most recent high pulse
157
158 \return Counts in mS or uS depending on configuration.
159 */
160 uint16_t readHighCounts()
161 {
162 refreshHighCountsLowCounts();
163 return (HighCounts);
164 }
165
166
167 /*
168 \brief Retreives the most recent Counts in the configured units for the most recent low pulse
169
170 \return Counts in mS or uS depending on configuration.
171 */
172 uint16_t readLowCounts()
173 {
174 refreshHighCountsLowCounts();
175 return (LowCounts);
176 }
177
178 /*
179 \brief Retreives the number of pulses
180
181 This value overflows at 65535. Reading this value does not clear it.
182 This function has a side effect of reading MeasurementOverflowOccured from the Serial Wombat chip,
183 which clears it for future readings.
184 \return Number of pulses counted (1 count per high/low cycle). Rolls over at 65535 to 0
185 */
186 uint16_t readPulses()
187 {
188 refreshHighCountsPulses();
189 return(Pulses);
190 }
191
192 /*
193 \brief Count in selected units of last retreived high pulse
194
195 This value is updated by refresh, refreshHighCountsLowCounts, refreshHighCountsPulses, readHighCounts, readLowCounts
196 */
197 uint16_t HighCounts = 0;
198 /*
199 \brief Count in selected units of last retreived low pulse
200
201 This value is updated by refresh, refreshHighCountsLowCounts, readHighCounts, readLowCounts
202 */
203 uint16_t LowCounts = 0;
204
205 /*
206 \brief Count of last retreived pulses
207
208 This value is updated by refresh, and refreshHighCountsPulses
209 */
210 uint16_t Pulses = 0;
211 bool MeasurementOverflowOccurred = false;
212
213 private:
214
215};
216
220This class adds functionality that is specific to the SW18AB firmware in addition
221to generic SerialWombatPulseTimer functionality avaialble on all Serial Wombat chips
222*/
223
224 class SerialWombatPulseTimer_18AB : public SerialWombatPulseTimer, public SerialWombatAbstractProcessedInput
225{
226 public:
227 enum publicDataOutput {
228 HIGH_TIME = 0,
229 LOW_TIME = 1,
230 PULSE_COUNT = 2,
231 PERIOD_ON_LTH_TRANSITION = 3,
232 PERIOD_ON_HTL_TRANSITION = 4,
233 FREQUENCY_ON_LTH_TRANSITION = 5,
234 FREQUENCY_ON_HTL_TRANSITION = 6,
235 DUTYCYCLE_ON_LTH_TRANSITION = 7,
236 DUTYCYCLE_ON_HTL_TRANSITION = 8,
237 };
238
243 SerialWombatPulseTimer_18AB(SerialWombatChip& serialWombat):SerialWombatPulseTimer(serialWombat), SerialWombatAbstractProcessedInput(serialWombat)
244 {
245 }
246
247 /*
248 \brief configures which measurement is the Public Data Output of this pin mode
249
250 This function sets what data is avaialble through the public data
251 This function is only avaialble on the Serial Wombat 18AB chip
252
253 \param publicDataOutput An enumerated type indicating what data to output
254 \return returns 0 or higher for success or a negative error code.
255 */
256 int16_t configurePublicDataOutput(SerialWombatPulseTimer_18AB::publicDataOutput publicDataOutput)
257 {
258
259 uint8_t tx[] = { 203,_pin,_pinMode,(uint8_t) publicDataOutput};
260 return _sw.sendPacket(tx);
261// return initPacketNoResponse(3,(uint8_t)publicDataOutput);
262 }
263
265 uint8_t pin() { return _pin; }
267 uint8_t swPinModeNumber() { return _pinMode; }
268};
SerialWombat.h
PIN_MODE_PULSETIMER
@ PIN_MODE_PULSETIMER
(18)
Definition SerialWombat.h:278
SerialWombatPulseTimerUnits
SerialWombatPulseTimerUnits
Definition SerialWombatPulseTimer.h:36
SW_PULSETIMER_mS
@ SW_PULSETIMER_mS
Definition SerialWombatPulseTimer.h:38
SW_PULSETIMER_uS
@ SW_PULSETIMER_uS
Definition SerialWombatPulseTimer.h:37
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:315
SerialWombat
This class name is depricated. Do not use for new development. Use SerialWombatChip instead.
Definition SerialWombat.h:1745
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:214
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::initPacketNoResponse
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)
Definition SerialWombatPin.h:130
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
SerialWombatPulseTimer_18AB::publicDataOutput
publicDataOutput
Definition SerialWombatPulseTimer.h:227
SerialWombatPulseTimer_18AB::LOW_TIME
@ LOW_TIME
the pulse low time in uS. Update on each low to high transition.
Definition SerialWombatPulseTimer.h:229
SerialWombatPulseTimer_18AB::PERIOD_ON_LTH_TRANSITION
@ PERIOD_ON_LTH_TRANSITION
The period of the pulse in uS, based on the previous high and low times, updated on low to high trans...
Definition SerialWombatPulseTimer.h:231
SerialWombatPulseTimer_18AB::DUTYCYCLE_ON_HTL_TRANSITION
@ DUTYCYCLE_ON_HTL_TRANSITION
Duty cycle of the pulse as a ratio from 0 to 65535, updated on high to low transition.
Definition SerialWombatPulseTimer.h:236
SerialWombatPulseTimer_18AB::PULSE_COUNT
@ PULSE_COUNT
The number of pulses that have occured since initialization. Updated on each high to low transition.
Definition SerialWombatPulseTimer.h:230
SerialWombatPulseTimer_18AB::FREQUENCY_ON_HTL_TRANSITION
@ FREQUENCY_ON_HTL_TRANSITION
The frequency of the pulse in Hz, based on the previous high and low times, updated on high to low tr...
Definition SerialWombatPulseTimer.h:234
SerialWombatPulseTimer_18AB::DUTYCYCLE_ON_LTH_TRANSITION
@ DUTYCYCLE_ON_LTH_TRANSITION
Duty cycle of the pulse as a ratio from 0 to 65535, updated on low to high transition.
Definition SerialWombatPulseTimer.h:235
SerialWombatPulseTimer_18AB::PERIOD_ON_HTL_TRANSITION
@ PERIOD_ON_HTL_TRANSITION
The period of the pulse in uS, based on the previous high and low times, updated on high to low trans...
Definition SerialWombatPulseTimer.h:232
SerialWombatPulseTimer_18AB::HIGH_TIME
@ HIGH_TIME
The pulse high time in uS. Updated on each high to low transition.
Definition SerialWombatPulseTimer.h:228
SerialWombatPulseTimer_18AB::FREQUENCY_ON_LTH_TRANSITION
@ FREQUENCY_ON_LTH_TRANSITION
The frequency of the pulse in Hz, based on the previous high and low times, updated on low to high tr...
Definition SerialWombatPulseTimer.h:233
SerialWombatPulseTimer_18AB::configurePublicDataOutput
int16_t configurePublicDataOutput(SerialWombatPulseTimer_18AB::publicDataOutput publicDataOutput)
Definition SerialWombatPulseTimer.h:256
SerialWombatPulseTimer_18AB::SerialWombatPulseTimer_18AB
SerialWombatPulseTimer_18AB(SerialWombatChip &serialWombat)
constructor for SerialWombatPulseTimer_18AB
Definition SerialWombatPulseTimer.h:243
SerialWombatPulseTimer_18AB::pin
uint8_t pin()
Facilitates multi-inheritance.
Definition SerialWombatPulseTimer.h:265
SerialWombatPulseTimer_18AB::swPinModeNumber
uint8_t swPinModeNumber()
Facilitates multi-inheritance.
Definition SerialWombatPulseTimer.h:267
SerialWombatPulseTimer::refreshHighCountsPulses
void refreshHighCountsPulses()
Definition SerialWombatPulseTimer.h:145
SerialWombatPulseTimer::refresh
void refresh()
Retreive the latest values for HighCounts, LowCounts, Pulses, and MeasurementOverflowOccured.
Definition SerialWombatPulseTimer.h:112
SerialWombatPulseTimer::SerialWombatPulseTimer
SerialWombatPulseTimer(SerialWombatChip &serialWombat)
Class constructor for SerialWombatPulseTimer.
Definition SerialWombatPulseTimer.h:89
SerialWombatPulseTimer::readLowCounts
uint16_t readLowCounts()
Definition SerialWombatPulseTimer.h:172
SerialWombatPulseTimer::begin
int16_t begin(uint8_t pin, SerialWombatPulseTimerUnits units=SW_PULSETIMER_uS, bool pullUpEnabled=false)
Initialization routine for SerialWombatPulseTimer.
Definition SerialWombatPulseTimer.h:101
SerialWombatPulseTimer::refreshHighCountsLowCounts
void refreshHighCountsLowCounts()
Retreive the High and Low counts from the Serial Wombat chip in a single transaction.
Definition SerialWombatPulseTimer.h:130
SerialWombatPulseTimer::Pulses
uint16_t Pulses
Definition SerialWombatPulseTimer.h:210
SerialWombatPulseTimer::HighCounts
uint16_t HighCounts
Definition SerialWombatPulseTimer.h:197
SerialWombatPulseTimer::readPulses
uint16_t readPulses()
Definition SerialWombatPulseTimer.h:186
SerialWombatPulseTimer::MeasurementOverflowOccurred
bool MeasurementOverflowOccurred
Definition SerialWombatPulseTimer.h:211
SerialWombatPulseTimer::LowCounts
uint16_t LowCounts
Definition SerialWombatPulseTimer.h:203
SerialWombatPulseTimer::readHighCounts
uint16_t readHighCounts()
Definition SerialWombatPulseTimer.h:160