Skip to content

Commit f304728

Browse files
committed
Initial commit
0 parents  commit f304728

20 files changed

+700
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mike Causer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# P9813
2+
3+
A MicroPython library for P9813 RGB LED drivers.
4+
5+
For example, the Seeed Studio [Grove - Chainable RGB LED](http://wiki.seeed.cc/Grove-Chainable_RGB_LED/).
6+
7+
![demo](docs/demo.jpg)
8+
9+
## Example
10+
11+
Copy the file to your device, using ampy, webrepl or compiling and deploying. eg.
12+
13+
```
14+
$ ampy put p9813.py
15+
```
16+
17+
**Basic usage**
18+
19+
```
20+
from machine import Pin
21+
import p9813
22+
23+
pin_clk = Pin(5, Pin.OUT)
24+
pin_data = Pin(4, Pin.OUT)
25+
26+
num_leds = 10
27+
chain = p9813.P9813(pin_clk, pin_data, num_leds)
28+
29+
# set the first LED to red
30+
chain[0] = (255, 0, 0)
31+
32+
# set the second LED to green
33+
chain[1] = (0, 255, 0)
34+
35+
# write data to all LEDs
36+
chain.write()
37+
38+
# make all LEDs red
39+
chain.fill((255,0,0))
40+
chain.write()
41+
42+
# turn off all LEDs
43+
chain.reset()
44+
```
45+
46+
See [p9813_examples.py](p9813_examples.py) and [examples](examples/) for more.
47+
48+
## Parts
49+
50+
* [WeMos D1 Mini](https://www.aliexpress.com/store/product/D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/1331105_32529101036.html) $4.00 USD
51+
* [Grove - Chainable RGB LED](https://www.seeedstudio.com/Grove-Chainable-RGB-LED-p-850.html) $3.90 USD
52+
* [Grove Male Jumper Cable](https://www.seeedstudio.com/Grove-4-pin-Male-Jumper-to-Grove-4-pin-Conversion-Cable-%285-PCs-per-Pack%29-p-1565.html) $2.90 USD
53+
54+
## Connections
55+
56+
WeMos D1 Mini | Grove Chainable RGB LED
57+
------------- | -----------------------
58+
D1 (GPIO5) | CI (clock) (yellow)
59+
D2 (GPIO4) | DI (data) (white)
60+
3V3 (or 5V) | VCC (red)
61+
G | GND (black)
62+
63+
If you are chaining multiple LEDs, clock out -> clock in, data out -> data in, eg.
64+
65+
LED1 | LED2
66+
---- | ----
67+
CO | CI (yellow)
68+
DO | DI (white)
69+
VCC | VCC (red)
70+
GND | GND (black)
71+
72+
## Links
73+
74+
* [WeMos D1 Mini](https://wiki.wemos.cc/products:d1:d1_mini)
75+
* [micropython.org](http://micropython.org)
76+
* [Adafruit Ampy](https://learn.adafruit.com/micropython-basics-load-files-and-run-code/install-ampy)
77+
* [P9813 datasheet](https://raw.githubusercontent.com/SeeedDocument/Grove-Chainable_RGB_LED/master/res/P9813_datasheet.pdf)

docs/demo.jpg

565 KB
Loading

examples/basic.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from machine import Pin
2+
import p9813
3+
4+
pin_clk = Pin(5, Pin.OUT)
5+
pin_data = Pin(4, Pin.OUT)
6+
7+
num_leds = 10
8+
chain = p9813.P9813(pin_clk, pin_data, num_leds)
9+
10+
# set the first pixel to red
11+
chain[0] = (255, 0, 0)
12+
13+
# set the first pixel to green
14+
chain[1] = (0, 255, 0)
15+
16+
# set the first pixel to blue
17+
chain[2] = (0, 0, 255)
18+
19+
# changes are not visible until you...
20+
# write data to all pixels
21+
chain.write()
22+
23+
# get first pixel colour
24+
r, g, b = chain[0]
25+
26+
# get second pixel colour
27+
r, g, b = chain[1]

examples/bounce.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from machine import Pin
2+
import p9813
3+
from time import sleep_ms
4+
5+
num_leds = 10
6+
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
7+
8+
def bounce(color, sleep):
9+
for i in range(4 * num_leds):
10+
for j in range(num_leds):
11+
chain[j] = color
12+
if (i // num_leds) % 2 == 0:
13+
chain[i % num_leds] = (0, 0, 0)
14+
else:
15+
chain[num_leds - 1 - (i % num_leds)] = (0, 0, 0)
16+
chain.write()
17+
sleep_ms(sleep)
18+
19+
red = (16,0,0)
20+
green = (0,16,0)
21+
colors = [red,green]
22+
23+
# Bounce a dark pixel back and forth
24+
for color in colors:
25+
bounce(color, 0)

examples/color_wipe.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from machine import Pin
2+
import p9813
3+
from time import sleep_ms
4+
5+
num_leds = 10
6+
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
7+
8+
# Illuminate the pixels one by one
9+
# X.......
10+
# XX......
11+
# XXX.....
12+
# XXXX....
13+
# XXXXX...
14+
# XXXXXX..
15+
# XXXXXXX.
16+
# XXXXXXXX
17+
def color_wipe(color, wait):
18+
for i in range(num_leds):
19+
chain[i] = color
20+
chain.write()
21+
sleep_ms(wait)
22+
23+
red = (16,0,0)
24+
green = (0,16,0)
25+
blue = (0,0,16)
26+
cyan = (0,16,16)
27+
magenta = (16,0,16)
28+
yellow = (16,16,0)
29+
black = (0,0,0)
30+
colors = [red,green,blue,cyan,magenta,yellow,black]
31+
32+
# Illuminate the pixels one by one, keeping them lit
33+
for color in colors:
34+
color_wipe(color, 0)

examples/cycle.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from machine import Pin
2+
import p9813
3+
from time import sleep_ms
4+
5+
num_leds = 10
6+
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
7+
8+
def cycle(color, sleep):
9+
for i in range(num_leds):
10+
for j in range(num_leds):
11+
chain[j] = (0, 0, 0)
12+
chain[i % num_leds] = color
13+
chain.write()
14+
sleep_ms(sleep)
15+
16+
# Predefine some colours
17+
red = (16,0,0)
18+
green = (0,16,0)
19+
blue = (0,0,16)
20+
cyan = (0,16,16)
21+
magenta = (16,0,16)
22+
yellow = (16,16,0)
23+
white = (16,16,16)
24+
black = (0,0,0)
25+
colors = [red,green,blue,cyan,magenta,yellow,white,black]
26+
27+
# Illuminate the pixels one by one
28+
for color in colors:
29+
cycle(color, 0)

examples/fade.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from machine import Pin
2+
import p9813
3+
4+
num_leds = 10
5+
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
6+
7+
def fade():
8+
for i in range(0, 4 * 256, 8):
9+
for j in range(num_leds):
10+
if (i // 256) % 2 == 0:
11+
val = i & 0xff
12+
else:
13+
val = 255 - (i & 0xff)
14+
chain[j] = (val, 0, 0)
15+
chain.write()
16+
17+
# Fade in/out
18+
fade()

examples/fill.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from machine import Pin
2+
import p9813
3+
4+
num_leds = 10
5+
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
6+
7+
red = (16,0,0)
8+
green = (0,16,0)
9+
blue = (0,0,16)
10+
11+
chain.fill(red)
12+
chain.write()
13+
14+
chain.fill(gren)
15+
chain.write()
16+
17+
chain.fill(blue)
18+
chain.write()
19+
20+
chain.fill((0,0,0))
21+
chain.write()
22+
23+
chain.fill((0xff,0x99,0x00))
24+
chain.write()
25+
26+
chain.reset()

examples/level.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from machine import Pin
2+
import p9813
3+
from time import sleep_ms
4+
5+
num_leds = 10
6+
chain = p9813.P9813(Pin(5), Pin(4), num_leds)
7+
8+
green = (0,16,0)
9+
yellow = (16,16,0)
10+
red = (16,0,0)
11+
black = (0,0,0)
12+
colors = [green,green,green,green,green,green,green,yellow,yellow,red]
13+
14+
def level(n):
15+
for i in range(num_leds):
16+
if n > i:
17+
chain[i] = colors[i]
18+
else:
19+
chain[i] = black
20+
chain.write()
21+
22+
# 10 levels, starting with green then yellow then red
23+
level(5)
24+
level(0)
25+
level(10)
26+
27+
for i in range(num_leds + 1):
28+
level(i)
29+
30+
31+
last = 0
32+
def level_sticky(n):
33+
global last
34+
for i in range(num_leds):
35+
if n > i:
36+
chain[i] = colors[i]
37+
elif last > i:
38+
(r, g, b) = colors[i]
39+
chain[i] = (r//8, g//8, b//8)
40+
else:
41+
chain[i] = black
42+
last = n
43+
chain.write()
44+
45+
# 10 levels, with previous level remaining 1/8th lit for 1 step, like a vu meter
46+
level_sticky(7)
47+
level_sticky(10)
48+
level_sticky(8)
49+
level_sticky(5)
50+
51+
for i in range(0,1000,23):
52+
level_sticky(i % num_leds)
53+
sleep_ms(50)
54+
55+
# why inc by 23? just a prime number for some pseduo-randomness

0 commit comments

Comments
 (0)