Light printer

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
randomhuman
Posts: 19
Joined: Mon Jun 09, 2014 1:54 pm

Light printer

Post by randomhuman » Mon Jun 16, 2014 9:53 pm

Playing around with a micropython board and some Adafruit "Neopixel" LEDs to make a light printer.

Image

It's not very sophisticated as yet. I tried to key the updates to the accelerometer, but it turned out I got more consistent results just updating at a constant rate.

wsled.py (based on code by Markus Gritsch http://forum.micropython.org/viewtopic.php?f=2&t=149):

Code: Select all

def _set_bit(b, mask):
    return b | mask

def _clear_bit(b, mask):
    return b & ~mask

def _set_bit_for_boolean(b, mask, val):
    if val:
        return _set_bit(b, mask)
    else:
        return _clear_bit(b, mask)

class WSLedChain(object):

    def __init__(self, bus, length):
        self.bus = bus
        if isinstance(self.bus, int):
            from pyb import SPI
            self.bus = SPI(self.bus, SPI.MASTER, baudrate=3200000, polarity=0, phase=1)
            self.bus.send(chr(0x00))
        self.length = length
        # Each LED requires three pieces of color data, and each color requires
        # 4 bytes for the SPI transfer.
        self._data = bytearray(3 * length * 4)
        self._initialize_data()

    def _initialize_data(self):
        for index in range(self.length):
            self.set_color(index, (0, 0, 0))

    def refresh(self):
        self.bus.send(self._data)

    def _set_data(self, index, byte):
        mask = 0x80
        for bit in range(0, 8, 2):
            bit1 = (0x1 if (byte & (mask >> bit) == 0) else 0x3)
            bit2 = (0x1 if (byte & (mask >> (bit + 1)) == 0) else 0x3)
            self._data[index + (bit//2)] = bit1 << 4 | bit2

    def _get_data(self, index):
        mask = 0x80
        byte = 0
        for bit in range(0, 8, 2):
            bit_data = self._data[index + (bit//2)]
            bit1 = bit_data >> 4
            bit2 = bit_data & 0x0f
            byte = _set_bit_for_boolean(byte, mask >> bit, bit1==0x3)
            byte = _set_bit_for_boolean(byte, mask >> (bit + 1), bit2==0x3)
        return byte

    def set_color(self, index, color):
        base_index = index * 12
        self._set_data(base_index, color[1])
        self._set_data(base_index + 4, color[0])
        self._set_data(base_index + 8, color[2])

    def get_color(self, index):
        base_index = index * 12
        return (self._get_data(base_index + 4), self._get_data(base_index), self._get_data(base_index + 8))

lightprinter.py:

Code: Select all


from wsled import WSLedChain
import pyb

x = (255,  80, 100)
_ = (  0,   0,   0)
z = ( 80,   0, 255)
y = ( 60, 255,  60)
o = (  0,   0,   0)

hi_py_data = (
    (o,o,o,o,),
    (_,_,_,_,),
    (x,x,x,x,),
    (_,x,_,_,),
    (x,x,x,x,),
    (_,_,_,_,),
    (x,x,x,_,),
    (_,_,_,_,),
    (_,_,_,_,),
    (_,_,_,_,),
    (y,y,y,y,),
    (_,y,_,_,),
    (_,y,y,y,),
    (z,z,z,z,),
    (_,z,_,z,),
    (_,_,z,_,),
    (_,_,_,_,),
    (_,_,_,z,),
    (_,_,z,_,),
    (z,z,_,_,),
    (_,_,z,_,),
    (_,_,_,z,),
    (_,_,_,_,),
    (o,o,o,o,),
)


def light_print(chain, data, time):
    pyb.delay(500)
    index = 0
    for index in range(len(data)):
        for p_index, pixel in enumerate(data[index]):
            chain.set_color(p_index, pixel)
        chain.refresh()

        pyb.delay(time//len(data))


def start_printer(bus, data, time):
    chain = WSLedChain(bus, 4)
    sw = pyb.Switch()
    while True:
        pyb.wfi()
        if sw():
            light_print(chain, data, time)

main.py:

Code: Select all

import lightprinter

lightprinter.start_printer(2, lightprinter.hi_py_data, 1000)

I am having a lot of fun with this board :D

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Light printer

Post by dhylands » Mon Jun 16, 2014 10:01 pm

Sweet

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: Light printer

Post by polygontwist » Sat Jun 28, 2014 8:18 pm

nice,
You could also use 4 LED from the phy-board ;-)

nande
Posts: 13
Joined: Fri Oct 10, 2014 6:11 pm
Location: argentina
Contact:

Re: Light printer

Post by nande » Mon Nov 03, 2014 9:16 pm

Awesome, this is something that i could try to build :)
~namida de ashita ga mienai~

Post Reply