Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Last active November 9, 2023 21:57
Show Gist options
  • Save harrisonhjones/73abb20f8aeed02cff084a9d8fb227c1 to your computer and use it in GitHub Desktop.
Save harrisonhjones/73abb20f8aeed02cff084a9d8fb227c1 to your computer and use it in GitHub Desktop.
p9813 ESPHome Component
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))
#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_);
}
}
}
#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_;
};
}
}
@harrisonhjones
Copy link
Author

harrisonhjones commented Nov 9, 2023

Notes:

  1. This gist is missing the code from https://github.com/MrKrabat/LED-Strip-Driver-Module/tree/master.

Changes I know I want to make for the next revision:

  1. Rename from p9813_light to just p9813 - addressed in rev2
  2. Remove unnecessary logging in write_state - addressed in rev2
  3. Re-write the LED-Strip-Driver-Module library so we don't need to pull it in as a dep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment