Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mcauser committed Feb 13, 2024
1 parent 5bd0fb6 commit ac34d25
Show file tree
Hide file tree
Showing 20 changed files with 752 additions and 283 deletions.
55 changes: 55 additions & 0 deletions examples/auto_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# SPDX-FileCopyrightText: 2024 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813

# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write on
num_leds = 10
chain = p9813.P9813_BITBANG(clk, data, num_leds, True)

# With auto-write enabled, each time you modify the LEDs, it calls
# write() and sends the data for every pixel in the chain.

# You can't update a single LED. You have to write the data for all.

# When making lots of individual LED changes, it's faster to defer
# writing until all changes have been made.

# Set first LED to red
chain[0] = (127, 0, 0)
# Notice it's immediately red

# Switch off auto-write
chain.auto_write = False

# Set second LED to green
chain[1] = (0, 127, 0)

# Set third LED to blue
chain[2] = (0, 0, 127)

# With auto-write off, modifications to the chain only update
# the internal buffer (checksum + red + green + blue values)
# Calling write() later sends the buffer to all LEDs.

# Notice the second and third LEDs are not lit yet
chain.write()
# Now they are lit

# Switch auto-write back on
chain.auto_write = True

# Make all LEDs green
chain.fill((0, 127, 0))
# Auto-write is temporarily ignored as fill() populates the entire buffer.
# Once the buffer is ready, if auto-write was previously on, data is send out.
43 changes: 31 additions & 12 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
# SPDX-FileCopyrightText: 2017 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813

pin_clk = Pin(5, Pin.OUT)
pin_data = Pin(4, Pin.OUT)
# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# TinyS2 (esp32s2)
# clk = Pin(4, Pin.OUT)
# data = Pin(5, Pin.OUT)

# Wemos D1 Mini (esp8266)
# clk = Pin(4, Pin.OUT)
# data = Pin(5, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write off
num_leds = 10
chain = p9813.P9813(pin_clk, pin_data, num_leds)
auto_write = False
chain = p9813.P9813_BITBANG(clk, data, num_leds, auto_write)

# set the first pixel to red
# Set the first LED to red
chain[0] = (255, 0, 0)

# set the first pixel to green
# Set the second LED to green
chain[1] = (0, 255, 0)

# set the first pixel to blue
# Set the third LED to blue
chain[2] = (0, 0, 255)

# changes are not visible until you...
# write data to all pixels
# With auto-write off, changes are not seen until you call write()
chain.write()

# get first pixel colour
r, g, b = chain[0]
# Get second LED colour
(r, g, b) = chain[2]

# get second pixel colour
r, g, b = chain[1]
# Print as hex colour
print(f"{r:02X}{g:02X}{b:02X}")
# prints 00FF00
34 changes: 34 additions & 0 deletions examples/bitbang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: 2024 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813

# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write on
num_leds = 10
chain = p9813.P9813_BITBANG(clk, data, num_leds, True)


def refresh_rate_test(count):
red = (16, 0, 0)
green = (0, 16, 0)
blue = (0, 0, 16)
colors = [red, green, blue]
for i in range(count):
chain[i % num_leds] = colors[i % 3]


# For ~4 seconds, rapidly draw red, green, blue incrementally.
# Roughly 2x slower than Software SPI at baud 100_000.
# Running the same test will instead of showing solid white LEDs,
# show them all flickering as it rapidly draws the colours.
refresh_rate_test(500)
52 changes: 34 additions & 18 deletions examples/bounce.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
# SPDX-FileCopyrightText: 2017 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813
from time import sleep_ms
import p9813

# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write off
num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
chain = p9813.P9813_BITBANG(clk, data, num_leds, False)


