PIDUINO
Loading...
Searching...
No Matches
Piduino::Mcp4725

Class for the MCP4725 series of digital-to-analog converters. More...

#include <mcp4725.h>

Inheritance diagram for Piduino::Mcp4725:
Collaboration diagram for Piduino::Mcp4725:

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< OpenModeFlagOpenMode
 Type representing a combination of OpenModeFlag values.
 

Public Member Functions

 Mcp4725 (int busId=I2cDev::Info::defaultBus().id(), int address=DefaultAddress)
 Constructs a Mcp4725 object with the specified I2C device and parameters.
 
virtual ~Mcp4725 ()
 Virtual destructor for the Mcp4725 class.
 
 Mcp4725 (const std::string &parameters)
 Constructs a Mcp4725 object from a string of parameters.
 
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 std::string registeredName ()
 Returns the name registered for this converter.
 
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 Converterfactory (const std::string &deviceName, const std::string &parameters="")
 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 &parameters="")
 Registers a converter class in the factory.
 
static std::vector< InfoavailableConverters ()
 Gets list of registered converter names.
 

Static Public Attributes

static constexpr int DefaultAddress = 0x60
 Default I2C address for the MCP4725.
 
static constexpr double DefaultFullScaleRange = 3.3
 Default full-scale range for the MCP4725.
 
static constexpr long InvalidValue = LONG_MIN
 Invalid value for the converter.
 

Protected Member Functions

 Mcp4725 (Private &dd)
 Protected constructor for internal use with PIMPL pattern.
 

Protected Attributes

std::unique_ptr< Privated_ptr
 Pointer to the private implementation.
 

Detailed Description

Class for the MCP4725 series of digital-to-analog converters.

The MCP4725 is a 12-bit digital-to-analog converter (DAC) with an I2C interface. It allows for easy integration into microcontroller projects and provides a simple way to generate analog voltages. The reference voltage is equal to the supply voltage (VDD), which is typically 3.3V or 5V (3.3V on Pi boards). An EEPROM can be enabled to store the DAC's value.

The parameters for the constructor registered are:

  • bus=id : The I2C bus ID (default is I2cDev::defaultBus().id(), use pinfo to get the default bus ID).
  • addr={0x60...0x67} : The I2C address of the converter (default is 0x60).
  • fsr=value : The full-scale range value. (default is 3.3V)
  • mode={norm,fast,eeprom,pd1k,pd100k,pd500k} : The mode setting (default is norm). See the MCP4725 datasheet for more information.

Definition at line 39 of file mcp4725.h.

Member Typedef Documentation

◆ Mode

Type representing a combination of ModeFlag values.

Definition at line 162 of file converter.h.

◆ OpenMode

Type representing a combination of OpenModeFlag values.

Definition at line 70 of file iodevice.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
inherited
Enumerator
hasFrequency 

Indicates that the converter has a clock.

hasReference 

Indicates that the converter has a reference voltage.

hasResolution 

Indicates that the converter has a resolution.

hasBipolar 

Indicates that the converter supports bipolar operation.

hasRange 

Indicates that the converter has a range.

hasSetFrequency 

Indicates that the converter supports setting the frequency.

hasSetRange 

Indicates that the converter supports setting the range.

hasSetReference 

Indicates that the converter supports setting the reference voltage.

hasSetResolution 

Indicates that the converter supports setting the resolution.

hasSetBipolar 

Indicates that the converter supports setting the bipolar mode.

requiresWaitLoop 

Indicates that the converter requires a wait loop to function correctly.

hasClockSelection 

Indicates that the converter supports clock selection.

hasModeSetting 

Indicates that the converter supports mode setting.

hasToggle 

Indicates that the converter supports toggling (GpioExpander only)

hasReferencePerChannel 

Indicates that the converter supports per-channel references.

Definition at line 164 of file converter.h.

◆ anonymous enum

anonymous enum
inherited
Enumerator
DefaultReference 

Default reference voltage.

InternalReference 

Internal reference voltage.

VddReference 

Vdd reference voltage (analog or digital power supply depending on the converter)

ExternalReference 

External reference voltage.

Internal1Reference 

1st internal reference voltage

Internal2Reference 

2nd internal reference voltage

Internal3Reference 

