Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatAnalogInput.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
33
34
35 enum class AnalogInputPublicDataOutput {
36 AnalogInputPublicDataOutput_Raw = 0,
37 AnalogInputPublicDataOutput_Filtered = 1,
38 AnalogInputPublicDataOutput_Averaged = 2,
39 AnalogInputPublicDataOutput_Minimum = 3,
40 AnalogInputPublicDataOutput_Maximum = 4,
41};
42
80 class SerialWombatAnalogInput : public SerialWombatPin
81{
82 public:
88 SerialWombatAnalogInput(SerialWombatChip& SerialWombatChip):SerialWombatPin(SerialWombatChip){}
89
90
91
92
93
105 int16_t begin(uint8_t pin, uint16_t averageSamples = 64, uint16_t filterConstant = 0xFF80, AnalogInputPublicDataOutput output = AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Raw)
106 {
107 _pin = pin;
108 _pinMode = PIN_MODE_ANALOGINPUT;
109 int16_t result = initPacketNoResponse(0,0,0,0,0,0);
110 if (result < 0) { return result; }
111 //uint8_t tx1[] = { 201,_pin,PIN_MODE_ANALOGINPUT,SW_LE16(averageSamples) ,SW_LE16(filterConstant),(uint8_t)output };
112 return initPacketNoResponse(1,averageSamples,filterConstant,(uint8_t)output);
113 }
114
115
124 uint16_t readVoltage_mV()
125 {
126 uint16_t reading = _sw.readPublicData(_pin);
127 uint32_t x = ((uint32_t) reading ) * _sw._supplyVoltagemV;
128 uint16_t returnval = x >> 16;
129 return (returnval);
130 }
131
147 uint16_t readCounts()
148 {
149 return (_sw.readPublicData(_pin));
150 }
151
160 uint16_t readFiltered_mV()
161 {
162 uint32_t x = readFilteredCounts(); // Counts ranging from 0 to 65535
163 x *= _sw._supplyVoltagemV;
164 return ((uint16_t)(x >> 16));
165 }
166
176 uint16_t readFilteredCounts()
177 {
178 uint8_t tx[] = { 204,_pin,PIN_MODE_ANALOGINPUT,0x55,0x55,0x55,0x55,0x55 };
179 uint8_t rx[8];
180
181 _sw.sendPacket(tx, rx, true, 3,1);
182
183 return(rx[5] + rx[6] * 256);
184 }
185
194 uint16_t readAveraged_mV()
195 {
196 uint32_t x = readAveragedCounts(); // Counts ranging from 0 to 65535
197 x *= _sw._supplyVoltagemV;
198 return ((uint16_t)(x >> 16));
199 }
200
214 uint16_t readAveragedCounts()
215 {
216 uint8_t tx[] = { 204,_pin,PIN_MODE_ANALOGINPUT,0x55,0x55,0x55,0x55,0x55 };
217 uint8_t rx[8];
218 _sw.sendPacket(tx, rx,true,3,1);
219 return(rx[3] + rx[4] * 256);
220 }
221
223 uint16_t updateSupplyVoltage_mV()
224 {
225 return _sw.readSupplyVoltage_mV();
226 }
227
228
238 uint16_t readMaximum_mV(bool resetAfterRead = false)
239 {
240 uint8_t tx[] = { 203,_pin,PIN_MODE_ANALOGINPUT,0,0x55,0x55,0x55,0x55 };
241 uint8_t rx[8];
242 if (resetAfterRead)
243 {
244 tx[3] = 1;
245 }
246 _sw.sendPacket(tx, rx);
247 return(rx[5] + rx[6] * 256);
248 }
249
250
256 \param resetAfterRead If True, maximum value is set to 0 after read so that subsequent values become maximum. Also resets minimum to next sample.
257
258 \return A 16 bit unsigned value indicating maximum A/D Counts
259 */
260 uint16_t readMaximumCounts(bool resetAfterRead = false)
261 {
262 uint8_t tx[] = { 203,_pin,PIN_MODE_ANALOGINPUT,0,0x55,0x55,0x55,0x55 };
263 uint8_t rx[8];
264 if (resetAfterRead)
265 {
266 tx[3] = 1;
267 }
268 _sw.sendPacket(tx, rx);
269 return(rx[5] + rx[6] * 256);
270 }
271
277 \param resetAfterRead If True, minimum value is set to 0 after read so that subsequent values become minimum. Also resets maximum to next sample.
278
279 \return A 16 bit unsigned value indicating measurement in mV
280 */
281 uint16_t readMinimum_mV(bool resetAfterRead = false)
282 {
283 uint32_t x = readMaximumCounts(resetAfterRead); // Counts ranging from 0 to 65535
284 x *= _sw._supplyVoltagemV;
285 return ((uint16_t)(x >> 16));
286 }
287
288
298 uint16_t readMinimumCounts(bool resetAfterRead = false)
299 {
300 uint8_t tx[] = { 203,_pin,PIN_MODE_ANALOGINPUT,0,0x55,0x55,0x55,0x55 };
301 uint8_t rx[8];
302 if (resetAfterRead)
303 {
304 tx[3] = 1;
305 }
306 _sw.sendPacket(tx, rx);
307 return(rx[3] + rx[4] * 256);
308 }
309
310
311 private:
312
313};
314
316 class SerialWombatAnalogInput_18AB : public SerialWombatAnalogInput, public SerialWombatAbstractProcessedInput
317{
318 public:
319
320 SerialWombatAnalogInput_18AB(SerialWombatChip& serialWombat):SerialWombatAnalogInput(serialWombat),SerialWombatAbstractProcessedInput(serialWombat){}
321
322 uint8_t pin() { return _pin; }
323 uint8_t swPinModeNumber() { return _pinMode; }
324};
325
SerialWombat.h
PIN_MODE_ANALOGINPUT
@ PIN_MODE_ANALOGINPUT
(2)
Definition SerialWombat.h:253
AnalogInputPublicDataOutput
AnalogInputPublicDataOutput
Definition SerialWombatAnalogInput.h:35
AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Minimum
@ AnalogInputPublicDataOutput_Minimum
The Minimum A/D reading is displayed as public data (default)
Definition SerialWombatAnalogInput.h:39
AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Filtered
@ AnalogInputPublicDataOutput_Filtered
The Filtered A/D reading is displayed as public data (default)
Definition SerialWombatAnalogInput.h:37
AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Maximum
@ AnalogInputPublicDataOutput_Maximum
The Maximum A/D reading is displayed as public data (default)
Definition SerialWombatAnalogInput.h:40
AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Averaged
@ AnalogInputPublicDataOutput_Averaged
The Averaged A/D reading is displayed as public data (default)
Definition SerialWombatAnalogInput.h:38
AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Raw
@ AnalogInputPublicDataOutput_Raw
The raw A/D reading is displayed as public data (default)
Definition SerialWombatAnalogInput.h:36
SerialWombatAbstractProcessedInput::SerialWombatAbstractProcessedInput
SerialWombatAbstractProcessedInput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition SerialWombatAbstractProcessedInput.h:87
SerialWombatAnalogInput_18AB::pin
uint8_t pin()
Used for inheritance.
Definition SerialWombatAnalogInput.h:322
SerialWombatAnalogInput_18AB::swPinModeNumber
uint8_t swPinModeNumber()
Used for ineheritance.
Definition SerialWombatAnalogInput.h:323
SerialWombatAnalogInput_18AB::SerialWombatAnalogInput_18AB
SerialWombatAnalogInput_18AB(SerialWombatChip &serialWombat)
Definition SerialWombatAnalogInput.h:320
SerialWombatAnalogInput::readFiltered_mV
uint16_t readFiltered_mV()
Retreive a filtered A/D measurement and convert it to mV.
Definition SerialWombatAnalogInput.h:160
SerialWombatAnalogInput::readAveraged_mV
uint16_t readAveraged_mV()
Retreive an averaged A/D measurement and convert it to mV.
Definition SerialWombatAnalogInput.h:194
SerialWombatAnalogInput::SerialWombatAnalogInput
SerialWombatAnalogInput(SerialWombatChip &SerialWombatChip)
Constructor for the SerialWombatAnalogInput class.
Definition SerialWombatAnalogInput.h:88
SerialWombatAnalogInput::readMinimum_mV
uint16_t readMinimum_mV(bool resetAfterRead=false)
Definition SerialWombatAnalogInput.h:281
SerialWombatAnalogInput::readAveragedCounts
uint16_t readAveragedCounts()
Retreive an averaged A/D measurement.
Definition SerialWombatAnalogInput.h:214
SerialWombatAnalogInput::readFilteredCounts
uint16_t readFilteredCounts()
Retreive a filtered A/D measurement.
Definition SerialWombatAnalogInput.h:176
SerialWombatAnalogInput::readMaximum_mV
uint16_t readMaximum_mV(bool resetAfterRead=false)
Retreive the maximum single sample A/D value in mV.
Definition SerialWombatAnalogInput.h:238
SerialWombatAnalogInput::begin
int16_t begin(uint8_t pin, uint16_t averageSamples=64, uint16_t filterConstant=0xFF80, AnalogInputPublicDataOutput output=AnalogInputPublicDataOutput::AnalogInputPublicDataOutput_Raw)
Initialize an analog input on a given pin.
Definition SerialWombatAnalogInput.h:105
SerialWombatAnalogInput::readMaximumCounts
uint16_t readMaximumCounts(bool resetAfterRead=false)
Definition SerialWombatAnalogInput.h:260
SerialWombatAnalogInput::readVoltage_mV
uint16_t readVoltage_mV()
Retreive a raw A/D measurement and convert it to mV.
Definition SerialWombatAnalogInput.h:124
SerialWombatAnalogInput::readMinimumCounts
uint16_t readMinimumCounts(bool resetAfterRead=false)
Retreive the maximum single sample A/D value in counts.
Definition SerialWombatAnalogInput.h:298
SerialWombatAnalogInput::readCounts
uint16_t readCounts()
Retreive a raw A/D measurement.
Definition SerialWombatAnalogInput.h:147
SerialWombatAnalogInput::updateSupplyVoltage_mV
uint16_t updateSupplyVoltage_mV()
Provides a wrapper around the readSupplyVoltage_mV() method for the SerialWombat chip hosting this pi...
Definition SerialWombatAnalogInput.h:223
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::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
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