mirror of
https://github.com/randybb/esphome-configs.git
synced 2026-01-02 11:37:28 +01:00
have fun
This commit is contained in:
24
components/bm8563/__init__.py
Normal file
24
components/bm8563/__init__.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c
|
||||
from esphome.const import CONF_ID, CONF_SLEEP_DURATION
|
||||
|
||||
DEPENDENCIES = ['i2c']
|
||||
|
||||
CONF_I2C_ADDR = 0x51
|
||||
|
||||
bm8563 = cg.esphome_ns.namespace('bm8563')
|
||||
BM8563 = bm8563.class_('BM8563', cg.Component, i2c.I2CDevice)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(BM8563),
|
||||
cv.Optional(CONF_SLEEP_DURATION): cv.positive_time_period_seconds,
|
||||
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(CONF_I2C_ADDR))
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
if CONF_SLEEP_DURATION in config:
|
||||
cg.add(var.set_sleep_duration(config[CONF_SLEEP_DURATION]))
|
||||
yield cg.register_component(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
|
||||
242
components/bm8563/bm8563.cpp
Normal file
242
components/bm8563/bm8563.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/i2c/i2c_bus.h"
|
||||
#include "bm8563.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace bm8563 {
|
||||
|
||||
static const char *TAG = "bm8563.sensor";
|
||||
|
||||
void BM8563::setup(){
|
||||
this->write_byte_16(0,0);
|
||||
this->setupComplete = true;
|
||||
if (this->sleep_duration_.has_value()) {
|
||||
SetAlarmIRQ(*this->sleep_duration_);
|
||||
}
|
||||
}
|
||||
|
||||
void BM8563::loop(){
|
||||
// if(!this->setupComplete){
|
||||
// return;
|
||||
// }
|
||||
// BM8563_TimeTypeDef BM8563_TimeStruct;
|
||||
// getTime(&BM8563_TimeStruct);
|
||||
// this->publish_state(BM8563_TimeStruct.seconds);
|
||||
}
|
||||
|
||||
void BM8563::dump_config(){
|
||||
ESP_LOGCONFIG(TAG, "BM8563:");
|
||||
ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
|
||||
ESP_LOGCONFIG(TAG, " setupComplete: %s", this->setupComplete ? "true" : "false");
|
||||
if (this->sleep_duration_.has_value()) {
|
||||
uint32_t duration = *this->sleep_duration_;
|
||||
ESP_LOGCONFIG(TAG, " Sleep Duration: %u ms", duration);
|
||||
}
|
||||
}
|
||||
|
||||
void BM8563::set_sleep_duration(uint32_t time_s) {
|
||||
this->sleep_duration_ = uint64_t(time_s);
|
||||
if (this->sleep_duration_.has_value()) {
|
||||
SetAlarmIRQ(*this->sleep_duration_);
|
||||
}
|
||||
}
|
||||
|
||||
bool BM8563::getVoltLow() {
|
||||
uint8_t data = ReadReg(0x02);
|
||||
return data & 0x80; // RTCC_VLSEC_MASK
|
||||
}
|
||||
|
||||
uint8_t BM8563::bcd2ToByte(uint8_t value) {
|
||||
uint8_t tmp = 0;
|
||||
tmp = ((uint8_t)(value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
|
||||
return (tmp + (value & (uint8_t)0x0F));
|
||||
}
|
||||
|
||||
uint8_t BM8563::byteToBcd2(uint8_t value) {
|
||||
uint8_t bcdhigh = 0;
|
||||
|
||||
while (value >= 10) {
|
||||
bcdhigh++;
|
||||
value -= 10;
|
||||
}
|
||||
|
||||
return ((uint8_t)(bcdhigh << 4) | value);
|
||||
}
|
||||
|
||||
void BM8563::getTime(BM8563_TimeTypeDef* BM8563_TimeStruct) {
|
||||
uint8_t buf[3] = {0};
|
||||
|
||||
this->read_register(0x02, buf, 3);
|
||||
|
||||
BM8563_TimeStruct->seconds = bcd2ToByte(buf[0] & 0x7f);
|
||||
BM8563_TimeStruct->minutes = bcd2ToByte(buf[1] & 0x7f);
|
||||
BM8563_TimeStruct->hours = bcd2ToByte(buf[2] & 0x3f);
|
||||
}
|
||||
|
||||
void BM8563::setTime(BM8563_TimeTypeDef* BM8563_TimeStruct) {
|
||||
if (BM8563_TimeStruct == NULL) {
|
||||
return;
|
||||
}
|
||||
uint8_t buf[3] = {byteToBcd2(BM8563_TimeStruct->seconds), byteToBcd2(BM8563_TimeStruct->minutes), byteToBcd2(BM8563_TimeStruct->hours)};
|
||||
|
||||
this->write_register(0x02, buf, 3);
|
||||
}
|
||||
|
||||
void BM8563::getDate(BM8563_DateTypeDef* BM8563_DateStruct) {
|
||||
uint8_t buf[4] = {0};
|
||||
this->read_register(0x05, buf, 4);
|
||||
|
||||
BM8563_DateStruct->date = bcd2ToByte(buf[0] & 0x3f);
|
||||
BM8563_DateStruct->weekDay = bcd2ToByte(buf[1] & 0x07);
|
||||
BM8563_DateStruct->month = bcd2ToByte(buf[2] & 0x1f);
|
||||
|
||||
if (buf[2] & 0x80) {
|
||||
BM8563_DateStruct->year = 1900 + bcd2ToByte(buf[3] & 0xff);
|
||||
} else {
|
||||
BM8563_DateStruct->year = 2000 + bcd2ToByte(buf[3] & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
void BM8563::setDate(BM8563_DateTypeDef* BM8563_DateStruct) {
|
||||
if (BM8563_DateStruct == NULL) {
|
||||
return;
|
||||
}
|
||||
uint8_t buf[4] = {byteToBcd2(BM8563_DateStruct->date), byteToBcd2(BM8563_DateStruct->weekDay), 0, byteToBcd2((uint8_t)(BM8563_DateStruct->year % 100))};
|
||||
|
||||
|
||||
if (BM8563_DateStruct->year < 2000) {
|
||||
buf[3] = byteToBcd2(BM8563_DateStruct->month) | 0x80;
|
||||
} else {
|
||||
buf[3] = byteToBcd2(BM8563_DateStruct->month) | 0x00;
|
||||
}
|
||||
|
||||
this->write_register(0x05, buf, 4);
|
||||
}
|
||||
|
||||
void BM8563::WriteReg(uint8_t reg, uint8_t data) {
|
||||
this->write_byte(reg, data);
|
||||
}
|
||||
|
||||
uint8_t BM8563::ReadReg(uint8_t reg) {
|
||||
uint8_t data;
|
||||
this->read_register(reg, &data, 1);
|
||||
return data;
|
||||
}
|
||||
|
||||
int BM8563::SetAlarmIRQ(int afterSeconds) {
|
||||
uint8_t reg_value = 0;
|
||||
reg_value = ReadReg(0x01);
|
||||
|
||||
if (afterSeconds < 0) {
|
||||
reg_value &= ~(1 << 0);
|
||||
WriteReg(0x01, reg_value);
|
||||
reg_value = 0x03;
|
||||
WriteReg(0x0E, reg_value);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t type_value = 2;
|
||||
uint8_t div = 1;
|
||||
if (afterSeconds > 255) {
|
||||
div = 60;
|
||||
type_value = 0x83;
|
||||
} else {
|
||||
type_value = 0x82;
|
||||
}
|
||||
|
||||
afterSeconds = (afterSeconds / div) & 0xFF;
|
||||
WriteReg(0x0F, afterSeconds);
|
||||
WriteReg(0x0E, type_value);
|
||||
|
||||
reg_value |= (1 << 0);
|
||||
reg_value &= ~(1 << 7);
|
||||
WriteReg(0x01, reg_value);
|
||||
return afterSeconds * div;
|
||||
}
|
||||
|
||||
int BM8563::SetAlarmIRQ(const BM8563_TimeTypeDef &BM8563_TimeStruct) {
|
||||
uint8_t irq_enable = false;
|
||||
uint8_t out_buf[4] = {0x80, 0x80, 0x80, 0x80};
|
||||
|
||||
if (BM8563_TimeStruct.minutes >= 0) {
|
||||
irq_enable = true;
|
||||
out_buf[0] = byteToBcd2(BM8563_TimeStruct.minutes) & 0x7f;
|
||||
}
|
||||
|
||||
if (BM8563_TimeStruct.hours >= 0) {
|
||||
irq_enable = true;
|
||||
out_buf[1] = byteToBcd2(BM8563_TimeStruct.hours) & 0x3f;
|
||||
}
|
||||
|
||||
out_buf[2] = 0x00;
|
||||
out_buf[3] = 0x00;
|
||||
|
||||
uint8_t reg_value = ReadReg(0x01);
|
||||
|
||||
if (irq_enable) {
|
||||
reg_value |= (1 << 1);
|
||||
} else {
|
||||
reg_value &= ~(1 << 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
WriteReg(0x09 + i, out_buf[i]);
|
||||
}
|
||||
WriteReg(0x01, reg_value);
|
||||
|
||||
return irq_enable ? 1 : 0;
|
||||
}
|
||||
|
||||
int BM8563::SetAlarmIRQ(const BM8563_DateTypeDef &BM8563_DateStruct, const BM8563_TimeTypeDef &BM8563_TimeStruct) {
|
||||
uint8_t irq_enable = false;
|
||||
uint8_t out_buf[4] = {0x80, 0x80, 0x80, 0x80};
|
||||
|
||||
if (BM8563_TimeStruct.minutes >= 0) {
|
||||
irq_enable = true;
|
||||
out_buf[0] = byteToBcd2(BM8563_TimeStruct.minutes) & 0x7f;
|
||||
}
|
||||
|
||||
if (BM8563_TimeStruct.hours >= 0) {
|
||||
irq_enable = true;
|
||||
out_buf[1] = byteToBcd2(BM8563_TimeStruct.hours) & 0x3f;
|
||||
}
|
||||
|
||||
if (BM8563_DateStruct.date >= 0) {
|
||||
irq_enable = true;
|
||||
out_buf[2] = byteToBcd2(BM8563_DateStruct.date) & 0x3f;
|
||||
}
|
||||
|
||||
if (BM8563_DateStruct.weekDay >= 0) {
|
||||
irq_enable = true;
|
||||
out_buf[3] = byteToBcd2(BM8563_DateStruct.weekDay) & 0x07;
|
||||
}
|
||||
|
||||
uint8_t reg_value = ReadReg(0x01);
|
||||
|
||||
if (irq_enable) {
|
||||
reg_value |= (1 << 1);
|
||||
} else {
|
||||
reg_value &= ~(1 << 1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
WriteReg(0x09 + i, out_buf[i]);
|
||||
}
|
||||
WriteReg(0x01, reg_value);
|
||||
|
||||
return irq_enable ? 1 : 0;
|
||||
}
|
||||
|
||||
void BM8563::clearIRQ() {
|
||||
uint8_t data = ReadReg(0x01);
|
||||
WriteReg(0x01, data & 0xf3);
|
||||
}
|
||||
|
||||
void BM8563::disableIRQ() {
|
||||
clearIRQ();
|
||||
uint8_t data = ReadReg(0x01);
|
||||
WriteReg(0x01, data & 0xfC);
|
||||
}
|
||||
|
||||
} // namespace bm8563
|
||||
} // namespace esphome
|
||||
62
components/bm8563/bm8563.h
Normal file
62
components/bm8563/bm8563.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
|
||||
namespace esphome {
|
||||
namespace bm8563 {
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int8_t hours;
|
||||
int8_t minutes;
|
||||
int8_t seconds;
|
||||
} BM8563_TimeTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int8_t weekDay;
|
||||
int8_t month;
|
||||
int8_t date;
|
||||
int16_t year;
|
||||
} BM8563_DateTypeDef;
|
||||
|
||||
class BM8563 : public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
|
||||
void set_sleep_duration(uint32_t time_ms);
|
||||
|
||||
bool getVoltLow();
|
||||
|
||||
void getTime(BM8563_TimeTypeDef* BM8563_TimeStruct);
|
||||
void getDate(BM8563_DateTypeDef* BM8563_DateStruct);
|
||||
|
||||
void setTime(BM8563_TimeTypeDef* BM8563_TimeStruct);
|
||||
void setDate(BM8563_DateTypeDef* BM8563_DateStruct);
|
||||
|
||||
int SetAlarmIRQ(int afterSeconds);
|
||||
int SetAlarmIRQ(const BM8563_TimeTypeDef &BM8563_TimeStruct);
|
||||
int SetAlarmIRQ(const BM8563_DateTypeDef &BM8563_DateStruct, const BM8563_TimeTypeDef &BM8563_TimeStruct);
|
||||
|
||||
void clearIRQ();
|
||||
void disableIRQ();
|
||||
|
||||
void WriteReg(uint8_t reg, uint8_t data);
|
||||
uint8_t ReadReg(uint8_t reg);
|
||||
|
||||
private:
|
||||
uint8_t bcd2ToByte(uint8_t value);
|
||||
uint8_t byteToBcd2(uint8_t value);
|
||||
|
||||
uint8_t trdata[7];
|
||||
optional<uint64_t> sleep_duration_;
|
||||
bool setupComplete;
|
||||
};
|
||||
|
||||
} // namespace bm8563
|
||||
} // namespace esphome
|
||||
30
components/m5stack_4relay/__init__.py
Normal file
30
components/m5stack_4relay/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
MULTI_CONF = True
|
||||
AUTO_LOAD = ["switch"]
|
||||
CODEOWNERS = ["@brotherdust"]
|
||||
|
||||
m5stack_4relay_ns = cg.esphome_ns.namespace("m5stack_4relay")
|
||||
M5STACK4RELAYOutput = m5stack_4relay_ns.class_(
|
||||
"M5STACK4RELAYSwitchComponent", cg.Component, i2c.I2CDevice
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(M5STACK4RELAYOutput),
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(i2c.i2c_device_schema(0x26))
|
||||
)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
49
components/m5stack_4relay/m5stack_4relay.cpp
Normal file
49
components/m5stack_4relay/m5stack_4relay.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "m5stack_4relay.h"
|
||||
#include <bitset>
|
||||
#include "esphome/core/log.h"
|
||||
namespace esphome {
|
||||
namespace m5stack_4relay {
|
||||
static const char *const TAG = "m5stack_4relay.switch";
|
||||
|
||||
void M5STACK4RELAYSwitchComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up M5STACK4RELAY (0x%02X)...", this->address_);
|
||||
component_status = 0;
|
||||
get_status(&component_status);
|
||||
ESP_LOGD(TAG, "Setup Status 0x%02X", component_status);
|
||||
}
|
||||
|
||||
M5STACK4RELAYChannel *M5STACK4RELAYSwitchComponent::create_channel(uint8_t channel) {
|
||||
return new M5STACK4RELAYChannel(this, channel);
|
||||
}
|
||||
|
||||
void M5STACK4RELAYSwitchComponent::get_status(uint8_t *state) { this->read_byte(RELAY_CONTROL_REG); }
|
||||
|
||||
bool M5STACK4RELAYSwitchComponent::set_channel_value_(uint8_t channel, bool state) {
|
||||
get_status(&component_status);
|
||||
|
||||
ESP_LOGD(TAG, "Current status 0x%02X", component_status);
|
||||
ESP_LOGD(TAG, "Desired channel: %1u", channel);
|
||||
ESP_LOGD(TAG, "Desired state: %1u", state);
|
||||
|
||||
channel = 3 - channel;
|
||||
if (state) {
|
||||
component_status |= (1u << channel);
|
||||
} else {
|
||||
component_status &= ~(1u << channel);
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "New status 0x%02X", component_status);
|
||||
|
||||
return this->write_byte(this->address_, RELAY_CONTROL_REG, component_status);
|
||||
}
|
||||
|
||||
void M5STACK4RELAYChannel::write_state(bool state) {
|
||||
if (!this->set_channel_value_(this->channel_, state)) {
|
||||
publish_state(false);
|
||||
} else {
|
||||
publish_state(state);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace m5stack_4relay
|
||||
} // namespace esphome
|
||||
49
components/m5stack_4relay/m5stack_4relay.h
Normal file
49
components/m5stack_4relay/m5stack_4relay.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/switch/switch.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace m5stack_4relay {
|
||||
|
||||
class M5STACK4RELAYSwitchComponent;
|
||||
class M5STACK4RELAYChannel : public switch_::Switch {
|
||||
public:
|
||||
M5STACK4RELAYChannel(M5STACK4RELAYSwitchComponent *parent, uint8_t channel) : parent_(parent), channel_(channel) {}
|
||||
|
||||
protected:
|
||||
void write_state(bool state) override;
|
||||
|
||||
M5STACK4RELAYSwitchComponent *parent_;
|
||||
uint8_t channel_;
|
||||
};
|
||||
|
||||
class M5STACK4RELAYSwitchComponent : public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
const uint8_t MODE_CONTROL_REG = 0x10;
|
||||
const uint8_t RELAY_CONTROL_REG = 0x11;
|
||||
uint8_t component_status;
|
||||
M5STACK4RELAYSwitchComponent() {}
|
||||
M5STACK4RELAYChannel *create_channel(uint8_t channel);
|
||||
|
||||
void setup() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
protected:
|
||||
friend M5STACK4RELAYChannel;
|
||||
|
||||
void get_status(uint8_t *state);
|
||||
|
||||
bool set_channel_value_(uint8_t channel, bool state);
|
||||
|
||||
bool read_bytes_(uint8_t a_register, uint8_t *data, uint8_t len, uint32_t conversion) {
|
||||
return this->raw_receive(this->address_, data, len);
|
||||
}
|
||||
|
||||
float value_;
|
||||
};
|
||||
|
||||
} // namespace m5stack_4relay
|
||||
} // namespace esphome
|
||||
25
components/m5stack_4relay/switch.py
Normal file
25
components/m5stack_4relay/switch.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import switch
|
||||
from esphome.const import CONF_ID, CONF_CHANNEL
|
||||
from . import M5STACK4RELAYOutput, m5stack_4relay_ns
|
||||
|
||||
DEPENDENCIES = ["m5stack_4relay"]
|
||||
|
||||
M5STACK4RELAYChannel = m5stack_4relay_ns.class_("M5STACK4RELAYChannel", switch.Switch)
|
||||
CONF_M5STACK_4RELAY_ID = "m5stack_4relay_id"
|
||||
|
||||
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(M5STACK4RELAYChannel),
|
||||
cv.GenerateID(CONF_M5STACK_4RELAY_ID): cv.use_id(M5STACK4RELAYOutput),
|
||||
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=3),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
paren = await cg.get_variable(config[CONF_M5STACK_4RELAY_ID])
|
||||
rhs = paren.create_channel(config[CONF_CHANNEL])
|
||||
var = cg.Pvariable(config[CONF_ID], rhs)
|
||||
await switch.register_switch(var, config)
|
||||
1
components/notes.txt
Normal file
1
components/notes.txt
Normal file
@@ -0,0 +1 @@
|
||||
https://github.com/TomG736/esphome-BM8563
|
||||
28
components/pca9536d/README.md
Normal file
28
components/pca9536d/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# PCA9536D i2c I/O expander
|
||||
|
||||
Requires a configured i2c bus
|
||||
|
||||
Example:
|
||||
```yaml
|
||||
pca9536d:
|
||||
- id: my_pca
|
||||
|
||||
switch:
|
||||
- platform: gpio
|
||||
pin:
|
||||
pca9536d: my_pca
|
||||
number: 0
|
||||
id: relay
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
pin:
|
||||
pca9536d: my_pca
|
||||
number: 1
|
||||
id: button
|
||||
```
|
||||
|
||||
# Optional parameters
|
||||
|
||||
`address:` defaults to 0x41
|
||||
|
||||
69
components/pca9536d/__init__.py
Normal file
69
components/pca9536d/__init__.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import i2c
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_INPUT,
|
||||
CONF_INVERTED,
|
||||
CONF_NUMBER,
|
||||
CONF_MODE,
|
||||
CONF_OUTPUT,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
MULTI_CONF = True
|
||||
|
||||
CONF_PCA9536D = "pca9536d"
|
||||
|
||||
pca9536d_ns = cg.esphome_ns.namespace("pca9536d")
|
||||
PCA9536DGPIOMode = pca9536d_ns.enum("PCA9536DGPIOMode")
|
||||
|
||||
PCA9536D = pca9536d_ns.class_("PCA9536D", cg.Component, i2c.I2CDevice)
|
||||
PCA9536DGPIOPin = pca9536d_ns.class_("PCA9536DGPIOPin", cg.GPIOPin)
|
||||
|
||||
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(PCA9536D),
|
||||
}
|
||||
).extend(i2c.i2c_device_schema(0x41))
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
def validate_mode(value):
|
||||
if not (value[CONF_INPUT] or value[CONF_OUTPUT]):
|
||||
raise cv.Invalid("Mode must be either input or output")
|
||||
if value[CONF_INPUT] and value[CONF_OUTPUT]:
|
||||
raise cv.Invalid("Mode must be either input or output")
|
||||
return value
|
||||
|
||||
PCA9536D_PIN_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(PCA9536DGPIOPin),
|
||||
cv.Required(CONF_PCA9536D): cv.use_id(PCA9536D),
|
||||
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=7),
|
||||
cv.Optional(CONF_MODE, default={}): cv.All(
|
||||
{
|
||||
cv.Optional(CONF_INPUT, default=False): cv.boolean,
|
||||
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
|
||||
},
|
||||
validate_mode,
|
||||
),
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
|
||||
@pins.PIN_SCHEMA_REGISTRY.register(CONF_PCA9536D, PCA9536D_PIN_SCHEMA)
|
||||
async def pca9536d_pin_to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
parent = await cg.get_variable(config[CONF_PCA9536D])
|
||||
|
||||
cg.add(var.set_parent(parent))
|
||||
cg.add(var.set_pin(config[CONF_NUMBER]))
|
||||
cg.add(var.set_inverted(config[CONF_INVERTED]))
|
||||
cg.add(var.set_flags(pins.gpio_flags_expr(config[CONF_MODE])))
|
||||
return var
|
||||
|
||||
BIN
components/pca9536d/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
components/pca9536d/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
108
components/pca9536d/pca9536d.cpp
Normal file
108
components/pca9536d/pca9536d.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
#include "pca9536d.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pca9536d {
|
||||
|
||||
static const char *TAG = "pca9536d";
|
||||
|
||||
void PCA9536D::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up PCA9536D...");
|
||||
if (!this->read_gpio_()) {
|
||||
ESP_LOGE(TAG, "PCA9536D not available under 0x%02X", this->address_);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
this->write_gpio_();
|
||||
}
|
||||
|
||||
void PCA9536D::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "PCA9536D:");
|
||||
LOG_I2C_DEVICE(this)
|
||||
if (this->is_failed())
|
||||
ESP_LOGE(TAG, "Communication with PCA9536D failed!");
|
||||
}
|
||||
|
||||
bool PCA9536D::digital_read(uint8_t pin) {
|
||||
this->read_gpio_();
|
||||
return this->inputs_ & (1 << pin);
|
||||
}
|
||||
|
||||
void PCA9536D::digital_write(uint8_t pin, bool value) {
|
||||
if (value)
|
||||
this->outputs_ |= (1 << pin);
|
||||
else
|
||||
this->outputs_ &= ~(1 << pin);
|
||||
this->write_gpio_();
|
||||
}
|
||||
|
||||
void PCA9536D::set_pin_mode(uint8_t pin, uint8_t mode) {
|
||||
switch (mode) {
|
||||
case gpio::FLAG_INPUT:
|
||||
this->modes_ |= 1 << pin;
|
||||
break;
|
||||
case gpio::FLAG_OUTPUT:
|
||||
this->modes_ &= ~(1 << pin);
|
||||
break;
|
||||
}
|
||||
this->set_modes_();
|
||||
}
|
||||
|
||||
bool PCA9536D::read_gpio_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint8_t data;
|
||||
if (!this->read_bytes(0, &data, 1)) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
|
||||
this->inputs_ = data;
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PCA9536D::write_gpio_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint8_t data = this->outputs_;
|
||||
if (!this->write_bytes(1, &data, 1)) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PCA9536D::set_modes_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint8_t data = this->modes_;
|
||||
if (!this->write_bytes(3, &data, 1)) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
|
||||
void PCA9536DGPIOPin::setup() { this->pin_mode(this->flags_); }
|
||||
|
||||
bool PCA9536DGPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
|
||||
void PCA9536DGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
|
||||
void PCA9536DGPIOPin::pin_mode(gpio::Flags flags) { this->parent_->set_pin_mode(this->pin_, flags); }
|
||||
|
||||
std::string PCA9536DGPIOPin::dump_summary() const {
|
||||
return str_sprintf("%u via PCA9536D", pin_);
|
||||
}
|
||||
|
||||
} // namespace pca9536d
|
||||
} // namespace esphome
|
||||
52
components/pca9536d/pca9536d.h
Normal file
52
components/pca9536d/pca9536d.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pca9536d {
|
||||
|
||||
class PCA9536D: public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
float get_setup_priority() const { return setup_priority::IO; }
|
||||
void dump_config() override;
|
||||
|
||||
bool digital_read(uint8_t pin);
|
||||
void digital_write(uint8_t pin, bool value);
|
||||
void set_pin_mode(uint8_t pin, uint8_t mode);
|
||||
|
||||
protected:
|
||||
bool read_gpio_();
|
||||
bool write_gpio_();
|
||||
bool set_modes_();
|
||||
|
||||
// pin modes - 0 means output, 1 means input
|
||||
uint8_t modes_{0xff};
|
||||
uint8_t outputs_{0x00};
|
||||
uint8_t inputs_{0x00};
|
||||
};
|
||||
|
||||
class PCA9536DGPIOPin : public GPIOPin {
|
||||
public:
|
||||
void setup() override;
|
||||
void pin_mode(gpio::Flags flags) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
std::string dump_summary() const override;
|
||||
|
||||
void set_parent(PCA9536D *parent) { parent_ = parent; }
|
||||
void set_pin(uint8_t pin) { pin_ = pin; }
|
||||
void set_inverted(bool inverted) { inverted_ = inverted; }
|
||||
void set_flags(gpio::Flags flags) { flags_ = flags; }
|
||||
|
||||
protected:
|
||||
PCA9536D *parent_;
|
||||
uint8_t pin_;
|
||||
bool inverted_;
|
||||
gpio::Flags flags_;
|
||||
};
|
||||
|
||||
} // namespace pca9536d
|
||||
} // namespace esphome
|
||||
57
components/pca9554_2/__init__.py
Normal file
57
components/pca9554_2/__init__.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import i2c
|
||||
from esphome.const import CONF_ID, CONF_NUMBER, CONF_MODE, CONF_INVERTED
|
||||
|
||||
DEPENDENCIES = ['i2c']
|
||||
MULTI_CONF = True
|
||||
|
||||
pca9554_ns = cg.esphome_ns.namespace('pca9554')
|
||||
PCA9554GPIOMode = pca9554_ns.enum('PCA9554GPIOMode')
|
||||
PCF8674_GPIO_MODES = {
|
||||
'INPUT': PCA9554GPIOMode.PCA9554_INPUT,
|
||||
'OUTPUT': PCA9554GPIOMode.PCA9554_OUTPUT,
|
||||
}
|
||||
|
||||
PCA9554Component = pca9554_ns.class_('PCA9554Component', cg.Component, i2c.I2CDevice)
|
||||
PCA9554GPIOPin = pca9554_ns.class_('PCA9554GPIOPin', cg.GPIOPin)
|
||||
|
||||
CONF_PCA9554 = 'pca9554'
|
||||
CONF_PCF8575 = 'pcf8575'
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.Required(CONF_ID): cv.declare_id(PCA9554Component),
|
||||
cv.Optional(CONF_PCF8575, default=False): cv.boolean,
|
||||
}).extend(cv.COMPONENT_SCHEMA).extend(i2c.i2c_device_schema(0x20))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
cg.add(var.set_pcf8575(config[CONF_PCF8575]))
|
||||
|
||||
|
||||
def validate_pca9554_gpio_mode(value):
|
||||
value = cv.string(value)
|
||||
return cv.enum(PCF8674_GPIO_MODES, upper=True)(value)
|
||||
|
||||
|
||||
PCA9554_OUTPUT_PIN_SCHEMA = cv.Schema({
|
||||
cv.Required(CONF_PCA9554): cv.use_id(PCA9554Component),
|
||||
cv.Required(CONF_NUMBER): cv.int_,
|
||||
cv.Optional(CONF_MODE, default="OUTPUT"): validate_pca9554_gpio_mode,
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
})
|
||||
PCA9554_INPUT_PIN_SCHEMA = cv.Schema({
|
||||
cv.Required(CONF_PCA9554): cv.use_id(PCA9554Component),
|
||||
cv.Required(CONF_NUMBER): cv.int_,
|
||||
cv.Optional(CONF_MODE, default="INPUT"): validate_pca9554_gpio_mode,
|
||||
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
|
||||
})
|
||||
|
||||
|
||||
@pins.PIN_SCHEMA_REGISTRY.register('pca9554', (PCA9554_OUTPUT_PIN_SCHEMA, PCA9554_INPUT_PIN_SCHEMA))
|
||||
def pca9554_pin_to_code(config):
|
||||
parent = yield cg.get_variable(config[CONF_PCA9554])
|
||||
yield PCA9554GPIOPin.new(parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED])
|
||||
96
components/pca9554_2/pca9554.cpp
Normal file
96
components/pca9554_2/pca9554.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "pca9554.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pca9554 {
|
||||
|
||||
static const char *TAG = "pca9554";
|
||||
|
||||
void PCA9554Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up PCA9554...");
|
||||
if (!this->read_gpio_()) {
|
||||
ESP_LOGE(TAG, "PCA9554 not available under 0x%02X", this->address_);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
this->write_gpio_();
|
||||
this->read_gpio_();
|
||||
}
|
||||
void PCA9554Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "PCA9554:");
|
||||
LOG_I2C_DEVICE(this)
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, "Communication with PCA9554 failed!");
|
||||
}
|
||||
}
|
||||
bool PCA9554Component::digital_read(uint8_t pin) {
|
||||
this->read_gpio_();
|
||||
return this->input_mask_ & (1 << pin);
|
||||
}
|
||||
void PCA9554Component::digital_write(uint8_t pin, bool value) {
|
||||
if (value) {
|
||||
this->output_mask_ |= (1 << pin);
|
||||
} else {
|
||||
this->output_mask_ &= ~(1 << pin);
|
||||
}
|
||||
|
||||
this->write_gpio_();
|
||||
}
|
||||
void PCA9554Component::pin_mode(uint8_t pin, uint8_t mode) {
|
||||
switch (mode) {
|
||||
case PCA9554_OUTPUT:
|
||||
// Clear mode mask bit
|
||||
this->mode_mask_ &= ~(1 << pin);
|
||||
// Write GPIO to enable input mode
|
||||
this->write_gpio_();
|
||||
break;
|
||||
case PCA9554_INPUT:
|
||||
// Set mode mask bit
|
||||
this->mode_mask_ |= 1 << pin;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool PCA9554Component::read_gpio_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
bool success;
|
||||
uint8_t data[2];
|
||||
|
||||
if (!success) {
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
}
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
bool PCA9554Component::write_gpio_() {
|
||||
if (this->is_failed())
|
||||
return false;
|
||||
|
||||
uint16_t value = 0;
|
||||
// Pins in OUTPUT mode and where pin is HIGH.
|
||||
value |= this->mode_mask_ & this->output_mask_;
|
||||
// Pins in INPUT mode must also be set here
|
||||
value |= ~this->mode_mask_;
|
||||
|
||||
uint8_t data[2];
|
||||
data[0] = value;
|
||||
data[1] = value >> 8;
|
||||
|
||||
this->status_clear_warning();
|
||||
return true;
|
||||
}
|
||||
float PCA9554Component::get_setup_priority() const { return setup_priority::IO; }
|
||||
|
||||
void PCA9554GPIOPin::setup() { this->pin_mode(this->mode_); }
|
||||
bool PCA9554GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
|
||||
void PCA9554GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
|
||||
void PCA9554GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); }
|
||||
PCA9554GPIOPin::PCA9554GPIOPin(PCA9554Component *parent, uint8_t pin, uint8_t mode, bool inverted)
|
||||
: GPIOPin(pin, mode, inverted), parent_(parent) {}
|
||||
|
||||
} // namespace pca9554
|
||||
} // namespace esphome
|
||||
61
components/pca9554_2/pca9554.h
Normal file
61
components/pca9554_2/pca9554.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/esphal.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace pca9554 {
|
||||
|
||||
/// Modes for PCA9554 pins
|
||||
enum PCA9554GPIOMode : uint8_t {
|
||||
PCA9554_INPUT = INPUT,
|
||||
PCA9554_OUTPUT = OUTPUT,
|
||||
};
|
||||
|
||||
class PCA9554Component : public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
PCA9554Component() = default;
|
||||
|
||||
/// Check i2c availability and setup masks
|
||||
void setup() override;
|
||||
/// Helper function to read the value of a pin.
|
||||
bool digital_read(uint8_t pin);
|
||||
/// Helper function to write the value of a pin.
|
||||
void digital_write(uint8_t pin, bool value);
|
||||
/// Helper function to set the pin mode of a pin.
|
||||
void pin_mode(uint8_t pin, uint8_t mode);
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
bool read_gpio_();
|
||||
|
||||
bool write_gpio_();
|
||||
|
||||
/// Mask for the pin mode - 1 means input, 0 means output
|
||||
uint16_t mode_mask_{0x00};
|
||||
/// The mask to write as output state - 1 means HIGH, 0 means LOW
|
||||
uint16_t output_mask_{0x00};
|
||||
/// The state read in read_gpio_ - 1 means HIGH, 0 means LOW
|
||||
uint16_t input_mask_{0x00};
|
||||
};
|
||||
|
||||
/// Helper class to expose a PCA9554 pin as an internal input GPIO pin.
|
||||
class PCA9554GPIOPin : public GPIOPin {
|
||||
public:
|
||||
PCA9554GPIOPin(PCA9554Component *parent, uint8_t pin, uint8_t mode, bool inverted = false);
|
||||
|
||||
void setup() override;
|
||||
void pin_mode(uint8_t mode) override;
|
||||
bool digital_read() override;
|
||||
void digital_write(bool value) override;
|
||||
|
||||
protected:
|
||||
PCA9554Component *parent_;
|
||||
};
|
||||
|
||||
} // namespace pca9554
|
||||
} // namespace esphome
|
||||
1
components/sim7600
Submodule
1
components/sim7600
Submodule
Submodule components/sim7600 added at d754e306ad
3
components/st7735/__init__.py
Normal file
3
components/st7735/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import esphome.codegen as cg
|
||||
|
||||
st7735_ns = cg.esphome_ns.namespace('st7735')
|
||||
BIN
components/st7735/__pycache__/__init__.cpython-36.pyc
Normal file
BIN
components/st7735/__pycache__/__init__.cpython-36.pyc
Normal file
Binary file not shown.
BIN
components/st7735/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
components/st7735/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
components/st7735/__pycache__/display.cpython-36.pyc
Normal file
BIN
components/st7735/__pycache__/display.cpython-36.pyc
Normal file
Binary file not shown.
BIN
components/st7735/__pycache__/display.cpython-39.pyc
Normal file
BIN
components/st7735/__pycache__/display.cpython-39.pyc
Normal file
Binary file not shown.
41
components/st7735/display.py
Normal file
41
components/st7735/display.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import display, spi
|
||||
from esphome.const import CONF_ID, CONF_LAMBDA
|
||||
from esphome.const import CONF_DC_PIN, CONF_CS_PIN, CONF_ID, CONF_LAMBDA, CONF_PAGES
|
||||
from esphome.const import CONF_EXTERNAL_VCC, CONF_LAMBDA, CONF_MODEL, CONF_RESET_PIN, \
|
||||
CONF_BRIGHTNESS
|
||||
from . import st7735_ns
|
||||
|
||||
DEPENDENCIES = ['spi']
|
||||
|
||||
ST7735 = st7735_ns.class_('ST7735', cg.PollingComponent, spi.SPIDevice)
|
||||
ST7735Ref = ST7735.operator('ref')
|
||||
|
||||
CONFIG_SCHEMA = display.FULL_DISPLAY_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(ST7735),
|
||||
cv.Required(CONF_RESET_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage,
|
||||
}).extend(cv.polling_component_schema('1s')).extend(spi.spi_device_schema())
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
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))
|
||||
|
||||
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN])
|
||||
cg.add(var.set_reset_pin(reset))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
lambda_ = yield cg.process_lambda(
|
||||
config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')], return_type=cg.void)
|
||||
cg.add(var.set_writer(lambda_))
|
||||
|
||||
yield display.register_display(var, config)
|
||||
351
components/st7735/st7735.cpp
Normal file
351
components/st7735/st7735.cpp
Normal file
@@ -0,0 +1,351 @@
|
||||
#include "st7735.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/display/display_buffer.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace st7735 {
|
||||
|
||||
#define ST_CMD_DELAY 0x80 // special signifier for command lists
|
||||
|
||||
#define ST77XX_NOP 0x00
|
||||
#define ST77XX_SWRESET 0x01
|
||||
#define ST77XX_RDDID 0x04
|
||||
#define ST77XX_RDDST 0x09
|
||||
|
||||
#define ST77XX_SLPIN 0x10
|
||||
#define ST77XX_SLPOUT 0x11
|
||||
#define ST77XX_PTLON 0x12
|
||||
#define ST77XX_NORON 0x13
|
||||
|
||||
#define ST77XX_INVOFF 0x20
|
||||
#define ST77XX_INVON 0x21
|
||||
#define ST77XX_DISPOFF 0x28
|
||||
#define ST77XX_DISPON 0x29
|
||||
#define ST77XX_CASET 0x2A
|
||||
#define ST77XX_RASET 0x2B
|
||||
#define ST77XX_RAMWR 0x2C
|
||||
#define ST77XX_RAMRD 0x2E
|
||||
|
||||
#define ST77XX_PTLAR 0x30
|
||||
#define ST77XX_TEOFF 0x34
|
||||
#define ST77XX_TEON 0x35
|
||||
#define ST77XX_MADCTL 0x36
|
||||
#define ST77XX_COLMOD 0x3A
|
||||
|
||||
#define ST77XX_MADCTL_MY 0x80
|
||||
#define ST77XX_MADCTL_MX 0x40
|
||||
#define ST77XX_MADCTL_MV 0x20
|
||||
#define ST77XX_MADCTL_ML 0x10
|
||||
#define ST77XX_MADCTL_RGB 0x00
|
||||
|
||||
#define ST77XX_RDID1 0xDA
|
||||
#define ST77XX_RDID2 0xDB
|
||||
#define ST77XX_RDID3 0xDC
|
||||
#define ST77XX_RDID4 0xDD
|
||||
|
||||
|
||||
// some flags for initR() :(
|
||||
#define INITR_GREENTAB 0x00
|
||||
#define INITR_REDTAB 0x01
|
||||
#define INITR_BLACKTAB 0x02
|
||||
#define INITR_18GREENTAB INITR_GREENTAB
|
||||
#define INITR_18REDTAB INITR_REDTAB
|
||||
#define INITR_18BLACKTAB INITR_BLACKTAB
|
||||
#define INITR_144GREENTAB 0x01
|
||||
#define INITR_MINI160x80 0x04
|
||||
#define INITR_HALLOWING 0x05
|
||||
|
||||
// Some register settings
|
||||
#define ST7735_MADCTL_BGR 0x08
|
||||
#define ST7735_MADCTL_MH 0x04
|
||||
|
||||
#define ST7735_FRMCTR1 0xB1
|
||||
#define ST7735_FRMCTR2 0xB2
|
||||
#define ST7735_FRMCTR3 0xB3
|
||||
#define ST7735_INVCTR 0xB4
|
||||
#define ST7735_DISSET5 0xB6
|
||||
|
||||
#define ST7735_PWCTR1 0xC0
|
||||
#define ST7735_PWCTR2 0xC1
|
||||
#define ST7735_PWCTR3 0xC2
|
||||
#define ST7735_PWCTR4 0xC3
|
||||
#define ST7735_PWCTR5 0xC4
|
||||
#define ST7735_VMCTR1 0xC5
|
||||
|
||||
#define ST7735_PWCTR6 0xFC
|
||||
|
||||
#define ST7735_GMCTRP1 0xE0
|
||||
#define ST7735_GMCTRN1 0xE1
|
||||
|
||||
static const uint8_t PROGMEM
|
||||
Rcmd1[] = { // 7735R init, part 1 (red or green tab)
|
||||
15, // 15 commands in list:
|
||||
ST77XX_SWRESET, ST_CMD_DELAY, // 1: Software reset, 0 args, w/delay
|
||||
150, // 150 ms delay
|
||||
ST77XX_SLPOUT, ST_CMD_DELAY, // 2: Out of sleep mode, 0 args, w/delay
|
||||
255, // 500 ms delay
|
||||
ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg:
|
||||
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
||||
ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args:
|
||||
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
|
||||
ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args:
|
||||
0x01, 0x2C, 0x2D, // Dot inversion mode
|
||||
0x01, 0x2C, 0x2D, // Line inversion mode
|
||||
ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg:
|
||||
0x07, // No inversion
|
||||
ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
|
||||
0xA2,
|
||||
0x02, // -4.6V
|
||||
0x84, // AUTO mode
|
||||
ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
|
||||
0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD
|
||||
ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
|
||||
0x0A, // Opamp current small
|
||||
0x00, // Boost frequency
|
||||
ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
|
||||
0x8A, // BCLK/2,
|
||||
0x2A, // opamp current small & medium low
|
||||
ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
|
||||
0x8A, 0xEE,
|
||||
ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
|
||||
0x0E,
|
||||
ST77XX_INVOFF, 0, // 13: Don't invert display, no args
|
||||
ST77XX_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg:
|
||||
0xC8, // row/col addr, bottom-top refresh
|
||||
ST77XX_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
|
||||
0x05 }, // 16-bit color
|
||||
|
||||
Rcmd2green[] = { // 7735R init, part 2 (green tab only)
|
||||
2, // 2 commands in list:
|
||||
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
|
||||
0x00, 0x02, // XSTART = 0
|
||||
0x00, 0x7F+0x02, // XEND = 127
|
||||
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
|
||||
0x00, 0x01, // XSTART = 0
|
||||
0x00, 0x9F+0x01 }, // XEND = 159
|
||||
Rcmd2green144[] = { // 7735R init, part 2 (green 1.44 tab)
|
||||
2, // 2 commands in list:
|
||||
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
|
||||
0x00, 0x00, // XSTART = 0
|
||||
0x00, 0x7F, // XEND = 127
|
||||
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
|
||||
0x00, 0x00, // XSTART = 0
|
||||
0x00, 0x7F }, // XEND = 127
|
||||
Rcmd3[] = { // 7735R init, part 3 (red or green tab)
|
||||
4, // 4 commands in list:
|
||||
ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args + delay:
|
||||
0x02, 0x1c, 0x07, 0x12, // (Not entirely necessary, but provides
|
||||
0x37, 0x32, 0x29, 0x2d, // accurate colors)
|
||||
0x29, 0x25, 0x2B, 0x39,
|
||||
0x00, 0x01, 0x03, 0x10,
|
||||
ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args + delay:
|
||||
0x03, 0x1d, 0x07, 0x06, // (Not entirely necessary, but provides
|
||||
0x2E, 0x2C, 0x29, 0x2D, // accurate colors)
|
||||
0x2E, 0x2E, 0x37, 0x3F,
|
||||
0x00, 0x00, 0x02, 0x10,
|
||||
ST77XX_NORON, ST_CMD_DELAY, // 3: Normal display on, no args, w/delay
|
||||
10, // 10 ms delay
|
||||
ST77XX_DISPON, ST_CMD_DELAY, // 4: Main screen turn on, no args w/delay
|
||||
100 }; // 100 ms delay
|
||||
|
||||
static const char *TAG = "st7735";
|
||||
|
||||
void ST7735::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up SPI ST7735...");
|
||||
this->spi_setup();
|
||||
this->dc_pin_->setup(); // OUTPUT
|
||||
|
||||
this->init_reset_();
|
||||
|
||||
this->displayInit(Rcmd1);
|
||||
this->displayInit(Rcmd2green);
|
||||
this->displayInit(Rcmd3);
|
||||
this->writecommand(ST7735_INVON);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//this->disable();
|
||||
delay(120);
|
||||
//this->enable();
|
||||
|
||||
this->writecommand(ST7735_DISPON); //Display on
|
||||
delay(120);
|
||||
|
||||
this->init_internal_(this->get_buffer_length_());
|
||||
memset(this->buffer_, 0x00, this->get_buffer_length_());
|
||||
}
|
||||
|
||||
void ST7735::write_display_data() {
|
||||
|
||||
uint16_t offsetx = 26;
|
||||
uint16_t offsety = 1;
|
||||
|
||||
uint16_t x1 = offsetx;
|
||||
uint16_t x2 = x1 + get_width_internal()-1;
|
||||
uint16_t y1 = offsety;
|
||||
uint16_t y2 = y1 + get_height_internal()-1;
|
||||
|
||||
this->enable();
|
||||
|
||||
// set column(x) address
|
||||
this->dc_pin_->digital_write(false);
|
||||
this->write_byte(ST77XX_CASET);
|
||||
this->dc_pin_->digital_write(true);
|
||||
this->spi_master_write_addr(x1, x2);
|
||||
|
||||
// set Page(y) address
|
||||
this->dc_pin_->digital_write(false);
|
||||
this->write_byte(ST77XX_RASET);
|
||||
this->dc_pin_->digital_write(true);
|
||||
this->spi_master_write_addr(y1, y2);
|
||||
|
||||
// Memory Write
|
||||
this->dc_pin_->digital_write(false);
|
||||
this->write_byte(ST77XX_RAMWR);
|
||||
this->dc_pin_->digital_write(true);
|
||||
|
||||
this->write_array(this->buffer_, this->get_buffer_length_());
|
||||
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void ST7735::spi_master_write_addr(uint16_t addr1, uint16_t addr2)
|
||||
{
|
||||
static uint8_t Byte[4];
|
||||
Byte[0] = (addr1 >> 8) & 0xFF;
|
||||
Byte[1] = addr1 & 0xFF;
|
||||
Byte[2] = (addr2 >> 8) & 0xFF;
|
||||
Byte[3] = addr2 & 0xFF;
|
||||
|
||||
|
||||
this->dc_pin_->digital_write(true);
|
||||
this->write_array(Byte, 4);
|
||||
|
||||
}
|
||||
|
||||
void ST7735::spi_master_write_color(uint16_t color, uint16_t size)
|
||||
{
|
||||
static uint8_t Byte[1024];
|
||||
int index = 0;
|
||||
for(int i=0;i<size;i++) {
|
||||
Byte[index++] = (color >> 8) & 0xFF;
|
||||
Byte[index++] = color & 0xFF;
|
||||
}
|
||||
|
||||
this->dc_pin_->digital_write(true);
|
||||
return write_array(Byte, size*2);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ST7735::dump_config() {
|
||||
LOG_DISPLAY("", "SPI ST7735", this);
|
||||
LOG_PIN(" CS Pin: ", this->cs_);
|
||||
LOG_PIN(" DC Pin: ", this->dc_pin_);
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
float ST7735::get_setup_priority() const {
|
||||
return setup_priority::PROCESSOR;
|
||||
}
|
||||
|
||||
void ST7735::update() {
|
||||
this->do_update_();
|
||||
this->write_display_data();
|
||||
}
|
||||
|
||||
void ST7735::loop() {
|
||||
|
||||
}
|
||||
|
||||
int ST7735::get_width_internal() {
|
||||
return 80;
|
||||
}
|
||||
|
||||
int ST7735::get_height_internal() {
|
||||
return 160;
|
||||
}
|
||||
|
||||
size_t ST7735::get_buffer_length_() {
|
||||
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
|
||||
}
|
||||
|
||||
void HOT ST7735::draw_absolute_pixel_internal(int x, int y, Color color) {
|
||||
if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
|
||||
return;
|
||||
const uint32_t color565 = color.to_rgb_565();
|
||||
// where should the bits go in the big buffer array? math...
|
||||
uint16_t pos = (x + y * this->get_width_internal()) * 2;
|
||||
this->buffer_[pos++] = (color565 >> 8) & 0xff;
|
||||
this->buffer_[pos] = color565 & 0xff;
|
||||
}
|
||||
|
||||
void ST7735::displayInit(const uint8_t *addr) {
|
||||
|
||||
uint8_t numCommands, cmd, numArgs;
|
||||
uint16_t ms;
|
||||
|
||||
numCommands = pgm_read_byte(addr++); // Number of commands to follow
|
||||
while(numCommands--) { // For each command...
|
||||
cmd = pgm_read_byte(addr++); // Read command
|
||||
numArgs = pgm_read_byte(addr++); // Number of args to follow
|
||||
ms = numArgs & ST_CMD_DELAY; // If hibit set, delay follows args
|
||||
numArgs &= ~ST_CMD_DELAY; // Mask out delay bit
|
||||
this->sendcommand(cmd, addr, numArgs);
|
||||
addr += numArgs;
|
||||
|
||||
if(ms) {
|
||||
ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
|
||||
if(ms == 255) ms = 500; // If 255, delay for 500 ms
|
||||
delay(ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ST7735::init_reset_() {
|
||||
if (this->reset_pin_ != nullptr) {
|
||||
this->reset_pin_->setup();
|
||||
this->reset_pin_->digital_write(true);
|
||||
delay(1);
|
||||
// Trigger Reset
|
||||
this->reset_pin_->digital_write(false);
|
||||
delay(10);
|
||||
// Wake up
|
||||
this->reset_pin_->digital_write(true);
|
||||
}
|
||||
}
|
||||
|
||||
void ST7735::writecommand(uint8_t value) {
|
||||
this->enable();
|
||||
this->dc_pin_->digital_write(false);
|
||||
this->write_byte(value);
|
||||
this->dc_pin_->digital_write(true);
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void ST7735::writedata(uint8_t value) {
|
||||
this->dc_pin_->digital_write(true);
|
||||
this->enable();
|
||||
this->write_byte(value);
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void ST7735::sendcommand(uint8_t cmd, const uint8_t* dataBytes, uint8_t numDataBytes) {
|
||||
this->writecommand(cmd);
|
||||
this->senddata(dataBytes,numDataBytes);
|
||||
}
|
||||
|
||||
void ST7735::senddata(const uint8_t* dataBytes, uint8_t numDataBytes) {
|
||||
this->dc_pin_->digital_write(true); //pull DC high to indicate data
|
||||
this->cs_->digital_write(false);
|
||||
this->enable();
|
||||
for (uint8_t i=0; i<numDataBytes; i++) {
|
||||
this->transfer_byte(pgm_read_byte(dataBytes++)); //write byte - SPI library
|
||||
}
|
||||
this->cs_->digital_write(true);
|
||||
this->disable();
|
||||
|
||||
}
|
||||
|
||||
} // namespace st7735
|
||||
} // namespace esphome
|
||||
149
components/st7735/st7735.h
Normal file
149
components/st7735/st7735.h
Normal file
@@ -0,0 +1,149 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
#include "esphome/components/display/display_buffer.h"
|
||||
|
||||
// ST7735 specific commands used in init
|
||||
#define ST7735_NOP 0x00
|
||||
#define ST7735_SWRESET 0x01
|
||||
#define ST7735_RDDID 0x04
|
||||
#define ST7735_RDDST 0x09
|
||||
|
||||
#define ST7735_RDDPM 0x0A // Read display power mode
|
||||
#define ST7735_RDD_MADCTL 0x0B // Read display MADCTL
|
||||
#define ST7735_RDD_COLMOD 0x0C // Read display pixel format
|
||||
#define ST7735_RDDIM 0x0D // Read display image mode
|
||||
#define ST7735_RDDSM 0x0E // Read display signal mode
|
||||
#define ST7735_RDDSR 0x0F // Read display self-diagnostic result (ST7735)
|
||||
|
||||
#define ST7735_SLPIN 0x10
|
||||
#define ST7735_SLPOUT 0x11
|
||||
#define ST7735_PTLON 0x12
|
||||
#define ST7735_NORON 0x13
|
||||
|
||||
#define ST7735_INVOFF 0x20
|
||||
#define ST7735_INVON 0x21
|
||||
#define ST7735_GAMSET 0x26 // Gamma set
|
||||
#define ST7735_DISPOFF 0x28
|
||||
#define ST7735_DISPON 0x29
|
||||
#define ST7735_CASET 0x2A
|
||||
#define ST7735_RASET 0x2B
|
||||
#define ST7735_RAMWR 0x2C
|
||||
#define ST7735_RGBSET 0x2D // Color setting for 4096, 64K and 262K colors
|
||||
#define ST7735_RAMRD 0x2E
|
||||
|
||||
#define ST7735_PTLAR 0x30
|
||||
#define ST7735_VSCRDEF 0x33 // Vertical scrolling definition (ST7735)
|
||||
#define ST7735_TEOFF 0x34 // Tearing effect line off
|
||||
#define ST7735_TEON 0x35 // Tearing effect line on
|
||||
#define ST7735_MADCTL 0x36 // Memory data access control
|
||||
#define ST7735_IDMOFF 0x38 // Idle mode off
|
||||
#define ST7735_IDMON 0x39 // Idle mode on
|
||||
#define ST7735_RAMWRC 0x3C // Memory write continue (ST7735)
|
||||
#define ST7735_RAMRDC 0x3E // Memory read continue (ST7735)
|
||||
#define ST7735_COLMOD 0x3A
|
||||
|
||||
#define ST7735_RAMCTRL 0xB0 // RAM control
|
||||
#define ST7735_RGBCTRL 0xB1 // RGB control
|
||||
#define ST7735_PORCTRL 0xB2 // Porch control
|
||||
#define ST7735_FRCTRL1 0xB3 // Frame rate control
|
||||
#define ST7735_PARCTRL 0xB5 // Partial mode control
|
||||
#define ST7735_GCTRL 0xB7 // Gate control
|
||||
#define ST7735_GTADJ 0xB8 // Gate on timing adjustment
|
||||
#define ST7735_DGMEN 0xBA // Digital gamma enable
|
||||
#define ST7735_VCOMS 0xBB // VCOMS setting
|
||||
#define ST7735_LCMCTRL 0xC0 // LCM control
|
||||
#define ST7735_IDSET 0xC1 // ID setting
|
||||
#define ST7735_VDVVRHEN 0xC2 // VDV and VRH command enable
|
||||
#define ST7735_VRHS 0xC3 // VRH set
|
||||
#define ST7735_VDVSET 0xC4 // VDV setting
|
||||
#define ST7735_VCMOFSET 0xC5 // VCOMS offset set
|
||||
#define ST7735_FRCTR2 0xC6 // FR Control 2
|
||||
#define ST7735_CABCCTRL 0xC7 // CABC control
|
||||
#define ST7735_REGSEL1 0xC8 // Register value section 1
|
||||
#define ST7735_REGSEL2 0xCA // Register value section 2
|
||||
#define ST7735_PWMFRSEL 0xCC // PWM frequency selection
|
||||
#define ST7735_PWCTRL1 0xD0 // Power control 1
|
||||
#define ST7735_VAPVANEN 0xD2 // Enable VAP/VAN signal output
|
||||
#define ST7735_CMD2EN 0xDF // Command 2 enable
|
||||
#define ST7735_PVGAMCTRL 0xE0 // Positive voltage gamma control
|
||||
#define ST7735_NVGAMCTRL 0xE1 // Negative voltage gamma control
|
||||
#define ST7735_DGMLUTR 0xE2 // Digital gamma look-up table for red
|
||||
#define ST7735_DGMLUTB 0xE3 // Digital gamma look-up table for blue
|
||||
#define ST7735_GATECTRL 0xE4 // Gate control
|
||||
#define ST7735_SPI2EN 0xE7 // SPI2 enable
|
||||
#define ST7735_PWCTRL2 0xE8 // Power control 2
|
||||
#define ST7735_EQCTRL 0xE9 // Equalize time control
|
||||
#define ST7735_PROMCTRL 0xEC // Program control
|
||||
#define ST7735_PROMEN 0xFA // Program mode enable
|
||||
#define ST7735_NVMSET 0xFC // NVM setting
|
||||
#define ST7735_PROMACT 0xFE // Program action
|
||||
|
||||
// Some ready-made 16-bit ('565') color settings:
|
||||
#define ST77XX_BLACK 0x0000
|
||||
#define ST77XX_WHITE 0xFFFF
|
||||
#define ST77XX_RED 0xF800
|
||||
#define ST77XX_GREEN 0x07E0
|
||||
#define ST77XX_BLUE 0x001F
|
||||
#define ST77XX_CYAN 0x07FF
|
||||
#define ST77XX_MAGENTA 0xF81F
|
||||
#define ST77XX_YELLOW 0xFFE0
|
||||
#define ST77XX_ORANGE 0xFC00
|
||||
|
||||
// Some ready-made 16-bit ('565') color settings:
|
||||
#define ST7735_BLACK ST77XX_BLACK
|
||||
#define ST7735_WHITE ST77XX_WHITE
|
||||
#define ST7735_RED ST77XX_RED
|
||||
#define ST7735_GREEN ST77XX_GREEN
|
||||
#define ST7735_BLUE ST77XX_BLUE
|
||||
#define ST7735_CYAN ST77XX_CYAN
|
||||
#define ST7735_MAGENTA ST77XX_MAGENTA
|
||||
#define ST7735_YELLOW ST77XX_YELLOW
|
||||
#define ST7735_ORANGE ST77XX_ORANGE
|
||||
|
||||
namespace esphome {
|
||||
namespace st7735 {
|
||||
|
||||
class ST7735 : public PollingComponent, public display::DisplayBuffer,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING,
|
||||
spi::DATA_RATE_8MHZ> {
|
||||
public:
|
||||
void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
|
||||
void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void update() override;
|
||||
void loop() override;
|
||||
|
||||
void write_display_data();
|
||||
|
||||
protected:
|
||||
GPIOPin *dc_pin_;
|
||||
GPIOPin *reset_pin_{nullptr};
|
||||
|
||||
void displayInit(const uint8_t *addr);
|
||||
void sendcommand(uint8_t cmd, const uint8_t* dataBytes, uint8_t numDataBytes);
|
||||
void senddata(const uint8_t* dataBytes, uint8_t numDataBytes);
|
||||
|
||||
void init_reset_();
|
||||
void writecommand(uint8_t value);
|
||||
void writedata(uint8_t value);
|
||||
|
||||
void spi_master_write_addr(uint16_t addr1, uint16_t addr2);
|
||||
void spi_master_write_color(uint16_t color, uint16_t size);
|
||||
|
||||
void draw_absolute_pixel_internal(int x, int y, Color color) override;
|
||||
|
||||
int get_height_internal() override;
|
||||
int get_width_internal() override;
|
||||
size_t get_buffer_length_();
|
||||
};
|
||||
|
||||
|
||||
} // namespace st7735
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user