Skip to content

Commit

Permalink
Rewrote samples
Browse files Browse the repository at this point in the history
  • Loading branch information
tinue committed Dec 11, 2015
1 parent 8e39997 commit 33c4903
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 83 deletions.
Empty file modified Finished.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified LICENSE
100644 → 100755
Empty file.
9 changes: 7 additions & 2 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Ideally, a 5$ Raspberry Pi Zero is dedicated to the task of driving the LEDs. Th

## Wiring

The Raspberry Pi is a 3.3 Volt device, and the APA102 LEDs are 5 Volt devices. Therefore it's possible that the 3.3 Volt SPI signal is not being recognized by the LED driver chips. To avoid this risk, use a 74AHCT125 level shifter for both the clock and the MOSI signal.
The Raspberry Pi is a 3.3 Volt device, and the APA102 LEDs are 5 Volt devices. Therefore it's possible that the 3.3 Volt SPI signal is not being recognized by the LED driver chips. To avoid this risk, use a 74AHCT125 or 74AHC125 level shifter for both the clock and the MOSI signal.

Without a level shifter, the wiring is very simple:
- LED ground to one of the Raspberry ground pins
Expand All @@ -49,4 +49,9 @@ Videos can't be embedded yet, so head over to youtube: https://youtu.be/N0MK1z8W


## Using the library
TODO
TODO

## Release history
2015-04-13: Initial version
2015-12-04: Add documentation
2015-12-11: Rewrote the examples, driver itself is unchanged
Empty file modified Wiring.jpg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions basecolorcycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import apa102
import time

"""
This class is the basis of all color cycles, such as rainbow or theater chase.
"""

class BaseColorCycle:
def __init__(self, numLEDs, pauseValue = 0, globalBrightness = 31): # Init method
self.numLEDs = numLEDs
self.pauseValuse = pauseValue
self.globalBrightness = globalBrightness
timeOfLastCall = 0
cycleCounter = 0

"""
void init()
This method is called to initialize a color program.
"""
def init(self):
# The default does nothing. A particular subclass could e.g. light all LEDs to white.
print('Init')

"""
void shutdown()
This method is called at the end, when the light program shoule terminate
"""
def shutdown(self):
# The default does nothing
print('Shutdown')

"""
void update()
This method paints one cycle. It must be implemented
"""
def update(self):
raise NotImplementedError("Please implement the update() method")

"""
Start the actual work
"""

try:
strip = apa102.APA102(numPixels, globalBrightness) # Low brightness (2 out of max. 31)
while True: # Loop forever
cycleCounter += 1
update()
strip.show()
time.sleep(pauseValue)

except KeyboardInterrupt: # Abbruch...
print('Interrupted...')
strip.clearStrip()
print('Strip cleared')
strip.cleanup()
print('SPI closed')
75 changes: 75 additions & 0 deletions colorcycletemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import apa102
import time

"""
This class is the basis of all color cycles, such as rainbow or theater chase.
A specific color cycle must subclass this template, and implement at least the
'update' method.
"""
class ColorCycleTemplate:
def __init__(self, numLEDs, pauseValue = 0, numCycles = -1, globalBrightness = 4): # Init method
self.numLEDs = numLEDs # The number of LEDs in the strip
self.pauseValue = pauseValue # How long to pause between two runs
self.numCycles = numCycles # How many times will the program run
self.globalBrightness = globalBrightness # Brightness of the strip

"""
void init()
This method is called to initialize a color program.
"""
def init(self, strip, numLEDs):
# The default does nothing. A particular subclass could setup variables, or
# even light the strip in an initial color.
print('Init not implemented')

"""
void shutdown()
This method is called at the end, when the light program should terminate
"""
def shutdown(self, strip, numLEDs):
# The default does nothing
print('Shutdown not implemented')

"""
void update()
This method paints one subcycle. It must be implemented
currentStep: This goes from zero to numLEDs, and then back to zero
currentCycle: Counts up whenever the currentStep goes back to zero
"""
def update(self, strip, numLEDs, currentStep, currentCycle):
raise NotImplementedError("Please implement the update() method")

