Boboter
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <vector>
11#include <soc/gpio_num.h>
12#include <esp_adc/adc_oneshot.h>
13#include <esp_adc/adc_cali.h>
14#include <freertos/FreeRTOS.h>
15
19namespace HAL::ADC {
21 adc_atten_t attenuation;
22 adc_bitwidth_t bitwidth;
23 adc_ulp_mode_t ulp_mode;
24 adc_oneshot_clk_src_t clock_source;
25 };
26
30 class Controller {
31 private:
32 static constexpr const char* TAG = "HAL::ADC";
33
34 mutable SemaphoreHandle_t mutex;
35
37 bool is_configured;
38
39 adc_oneshot_unit_handle_t adc_handle = nullptr;
40 adc_cali_handle_t cali_handle = nullptr;
41
42 std::vector<adc_channel_t> registered_channels;
43
44 private:
45 explicit Controller();
46 ~Controller();
47
55 static gpio_num_t adc_channel_to_gpio(const adc_channel_t adc_channel) {
56 switch (adc_channel) {
57 case ADC_CHANNEL_0: return GPIO_NUM_36;
58 case ADC_CHANNEL_3: return GPIO_NUM_39;
59 case ADC_CHANNEL_4: return GPIO_NUM_32;
60 case ADC_CHANNEL_5: return GPIO_NUM_33;
61 case ADC_CHANNEL_6: return GPIO_NUM_34;
62 case ADC_CHANNEL_7: return GPIO_NUM_35;
63 default: return GPIO_NUM_NC;
64 }
65 }
66
67 public:
68 Controller(const Controller&) = delete;
69 Controller& operator=(const Controller&) = delete;
70
77 static Controller& get_instance() {
78 static Controller _instance;
79 return _instance;
80 }
81
87 void configure(const controller_config_t& config);
88
92 void shutdown();
93
99 void add(adc_channel_t adc_channel);
100
109 [[nodiscard]] uint16_t read_raw(adc_channel_t adc_channel, uint16_t samples = 1) const;
110
119 [[nodiscard]] uint16_t read_millivolts(adc_channel_t adc_channel, uint16_t samples = 1) const;
120 };
121}
void configure(const controller_config_t &config)
Configures the ADC Controller.
Definition adc.cpp:36
static Controller & get_instance()
Returns a reference to the static controller instance.
Definition adc.h:77
uint16_t read_raw(adc_channel_t adc_channel, uint16_t samples=1) const
Reads the current raw ADC reading.
Definition adc.cpp:121
Controller & operator=(const Controller &)=delete
uint16_t read_millivolts(adc_channel_t adc_channel, uint16_t samples=1) const
Reads the current voltage in millivolts.
Definition adc.cpp:152
void shutdown()
Safely shuts down the controller and hardware.
Definition adc.cpp:64
Controller(const Controller &)=delete
void add(adc_channel_t adc_channel)
Adds an ADC input to the controller.
Definition adc.cpp:95
A namespace containing all components of the ADC hardware abstraction layer.
Definition adc.h:19
adc_atten_t attenuation
Definition adc.h:21
adc_bitwidth_t bitwidth
Definition adc.h:22
adc_oneshot_clk_src_t clock_source
Definition adc.h:24
adc_ulp_mode_t ulp_mode
Definition adc.h:23