Last active
November 9, 2023 21:57
-
-
Save harrisonhjones/73abb20f8aeed02cff084a9d8fb227c1 to your computer and use it in GitHub Desktop.
p9813 ESPHome Component
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
import esphome.codegen as cg | |
import esphome.config_validation as cv | |
from esphome import pins | |
from esphome.components import light, output | |
from esphome.const import CONF_OUTPUT_ID, CONF_CLOCK_PIN, CONF_DATA_PIN | |
from esphome.cpp_helpers import gpio_pin_expression | |
namespace = cg.esphome_ns.namespace('p9813') | |
P9813LightOutput = namespace.class_('P9813LightOutput', light.LightOutput) | |
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend({ | |
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(P9813LightOutput), | |
cv.Required(CONF_CLOCK_PIN): cv.All(pins.internal_gpio_input_pin_schema), | |
cv.Required(CONF_DATA_PIN): cv.All(pins.internal_gpio_input_pin_schema), | |
}) | |
async def to_code(config): | |
var = cg.new_Pvariable(config[CONF_OUTPUT_ID]) | |
await light.register_light(var, config) | |
clockPin = await cg.gpio_pin_expression(config[CONF_CLOCK_PIN]) | |
cg.add(var.set_clock_pin(clockPin)) | |
dataPin = await cg.gpio_pin_expression(config[CONF_DATA_PIN]) | |
cg.add(var.set_data_pin(dataPin)) |
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
#include "esphome/core/log.h" | |
#include "p9813.h" | |
#include "LEDStripDriver.h" | |
namespace esphome { | |
namespace p9813 { | |
static const char *TAG = "p9813.light"; | |
void P9813LightOutput::setup() {} | |
light::LightTraits P9813LightOutput::get_traits() { | |
auto traits = light::LightTraits(); | |
traits.set_supported_color_modes({light::ColorMode::RGB}); | |
return traits; | |
} | |
void P9813LightOutput::write_state(light::LightState *state) { | |
LEDStripDriver ledDriver = LEDStripDriver(this->data_pin_->get_pin(), this->clock_pin_->get_pin()); | |
float red, green, blue; | |
state->current_values_as_rgb(&red, &green, &blue, false); | |
ledDriver.setColor(static_cast<uint8_t>(red * 255), static_cast<uint8_t>(green * 255), static_cast<uint8_t>(blue * 255)); // RGB | |
} | |
void P9813LightOutput::dump_config(){ | |
ESP_LOGCONFIG(TAG, "P9813:"); | |
LOG_PIN(" Clock Pin: ", this->clock_pin_); | |
LOG_PIN(" Data Pin: ", this->data_pin_); | |
} | |
} | |
} |
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
#pragma once | |
#include "esphome/core/component.h" | |
#include "esphome/components/output/float_output.h" | |
#include "esphome/components/light/light_output.h" | |
namespace esphome { | |
namespace p9813 { | |
class P9813LightOutput : public light::LightOutput, public Component { | |
public: | |
void setup() override; | |
light::LightTraits get_traits() override; | |
void set_clock_pin(InternalGPIOPin *pin) { clock_pin_ = pin; } | |
void set_data_pin(InternalGPIOPin *pin) { data_pin_ = pin; } | |
void write_state(light::LightState *state) override; | |
void dump_config() override; | |
protected: | |
InternalGPIOPin *clock_pin_; | |
InternalGPIOPin *data_pin_; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes:
Changes I know I want to make for the next revision:
Rename from- addressed in rev2p9813_light
to justp9813
Remove unnecessary logging in- addressed in rev2write_state
LED-Strip-Driver-Module
library so we don't need to pull it in as a dep