def cleanup(self, strip):
self.shutdown(strip, self.numLEDs)
strip.clearStrip()
print('Strip cleared')
strip.cleanup()
print('SPI closed')

"""
Start the actual work
"""
def start(self):
try:
strip = apa102.APA102(self.numLEDs, self.globalBrightness) # Initialize the strip
strip.clearStrip()
self.init(strip, self.numLEDs) # Call the subclasses init method
strip.show()
currentCycle = 0
currentStep = 0
while True: # Loop forever
needRepaint = self.update(strip, self.numLEDs, currentStep, currentCycle) # Call the subclasses update method
if (needRepaint): strip.show() # Display, only if required
time.sleep(self.pauseValue) # Pause until the next iteration
currentStep += 1
if (currentStep >= self.numLEDs):
currentStep = 0 # Start next loop
currentCycle += 1
if (self.numCycles != -1):
if (currentCycle >= self.numCycles): break
# Finished, cleanup everything
self.cleanup(strip)

except KeyboardInterrupt: # Ctrl-C can halt the light program
print('Interrupted...')
self.cleanup(strip)
50 changes: 50 additions & 0 deletions colorschemes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from colorcycletemplate import ColorCycleTemplate

class StrandTest(ColorCycleTemplate):

def init(self, strip, numLEDs):
self.color = 0x000000 # Initialize with black

def update(self, strip, numLEDs, currentStep, currentCycle):
if (currentStep == 0): self.color >>= 8 # Red->green->blue->black
if (self.color == 0): self.color = 0xFF0000 # If black, reset to red

head = (currentStep + 9) % numLEDs # The head pixel that will be turned on in this cycle
tail = currentStep # The tail pixel that will be turned off
strip.setPixelRGB(head, self.color) # Paint head
strip.setPixelRGB(tail, 0) # Clear tail

return 1 # Repaint is necessary


class TheaterChase(ColorCycleTemplate):
def update(self, strip, numLEDs, currentStep, currentCycle):
startIndex = currentStep % 7 # Each segment is 7 dots long: 2 blank, and 5 filled
colorIndex = strip.wheel((currentCycle*numLEDs+currentStep) % 255)
for pixel in range(numLEDs):
# Two LEDs out of 7 are blank. At each step, the blank ones move one pixel ahead.
if ((pixel+startIndex) % 7 == 0) or ((pixel+startIndex) % 7 == 1): strip.setPixelRGB(pixel, 0)
else: strip.setPixelRGB(pixel, colorIndex)
return 1



class Solid(ColorCycleTemplate):

def init(self, strip, numLEDs):
for led in range(0, numLEDs):
strip.setPixelRGB(led,0xFFFFFF) # Paint white

def update(self, strip, numLEDs, currentStep, currentCycle):
# Do nothing: Init lit the strip, and update just keeps it this way
return 0


class Rainbow(ColorCycleTemplate):
def update(self, strip, numLEDs, currentStep, currentCycle):
shiftCounter = currentCycle * numLEDs + currentStep
for i in range(numLEDs): # spread (or compress) one rainbow onto the strip
# For a faster shift, add more than 1 * j per loop (e.g. + 2 * j)
index = strip.wheel((((i << 8) // numLEDs) + shiftCounter) & 255)
strip.setPixelRGB(i, index);
return 1 # Repaint the strip
22 changes: 0 additions & 22 deletions rainbow.py

This file was deleted.

10 changes: 10 additions & 0 deletions runColorCycle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import colorschemes

myCycle = colorschemes.Solid(numLEDs=430, pauseValue=0.01, numCycles = 1)
myCycle.start()
myCycle = colorschemes.StrandTest(numLEDs=430, pauseValue=0, numCycles = 4, globalBrightness=10)
myCycle.start()
myCycle = colorschemes.Rainbow(numLEDs=430, pauseValue=0, numCycles = 6, globalBrightness=10)
myCycle.start()
myCycle = colorschemes.TheaterChase(numLEDs=430, pauseValue=0.04, numCycles = 2, globalBrightness=10)
myCycle.start()
29 changes: 0 additions & 29 deletions strandtest.py

This file was deleted.

30 changes: 0 additions & 30 deletions theaterchase.py

This file was deleted.

0 comments on commit 33c4903

Please sign in to comment.