MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
adc.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "adc.h"
16 
17 #include "utility.h"
18 
19 ADC::ADC() : i2c_bus_(constants::adc_bus_num) {}
20 
21 void ADC::begin(int addr, int sda_pin, int scl_pin)
22 {
23  i2c_bus_.begin(sda_pin, scl_pin);
24  ads_.begin(addr, &i2c_bus_);
25  ads_.setGain(
26  GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
27  // ads_.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
28  // ads_.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
29  // ads_.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
30  // ads_.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
31  // ads_.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
32 }
33 
34 int16_t ADC::read(ADCChannel ch)
35 {
36  return ads_.readADC_SingleEnded((int16_t)ch);
37 }
38 
39 float ADC::read_voltage(ADCChannel ch) { return ads_.computeVolts(read(ch)); }
ADCChannel
Definition: adc.h:25
void begin(int addr, int sda_pin, int scl_pin)
Definition: adc.cpp:21
ADC()
Definition: adc.cpp:19
float read_voltage(ADCChannel ch)
Read voltage from ADC channel (computes voltage using the set fixed gain)
Definition: adc.cpp:39
int16_t read(ADCChannel ch)
Read ADC value from channel.
Definition: adc.cpp:34