Skip to content
Snippets Groups Projects
Commit b2b42afd authored by Daniel Joseph Antrim's avatar Daniel Joseph Antrim
Browse files

use fsr enum

parent 00bee7d0
No related branches found
No related tags found
6 merge requests!308Bring main and devel back in sync,!300playing with fixing divergence between devel and main,!287Updates for Julabo Chiller,!285Julabo Chiller Added with Fixed Pipeline Compatibility,!269Merge devel into main: largest change is code formatting checker and enforcement in CI,!225Add support for 4-channel 12-bit ADC ADS1015
Pipeline #2575399 failed
#include "ADS1015.h" #include "ADS1015.h"
// std/stl // std/stl
#include <algorithm> // std::find #include <algorithm> // std::find, std::find_if
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
...@@ -121,7 +121,7 @@ void ADS1015::configSingleShot(uint16_t& config) { ...@@ -121,7 +121,7 @@ void ADS1015::configSingleShot(uint16_t& config) {
void ADS1015::configPGA(uint16_t& config) { void ADS1015::configPGA(uint16_t& config) {
// configure programmable gain amplifier based on the configured voltage // configure programmable gain amplifier based on the configured voltage
// reference // reference
Gain gain = fsrToGainMap.at(m_fullscale_range); Gain gain = gainFromFSR(m_fullscale_range);
config |= static_cast<uint16_t>(gainToConfigMap.at(gain)); config |= static_cast<uint16_t>(gainToConfigMap.at(gain));
} }
...@@ -215,3 +215,17 @@ bool ADS1015::conversionComplete() { ...@@ -215,3 +215,17 @@ bool ADS1015::conversionComplete() {
m_com->read_reg16(static_cast<uint16_t>(Address::POINTER_CONFIG)); m_com->read_reg16(static_cast<uint16_t>(Address::POINTER_CONFIG));
return (reg & 0x8000) != 0; return (reg & 0x8000) != 0;
} }
ADS1015::Gain ADS1015::gainFromFSR(FullScaleRange range) {
auto it = std::find_if(gainToFSRMap.begin(), gainToFSRMap.end(),
[&range](const std::pair<Gain, FullScaleRange>& item) {
return item.second == range;
});
// shouldn't happen
if(it == gainToFSRMap.end()) {
throw std::runtime_error("Invalid FullScaleRange value");
}
return it->first;
}
...@@ -128,6 +128,9 @@ class ADS1015 : public ADCDevice { ...@@ -128,6 +128,9 @@ class ADS1015 : public ADCDevice {
private: private:
void init(FullScaleRange full_scale_range, std::shared_ptr<I2CCom> com); void init(FullScaleRange full_scale_range, std::shared_ptr<I2CCom> com);
std::shared_ptr<I2CCom> m_com;
std::shared_ptr<LinearCalibration> m_calibration;
enum class Address : uint16_t { enum class Address : uint16_t {
POINTER_CONVERT = 0x00, POINTER_CONVERT = 0x00,
POINTER_CONFIG = 0x01, POINTER_CONFIG = 0x01,
...@@ -213,6 +216,15 @@ class ADS1015 : public ADCDevice { ...@@ -213,6 +216,15 @@ class ADS1015 : public ADCDevice {
{Gain::xEIGHT, FullScaleRange::FSR_0_512}, {Gain::xEIGHT, FullScaleRange::FSR_0_512},
{Gain::xSIXTEEN, FullScaleRange::FSR_0_256}}; {Gain::xSIXTEEN, FullScaleRange::FSR_0_256}};
// return the ADS1015::Gain associated with ADS1015::FullScaleRange
Gain gainFromFSR(FullScaleRange range);
// map to the actual gain value associated with ADS1015::FullScaleRange enum element
const std::map<FullScaleRange, double> fsrToValMap{
{FullScaleRange::FSR_6_144, 6.144}, {FullScaleRange::FSR_4_096, 4.096},
{FullScaleRange::FSR_2_048, 2.048}, {FullScaleRange::FSR_1_024, 1.024},
{FullScaleRange::FSR_0_512, 0.512}, {FullScaleRange::FSR_0_256, 0.256}};
// map between programmable gain configuration and ADS1015 configuration // map between programmable gain configuration and ADS1015 configuration
// register values // register values
const std::map<Gain, Config> gainToConfigMap{ const std::map<Gain, Config> gainToConfigMap{
...@@ -223,21 +235,6 @@ class ADS1015 : public ADCDevice { ...@@ -223,21 +235,6 @@ class ADS1015 : public ADCDevice {
{Gain::xEIGHT, Config::CONFIG_PGA_8}, {Gain::xEIGHT, Config::CONFIG_PGA_8},
{Gain::xSIXTEEN, Config::CONFIG_PGA_16}}; {Gain::xSIXTEEN, Config::CONFIG_PGA_16}};
const std::map<FullScaleRange, double> fsrToValMap{
{FullScaleRange::FSR_6_144, 6.144}, {FullScaleRange::FSR_4_096, 4.096},
{FullScaleRange::FSR_2_048, 2.048}, {FullScaleRange::FSR_1_024, 1.024},
{FullScaleRange::FSR_0_512, 0.512}, {FullScaleRange::FSR_0_256, 0.256}};
const std::map<FullScaleRange, Gain> fsrToGainMap{
{FullScaleRange::FSR_6_144, Gain::xTWOTHIRDS},
{FullScaleRange::FSR_4_096, Gain::xONE},
{FullScaleRange::FSR_2_048, Gain::xTWO},
{FullScaleRange::FSR_1_024, Gain::xFOUR},
{FullScaleRange::FSR_0_512, Gain::xEIGHT},
{FullScaleRange::FSR_0_256, Gain::xSIXTEEN}};
std::shared_ptr<I2CCom> m_com;
std::shared_ptr<LinearCalibration> m_calibration;
// Note that for ADS1015, single-ended mode has 11-bit resolution (max val = // Note that for ADS1015, single-ended mode has 11-bit resolution (max val =
// 0x7ff). Only when performing differential measurements do you get the // 0x7ff). Only when performing differential measurements do you get the
...@@ -248,7 +245,6 @@ class ADS1015 : public ADCDevice { ...@@ -248,7 +245,6 @@ class ADS1015 : public ADCDevice {
// some helper methods // some helper methods
bool conversionComplete(); bool conversionComplete();
Gain fsrToGain(FullScaleRange fsr);
// methods for configuring the ADS1015 // methods for configuring the ADS1015
void configSingleShot(uint16_t& config); void configSingleShot(uint16_t& config);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment