Added offset parameter to Analog, and fixed it not switching between ADC pins

This commit is contained in:
ZodiusInfuser 2022-03-25 23:31:27 +00:00
parent 41d55b1115
commit 1b79e85be7
2 changed files with 7 additions and 3 deletions

View File

@ -2,11 +2,14 @@
namespace pimoroni {
uint16_t Analog::read_raw() {
adc_select_input(pin - 26);
return adc_read();
}
float Analog::read_voltage() {
return ((float)adc_read() * 3.3f) / (1 << 12) / amplifier_gain;
adc_select_input(pin - 26);
float voltage = ((((float)adc_read() * 3.3f) / (1 << 12)) + offset) / amplifier_gain;
return MAX(voltage, 0.0f);
}
float Analog::read_current() {

View File

@ -9,8 +9,8 @@ namespace pimoroni {
class Analog {
public:
Analog(uint pin, float amplifier_gain = 1.0f, float resistor = 0.0f) :
pin(pin), amplifier_gain(amplifier_gain), resistor(resistor) {
Analog(uint pin, float amplifier_gain = 1.0f, float resistor = 0.0f, float offset = 0.0f) :
pin(pin), amplifier_gain(amplifier_gain), resistor(resistor), offset(offset) {
adc_init();
//Make sure GPIO is high-impedance, no pullups etc
@ -26,6 +26,7 @@ namespace pimoroni {
uint pin;
float amplifier_gain;
float resistor;
float offset;
};
}