FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
Ads1115< CHANNEL_NUM > Class Template Reference

ADS1115 ADC driver with GPIO interrupt-based channel cycling. More...

#include <ads1115.hpp>

Collaboration diagram for Ads1115< CHANNEL_NUM >:

Public Member Functions

 Ads1115 (I2cDevice &i2c, Gpio &drdy, uint8_t address=DEFAULT_I2C_ADDR)
 Constructor.
 
void StartScan ()
 Starts scanning the channels and enabling data-ready interrupt.
 
void Stop ()
 Stops the scan process.
 
float GetVoltage (int channel) const
 Returns the latest voltage reading from a specific channel.
 
void EnableReadyInterruptMode ()
 Registers interrupt-based data-ready mode (comparator configuration)
 
void Display () const
 Prints voltage readings of all channels to console.
 
void RegisterChannelCallback (int channel, std::function< void(float)> cb)
 Registers a callback function for a specific channel.
 

Static Public Attributes

static constexpr uint8_t DEFAULT_I2C_ADDR = 0x48
 
static constexpr uint8_t POINTER_CONVERSION = 0x00
 
static constexpr uint8_t POINTER_CONFIG = 0x01
 

Private Member Functions

void OnDataReady ()
 Called on rising edge of ALERT/RDY pin Reads current value, stores it, calls callback, then switches to next channel.
 
void ConfigureChannel (int channel)
 Configures ADS1115 for a specific single-ended channel.
 
int16_t ReadConversion ()
 Reads the 16-bit signed conversion result.
 

Private Attributes

I2cDevicei2c_
 
Gpiodrdy_gpio_
 
uint8_t i2c_addr_
 
std::function< void(float)> callbacks_ [CHANNEL_NUM]
 
std::atomic< bool > scanning_ {false}
 
int current_channel_ = 0
 
float voltages_ [CHANNEL_NUM] = {0.0f}
 

Detailed Description

template<int CHANNEL_NUM>
class Ads1115< CHANNEL_NUM >

ADS1115 ADC driver with GPIO interrupt-based channel cycling.

Continuously scans 4 single-ended channels using ALERT/RDY pin as data-ready signal, stores voltage values in voltages_[], and allows user-defined per-channel callbacks.

Definition at line 22 of file ads1115.hpp.

Constructor & Destructor Documentation

◆ Ads1115()

template<int CHANNEL_NUM>
Ads1115< CHANNEL_NUM >::Ads1115 ( I2cDevice i2c,
Gpio drdy,
uint8_t  address = DEFAULT_I2C_ADDR 
)
inline

Constructor.

Parameters
i2cI2C device reference
drdyGPIO for ALERT/RDY pin (interrupt-enabled)
addressI2C address (default 0x48)

Definition at line 34 of file ads1115.hpp.

35 : i2c_(i2c), drdy_gpio_(drdy), i2c_addr_(address) {
37 StartScan();
38 }
void StartScan()
Starts scanning the channels and enabling data-ready interrupt.
Definition ads1115.hpp:43
void EnableReadyInterruptMode()
Registers interrupt-based data-ready mode (comparator configuration)
Definition ads1115.hpp:72

Member Function Documentation

◆ ConfigureChannel()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::ConfigureChannel ( int  channel)
inlineprivate

Configures ADS1115 for a specific single-ended channel.

Parameters
channelIndex 0~3

Definition at line 144 of file ads1115.hpp.

144 {
145 if (channel < 0 || channel > 3) {
146 return;
147 }
148
149 uint16_t config = 0;
150 config |= (0x04 + channel) << 12; // MUX: AINx vs GND
151 config |= (1 << 9); // PGA ±4.096V
152 config |= (0 << 8); // Mode: Continuous
153 config |= (0b111 << 5); // Data Rate: 860 SPS
154
155 uint8_t data[3] = {
156 POINTER_CONFIG,
157 static_cast<uint8_t>((config >> 8) & 0xFF),
158 static_cast<uint8_t>(config & 0xFF),
159 };
160
161 i2c_.WriteRaw(data, 3);
162
163 // Wait for config to take effect; discard first reading
164 std::this_thread::sleep_for(std::chrono::milliseconds(1));
166 }
int16_t ReadConversion()
Reads the 16-bit signed conversion result.
Definition ads1115.hpp:172
void WriteRaw(const uint8_t *data, size_t length)
Definition bsp_i2c.hpp:104

◆ Display()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::Display ( ) const
inline

Prints voltage readings of all channels to console.

Definition at line 85 of file ads1115.hpp.

85 {
86 for (int i = 0; i < CHANNEL_NUM; ++i) {
87 std::cout << "Channel " << i << ": " << voltages_[i] << " V ";
88 }
89 std::cout << '\n';
90 }

◆ EnableReadyInterruptMode()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::EnableReadyInterruptMode ( )
inline

Registers interrupt-based data-ready mode (comparator configuration)

Definition at line 72 of file ads1115.hpp.

72 {
73 // Set Lo_thresh = 0x0000 (MSB = 0)
74 uint8_t lo_thresh[3] = {0x02, 0x00, 0x00};
75 i2c_.WriteRaw(lo_thresh, 3);
76
77 // Set Hi_thresh = 0x8000 (MSB = 1)
78 uint8_t hi_thresh[3] = {0x03, 0x80, 0x00};
79 i2c_.WriteRaw(hi_thresh, 3);
80 }

◆ GetVoltage()

template<int CHANNEL_NUM>
float Ads1115< CHANNEL_NUM >::GetVoltage ( int  channel) const
inline

Returns the latest voltage reading from a specific channel.

Parameters
channelIndex (0 ~ CHANNEL_NUM-1)
Returns
Voltage value in volts

Definition at line 62 of file ads1115.hpp.

62 {
63 if (channel < 0 || channel >= CHANNEL_NUM) {
64 return 0.0f;
65 }
66 return voltages_[channel];
67 }

◆ OnDataReady()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::OnDataReady ( )
inlineprivate

Called on rising edge of ALERT/RDY pin Reads current value, stores it, calls callback, then switches to next channel.

Definition at line 120 of file ads1115.hpp.

120 {
121 if (!scanning_) {
122 return;
123 }
124
125 // Read and convert voltage
126 int16_t raw = ReadConversion();
127 voltages_[current_channel_] =
128 static_cast<float>(raw) * 0.000125f; // 125 µV/LSB
129
130 // Invoke user callback
131 if (callbacks_[current_channel_]) {
132 callbacks_[current_channel_](voltages_[current_channel_]);
133 }
134
135 // Switch to next channel
136 current_channel_ = (current_channel_ + 1) % CHANNEL_NUM;
137 ConfigureChannel(current_channel_);
138 }
void ConfigureChannel(int channel)
Configures ADS1115 for a specific single-ended channel.
Definition ads1115.hpp:144

◆ ReadConversion()

template<int CHANNEL_NUM>
int16_t Ads1115< CHANNEL_NUM >::ReadConversion ( )
inlineprivate

Reads the 16-bit signed conversion result.

Returns
ADC result (LSB = 125 µV at ±4.096V)

Definition at line 172 of file ads1115.hpp.

172 {
173 uint8_t pointer = POINTER_CONVERSION;
174 i2c_.WriteRaw(&pointer, 1);
175
176 uint8_t buf[2] = {};
177 i2c_.ReadRegisters(POINTER_CONVERSION, buf, 2);
178
179 return static_cast<int16_t>((buf[0] << 8) | buf[1]);
180 }
void ReadRegisters(uint8_t reg, uint8_t *buffer, size_t length)
Definition bsp_i2c.hpp:86

◆ RegisterChannelCallback()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::RegisterChannelCallback ( int  channel,
std::function< void(float)>  cb 
)
inline

Registers a callback function for a specific channel.

Parameters
channelIndex (0 ~ CHANNEL_NUM-1)
cbCallback function taking float voltage as parameter

Definition at line 97 of file ads1115.hpp.

97 {
98 if (channel >= 0 && channel < CHANNEL_NUM) {
99 callbacks_[channel] = cb;
100 }
101 }

◆ StartScan()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::StartScan ( )
inline

Starts scanning the channels and enabling data-ready interrupt.

Definition at line 43 of file ads1115.hpp.

43 {
44 scanning_ = true;
45 current_channel_ = 0;
46 ConfigureChannel(current_channel_);
47
49 [this]() { OnDataReady(); });
50 }
void OnDataReady()
Called on rising edge of ALERT/RDY pin Reads current value, stores it, calls callback,...
Definition ads1115.hpp:120
void EnableInterruptRisingEdgeWithCallback(Callback cb)
Definition bsp_gpio.hpp:109

◆ Stop()

template<int CHANNEL_NUM>
void Ads1115< CHANNEL_NUM >::Stop ( )
inline

Stops the scan process.

Definition at line 55 of file ads1115.hpp.

55{ scanning_ = false; }

Field Documentation

◆ callbacks_

template<int CHANNEL_NUM>
std::function<void(float)> Ads1115< CHANNEL_NUM >::callbacks_[CHANNEL_NUM]
private

Definition at line 109 of file ads1115.hpp.

◆ current_channel_

template<int CHANNEL_NUM>
int Ads1115< CHANNEL_NUM >::current_channel_ = 0
private

Definition at line 112 of file ads1115.hpp.

◆ DEFAULT_I2C_ADDR

template<int CHANNEL_NUM>
constexpr uint8_t Ads1115< CHANNEL_NUM >::DEFAULT_I2C_ADDR = 0x48
staticconstexpr

Definition at line 24 of file ads1115.hpp.

◆ drdy_gpio_

template<int CHANNEL_NUM>
Gpio& Ads1115< CHANNEL_NUM >::drdy_gpio_
private

Definition at line 107 of file ads1115.hpp.

◆ i2c_

template<int CHANNEL_NUM>
I2cDevice& Ads1115< CHANNEL_NUM >::i2c_
private

Definition at line 105 of file ads1115.hpp.

◆ i2c_addr_

template<int CHANNEL_NUM>
uint8_t Ads1115< CHANNEL_NUM >::i2c_addr_
private

Definition at line 108 of file ads1115.hpp.

◆ POINTER_CONFIG

template<int CHANNEL_NUM>
constexpr uint8_t Ads1115< CHANNEL_NUM >::POINTER_CONFIG = 0x01
staticconstexpr

Definition at line 26 of file ads1115.hpp.

◆ POINTER_CONVERSION

template<int CHANNEL_NUM>
constexpr uint8_t Ads1115< CHANNEL_NUM >::POINTER_CONVERSION = 0x00
staticconstexpr

Definition at line 25 of file ads1115.hpp.

◆ scanning_

template<int CHANNEL_NUM>
std::atomic<bool> Ads1115< CHANNEL_NUM >::scanning_ {false}
private

Definition at line 111 of file ads1115.hpp.

111{false};

◆ voltages_

template<int CHANNEL_NUM>
float Ads1115< CHANNEL_NUM >::voltages_[CHANNEL_NUM] = {0.0f}
private

Definition at line 113 of file ads1115.hpp.

113{0.0f};

The documentation for this class was generated from the following file: