Using uart.write to send bytes stored in numbers

I’m working on recreating the functionality of a smart X-Mas light string. The LEDs in the light sting each have a driver IC for each pixel (RGB led) but they are not individually addressable. after reverse engineering the circuit and the protocol used to send commands from the BK7231N to the MCU, what I found happens is the protocol is a 9600 baud 8-n-1 pulse train sent to the MCU that in turn send a different protocol down the power line to the LEDs by switching the V+ line to zero using MOSFETs, Tim has reversed that protocol here Tim’s blog for anyone interested. Since the MCU already in the circuit is taking care of that I just need the communication from the BK7231 to the MCU and that has the format

[0x55, 0xAA, Checksum(byte), Function(byte), Data1(byte), Data2(byte), Data3(byte)]

the checksum value is the (least significant) byte of the sum of the function byte and the 3 data bytes. I can successfully send the bytes in a byte block; i.e. in a select: this works

.
.
.
 on_value:
       then: 
       - logger.log: 
            format: "Mode: %s (index %d)"
            args: ["x.c_str()", "i"]
       - if:
           condition:
             lambda: return (x == 0);
           then:
           - uart.write: [0x55, 0xAA, 0x00, 0x01, 0xFF, 0x00, 0x00]
       - if:  
           condition:
             lambda: return (x == 1);
           then:
             - uart.write: [0x55, 0xAA, 0x00, 0x01, 0xFF, 0x00, 0x00]
.
.
.

what I want to do is store the values for data1, data2, and data3 in numbers that can be calculated or set directly as well as set the function from the select and sent, something like;

uart.write [0x55,0xAA, lambda: return (0xFF & (id(mode).active_index() + id(data1) + id(data2) + id(data3)));, lambda: return(id(mode).active_index());, id(data1), id(data2), id(data3)]

I can’t seem to find this kind of info in the ESPHome wiki and I’m looking for recommendations on beginner sites/documentation on using lambda functions in ESPHome and the methods and properties exposed by the ESPHome objects and some examples, Something that will help me with my project but also walk me through learning and understanding ESPHome codeing/object model. Also looking for information on how the linker works with the various components in the yaml file. i.e. if I add web_server: (or any other module) what gets linked into the firmware file and how much extra space will it take up?

Lambda is C++ code, there is an example in uart.write showing the correct way to do this:

  # Templated, return type is std::vector<uint8_t>
  - uart.write: !lambda
      return {0x00, 0x20, 0x42};

So any Google search about ways to define std::vector<uint8_t> will show you how to define the values to supply.

You won’t find much help with that on these forums, they tend to be mostly at the user level. If you want developers to chat with my advice would be to subscribe to the ESPHome Discord servers:

1 Like

I reckon you may also find that the advanced users over on Discord will probably suggest you use a custom component for this.

https://esphome.io/custom/custom_component.html