PIDUINO
Loading...
Searching...
No Matches
converter.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include <string>
20#include <vector>
21#include <map>
22#include <functional>
23#include <piduino/iodevice.h>
24#include <climits>
25
26namespace Piduino {
34 class Converter : public IoDevice {
35 public:
46
50 enum ModeFlag {
51 NoMode = 0x00000000,
52 NormalMode = 0x00000000,
53 DigitalInput = 0x00000001,
54 DigitalOutput = 0x00000002,
55 AnalogInput = 0x00000004,
56 AnalogOutput = 0x00000008,
57 PullUp = 0x00000010,
58 PullDown = 0x00000020,
59 ActiveLow = 0x00000040,
60
61 // Trigger Modes for GPIO
62 EdgeRising = 0x00000080,
63 EdgeFalling = 0x00000100,
65 Interrupt = 0x00000200,
66
67 // Conversion Modes for ADCs
68 Continuous = 0x00000400,
69 SingleShot = 0x00000800,
70
71 FastMode = 0x00001000,
72 SaveToEEPROM = 0x00002000,
73
74 // Power Down Modes
75 PwrDwn0 = 0x00004000,
76 PwrDwn1 = 0x00008000,
77 PwrDwnEn = 0x00010000,
84
85 // Amplifier Gain Selection
86 GainBit0 = 0x00020000,
87 GainBit1 = 0x00040000,
88 GainBit2 = 0x00080000,
89 GainEn = 0x00100000,
99 };
100
127 static const std::map<ModeFlag, std::string> &modeFlagToStringMap();
128
155 static const std::map<std::string, Converter::ModeFlag> &stringToModeFlagMap();
156
157
163
164 enum {
165 hasFrequency = 0x00000001,
166 hasReference = 0x00000002,
167 hasResolution = 0x00000004,
168 hasBipolar = 0x00000008,
169 hasRange = 0x00000010,
170 hasSetFrequency = 0x00000020,
171 hasSetRange = 0x00000040,
172 hasSetReference = 0x00000080,
173 hasSetResolution = 0x00000100,
174 hasSetBipolar = 0x00000200,
175 requiresWaitLoop = 0x00000400,
176 hasClockSelection = 0x00000800,
177 hasModeSetting = 0x00001000,
178 hasToggle = 0x00002000,
180 };
181
182 enum {
187 // The following references are for converters that have multiple internal reference configurations
192 UnknownReference = -1
193 };
194
195 enum {
199 UnknownClock = -1
200 };
201
202 static constexpr long InvalidValue = LONG_MIN;
203
210 explicit Converter (const std::string &parameters = "");
211
218 static Converter *factory (const std::string &deviceName, const std::string &parameters = "");
219
225 static void registerConverter (const std::string &deviceName,
226 std::function<Converter* (const std::string &) > creator,
227 const std::string &type = "dac",
228 const std::string &parameters = "");
232 struct Info {
233 std::string name;
234 std::string type;
235 std::string parameters;
236 Info() = default;
237 Info (const std::string &name, const std::string &type, const std::string &parameters)
239 };
240
245 static std::vector<Info> availableConverters();
246
250 virtual ~Converter();
251
256 virtual const std::string &deviceName() const;
257
262 Type type() const;
263
268 virtual unsigned int flags() const;
269
276
280 virtual void close();
281
287 virtual long read();
288
296 virtual bool write (long value);
297
304 virtual bool toggle (int channel = -1);
305
312 virtual long readChannel (int channel = 0, bool differential = false);
313
320 virtual double readValue (int channel = 0, bool differential = false);
321
329 virtual double readAverage (int channel = 0, bool differential = false, int count = 8);
330
338 virtual double readAverageValue (int channel = 0, bool differential = false, int count = 8);
339
348 virtual bool writeChannel (long value, int channel = -1, bool differential = false);
349
359 virtual bool writeValue (double value, int channel = -1, bool differential = false);
360
366 virtual int numberOfChannels() const;
367
374 virtual double digitalToValue (long digitalValue, bool differential = false) const;
375
384 virtual double digitalToValue (int channel, long digitalValue, bool differential = false) const;
385
392 virtual long valueToDigital (double value, bool differential = false) const;
393
402 virtual long valueToDigital (int channel, double value, bool differential = false) const;
403
408 virtual void setEnable (bool enable);
409
414 virtual bool isEnabled () const;
415
419 inline void run();
420
424 inline void stop();
425
430 virtual long max (bool differential = false) const;
431
436 virtual long min (bool differential = false) const;
437
442 virtual int resolution() const;
443
449 virtual int setResolution (int resolution);
450
455 virtual bool isBipolar() const;
456
462 virtual bool setBipolar (bool bipolar);
463
471 virtual bool setReference (int referenceId, double fsr = 0.0);
472
482 virtual bool setReference (int referenceId, int channel, double fsr = 0.0);
483
489 virtual int reference() const;
490
497 virtual int reference (int channel) const;
498
508 virtual double fullScaleRange() const;
509
519 virtual double fullScaleRange (int channel) const;
520
528 virtual bool setFullScaleRange (double fsr);
529
538 virtual bool setFullScaleRange (int channel, double fsr);
539
544 virtual long frequency() const;
545
552 virtual long setFrequency (long freq);
553
561 virtual long range() const;
562
569 virtual long setRange (long range);
570
576 virtual int clock() const;
577
583 virtual bool setClock (int clock);
584
590 virtual Mode mode (int channel = 0) const;
591
599 virtual bool setMode (Mode m, int channel = -1);
600
610 virtual bool setModeFlags (long flags, long mask = -1, int channel = -1);
611
621 virtual bool clearModeFlags (long flags, long mask = -1, int channel = -1);
622
623 protected:
627 class Private;
628
634
635 private:
640 };
641
642 #ifndef DOXYGEN
643 inline void Converter::run() {
644 setEnable (true);
645 }
646 inline void Converter::stop() {
647 setEnable (false);
648 }
649 #endif // DOXYGEN
650} // namespace Piduino
651/* ========================================================================== */
The Converter class provides an interface for analog-to-digital and digital-to-analog converters.
Definition converter.h:34
virtual bool open(IoDevice::OpenMode mode=IoDevice::ReadWrite)
Opens the converter device with the specified mode.
virtual unsigned int flags() const
returns the flags associated with the converter.
static const std::map< std::string, Converter::ModeFlag > & stringToModeFlagMap()
Get the mapping of mode flags from their string representations.
virtual long setRange(long range)
Sets the digital range of the converter.
virtual void setEnable(bool enable)
Enables or disables the converter.
@ InternalReference
Internal reference voltage.
Definition converter.h:184
@ Internal3Reference
3rd internal reference voltage
Definition converter.h:190
@ Internal2Reference
2nd internal reference voltage
Definition converter.h:189
@ VddReference
Vdd reference voltage (analog or digital power supply depending on the converter)
Definition converter.h:185
@ DefaultReference
Default reference voltage.
Definition converter.h:183
@ ExternalReference
External reference voltage.
Definition converter.h:186
@ UnknownReference
Unknown reference voltage.
Definition converter.h:192
@ Internal4Reference
4th internal reference voltage
Definition converter.h:191
@ Internal1Reference
1st internal reference voltage
Definition converter.h:188
virtual long read()
Reads a digital value from the converter.
virtual bool setClock(int clock)
Sets the current clock setting.
virtual long valueToDigital(int channel, double value, bool differential=false) const
virtual int resolution() const
Returns the resolution of the converter in bits.
Converter(const std::string &parameters="")
Constructs a Converter object.
virtual ~Converter()
Destroys the Converter object.
virtual bool setModeFlags(long flags, long mask=-1, int channel=-1)
Sets the mode flags for the converter.
ModeFlag
Flags that specify the channel mode for the converter.
Definition converter.h:50
@ PwrDwnR2
Power down mode (for DACs), resistor value depends on device.
Definition converter.h:80
@ PwrDwn
Power down mode (for DACs)
Definition converter.h:78
@ NoMode
No mode specified.
Definition converter.h:51
@ PwrDwnR3
Power down mode (for DACs), resistor value depends on device.
Definition converter.h:81
@ ActiveLow
Active low mode.
Definition converter.h:59
@ PwrDwnR1
Power down mode (for DACs), resistor value depends on device.
Definition converter.h:79
@ PwrDwnEn
Power down mode enable (for DACs)
Definition converter.h:77
@ GainEn
Gain Enable.
Definition converter.h:89
@ Gain4
Gain4 = 011.
Definition converter.h:94
@ Gain1
Gain1 = 000 -> The lowest gain level, the real value depends on the device.
Definition converter.h:91
@ DigitalOutput
Digital output mode.
Definition converter.h:54
@ PwrDwnMask
Power down mode (for DACs), resistor value depends on device.
Definition converter.h:83
@ FastMode
Fast mode (for DACs)
Definition converter.h:71
@ PwrDwn0
Power down mode (for DACs), LSB.
Definition converter.h:75
@ AnalogInput
Analog input mode (for ADCs)
Definition converter.h:55
@ Interrupt
Interrupt mode.
Definition converter.h:65
@ SaveToEEPROM
Save to EEPROM (for DACs)
Definition converter.h:72
@ EdgeRising
Edge rising trigger.
Definition converter.h:62
@ PwrDwnR4
Power down mode (for DACs), resistor value depends on device.
Definition converter.h:82
@ SingleShot
Single-shot mode (for ADCs)
Definition converter.h:69
@ NormalMode
Normal mode (default)
Definition converter.h:52
@ GainBit0
Gain Bit 0.
Definition converter.h:86
@ PullDown
Pull-down resistor enabled.
Definition converter.h:58
@ PwrDwn1
Power down mode (for DACs), MSB.
Definition converter.h:76
@ Gain8
Gain8 = 111 -> The highest gain level, the real value depends on the device.
Definition converter.h:98
@ Gain5
Gain5 = 100.
Definition converter.h:95
@ GainBit1
Gain Bit 1.
Definition converter.h:87
@ Gain6
Gain6 = 101.
Definition converter.h:96
@ AnalogOutput
Analog output mode (for DACs)
Definition converter.h:56
@ EdgeFalling
Edge falling trigger.
Definition converter.h:63
@ Gain2
Gain2 = 001.
Definition converter.h:92
@ EdgeBoth
Both rising and falling edge triggers.
Definition converter.h:64
@ Continuous
Continuous mode (for ADCs)
Definition converter.h:68
@ PullUp
Pull-up resistor enabled.
Definition converter.h:57
@ Gain7
Gain7 = 110.
Definition converter.h:97
@ DigitalInput
Digital input mode.
Definition converter.h:53
@ GainMask
Gain Mask.
Definition converter.h:90
@ GainBit2
Gain Bit 2.
Definition converter.h:88
@ Gain3
Gain3 = 010.
Definition converter.h:93
static const std::map< ModeFlag, std::string > & modeFlagToStringMap()
Get the mapping of mode flags to their string representations.
virtual int reference() const
Gets the current reference ID of the converter.
virtual bool setReference(int referenceId, double fsr=0.0)
Sets the reference value of the converter.
virtual double digitalToValue(long digitalValue, bool differential=false) const
Converts a digital value to an analog value.
static std::vector< Info > availableConverters()
Gets list of registered converter names.
virtual bool clearModeFlags(long flags, long mask=-1, int channel=-1)
Clears the mode flags for the converter.
@ ExternalClock
External clock setting.
Definition converter.h:198
@ DefaultClock
Default clock setting.
Definition converter.h:196
@ InternalClock
Internal clock setting.
Definition converter.h:197
@ UnknownClock
Unknown clock setting.
Definition converter.h:199
virtual int clock() const
Gets the current clock setting.
virtual long readChannel(int channel=0, bool differential=false)
Reads the digital value of a specific channel from the converter.
virtual bool write(long value)
Writes a value to the converter.
virtual long range() const
Gets the digital range of the converter.
virtual double fullScaleRange() const
Gets the current full-scale range of the converter.
virtual double readAverageValue(int channel=0, bool differential=false, int count=8)
Reads an average value from the converter.
Flags< ModeFlag, long > Mode
Type representing a combination of ModeFlag values.
Definition converter.h:162
virtual long valueToDigital(double value, bool differential=false) const
Converts a analog value to a digital value.
virtual double readAverage(int channel=0, bool differential=false, int count=8)
Reads an average value of count samples from the converter.
virtual bool setMode(Mode m, int channel=-1)
virtual long min(bool differential=false) const
Returns the minimum value the converter can produce.
virtual bool setFullScaleRange(double fsr)
Sets the full-scale range of the converter.
virtual double digitalToValue(int channel, long digitalValue, bool differential=false) const
virtual bool setFullScaleRange(int channel, double fsr)
static constexpr long InvalidValue
Invalid value for the converter.
Definition converter.h:202
virtual bool isEnabled() const
Checks if the converter is enabled.
static Converter * factory(const std::string &deviceName, const std::string &parameters="")
Creates a converter instance from device name.
virtual long max(bool differential=false) const
Returns the maximum value the converter can produce.
Type type() const
Returns the type of the converter.
virtual bool isBipolar() const
Indicates if the converter supports bipolar operation.
virtual bool writeChannel(long value, int channel=-1, bool differential=false)
Writes a digital value to a specific channel of the converter.
virtual double readValue(int channel=0, bool differential=false)
Reads a value from the converter (ADC)
virtual long frequency() const
Gets the current clock frequency.
virtual void close()
Closes the converter device.
virtual bool setBipolar(bool bipolar)
Sets the bipolar mode of the converter.
virtual int numberOfChannels() const
Returns the number of channels supported by the converter.
virtual long setFrequency(long freq)
Sets the clock frequency.
virtual Mode mode(int channel=0) const
virtual bool toggle(int channel=-1)
Toggles the state of a specific channel on the converter.
virtual const std::string & deviceName() const
Returns the device name associated with the converter.
static void registerConverter(const std::string &deviceName, std::function< Converter *(const std::string &) > creator, const std::string &type="dac", const std::string &parameters="")
Registers a converter class in the factory.
virtual int setResolution(int resolution)
Sets the resolution of the converter.
@ hasSetResolution
Indicates that the converter supports setting the resolution.
Definition converter.h:173
@ requiresWaitLoop
Indicates that the converter requires a wait loop to function correctly.
Definition converter.h:175
@ hasModeSetting
Indicates that the converter supports mode setting.
Definition converter.h:177
@ hasReference
Indicates that the converter has a reference voltage.
Definition converter.h:166
@ hasSetFrequency
Indicates that the converter supports setting the frequency.
Definition converter.h:170
@ hasToggle
Indicates that the converter supports toggling (GpioExpander only)
Definition converter.h:178
@ hasResolution
Indicates that the converter has a resolution.
Definition converter.h:167
@ hasClockSelection
Indicates that the converter supports clock selection.
Definition converter.h:176
@ hasBipolar
Indicates that the converter supports bipolar operation.
Definition converter.h:168
@ hasSetReference
Indicates that the converter supports setting the reference voltage.
Definition converter.h:172
@ hasReferencePerChannel
Indicates that the converter supports per-channel references.
Definition converter.h:179
@ hasFrequency
Indicates that the converter has a clock.
Definition converter.h:165
@ hasRange
Indicates that the converter has a range.
Definition converter.h:169
@ hasSetRange
Indicates that the converter supports setting the range.
Definition converter.h:171
@ hasSetBipolar
Indicates that the converter supports setting the bipolar mode.
Definition converter.h:174
virtual bool writeValue(double value, int channel=-1, bool differential=false)
Writes a value to the converter (DAC).
virtual int reference(int channel) const
virtual double fullScaleRange(int channel) const
void stop()
Stops the converter (disables it).
Converter(Private &dd)
Protected constructor for use by derived classes with private data.
virtual bool setReference(int referenceId, int channel, double fsr=0.0)
void run()
Starts the converter (enables it).
Type
Enumeration of converter types.
Definition converter.h:39
@ DigitalToAnalog
Digital-to-Analog Converter (DAC)
Definition converter.h:41
@ AnalogToDigital
Analog-to-Digital Converter (ADC)
Definition converter.h:40
@ GpioExpander
GPIO expander (not a converter, but can be used with converters)
Definition converter.h:43
@ Sensor
Sensor (not a converter, but can be used with converters)
Definition converter.h:42
@ UnknownType
No converter type specified.
Definition converter.h:44
A type-safe flags class for bitwise operations on enum values.
Definition flags.h:34
Abstract base class for input/output devices.
Definition iodevice.h:37
@ ReadWrite
Open the device for both input and output operations.
Definition iodevice.h:60
Internal implementation class for GpioDevice.
#define PIMP_DECLARE_PRIVATE(Class)
PIMP_DECLARE_PRIVATE.
Definition global.h:82
Global namespace for Piduino.
Definition board.h:28
Contains information about a registered converter.
Definition converter.h:232
std::string type
The type of the converter (e.g., "dac", "adc").
Definition converter.h:234
std::string name
The name of the converter class.
Definition converter.h:233
std::string parameters
Parameters for the converter, a colon-separated list of values.
Definition converter.h:235
Info(const std::string &name, const std::string &type, const std::string &parameters)
Definition converter.h:237