Serial Wombat Arduino Library
SerialWombatPulseTimer.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 Copyright 2020-2023 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  void begin(uint8_t pin, SerialWombatPulseTimerUnits units = SW_PULSETIMER_uS, bool pullUpEnabled = false)
101  {
102  _pin = pin;
104  uint8_t tx[] = { 200,_pin,_pinMode,pullUpEnabled,(uint8_t)units,0x55,0x55,0x55 };
105  uint8_t rx[8];
106  _sw.sendPacket(tx, rx);
107  }
108 
114  void refresh()
115  {
117  {
118  uint8_t tx[] = { 202,_pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
119  uint8_t rx[8];
120  _sw.sendPacket(tx, rx);
121  Pulses = rx[5] + 256 * rx[6];
123  }
124  }
125 
133  {
134  uint8_t tx[] = { 201,_pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
135  uint8_t rx[8];
136  _sw.sendPacket(tx, rx);
137  HighCounts = rx[3] + 256 * rx[4];
138  LowCounts = rx[5] + 256 * rx[6];
139  }
140 
141  /*
142  \brief Retreive the High counts and number of pulses Serial Wombat chip in a single transaction
143 
144  This command will retreive consecutive high and low periods from the Serial Wombat chip.
145  It it not guaranteed which is the most recent.
146  */
148  {
149  uint8_t tx[] = { 202,_pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
150  uint8_t rx[8];
151  _sw.sendPacket(tx, rx);
152  HighCounts = rx[3] + 256 * rx[4];
153  Pulses = rx[5] + 256 * rx[6];
155  }
156 
157  /*
158  \brief Retreives the most recent Counts in the configured units for the most recent high pulse
159 
160  \return Counts in mS or uS depending on configuration.
161  */
162  uint16_t readHighCounts()
163  {
165  return (HighCounts);
166  }
167 
168 
169  /*
170  \brief Retreives the most recent Counts in the configured units for the most recent low pulse
171 
172  \return Counts in mS or uS depending on configuration.
173  */
174  uint16_t readLowCounts()
175  {
177  return (LowCounts);
178  }
179 
180  /*
181  \brief Retreives the number of pulses
182 
183  This value overflows at 65535. Reading this value does not clear it.
184  This function has a side effect of reading MeasurementOverflowOccured from the Serial Wombat chip,
185  which clears it for future readings.
186  \return Number of pulses counted (1 count per high/low cycle). Rolls over at 65535 to 0
187  */
188  uint16_t readPulses()
189  {
191  return(Pulses);
192  }
193 
194  /*
195  \brief Count in selected units of last retreived high pulse
196 
197  This value is updated by refresh, refreshHighCountsLowCounts, refreshHighCountsPulses, readHighCounts, readLowCounts
198  */
199  uint16_t HighCounts = 0;
200  /*
201  \brief Count in selected units of last retreived low pulse
202 
203  This value is updated by refresh, refreshHighCountsLowCounts, readHighCounts, readLowCounts
204  */
205  uint16_t LowCounts = 0;
206 
207  /*
208  \brief Count of last retreived pulses
209 
210  This value is updated by refresh, and refreshHighCountsPulses
211  */
212  uint16_t Pulses = 0;
214 
215 private:
216 
217 };
218 
222 
227 {
228 public:
230  HIGH_TIME = 0,
231  LOW_TIME = 1,
239  };
240 
246  {
247  }
248 
249  /*
250  \brief configures which measurement is the Public Data Output of this pin mode
251 
252  This function sets what data is avaialble through the public data
253  This function is only avaialble on the Serial Wombat 18AB chip
254 
255  \param publicDataOutput An enumerated type indicating what data to output
256  \return returns 0 or higher for success or a negative error code.
257  */
259  {
260  uint8_t tx[] = { 203,_pin,_pinMode,(uint8_t) publicDataOutput};
261  return _sw.sendPacket(tx);
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:237
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:279
SerialWombatPulseTimer::readPulses
uint16_t readPulses()
Definition: SerialWombatPulseTimer.h:188
SerialWombatPulseTimer::HighCounts
uint16_t HighCounts
Definition: SerialWombatPulseTimer.h:199
SerialWombatPulseTimer_18AB::HIGH_TIME
@ HIGH_TIME
The pulse high time in uS. Updated on each high to low transition.
Definition: SerialWombatPulseTimer.h:230
SerialWombatPulseTimer::MeasurementOverflowOccurred
bool MeasurementOverflowOccurred
Definition: SerialWombatPulseTimer.h:213
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition: SerialWombat.h:1566
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:236
SerialWombatPulseTimer_18AB::pin
uint8_t pin()
Facilitates multi-inheritance.
Definition: SerialWombatPulseTimer.h:265
SerialWombatPulseTimer::readLowCounts
uint16_t readLowCounts()
Definition: SerialWombatPulseTimer.h:174
SerialWombatPulseTimer::readHighCounts
uint16_t readHighCounts()
Definition: SerialWombatPulseTimer.h:162
SerialWombatPulseTimerUnits
SerialWombatPulseTimerUnits
Definition: SerialWombatPulseTimer.h:35
PIN_MODE_PULSETIMER
@ PIN_MODE_PULSETIMER
(18)
Definition: SerialWombat.h:254
SerialWombat.h
SerialWombatPin
Describes a Serial Wombat Pin. Is base class for other pin modes.
Definition: SerialWombat.h:1470
SerialWombatPulseTimer_18AB
extends the SerialWombatPulseTimer class with SW18AB specific functionality / This class adds functio...
Definition: SerialWombatPulseTimer.h:226
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition: SerialWombat.h:1567
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:139
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:233
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:205
SerialWombatPulseTimer::refreshHighCountsPulses
void refreshHighCountsPulses()
Definition: SerialWombatPulseTimer.h:147
SerialWombatPulseTimer::refresh
void refresh()
Retreive the latest values for HighCounts, LowCounts, Pulses, and MeasurementOverflowOccured.
Definition: SerialWombatPulseTimer.h:114
SerialWombatPulseTimer_18AB::SerialWombatPulseTimer_18AB
SerialWombatPulseTimer_18AB(SerialWombatChip &serialWombat)
constructor for SerialWombatPulseTimer_18AB
Definition: SerialWombatPulseTimer.h:245
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: SerialWombat.h:1553
SerialWombat
This class name is depricated. Do not use for new development. Use SerialWombatChip instead.
Definition: SerialWombat.h:1455
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: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:238
SerialWombatPin::_pin
uint8_t _pin
Definition: SerialWombat.h:1565
SerialWombatPulseTimer::refreshHighCountsLowCounts
void refreshHighCountsLowCounts()
Retreive the High and Low counts from the Serial Wombat chip in a single transaction.
Definition: SerialWombatPulseTimer.h:132
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:235
SerialWombatPulseTimer::begin
void begin(uint8_t pin, SerialWombatPulseTimerUnits units=SW_PULSETIMER_uS, bool pullUpEnabled=false)
Initialization routine for SerialWombatPulseTimer.
Definition: SerialWombatPulseTimer.h:100
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:234
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:258
SerialWombatPulseTimer_18AB::publicDataOutput
publicDataOutput
Definition: SerialWombatPulseTimer.h:229
SerialWombatPulseTimer::Pulses
uint16_t Pulses
Definition: SerialWombatPulseTimer.h:212
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:232