Understanding Accent LED Code

I’ve always been the type of person that would rather code a website in notepad than Dreamweaver. I’ve never coded much, but when I do, I like to understand the code and what it is doing.

I started building FX sabers three months ago. I have completed 11 so far and have used Proffieboard v3.9 in all of them. I taught myself enough to write configuration files for each. I have Crystal Chamber LEDs in some, and I’ve implemented Bluetooth (in the ones with enough space) and custom saber fonts for each saber. My next builds will utilize OLED screens in a chassis of my own design, printed on my Raise3D Pro 3 Plus printer.

Three of my sabers use red and green accent LEDs that I’ve implemented in different ways. I wired one saber with the accents as sub-blades. The other two have the accent LEDs as separate blades. However, all three utilize the same blade styles to illuminate.

I use this code to turn on the LEDs:

//Red Accent LED
StyleNormalPtr<RED, RED, 300, 800>(),

//Green Accent LED
StyleNormalPtr<GREEN, GREEN, 300, 800>(),

I also have code (I don’t know where it came from) that makes the accent LEDs blink in a somewhat random pattern:

//Red Accent LED
StylePtr<InOutHelper<Blinking<Red, Black, 2000, 200>,1,1,Sequence<RED, BLACK, 100, 80,
0b0000000000000000,0b1010101010101010,0b1010101000000000,0b0000000000000000,0b1010101010101010>>>(),

//Green Accent LED
StylePtr<InOutHelper<Blinking<Green,Blinking<Green, Black, 300, 500>,3000,500>,1,1,Sequence<GREEN, BLACK, 100, 80, 0b1010101010101010,0b0000000000000000,0b0000000000000001,0b1010101010101010,0b0101000000000000>>>(),

I’ve mixed both pieces of code to create different accent light effects for different blade styles (both on, one on & one blinking, both blinking, etc.).

My issue is that the random blinking occurs even when the saber blade is off. I’d like them to stay off when the saber is off and only start blinking when the saber is active. I understand that the “InOutHelper” is the key to this, but I do not understand how it works. I’ve tried removing this part from the code (and the associated > at the end), but I get an error in verification.

What I’d like to know is how to modify the code to act the way I have described. More than that though, I’d like to understand what each piece of the code is doing and how it affects the light.

What I [think] I know:
The green blinks more than the red, and I’m fairly certain that is due to the fact that the green implements two “BLINKING” calls that break up the sequence even more. The five long strings at the end look like binary, except for the 0b at the beginning. But maybe this is a type of call that indicates the rest of the string IS binary. I’m sure that the numeric values are time (most likely in milliseconds) that the various functions are in effect. I don’t know the difference between “StylePtr” and “StyleNormalPtr” and why one would be used over the other.

I’ve seen threads where people want accent lights to be on when the blade is off, but not the other way around.

If anybody could shed some blinking light on the subject, I’d be most appreciative.

Thanks!

You’re using an older syntax, I’d recommend updating to the Layers<> format. It’s more flexible AND it makes it easier to understand what’s going on with each piece of the style in my opinion.

Just so you’re aware, ANY style can be used for ANY type of “blade”. We often separate by Main Blade, Side Blade, Accents or Crystals because of commonly desired behaviors like doing something while the main blade is Off or not show visual effects for blasts, etc. But technically you can use any code for any blade depending on the behavior you want.

Sequence<> is a very specific style if you need a very specific blinking sequence, if you just want it to blink or just randomly blink you can do so with much simpler styles and it won’t be quite so challenging to implement.

That said, here’s a simple starting point that will also help both with the switch to Layers<> and with seeing the parts of the style code and what they do.

StylePtr<
  Layers<
    // Base effect (runs while on and appears under all other effects)
    Blinking<Red,Black,1000,500>,
    // InOutLayer (controls the ON/OFF behavior, with Black making the LED off when you turn off the blade, you can put styles or colors in place of Black to have it do things while off)
    InOutTrL<TrInstant,TrInstant,Black>
  >
>(),

