Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcauser committed Jun 13, 2017
0 parents commit f304728
Show file tree
Hide file tree
Showing 20 changed files with 700 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Mike Causer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# P9813

A MicroPython library for P9813 RGB LED drivers.

For example, the Seeed Studio [Grove - Chainable RGB LED](http://wiki.seeed.cc/Grove-Chainable_RGB_LED/).

![demo](docs/demo.jpg)

## Example

Copy the file to your device, using ampy, webrepl or compiling and deploying. eg.

```
$ ampy put p9813.py
```

**Basic usage**

```
from machine import Pin
import p9813
pin_clk = Pin(5, Pin.OUT)
pin_data = Pin(4, Pin.OUT)
num_leds = 10
chain = p9813.P9813(pin_clk, pin_data, num_leds)
# set the first LED to red
chain[0] = (255, 0, 0)
# set the second LED to green
chain[1] = (0, 255, 0)
# write data to all LEDs
chain.write()
# make all LEDs red
chain.fill((255,0,0))
chain.write()
# turn off all LEDs
chain.reset()
```

See [p9813_examples.py](p9813_examples.py) and [examples](examples/) for more.

## Parts

* [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
* [Grove - Chainable RGB LED](https://www.seeedstudio.com/Grove-Chainable-RGB-LED-p-850.html) $3.90 USD
* [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

## Connections

WeMos D1 Mini | Grove Chainable RGB LED
------------- | -----------------------
D1 (GPIO5) | CI (clock) (yellow)
D2 (GPIO4) | DI (data) (white)
3V3 (or 5V) | VCC (red)
G | GND (black)

If you are chaining multiple LEDs, clock out -> clock in, data out -> data in, eg.

LED1 | LED2
---- | ----
CO | CI (yellow)
DO | DI (white)
VCC | VCC (red)
GND | GND (black)

## Links

* [WeMos D1 Mini](https://wiki.wemos.cc/products:d1:d1_mini)
* [micropython.org](http://micropython.org)
* [Adafruit Ampy](https://learn.adafruit.com/micropython-basics-load-files-and-run-code/install-ampy)
* [P9813 datasheet](https://raw.githubusercontent.com/SeeedDocument/Grove-Chainable_RGB_LED/master/res/P9813_datasheet.pdf)
Binary file added docs/demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from machine import Pin
import p9813

pin_clk = Pin(5, Pin.OUT)
pin_data = Pin(4, Pin.OUT)

num_leds = 10
chain = p9813.P9813(pin_clk, pin_data, num_leds)

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

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

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

# changes are not visible until you...
# write data to all pixels
chain.write()

# get first pixel colour
r, g, b = chain[0]

# get second pixel colour
r, g, b = chain[1]
25 changes: 25 additions & 0 deletions examples/bounce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from machine import Pin
import p9813
from time import sleep_ms

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

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 color in colors:
bounce(color, 0)
34 changes: 34 additions & 0 deletions examples/color_wipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from machine import Pin
import p9813
from time import sleep_ms

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

# Illuminate the pixels one by one
# X.......
# XX......
# XXX.....
# XXXX....
# XXXXX...
# XXXXXX..
# 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 color in colors:
color_wipe(color, 0)
29 changes: 29 additions & 0 deletions examples/cycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from machine import Pin
import p9813
from time import sleep_ms

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

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 color in colors:
cycle(color, 0)
18 changes: 18 additions & 0 deletions examples/fade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from machine import Pin
import p9813

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

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()

# Fade in/out
fade()
26 changes: 26 additions & 0 deletions examples/fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from machine import Pin
import p9813

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

red = (16,0,0)
green = (0,16,0)
blue = (0,0,16)

chain.fill(red)
chain.write()

chain.fill(gren)
chain.write()

chain.fill(blue)
chain.write()

chain.fill((0,0,0))
chain.write()

chain.fill((0xff,0x99,0x00))
chain.write()

chain.reset()
55 changes: 55 additions & 0 deletions examples/level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from machine import Pin
import p9813
from time import sleep_ms

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

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

def level(n):
for i in range(num_leds):
if n > i:
chain[i] = colors[i]
else:
chain[i] = black
chain.write()

# 10 levels, starting with green then yellow then red
level(5)
level(0)
level(10)

for i in range(num_leds + 1):
level(i)


last = 0
def level_sticky(n):
global last
for i in range(num_leds):
if n > i:
chain[i] = colors[i]
elif last > i:
(r, g, b) = colors[i]
chain[i] = (r//8, g//8, b//8)
else:
chain[i] = black
last = n
chain.write()

# 10 levels, with previous level remaining 1/8th lit for 1 step, like a vu meter
level_sticky(7)
level_sticky(10)
level_sticky(8)
level_sticky(5)

for i in range(0,1000,23):
level_sticky(i % num_leds)
sleep_ms(50)

# why inc by 23? just a prime number for some pseduo-randomness
25 changes: 25 additions & 0 deletions examples/rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from machine import Pin
import p9813

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

def rainbow():
for i in range(num_leds):
chain[i] = wheel((i * 256 // num_leds) % 255)
chain.write()

# Helper for converting 0-255 offset to a colour tuple
def wheel(offset):
# The colours are a transition r - g - b - back to r
offset = 255 - offset
if offset < 85:
return (255 - offset * 3, 0, offset * 3)
if offset < 170:
offset -= 85
return (0, offset * 3, 255 - offset * 3)
offset -= 170
return (offset * 3, 255 - offset * 3, 0)

# Show all colours of the rainbow
rainbow()
30 changes: 30 additions & 0 deletions examples/rainbow_cycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from machine import Pin
import p9813
from time import sleep_ms

num_leds = 10
chain = p9813.P9813(Pin(5), Pin(4), num_leds)

def rainbow_cycle(np, sleep):
# 5 cycles of all colors on wheel
for r in range(5):
for n in range(256):
for i in range(num_leds):
chain[i] = wheel(((i * 256 // num_leds) + n) & 255)
chain.write()
sleep_ms(sleep)

# Helper for converting 0-255 offset to a colour tuple
def wheel(offset):
# The colours are a transition r - g - b - back to r
offset = 255 - offset
if offset < 85:
return (255 - offset * 3, 0, offset * 3)
if offset < 170:
offset -= 85
return (0, offset * 3, 255 - offset * 3)
offset -= 170
return (offset * 3, 255 - offset * 3, 0)

# Fade all pixels together through rainbow colours, offset each pixel
rainbow_cycle(0)
Loading

0 comments on commit f304728

Please sign in to comment.