1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00

update for timings.

does not compile yet
This commit is contained in:
mvturnho 2019-06-02 09:11:59 +02:00
parent 5d437c243e
commit e2937430a0
9 changed files with 132 additions and 72 deletions

View File

@ -4,7 +4,12 @@ from esphome import pins
from esphome.components import i2c from esphome.components import i2c
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED
CONF_TIME_ON = 'time_on' CONF_ON_TIME = 'on_time'
CONF_OFF_TIME = 'off_time'
CONF_RISE_TIME = 'rise_time'
CONF_FALL_TIME = 'fall_time'
CONF_ON_INT = 'on_intemsity'
CONF_OFF_INT = 'off_intensity'
DEPENDENCIES = ['i2c'] DEPENDENCIES = ['i2c']
MULTI_CONF = True MULTI_CONF = True
@ -39,8 +44,12 @@ SX1509_OUTPUT_PIN_SCHEMA = cv.Schema({
cv.Required(CONF_NUMBER): cv.int_, cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_MODE, default="OUTPUT"): cv.enum(SX1509_GPIO_MODES, upper=True), cv.Optional(CONF_MODE, default="OUTPUT"): cv.enum(SX1509_GPIO_MODES, upper=True),
cv.Optional(CONF_INVERTED, default=False): cv.boolean, cv.Optional(CONF_INVERTED, default=False): cv.boolean,
cv.Optional(CONF_TIME_ON): cv.int_, cv.Optional(CONF_ON_TIME, default=500): cv.int_,
}) cv.Optional(CONF_OFF_TIME, default=500): cv.int_,
cv.Optional(CONF_RISE_TIME, default=500): cv.int_,
cv.Optional(CONF_FALL_TIME, default=500): cv.int_,
cv.Optional(CONF_ON_INT, default=7): cv.int_range(min=0, max=7),
cv.Optional(CONF_OFF_INT, default=0): cv.int_range(min=0, max=7), })
SX1509_INPUT_PIN_SCHEMA = cv.Schema({ SX1509_INPUT_PIN_SCHEMA = cv.Schema({
cv.Required(CONF_SX1509): cv.use_id(SX1509Component), cv.Required(CONF_SX1509): cv.use_id(SX1509Component),
cv.Required(CONF_NUMBER): cv.int_, cv.Required(CONF_NUMBER): cv.int_,
@ -53,4 +62,9 @@ SX1509_INPUT_PIN_SCHEMA = cv.Schema({
(SX1509_OUTPUT_PIN_SCHEMA, SX1509_INPUT_PIN_SCHEMA)) (SX1509_OUTPUT_PIN_SCHEMA, SX1509_INPUT_PIN_SCHEMA))
def sx1509_pin_to_code(config): def sx1509_pin_to_code(config):
parent = yield cg.get_variable(config[CONF_SX1509]) parent = yield cg.get_variable(config[CONF_SX1509])
if(config[CONF_MODE] == 'BREATHE_OUTPUT' or config[CONF_MODE] == 'BLINK_OUTPUT'):
yield SX1509GPIOPin.new(parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED],
config[CONF_ON_TIME], config[CONF_OFF_TIME],
config[CONF_RISE_TIME], config[CONF_FALL_TIME])
else:
yield SX1509GPIOPin.new(parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED]) yield SX1509GPIOPin.new(parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED])

View File

@ -0,0 +1,29 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_ID
from sensor import SX1509KeypadSensor, sx1509_ns
CONF_ROW = 'row'
CONF_COL = 'col'
SX1509BinarySensor = sx1509_ns.class_('SX1509BinarySensor', binary_sensor.BinarySensor)
CONF_SX1509_KEYPAD_ID = 'sx1509_keypad_id'
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SX1509BinarySensor),
cv.GenerateID(CONF_SX1509_KEYPAD_ID): cv.use_id(SX1509KeypadSensor),
cv.Required(CONF_ROW): cv.int_range(min=0, max=4),
cv.Required(CONF_COL): cv.int_range(min=0, max=4),
})
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_SX1509_KEYPAD_ID])
cg.add(var.set_row_col(config[CONF_ROW], config[CONF_COL]))
cg.add(hub.register_binary_sensor(var))

View File