3rd internal reference voltage

Internal4Reference 

4th internal reference voltage

UnknownReference 

Unknown reference voltage.

Definition at line 182 of file converter.h.

◆ anonymous enum

anonymous enum
inherited
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.

◆ ModeFlag

Flags that specify the channel mode for the converter.

Enumerator
NoMode 

No mode specified.

NormalMode 

Normal mode (default)

DigitalInput 

Digital input mode.

DigitalOutput 

Digital output mode.

AnalogInput 

Analog input mode (for ADCs)

AnalogOutput 

Analog output mode (for DACs)

PullUp 

Pull-up resistor enabled.

PullDown 

Pull-down resistor enabled.

ActiveLow 

Active low mode.

EdgeRising 

Edge rising trigger.

EdgeFalling 

Edge falling trigger.

EdgeBoth 

Both rising and falling edge triggers.

Interrupt 

Interrupt mode.

Continuous 

Continuous mode (for ADCs)

SingleShot 

Single-shot mode (for ADCs)

FastMode 

Fast mode (for DACs)

SaveToEEPROM 

Save to EEPROM (for DACs)

PwrDwn0 

Power down mode (for DACs), LSB.

PwrDwn1 

Power down mode (for DACs), MSB.

PwrDwnEn 

Power down mode enable (for DACs)

PwrDwn 

Power down mode (for DACs)

PwrDwnR1 

Power down mode (for DACs), resistor value depends on device.

PwrDwnR2 

Power down mode (for DACs), resistor value depends on device.

PwrDwnR3 

Power down mode (for DACs), resistor value depends on device.

PwrDwnR4 

Power down mode (for DACs), resistor value depends on device.

PwrDwnMask 

Power down mode (for DACs), resistor value depends on device.

GainBit0 

Gain Bit 0.

GainBit1 

Gain Bit 1.

GainBit2 

Gain Bit 2.

GainEn 

Gain Enable.

GainMask 

Gain Mask.

Gain1 

Gain1 = 000 -> The lowest gain level, the real value depends on the device.

Gain2 

Gain2 = 001.

Gain3 

Gain3 = 010.

Gain4 

Gain4 = 011.

Gain5 

Gain5 = 100.

Gain6 

Gain6 = 101.

Gain7 

Gain7 = 110.

Gain8 

Gain8 = 111 -> The highest gain level, the real value depends on the device.

Definition at line 50 of file converter.h.

◆ OpenModeFlag

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.

Enumerator
NotOpen 

Device is not open.

Append 

Set the stream's position indicator to the end of the stream before each output operation.

AtEnd 

Set the stream's position indicator to the end of the stream on opening.

Binary 

Open the device in binary mode (no text translation).

ReadOnly 

Open the device for input operations only.

Text mode: When reading, end-of-line terminators are translated to '
'. When writing, end-of-line terminators are translated to the local encoding (e.g., '\r
' for Win32).

WriteOnly 

Open the device for output operations only.

ReadWrite 

Open the device for both input and output operations.

Truncate 

Discard any existing content when opening the device.

IosModes 

Combination of all standard I/O modes.

Unbuffered 

Open the device in unbuffered mode (implementation-defined).

Definition at line 48 of file iodevice.h.

◆ Type

enum Piduino::Converter::Type
inherited

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.

Constructor & Destructor Documentation

◆ Mcp4725() [1/3]

Piduino::Mcp4725::Mcp4725 ( int  busId = I2cDev::Info::defaultBus().id(),
int  address = DefaultAddress 
)

Constructs a Mcp4725 object with the specified I2C device and parameters.

Creates a new MCP4725 DAC controller instance connected to the specified I2C bus with the given device address. The device must be properly connected and powered before attempting communication.

Parameters
busIdThe I2C bus ID (default is I2cDev::Info::defaultBus().id()).
addressThe I2C address of the converter (default is 0x60).

◆ ~Mcp4725()

virtual Piduino::Mcp4725::~Mcp4725 ( )
virtual

Virtual destructor for the Mcp4725 class.

Ensures proper cleanup of I2C resources and resets the device state when the object is destroyed.

◆ Mcp4725() [2/3]

Piduino::Mcp4725::Mcp4725 ( const std::string &  parameters)
explicit

