ledfx.py 1.23 KB
Newer Older
fleur's avatar
fleur committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import leds, utime, math


def col_cor(colors, brightness=1, gamma=1):
    return [
        [int(255 * brightness * math.pow((y / 255.0), gamma)) for y in x]
        for x in colors
    ]


def halo(colors):
    used_leds = len(colors)
    colors += [[0, 0, 0]] * (11 - used_leds)
    colors += [colors[used_leds - 1]] + [colors[0]] * 2 + [colors[used_leds - 1]]
    return colors


def kitt(
    cycles=100,
    delay=80,
    power=10,
    minimum=0.3,
    rgb=[255, 0, 0],
    spectrum=[],
    halo=False,
):
Rahix's avatar
Rahix committed
27 28 29
    """
    LED Animation.
    """
fleur's avatar
fleur committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    kitt_table = [((-math.cos(math.pi * (x / 10.0))) + 1) / 2.0 for x in range(21)]
    kitt_table = [math.pow(x, power) * (1 - minimum) + minimum for x in kitt_table]

    for i in range(cycles):
        j = i % 20
        if j > 10:
            j = 20 - j
        if spectrum == []:
            used_leds = 11
            output = [[int(x * y) for y in rgb] for x in kitt_table[j : (j + 11)]]
        else:
            used_leds = len(spectrum)
            output = [
                [int(y * kitt_table[j + x]) for y in spectrum[x]]
                for x in range(used_leds)
            ]
        if halo:
            halo(output)
        leds.set_all(output)
        utime.sleep_ms(delay)
    leds.clear()