Esp32-Mcp3008-LineSensor
Library for Espressiff ESP32 based line followers, using the MCP3008 ADC chip.
mcp3008_driver.h
Go to the documentation of this file.
1#pragma once
2
3#include <driver/spi_master.h>
4#include <vector>
5
6namespace mcp3008 {
7
15class Driver {
16public:
17 static constexpr int CHANNELS = 8;
18 static constexpr uint16_t MAX_VAL = 1023;
19
23 struct Config {
24 Config(gpio_num_t pin_cs = GPIO_NUM_25, gpio_num_t pin_mosi = GPIO_NUM_33,
25 gpio_num_t pin_miso = GPIO_NUM_32, gpio_num_t pin_sck = GPIO_NUM_26,
26 uint8_t channels_mask = 0xFF, int freq = 1350000, spi_host_device_t spi_dev = HSPI_HOST) {
27 this->freq = freq;
28 this->spi_dev = spi_dev;
30
31 this->pin_cs = pin_cs;
32 this->pin_mosi = pin_mosi;
33 this->pin_miso = pin_miso;
34 this->pin_sck = pin_sck;
35 }
36
37 int freq;
38 spi_host_device_t spi_dev;
39 uint8_t channels_mask;
41
42 gpio_num_t pin_cs;
43 gpio_num_t pin_mosi;
44 gpio_num_t pin_miso;
45 gpio_num_t pin_sck;
46 };
47
48 Driver();
49 virtual ~Driver();
50
59 esp_err_t install(const Config& cfg = Config());
60
67 esp_err_t uninstall();
68
69 uint8_t getChannelsMask() const { return m_channels_mask; }
70
81 esp_err_t read(std::vector<uint16_t>& results, bool differential = false) const;
82
88 esp_err_t read(uint16_t* dest, bool differential = false) const;
89
97 uint16_t readChannel(uint8_t channel, bool differential = false, esp_err_t* result = nullptr) const;
98
99protected:
100 int requestToChannel(int request) const;
101
102private:
103 Driver(const Driver&) = delete;
104
105 spi_device_handle_t m_spi;
106 spi_host_device_t m_spi_dev;
107 bool m_installed;
108 uint8_t m_channels_mask;
109};
110
111}; // namespace mcp3008
The MCP3008 driver.
Definition: mcp3008_driver.h:15
esp_err_t install(const Config &cfg=Config())
Initialize the SPI bus. Must be called before any other methods, otherwise they will return ESP_FAIL.
Definition: mcp3008_driver.cpp:21
static constexpr int CHANNELS
Amount of channels on the chip.
Definition: mcp3008_driver.h:17
virtual ~Driver()
The uninstall() method is called from the destructor.
Definition: mcp3008_driver.cpp:17
esp_err_t read(std::vector< uint16_t > &results, bool differential=false) const
Read values from the chip. Returns values in range <0; Driver::MAX_VAL>.
Definition: mcp3008_driver.cpp:88
Driver()
Definition: mcp3008_driver.cpp:10
uint8_t getChannelsMask() const
Get the channel mask, specified in Config::channels_mask.
Definition: mcp3008_driver.h:69
uint16_t readChannel(uint8_t channel, bool differential=false, esp_err_t *result=nullptr) const
Read a single channel from the chip. Returns value is in range <0; Driver::MAX_VAL>.
Definition: mcp3008_driver.cpp:145
esp_err_t uninstall()
Free the SPI bus. Must be called last, or other methods return ESP_FAIL.
Definition: mcp3008_driver.cpp:56
static constexpr uint16_t MAX_VAL
Maximum value returned by from the chip (10bits).
Definition: mcp3008_driver.h:18
int requestToChannel(int request) const
Definition: mcp3008_driver.cpp:72
Definition: mcp3008_driver.cpp:8
The Driver SPI configuration.
Definition: mcp3008_driver.h:23
gpio_num_t pin_cs
Definition: mcp3008_driver.h:42
gpio_num_t pin_miso
Definition: mcp3008_driver.h:44
gpio_num_t pin_mosi
Definition: mcp3008_driver.h:43
int freq
SPI communication frequency.
Definition: mcp3008_driver.h:37
spi_host_device_t spi_dev
Which ESP32 SPI device to use.
Definition: mcp3008_driver.h:38
uint8_t channels_mask
(1 << 0) | (1 << 2) == channels 0 and 2 only.
Definition: mcp3008_driver.h:39
gpio_num_t pin_sck
Definition: mcp3008_driver.h:45
Config(gpio_num_t pin_cs=GPIO_NUM_25, gpio_num_t pin_mosi=GPIO_NUM_33, gpio_num_t pin_miso=GPIO_NUM_32, gpio_num_t pin_sck=GPIO_NUM_26, uint8_t channels_mask=0xFF, int freq=1350000, spi_host_device_t spi_dev=HSPI_HOST)
Definition: mcp3008_driver.h:24