Constructs a Mcp4725 object from a string of parameters.

Creates a MCP4725 DAC controller instance using configuration parameters provided as a formatted string. This constructor is primarily used by the converter factory system for dynamic object creation.

Parameters
parametersA string containing the parameters for the Mcp4725 configuration, formatted as "bus=id:addr={0x60..0x67}:fsr=value:mode={norm,fast,eeprom,pd1k,pd100k,pd500k}".
The parameters for the constructor registered are:
  • bus=id : The I2C bus ID (default is I2cDev::Info::defaultBus().id(), use pinfo to check the default bus ID).
  • addr={0x60..0x67} : The I2C address of the converter (default is 0x60).
  • fsr=value : The full-scale range value. (default is 3.3V).
  • mode={norm,fast,eeprom,pd1k,pd100k,pd500k} : The mode setting (default is norm). See the MCP4725 datasheet for more information.
Note
This constructor is used for factory registration and must be implemented by subclasses.

◆ Mcp4725() [3/3]

Piduino::Mcp4725::Mcp4725 ( Private dd)
protected

Protected constructor for internal use with PIMPL pattern.

This constructor is used internally by the class hierarchy to create instances with a specific private implementation object. It supports the PIMPL design pattern used throughout the Piduino library.

Parameters
ddReference to the private implementation object

Member Function Documentation

◆ availableConverters()

static std::vector< Info > Piduino::Converter::availableConverters ( )
staticinherited

Gets list of registered converter names.

Returns
Vector of available converter names.

◆ clearModeFlags()

virtual bool Piduino::Converter::clearModeFlags ( long  flags,
long  mask = -1,
int  channel = -1 
)
virtualinherited

Clears the mode flags for the converter.

Parameters
flagsThe flags to clear.
maskThe mask to apply (default is -1, which means all flags). The bits are used to select specific mode flags.
channelThe channel number (default is -1, indicating all channels).
Returns
True if the mode flags were successfully cleared, false otherwise.
Note
This function may be overridden by subclasses to implement specific mode flag clearing logic.
The flags are a combination of ModeFlag values, and the mask allows selective clearing.

◆ clock()

virtual int Piduino::Converter::clock ( ) const
virtualinherited

Gets the current clock setting.

Returns
The current clock setting. This is typically used to choose a specific clock source or frequency for the converter.
Note
Default implementation returns UnknownClock, indicating no clock set.

◆ close()

virtual void Piduino::Converter::close ( )
virtualinherited

Closes the converter device.

Reimplemented from Piduino::IoDevice.

◆ deviceName()

virtual const std::string & Piduino::Converter::deviceName ( ) const
virtualinherited

Returns the device name associated with the converter.

Returns
The device name as a string.

◆ digitalToValue() [1/2]

virtual double Piduino::Converter::digitalToValue ( int  channel,
long  digitalValue,
bool  differential = false 
) const
virtualinherited

Converts a digital value to an analog value for a specific channel.

Parameters
channelThe channel number.
digitalValueThe digital value to convert.
differentialIf true, converts in differential mode (default is false).
Returns
The corresponding analog value.
Note
if the converter does not support per-channel references, the default channel reference ID will be used.

◆ digitalToValue() [2/2]

virtual double Piduino::Converter::digitalToValue ( long  digitalValue,
bool  differential = false 
) const
virtualinherited

Converts a digital value to an analog value.

Parameters
digitalValueThe digital value to convert.
differentialIf true, converts in differential mode (default is false).
Returns
The corresponding analog value.

◆ error()

virtual int Piduino::IoDevice::error ( ) const
virtualinherited

Returns the code of the last error.

Returns
The error code.

◆ errorString()

virtual std::string Piduino::IoDevice::errorString ( ) const
virtualinherited

Returns a human-readable description of the last error.

Returns
The error string.

◆ factory()

static Converter * Piduino::Converter::factory ( const std::string &  deviceName,
const std::string &  parameters = "" 
)
staticinherited

Creates a converter instance from device name.

Parameters
deviceNameThe name of the converter class to instantiate, this name may be followed by parameters in the form of a colon-separated list.
parametersThe parameters for the converter, a colon-separated list of values if not specified in deviceName.
Returns
Pointer to new Converter instance, or nullptr if not found.

