ESPHome
2025.5.0
Loading...
Searching...
No Matches
esphome
components
mcp3008
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
11
float
MCP3008::get_setup_priority
()
const
{
return
setup_priority::HARDWARE
; }
12
13
void
MCP3008::setup
() {
14
ESP_LOGCONFIG(TAG,
"Setting up mcp3008"
);
15
this->
spi_setup
();
16
}
17
18
void
MCP3008::dump_config
() {
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
esphome::mcp3008::MCP3008::read_data
float read_data(uint8_t pin)
Definition
mcp3008.cpp:23
esphome::mcp3008::MCP3008::setup
void setup() override
Definition
mcp3008.cpp:13
esphome::mcp3008::MCP3008::dump_config
void dump_config() override
Definition
mcp3008.cpp:18
esphome::mcp3008::MCP3008::get_setup_priority
float get_setup_priority() const override
Definition
mcp3008.cpp:11
esphome::spi::SPIClient::cs_
GPIOPin * cs_
Definition
spi.h:408
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_75KHZ >::enable
void enable()
Definition
spi.h:493
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_75KHZ >::spi_setup
void spi_setup() override
Definition
spi.h:430
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_75KHZ >::disable
void disable()
Definition
spi.h:495
esphome::spi::SPIDevice< spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_75KHZ >::transfer_byte
uint8_t transfer_byte(uint8_t data)
Definition
spi.h:479
helpers.h
log.h
mcp3008.h
esphome::setup_priority::HARDWARE
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition
component.cpp:18
esphome
Providing packet encoding functions for exchanging data with a remote host.
Definition
a01nyub.cpp:7
esphome::encode_uint16
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:191
Generated by
1.12.0