2021-08-18 12:16:51 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/adc.h"
|
|
|
|
#include "common/pimoroni_common.hpp"
|
|
|
|
|
|
|
|
namespace pimoroni {
|
|
|
|
|
|
|
|
class Analog {
|
|
|
|
public:
|
2022-03-25 23:31:27 +00:00
|
|
|
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) {
|
2021-08-18 12:16:51 +01:00
|
|
|
adc_init();
|
|
|
|
|
|
|
|
//Make sure GPIO is high-impedance, no pullups etc
|
|
|
|
adc_gpio_init(pin);
|
|
|
|
|
|
|
|
//Select ADC input 0 (GPIO26)
|
|
|
|
adc_select_input(pin - 26);
|
|
|
|
};
|
|
|
|
uint16_t read_raw();
|
|
|
|
float read_voltage();
|
|
|
|
float read_current();
|
|
|
|
private:
|
|
|
|
uint pin;
|
|
|
|
float amplifier_gain;
|
|
|
|
float resistor;
|
2022-03-25 23:31:27 +00:00
|
|
|
float offset;
|
2021-08-18 12:16:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|