◆ flags()

virtual unsigned int Piduino::Converter::flags ( ) const
virtualinherited

returns the flags associated with the converter.

Returns
The flags indicating the capabilities of the converter.

◆ frequency()

virtual long Piduino::Converter::frequency ( ) const
virtualinherited

Gets the current clock frequency.

Returns
The frequency in Hertz.

◆ fullScaleRange() [1/2]

virtual double Piduino::Converter::fullScaleRange ( ) const
virtualinherited

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.

Returns
The full-scale range value, typically in volts but may vary depending on the converter model.
Note
Default implementation returns 3.3V, should be overridden by subclasses.
if the converter supports per-channel full-scale ranges, the default channel full-scale range will be returned (Channel 0)

◆ fullScaleRange() [2/2]

virtual double Piduino::Converter::fullScaleRange ( int  channel) const
virtualinherited

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.

Parameters
channelThe channel number.
Returns
The full-scale range value for the specified channel.
Note
if it's not possible to set for a specific channel, the common full-scale range will be returned (channel will be ignored)

◆ isBipolar()

virtual bool Piduino::Converter::isBipolar ( ) const
virtualinherited

Indicates if the converter supports bipolar operation.

Returns
true if bipolar, false otherwise.

◆ isBuffered()

bool Piduino::IoDevice::isBuffered ( ) const
inherited

Returns true if the device is buffered.

Returns
True if buffered, false otherwise.

◆ isDebug()

bool Piduino::IoDevice::isDebug ( ) const
inherited

Returns true if debug mode is enabled.

Returns
True if debug mode is enabled, false otherwise.

◆ isEnabled()

virtual bool Piduino::Converter::isEnabled ( ) const
virtualinherited

Checks if the converter is enabled.

Returns
true if enabled, false otherwise.

◆ isOpen()

virtual bool Piduino::IoDevice::isOpen ( ) const
virtualinherited

Returns true if the device is currently open.

Returns
True if open, false otherwise.

◆ isReadable()

bool Piduino::IoDevice::isReadable ( ) const
inherited

Returns true if the device is readable.

Returns
True if readable, false otherwise.

◆ isSequential()

virtual bool Piduino::IoDevice::isSequential ( ) const
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.

Returns
True if the device is sequential, false otherwise.

◆ isTextModeEnabled()

bool Piduino::IoDevice::isTextModeEnabled ( ) const
inherited

Returns true if text mode is enabled.

Returns
True if text mode is enabled, false otherwise.

◆ isWritable()

bool Piduino::IoDevice::isWritable ( ) const
inherited

Returns true if the device is writable.

Returns
True if writable, false otherwise.

◆ max()

virtual long Piduino::Converter::max ( bool  differential = false) const
virtualinherited

Returns the maximum value the converter can produce.

Returns
The maximum value.

◆ min()

virtual long Piduino::Converter::min ( bool  differential = false) const
virtualinherited

Returns the minimum value the converter can produce.

Returns
The minimum value.

◆ mode()

virtual Mode Piduino::Converter::mode ( int  channel = 0) const
virtualinherited

Return the current mode of the converter.

Parameters
channelThe channel number (default is 0).
Returns
The current mode of the converter as a Mode object, the default channel implementation returns NoMode.

◆ modeFlagToStringMap()

static const std::map< ModeFlag, std::string > & Piduino::Converter::modeFlagToStringMap ( )
staticinherited

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

◆ numberOfChannels()

virtual int Piduino::Converter::numberOfChannels ( ) const
virtualinherited

Returns the number of channels supported by the converter.

Returns
The number of channels, a channel is numbering from 0 to numberOfChannels() - 1.
Note
Default implementation returns 1, indicating a single channel.

◆ open()

virtual bool Piduino::Converter::open ( IoDevice::OpenMode  mode = IoDevice::ReadWrite)
virtualinherited

Opens the converter device with the specified mode.

Parameters
modeThe open mode (default is ReadWrite).
Returns
true if successful, false otherwise.

Reimplemented from Piduino::IoDevice.

◆ openMode()

OpenMode Piduino::IoDevice::openMode ( ) const
inherited

Returns the current open mode of the device.

Returns
The open mode flags.

◆ range()

