11#include "bsp_gpio.hpp"
21template <
int CHANNEL_NUM>
24 static constexpr uint8_t DEFAULT_I2C_ADDR = 0x48;
25 static constexpr uint8_t POINTER_CONVERSION = 0x00;
26 static constexpr uint8_t POINTER_CONFIG = 0x01;
35 : i2c_(i2c), drdy_gpio_(drdy), i2c_addr_(address) {
55 void Stop() { scanning_ =
false; }
63 if (channel < 0 || channel >= CHANNEL_NUM) {
66 return voltages_[channel];
74 uint8_t lo_thresh[3] = {0x02, 0x00, 0x00};
78 uint8_t hi_thresh[3] = {0x03, 0x80, 0x00};
86 for (
int i = 0; i < CHANNEL_NUM; ++i) {
87 std::cout <<
"Channel " << i <<
": " << voltages_[i] <<
" V ";
98 if (channel >= 0 && channel < CHANNEL_NUM) {
99 callbacks_[channel] = cb;
109 std::function<void(
float)> callbacks_[CHANNEL_NUM];
111 std::atomic<bool> scanning_{
false};
112 int current_channel_ = 0;
113 float voltages_[CHANNEL_NUM] = {0.0f};
127 voltages_[current_channel_] =
128 static_cast<float>(raw) * 0.000125f;
131 if (callbacks_[current_channel_]) {
132 callbacks_[current_channel_](voltages_[current_channel_]);
136 current_channel_ = (current_channel_ + 1) % CHANNEL_NUM;
145 if (channel < 0 || channel > 3) {
150 config |= (0x04 + channel) << 12;
153 config |= (0b111 << 5);
157 static_cast<uint8_t
>((config >> 8) & 0xFF),
158 static_cast<uint8_t
>(config & 0xFF),
164 std::this_thread::sleep_for(std::chrono::milliseconds(1));
173 uint8_t pointer = POINTER_CONVERSION;
179 return static_cast<int16_t
>((buf[0] << 8) | buf[1]);
ADS1115 ADC driver with GPIO interrupt-based channel cycling.
float GetVoltage(int channel) const
Returns the latest voltage reading from a specific channel.
void Display() const
Prints voltage readings of all channels to console.
void ConfigureChannel(int channel)
Configures ADS1115 for a specific single-ended channel.
void StartScan()
Starts scanning the channels and enabling data-ready interrupt.
Ads1115(I2cDevice &i2c, Gpio &drdy, uint8_t address=DEFAULT_I2C_ADDR)
Constructor.
void Stop()
Stops the scan process.
int16_t ReadConversion()
Reads the 16-bit signed conversion result.
void EnableReadyInterruptMode()
Registers interrupt-based data-ready mode (comparator configuration)
void RegisterChannelCallback(int channel, std::function< void(float)> cb)
Registers a callback function for a specific channel.
void OnDataReady()
Called on rising edge of ALERT/RDY pin Reads current value, stores it, calls callback,...
void EnableInterruptRisingEdgeWithCallback(Callback cb)
void WriteRaw(const uint8_t *data, size_t length)
void ReadRegisters(uint8_t reg, uint8_t *buffer, size_t length)