@ -36,4 +36,5 @@ def to_code(config):
cg.add(var.set_parent(parent)) cg.add(var.set_parent(parent))
cg.add(var.set_rows_cols(config[CONF_KEY_ROWS], config[CONF_KEY_COLUMNS])) cg.add(var.set_rows_cols(config[CONF_KEY_ROWS], config[CONF_KEY_COLUMNS]))
cg.add(var.set_timers(config[CONF_SLEEP_TIME],config[CONF_SCAN_TIME], config[CONF_DEBOUNCE_TIME] )) cg.add(var.set_timers(config[CONF_SLEEP_TIME], config[CONF_SCAN_TIME], \
config[CONF_DEBOUNCE_TIME]))

View File

@ -28,8 +28,6 @@ void SX1509Component::setup() {
// channel->setup(); // channel->setup();
delayMicroseconds(500); delayMicroseconds(500);
this->loop();
} }
void SX1509Component::dump_config() { void SX1509Component::dump_config() {
@ -39,15 +37,6 @@ void SX1509Component::dump_config() {
} }
} }
void SX1509Component::loop() {}
// SX1509FloatOutputChannel *SX1509Component::create_float_output_channel(uint8_t pin) {
// ESP_LOGD(TAG, "Create float channel for pin %d", pin);
// auto *channel = new SX1509FloatOutputChannel(this, pin);
// float_output_channels_.push_back(channel);
// return channel;
// }
uint8_t SX1509Component::digital_read(uint8_t pin) { uint8_t SX1509Component::digital_read(uint8_t pin) {
uint16_t tempRegDir; uint16_t tempRegDir;
this->read_byte_16(REG_DIR_B, &tempRegDir); this->read_byte_16(REG_DIR_B, &tempRegDir);
@ -120,10 +109,6 @@ void SX1509Component::pin_mode(uint8_t pin, uint8_t mode) {
if (mode == ANALOG_OUTPUT) { if (mode == ANALOG_OUTPUT) {
led_driver_init(pin); led_driver_init(pin);
} }
if (mode == BREATHE_OUTPUT) {
breathe(pin, 1000, 1000, 1000, 1000);
}
} }
void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) { void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) {
@ -174,41 +159,41 @@ void SX1509Component::led_driver_init(uint8_t pin, uint8_t freq, bool log) {
this->write_byte_16(REG_DATA_B, temp_word); this->write_byte_16(REG_DATA_B, temp_word);
} }
void SX1509Component::breathe(uint8_t pin, unsigned long tOn, unsigned long tOff, unsigned long rise, void SX1509Component::breathe(uint8_t pin, uint16_t t_on, uint16_t t_off,uint16_t t_rise,
unsigned long fall, uint8_t onInt, uint8_t offInt, bool log) { uint16_t t_fall, uint8_t on_intensity, uint8_t off_intensity, bool log) {
offInt = constrain(offInt, 0, 7); off_intensity = constrain(off_intensity, 0, 7);
uint8_t onReg = calculate_led_t_register(tOn); uint8_t onReg = calculate_led_t_register(t_on);
uint8_t offReg = calculate_led_t_register(tOff); uint8_t offReg = calculate_led_t_register(t_off);
uint8_t riseTime = calculate_slope_register(rise, onInt, offInt); uint8_t rise_time = calculate_slope_register(t_rise, on_intensity, off_intensity);
uint8_t fallTime = calculate_slope_register(fall, onInt, offInt); uint8_t fall_time = calculate_slope_register(t_fall, on_intensity, off_intensity);
setup_blink(pin, onReg, offReg, onInt, offInt, riseTime, fallTime, log); setup_blink(pin, onReg, offReg, on_intensity, off_intensity, rise_time, fall_time, log);
} }
void SX1509Component::setup_blink(uint8_t pin, uint8_t tOn, uint8_t tOff, uint8_t onIntensity, uint8_t offIntensity, void SX1509Component::setup_blink(uint8_t pin, uint8_t t_on, uint8_t t_off, uint8_t on_intensity, uint8_t off_intensity,
uint8_t tRise, uint8_t tFall, bool log) { uint8_t t_rise, uint8_t t_fall, bool log) {
led_driver_init(pin, log); led_driver_init(pin, log);
tOn &= 0x1F; // tOn should be a 5-bit value t_on &= 0x1F; // t_on should be a 5-bit value
tOff &= 0x1F; // tOff should be a 5-bit value t_off &= 0x1F; // t_off should be a 5-bit value
offIntensity &= 0x07; off_intensity &= 0x07;
// Write the time on // Write the time on
this->write_byte(REG_T_ON[pin], tOn); this->write_byte(REG_T_ON[pin], t_on);
this->write_byte(REG_OFF[pin], (tOff << 3) | offIntensity); this->write_byte(REG_OFF[pin], (t_off << 3) | off_intensity);
this->write_byte(REG_I_ON[pin], onIntensity); this->write_byte(REG_I_ON[pin], on_intensity);
tRise &= 0x1F; t_rise &= 0x1F;
tFall &= 0x1F; t_fall &= 0x1F;
if (REG_T_RISE[pin] != 0xFF) if (REG_T_RISE[pin] != 0xFF)
this->write_byte(REG_T_RISE[pin], tRise); this->write_byte(REG_T_RISE[pin], t_rise);
if (REG_T_FALL[pin] != 0xFF) if (REG_T_FALL[pin] != 0xFF)
this->write_byte(REG_T_FALL[pin], tFall); this->write_byte(REG_T_FALL[pin], t_fall);
} }
void SX1509Component::clock(byte oscSource, byte oscPinFunction, byte oscFreqOut, byte oscDivider) { void SX1509Component::clock(byte oscSource, byte oscPinFunction, byte oscFreqOut, byte oscDivider) {

View File

@ -34,7 +34,6 @@ class SX1509Component : public Component, public i2c::I2CDevice {
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; } float get_setup_priority() const override { return setup_priority::HARDWARE; }
void loop() override;
void pin_mode(uint8_t pin, uint8_t in_out); void pin_mode(uint8_t pin, uint8_t in_out);
void led_driver_init(uint8_t pin, uint8_t freq = 1, bool log = false); void led_driver_init(uint8_t pin, uint8_t freq = 1, bool log = false);
void clock(uint8_t osc_source = 2, uint8_t osc_divider = 1, uint8_t osc_pin_function = 0, uint8_t osc_freq_out = 0); void clock(uint8_t osc_source = 2, uint8_t osc_divider = 1, uint8_t osc_pin_function = 0, uint8_t osc_freq_out = 0);
@ -44,9 +43,9 @@ class SX1509Component : public Component, public i2c::I2CDevice {
void setup_blink(uint8_t pin, uint8_t t_on, uint8_t t_off, uint8_t on_intensity = 255, uint8_t off_intensity = 0, void setup_blink(uint8_t pin, uint8_t t_on, uint8_t t_off, uint8_t on_intensity = 255, uint8_t off_intensity = 0,
uint8_t t_rise = 0, uint8_t t_fall = 0, bool log = false); uint8_t t_rise = 0, uint8_t t_fall = 0, bool log = false);
void blink(uint8_t pin, unsigned long tOn, unsigned long tOff, uint8_t on_intensity = 255, uint8_t off_intensity = 0); void blink(uint8_t pin, uint16_t tOn, uint16_t tOff, uint8_t on_intensity = 255, uint8_t off_intensity = 0);
void breathe(uint8_t pin, unsigned long tOn, unsigned long tOff, unsigned long rise, unsigned long fall, void breathe(uint8_t pin, uint16_t tOn, uint16_t tOff, uint16_t rise, uint16_t fall,
uint8_t onInt = 255, uint8_t offInt = 0, bool log = LINEAR); uint8_t on_intensity = 255, uint8_t off_intensity = 0, bool log = LINEAR);
uint8_t calculate_led_t_register(uint16_t ms); uint8_t calculate_led_t_register(uint16_t ms);
uint8_t calculate_slope_register(uint16_t ms, uint8_t on_intensity, uint8_t off_intensity); uint8_t calculate_slope_register(uint16_t ms, uint8_t on_intensity, uint8_t off_intensity);
void setup_keypad(uint8_t rows, uint8_t columns, uint16_t sleep_time = 0, uint8_t scan_time = 1, void setup_keypad(uint8_t rows, uint8_t columns, uint16_t sleep_time = 0, uint8_t scan_time = 1,

View File

@ -7,11 +7,25 @@ namespace sx1509 {
static const char *TAG = "sx1509_gpio_pin"; static const char *TAG = "sx1509_gpio_pin";
SX1509GPIOPin::SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted) SX1509GPIOPin::SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted, uint16_t t_on,
: GPIOPin(pin, mode, inverted), parent_(parent) {} uint16_t t_off, uint16_t t_rise, uint16_t t_fall)
: GPIOPin(pin, mode, inverted), parent_(parent) {
t_on_ = t_on;
t_off_ = t_off;
t_rise_ = t_rise;
t_fall_ = t_fall;
}
void SX1509GPIOPin::setup() { void SX1509GPIOPin::setup() {
ESP_LOGD(TAG, "setup pin %d", this->pin_); ESP_LOGD(TAG, "setup pin %d", this->pin_);
this->pin_mode(this->mode_); this->pin_mode(this->mode_);
switch (this->mode_) {
case BREATHE_OUTPUT:
this->parent_->breathe(this->pin_, t_on_, t_off_, t_rise_, t_fall_);
break;
case BLINK_OUTPUT:
this->parent_->blink(this->pin_, t_on_, t_off_);
break;
}
} }
void SX1509GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); } void SX1509GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); }
bool SX1509GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; } bool SX1509GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }

