1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-21 20:38:16 +00:00
Otto Winter 6682c43dfa
🏗 Merge C++ into python codebase (#504)
## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-17 12:06:00 +02:00

97 lines
2.8 KiB
C++

#include "mcp23017.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp23017 {
static const char *TAG = "mcp23017";
void MCP23017::setup() {
ESP_LOGCONFIG(TAG, "Setting up MCP23017...");
uint8_t iocon;
if (!this->read_reg_(MCP23017_IOCONA, &iocon)) {
this->mark_failed();
return;
}
// all pins input
this->write_reg_(MCP23017_IODIRA, 0xFF);
this->write_reg_(MCP23017_IODIRB, 0xFF);
}
bool MCP23017::digital_read(uint8_t pin) {
uint8_t bit = pin % 8;
uint8_t reg_addr = pin < 8 ? MCP23017_GPIOA : MCP23017_GPIOB;
uint8_t value = 0;
this->read_reg_(reg_addr, &value);
return value & (1 << bit);
}
void MCP23017::digital_write(uint8_t pin, bool value) {
uint8_t reg_addr = pin < 8 ? MCP23017_OLATA : MCP23017_OLATB;
this->update_reg_(pin, value, reg_addr);
}
void MCP23017::pin_mode(uint8_t pin, uint8_t mode) {
uint8_t iodir = pin < 8 ? MCP23017_IODIRA : MCP23017_IODIRB;
uint8_t gppu = pin < 8 ? MCP23017_GPPUA : MCP23017_GPPUB;
switch (mode) {
case MCP23017_INPUT:
this->update_reg_(pin, true, iodir);
break;
case MCP23017_INPUT_PULLUP:
this->update_reg_(pin, true, iodir);
this->update_reg_(pin, true, gppu);
break;
case MCP23017_OUTPUT:
this->update_reg_(pin, false, iodir);
break;
default:
break;
}
}
float MCP23017::get_setup_priority() const { return setup_priority::HARDWARE; }
bool MCP23017::read_reg_(uint8_t reg, uint8_t *value) {
if (this->is_failed())
return false;
return this->read_byte(reg, value);
}
bool MCP23017::write_reg_(uint8_t reg, uint8_t value) {
if (this->is_failed())
return false;
return this->write_byte(reg, value);
}
void MCP23017::update_reg_(uint8_t pin, bool pin_value, uint8_t reg_addr) {
uint8_t bit = pin % 8;
uint8_t reg_value = 0;
if (reg_addr == MCP23017_OLATA) {
reg_value = this->olat_a_;
} else if (reg_addr == MCP23017_OLATB) {
reg_value = this->olat_b_;
} else {
this->read_reg_(reg_addr, &reg_value);
}
if (pin_value)
reg_value |= 1 << bit;
else
reg_value &= ~(1 << bit);
this->write_reg_(reg_addr, reg_value);
if (reg_addr == MCP23017_OLATA) {
this->olat_a_ = reg_value;
} else if (reg_addr == MCP23017_OLATB) {
this->olat_b_ = reg_value;
}
}
MCP23017GPIOPin::MCP23017GPIOPin(MCP23017 *parent, uint8_t pin, uint8_t mode, bool inverted)
: GPIOPin(pin, mode, inverted), parent_(parent) {}
void MCP23017GPIOPin::setup() { this->pin_mode(this->mode_); }
void MCP23017GPIOPin::pin_mode(uint8_t mode) { this->parent_->pin_mode(this->pin_, mode); }
bool MCP23017GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
void MCP23017GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
} // namespace mcp23017
} // namespace esphome