ESPHome 2025.9.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 {
7namespace mcp3008 {
8
9static const char *const TAG = "mcp3008";
10
12
13void MCP3008::setup() { this->spi_setup(); }
14
16 ESP_LOGCONFIG(TAG, "MCP3008:");
17 LOG_PIN(" CS Pin:", this->cs_);
18}
19
20float MCP3008::read_data(uint8_t pin) {
21 uint8_t data_msb, data_lsb = 0;
22
23 uint8_t command = ((0x01 << 7) | // start bit
24 ((pin & 0x07) << 4)); // channel number
25
26 this->enable();
27 this->transfer_byte(0x01);
28
29 data_msb = this->transfer_byte(command) & 0x03;
30 data_lsb = this->transfer_byte(0x00);
31
32 this->disable();
33
34 uint16_t data = encode_uint16(data_msb, data_lsb);
35
36 return data / 1023.0f;
37}
38
39} // namespace mcp3008
40} // namespace esphome
float read_data(uint8_t pin)
Definition mcp3008.cpp:20
void setup() override
Definition mcp3008.cpp:13
void dump_config() override
Definition mcp3008.cpp:15
float get_setup_priority() const override
Definition mcp3008.cpp:11
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:49
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:173