|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Test code for Adafruit LED Pixels, uses hardware SPI |
| 4 | + |
| 5 | +import RPi.GPIO as GPIO, time, os |
| 6 | + |
| 7 | +DEBUG = 1 |
| 8 | +GPIO.setmode(GPIO.BCM) |
| 9 | + |
| 10 | +def slowspiwrite(clockpin, datapin, byteout): |
| 11 | + GPIO.setup(clockpin, GPIO.OUT) |
| 12 | + GPIO.setup(datapin, GPIO.OUT) |
| 13 | + for i in range(8): |
| 14 | + if (byteout & 0x80): |
| 15 | + GPIO.output(datapin, True) |
| 16 | + else: |
| 17 | + GPIO.output(clockpin, False) |
| 18 | + byteout <<= 1 |
| 19 | + GPIO.output(clockpin, True) |
| 20 | + GPIO.output(clockpin, False) |
| 21 | + |
| 22 | + |
| 23 | +SPICLK = 18 |
| 24 | +SPIDO = 17 |
| 25 | + |
| 26 | +ledpixels = [0] * 25 |
| 27 | + |
| 28 | +def writestrip(pixels): |
| 29 | + spidev = file("/dev/spidev0.0", "w") |
| 30 | + for i in range(len(pixels)): |
| 31 | + spidev.write(chr((pixels[i]>>16) & 0xFF)) |
| 32 | + spidev.write(chr((pixels[i]>>8) & 0xFF)) |
| 33 | + spidev.write(chr(pixels[i] & 0xFF)) |
| 34 | + spidev.close() |
| 35 | + time.sleep(0.002) |
| 36 | + |
| 37 | +def Color(r, g, b): |
| 38 | + return ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF) |
| 39 | + |
| 40 | +def setpixelcolor(pixels, n, r, g, b): |
| 41 | + if (n >= len(pixels)): |
| 42 | + return |
| 43 | + pixels[n] = Color(r,g,b) |
| 44 | + |
| 45 | +def setpixelcolor(pixels, n, c): |
| 46 | + if (n >= len(pixels)): |
| 47 | + return |
| 48 | + pixels[n] = c |
| 49 | + |
| 50 | +def colorwipe(pixels, c, delay): |
| 51 | + for i in range(len(pixels)): |
| 52 | + setpixelcolor(pixels, i, c) |
| 53 | + writestrip(pixels) |
| 54 | + time.sleep(delay) |
| 55 | + |
| 56 | +def Wheel(WheelPos): |
| 57 | + if (WheelPos < 85): |
| 58 | + return Color(WheelPos * 3, 255 - WheelPos * 3, 0) |
| 59 | + elif (WheelPos < 170): |
| 60 | + WheelPos -= 85; |
| 61 | + return Color(255 - WheelPos * 3, 0, WheelPos * 3) |
| 62 | + else: |
| 63 | + WheelPos -= 170; |
| 64 | + return Color(0, WheelPos * 3, 255 - WheelPos * 3) |
| 65 | + |
| 66 | +def rainbowCycle(pixels, wait): |
| 67 | + for j in range(256): # one cycle of all 256 colors in the wheel |
| 68 | + for i in range(len(pixels)): |
| 69 | +# tricky math! we use each pixel as a fraction of the full 96-color wheel |
| 70 | +# (thats the i / strip.numPixels() part) |
| 71 | +# Then add in j which makes the colors go around per pixel |
| 72 | +# the % 96 is to make the wheel cycle around |
| 73 | + setpixelcolor(pixels, i, Wheel( ((i * 256 / len(pixels)) + j) % 256) ) |
| 74 | + writestrip(pixels) |
| 75 | + time.sleep(wait) |
| 76 | + |
| 77 | +colorwipe(ledpixels, Color(255, 0, 0), 0.05) |
| 78 | +colorwipe(ledpixels, Color(0, 255, 0), 0.05) |
| 79 | +colorwipe(ledpixels, Color(0, 0, 255), 0.05) |
| 80 | +while True: |
| 81 | + rainbowCycle(ledpixels, 0.00) |
0 commit comments