Working WS2812 LED Example

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Architekt
Posts: 10
Joined: Wed Oct 15, 2014 8:59 am

Re: Working WS2812 LED Example

Post by Architekt » Sat Nov 08, 2014 9:32 am

Hi, I made driver for WS2812 LEDs which provides you more hi-level Pythonic interface. More here: http://forum.micropython.org/viewtopic.php?f=5&t=394

alanwang
Posts: 1
Joined: Thu Nov 20, 2014 7:21 am

Re: Working WS2812 LED Example

Post by alanwang » Thu Nov 20, 2014 7:23 am

Hello Markus, do you have the MicroPython code OFAPA102 LED

RetroJeff
Posts: 1
Joined: Mon Mar 01, 2021 4:41 am

Re: Working WS2812 LED Example

Post by RetroJeff » Mon Mar 01, 2021 4:47 am

Resurrecting a Zombie thread I found with google.
I am programming a Raspberry Pi Pico with a string of WS2811
I really didn't want to load Adafruits' version of Python, preferring to see it work with standard Micropython.
Based on the post by Marcus, I was able to come up with this.

If you are still on the forum, thank you sir!

RJ

Code: Select all

"""
 ###############################################
 ##                                           ##
 ##   WS281X CODE FOR PICO WITH MICROPYTHON   ##
 ##                                           ##
 ## BASED ON DRIVER EXAMPLE BY Markus Gritsch ##
 ## Demo by RetroJeff                         ##
 ##                                           ##
 ###############################################

"""

import machine
import utime
import math
import gc

ws281x=machine.Pin(3)    # This is the actual data pin of the WS2811/WS2812
led_count = 50           # Number of Leds in the device

spi = machine.SPI(0, baudrate=6400000, mosi=ws281x)
buf = bytearray(led_count * 3)
spi_data = bytearray(len(buf) * 8)
spi.write(chr(0x00))

### SUBROUTINES FOR LIGHTING WS2811 LAMPS ###
def buf2data():
    b0 = 0x03
    b1 = 0x0F
    i = 0
    size = len(spi_data)
    while i < size:
        byte = buf[ i >> 3 ]
        mask = 0x80
        while mask != 0:
            spi_data[ i ] = b0 if ( byte & mask ) == 0 else b1
            mask >>= 1
            i += 1

def send_buf():
    buf2data()
    spi.write(spi_data)
    gc.collect()

def lamp(digit,red,green,blue):
    pos = digit * 3
    buf[pos] = red; buf[pos + 1] = green; buf[pos + 2] = blue
    send_buf()

""" MAIN CODE """
n = 0

while True:
    for x in range(0, 20):
        lamp(x,255, 0,0)

    n=20
    for x in range(0, 20):
        n=n-1
        lamp(n,0, 0,255)

Post Reply