rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes
Ads1015 Class Reference

#include <ads1015.h>

Collaboration diagram for Ads1015:
Collaboration graph
[legend]

Public Member Functions

bool init (brain_pin_e scl, brain_pin_e sda)
 
void readChannels (float(&result)[4])
 

Private Member Functions

float readChannel (uint8_t ch)
 
void writeReg (uint8_t reg, uint16_t data)
 
uint16_t readReg (uint8_t reg)
 

Private Attributes

bool m_hasInit = false
 
BitbangI2c m_i2c
 

Detailed Description

Definition at line 5 of file ads1015.h.

Member Function Documentation

◆ init()

bool Ads1015::init ( brain_pin_e  scl,
brain_pin_e  sda 
)

Definition at line 21 of file ads1015.cpp.

21 {
22 if (!m_i2c.init(scl, sda)) {
23 return false;
24 }
25
26 // ADS1015 has no ID register - so we read the Lo_thresh instead
27 uint16_t loThresh = readReg(ADS1015_LO_THRESH);
28
29 if (loThresh != 0x8000) {
30 return false;
31 }
32
33 m_hasInit = true;
34 return true;
35}
BitbangI2c m_i2c
Definition ads1015.h:17
uint16_t readReg(uint8_t reg)
Definition ads1015.cpp:72
bool m_hasInit
Definition ads1015.h:16
bool init(brain_pin_e scl, brain_pin_e sda)
Definition i2c_bb.cpp:37
Here is the call graph for this function:

◆ readChannel()

float Ads1015::readChannel ( uint8_t  ch)
private

Definition at line 43 of file ads1015.cpp.

43 {
44 // set the channel
45 // set to +-6.144v full scale, fastest sampling, manual conversion start
46 writeReg(ADS1015_CONFIG, 0xC1E0 | channel << 12);
47
48 // Wait for conversion to complete
49 // Bit is cleared while conversion is ongoing, set when done
50 while ((readReg(ADS1015_CONFIG) & 0x8000) == 0) ;
51
52 // Read the result
53 int16_t result = readReg(ADS1015_CONV);
54 // Result is 12 bits left aligned, so right align the result
55 result = result >> 4;
56
57 // 2048 counts = positive 6.144 volts
58 constexpr float ratio = 6.144f / 2048;
59 return result * ratio;
60}
uint16_t channel
Definition adc_inputs.h:104
void writeReg(uint8_t reg, uint16_t data)
Definition ads1015.cpp:62

Referenced by readChannels().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readChannels()

void Ads1015::readChannels ( float(&)  result[4])

Definition at line 37 of file ads1015.cpp.

37 {
38 for (size_t i = 0; i < 4; i++) {
39 result[i] = readChannel(i);
40 }
41}
float readChannel(uint8_t ch)
Definition ads1015.cpp:43
Here is the call graph for this function:

◆ readReg()

uint16_t Ads1015::readReg ( uint8_t  reg)
private

Definition at line 72 of file ads1015.cpp.

72 {
73 uint16_t res;
74 m_i2c.write(addr, &reg, 1);
75 m_i2c.read(addr, reinterpret_cast<uint8_t*>(&res), 2);
76 return SWAP_UINT16(res);
77}
constexpr uint8_t addr
Definition ads1015.cpp:14
void read(uint8_t addr, uint8_t *data, size_t size)
Definition i2c_bb.cpp:210
void write(uint8_t addr, const uint8_t *data, size_t size)
Definition i2c_bb.cpp:190
uint16_t SWAP_UINT16(uint16_t x)
Definition efilib.h:22

Referenced by init(), and readChannel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeReg()

void Ads1015::writeReg ( uint8_t  reg,
uint16_t  data 
)
private

Definition at line 62 of file ads1015.cpp.

62 {
63 uint8_t packet[3];
64
65 packet[0] = reg;
66 packet[1] = data >> 8;
67 packet[2] = data & 0xFF;
68
69 m_i2c.write(addr, packet, 3);
70}

Referenced by readChannel().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ m_hasInit

bool Ads1015::m_hasInit = false
private

Definition at line 16 of file ads1015.h.

Referenced by init().

◆ m_i2c

BitbangI2c Ads1015::m_i2c
private

Definition at line 17 of file ads1015.h.

Referenced by init(), readReg(), and writeReg().


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