Close

Chord Animated

A project log for Playing with MicroPython on the ESP

Instant gratification on the ESP8266 and ESP-32

Hari WigunaHari Wiguna 05/20/2019 at 00:410 Comments

 Even on a Wemos D1 Mini (ESP8266), MicroPython is amazingly fast!

# Chord Animated by Hari Wiguna, 2019
# ssd1306 library by AdaFruit

import machine, ssd1306, math, urandom

nFrames = 36
xMax = 64
yMax = 48
x0 = xMax / 2
y0 = yMax / 2

i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
oled = ssd1306.SSD1306_I2C(xMax, yMax, i2c)


def randint(min, max):  # ESP8266 does not have randint. Remove this if you're on the ESP32.'
    span = max - min + 1
    div = 0x3fffffff // span
    offset = urandom.getrandbits(30) // div
    val = min + offset
    return val


def draw_chord(f, xOff, yOff):
    node_count = 9
    r_max = yMax * 2

    r = f * r_max / nFrames
    rot = f * math.pi * 2 / nFrames
    x = []
    y = []
    for i in range(0, node_count):
        a = -rot + math.pi * 2 * i / node_count
        x.append(int(x0 + xOff + math.sin(a) * r))
        y.append(int(y0 + yOff + math.cos(a) * r))

    oled.fill(0)
    for i in range(0, node_count - 1):
        for j in range(i, node_count):
            oled.line(x[i], y[i], x[j], y[j], 1)
    oled.show()


def zoom_in(xOff, yOff):
    for f in range(nFrames):
        draw_chord(f, xOff, yOff)


def zoom_out(xOff, yOff):
    for f in range(nFrames-1, -1, -1):
        draw_chord(f, xOff, yOff)


def main():
    while True:
        xOff = randint(-x0, x0)
        yOff = randint(-y0, y0)
        zoom_in(xOff, yOff)
        zoom_out(xOff, yOff)


main()

Discussions