Analog-to-digital converter for analog I2C sensors. More...
#include <sg_103020013_I2CADC.hpp>
Public Types | |
enum | ERegister { ERegister::kConversionResult = 0, ERegister::kAlertStatus = 1, ERegister::kConfiguration = 2, ERegister::kLowLimit = 3, ERegister::kHighLimit = 4, ERegister::kHysteresis = 5, ERegister::kLowestConversion = 6, ERegister::kHighestConversion = 7 } |
I2CADC registers. More... | |
typedef uint8_t | I2CBus |
I2C bus. More... | |
typedef uint8_t | I2CAddress |
I2C addresses. More... | |
typedef uint8_t | I2CRegister |
I2C register. More... | |
typedef std::vector< uint8_t > | I2CBlock |
Block of sequential bytes to read or write. More... | |
Public Member Functions | |
virtual | ~I2CADC (void) |
Destructor. More... | |
I2CADC (const std::string &n="", const I2CAddress addr=0) | |
Constructor. More... | |
I2CADC & | reset (void) |
Reset the I2CADC configuration. More... | |
I2CADC & | disable_automatic_mode (void) |
The I2CADC can be configured to perform auto conversions. More... | |
I2CADC & | enable_automatic_mode (const uint8_t value=1) |
The I2CADC can be configured to perform periodic auto conversions. More... | |
uint16_t | get_result (void) |
Read the most recent 12-bit analog-to-digital conversion result register (0x00). More... | |
uint16_t | get_minimum_result (void) |
Return the lowest conversion result seen since the I2CADC was last reset. More... | |
uint16_t | get_maximum_result (void) |
Return the highest conversion result seen since the I2CADC was last reset. More... | |
I2CADC & | reset_minimum_result (void) |
Reset the minimum conversion value stored in register 0x06 when automatic conversion mode is enabled. More... | |
I2CADC & | reset_maximum_result (void) |
Reset the maximum conversion value stored in register 0x07 when automatic conversion mode is enabled. More... | |
uint16_t | get_and_reset_minimum_result (void) |
Get the minimum conversion result and reset the register. More... | |
uint16_t | get_and_reset_maximum_result (void) |
Get the maximum conversion result and reset the register. More... | |
virtual bool | is_analog (void) const |
This is always false for GroveI2CDigital. More... | |
virtual bool | is_digital (void) const |
This is always true for GroveI2CDigital. More... | |
I2CAddress | get_address_from_type (void) const |
Get the address for this Grove type. More... | |
virtual uint8_t | read8 (const I2CRegister reg) |
Read a byte (8 bits) from the specificed register. More... | |
virtual uint16_t | read16 (const I2CRegister reg) |
Read a 16-bit word from the specified register. More... | |
virtual uint16_t | read12 (const I2CRegister reg) |
Read a 16-bit word from the specified register, but only keep the bottom 12 bits. More... | |
virtual I2CBlock | read_block (const I2CRegister reg) |
Read a consecutive block of bytes, anywhere between 1 and 32 bytes in length. More... | |
virtual GroveI2CDigital & | write_byte (const uint8_t value) |
Write a single byte to the I2C bus. More... | |
virtual GroveI2CDigital & | write_block (const I2CRegister reg, const I2CBlock &v) |
Write a consecutive block of bytes, anywhere between 1 and 32 bytes in length. More... | |
virtual GroveI2CDigital & | write_block (const I2CBlock &v) |
Write a consecutive block of bytes directly to the I2C file handle. More... | |
virtual bool | is_grove_uart (void) const |
This is always false for GroveI2C. More... | |
virtual bool | is_grove_i2c (void) const |
This is always true for GroveI2C. More... | |
virtual bool | is_grove_gpio (void) const |
This is always false for GroveI2C. More... | |
virtual uint32_t | get_sku (void) const |
Return a guess as to the Seeed SKU number based on the grove type. More... | |
virtual std::string | get_type_name (void) const |
Return a short text string representing the grove type. More... | |
virtual EGroveType | get_type (void) const |
Return the grove type. More... | |
virtual std::string | get_name (void) const |
Return the user-supplied name. More... | |
virtual std::string | get_description (void) const |
Return a multi-word 1-line description of this object. More... | |
bool | operator== (const I2CADC &rhs) const |
Test object equality. More... | |
bool | operator!= (const I2CADC &rhs) const |
Test object equality. More... | |
virtual uint8_t | read8 (const ERegister reg) |
For convenience, read + write using the register enum. More... | |
virtual uint16_t | read12 (const ERegister reg) |
For convenience, read + write using the register enum. More... | |
virtual I2CADC & | write8 (const ERegister reg, const uint8_t value) |
For convenience, read + write using the register enum. More... | |
virtual I2CADC & | write16 (const ERegister reg, const uint16_t value) |
For convenience, read + write using the register enum. More... | |
virtual GroveI2CDigital & | write8 (const I2CRegister reg, const uint8_t value) |
Write the given value to the specified register. More... | |
virtual GroveI2CDigital & | write16 (const I2CRegister reg, const uint16_t value) |
bool | operator== (const GroveBase &rhs) const |
Test object equality. More... | |
bool | operator!= (const GroveBase &rhs) const |
Test object equality. More... | |
Static Public Member Functions | |
static I2CBus | detect_i2c_bus_number (const bool force_detect=false, const int force_bus_number=-1) |
Detect the I2C bus to use to talk to Grove I2C devices. More... | |
static I2CAddress | get_address_from_type (const EGroveType &type) |
Get the default I2C address for a given type. More... | |
Public Attributes | |
I2CBus | i2c_bus_number |
The bus number to use for I2C Grove twigs. More... | |
I2CAddress | i2c_address |
The I2C address used to communicate with the Grove device. More... | |
int | i2c_file_handle |
The file handle to /dev/i2c-* used to communicate with the Grove device. More... | |
EGroveType | grove_type |
Grove type. More... | |
std::string | grove_name |
User-supplied name. More... | |
Analog-to-digital converter for analog I2C sensors.
Apparently, the default I2C address for this twig used to be 0x55
. This conflicts with the BeagleBone Green EEPROM which also uses i2c-1
addresses 0x54
through 0x57
.
For example, do not connect any I2C Groves twigs and run the i2cdetect
command:
If your ADC indicates it is using address 0x55
, then locate the labels for ADDR0
and ADDR1
, along with H
and L
at the bottom of the ADC. You'll need to cut the trace to use a different address. See http://www.seeedstudio.com/wiki/Grove_-_I2C_ADC#With_Beaglebone_Green for details.
Connect the ADC to the left-hand J4
I2C grove interface, and run the i2cdetect
command to find the address of your ADC:
In the above example, the ADC is at address 0x50
.
Description | Image |
---|---|
I2C ADC with a default address of 0x50 . | |
I2C ADC with a temperature sensor.J1 connects to the BeagleBone Green, and J2 connects to the analog I2C twig. |
|
inherited |
I2C addresses.
|
inherited |
Block of sequential bytes to read or write.
|
inherited |
I2C bus.
|
inherited |
I2C register.
|
strong |
I2CADC registers.
Enumerator | |
---|---|
kConversionResult |
0 (this register is read-only) |
kAlertStatus |
1 |
kConfiguration |
2 |
kLowLimit |
3 |
kHighLimit |
4 |
kHysteresis |
5 |
kLowestConversion |
6 |
kHighestConversion |
7 |
|
virtual |
Destructor.
SG::I2CADC::I2CADC | ( | const std::string & | n = "" , |
const I2CAddress | addr = 0 |
||
) |
Constructor.
|
staticinherited |
Detect the I2C bus to use to talk to Grove I2C devices.
This is normally 1
or 2
. The detection consists of looking at /dev/i2c* devices.
[in] | force_detect | The detected bus number is usually remembered to prevent having to re-detect it every time a new GroveI2C object is instantiated. If force_detect is set to true , then this will force GroveI2C to ignore the previously cached bus number and re-detect the I2C bus. |
[in] | force_bus_number | If an explicit bus number should be used, then force_bus_number can be set to the correct bus value. This will prevent calls to detect_i2c_bus_number() from attempting to auto-detect the I2C bus. Setting the I2C bus number this way wont change existing GroveI2C objects that have already been instantiated. Only new objects will use the new bus number. The actual bus number used by specific objects is i2c_bus_number. |
std::system_error | if /dev cannot be opened. |
std::runtime_error | if the I2C bus cannot be found in /dev. |
Referenced by SG::GroveI2CDigital::is_digital().
|
inline |
The I2CADC can be configured to perform auto conversions.
When automatic mode is disabled, the I2CADC can enter low-power mode, and a conversion is only started whenever the result register (register #0) is read.
Disabling automatic mode is equivalent to calling enable_automatic_mode(0)
.
References enable_automatic_mode().
SG::I2CADC & SG::I2CADC::enable_automatic_mode | ( | const uint8_t | value = 1 | ) |
The I2CADC can be configured to perform periodic auto conversions.
When enabled, the I2CADC will not go into low-power mode, but instead will periodically perform analog-to-digital conversions, set alerts if necessary, remember high and low values, and store the most recent result into the conversion result register.
[in] | value | Determines the conversion interval. Must be between 0 and 7 (total of 3 bits). When auto-conversion is enabled, the I2CADC can be configured to perform between 0.4 and 27 thousands samples per second (ksps). |
Value | Binary | Meaning |
---|---|---|
0 | 0b0000 | automatic conversion mode is disabled. |
1 | 0b0001 | cycle time x 32 (27 ksps) |
2 | 0b0010 | cycle time x 64 (13.5 ksps) |
3 | 0b0011 | cycle time x 128 (6.7 ksps) |
4 | 0b0100 | cycle time x 256 (3.4 ksps) |
5 | 0b0101 | cycle time x 512 (1.7 ksps) |
6 | 0b0110 | cycle time x 1024 (0.9 ksps) |
7 | 0b0111 | cycle time x 2048 (0.4 ksps) |
References kConfiguration, read8(), and write8().
Referenced by disable_automatic_mode(), and temperature().
|
staticinherited |
Get the default I2C address for a given type.
Note that several Grove types have multiple addresses. This only returns the first address.
References SG::k3AxisDigitalAccelerometer16g, SG::k3AxisDigitalAccelerometer1_5g, SG::k3AxisDigitalAccelerometer400g, SG::k3AxisDigitalCompass, SG::k3AxisDigitalGyro, SG::k6AxisAccelerometerAndCompassV1, SG::k6AxisAccelerometerAndCompassV2, SG::kBarometerHighAccuracy, SG::kBarometerSensor, SG::kBarometerSensorBMP180, SG::kDigitalInfraredTemperatureSensor, SG::kDigitalLightSensor, SG::kGestureV1, SG::kI2CADC, SG::kI2CColorSensor, SG::kI2CFMReceiver, SG::kI2CMotorDriverV10, SG::kI2CMotorDriverV12, SG::kIMU10DOF, SG::kIMU9DOFV1, SG::kIMU9DOFV2, SG::kLCDRGBBacklight, SG::kMultichannelGasSensor, SG::kNFCTag, SG::kOLEDDisplay0_96, SG::kOLEDDisplay1_12, SG::kQTouchSensor, SG::kRTC, SG::kTemperatureAndHumiditySensor, and SG::kTouchSensor.
|
inlineinherited |
Get the address for this Grove type.
References SG::GroveI2CDigital::get_address_from_type(), SG::GroveBase::get_type(), SG::GroveI2CDigital::read16(), and SG::GroveI2CDigital::read8().
Referenced by SG::GroveI2CDigital::get_address_from_type(), SG::GroveI2CDigital::GroveI2CDigital(), and SG::GroveI2CDigital::is_digital().
|
inline |
Get the maximum conversion result and reset the register.
References get_maximum_result(), read12(), read8(), reset_maximum_result(), write16(), and write8().
|
inline |
Get the minimum conversion result and reset the register.
References get_minimum_result(), and reset_minimum_result().
|
virtualinherited |
Return a multi-word 1-line description of this object.
References SG::demangle(), SG::GroveBase::get_name(), SG::GroveBase::get_sku(), SG::GroveBase::get_type_name(), SG::GroveBase::is_grove_gpio(), SG::GroveBase::is_grove_i2c(), SG::GroveBase::is_grove_uart(), and SG::BeagleBone::Detect::to_string().
Referenced by buzzer(), chainable_rgb_led(), common_oled(), SG::GroveBase::get_name(), SG::GroveI2CDigital::GroveI2CDigital(), SG::GroveI2CDigital::read16(), SG::GroveI2CDigital::read8(), SG::GroveI2CDigital::read_block(), SG::CommonOLED::set_display_bitmap(), SG::CommonOLED::set_orientation(), temperature(), variable_led(), SG::GroveI2CDigital::write16(), SG::GroveI2CDigital::write8(), SG::GroveI2CDigital::write_block(), and SG::GroveI2CDigital::write_byte().
|
inline |
Return the highest conversion result seen since the I2CADC was last reset.
This is the 12-bit value stored in register Vmin (0x06).
References kHighestConversion, and read12().
Referenced by get_and_reset_maximum_result().
|
inline |
Return the lowest conversion result seen since the I2CADC was last reset.
This is the 12-bit value stored in register Vmin (0x06).
References kLowestConversion, and read12().
Referenced by get_and_reset_minimum_result().
|
inlinevirtualinherited |
Return the user-supplied name.
References SG::GroveBase::get_description(), SG::GroveBase::grove_name, SG::GroveBase::is_grove_gpio(), SG::GroveBase::is_grove_i2c(), and SG::GroveBase::is_grove_uart().
Referenced by SG::GroveBase::get_description(), SG::GroveBase::operator=(), and SG::GroveBase::operator==().
|
inline |
Read the most recent 12-bit analog-to-digital conversion result register (0x00).
If automatic mode is disabled, this will force the I2CADC to immediatelly perform a conversion.
When you have an analog Grove twig connected to an I2CADC, this is the method to call to read the value from the connected Grove. The value returned will only have 12 bits, even though the C++ type is uint16_t
. This is because the I2CADC specifically stores only 12 bits. This means the result will be between 0 and 4095 (0x0fff).
read16(0x00)
instead to get access to bit D15
. Calling get_result() will mask out the upper 4 bits, including the alert flag. References kConversionResult, and read12().
Referenced by SG::TemperatureSensor::get_celsius().
|
inlinevirtualinherited |
Return a guess as to the Seeed SKU number based on the grove type.
References SG::get_sku_from_grove_type(), and SG::GroveBase::grove_type.
Referenced by SG::GroveBase::get_description().
|
inlinevirtualinherited |
Return the grove type.
References SG::GroveBase::grove_type.
Referenced by SG::GroveI2CDigital::get_address_from_type(), SG::GroveBase::operator=(), and SG::GroveBase::operator==().
|
inlinevirtualinherited |
Return a short text string representing the grove type.
References SG::GroveBase::grove_type, and SG::to_string().
Referenced by SG::GroveBase::get_description().
|
inlinevirtualinherited |
This is always false
for GroveI2CDigital.
Implements SG::GroveI2C.
|
inlinevirtualinherited |
This is always true
for GroveI2CDigital.
Implements SG::GroveI2C.
References SG::GroveI2CDigital::detect_i2c_bus_number(), and SG::GroveI2CDigital::get_address_from_type().
|
inlinevirtualinherited |
This is always false
for GroveI2C.
Implements SG::GroveBase.
References SG::GroveI2C::is_analog(), and SG::GroveI2C::is_digital().
|
inlinevirtualinherited |
This is always true
for GroveI2C.
Implements SG::GroveBase.
|
inlinevirtualinherited |
This is always false
for GroveI2C.
Implements SG::GroveBase.
|
inlineinherited |
Test object equality.
References SG::GroveBase::operator==().
|
inline |
Test object equality.
References operator==(), and reset().
|
inherited |
Test object equality.
References SG::GroveBase::get_name(), and SG::GroveBase::get_type().
Referenced by SG::GroveBase::operator!=(), SG::Buzzer::operator==(), SG::TemperatureAndHumiditySensor::operator==(), SG::TemperatureSensor::operator==(), and operator==().
bool SG::I2CADC::operator== | ( | const I2CADC & | rhs | ) | const |
Test object equality.
References SG::GroveI2CDigital::i2c_address, and SG::GroveBase::operator==().
Referenced by operator!=().
|
inlinevirtualinherited |
Read a 16-bit word from the specified register, but only keep the bottom 12 bits.
Some devices such as the I2CADC return 12-bit values.
References SG::GroveI2CDigital::read16(), SG::GroveI2CDigital::read_block(), SG::GroveI2CDigital::write16(), SG::GroveI2CDigital::write8(), SG::GroveI2CDigital::write_block(), and SG::GroveI2CDigital::write_byte().
Referenced by read12().
|
virtual |
For convenience, read + write using the register enum.
References SG::GroveI2CDigital::read12().
Referenced by get_and_reset_maximum_result(), get_maximum_result(), get_minimum_result(), and get_result().
|
virtualinherited |
Read a 16-bit word from the specified register.
std::system_error | if the register cannot be read. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, and SG::GroveI2CDigital::i2c_file_handle.
Referenced by SG::GroveI2CDigital::get_address_from_type(), and SG::GroveI2CDigital::read12().
|
virtualinherited |
Read a byte (8 bits) from the specificed register.
std::system_error | if the register cannot be read. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, and SG::GroveI2CDigital::i2c_file_handle.
Referenced by SG::GroveI2CDigital::get_address_from_type(), SG::ThreeAxisDigitalCompass::get_config(), SG::ThreeAxisDigitalCompass::get_data(), SG::ThreeAxisDigitalCompass::get_mode(), SG::ThreeAxisDigitalCompass::get_status(), and read8().
|
virtual |
For convenience, read + write using the register enum.
References SG::GroveI2CDigital::read8().
Referenced by enable_automatic_mode(), and get_and_reset_maximum_result().
|
virtualinherited |
Read a consecutive block of bytes, anywhere between 1 and 32 bytes in length.
std::system_error | if the register cannot be read. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, and SG::GroveI2CDigital::i2c_file_handle.
Referenced by SG::GroveI2CDigital::read12().
SG::I2CADC & SG::I2CADC::reset | ( | void | ) |
Reset the I2CADC configuration.
This stores a default value into every register, similar to when it is first powered on.
The exact default values for each register are described in the ADC121C021 datasheet, pages 18-22.
References kAlertStatus, kConfiguration, kHighestConversion, kHighLimit, kHysteresis, kLowestConversion, kLowLimit, write16(), and write8().
Referenced by operator!=().
|
inline |
Reset the maximum conversion value stored in register 0x07 when automatic conversion mode is enabled.
References kHighestConversion, and write16().
Referenced by get_and_reset_maximum_result().
|
inline |
Reset the minimum conversion value stored in register 0x06 when automatic conversion mode is enabled.
References kLowestConversion, and write16().
Referenced by get_and_reset_minimum_result().
|
virtualinherited |
std::system_error | if the value cannot be written. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, and SG::GroveI2CDigital::i2c_file_handle.
Referenced by SG::GroveI2CDigital::read12(), and write16().
|
virtual |
For convenience, read + write using the register enum.
References SG::GroveI2CDigital::write16().
Referenced by get_and_reset_maximum_result(), reset(), reset_maximum_result(), and reset_minimum_result().
|
virtualinherited |
Write the given value to the specified register.
std::system_error | if the value cannot be written. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, and SG::GroveI2CDigital::i2c_file_handle.
Referenced by SG::GroveI2CDigital::read12(), SG::ThreeAxisDigitalCompass::set_config(), SG::ThreeAxisDigitalCompass::set_mode(), and write8().
|
virtual |
For convenience, read + write using the register enum.
References SG::GroveI2CDigital::write8().
Referenced by enable_automatic_mode(), get_and_reset_maximum_result(), and reset().
|
virtualinherited |
Write a consecutive block of bytes, anywhere between 1 and 32 bytes in length.
std::length_error | if the block is too long to write. |
std::system_error | if the block failed to write. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, SG::GroveI2CDigital::i2c_file_handle, and SG::BeagleBone::Detect::to_string().
Referenced by SG::CommonOLED::flood(), SG::GroveI2CDigital::read12(), SG::OLED096::reset(), SG::CommonOLED::send_command(), SG::CommonOLED::set_display_bitmap(), SG::CommonOLED::set_orientation(), and SG::CommonOLED::show().
|
virtualinherited |
Write a consecutive block of bytes directly to the I2C file handle.
std::system_error | if the block failed to write. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, SG::GroveI2CDigital::i2c_file_handle, and SG::BeagleBone::Detect::to_string().
|
virtualinherited |
Write a single byte to the I2C bus.
std::system_error | if the value cannot be writen. |
References SG::GroveBase::get_description(), SG::GroveI2CDigital::i2c_address, and SG::GroveI2CDigital::i2c_file_handle.
Referenced by SG::OLED096::all_off_display(), SG::OLED096::all_on_display(), SG::OLED096::invert_display(), SG::OLED096::normal_display(), SG::GroveI2CDigital::read12(), and SG::CommonOLED::set_display_bitmap().
|
inherited |
User-supplied name.
Referenced by SG::GroveBase::get_name(), and SG::GroveBase::operator=().
|
inherited |
Grove type.
Referenced by SG::GroveBase::get_sku(), SG::GroveBase::get_type(), SG::GroveBase::get_type_name(), and SG::GroveBase::operator=().
|
inherited |
The I2C address used to communicate with the Grove device.
Referenced by SG::GroveI2CDigital::GroveI2CDigital(), operator==(), SG::GroveI2CDigital::read16(), SG::GroveI2CDigital::read8(), SG::GroveI2CDigital::read_block(), SG::GroveI2CDigital::write16(), SG::GroveI2CDigital::write8(), SG::GroveI2CDigital::write_block(), and SG::GroveI2CDigital::write_byte().
|
inherited |
The bus number to use for I2C Grove twigs.
The exposed I2C bus is usually 1
or 2
. This is normally auto-detected by detect_i2c_bus_number() called by the constructor, and doesn't need to be manually set.
Referenced by SG::GroveI2CDigital::GroveI2CDigital().
|
inherited |
The file handle to /dev/i2c-* used to communicate with the Grove device.
Referenced by SG::GroveI2CDigital::GroveI2CDigital(), SG::GroveI2CDigital::read16(), SG::GroveI2CDigital::read8(), SG::GroveI2CDigital::read_block(), SG::GroveI2CDigital::write16(), SG::GroveI2CDigital::write8(), SG::GroveI2CDigital::write_block(), SG::GroveI2CDigital::write_byte(), and SG::GroveI2CDigital::~GroveI2CDigital().