-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wsd2.py
286 lines (246 loc) · 9.5 KB
/
Wsd2.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/python
# wsd project main script
# hardware: ws2801 led strips + raspberry pi + internet adapter
# software pulls twits from an 'admin' (twits and retwits) and
# displays the last result through the led strip
# Written by Pratipo.org, hightly based on Adafruit's IoT Pinter. MIT license.
# MUST BE RUN AS ROOT (due to GPIO access)
import RPi.GPIO as GPIO
import time, random
class Wsd2:
def __init__(self, m=30, h=7, w=5):
# Open SPI device
dev = "/dev/spidev0.0"
self.spidev = file(dev, "wb")
self.lapse = 0.1
self.tick = 0
self.gamma = bytearray(256)
for i in range(256):
self.gamma[i] = int(pow(float(i) / 255.0, 2.5) * 255.0 + 0.5)
self.asciiTable = [
[0x00,0x00,0x00,0x00,0x00], # 0x20 32
[0x00,0x00,0x6f,0x00,0x00], # ! 0x21 33
[0x00,0x07,0x00,0x07,0x00], # " 0x22 34
[0x14,0x7f,0x14,0x7f,0x14], # # 0x23 35
[0x00,0x07,0x04,0x1e,0x00], # $ 0x24 36
[0x23,0x13,0x08,0x64,0x62], # % 0x25 37
[0x36,0x49,0x56,0x20,0x50], # & 0x26 38
[0x00,0x00,0x07,0x00,0x00], # ' 0x27 39
[0x00,0x1c,0x22,0x41,0x00], # ( 0x28 40
[0x00,0x41,0x22,0x1c,0x00], # ) 0x29 41
[0x14,0x08,0x3e,0x08,0x14], # * 0x2a 42
[0x08,0x08,0x3e,0x08,0x08], # + 0x2b 43
[0x00,0x50,0x30,0x00,0x00], # , 0x2c 44
[0x08,0x08,0x08,0x08,0x08], # - 0x2d 45
[0x00,0x60,0x60,0x00,0x00], # . 0x2e 46
[0x20,0x10,0x08,0x04,0x02], # / 0x2f 47
[0x3e,0x51,0x49,0x45,0x3e], # 0 0x30 48
[0x00,0x42,0x7f,0x40,0x00], # 1 0x31 49
[0x42,0x61,0x51,0x49,0x46], # 2 0x32 50
[0x21,0x41,0x45,0x4b,0x31], # 3 0x33 51
[0x18,0x14,0x12,0x7f,0x10], # 4 0x34 52
[0x27,0x45,0x45,0x45,0x39], # 5 0x35 53
[0x3c,0x4a,0x49,0x49,0x30], # 6 0x36 54
[0x01,0x71,0x09,0x05,0x03], # 7 0x37 55
[0x36,0x49,0x49,0x49,0x36], # 8 0x38 56
[0x06,0x49,0x49,0x29,0x1e], # 9 0x39 57
[0x00,0x36,0x36,0x00,0x00], # : 0x3a 58
[0x00,0x56,0x36,0x00,0x00], # ; 0x3b 59
[0x08,0x14,0x22,0x41,0x00], # < 0x3c 60
[0x14,0x14,0x14,0x14,0x14], # = 0x3d 61
[0x00,0x41,0x22,0x14,0x08], # > 0x3e 62
[0x02,0x01,0x51,0x09,0x06], # ? 0x3f 63
[0x3e,0x41,0x5d,0x49,0x4e], # @ 0x40 64
[0x7e,0x09,0x09,0x09,0x7e], # A 0x41 65
[0x7f,0x49,0x49,0x49,0x36], # B 0x42 66
[0x3e,0x41,0x41,0x41,0x22], # C 0x43 67
[0x7f,0x41,0x41,0x41,0x3e], # D 0x44 68
[0x7f,0x49,0x49,0x49,0x41], # E 0x45 69
[0x7f,0x09,0x09,0x09,0x01], # F 0x46 70
[0x3e,0x41,0x49,0x49,0x7a], # G 0x47 71
[0x7f,0x08,0x08,0x08,0x7f], # H 0x48 72
[0x00,0x41,0x7f,0x41,0x00], # I 0x49 73
[0x20,0x40,0x41,0x3f,0x01], # J 0x4a 74
[0x7f,0x08,0x14,0x22,0x41], # K 0x4b 75
[0x7f,0x40,0x40,0x40,0x40], # L 0x4c 76
[0x7f,0x02,0x0c,0x02,0x7f], # M 0x4d 77
[0x7f,0x04,0x08,0x10,0x7f], # N 0x4e 78
[0x3e,0x41,0x41,0x41,0x3e], # O 0x4f 79
[0x7f,0x09,0x09,0x09,0x06], # P 0x50 80
[0x3e,0x41,0x51,0x21,0x5e], # Q 0x51 81
[0x7f,0x09,0x19,0x29,0x46], # R 0x52 82
[0x46,0x49,0x49,0x49,0x31], # S 0x53 83
[0x01,0x01,0x7f,0x01,0x01], # T 0x54 84
[0x3f,0x40,0x40,0x40,0x3f], # U 0x55 85
[0x0f,0x30,0x40,0x30,0x0f], # V 0x56 86
[0x3f,0x40,0x30,0x40,0x3f], # W 0x57 87
[0x63,0x14,0x08,0x14,0x63], # X 0x58 88
[0x07,0x08,0x70,0x08,0x07], # Y 0x59 89
[0x61,0x51,0x49,0x45,0x43], # Z 0x5a 90
[0x3c,0x4a,0x49,0x29,0x1e], # [ 0x5b 91
[0x02,0x04,0x08,0x10,0x20], # \ 0x5c 92
[0x00,0x41,0x7f,0x00,0x00], # ] 0x5d 93
[0x04,0x02,0x01,0x02,0x04], # ^ 0x5e 94
[0x40,0x40,0x40,0x40,0x40], # _ 0x5f 95
[0x00,0x00,0x03,0x04,0x00], # ` 0x60 96
[0x20,0x54,0x54,0x54,0x78], # a 0x61 97
[0x7f,0x48,0x44,0x44,0x38], # b 0x62 98
[0x38,0x44,0x44,0x44,0x20], # c 0x63 99
[0x38,0x44,0x44,0x48,0x7f], # d 0x64 100
[0x38,0x54,0x54,0x54,0x18], # e 0x65 101
[0x08,0x7e,0x09,0x01,0x02], # f 0x66 102
[0x0c,0x52,0x52,0x52,0x3e], # g 0x67 103
[0x7f,0x08,0x04,0x04,0x78], # h 0x68 104
[0x00,0x44,0x7d,0x40,0x00], # i 0x69 105
[0x20,0x40,0x44,0x3d,0x00], # j 0x6a 106
[0x00,0x7f,0x10,0x28,0x44], # k 0x6b 107
[0x00,0x41,0x7f,0x40,0x00], # l 0x6c 108
[0x7c,0x04,0x18,0x04,0x78], # m 0x6d 109
[0x7c,0x08,0x04,0x04,0x78], # n 0x6e 110
[0x38,0x44,0x44,0x44,0x38], # o 0x6f 111
[0x7c,0x14,0x14,0x14,0x08], # p 0x70 112
[0x08,0x14,0x14,0x18,0x7c], # q 0x71 113
[0x7c,0x08,0x04,0x04,0x08], # r 0x72 114
[0x48,0x54,0x54,0x54,0x20], # s 0x73 115
[0x04,0x3f,0x44,0x40,0x20], # t 0x74 116
[0x3c,0x40,0x40,0x20,0x7c], # u 0x75 117
[0x1c,0x20,0x40,0x20,0x1c], # v 0x76 118
[0x3c,0x40,0x30,0x40,0x3c], # w 0x77 119
[0x44,0x28,0x10,0x28,0x44], # x 0x78 120
[0x0c,0x50,0x50,0x50,0x3c], # y 0x79 121
[0x44,0x64,0x54,0x4c,0x44], # z 0x7a 122
[0x00,0x08,0x36,0x41,0x41], # [ 0x7b 123
[0x00,0x00,0x7f,0x00,0x00], # | 0x7c 124
[0x41,0x41,0x36,0x08,0x00], # ] 0x7d 125
[0x04,0x02,0x04,0x08,0x04] # ~ 0x7e 126
]
self.modules = m
self.moduleH = h
self.moduleW = w
self.mN = self.moduleH*self.moduleW # pixels in one module
self.preOffset = self.moduleW * 5
#160 == twit total length in chars
#self.stringLength = 140 +10
#self.asciiString = [32 for i in range(140+10)] # 32 == 'space' in the ascii table
#self.binMatrix = [[0 for i in range(self.moduleH)] for j in range(140+10*(self.moduleW+1))]
self.asciiString = [32 for i in range(140 + 10)]
self.binMatrix = [[0 for i in range(h)] for j in range((140+10)*(w+1))]
self.pixels = bytearray(m*w*h*3)
self.mode = 0
self.colors = [0 for i in range(140+10)]
def setText(self, t):
self.asciiString = [ord(c) for c in t]
self.asciiTobinMatrix()
def asciiTobinMatrix(self):
bits = [1,2,4,8,16,32,64,128]
print("twit length")
print(len(self.asciiString))
self.binMatrix = [[0 for i in range(self.moduleH)] for j in range((140+10)*(self.moduleW+1))]
for char in range(len(self.asciiString)):
for col in range(self.moduleW):
for row in range(self.moduleH):
self.binMatrix[char*(self.moduleW+1) + col + self.preOffset][row] = bool( self.asciiTable[self.asciiString [char]-32][col] & bits[row] )
def setPixel(self, x, y, color):
b = x/int(self.moduleW)
r = x%self.moduleW # division left-over part
if (y%2 == 1): # if odd raw
r = (self.moduleW-1) - r
# pixels in previous panels + previous pixels in panel
pindex = ( b*self.mN + ( y*int(self.moduleW) + r ) )*3
for i in range(3):
self.pixels[pindex + i] = self.gamma[color[i]]
def display(self):
# print 'displaying text'
self.spidev.write(self.pixels)
self.spidev.flush()
time.sleep(0.001)
def wheel(self, wheelPos):
if (wheelPos < 85):
return [wheelPos * 3, 255 - wheelPos * 3, 0]
elif (wheelPos < 170):
wheelPos -= 85
return [255 - wheelPos * 3, 0, wheelPos * 3]
else:
wheelPos -= 170
return [0, wheelPos * 3, 255 - wheelPos * 3]
def loadPixels(self, seed, seed2, offset=0):
for x in range(self.modules*self.moduleW):
color= [0,0,0]
for y in range(self.moduleH):
if (self.mode == 7):
color = [255,0,0]
elif (self.mode == 8):
color = [0,255,0]
elif (self.mode == 9):
color = [0,0,255]
elif (self.mode == 10):
color = [255,255,255]
#random colored pixels
elif (self.mode == 0):
color = self.wheel((random.randint(0,255)+x+self.tick)%255)
#gradient colored pixels... modes 1 to 3
elif (self.mode == 1):
color = self.wheel((seed+4*(x+self.tick))%255)
elif (self.mode == 2):
color = self.wheel((seed-4*self.tick-3*y)%255)
elif (self.mode == 3):
color = self.wheel((seed+4*x)%255)
# stripes
elif (self.mode == 4):
if(((x+y)/(seed2))%2):
color = self.wheel(seed%255)
else:
color= [100,100,100]
else:
if ( len(self.colors)-1 >= (x+offset-1)/(self.moduleW+1) ):
if(self.mode == 5):
color = self.wheel( (self.colors[(x+offset-1)/(self.moduleW+1)] + self.tick)%255 )
elif(self.mode == 6):
color = self.wheel((self.colors[(x+offset-1)/(self.moduleW+1)] + 2*self.tick + 4*y)%255)
if ( (x+offset)>=len(self.binMatrix) ): #??? clear end of the text -> pixel is turned off
self.setPixel(x, y, [0, 0 ,0])
else:
if (self.binMatrix[x+offset][y]):
c = [0,0,0]
# fix diferent color schema in the last 9 modules
#remap R G B chanels
if (x < self.moduleW*12):
# first 11 modules are GBR :$
c = [color[1],color[2],color[0]]
else:
# last modules are BRG :$
c = [color[2],color[0],color[1]]
#end fix
self.setPixel(x, y, c)
else:
self.setPixel(x, y, [0, 0 ,0])
self.tick += 1
self.display()
def rollPixels(self, roll):
self.colors = [random.randint(0,255) for r in range(len(self.asciiString))]
print(self.colors)
self.mode = random.randint(1,10) #0-6 color combinations
print(self.mode)
seedColor = random.randint(0,255)
bandwidth = random.randint(1,7)
if (roll):
ran = range( (self.moduleW+1)*(len(self.asciiString))+self.preOffset )
else:
ran = range(1)
for offset in ran:
self.loadPixels(seedColor, bandwidth, offset)
if(roll):
time.sleep(self.lapse)
else:
time.sleep(self.lapse*300)
def staticPixels(self):
self.colors = 0
self.mode = 7
print(self.mode)
seedColor = 0
bandwidth = 0
self.loadPixels(seedColor, bandwidth, 0)
time.sleep(self.lapse*300)
#display = Wsd2()
#display.setText("Practicas artisticas y redes")
#display.rollPixels()