1
0
mirror of https://github.com/esphome/esphome.git synced 2025-01-22 22:04:06 +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

112 lines
4.0 KiB
C++

#include "my9231.h"
#include "esphome/core/log.h"
namespace esphome {
namespace my9231 {
static const char *TAG = "my9231.output";
// One-shot select (frame cycle repeat mode / frame cycle One-shot mode)
static const uint8_t MY9231_CMD_ONE_SHOT_DISABLE = 0x0 << 6;
static const uint8_t MY9231_CMD_ONE_SHOT_ENFORCE = 0x1 << 6;
// Reaction time of Iout
static const uint8_t MY9231_CMD_REACTION_FAST = 0x0 << 5;
static const uint8_t MY9231_CMD_REACTION_SLOW = 0x1 << 5;
// Grayscale resolution select
static const uint8_t MY9231_CMD_BIT_WIDTH_16 = 0x0 << 3;
static const uint8_t MY9231_CMD_BIT_WIDTH_14 = 0x1 << 3;
static const uint8_t MY9231_CMD_BIT_WIDTH_12 = 0x2 << 3;
static const uint8_t MY9231_CMD_BIT_WIDTH_8 = 0x3 << 3;
// Internal oscillator freq. select (divider)
static const uint8_t MY9231_CMD_FREQUENCY_DIVIDE_1 = 0x0 << 1;
static const uint8_t MY9231_CMD_FREQUENCY_DIVIDE_4 = 0x1 << 1;
static const uint8_t MY9231_CMD_FREQUENCY_DIVIDE_16 = 0x2 << 1;
static const uint8_t MY9231_CMD_FREQUENCY_DIVIDE_64 = 0x3 << 1;
// Output waveform
static const uint8_t MY9231_CMD_SCATTER_APDM = 0x0 << 0;
static const uint8_t MY9231_CMD_SCATTER_PWM = 0x1 << 0;
void MY9231OutputComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up MY9231OutputComponent...");
this->pin_di_->setup();
this->pin_di_->digital_write(false);
this->pin_dcki_->setup();
this->pin_dcki_->digital_write(false);
this->pwm_amounts_.resize(this->num_channels_, 0);
uint8_t command = 0;
if (this->bit_depth_ <= 8) {
this->bit_depth_ = 8;
command |= MY9231_CMD_BIT_WIDTH_8;
} else if (this->bit_depth_ <= 12) {
this->bit_depth_ = 12;
command |= MY9231_CMD_BIT_WIDTH_12;
} else if (this->bit_depth_ <= 14) {
this->bit_depth_ = 14;
command |= MY9231_CMD_BIT_WIDTH_14;
} else {
this->bit_depth_ = 16;
command |= MY9231_CMD_BIT_WIDTH_16;
}
command |=
MY9231_CMD_SCATTER_APDM | MY9231_CMD_FREQUENCY_DIVIDE_1 | MY9231_CMD_REACTION_FAST | MY9231_CMD_ONE_SHOT_DISABLE;
ESP_LOGV(TAG, " Command: 0x%02X", command);
this->init_chips_(command);
ESP_LOGV(TAG, " Chips initialized.");
}
void MY9231OutputComponent::dump_config() {
ESP_LOGCONFIG(TAG, "MY9231:");
LOG_PIN(" DI Pin: ", this->pin_di_);
LOG_PIN(" DCKI Pin: ", this->pin_dcki_);
ESP_LOGCONFIG(TAG, " Total number of channels: %u", this->num_channels_);
ESP_LOGCONFIG(TAG, " Number of chips: %u", this->num_chips_);
ESP_LOGCONFIG(TAG, " Bit depth: %u", this->bit_depth_);
}
void MY9231OutputComponent::loop() {
if (!this->update_)
return;
for (auto pwm_amount : this->pwm_amounts_) {
this->write_word_(pwm_amount, this->bit_depth_);
}
// Send 8 DI pulses. After 8 falling edges, the duty data are store.
this->send_di_pulses_(8);
this->update_ = false;
}
void MY9231OutputComponent::set_channel_value_(uint8_t channel, uint16_t value) {
ESP_LOGV(TAG, "set channels %u to %u", channel, value);
uint8_t index = this->num_channels_ - channel - 1;
if (this->pwm_amounts_[index] != value) {
this->update_ = true;
}
this->pwm_amounts_[index] = value;
}
void MY9231OutputComponent::init_chips_(uint8_t command) {
// Send 12 DI pulse. After 6 falling edges, the duty data are stored
// and after 12 rising edges the command mode is activated.
this->send_di_pulses_(12);
delayMicroseconds(12);
for (uint8_t i = 0; i < this->num_chips_; i++) {
this->write_word_(command, 8);
}
// Send 16 DI pulse. After 14 falling edges, the command data are
// stored and after 16 falling edges the duty mode is activated.
this->send_di_pulses_(16);
}
void MY9231OutputComponent::write_word_(uint16_t value, uint8_t bits) {
for (uint8_t i = bits; i > 0; i--) {
this->pin_di_->digital_write(value & (1 << (i - 1)));
this->pin_dcki_->digital_write(!this->pin_dcki_->digital_read());
}
}
void MY9231OutputComponent::send_di_pulses_(uint8_t count) {
delayMicroseconds(12);
for (uint8_t i = 0; i < count; i++) {
this->pin_di_->digital_write(true);
this->pin_di_->digital_write(false);
}
}
} // namespace my9231
} // namespace esphome