virtual long Piduino::Converter::range ( ) const
virtualinherited

Gets the digital range of the converter.

This function is used by default by valueToDigital() and digitalToValue() to calculate the appropriate scaling factors.

Returns
The range value in LSB, e.g. max() - min() + 1.

◆ read()

virtual long Piduino::Converter::read ( )
virtualinherited

Reads a digital value from the converter.

Returns
The value read from the converter as a digital value. This value is between min() and max().
Note
This function is disabled if the open mode is not ReadOnly or ReadWrite.

◆ readAverage()

virtual double Piduino::Converter::readAverage ( int  channel = 0,
bool  differential = false,
int  count = 8 
)
virtualinherited

Reads an average value of count samples from the converter.

Parameters
channelThe channel to read from (default is 0).
differentialIf true, reads in differential mode (default is false).
countThe number of samples to average (default is 8).
Returns
The average digital value read from the converter.

◆ readAverageValue()

virtual double Piduino::Converter::readAverageValue ( int  channel = 0,
bool  differential = false,
int  count = 8 
)
virtualinherited

Reads an average value from the converter.

Parameters
channelThe channel to read from (default is 0).
differentialIf true, reads in differential mode (default is false).
countThe number of samples to average (default is 8).
Returns
The average value read from the converter, which is converted to a value with digitalToValue().

◆ readChannel()

virtual long Piduino::Converter::readChannel ( int  channel = 0,
bool  differential = false 
)
virtualinherited

Reads the digital value of a specific channel from the converter.

Parameters
channelThe channel to read from.
differentialIf true, reads in differential mode (default is false).
Returns
The value read from the converter as a digital value. This value is between min() and max().

◆ readValue()

virtual double Piduino::Converter::readValue ( int  channel = 0,
bool  differential = false 
)
virtualinherited

Reads a value from the converter (ADC)

Parameters
channelThe channel to read from (default is 0).
differentialIf true, reads in differential mode (default is false).
Returns
The value read from the converter, which is converted to a value with digitalToValue().

◆ reference() [1/2]

virtual int Piduino::Converter::reference ( ) const
virtualinherited

Gets the current reference ID of the converter.

Returns
The ID reference of the reference voltage, which can be a predefined constant or a custom value depending on the converter model.
Note
if the converter supports per-channel references, the default channel reference ID will be returned (Channel 0)

◆ reference() [2/2]

virtual int Piduino::Converter::reference ( int  channel) const
virtualinherited

Gets the current reference ID of the converter for a specific channel.

Parameters
channelThe channel number.
Returns
The reference ID for the specified channel.
Note
if it's not possible to set for a specific channel, the common reference ID will be returned (channel will be ignored)

◆ registerConverter()

static void Piduino::Converter::registerConverter ( const std::string &  deviceName,
std::function< Converter *(const std::string &) >  creator,
const std::string &  type = "dac",
const std::string &  parameters = "" 
)
staticinherited

Registers a converter class in the factory.

Parameters
deviceNameThe name to register the class under.
creatorFunction that creates an instance of the class.

◆ registeredName()

static std::string Piduino::Mcp4725::registeredName ( )
inlinestatic

Returns the name registered for this converter.

Provides the string identifier used by the converter factory system to create instances of this converter type. This name is used in configuration files and command-line interfaces.

Returns
The name of the converter as a string ("mcp4725").
Note
This method is used for factory registration and must be implemented by subclasses.

Definition at line 96 of file mcp4725.h.

◆ resolution()

virtual int Piduino::Converter::resolution ( ) const
virtualinherited

Returns the resolution of the converter in bits.

Returns
The resolution in bits.

◆ run()

void Piduino::Converter::run ( )
inlineinherited

Starts the converter (enables it).

◆ setBipolar()

virtual bool Piduino::Converter::setBipolar ( bool  bipolar)
virtualinherited

Sets the bipolar mode of the converter.

Parameters
bipolartrue to enable bipolar mode, false to disable.
Returns
true if the operation was successful, false otherwise.

◆ setClock()

virtual bool Piduino::Converter::setClock ( int  clock)
virtualinherited

Sets the current clock setting.

Parameters
clockThe desired clock setting.
Returns
true if the clock was set successfully, false otherwise.

◆ setDebug()