StylePtr<
  Layers<
    // Base effect (runs while on and appears under all other effects)
    Blinking<Green,Black,1000,500>,
    // InOutLayer (controls the ON/OFF behavior, with Black making the LED off when you turn off the blade, you can put styles or colors in place of Black to have it do things while off)
    InOutTrL<TrInstant,TrInstant,Black>
  >
>(),

The order of layers matter, the first layer (Base effect) is covered by the second, and so on. So the InOutLayer covers the Base effect only when Off with Black in this case (which means Off in the OS), when the blade is On the InOutLayer is transparent so everything before it becomes visible.

Now to really understand the parameters in the styles I recommend either using the Style Editor - or reading the individual style Usage templates and explanations in ProffieOS.

All styles have a corresponding .h file with the Usage template and parameter info at the top in the /styles folder, this is the best way to understand how they work and what they do.

That said, even though you’re looking to hand-code, the Style Editor is also a very, very helpful tool in understanding the parameters especially when first trying to learn. Then once you actually understand the parts of the styles and effects you can move to hand-coding much easier.
https://fredrik.hubbe.net/lightsaber/style_editor_beta.html?S=Blinking<Red%2CBlue%2C1000%2C500>

I started out much the same away, learning each piece individually in the Style Editor and then hand-coding the styles together once I knew how they worked. I still hand-code the more complex styles during R&D for my library. That said, my library can very much help you get started and then you can “backwards engineer” from there to learn the more advanced concepts available, I just recommend watching the videos at the top to get started. The library let’s you build and preview much more complex styles and generate the code, but then you can learn from the pieces and get a fuller understanding of everything possible.

https://www.fett263.com/fett263-proffieOS7-style-library.html

Circling back to Sequence<> in case you really want a specific sequence you can read more here:

My issue is that the random blinking occurs even when the saber blade is off. I’d like them to stay off when the saber is off and only start blinking when the saber is active.

Also look at the idle time define.

When it comes to understanding, the style editor is your friend:

https://fredrik.hubbe.net/lightsaber/style_editor.html?S=StyleNormalPtr<AudioFlicker<Yellow%2CWhite>%2CBlue%2C300%2C800>

StyleNormalPtr<> is an alias. That makes it short and easy to type, but it makes it harder to see what it really does. The style editor has an “Expand” button which expands an alias to it’s definition. However, you have to click it quite a few times to really understand what is going on. An easier way to do is to use the “Layerize” button which will attempt to expand macros and organize the results into layers. When you Layerize “StyleNormalPtr”, you get this:

Layers<
  Red,
  BlastL<White>,
  LockupL<AudioFlicker<Red,White>>,
  SimpleClashL<Red>,
  InOutHelperL<InOutFuncX<Int<300>,Int<800>>>>

This is exactly the same as what StyleNormalPtr does, but sorted into layers that can be tested, understood and replaced individually.

The right hand side of the style editor also has a small comment for each argument, which tries to tell you what that argument is for. We can use this to understand the blinking stuff better:

When I put in your “Red Accent LED” style into the style editor, the right hand side looks like this:

InOutHelper<
  /* Base color */
  Blinking<
    /* A */
    Red
    ,
    /* B */
    Black
    ,
    /* milliseconds between blinks */
    2000
    ,
    /* 0 = off, 1000 = on */
    200
  >
  ,
  /* Time to extend. */
  1
  ,
  /* Time to retract. */
  1
  ,
  /* color when retracted */
  Sequence<
    /* Color if bit is 1 */
    Red
    ,
    /* Color if bit is 0 */
    Black
    ,
    /* Milliseconds per bit. */
    100
    ,
    /* total bits */
    80
    ,
    /* Bit sequence 1 */
    0b0
    ,
    /* Bit sequence 2 */
    0b1010101010101010
    ,
    /* Bit sequence 3 */
    0b1010101000000000
    ,
    /* Bit sequence 4 */
    0b0
    ,
    /* Bit sequence 5 */
    0b1010101010101010
  >
>

The interesting part here is “color when retracted”. If you don’t want it to do anything when the saber is off, this part should be “Black” or just completely removed.

I’m sorry I didn’t follow-up sooner. Thank you for the great input. I did not know able layers, which sounds like a fantastic feature. I will be studying your replies and will try to educate myself on the subject. :grinning: