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#include "include/log_sources.h"
16
20namespace HAL::ADC {
22 adc_atten_t attenuation;
23 adc_bitwidth_t bitwidth;
24 adc_ulp_mode_t ulp_mode;
25 adc_oneshot_clk_src_t clock_source;
26 };
27
31 class Controller {
32 private:
33 static constexpr const char* TAG = "HAL::ADC";
34 static constexpr log_source LOG_SOURCE = LOG_SOURCE_HAL_ADC;
35
36 mutable SemaphoreHandle_t mutex;
37
39 bool is_configured;
40
41 adc_oneshot_unit_handle_t adc_handle = nullptr;
42 adc_cali_handle_t cali_handle = nullptr;
43
44 std::vector<adc_channel_t> registered_channels;
45
46 private:
47 explicit Controller();
48 ~Controller();
49
57 static gpio_num_t adc_channel_to_gpio(const adc_channel_t adc_channel) {
58 switch (adc_channel) {
59 case ADC_CHANNEL_0: return GPIO_NUM_36;
60 case ADC_CHANNEL_3: return GPIO_NUM_39;
61 case ADC_CHANNEL_4: return GPIO_NUM_32;
62 case ADC_CHANNEL_5: return GPIO_NUM_33;
63 case ADC_CHANNEL_6: return GPIO_NUM_34;
64 case ADC_CHANNEL_7: return GPIO_NUM_35;
65 default: return GPIO_NUM_NC;
66 }
67 }
68
69 public:
70 Controller(const Controller&) = delete;
71 Controller& operator=(const Controller&) = delete;
72
79 static Controller& get_instance() {
80 static Controller _instance;
81 return _instance;
82 }
83
89 void configure(const controller_config_t& config);
90
94 void shutdown();
95
101 void add(adc_channel_t adc_channel);
102
111 [[nodiscard]] uint16_t read_raw(adc_channel_t adc_channel, uint16_t samples = 1) const;
112
121 [[nodiscard]] uint16_t read_millivolts(adc_channel_t adc_channel, uint16_t samples = 1) const;
122 };
123}
void configure(const controller_config_t &config)
Configures the ADC Controller.
Definition adc.cpp:42
static Controller & get_instance()
Returns a reference to the static controller instance.
Definition adc.h:79
uint16_t read_raw(adc_channel_t adc_channel, uint16_t samples=1) const
Reads the current raw ADC reading.
Definition adc.cpp:127
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:159
void shutdown()
Safely shuts down the controller and hardware.
Definition adc.cpp:70
Controller(const Controller &)=delete
void add(adc_channel_t adc_channel)
Adds an ADC input to the controller.
Definition adc.cpp:101
log_source
Definition log_sources.h:12
@ LOG_SOURCE_HAL_ADC
Definition log_sources.h:17
A namespace containing all components of the ADC hardware abstraction layer.
Definition adc.h:20
adc_atten_t attenuation
Definition adc.h:22
adc_bitwidth_t bitwidth
Definition adc.h:23
adc_oneshot_clk_src_t clock_source
Definition adc.h:25
adc_ulp_mode_t ulp_mode
Definition adc.h:24