![]() |
PIDUINO
|
The Converter class provides an interface for analog-to-digital and digital-to-analog converters. More...
#include <converter.h>
Classes | |
struct | Info |
Contains information about a registered converter. More... | |
Public Types | |
enum | Type { AnalogToDigital , DigitalToAnalog , Sensor , GpioExpander , UnknownType = -1 } |
Enumeration of converter types. More... | |
enum | ModeFlag { NoMode = 0x00000000 , NormalMode = 0x00000000 , DigitalInput = 0x00000001 , DigitalOutput = 0x00000002 , AnalogInput = 0x00000004 , AnalogOutput = 0x00000008 , PullUp = 0x00000010 , PullDown = 0x00000020 , ActiveLow = 0x00000040 , EdgeRising = 0x00000080 , EdgeFalling = 0x00000100 , EdgeBoth = EdgeRising | EdgeFalling , Interrupt = 0x00000200 , Continuous = 0x00000400 , SingleShot = 0x00000800 , FastMode = 0x00001000 , SaveToEEPROM = 0x00002000 , PwrDwn0 = 0x00004000 , PwrDwn1 = 0x00008000 , PwrDwnEn = 0x00010000 , PwrDwn = PwrDwnEn , PwrDwnR1 = PwrDwnEn , PwrDwnR2 = PwrDwnEn | PwrDwn0 , PwrDwnR3 = PwrDwnEn | PwrDwn1 , PwrDwnR4 = PwrDwnEn | PwrDwn1 | PwrDwn0 , PwrDwnMask = PwrDwnEn | PwrDwn1 | PwrDwn0 , GainBit0 = 0x00020000 , GainBit1 = 0x00040000 , GainBit2 = 0x00080000 , GainEn = 0x00100000 , GainMask = GainBit0 | GainBit1 | GainBit2 | GainEn , Gain1 = GainEn , Gain2 = GainEn | GainBit0 , Gain3 = GainEn | GainBit1 , Gain4 = GainEn | GainBit1 | GainBit0 , Gain5 = GainEn | GainBit2 , Gain6 = GainEn | GainBit2 | GainBit0 , Gain7 = GainEn | GainBit2 | GainBit1 , Gain8 = GainEn | GainBit2 | GainBit1 | GainBit0 } |
Flags that specify the channel mode for the converter. More... | |
enum | { hasFrequency = 0x00000001 , hasReference = 0x00000002 , hasResolution = 0x00000004 , hasBipolar = 0x00000008 , hasRange = 0x00000010 , hasSetFrequency = 0x00000020 , hasSetRange = 0x00000040 , hasSetReference = 0x00000080 , hasSetResolution = 0x00000100 , hasSetBipolar = 0x00000200 , requiresWaitLoop = 0x00000400 , hasClockSelection = 0x00000800 , hasModeSetting = 0x00001000 , hasToggle = 0x00002000 , hasReferencePerChannel = 0x00004000 } |
enum | { DefaultReference = 0 , InternalReference , VddReference , ExternalReference , Internal1Reference , Internal2Reference , Internal3Reference , Internal4Reference , UnknownReference = -1 } |
enum | { DefaultClock = 0 , InternalClock , ExternalClock , UnknownClock = -1 } |
typedef Flags< ModeFlag, long > | Mode |
Type representing a combination of ModeFlag values. | |
enum | OpenModeFlag { NotOpen = 0x0000 , Append = std::ios_base::app , AtEnd = std::ios_base::ate , Binary = std::ios_base::binary , ReadOnly = std::ios_base::in , WriteOnly = std::ios_base::out , ReadWrite = ReadOnly | WriteOnly , Truncate = std::ios_base::trunc , IosModes = Append | AtEnd | Binary | ReadOnly | WriteOnly | Truncate , Unbuffered = (Truncate << 1) } |
Flags that specify how the device is to be opened. More... | |
typedef Flags< OpenModeFlag > | OpenMode |
Type representing a combination of OpenModeFlag values. | |
Public Member Functions | |
Converter (const std::string ¶meters="") | |
Constructs a Converter object. | |
virtual | ~Converter () |
Destroys the Converter object. | |
virtual const std::string & | deviceName () const |
Returns the device name associated with the converter. | |
Type | type () const |
Returns the type of the converter. | |
virtual unsigned int | flags () const |
returns the flags associated with the converter. | |
virtual bool | open (IoDevice::OpenMode mode=IoDevice::ReadWrite) |
Opens the converter device with the specified mode. | |
virtual void | close () |
Closes the converter device. | |
virtual long | read () |
Reads a digital value from the converter. | |
virtual bool | write (long value) |
Writes a value to the converter. | |
virtual bool | toggle (int channel=-1) |
Toggles the state of a specific channel on the converter. | |
virtual long | readChannel (int channel=0, bool differential=false) |
Reads the digital value of a specific channel from the converter. | |
virtual double | readValue (int channel=0, bool differential=false) |
Reads a value from the converter (ADC) | |
virtual double | readAverage (int channel=0, bool differential=false, int count=8) |
Reads an average value of count samples from the converter. | |
virtual double | readAverageValue (int channel=0, bool differential=false, int count=8) |
Reads an average value from the converter. | |
virtual bool | writeChannel (long value, int channel=-1, bool differential=false) |
Writes a digital value to a specific channel of the converter. | |
virtual bool | writeValue (double value, int channel=-1, bool differential=false) |
Writes a value to the converter (DAC). | |
virtual int | numberOfChannels () const |
Returns the number of channels supported by the converter. | |
virtual double | digitalToValue (long digitalValue, bool differential=false) const |
Converts a digital value to an analog value. | |
virtual double | digitalToValue (int channel, long digitalValue, bool differential=false) const |
virtual long | valueToDigital (double value, bool differential=false) const |
Converts a analog value to a digital value. | |
virtual long | valueToDigital (int channel, double value, bool differential=false) const |
virtual void | setEnable (bool enable) |
Enables or disables the converter. | |
virtual bool | isEnabled () const |
Checks if the converter is enabled. | |
void | run () |
Starts the converter (enables it). | |
void | stop () |
Stops the converter (disables it). | |
virtual long | max (bool differential=false) const |
Returns the maximum value the converter can produce. | |
virtual long | min (bool differential=false) const |
Returns the minimum value the converter can produce. | |
virtual int | resolution () const |
Returns the resolution of the converter in bits. | |
virtual int | setResolution (int resolution) |
Sets the resolution of the converter. | |
virtual bool | isBipolar () const |
Indicates if the converter supports bipolar operation. | |
virtual bool | setBipolar (bool bipolar) |
Sets the bipolar mode of the converter. | |
virtual bool | setReference (int referenceId, double fsr=0.0) |
Sets the reference value of the converter. | |
virtual bool | setReference (int referenceId, int channel, double fsr=0.0) |
virtual int | reference () const |
Gets the current reference ID of the converter. | |
virtual int | reference (int channel) const |
virtual double | fullScaleRange () const |
Gets the current full-scale range of the converter. | |
virtual double | fullScaleRange (int channel) const |
virtual bool | setFullScaleRange (double fsr) |
Sets the full-scale range of the converter. | |
virtual bool | setFullScaleRange (int channel, double fsr) |
virtual long | frequency () const |
Gets the current clock frequency. | |
virtual long | setFrequency (long freq) |
Sets the clock frequency. | |
virtual long | range () const |
Gets the digital range of the converter. | |
virtual long | setRange (long range) |
Sets the digital range of the converter. | |
virtual int | clock () const |
Gets the current clock setting. | |
virtual bool | setClock (int clock) |
Sets the current clock setting. | |
virtual Mode | mode (int channel=0) const |
virtual bool | setMode (Mode m, int channel=-1) |
virtual bool | setModeFlags (long flags, long mask=-1, int channel=-1) |
Sets the mode flags for the converter. | |
virtual bool | clearModeFlags (long flags, long mask=-1, int channel=-1) |
Clears the mode flags for the converter. | |
OpenMode | openMode () const |
Returns the current open mode of the device. | |
virtual bool | isOpen () const |
Returns true if the device is currently open. | |
bool | isReadable () const |
Returns true if the device is readable. | |
bool | isWritable () const |
Returns true if the device is writable. | |
bool | isBuffered () const |
Returns true if the device is buffered. | |
virtual bool | isSequential () const |
Returns true if this device is sequential; otherwise returns false. | |
void | setTextModeEnabled (bool enabled) |
Enables or disables text mode for the device. | |
bool | isTextModeEnabled () const |
Returns true if text mode is enabled. | |
void | setDebug (bool enabled) |
Enables or disables debug mode for the device. | |
bool | isDebug () const |
Returns true if debug mode is enabled. | |
virtual std::string | errorString () const |
Returns a human-readable description of the last error. | |
virtual int | error () const |
Returns the code of the last error. | |
Static Public Member Functions | |
static const std::map< ModeFlag, std::string > & | modeFlagToStringMap () |
Get the mapping of mode flags to their string representations. | |
static const std::map< std::string, Converter::ModeFlag > & | stringToModeFlagMap () |
Get the mapping of mode flags from their string representations. | |
static Converter * | factory (const std::string &deviceName, const std::string ¶meters="") |
Creates a converter instance from device name. | |
static void | registerConverter (const std::string &deviceName, std::function< Converter *(const std::string &) > creator, const std::string &type="dac", const std::string ¶meters="") |
Registers a converter class in the factory. | |
static std::vector< Info > | availableConverters () |
Gets list of registered converter names. | |
Static Public Attributes | |
static constexpr long | InvalidValue = LONG_MIN |
Invalid value for the converter. | |
Protected Member Functions | |
Converter (Private &dd) | |
Protected constructor for use by derived classes with private data. | |
Protected Attributes | |
std::unique_ptr< Private > | d_ptr |
Pointer to the private implementation. | |
The Converter class provides an interface for analog-to-digital and digital-to-analog converters.
This abstract class defines the basic operations and properties for converter devices, such as ADCs (Analog-to-Digital Converters) and DACs (Digital-to-Analog Converters). It inherits from IoDevice and provides methods for reading, writing, enabling, and configuring the converter.
Definition at line 34 of file converter.h.
Type representing a combination of ModeFlag values.
Definition at line 162 of file converter.h.
|
inherited |
Type representing a combination of OpenModeFlag values.
Definition at line 70 of file iodevice.h.
anonymous enum |
Definition at line 164 of file converter.h.
anonymous enum |
Definition at line 182 of file converter.h.
anonymous enum |
Enumerator | |
---|---|
DefaultClock | Default clock setting. |
InternalClock | Internal clock setting. |
ExternalClock | External clock setting. |
UnknownClock | Unknown clock setting. |
Definition at line 195 of file converter.h.
Flags that specify the channel mode for the converter.
Definition at line 50 of file converter.h.
|
inherited |
Flags that specify how the device is to be opened.
These flags control the behavior of the device when it is opened. They can be combined using bitwise OR.
Definition at line 48 of file iodevice.h.
Enumeration of converter types.
Enumerator | |
---|---|
AnalogToDigital | Analog-to-Digital Converter (ADC) |
DigitalToAnalog | Digital-to-Analog Converter (DAC) |
Sensor | Sensor (not a converter, but can be used with converters) |
GpioExpander | GPIO expander (not a converter, but can be used with converters) |
UnknownType | No converter type specified. |
Definition at line 39 of file converter.h.
|
explicit |
Constructs a Converter object.
parameters | The parameters for the converter. This is a list of values separated by colon |
|
virtual |
Destroys the Converter object.
|
protected |
Protected constructor for use by derived classes with private data.
dd | Reference to the private data. |
|
static |
Gets list of registered converter names.
|
virtual |
Clears the mode flags for the converter.
flags | The flags to clear. |
mask | The mask to apply (default is -1, which means all flags). The bits are used to select specific mode flags. |
channel | The channel number (default is -1, indicating all channels). |
|
virtual |
Gets the current clock setting.
|
virtual |
Closes the converter device.
Reimplemented from Piduino::IoDevice.
|
virtual |
Returns the device name associated with the converter.
|
virtual |
Converts a digital value to an analog value for a specific channel.
channel | The channel number. |
digitalValue | The digital value to convert. |
differential | If true, converts in differential mode (default is false). |
|
virtual |
Converts a digital value to an analog value.
digitalValue | The digital value to convert. |
differential | If true, converts in differential mode (default is false). |
|
virtualinherited |
Returns the code of the last error.
|
virtualinherited |
Returns a human-readable description of the last error.
|
static |
Creates a converter instance from device name.
deviceName | The name of the converter class to instantiate, this name may be followed by parameters in the form of a colon-separated list. |
parameters | The parameters for the converter, a colon-separated list of values if not specified in deviceName. |
|
virtual |
returns the flags associated with the converter.
|
virtual |
Gets the current clock frequency.
|
virtual |
Gets the current full-scale range of the converter.
This function is used, by default, by valueToDigital()
and digitalToValue()
to calculate the appropriate scaling factors.
|
virtual |
Gets the current full-scale range of the converter for a specific channel.
This function is used, by default, by valueToDigital()
and digitalToValue()
to calculate the appropriate scaling factors.
channel | The channel number. |
|
virtual |
Indicates if the converter supports bipolar operation.
|
inherited |
Returns true if the device is buffered.
|
inherited |
Returns true if debug mode is enabled.
|
virtual |
Checks if the converter is enabled.
|
virtualinherited |
Returns true if the device is currently open.
|
inherited |
Returns true if the device is readable.
|
virtualinherited |
Returns true if this device is sequential; otherwise returns false.
Sequential devices, as opposed to random-access devices, have no concept of a start, an end, a size, or a current position, and do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential. Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.
The IoDevice implementation returns false.
|
inherited |
Returns true if text mode is enabled.
|
inherited |
Returns true if the device is writable.
|
virtual |
Returns the maximum value the converter can produce.
|
virtual |
Returns the minimum value the converter can produce.
|
virtual |
Return the current mode of the converter.
channel | The channel number (default is 0). |
|
static |
Get the mapping of mode flags to their string representations.
This function returns a map where the keys are the mode flags and the values are their string representations:
Flag | Str |
---|---|
DigitalInput | in |
DigitalOutput | out |
AnalogInput | ain |
AnalogOutput | aout |
ActiveLow | activelow |
PullUp | up |
PullDown | down |
NoMode | off |
EdgeRising | rising |
EdgeFalling | falling |
Interrupt | int |
Continuous | continuous |
SingleShot | single |
FastMode | fast |
SaveToEEPROM | eeprom |
PwrDwnEn | pden |
PwrDwn0 | pd0 |
PwrDwn1 | pd1 |
|
virtual |
Returns the number of channels supported by the converter.
|
virtual |
Opens the converter device with the specified mode.
mode | The open mode (default is ReadWrite). |
Reimplemented from Piduino::IoDevice.
|
inherited |
Returns the current open mode of the device.
|
virtual |
Gets the digital range of the converter.
This function is used by default by valueToDigital()
and digitalToValue()
to calculate the appropriate scaling factors.
|
virtual |
|
virtual |
Reads an average value of count samples from the converter.
channel | The channel to read from (default is 0). |
differential | If true, reads in differential mode (default is false). |
count | The number of samples to average (default is 8). |
|
virtual |
Reads an average value from the converter.
channel | The channel to read from (default is 0). |
differential | If true, reads in differential mode (default is false). |
count | The number of samples to average (default is 8). |
|
virtual |
|
virtual |
Reads a value from the converter (ADC)
channel | The channel to read from (default is 0). |
differential | If true, reads in differential mode (default is false). |
|
virtual |
Gets the current reference ID of the converter.
|
virtual |
Gets the current reference ID of the converter for a specific channel.
channel | The channel number. |
|
static |
Registers a converter class in the factory.
deviceName | The name to register the class under. |
creator | Function that creates an instance of the class. |
|
virtual |
Returns the resolution of the converter in bits.
|
inline |
Starts the converter (enables it).
|
virtual |
Sets the bipolar mode of the converter.
bipolar | true to enable bipolar mode, false to disable. |
|
virtual |
Sets the current clock setting.
clock | The desired clock setting. |
|
inherited |
Enables or disables debug mode for the device.
enabled | True to enable debug mode, false to disable. |
|
virtual |
Enables or disables the converter.
enable | true to enable, false to disable. |
|
virtual |
Sets the clock frequency.
freq | The desired frequency in Hertz. |
|
virtual |
Sets the full-scale range of the converter.
fsr | The desired full-scale range value. |
|
virtual |
Sets the full-scale range of the converter for a specific channel.
channel | The channel number. |
fsr | The desired full-scale range value. |
|
virtual |
Set the mode of the converter.
m | The mode to set. |
channel | The channel number (default is -1, indicating all channels). |
|
virtual |
Sets the mode flags for the converter.
flags | The flags to set. |
mask | The mask to apply (default is -1, which means all flags). The bits are used to select specific mode flags. |
channel | The channel number (default is -1, indicating all channels). |
|
virtual |
Sets the digital range of the converter.
range | The desired range value. |
|
virtual |
Sets the reference value of the converter.
referenceId | The ID of the reference value to set, which can be a predefined constant or a custom value depending on the converter model. |
fsr | The full-scale range value, mandatory for external reference (default is 0.0 that indicates internal or VDD reference). |
|
virtual |
Sets the reference value of the converter for a specific channel.
referenceId | The ID of the reference value to set, which can be a predefined constant or a custom value depending on the converter model. |
channel | The channel number. |
fsr | The full-scale range value, mandatory for external reference (default is 0.0 that indicates internal or VDD reference). |
|
virtual |
Sets the resolution of the converter.
resolution | The desired resolution in bits. |
|
inherited |
Enables or disables text mode for the device.
enabled | True to enable text mode, false to disable. |
|
inline |
Stops the converter (disables it).
|
static |
Get the mapping of mode flags from their string representations.
This function returns a map where the keys are the string representations and the values are the mode flags.
Flag | Str |
---|---|
DigitalInput | in |
DigitalOutput | out |
AnalogInput | ain |
AnalogOutput | aout |
ActiveLow | activelow |
PullUp | up |
PullDown | down |
NoMode | off |
EdgeRising | rising |
EdgeFalling | falling |
Interrupt | int |
Continuous | continuous |
SingleShot | single |
FastMode | fast |
SaveToEEPROM | eeprom |
PwrDwnEn | pden |
PwrDwn0 | pd0 |
PwrDwn1 | pd1 |
|
virtual |
Toggles the state of a specific channel on the converter.
channel | The channel to toggle (default is -1, which toggles all channels). |
Type Piduino::Converter::type | ( | ) | const |
Returns the type of the converter.
|
virtual |
Converts a analog value to a digital value.
value | The value to convert. |
differential | If true, converts in differential mode (default is false). |
|
virtual |
Converts an analog value to a digital value for a specific channel.
channel | The channel number. |
value | The value to convert. |
differential | If true, converts in differential mode (default is false). |
|
virtual |
Writes a value to the converter.
value | The value to write. This value will be clamped to the valid range defined by min() and max() . |
|
virtual |
Writes a digital value to a specific channel of the converter.
value | The sample value to write. This value will be clamped to the valid range defined by min() and max() . |
channel | The channel to write to (default is -1, which writes to all channels). |
differential | If true, writes in differential mode (default is false). |
|
virtual |
Writes a value to the converter (DAC).
value | The value to write, which is converted to a digital value with valueToDigital(). |
channel | The channel to write to (default is -1, which writes to all channels). |
differential | If true, writes in differential mode (default is false). |
|
protectedinherited |
Pointer to the private implementation.
Definition at line 190 of file iodevice.h.
|
staticconstexpr |
Invalid value for the converter.
Definition at line 202 of file converter.h.