Serial Wombat Arduino Library
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 {
40 
83 {
84 public:
90  {
91  }
92 
100  int16_t begin(uint8_t pin, SerialWombatPulseTimerUnits units = SW_PULSETIMER_uS, bool pullUpEnabled = false)
101  {
102  _pin = pin;
104  return initPacketNoResponse(0,(uint8_t)pullUpEnabled,(uint8_t)units);
105  }
106 
112  void refresh()
113  {
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];
121  }
122  }
123 
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  */
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];
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  {
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  {
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  {
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;
212 
213 private:
214 
215 };
216 
220 
225 {
226 public:
228  HIGH_TIME = 0,
229  LOW_TIME = 1,
237  };
238 
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  */
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 };
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
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:289
SerialWombatPulseTimer::readPulses
uint16_t readPulses()
Definition: SerialWombatPulseTimer.h:186
SerialWombatPulseTimer::begin
int16_t begin(uint8_t pin, SerialWombatPulseTimerUnits units=SW_PULSETIMER_uS, bool pullUpEnabled=false)
Initialization routine for SerialWombatPulseTimer.
Definition: SerialWombatPulseTimer.h:100
SerialWombatPulseTimer::HighCounts
uint16_t HighCounts
Definition: SerialWombatPulseTimer.h:197
SerialWombatPulseTimer_18AB::HIGH_TIME
@ HIGH_TIME
The pulse high time in uS. Updated on each high to low transition.
Definition: SerialWombatPulseTimer.h:228
SerialWombatPulseTimer::MeasurementOverflowOccurred
bool MeasurementOverflowOccurred
Definition: SerialWombatPulseTimer.h:211
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition: SerialWombatPin.h:163
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::pin
uint8_t pin()
Facilitates multi-inheritance.
Definition: SerialWombatPulseTimer.h:265
SerialWombatPulseTimer::readLowCounts
uint16_t readLowCounts()
Definition: SerialWombatPulseTimer.h:172
SerialWombatPulseTimer::readHighCounts
uint16_t readHighCounts()
Definition: SerialWombatPulseTimer.h:160
SerialWombatPulseTimerUnits
SerialWombatPulseTimerUnits
Definition: SerialWombatPulseTimer.h:35
PIN_MODE_PULSETIMER
@ PIN_MODE_PULSETIMER
(18)
Definition: SerialWombat.h:261
SerialWombat.h
SerialWombatPin
Describes a Serial Wombat Pin. Is base class for other pin modes.
Definition: SerialWombatPin.h:38
SerialWombatPulseTimer_18AB
extends the SerialWombatPulseTimer class with SW18AB specific functionality / This class adds functio...
Definition: SerialWombatPulseTimer.h:224
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition: SerialWombatPin.h:164
SerialWombatPulseTimer
A Class which uses a Serial Wombat pin to measure the length of a pulse high and low time.
Definition: SerialWombatPulseTimer.h:82
SerialWombatChip::sendPacket
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
Definition: SerialWombat.cpp:115
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::swPinModeNumber
uint8_t swPinModeNumber()
Facilitates multi-inheritance.
Definition: SerialWombatPulseTimer.h:267
SW_PULSETIMER_uS
@ SW_PULSETIMER_uS
Definition: SerialWombatPulseTimer.h:37
SerialWombatPulseTimer::LowCounts
uint16_t LowCounts
Definition: SerialWombatPulseTimer.h:203
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_18AB::SerialWombatPulseTimer_18AB
SerialWombatPulseTimer_18AB(SerialWombatChip &serialWombat)
constructor for SerialWombatPulseTimer_18AB
Definition: SerialWombatPulseTimer.h:243
SW_PULSETIMER_mS
@ SW_PULSETIMER_mS
Definition: SerialWombatPulseTimer.h:38
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition: SerialWombatPin.h:121
SerialWombat
This class name is depricated. Do not use for new development. Use SerialWombatChip instead.
Definition: SerialWombat.h:1525
SerialWombatAbstractProcessedInput
SerialWombatAnalogInput, SerialWombatPulseTimer, SerialWombatResistanceInput and others inherit from ...
Definition: SerialWombatAbstractProcessedInput.h:79
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::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
SerialWombatPin::_pin
uint8_t _pin
Definition: SerialWombatPin.h:162
SerialWombatPulseTimer::refreshHighCountsLowCounts
void refreshHighCountsLowCounts()
Retreive the High and Low counts from the Serial Wombat chip in a single transaction.
Definition: SerialWombatPulseTimer.h:130
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::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
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:131
SerialWombatPulseTimer::SerialWombatPulseTimer
SerialWombatPulseTimer(SerialWombatChip &serialWombat)
Class constructor for SerialWombatPulseTimer.
Definition: SerialWombatPulseTimer.h:89
SerialWombatPulseTimer_18AB::configurePublicDataOutput
int16_t configurePublicDataOutput(SerialWombatPulseTimer_18AB::publicDataOutput publicDataOutput)
Definition: SerialWombatPulseTimer.h:256
SerialWombatPulseTimer_18AB::publicDataOutput
publicDataOutput
Definition: SerialWombatPulseTimer.h:227
SerialWombatPulseTimer::Pulses
uint16_t Pulses
Definition: SerialWombatPulseTimer.h:210
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