void Piduino::IoDevice::setDebug ( bool  enabled)
inherited

Enables or disables debug mode for the device.

Parameters
enabledTrue to enable debug mode, false to disable.

◆ setEnable()

virtual void Piduino::Converter::setEnable ( bool  enable)
virtualinherited

Enables or disables the converter.

Parameters
enabletrue to enable, false to disable.

◆ setFrequency()

virtual long Piduino::Converter::setFrequency ( long  freq)
virtualinherited

Sets the clock frequency.

Parameters
freqThe desired frequency in Hertz.
Returns
The actual frequency set, 0 if not set, -1 if not supported.
Note
Default implementation returns -1. Should be overridden by subclasses.

◆ setFullScaleRange() [1/2]

virtual bool Piduino::Converter::setFullScaleRange ( double  fsr)
virtualinherited

Sets the full-scale range of the converter.

Parameters
fsrThe desired full-scale range value.
Returns
True if the full-scale range was successfully set, false otherwise.
Note
Default implementation returns false. Should be overridden by subclasses.
if the converter does not support per-channel full-scale ranges, the default channel full-scale range will be set (Channel 0)

◆ setFullScaleRange() [2/2]

virtual bool Piduino::Converter::setFullScaleRange ( int  channel,
double  fsr 
)
virtualinherited

Sets the full-scale range of the converter for a specific channel.

Parameters
channelThe channel number.
fsrThe desired full-scale range value.
Returns
True if the full-scale range was successfully set, false otherwise.
Note
Default implementation returns false. Should be overridden by subclasses.
if the converter does not support per-channel full-scale ranges, the common full-scale range will be set (channel will be ignored).

◆ setMode()

virtual bool Piduino::Converter::setMode ( Mode  m,
int  channel = -1 
)
virtualinherited

Set the mode of the converter.

Parameters
mThe mode to set.
channelThe channel number (default is -1, indicating all channels).
Returns
True if the mode was successfully set, false otherwise. The default implementation returns false.
Note
This function may be overridden by subclasses to implement specific mode setting logic.

◆ setModeFlags()

virtual bool Piduino::Converter::setModeFlags ( long  flags,
long  mask = -1,
int  channel = -1 
)
virtualinherited

Sets the mode flags for the converter.

Parameters
flagsThe flags to set.
maskThe mask to apply (default is -1, which means all flags). The bits are used to select specific mode flags.
channelThe channel number (default is -1, indicating all channels).
Returns
True if the mode flags were successfully set, false otherwise.
Note
This function may be overridden by subclasses to implement specific mode flag setting logic.
The flags are a combination of ModeFlag values, and the mask allows selective setting

◆ setRange()

virtual long Piduino::Converter::setRange ( long  range)
virtualinherited

Sets the digital range of the converter.

Parameters
rangeThe desired range value.
Returns
The set range value, 0 if not set, or -1 if not supported.
Note
Default implementation returns -1. Should be overridden by subclasses.

◆ setReference() [1/2]

virtual bool Piduino::Converter::setReference ( int  referenceId,
double  fsr = 0.0 
)
virtualinherited

Sets the reference value of the converter.

Parameters
referenceIdThe ID of the reference value to set, which can be a predefined constant or a custom value depending on the converter model.
fsrThe full-scale range value, mandatory for external reference (default is 0.0 that indicates internal or VDD reference).
Returns
true if the reference value was set successfully, false otherwise.
Note
Default implementation returns false, should be overridden by subclasses.

◆ setReference() [2/2]

virtual bool Piduino::Converter::setReference ( int  referenceId,
int  channel,
double  fsr = 0.0 
)
virtualinherited

Sets the reference value of the converter for a specific channel.

Parameters
referenceIdThe ID of the reference value to set, which can be a predefined constant or a custom value depending on the converter model.
channelThe channel number.
fsrThe full-scale range value, mandatory for external reference (default is 0.0 that indicates internal or VDD reference).
Returns
true if the reference value was set successfully, false otherwise.
Note
Default implementation returns false, should be overridden by subclasses.
if the converter does not support per-channel references, the common reference ID will be set (channel will be ignored).

◆ setResolution()

virtual int Piduino::Converter::setResolution ( int  resolution)
virtualinherited

