164 lines
3.5 KiB
Python
164 lines
3.5 KiB
Python
#!/usr/bin/python
|
|
from MCP23S17 import *
|
|
from thread import *
|
|
from random import randint
|
|
|
|
class lightChip(MCP23S17):
|
|
def __init__(self, addr,cable_select,clock,miso,mosi):
|
|
MCP23S17.__init__(self,addr, cable_select, clock, miso, mosi, False)
|
|
self.setDirection(0xff,0x00) # configure a as input, b as outputs
|
|
self.sendSPI(0x0C, 0xFF) # pullups aktivieren
|
|
self.sendSPI(0x02, 0xFF) # logik invertieren
|
|
self.state=[0,0,0,0,0,0,0,0]
|
|
start_new_thread(self.shine,())
|
|
|
|
def shine(self):
|
|
self.shining=True
|
|
lastval = 0
|
|
selected = 0
|
|
while self.shining:
|
|
d=0
|
|
for t in range(10,0,-1):
|
|
for chnl in range(0,8):
|
|
if self.state[chnl]>=t:
|
|
d|=1<<chnl
|
|
self.sendSPI(0x13,d)
|
|
time.sleep(0.00001)
|
|
val = self.readSPI(0x12);
|
|
if val != lastval:
|
|
if val == 32:
|
|
selected = (selected+1) % 8
|
|
start_new_thread(self.blink,(selected,))
|
|
elif val == 64:
|
|
self.state[selected] = min(self.state[selected]+1,10)
|
|
elif val == 128:
|
|
self.state[selected] = max(self.state[selected]-1,0)
|
|
elif val == 96:
|
|
for channel in range(0,8):
|
|
self.state[channel]=10
|
|
elif val == 192:
|
|
for channel in range(0,8):
|
|
self.state[channel]=0
|
|
elif val == 160:
|
|
self.halloween()
|
|
else:
|
|
print val
|
|
|
|
lastval = val
|
|
self.sendSPI(0x13,0)
|
|
print "stopped"
|
|
|
|
def activate(self,num):
|
|
self.sendSPI(0x13,1<<(num-1))
|
|
|
|
def halloween(self):
|
|
for c in range(0,50):
|
|
channel = randint(1,8)
|
|
duration = randint(10,100)/400.0
|
|
count = randint(1,20)
|
|
pause = randint(1,120)
|
|
for i in range(0,count):
|
|
self.activate(channel)
|
|
time.sleep(randint(0,20)/250.0)
|
|
self.activate(11)
|
|
time.sleep(randint(10,150)/200.0)
|
|
self.activate(6)
|
|
time.sleep(pause)
|
|
|
|
|
|
def blink(self,channel):
|
|
old = self.state[channel]
|
|
for c in range(0,2):
|
|
self.state[channel] = 1
|
|
time.sleep(0.01)
|
|
self.state[channel] = 0
|
|
time.sleep(0.2)
|
|
self.state[channel] = old
|
|
|
|
if __name__ == "__main__":
|
|
GPIO.cleanup()
|
|
GPIO.setmode(GPIO.BOARD);
|
|
GPIO.setwarnings(True);
|
|
|
|
CS=13
|
|
CLK=11
|
|
MOSI=7
|
|
MISO=5
|
|
|
|
CS=16
|
|
CLK=12
|
|
MOSI=10
|
|
MISO=8
|
|
print "configuring line pins."
|
|
GPIO.setup(CLK, GPIO.OUT)
|
|
GPIO.setup(MOSI, GPIO.OUT)
|
|
GPIO.setup(MISO, GPIO.IN)
|
|
GPIO.setup(CS, GPIO.OUT)
|
|
|
|
print "initializing line level."
|
|
GPIO.output(CS, GPIO.HIGH);
|
|
GPIO.output(CLK, GPIO.LOW);
|
|
chip = lightChip(0,CS, CLK, MISO, MOSI)
|
|
|
|
t=0.06
|
|
for i in range(0,6):
|
|
for b in range(1,11):
|
|
for c in range(0,8):
|
|
chip.state[c]=b
|
|
time.sleep(t)
|
|
time.sleep(0.5)
|
|
for b in range(10,-1,-1):
|
|
for c in range(0,8):
|
|
chip.state[c]=b
|
|
time.sleep(t)
|
|
|
|
t=0.1
|
|
for i in range(0,20):
|
|
|
|
chip.state[1]=10
|
|
time.sleep(t)
|
|
chip.state[1]=0
|
|
|
|
chip.state[4]=10
|
|
time.sleep(t)
|
|
chip.state[4]=0
|
|
|
|
chip.state[2]=10
|
|
time.sleep(t)
|
|
chip.state[2]=0
|
|
|
|
chip.state[3]=10
|
|
time.sleep(t)
|
|
chip.state[3]=0
|
|
|
|
chip.state[6]=10
|
|
time.sleep(t)
|
|
chip.state[6]=0
|
|
|
|
chip.state[7]=10
|
|
time.sleep(t)
|
|
chip.state[7]=0
|
|
|
|
chip.state[7]=0
|
|
t=0.06
|
|
for i in range(0,100):
|
|
chip.state[1]=10
|
|
chip.state[2]=10
|
|
chip.state[3]=10
|
|
chip.state[4]=10
|
|
chip.state[5]=10
|
|
time.sleep(t)
|
|
chip.state[1]=0
|
|
chip.state[2]=0
|
|
chip.state[3]=0
|
|
chip.state[4]=0
|
|
chip.state[5]=0
|
|
|
|
chip.state[0]=10
|
|
chip.state[7]=10
|
|
time.sleep(t)
|
|
chip.state[7]=0
|
|
chip.state[0]=0
|
|
time.sleep(1)
|
|
GPIO.cleanup()
|