ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mcp3008.cpp
Go to the documentation of this file.
1#include "mcp3008.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome::mcp3008 {
7
8static const char *const TAG = "mcp3008";
9
11
12void MCP3008::setup() { this->spi_setup(); }
13
15 ESP_LOGCONFIG(TAG, "MCP3008:");
16 LOG_PIN(" CS Pin:", this->cs_);
17}
18
19float MCP3008::read_data(uint8_t pin) {
20 uint8_t data_msb, data_lsb = 0;
21
22 uint8_t command = ((0x01 << 7) | // start bit
23 ((pin & 0x07) << 4)); // channel number
24
25 this->enable();
26 this->transfer_byte(0x01);
27
28 data_msb = this->transfer_byte(command) & 0x03;
29 data_lsb = this->transfer_byte(0x00);
30
31 this->disable();
32
33 uint16_t data = encode_uint16(data_msb, data_lsb);
34
35 return data / 1023.0f;
36}
37
38} // namespace esphome::mcp3008
float read_data(uint8_t pin)
Definition mcp3008.cpp:19
void setup() override
Definition mcp3008.cpp:12
void dump_config() override
Definition mcp3008.cpp:14
float get_setup_priority() const override
Definition mcp3008.cpp:10
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41
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:859