1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-13 14:20:29 +01:00

update for platform canbus

This commit is contained in:
mvturnho 2019-07-16 15:46:32 +02:00
parent 56df9e8299
commit 3d54ac6c3b
11 changed files with 151 additions and 27 deletions

2
.gitignore vendored
View File

@ -113,3 +113,5 @@ config/
tests/build/
tests/.esphome/
/.temp-clang-tidy.cpp
.pio/
.gitignore

View File

@ -1,39 +1,37 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.automation import Condition, maybe_simple_id
from esphome.components import binary_sensor
from esphome import automation
from esphome import pins
from esphome.const import CONF_BUFFER_SIZE, CONF_DUMP, CONF_FILTER, CONF_ID, CONF_IDLE, \
CONF_PIN, CONF_TOLERANCE
canbus_ns = cg.esphome_ns.namespace('canbus')
CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component)
from esphome.automation import maybe_simple_id
from esphome.core import coroutine_with_priority
IS_PLATFORM_COMPONENT = True
CONF_CANBUS_ID = 'canbus_id'
CONF_CAN_ID = 'can_id'
CONF_DATA = 'data'
CONF_CAN_DATA = 'can_data'
SendAction = canbus_ns.class_('SendAction', automation.Action)
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.SplitDefault(CONF_BUFFER_SIZE, esp32='10000b', esp8266='1000b'): cv.validate_bytes,
}).extend(cv.COMPONENT_SCHEMA)
# Actions
SendAction = canbus_ns.class_('SendAction', automation.Action)
CANBUS_ACTION_SCHEMA = maybe_simple_id({
cv.Required(CONF_ID): cv.use_id(binary_sensor),
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_DATA): cv.All(),
})
@automation.register_action('canbus.send', SendAction, CANBUS_ACTION_SCHEMA)
def canbus_send_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(action_id, template_arg, paren)
canbus = yield cg.get_variable(config[CONF_CANBUS_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):
print("canbus to_code")
#var = cg.new_Pvariable(config[CONF_ID])
# yield cg.register_component(var, config)
# cg.add(var.set_buffer_size(config[CONF_BUFFER_SIZE]))
cg.add_global(canbus_ns.using)

View File

@ -0,0 +1,10 @@
#include "automation.h"
#include "esphome/core/log.h"
namespace esphome {
namespace switch_ {
static const char *TAG = "switch.automation";
} // namespace switch_
} // namespace esphome

View File

@ -0,0 +1,35 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/canbus/canbus.h"
namespace esphome {
namespace canbus {
template<typename... Ts> class SendAction : public Action<Ts...> {
public:
explicit SendAction(Canbus *a_canbus, int can_id) : canbus_(a_canbus), can_id_(can_id) { }
void play(Ts... x) override {
uint8_t data[8]={0};
data[1] = 10;
this->canbus_->send(this->can_id_,data); }
protected:
Canbus *canbus_;
int can_id_;
};
template<typename... Ts> class SendStateAction : public Action<Ts...> {
public:
SendStateAction(Canbus *a_canbus) : canbus_(a_canbus) {}
TEMPLATABLE_VALUE(bool, state)
void play(Ts... x) override { this->canbus_->send_state(this->state_.value(x...)); }
protected:
Canbus *canbus_;
};
} // namespace canbus
} // namespace esphome

View File

@ -2,9 +2,10 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_ID
from . import canbus_ns, CanbusComponent, CONF_CANBUS_ID
from . import canbus_ns, CanbusComponent, CONF_CANBUS_ID, CONF_CAN_ID
CONF_CAN_ID = 'can_id'
print("canbus.binary_sensor.py")
DEPENDENCIES = ['canbus']
CanbusBinarySensor = canbus_ns.class_('CanbusBinarySensor', binary_sensor.BinarySensor)
@ -18,7 +19,7 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield binary_sensor.register_binary_sensor(var, config)
hub = yield cg.get_variable(config[CONF_CANBUS_ID])
cg.add(var.set_can_id(config[CONF_CAN_ID]))
cg.add(hub.register_can_device(var))

View File

@ -0,0 +1,32 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome {
namespace canbus {
class CanbusSensor {
public:
void set_can_id(int can_id) { this->can_id_ = can_id; }
private:
int can_id_{0};
};
class CanbusBinarySensor : public CanbusSensor , public binary_sensor::BinarySensor {
friend class Canbus;
};
class Canbus : public Component {
public:
Canbus(){};
Canbus(const std::string &name){};
virtual void send(int can_id, uint8_t *data);
void register_can_device(CanbusSensor *component){};
};
} // namespace canbus
} // namespace esphome

View File

@ -1,14 +1,21 @@
import esphome.codegen as cg
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
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)
CONFIG_SCHEMA = cv.Schema({
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)
@ -17,4 +24,3 @@ def to_code(config):
var = cg.Pvariable(config[CONF_ID], rhs)
yield cg.register_component(var, config)

View File

@ -0,0 +1,16 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/canbus/canbus.h"
namespace esphome {
namespace esp32_can {
class ESP32Can : public canbus::Canbus {
public:
ESP32Can() {};
ESP32Can(const std::string &name){};
void send(int can_id, uint8_t *data){};
};
} // namespace esp32_can
} // namespace esphome

View File

@ -1 +1 @@
print "hello"
print("mcp2515.__init__.py")

View File

@ -2,7 +2,10 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import spi, binary_sensor, canbus
from esphome.const import CONF_DC_PIN, CONF_CS_PIN, CONF_ID
from esphome.const import CONF_CS_PIN, CONF_ID
from esphome.components.canbus import CONF_CAN_ID
print("mcp2515.canbus.py")
DEPENDENCIES = ['spi']
@ -11,8 +14,8 @@ mcp2515 = mcp2515_ns.class_('MCP2515', cg.Component, canbus.CanbusComponent, spi
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(mcp2515),
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
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)
@ -22,7 +25,5 @@ def to_code(config):
yield cg.register_component(var, config)
yield spi.register_spi_device(var, config)
dc = yield cg.gpio_pin_expression(config[CONF_DC_PIN])
cg.add(var.set_dc_pin(dc))
cs = yield cg.gpio_pin_expression(config[CONF_CS_PIN])
cg.add(var.set_cs_pin(cs))

View File

@ -0,0 +1,23 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/canbus/canbus.h"
namespace esphome {
namespace 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(const std::string &name){};
void send(int can_id, uint8_t *data) {};
void set_cs_pin(GPIOPin *cs_pin) { cs_pin_ = cs_pin; }
protected:
GPIOPin *cs_pin_;
};
} // namespace mcp2515
} // namespace esphome