This repository has been archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27a2037
commit 2719167
Showing
9 changed files
with
704 additions
and
513 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
doc/dc1引脚功能及走线定位.xlsx | ||
image/PSD | ||
自动git更新.bat | ||
自动git更新.bat | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 | ||
|
||
cat9554_ns = cg.esphome_ns.namespace('cat9554') | ||
CAT9554GPIOMode = cat9554_ns.enum('CAT9554GPIOMode') | ||
CAT9554_GPIO_MODES = { | ||
'INPUT': CAT9554GPIOMode.CAT9554_INPUT, | ||
'OUTPUT': CAT9554GPIOMode.CAT9554_OUTPUT, | ||
} | ||
|
||
CAT9554Component = cat9554_ns.class_('CAT9554Component', cg.Component, i2c.I2CDevice) | ||
CAT9554GPIOPin = cat9554_ns.class_('CAT9554GPIOPin', cg.GPIOPin) | ||
|
||
CONF_CAT9554 = 'cat9554' | ||
CONFIG_SCHEMA = cv.Schema({ | ||
cv.Required(CONF_ID): cv.declare_id(CAT9554Component), | ||
}).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) | ||
|
||
|
||
def validate_cat9554_gpio_mode(value): | ||
value = cv.string(value) | ||
return cv.enum(CAT9554_GPIO_MODES, upper=True)(value) | ||
|
||
|
||
CAT9554_OUTPUT_PIN_SCHEMA = cv.Schema({ | ||
cv.Required(CONF_CAT9554): cv.use_id(CAT9554Component), | ||
cv.Required(CONF_NUMBER): cv.int_, | ||
cv.Optional(CONF_MODE, default="OUTPUT"): validate_cat9554_gpio_mode, | ||
cv.Optional(CONF_INVERTED, default=False): cv.boolean, | ||
}) | ||
CAT9554_INPUT_PIN_SCHEMA = cv.Schema({ | ||
cv.Required(CONF_CAT9554): cv.use_id(CAT9554Component), | ||
cv.Required(CONF_NUMBER): cv.int_, | ||
cv.Optional(CONF_MODE, default="INPUT"): validate_cat9554_gpio_mode, | ||
cv.Optional(CONF_INVERTED, default=False): cv.boolean, | ||
}) | ||
|
||
|
||
@pins.PIN_SCHEMA_REGISTRY.register('cat9554', (CAT9554_OUTPUT_PIN_SCHEMA, CAT9554_INPUT_PIN_SCHEMA)) | ||
def cat9554_pin_to_code(config): | ||
parent = yield cg.get_variable(config[CONF_CAT9554]) | ||
yield CAT9554GPIOPin.new(parent, config[CONF_NUMBER], config[CONF_MODE], config[CONF_INVERTED]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "cat9554.h" | ||
#include "esphome/core/log.h" | ||
|
||
namespace esphome { | ||
namespace cat9554 { | ||
|
||
static const char *TAG = "cat9554"; | ||
|
||
void CAT9554Component::setup() { | ||
ESP_LOGCONFIG(TAG, "Setting up CAT9554..."); | ||
if (!this->read_gpio_()) { | ||
ESP_LOGE(TAG, "CAT9554 not available under 0x%02X", this->address_); | ||
this->mark_failed(); | ||
return; | ||
} | ||
|
||
this->write_gpio_(); | ||
this->read_gpio_(); | ||
} | ||
void CAT9554Component::dump_config() { | ||
ESP_LOGCONFIG(TAG, "CAT9554:"); | ||
LOG_I2C_DEVICE(this) | ||
if (this->is_failed()) { | ||
ESP_LOGE(TAG, "Communication with CAT9554 failed!"); | ||
} | ||
} | ||
bool CAT9554Component::digital_read(uint8_t pin) { | ||
this->read_gpio_(); | ||
return this->input_mask_ & (1 << pin); | ||
} | ||
void CAT9554Component::digital_write(uint8_t pin, bool value) { | ||
if (value) { | ||
this->output_mask_ |= (1 << pin); | ||
} else { | ||
this->output_mask_ &= ~(1 << pin); | ||
} | ||
|
||
this->write_gpio_(); | ||
} | ||
void CAT9554Component::pin_mode(uint8_t pin, uint8_t mode) { | ||
switch (mode) { | ||
case CAT9554_INPUT: | ||
// Clear mode mask bit | ||
this->mode_mask_ |= (1 << pin); | ||
// Write GPIO to enable input mode | ||
this->write_gpio_(); | ||
break; | ||
case CAT9554_OUTPUT: | ||
// Set mode mask bit | ||
this->mode_mask_ &= ~(1 << pin); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
bool CAT9554Component::read_gpio_() { | ||
if (this->is_failed()) | ||
return false; | ||
bool success; | ||
uint8_t data[2]; | ||
success = this->read_bytes_raw(data, 1); | ||
this->input_mask_ = data[0]; | ||
|
||
if (!success) { | ||
this->status_set_warning(); | ||
return false; | ||
} | ||
this->status_clear_warning(); | ||
return true; | ||
} | ||
bool CAT9554Component::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; | ||
if (!this->write_bytes_raw(data, 1)) { | ||
this->status_set_warning(); | ||
return false; | ||
} | ||
|
||
this->status_clear_warning(); | ||
return true; | ||
} | ||
float CAT9554Component::get_setup_priority() const { return setup_priority::IO; } | ||
|
||
void CAT9554GPIOPin::setup() { this->pin_mode(this->mode_); } | ||
bool CAT9554GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; } | ||
void CAT9554GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } | ||
void CAT9554GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); } | ||
CAT9554GPIOPin::CAT9554GPIOPin(CAT9554Component *parent, uint8_t pin, uint8_t mode, bool inverted) | ||
: GPIOPin(pin, mode, inverted), parent_(parent) {} | ||
|
||
} // namespace cat9554 | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#pragma once | ||
|
||
#include "esphome/core/component.h" | ||
#include "esphome/core/esphal.h" | ||
#include "esphome/components/i2c/i2c.h" | ||
|
||
namespace esphome { | ||
namespace cat9554 { | ||
|
||
/// Modes for CAT9554 pins | ||
enum CAT9554GPIOMode : uint8_t { | ||
CAT9554_INPUT = INPUT, | ||
CAT9554_OUTPUT = OUTPUT, | ||
}; | ||
|
||
class CAT9554Component : public Component, public i2c::I2CDevice { | ||
public: | ||
CAT9554Component() = 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 output, 0 means input | ||
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 CAT9554 pin as an internal input GPIO pin. | ||
class CAT9554GPIOPin : public GPIOPin { | ||
public: | ||
CAT9554GPIOPin(CAT9554Component *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: | ||
CAT9554Component *parent_; | ||
}; | ||
|
||
} // namespace cat9554 | ||
} // namespace esphome |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.