-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefly1.py
133 lines (112 loc) · 3.13 KB
/
firefly1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
import spi
from time import sleep
import random, time
width = 26
height = 16
thresh = 100 # charge threshold
charge = 1 # charging rate
sync = 13 # boost given to neighbors
rate = 20 # frames per sec
status = spi.openSPI(speed=5000000)
print "SPI configuration = ", status
# print "PY: initialising SPI mode, reading data, reading length . . . \n"
pixels = 424
class display(object):
def __init__(self, pixels):
self.pixels = pixels
self.clear()
def update(self):
spi.transferrgb(self.red, self.green, self.blue)
def set(self, pos, rv, gv, bv):
'''set pixel at pos to rgb'''
self.red[pos] = rv
self.green[pos] = gv
self.blue[pos] = bv
def set2d(self, x, y, rv, gv, bv):
if y & 1:
off = y * 26 + x
else:
off = y * 26 + (25 - x)
self.set(off, rv, gv, bv)
def setall(self, rv, gv, bv):
'''set all pixels to rgb'''
for pos in xrange(self.pixels):
self.red[pos] = rv
self.green[pos] = gv
self.blue[pos] = bv
def clear(self):
'''clear all pixels to black'''
self.red = [0] * pixels
self.green = [0] * pixels
self.blue = [0] * pixels
d = display(424)
# how we display/fade
bright = 10
levels = ' .oO'
# build emptyarrays
screen = []
state = []
for y in range(height):
screen.append([0] * width)
state.append([0] * width)
# set all fireflies to a random state
for x in range(width):
for y in range(height):
state[y][x] = random.randint(0, thresh)
def push(x, y):
if 0 <= x < width and 0 <= y < height:
if state[y][x] > thresh * .8:
state[y][x] += sync
while 1:
# update state
for x in range(width):
for y in range(height):
# fully charged?
if state[y][x] > thresh:
# flash on screen
screen[y][x] = bright
# lose all charge
state[y][x] = 0
# speed up nearest four neighbors
push(x, y-1)
push(x, y+1)
push(x-1, y)
push(x+1, y)
else:
# charge up
state[y][x] += charge
# blank/fade
if screen[y][x] > 0:
screen[y][x] -= 1
# draw screen
for y in range(height):
for x in range(width):
d.set2d(x, y, screen[y][x]* 255/bright, 0, 0)
d.update()
time.sleep(1.0/rate)
SLEEPTIME=2
#At the beginning of the program open up the SPI port.
#this is port /dev/spidevX.Y
#Being called as as spi.SPI(X,Y)
#a = spi.SPI(0,0)
#spi.initialize()
def chase(red, green, blue):
for light in range(0, pixels, 2):
d.set(light, red, green, blue)
#print("Setting LED #", light, red, green, blue)
if(light != pixels):
light = light + 1
d.set(light, red, green, blue)
# if(light != pixels):
# light = light + 1
# d.set(light, red, green, blue)
#print("Setting LED #", light, red, green, blue)
d.update()
sleep(.02)
d.clear()
d.update()
while 1:
for y in xrange(16):
d.set2d(10, y, 0, 15 * y, 0)
d.update()