View File

@ -3,7 +3,7 @@
#include "sx1509.h" #include "sx1509.h"
#define BREATHE_OUTPUT 0x04 #define BREATHE_OUTPUT 0x04
#define BLINK_OUTPUT 0x04 #define BLINK_OUTPUT 0x05
namespace esphome { namespace esphome {
namespace sx1509 { namespace sx1509 {
@ -21,8 +21,8 @@ class SX1509Component;
class SX1509GPIOPin : public GPIOPin { class SX1509GPIOPin : public GPIOPin {
public: public:
SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted = false); SX1509GPIOPin(SX1509Component *parent, uint8_t pin, uint8_t mode, bool inverted = false, uint16_t t_on = 500,
uint16_t t_off = 500, uint16_t t_rise = 250, uint16_t t_fall = 250);
void setup() override; void setup() override;
void pin_mode(uint8_t mode) override; void pin_mode(uint8_t mode) override;
bool digital_read() override; bool digital_read() override;
@ -30,6 +30,11 @@ class SX1509GPIOPin : public GPIOPin {
protected: protected:
SX1509Component *parent_; SX1509Component *parent_;
uint16_t t_on_ = {0};
uint16_t t_off_ = {0};
uint16_t t_rise_ = {0};
uint16_t t_fall_ = {0};
}; };
}} } // namespace sx1509
} // namespace esphome

