ads1115  0.2.0
ADS1115::ADS1115 Class Reference

#include "ads1115.hpp"

Collaboration diagram for ADS1115::ADS1115:
[legend]

Public Member Functions

 ADS1115 (const int adapter_nr, const ADDR addr)
 
 ADS1115 (const ADS1115 &other)=delete
 
 ADS1115 (ADS1115 &&other) noexcept
 
ADS1115operator= (const ADS1115 &other)=delete
 
ADS1115operator= (ADS1115 &&other) noexcept
 
 ~ADS1115 ()=default
 Destroys the object that represents the ADS1115. More...
 
ADDR getADDR () const
 
std::int16_t read () const
 
double readVoltage () const
 
void reset ()
 
double toVoltage (const std::int16_t value) const
 
std::int16_t fromVoltage (const double value) const
 
Config readRegConfig ()
 
Config getRegConfig () const
 
void setRegConfig (const Config config)
 
Threshold readRegThreshold ()
 
Threshold getRegThreshold () const
 
void setRegThreshold (const Threshold threshold)
 

Private Attributes

i2c::i2c_device m_device
 The generic i2c device in use. More...
 
ADDR m_addr
 The i2c bus address of the ADS1115. More...
 
Config m_config {}
 The current configuration of the ADS1115. More...
 
Threshold m_threshold {}
 The current configuration of the threshold registers. More...
 

Detailed Description

This class describes the ADS1115. The data sheet of the ADS1115 can be fond at https://cdn-shop.adafruit.com/datasheets/ads1115.pdf.

Definition at line 23 of file ads1115.hpp.

Constructor & Destructor Documentation

◆ ADS1115() [1/3]

ADS1115::ADS1115::ADS1115 ( const int  adapter_nr,
const ADDR  addr 
)

Creates the object to represent the ADS1115.

This will open the device given by fs_dev and try to connect to the given i2c bus address. The the configuration register of the ADS1115 is loaded and stored in the object.

Parameters
fs_devstd::filesystem::path to the i2c bus device file.
addrAddress of the ADS1115 on the i2c bus.

Definition at line 21 of file ads1115.cpp.

22  : m_device(i2c::AdapterNumber(adapter_nr), i2c::DeviceID(static_cast<int>(addr))),
23  m_addr(addr)
24  {
25  }

◆ ADS1115() [2/3]

ADS1115::ADS1115::ADS1115 ( const ADS1115 other)
delete

◆ ADS1115() [3/3]

ADS1115::ADS1115::ADS1115 ( ADS1115 &&  other)
noexcept

Definition at line 27 of file ads1115.cpp.

28  : m_device(std::move(other.m_device)),
29  m_addr(other.m_addr),
30  m_config(other.m_config),
31  m_threshold(other.m_threshold)
32  {
33  }

◆ ~ADS1115()

ADS1115::ADS1115::~ADS1115 ( )
default

Destroys the object that represents the ADS1115.

Member Function Documentation

◆ fromVoltage()

std::int16_t ADS1115::ADS1115::fromVoltage ( const double  value) const

Converts a voltage value to a value returned by read(),

Uses the current PGA setting for the conversion.

Parameters
valueThe value to be converted.
Returns
The value in the same representation as returned by read().

Definition at line 88 of file ads1115.cpp.

89  {
90  return static_cast<std::int16_t>(
91  value * std::numeric_limits<std::int16_t>::max() / pga_voltage_map.at(m_config.pga));
92  }

References ADS1115::pga_voltage_map.

◆ getADDR()

ADDR ADS1115::ADS1115::getADDR ( ) const

Returns the address of the ADS1115 that is currently used.

Returns
ADDR that represents the currently used address.

Definition at line 45 of file ads1115.cpp.

46  {
47  return m_addr;
48  }

◆ getRegConfig()

Config ADS1115::ADS1115::getRegConfig ( ) const

Returns the currently stored configuration of the ADS1115 without hiting the chip.

Returns
A Config object representing the content of the config register.

Definition at line 106 of file ads1115.cpp.

107  {
108  return m_config;
109  }

Referenced by TEST().

Here is the caller graph for this function:

◆ getRegThreshold()

Threshold ADS1115::ADS1115::getRegThreshold ( ) const

Returns the currently stored threshold registers of the ADS1115 whithout hiting the chip.

Returns
A Threshold object representing the contents of the threshold registers.

