LocalBytes / localdeck-config

5 stars 3 forks source link

Simplify blip_light #31

Closed mcristina422 closed 3 weeks ago

mcristina422 commented 1 month ago

After working with this device for a couple hours, I wanted to optimize some of the larger parts of the config.

I first attempted to implement blip_light as a pure lambda, but I cant quite figure out what's wrong with it yet, I'm new to esphome component dev.

Not working code Any pointers on how to properly target the ledstrip as a range? [This seems useful](https://esphome.io/api/classesphome_1_1light_1_1_addressable_light) for selecting a range? Not sure how to use it ```yaml - lambda: |- for (float i = 1.0; i > 0; i-=0.05) { ESP_LOGI("set_led_rgb", "Blip light %f", i); auto call = id(ledstrip).make_call(); call.set_transition_length(0); call.set_rgb(i, i, i); call.perform(); delay(25); } ``` I'm intentionally avoiding using the light partitions, due to the [note found here](https://esphome.io/components/light/partition.html)

But tangentially, a simple optimization, instead of generating a 20 element array in the codegen, simply use the esphome repeat component. My simplified script, tested by manually updating the generated YAML:

  - id: blip_light
    parameters:
      led_index: int
    mode: parallel
    then:
      - repeat:
          count: 20
          then:
          - light.addressable_set:
              id: ledstrip
              range_from: !lambda return led_index;
              range_to: !lambda return led_index;
              red: !lambda return 1.0 - iteration*(1.0 / 20.0);
              green: !lambda return 1.0 - iteration*(1.0 / 20.0);
              blue: !lambda return 1.0 - iteration*(1.0 / 20.0);
          - delay: 25ms

I attempted to add this to the codegen, but failed to find repeat in the typescript. So the change is a bit more invoved in a codebase I'm not familiar with.

The original codegen output: https://github.com/LocalBytes/localdeck-config/blob/9fa3704d7e8937e87561d747afbc43a770fdb94b/packages/localdeck-codegen/esphome-localdeck.yaml#L1165-L1350

AAllport commented 1 month ago

I like this!

You can find the script definition here if you want to try it!

https://github.com/LocalBytes/localdeck-config/blob/main/packages/localdeck-codegen/src/scripts/blip-light.ts

I'll note that actions don't have their own classes (yet); I planned on doing this but didn't get around to it.

You can see a good example of how actions are called here https://github.com/LocalBytes/localdeck-config/blob/9fa3704d7e8937e87561d747afbc43a770fdb94b/packages/localdeck-codegen/src/virtuals/configured-button.ts#L132-L145