ESPHome  2024.11.0
mcp3008.cpp
Go to the documentation of this file.
1 #include "mcp3008.h"
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace mcp3008 {
8 
9 static const char *const TAG = "mcp3008";
10 
12 
14  ESP_LOGCONFIG(TAG, "Setting up mcp3008");
15  this->spi_setup();
16 }
17 
19  ESP_LOGCONFIG(TAG, "MCP3008:");
20  LOG_PIN(" CS Pin:", this->cs_);
21 }
22 
23 float MCP3008::read_data(uint8_t pin) {
24  uint8_t data_msb, data_lsb = 0;
25 
26  uint8_t command = ((0x01 << 7) | // start bit
27  ((pin & 0x07) << 4)); // channel number
28 
29  this->enable();
30  this->transfer_byte(0x01);
31 
32  data_msb = this->transfer_byte(command) & 0x03;
33  data_lsb = this->transfer_byte(0x00);
34 
35  this->disable();
36 
37  uint16_t data = encode_uint16(data_msb, data_lsb);
38 
39  return data / 1023.0f;
40 }
41 
42 } // namespace mcp3008
43 } // namespace esphome
void dump_config() override
Definition: mcp3008.cpp:18
float get_setup_priority() const override
Definition: mcp3008.cpp:11
GPIOPin * cs_
Definition: spi.h:378
void setup() override
Definition: mcp3008.cpp:13
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition: helpers.h:183
float read_data(uint8_t pin)
Definition: mcp3008.cpp:23
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7