Definition at line 135 of file ads1115.cpp.

136  {
137  return m_threshold;
138  }

Referenced by TEST().

Here is the caller graph for this function:

◆ operator=() [1/2]

ADS1115 & ADS1115::ADS1115::operator= ( ADS1115 &&  other)
noexcept

Definition at line 35 of file ads1115.cpp.

36  {
37  m_device = std::move(other.m_device);
38  m_addr = std::move(other.m_addr);
39  m_config = std::move(other.m_config);
40  m_threshold = std::move(other.m_threshold);
41 
42  return *this;
43  }

◆ operator=() [2/2]

ADS1115& ADS1115::ADS1115::operator= ( const ADS1115 other)
delete

◆ read()

std::int16_t ADS1115::ADS1115::read ( ) const

Read the current value of the conversion register of the ADS1115. If the ADS1115 is in single conversion mode a new conversion will be started and the function returns when the conversion is done.

Returns
The value of the conversion register.

Definition at line 50 of file ads1115.cpp.

51  {
53  // to start a conversion in single conversion mode the MSB of the config
54  // register has to be set
55  m_device.write_word_data(conf_reg_addr, m_config.to_bytes() | 0x8000);
56  // when the conversion starts the ADS1115 resets the MSB and sets it when the conversion
57  // is done
58  while (!(m_device.read_word_data(conf_reg_addr) & 0x8000)) {
59  // poll every millisecond, as the shortest conversion the device can do takes 1.2ms
60  std::this_thread::sleep_for(std::chrono::milliseconds(1));
61  }
62  }
63 
64  std::uint16_t data = m_device.read_word_data(conv_reg_addr);
65 
66  return std::bit_cast<std::int16_t>(data);
67  }

References ADS1115::conf_reg_addr, ADS1115::conv_reg_addr, and ADS1115::SINGLE_CONV.

◆ readRegConfig()

Config ADS1115::ADS1115::readRegConfig ( )

Read the config register of the ADS1115.

Reads the current value of the config register of the ADS1115 and updates the internal object representation of the config.

Returns
A Config object representing the content of the config register.

Definition at line 98 of file ads1115.cpp.

99  {
100  const std::uint16_t config_word = m_device.read_word_data(conf_reg_addr);
101  Config config(config_word);
102  m_config = config;
103  return config;
104  }

References ADS1115::conf_reg_addr.

◆ readRegThreshold()

Threshold ADS1115::ADS1115::readRegThreshold ( )

Read the threshold registers of the ADS1115.

Reads the current value of the threshold registers of the ADS1115 and updates the internal object representation.

Returns
A Threshold object representing the contents of the threshold registers.

Definition at line 121 of file ads1115.cpp.

122  {
123  std::uint16_t low_threshold = m_device.read_word_data(lo_thresh_reg_addr);
124  std::uint16_t high_threshold = m_device.read_word_data(hi_thresh_reg_addr);
125 
126  Threshold threshold {
127  std::bit_cast<std::int16_t>(low_threshold),
128  std::bit_cast<std::int16_t>(high_threshold),
129  };
130 
131  m_threshold = threshold;
132  return threshold;
133  }

References ADS1115::hi_thresh_reg_addr, and ADS1115::lo_thresh_reg_addr.

◆ readVoltage()

double ADS1115::ADS1115::readVoltage ( ) const

Read the current voltage measured by the ADS1115. If the ADS1115 is in single conversion mode a new conversion will be started and the function returns when the conversion is done.

Returns
The voltage value.

Definition at line 69 of file ads1115.cpp.

70  {
71  return toVoltage(read());
72  }

◆ reset()

void ADS1115::ADS1115::reset ( )

Resets the device to it's default state.

Definition at line 74 of file ads1115.cpp.

75  {
76  Config config {};
77  setRegConfig(config);
78 
79  Threshold threshold {};
80  setRegThreshold(threshold);
81  }

Referenced by TEST().

Here is the caller graph for this function:

◆ setRegConfig()

void ADS1115::ADS1115::setRegConfig ( const Config  config)

Sets the configuration of the ADS1115.

Updates the internal object representation of the configuration and writes it to the config register of the ADS1115.

Parameters
configA Config object representing the content of the config register.

Definition at line 111 of file ads1115.cpp.

112  {
113  m_device.write_word_data(conf_reg_addr, m_config.to_bytes());
114  m_config = config;
115  }

References ADS1115::conf_reg_addr.

◆ setRegThreshold()

void ADS1115::ADS1115::setRegThreshold ( const Threshold  threshold)

Sets the threshold registers of the ADS1115.

Updates the internal object representation of the configuration and writes it to the threshold registers of the ADS1115.

Parameters
thresholdA Threshold object representing the contents of the threshold registers.

Definition at line 140 of file ads1115.cpp.

141  {
142  m_device.write_word_data(
144  std::bit_cast<std::uint16_t>(threshold.getLow()));
145  m_device.write_word_data(
147  std::bit_cast<std::uint16_t>(threshold.getHigh()));
148  m_threshold = threshold;
149  }

References ADS1115::Threshold::getHigh(), ADS1115::Threshold::getLow(), ADS1115::hi_thresh_reg_addr, and ADS1115::lo_thresh_reg_addr.

Here is the call graph for this function:

◆ toVoltage()

double ADS1115::ADS1115::toVoltage ( const std::int16_t  value) const

Converts a value returned by read() to a Voltage.

Uses the current PGA setting for the conversion.

Parameters
valueThe value to be converted.
Returns
The voltage value of the input.

Definition at line 83 of file ads1115.cpp.

84  {
85  return value * pga_voltage_map.at(m_config.pga) / std::numeric_limits<std::int16_t>::max();
86  }

References ADS1115::pga_voltage_map.

Member Data Documentation

◆ m_addr

ADDR ADS1115::ADS1115::m_addr
private

The i2c bus address of the ADS1115.

Definition at line 29 of file ads1115.hpp.

◆ m_config

Config ADS1115::ADS1115::m_config {}
private

The current configuration of the ADS1115.

Definition at line 32 of file ads1115.hpp.

◆ m_device

i2c::i2c_device ADS1115::ADS1115::m_device
private

The generic i2c device in use.

Definition at line 26 of file ads1115.hpp.

◆ m_threshold

Threshold ADS1115::ADS1115::m_threshold {}
private

The current configuration of the threshold registers.

Definition at line 35 of file ads1115.hpp.


The documentation for this class was generated from the following files:
ADS1115::conv_reg_addr
constexpr std::uint8_t conv_reg_addr
Address of the conversion register.
Definition: parameters.hpp:11
ADS1115::Config::pga
PGA pga
Definition: config.hpp:26
ADS1115::conf_reg_addr
constexpr std::uint8_t conf_reg_addr
Address of the config register.
Definition: parameters.hpp:13
ADS1115::ADS1115::m_addr
ADDR m_addr
The i2c bus address of the ADS1115.
Definition: ads1115.hpp:29
ADS1115::ADS1115::m_device
i2c::i2c_device m_device
The generic i2c device in use.
Definition: ads1115.hpp:26
ADS1115::hi_thresh_reg_addr
constexpr std::uint8_t hi_thresh_reg_addr
Address of the high threshold register.
Definition: parameters.hpp:17
ADS1115::Config::to_bytes
std::uint16_t to_bytes() const
Definition: config.cpp:23
ADS1115::ADS1115::toVoltage
double toVoltage(const std::int16_t value) const
Definition: ads1115.cpp:83
ADS1115::MODE::SINGLE_CONV
@ SINGLE_CONV
Sets the ADS1115 to power-down single-shot mode.
ADS1115::ADS1115::setRegThreshold
void setRegThreshold(const Threshold threshold)
Definition: ads1115.cpp:140
ADS1115::ADS1115::setRegConfig
void setRegConfig(const Config config)
Definition: ads1115.cpp:111
ADS1115::ADS1115::m_config
Config m_config
The current configuration of the ADS1115.
Definition: ads1115.hpp:32
ADS1115::lo_thresh_reg_addr
constexpr std::uint8_t lo_thresh_reg_addr
Address of the low threshold register.
Definition: parameters.hpp:15
ADS1115::pga_voltage_map
const static std::unordered_map< PGA, double > pga_voltage_map
Definition: parameters.hpp:147
ADS1115::Config::mode
MODE mode
Definition: config.hpp:27
ADS1115::ADS1115::read
std::int16_t read() const
Definition: ads1115.cpp:50
ADS1115::ADS1115::m_threshold
Threshold m_threshold
The current configuration of the threshold registers.
Definition: ads1115.hpp:35