-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
198 additions
and
83 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
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') |
This file contains 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
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) |
This file contains 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
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 |
This file was deleted.
Oops, something went wrong.
This file contains 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
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() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.