View File

@ -10,13 +10,6 @@ static const char *TAG = "sx1509_keypad_sensor";
void SX1509KeypadSensor::setup() { void SX1509KeypadSensor::setup() {
ESP_LOGD(TAG, "setup rows: %d , cols: %d", this->rows_, this->cols_); ESP_LOGD(TAG, "setup rows: %d , cols: %d", this->rows_, this->cols_);
this->parent_->setup_keypad(this->rows_, this->cols_, this->sleep_time_, this->scan_time_, this->debounce_time_); this->parent_->setup_keypad(this->rows_, this->cols_, this->sleep_time_, this->scan_time_, this->debounce_time_);
keypad_values_ = new uint8_t *[this->rows_];
for (uint8_t i = 0; i < this->rows_; ++i) {
keypad_values_[i] = new uint8_t[this->cols_];
for (uint8_t j = 0; j < this->cols_; j++) {
keypad_values_[i][j] = i * j;
}
}
} }
void SX1509KeypadSensor::dump_config() { void SX1509KeypadSensor::dump_config() {
@ -27,11 +20,11 @@ void SX1509KeypadSensor::dump_config() {
void SX1509KeypadSensor::loop() { void SX1509KeypadSensor::loop() {
uint16_t key_data = this->parent_->read_key_data(); uint16_t key_data = this->parent_->read_key_data();
if (key_data != 0) { if (key_data != 0) {
byte row = get_row_(key_data); uint8_t row = get_row_(key_data);
byte col = get_col_(key_data); uint8_t col = get_col_(key_data);
uint8_t key = this->keypad_values_[row][col]; uint8_t key = (row * cols_) + col;
if (key_data != last_key_press_) { if (key_data != last_key_press_) {
ESP_LOGD(TAG, "'%s' - Publishing %d", this->name_.c_str(), key); ESP_LOGD(TAG, "'%s' - Publishing %d rawdata: %04x", this->name_.c_str(), key, key_data);
this->publish_state(key); this->publish_state(key);
} }
} else if (this->last_key_press_ != 0ULL) { } else if (this->last_key_press_ != 0ULL) {
@ -40,9 +33,12 @@ void SX1509KeypadSensor::loop() {
this->publish_state(NAN); this->publish_state(NAN);
} }
this->last_key_press_ = key_data; this->last_key_press_ = key_data;
for (auto *binary_sensor : this->binary_sensors_)
binary_sensor->process(key_data);
} }
byte SX1509KeypadSensor::get_row_(uint16_t key_data) { uint8_t SX1509KeypadSensor::get_row_(uint16_t key_data) {
uint8_t row_data = uint8_t(key_data & 0x00FF); uint8_t row_data = uint8_t(key_data & 0x00FF);
for (uint16_t i = 0; i < 8; i++) { for (uint16_t i = 0; i < 8; i++) {
if (row_data & (1 << i)) if (row_data & (1 << i))
@ -51,7 +47,7 @@ byte SX1509KeypadSensor::get_row_(uint16_t key_data) {
return 0; return 0;
} }
byte SX1509KeypadSensor::get_col_(uint16_t key_data) { uint8_t SX1509KeypadSensor::get_col_(uint16_t key_data) {
uint8_t col_data = uint8_t((key_data & 0xFF00) >> 8); uint8_t col_data = uint8_t((key_data & 0xFF00) >> 8);
for (uint16_t i = 0; i < 8; i++) { for (uint16_t i = 0; i < 8; i++) {
if (col_data & (1 << i)) if (col_data & (1 << i))

View File

@ -3,12 +3,28 @@
#include "sx1509.h" #include "sx1509.h"
#include "esphome/components/output/float_output.h" #include "esphome/components/output/float_output.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/core/log.h"
namespace esphome { namespace esphome {
namespace sx1509 { namespace sx1509 {
class SX1509Component; class SX1509Component;
class SX1509BinarySensor : public binary_sensor::BinarySensor {
friend class SX1509KeypadSensor;
public:
void set_row_col(uint8_t row, uint8_t col) {
this->key_ = (1 << (row + 8)) | (1 << col);
ESP_LOGD("SX1509BinarySensor", "register row: %d , col: %d , key: %04x", row, col, key_);
}
void process(uint16_t keydata) { this->publish_state(static_cast<bool>(keydata == key_)); }
protected:
uint16_t key_{0};
};
class SX1509KeypadSensor : public sensor::Sensor, public Component { class SX1509KeypadSensor : public sensor::Sensor, public Component {
public: public:
void set_parent(SX1509Component *parent) { this->parent_ = parent; } void set_parent(SX1509Component *parent) { this->parent_ = parent; }
@ -25,16 +41,17 @@ class SX1509KeypadSensor : public sensor::Sensor, public Component {
void dump_config() override; void dump_config() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; } float get_setup_priority() const override { return setup_priority::HARDWARE; }
void loop() override; void loop() override;
void register_binary_sensor(SX1509BinarySensor *binary_sensor) { this->binary_sensors_.push_back(binary_sensor); };
protected: protected:
SX1509Component *parent_; SX1509Component *parent_;
uint8_t **keypad_values_;
uint8_t rows_ = {}; uint8_t rows_ = {};
uint8_t cols_ = {}; uint8_t cols_ = {};
uint16_t sleep_time_ = {}; uint16_t sleep_time_ = {};
uint8_t scan_time_ = {}; uint8_t scan_time_ = {};
uint8_t debounce_time_ = {}; uint8_t debounce_time_ = {};
uint16_t last_key_press_ = {0}; uint16_t last_key_press_ = {0};
std::vector<SX1509BinarySensor *> binary_sensors_{};
uint8_t get_row_(uint16_t key_data); uint8_t get_row_(uint16_t key_data);
uint8_t get_col_(uint16_t key_data); uint8_t get_col_(uint16_t key_data);