mirror of
https://github.com/esphome/esphome.git
synced 2025-03-20 17:48:17 +00:00
implementation files for canbus and hardware
This commit is contained in:
parent
51f7155d32
commit
4ad4c70b11
@ -10,12 +10,14 @@ IS_PLATFORM_COMPONENT = True
|
||||
CONF_CANBUS_ID = 'canbus_id'
|
||||
CONF_CAN_ID = 'can_id'
|
||||
CONF_CAN_DATA = 'can_data'
|
||||
CONF_SENDER_ID = 'sender_id'
|
||||
|
||||
canbus_ns = cg.esphome_ns.namespace('canbus')
|
||||
CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(CanbusComponent),
|
||||
cv.Required(CONF_SENDER_ID): cv.int_range(min=0, max=255),
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
# Actions
|
||||
@ -23,32 +25,36 @@ SendAction = canbus_ns.class_('SendAction', automation.Action)
|
||||
|
||||
CANBUS_ACTION_SCHEMA = maybe_simple_id({
|
||||
cv.Required(CONF_CANBUS_ID): cv.use_id(CanbusComponent),
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=999),
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=1, max=4096),
|
||||
cv.Required(CONF_CAN_DATA): cv.All(),
|
||||
})
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_canbus_core_(var, config):
|
||||
yield cg.register_component(var, config)
|
||||
if CONF_CANBUS_ID in config:
|
||||
cg.add(var.set_canbus_id(config[CONF_CANBUS_ID]))
|
||||
if CONF_CAN_ID in config:
|
||||
cg.add(var.set_can_id([config[CONF_CAN_ID]]))
|
||||
if CONF_SENDER_ID in config:
|
||||
cg.add(var.set_sender_id([config[CONF_SENDER_ID]]))
|
||||
if CONF_CAN_DATA in config:
|
||||
cg.add(var.set_can_data([config[CONF_CAN_DATA]]))
|
||||
|
||||
|
||||
@coroutine
|
||||
def register_canbus(var, config):
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
var = cg.Pvariable(config[CONF_ID], var)
|
||||
yield setup_canbus_core_(var, config)
|
||||
|
||||
|
||||
@automation.register_action('canbus.send', SendAction, CANBUS_ACTION_SCHEMA)
|
||||
def canbus_send_to_code(config, action_id, template_arg, args):
|
||||
canbus = yield cg.get_variable(config[CONF_CANBUS_ID])
|
||||
#paren = yield cg.get_variable(config[CONF_ID])
|
||||
# paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(action_id, template_arg, canbus, config[CONF_CAN_ID])
|
||||
|
||||
|
||||
@coroutine_with_priority(100.0)
|
||||
def to_code(config):
|
||||
cg.add_global(canbus_ns.using)
|
||||
|
@ -12,7 +12,7 @@ CanbusBinarySensor = canbus_ns.class_('CanbusBinarySensor', binary_sensor.Binary
|
||||
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(CanbusBinarySensor),
|
||||
cv.GenerateID(CONF_CANBUS_ID): cv.use_id(CanbusComponent),
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=255)
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=1, max=255)
|
||||
})
|
||||
|
||||
|
||||
|
23
esphome/components/canbus/canbus.cpp
Normal file
23
esphome/components/canbus/canbus.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "canbus.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace canbus {
|
||||
|
||||
static const char *TAG = "canbus";
|
||||
|
||||
void Canbus::setup() { ESP_LOGCONFIG(TAG, "Setting up Canbus..."); }
|
||||
|
||||
void Canbus::dump_config() { ESP_LOGCONFIG(TAG, "Canbus: sender_id=%d", this->sender_id_); }
|
||||
|
||||
void Canbus::send(int can_id, uint8_t *data) {
|
||||
ESP_LOGD(TAG, "send: sender_id=%d, id=%d, data=%d", this->sender_id_, can_id, data[0]);
|
||||
this->send_internal_(can_id, data);
|
||||
};
|
||||
|
||||
void Canbus::loop() {
|
||||
//check harware inputbuffer and process to esphome outputs
|
||||
}
|
||||
|
||||
} // namespace canbus
|
||||
} // namespace esphome
|
@ -23,12 +23,18 @@ class Canbus : public Component {
|
||||
public:
|
||||
Canbus(){};
|
||||
Canbus(const std::string &name){};
|
||||
virtual void send(int can_id, uint8_t *data);
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
void loop() override;
|
||||
|
||||
void send(int can_id, uint8_t *data);
|
||||
void register_can_device(CanbusSensor *component){};
|
||||
void set_can_id(int can_id) { this->can_id_ = can_id; }
|
||||
void set_sender_id(int sender_id) { this->sender_id_ = sender_id; }
|
||||
|
||||
protected:
|
||||
int can_id_{0};
|
||||
int sender_id_{0};
|
||||
virtual bool send_internal_(int can_id, uint8_t *data);
|
||||
};
|
||||
} // namespace canbus
|
||||
} // namespace esphome
|
@ -3,24 +3,23 @@ import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import canbus
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.components.canbus import CONF_CAN_ID
|
||||
from esphome.components.canbus import CanbusComponent, CONF_CAN_ID
|
||||
|
||||
print("esp32_can.canbus.py")
|
||||
CONF_RX_PIN = 'rx_pin'
|
||||
CONF_TX_PIN = 'tx_pin'
|
||||
|
||||
esp32_can_ns = cg.esphome_ns.namespace('esp32_can')
|
||||
esp32_can = esp32_can_ns.class_('ESP32Can', cg.Component, canbus.CanbusComponent)
|
||||
esp32_can = esp32_can_ns.class_('ESP32Can', CanbusComponent)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
CONFIG_SCHEMA = canbus.CONFIG_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(esp32_can),
|
||||
cv.Required(CONF_RX_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_TX_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=999),
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
})
|
||||
|
||||
|
||||
def to_code(config):
|
||||
rhs = esp32_can.new()
|
||||
var = cg.Pvariable(config[CONF_ID], rhs)
|
||||
|
||||
yield cg.register_component(var, config)
|
||||
yield canbus.register_canbus(var, config)
|
||||
|
12
esphome/components/esp32_can/esp32_can.cpp
Normal file
12
esphome/components/esp32_can/esp32_can.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "esp32_can.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_can {
|
||||
|
||||
static const char *TAG = "esp32_can";
|
||||
|
||||
bool ESP32Can::send_internal_(int can_id, uint8_t *data) { return true; };
|
||||
|
||||
} // namespace esp32_can
|
||||
} // namespace esphome
|
@ -6,11 +6,14 @@
|
||||
|
||||
namespace esphome {
|
||||
namespace esp32_can {
|
||||
|
||||
class ESP32Can : public canbus::Canbus {
|
||||
public:
|
||||
ESP32Can() {};
|
||||
ESP32Can(){};
|
||||
ESP32Can(const std::string &name){};
|
||||
void send(int can_id, uint8_t *data){};
|
||||
|
||||
protected:
|
||||
bool send_internal_(int can_id, uint8_t *data);
|
||||
};
|
||||
} // namespace esp32_can
|
||||
} // namespace esphome
|
@ -10,12 +10,11 @@ AUTO_LOAD = ['canbus']
|
||||
DEPENDENCIES = ['spi']
|
||||
|
||||
mcp2515_ns = cg.esphome_ns.namespace('mcp2515')
|
||||
mcp2515 = mcp2515_ns.class_('MCP2515', canbus.CanbusComponent, spi.SPIDevice)
|
||||
mcp2515 = mcp2515_ns.class_('MCP2515', CanbusComponent, spi.SPIDevice)
|
||||
|
||||
CONFIG_SCHEMA = canbus.CONFIG_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(mcp2515),
|
||||
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=999),
|
||||
}).extend(spi.SPI_DEVICE_SCHEMA)
|
||||
|
||||
|
||||
|
12
esphome/components/mcp2515/mcp2515.cpp
Normal file
12
esphome/components/mcp2515/mcp2515.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "mcp2515.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mcp2515 {
|
||||
|
||||
static const char *TAG = "mcp2515";
|
||||
|
||||
bool MCP2515::send_internal_(int can_id, uint8_t *data) { return true; };
|
||||
|
||||
} // namespace mcp2515
|
||||
} // namespace esphome
|
@ -3,23 +3,22 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
#include "esphome/components/canbus/canbus.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mcp2515 {
|
||||
static const char *TAG = "mcp2515";
|
||||
|
||||
class MCP2515 : public canbus::Canbus,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_8MHZ> {
|
||||
public:
|
||||
MCP2515() {};
|
||||
MCP2515(){};
|
||||
MCP2515(const std::string &name){};
|
||||
void send(int can_id, uint8_t *data) { ESP_LOGD(TAG, "send: id=%d, data=%d",can_id,data[0]); };
|
||||
|
||||
void set_cs_pin(GPIOPin *cs_pin) { cs_pin_ = cs_pin; }
|
||||
|
||||
protected:
|
||||
GPIOPin *cs_pin_;
|
||||
bool send_internal_(int can_id, uint8_t *data);
|
||||
};
|
||||
} // namespace mcp2515
|
||||
} // namespace esphome
|
Loading…
x
Reference in New Issue
Block a user