2020-06-15 03:42:46 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
|
|
|
#include "esphome/core/esphal.h"
|
|
|
|
#include "esphome/components/sensor/sensor.h"
|
|
|
|
#include "esphome/components/spi/spi.h"
|
2021-01-13 19:33:19 +01:00
|
|
|
#include "esphome/components/voltage_sampler/voltage_sampler.h"
|
2020-06-15 03:42:46 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace mcp3008 {
|
|
|
|
|
|
|
|
class MCP3008 : public Component,
|
|
|
|
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
2021-01-13 19:33:19 +01:00
|
|
|
spi::DATA_RATE_75KHZ> { // Running at the slowest max speed supported by the
|
|
|
|
// mcp3008. 2.7v = 75ksps
|
2020-06-15 03:42:46 +02:00
|
|
|
public:
|
|
|
|
MCP3008() = default;
|
|
|
|
|
|
|
|
void setup() override;
|
|
|
|
void dump_config() override;
|
|
|
|
float get_setup_priority() const override;
|
2021-01-13 19:33:19 +01:00
|
|
|
float read_data(uint8_t pin);
|
2020-06-15 03:42:46 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
};
|
|
|
|
|
2021-01-13 19:33:19 +01:00
|
|
|
class MCP3008Sensor : public PollingComponent, public sensor::Sensor, public voltage_sampler::VoltageSampler {
|
2020-06-15 03:42:46 +02:00
|
|
|
public:
|
2021-06-10 13:04:40 +02:00
|
|
|
MCP3008Sensor(MCP3008 *parent, const std::string &name, uint8_t pin, float reference_voltage);
|
2020-06-15 03:42:46 +02:00
|
|
|
|
2021-01-13 19:33:19 +01:00
|
|
|
void set_reference_voltage(float reference_voltage) { reference_voltage_ = reference_voltage; }
|
2020-06-15 03:42:46 +02:00
|
|
|
void setup() override;
|
|
|
|
void update() override;
|
2021-01-13 19:33:19 +01:00
|
|
|
void dump_config() override;
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
float sample() override;
|
2020-06-15 03:42:46 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
MCP3008 *parent_;
|
|
|
|
uint8_t pin_;
|
2021-01-13 19:33:19 +01:00
|
|
|
float reference_voltage_;
|
2020-06-15 03:42:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mcp3008
|
|
|
|
} // namespace esphome
|