Skip to content

Instantly share code, notes, and snippets.

@zeroping
Created May 1, 2022 15:17
Show Gist options
  • Save zeroping/6801d8bd16ae6e0ca7e9fe1d897181b7 to your computer and use it in GitHub Desktop.
Save zeroping/6801d8bd16ae6e0ca7e9fe1d897181b7 to your computer and use it in GitHub Desktop.
Temp upload of i2cdimmer for esphome on linkind
#include "esphome.h"
using namespace esphome;
class I2CDimmerOutput : public Component, public FloatOutput {
public:
I2CDimmerOutput( IDFI2CBus* i2cbus) : i2cbus(i2cbus) {
};
void setup() override {
// This will be called by App.setup()
if (i2cbus == NULL)
{
ESP_LOGE("I2CDimmer", "i2cbus is null");
}
else
{
ESP_LOGI("I2CDimmer", "send dimmer init" );
const uint8_t tosend [] = {0x2A, 0x07, 0x56, 0x01, 0x11, 0xFF, 0x66};
ErrorCode ret = i2cbus->write(0x50, tosend, 7);
ESP_LOGI("I2CDimmer", "result: %d", (int)ret );
}
}
void write_state(float state) override {
// state is the amount this output should be on, from 0.0 to 1.0
// we need to convert it to an integer first
//int value = state * 1024;
//analogWrite(5, value);
ESP_LOGD("I2CDimmer", "new state %f!", state);
if (i2cbus == NULL)
{
ESP_LOGD("I2CDimmer", "i2cbus is null");
}
else
{
uint8_t dim_val = 255 * state;
uint16_t chk = 65403-dim_val;
//uint8_t tosend [] = {0x09, 0x50, 0x01, dim_val, 0x00, 0x00, chk>>8, chk};
const uint8_t tosend [] = {0x2A, 0x09, 0x50 ,0x01, dim_val, 0x00, 0x00, (uint8_t)(chk>>8), (uint8_t)(chk)};
//for (int i=0; i< 9; i++)
//{
// ESP_LOGD("custom", "byte %d : %02x", i, tosend[i]);
//}
ErrorCode ret = i2cbus->write(0x50, tosend, 9);
ESP_LOGI("I2CDimmer", "result: %d", (int)ret );
}
//IDFI2CBus->dump_config();
}
private:
IDFI2CBus* i2cbus;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment