Esp32-Mcp3008-LineSensor
Library for Espressiff ESP32 based line followers, using the MCP3008 ADC chip.
Classes | Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
mcp3008::Driver Class Reference

The MCP3008 driver. More...

#include <mcp3008_driver.h>

Inheritance diagram for mcp3008::Driver:
mcp3008::LineSensor

Classes

struct  Config
 The Driver SPI configuration. More...
 

Public Member Functions

 Driver ()
 
virtual ~Driver ()
 The uninstall() method is called from the destructor. More...
 
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. More...
 
esp_err_t uninstall ()
 Free the SPI bus. Must be called last, or other methods return ESP_FAIL. More...
 
uint8_t getChannelsMask () const
 Get the channel mask, specified in Config::channels_mask. More...
 
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>. More...
 
esp_err_t read (uint16_t *dest, bool differential=false) const
 See the other read(std::vector<uint16_t>&, bool) const method. More...
 
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>. More...
 

Static Public Attributes

static constexpr int CHANNELS = 8
 Amount of channels on the chip. More...
 
static constexpr uint16_t MAX_VAL = 1023
 Maximum value returned by from the chip (10bits). More...
 

Protected Member Functions

int requestToChannel (int request) const
 

Detailed Description

The MCP3008 driver.

This class is not thread-safe, you have to make sure the methods are called from one thread at a time only. The install() method has to be called before you can use any other methods.

Constructor & Destructor Documentation

◆ Driver()

mcp3008::Driver::Driver ( )

◆ ~Driver()

mcp3008::Driver::~Driver ( )
virtual

The uninstall() method is called from the destructor.

Member Function Documentation

◆ getChannelsMask()

uint8_t mcp3008::Driver::getChannelsMask ( ) const
inline

Get the channel mask, specified in Config::channels_mask.

◆ install()

esp_err_t mcp3008::Driver::install ( const Config cfg = Config())

Initialize the SPI bus. Must be called before any other methods, otherwise they will return ESP_FAIL.

Parameters
cfgthe SPI bus configuration.
Returns
ESP_OK or any error code encountered during the inialization. Will return ESP_FAIL if called when already installed.

◆ read() [1/2]

esp_err_t mcp3008::Driver::read ( std::vector< uint16_t > &  results,
bool  differential = false 
) const

Read values from the chip. Returns values in range <0; Driver::MAX_VAL>.

Parameters
resultsthe results will be APPENDED to this vector. It will be unchanged unless the ESP_OK result is returned (except possibly its capacity). Between 0 and Driver::CHANNELS values are appended, depending on Config::channels_mask.
differentialreturn differential readings, as specified in the MCP3008 datasheet.
Returns
ESP_OK or any error code encountered during reading. Will return ESP_FAIL if called when not installed.

◆ read() [2/2]

esp_err_t mcp3008::Driver::read ( uint16_t *  dest,
bool  differential = false 
) const

See the other read(std::vector<uint16_t>&, bool) const method.

Parameters
destarray MUST be big enough to accomodate all the channels specified by Config::channels_mask!

◆ readChannel()

uint16_t mcp3008::Driver::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>.

Parameters
differentialreturn differential readings, as specified in the MCP3008 datasheet.
resultresult code will be written here, may be null.
Returns
measured value or 0xFFFF on error.

◆ requestToChannel()

int mcp3008::Driver::requestToChannel ( int  request) const
protected

◆ uninstall()

esp_err_t mcp3008::Driver::uninstall ( )

Free the SPI bus. Must be called last, or other methods return ESP_FAIL.

Returns
ESP_OK or any error code encountered during the freeing. Will return ESP_FAIL if called when not installed.

Member Data Documentation

◆ CHANNELS

constexpr int mcp3008::Driver::CHANNELS = 8
staticconstexpr

Amount of channels on the chip.

◆ MAX_VAL

constexpr uint16_t mcp3008::Driver::MAX_VAL = 1023
staticconstexpr

Maximum value returned by from the chip (10bits).


The documentation for this class was generated from the following files: