ads1115  0.2.0
ads1115.cpp
Go to the documentation of this file.
1 // ADS1115
2 #include "ads1115/ads1115.hpp"
3 
4 #include "ads1115/config.hpp"
5 #include "ads1115/parameters.hpp"
6 
7 // i2c-device
8 #include "i2c-device/device.hpp"
9 #include "i2c-device/util.hpp"
10 
11 // stl
12 #include <bit>
13 #include <chrono>
14 #include <cstdint>
15 #include <limits>
16 #include <stdexcept>
17 #include <thread>
18 
19 namespace ADS1115
20 {
21  ADS1115::ADS1115(const int adapter_nr, const ADDR addr)
22  : m_device(i2c::AdapterNumber(adapter_nr), i2c::DeviceID(static_cast<int>(addr))),
23  m_addr(addr)
24  {
25  }
26 
27  ADS1115::ADS1115(ADS1115&& other) noexcept
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  }
34 
35  ADS1115& ADS1115::operator=(ADS1115&& other) noexcept
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  }
44 
45  ADDR ADS1115::getADDR() const
46  {
47  return m_addr;
48  }
49 
50  std::int16_t ADS1115::read() const
51  {
52  if (m_config.mode == MODE::SINGLE_CONV) {
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  }
68 
69  double ADS1115::readVoltage() const
70  {
71  return toVoltage(read());
72  }
73 
74  void ADS1115::reset()
75  {
76  Config config {};
77  setRegConfig(config);
78 
79  Threshold threshold {};
80  setRegThreshold(threshold);
81  }
82 
83  double ADS1115::toVoltage(const std::int16_t value) const
84  {
85  return value * pga_voltage_map.at(m_config.pga) / std::numeric_limits<std::int16_t>::max();
86  }
87 
88  std::int16_t ADS1115::fromVoltage(const double value) const
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  }
93 
94  /*******************
95  * Config register *
96  *******************/
97 
98  Config ADS1115::readRegConfig()
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  }
105 
106  Config ADS1115::getRegConfig() const
107  {
108  return m_config;
109  }
110 
111  void ADS1115::setRegConfig(const Config config)
112  {
113  m_device.write_word_data(conf_reg_addr, m_config.to_bytes());
114  m_config = config;
115  }
116 
117  /**********************
118  * Threshold register *
119  **********************/
120 
121  Threshold ADS1115::readRegThreshold()
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  }
134 
135  Threshold ADS1115::getRegThreshold() const
136  {
137  return m_threshold;
138  }
139 
140  void ADS1115::setRegThreshold(const Threshold threshold)
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  }
150 
151 } // namespace ADS1115
ADS1115::conv_reg_addr
constexpr std::uint8_t conv_reg_addr
Address of the conversion register.
Definition: parameters.hpp:11
ADS1115
Definition: ads1115.hpp:16
ADS1115::Threshold::getLow
std::int16_t getLow() const
Definition: threshold.cpp:35
parameters.hpp
ADS1115::Threshold
Definition: threshold.hpp:12
config.hpp
ADS1115::ADS1115
Definition: ads1115.hpp:23
ADS1115::conf_reg_addr
constexpr std::uint8_t conf_reg_addr
Address of the config register.
Definition: parameters.hpp:13
ADS1115::hi_thresh_reg_addr
constexpr std::uint8_t hi_thresh_reg_addr
Address of the high threshold register.
Definition: parameters.hpp:17
ADS1115::MODE::SINGLE_CONV
@ SINGLE_CONV
Sets the ADS1115 to power-down single-shot mode.
ADS1115::Threshold::getHigh
std::int16_t getHigh() const
Definition: threshold.cpp:31
ADS1115::Config
Definition: config.hpp:20
ADS1115::lo_thresh_reg_addr
constexpr std::uint8_t lo_thresh_reg_addr
Address of the low threshold register.
Definition: parameters.hpp:15
ads1115.hpp
ADS1115::pga_voltage_map
const static std::unordered_map< PGA, double > pga_voltage_map
Definition: parameters.hpp:147
ADS1115::ADDR
ADDR
This Enum describes the possible i2c bus addresses of a ADS1115.
Definition: parameters.hpp:20