Sets the resolution of the converter.

Parameters
resolutionThe desired resolution in bits.
Returns
The actual resolution set, or -1 if not supported.

◆ setTextModeEnabled()

void Piduino::IoDevice::setTextModeEnabled ( bool  enabled)
inherited

Enables or disables text mode for the device.

Parameters
enabledTrue to enable text mode, false to disable.

◆ stop()

void Piduino::Converter::stop ( )
inlineinherited

Stops the converter (disables it).

◆ stringToModeFlagMap()

static const std::map< std::string, Converter::ModeFlag > & Piduino::Converter::stringToModeFlagMap ( )
staticinherited

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

◆ toggle()

virtual bool Piduino::Converter::toggle ( int  channel = -1)
virtualinherited

Toggles the state of a specific channel on the converter.

Parameters
channelThe channel to toggle (default is -1, which toggles all channels).
Returns
true if successful, false otherwise.
Note
This function is disabled if the open mode is not WriteOnly or ReadWrite or if type() is not GpioExpander.

◆ type()

Type Piduino::Converter::type ( ) const
inherited

Returns the type of the converter.

Returns
The converter type (ADC, DAC, or None).

◆ valueToDigital() [1/2]

virtual long Piduino::Converter::valueToDigital ( double  value,
bool  differential = false 
) const
virtualinherited

Converts a analog value to a digital value.

Parameters
valueThe value to convert.
differentialIf true, converts in differential mode (default is false).
Returns
The corresponding digital value.

◆ valueToDigital() [2/2]

virtual long Piduino::Converter::valueToDigital ( int  channel,
double  value,
bool  differential = false 
) const
virtualinherited

Converts an analog value to a digital value for a specific channel.

Parameters
channelThe channel number.
valueThe value to convert.
differentialIf true, converts in differential mode (default is false).
Returns
The corresponding digital value.
Note
if the converter does not support per-channel references, the default channel reference ID will be used.

◆ write()

virtual bool Piduino::Converter::write ( long  value)
virtualinherited

Writes a value to the converter.

Parameters
valueThe value to write. This value will be clamped to the valid range defined by min() and max().
Returns
true if successful, false otherwise.
Note
This function is disabled if the open mode is not WriteOnly or ReadWrite.
if the converter has channels, this function will write all channels with the same value.

◆ writeChannel()

virtual bool Piduino::Converter::writeChannel ( long  value,
int  channel = -1,
bool  differential = false 
)
virtualinherited

Writes a digital value to a specific channel of the converter.

Parameters
valueThe sample value to write. This value will be clamped to the valid range defined by min() and max().
channelThe channel to write to (default is -1, which writes to all channels).
differentialIf true, writes in differential mode (default is false).
Returns
true if successful, false otherwise.
Note
This function is disabled if the open mode is not WriteOnly or ReadWrite.

◆ writeValue()

virtual bool Piduino::Converter::writeValue ( double  value,
int  channel = -1,
bool  differential = false 
)
virtualinherited

Writes a value to the converter (DAC).

Parameters
valueThe value to write, which is converted to a digital value with valueToDigital().
channelThe channel to write to (default is -1, which writes to all channels).
differentialIf true, writes in differential mode (default is false).
Returns
true if successful, false otherwise.
Note
This function is disabled if the open mode is not WriteOnly or ReadWrite.
if the converter supports per-channel references, this function cannot be called with channel=-1

Member Data Documentation

◆ d_ptr

std::unique_ptr<Private> Piduino::IoDevice::d_ptr
protectedinherited

Pointer to the private implementation.

Definition at line 190 of file iodevice.h.

◆ DefaultAddress

constexpr int Piduino::Mcp4725::DefaultAddress = 0x60
staticconstexpr

Default I2C address for the MCP4725.

Definition at line 43 of file mcp4725.h.

◆ DefaultFullScaleRange

constexpr double Piduino::Mcp4725::DefaultFullScaleRange = 3.3
staticconstexpr

Default full-scale range for the MCP4725.

Definition at line 44 of file mcp4725.h.

◆ InvalidValue

constexpr long Piduino::Converter::InvalidValue = LONG_MIN
staticconstexprinherited

Invalid value for the converter.

Definition at line 202 of file converter.h.