def bounce(color, sleep):
for i in range(4 * num_leds):
for j in range(num_leds):
chain[j] = color
if (i // num_leds) % 2 == 0:
chain[i % num_leds] = (0, 0, 0)
else:
chain[num_leds - 1 - (i % num_leds)] = (0, 0, 0)
chain.write()
sleep_ms(sleep)

red = (16,0,0)
green = (0,16,0)
colors = [red,green]

# Bounce a dark pixel back and forth
for i in range(4 * num_leds):
for j in range(num_leds):
chain[j] = color
if (i // num_leds) % 2 == 0:
chain[i % num_leds] = (0, 0, 0)
else:
chain[num_leds - 1 - (i % num_leds)] = (0, 0, 0)
chain.write()
sleep_ms(sleep)


# Test colours
red = (16, 0, 0)
green = (0, 16, 0)
colors = [red, green]

# Bounce a dark (off) LED back and forth
for color in colors:
bounce(color, 0)
bounce(color, 0)
55 changes: 36 additions & 19 deletions examples/color_wipe.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
# SPDX-FileCopyrightText: 2017 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813
from time import sleep_ms
import p9813

# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write off
num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
chain = p9813.P9813_BITBANG(clk, data, num_leds, False)

# Illuminate the pixels one by one

# Illuminate the LEDs one-by-one
# X.......
# XX......
# XXX.....
Expand All @@ -15,20 +29,23 @@
# XXXXXXX.
# XXXXXXXX
def color_wipe(color, wait):
for i in range(num_leds):
chain[i] = color
chain.write()
sleep_ms(wait)

red = (16,0,0)
green = (0,16,0)
blue = (0,0,16)
cyan = (0,16,16)
magenta = (16,0,16)
yellow = (16,16,0)
black = (0,0,0)
colors = [red,green,blue,cyan,magenta,yellow,black]

# Illuminate the pixels one by one, keeping them lit
for i in range(num_leds):
chain[i] = color
chain.write()
sleep_ms(wait)


# Test colours
red = (16, 0, 0)
green = (0, 16, 0)
blue = (0, 0, 16)
cyan = (0, 16, 16)
magenta = (16, 0, 16)
yellow = (16, 16, 0)
black = (0, 0, 0)
colors = [red, green, blue, cyan, magenta, yellow, black]

# Illuminate the LEDs one-by-one, keeping them lit as it
# moves on to the next
for color in colors:
color_wipe(color, 0)
color_wipe(color, 10)
59 changes: 37 additions & 22 deletions examples/cycle.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
# SPDX-FileCopyrightText: 2017 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813
from time import sleep_ms
import p9813

# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write off
num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
chain = p9813.P9813_BITBANG(clk, data, num_leds, False)


def cycle(color, sleep):
for i in range(num_leds):
for j in range(num_leds):
chain[j] = (0, 0, 0)
chain[i % num_leds] = color
chain.write()
sleep_ms(sleep)

# Predefine some colours
red = (16,0,0)
green = (0,16,0)
blue = (0,0,16)
cyan = (0,16,16)
magenta = (16,0,16)
yellow = (16,16,0)
white = (16,16,16)
black = (0,0,0)
colors = [red,green,blue,cyan,magenta,yellow,white,black]

# Illuminate the pixels one by one
for i in range(num_leds):
for j in range(num_leds):
chain[j] = (0, 0, 0)
chain[i % num_leds] = color
chain.write()
sleep_ms(sleep)


# Test colours
red = (16, 0, 0)
green = (0, 16, 0)
blue = (0, 0, 16)
cyan = (0, 16, 16)
magenta = (16, 0, 16)
yellow = (16, 16, 0)
white = (16, 16, 16)
black = (0, 0, 0)
colors = [red, green, blue, cyan, magenta, yellow, white, black]

# Illuminate the LEDs one-by-one
for color in colors:
cycle(color, 0)
cycle(color, 10)
33 changes: 24 additions & 9 deletions examples/fade.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
# SPDX-FileCopyrightText: 2017 Mike Causer <https://github.com/mcauser>
# SPDX-License-Identifier: MIT

"""
MicroPython P9813 RGB LED driver
https://github.com/mcauser/micropython-p9813
"""

from machine import Pin
import p9813

# TinyPICO (esp32)
clk = Pin(4, Pin.OUT)
data = Pin(14, Pin.OUT)

# Bit-banged, 10 LEDs, auto-write off
num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
chain = p9813.P9813_BITBANG(clk, data, num_leds, False)


def fade():
for i in range(0, 4 * 256, 8):
for j in range(num_leds):
if (i // 256) % 2 == 0:
val = i & 0xff
else:
val = 255 - (i & 0xff)
chain[j] = (val, 0, 0)
chain.write()
for i in range(0, 4 * 256, 8):
for j in range(num_leds):
if (i // 256) % 2 == 0:
val = i & 0xFF
else:
val = 255 - (i & 0xFF)
chain[j] = (val, 0, 0)
chain.write()


# Fade in/out
fade()
Loading

0 comments on commit ac34d25

Please sign in to comment.