TILE-LED36 reference

TILE-LED36

Usage

Please see the reference manual, in PDF form here: TILE_LED36_ref.pdf

The reference manual is also available in HTML form here: TILE_LED36_ref.html

Code Samples

"""
from LED36_examples import *
"""
from micropython import const
import time, machine

LED_ADDR = const(60)        # default LED36 address

i2c = machine.I2C(1)        # select I2C1

def cyc(dt=250, addr=1):
    """ set all LEDs to black, red, green, yellow, blue, magenta, cayn and white for dt ms
        ramp up brightnes from 0 % to 100 %
    """
    while True:
        try:
            fill_rgb(100,100,100)
            break
        except:
            time.sleep_ms(100)
    for i in range(8):
        fill_rgb((i & 1)*255, ((i >> 1) & 1)*255, ((i >> 2) & 1)*255)
        time.sleep_ms(dt)
    for i in range(100):
        brightness(i)
        time.sleep_ms(20)

def save_nvram(addr=1):
    """ save NVRAM state
    """
    ba = bytearray(b'\x02fn')
    i2c.writeto(addr, ba)

def set_i2caddr(addr=60, oaddr=1):
    """ modify I2C address and save it to NVRAM """
    ba = bytearray(b'\x02\x0eI2C ')
    ba[-1] = (addr*2) & 0xfe
    i2c.writeto(oaddr, ba)
    save_nvram()

def brightness(b=100, addr=1):
    """ set brigntness
    """
    ba = bytearray(b'\x02\x16 ')
    ba[-1] = b & 0xff
    i2c.writeto(addr, ba)

def bloop(dt=100, maxv=100, inc=1):
    """ cycle through brigntness ramp
    """
    b = 0
    while True:
        print(b)
        brightness(b)
        b += inc
        b %= maxv
        time.sleep_ms(dt)

def pump(dt=10, maxv=100):
    """ cycle through brigntness modulation
    """
    import math
    sinar = []
    for i in range(90):
        sinar.append(int((math.sin(i*4/180*math.pi)+1)*maxv/2))
    i = 0
    while True:
        brightness(sinar[i])
        i += 1
        i %= len(sinar)
        time.sleep_ms(dt)

def fill_rgb(r=0x80, g=0, b=0, addr=1):
    """ fill LED array using set pixel command
    """
    i2c.writeto(addr, b'\x02X\x00\x00')
    buf = bytearray(b'\x02A   ')
    buf[2] = r
    buf[3] = g
    buf[4] = b
    for i in range(36):
        i2c.writeto(addr, buf)

def illu(r=0x80, g=0, b=0, addr=1):
    """ fill LED array using set illumination command
    """
    buf = bytearray(b'\x02i   ')
    buf[2] = r
    buf[3] = g
    buf[4] = b
    i2c.writeto(addr, buf)

def fill_frame(r=0x80, g=0, b=0, addr=1):
    """ fill LED array using fill frame command
    """
    i2c.writeto(addr, b'\x02ml')
    buf = bytearray(b'   ')
    buf[0] = r
    buf[1] = g
    buf[2] = b
    for i in range(36):
        i2c.writeto(addr, buf)

def set_dot(x=0, y=0, r=100, g=0, b=0, addr=1):
    """ set single LED color at position
    """
    buf = bytearray(b'\x02X  ')
    buf[2] = x
    buf[3] = y
    i2c.writeto(addr, buf)

    buf = bytearray(b'\x02A   ')
    buf[2] = r
    buf[3] = g
    buf[4] = b
    i2c.writeto(addr, buf)

def fill_raw(r=0x80, g=0, b=0, addr=1):
    """ fill LED array with raw values using fill frame command
    """
    i2c.writeto(addr, b'\x02nl')
    buf = bytearray(b'   ')
    buf[0] = r
    buf[1] = g
    buf[2] = b
    for i in range(36):
        i2c.writeto(addr, buf)

def ledpins(v=0, addr=1):
    """ permute LED colors (use with care)
    """
    buf = bytearray(b'\x02\x1c\x00')
    buf[-1] = v & 3
    i2c.writeto(addr, buf)

def random_dots(dt=10, addr=1):
    """ set random colors at random positions
    """
    import pyb
    while True:
        rn = pyb.rng()
        r = rn & 0xff
        g = (rn >> 8) & 0xff
        b = (rn >> 16) & 0xff
        x = (rn >> 24) % 36
        y = x // 6
        x %= 6
        set_dot(x, y, r, g, b, addr)
        time.sleep_ms(dt)