Last active
August 29, 2015 14:22
-
-
Save arthurbenemann/acfac3978a22cf278d7e to your computer and use it in GitHub Desktop.
ubled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # | |
| # This is a test code for UBLEDs, controlled by a raspberry pi 2. | |
| # Photos and video at https://goo.gl/89iMrc | |
| # PB3xx301V050A (http://goo.gl/0cyNxy) | |
| # | |
| # 1.Enable the raspberry SPI bus (https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/) | |
| # | |
| # 2.Connected to ubled chain to SPI pins on Raspberry (http://www.jameco.com/Jameco/workshop/circuitnotes/raspberry_pi_circuit_note_fig2a.jpg) | |
| # 5V (1) - 5V (2) | |
| # SDI (2) - SPI0_MOSI (19) | |
| # CKI (3) - SPI0_SCLK (23) | |
| # Neg (4) - GND (25) | |
| # | |
| # 3. Run it with 'python ubled.py' , you may need 'sudo' at the start | |
| import time | |
| import sys | |
| import spidev | |
| import struct | |
| import math | |
| import colorsys | |
| def start_frame(): | |
| return [0,0,0,0] | |
| def parse_int_or_float(value): | |
| if isinstance(value,int): | |
| return value | |
| else: | |
| return int(value*0xff) | |
| def pack_rgb(rgb): | |
| r_mask = (parse_int_or_float(rgb[0])>>3) & 0x1f | |
| g_mask = (parse_int_or_float(rgb[1])>>3) & 0x1f | |
| b_mask = (parse_int_or_float(rgb[2])>>3) & 0x1f | |
| data = 0x8000 | (r_mask<<10) | (b_mask<<5) | (g_mask) | |
| array = [(data>>8) & 0xff, data & 0x00ff] | |
| return array | |
| def pack_chain(list, padding): | |
| array = start_frame() | |
| for i in list: | |
| array.extend(pack_rgb(i)) | |
| padding_size = padding-len(list) | |
| if padding >0: | |
| array.extend(pack_rgb([0,0,0])*padding_size) | |
| # this extra byte is required to end the transfer | |
| array.extend([0x00]) | |
| return array | |
| def update_chain(list, padding = 0): | |
| spi = spidev.SpiDev() | |
| spi.open(0,0) | |
| transfer = pack_chain(list, padding) | |
| spi.xfer2(transfer) | |
| spi.close() | |
| if __name__ == '__main__': | |
| t = 0; | |
| while True: | |
| t += 1.0/100 | |
| phase = 3.1415*2/12 | |
| update_chain([ | |
| ((math.sin(t+0*phase)/2)+0.5,0x00,0x00), | |
| (0x0,(math.sin(t+1*phase)/2)+0.5,0x00), | |
| (0x00,0x00,(math.sin(t+2*phase)/2)+0.5), | |
| ((math.sin(t+3*phase)/2)+0.5,0x00,0x00), | |
| (0x0,(math.sin(t+4*phase)/2)+0.5,0x00), | |
| (0x00,0x00,(math.sin(t+5*phase)/2)+0.5), | |
| ((math.sin(t+6*phase)/2)+0.5,0x00,0x00), | |
| (0x0,(math.sin(t+7*phase)/2)+0.5,0x00), | |
| (0x00,0x00,(math.sin(t+8*phase)/2)+0.5), | |
| ],9) | |
| time.sleep(0.001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment