forked from topshed/UnicornHatScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUHScroll.py
134 lines (115 loc) · 4.31 KB
/
UHScroll.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
#V1.01
''' This python script provides the functions to display simple scrolling text on
a Pimoroni Unicorn hat, and add-on board for the Raspberry Pi model B+'''
import unicornhat as UH
from bitarray import bitarray
import time
#import letter definitions and mappings
from UHScroll_defs import *
'''It assumes the Pi/hat will orientated with the long side of the Pi without any connectors on
the bottom, i.e. the Hat will be rotated 90 degrees clockwise (assuming the "UNICORN HAT" label and
Pimoroni logo are normally at the bottom of the hat. If you want to use a different orientation
then you can alter the UH.rotation value in the show_letter function below. You may also need to adjust or omit
the flip call which is used to ensure that the bitarray definitions in uhscroll_letters are the correct
way round for easy reading'''
flip = [7,6,5,4,3,2,1,0]
_scrollrotation = 270
def rotation(r=_scrollrotation):
global _scrollrotation
if r in [0, 90, 180, 270]:
_scrollrotation = r
return True
else:
raise ValueError('Rotation must be 0, 90, 180 or 270 degrees')
def show_letter(letter,colour,brightness): #displays a single letter on th UH
UH.rotation(_scrollrotation)
for i in range(8):
for j in range(8):
if letter[j][i]:
if colour == 'red':
UH.set_pixel(j,flip[i],brightness,0,0)
elif colour == 'green':
UH.set_pixel(j,flip[i],0,brightness,0)
elif colour == 'blue':
UH.set_pixel(j,flip[i],0,0,brightness)
elif colour == 'white':
UH.set_pixel(j,flip[i],brightness,brightness,brightness)
elif colour == 'pink':
UH.set_pixel(j,flip[i],brightness,52,179)
elif colour == 'cyan':
UH.set_pixel(j,flip[i],0,brightness,brightness)
elif colour == 'yellow':
UH.set_pixel(j,flip[i],brightness,brightness,0)
elif colour == 'orange':
UH.set_pixel(j,flip[i],brightness,128,0)
else:
UH.set_pixel(j,flip[i],0,0,0)
UH.show()
def scroll_letter(letter,colour,brightness,speed): # scrolls a single letter across the UH
for i in range(8):
for p in range(6):
letter[i].insert(0,False)
for s in range(14):
show_letter(letter,colour,brightness)
time.sleep(speed)
for i in range(8):
letter[i].pop(0)
letter[i].append(0)
'''scrolling is achieved by redrawing the letter with a column of the bitarray shifted to the left and a new blank column
added to the right'''
def scroll_word(word,colour,brightness,speed): # scrolls a word across the UH
for s in range(len(word[0])):
show_letter(word,colour,brightness)
time.sleep(speed)
for i in range(8):
word[i].pop(0)
word[i].append(0)
def make_word(words): # takes a list of chars and concats into a word by making one big bitarray
bigword = [bitarray(''),bitarray(''), bitarray(''),bitarray(''), bitarray(''),bitarray(''), bitarray(''),bitarray('')]
for w in range(len(words)):
for i in range(len(words[w])):
bigword[i] = bigword[i] + words[w][i]
return bigword
def trim_letter(letter): #trims a char's bitarray so that it can be joined without too big a gap
trim = []
for c in range(len(letter)):
trim.append(letter[c].copy())
if letter not in super_wides:
for i in range(8):
if letter not in wides:
trim[i].pop(0)
trim[i].pop(0)
trim[i].pop(5)
if letter in narrows:
trim[i].pop(0)
if letter in super_narrow:
trim[i].pop(0)
return trim
def map_character(chr):
if chr in mapping:
return mapping[chr]
else:
return mapping['_']
def load_message(message):
unicorn_message = []
message = ' ' + message # pad the message with a couple of spaces so it starts on the right
skip = 0
for ch in (range(len(message))):
#print message[ch]
if skip != 0:
skip-=1
else:
if message[ch] == '~':
spec = message[ch+1] + message[ch+2] + message[ch+3] + message[ch+4] + message[ch+5]
unicorn_message.append(trim_letter(map_character(spec)))
skip = 5
else:
unicorn_message.append(trim_letter(map_character(message[ch].upper())))
return(unicorn_message)
def unicorn_scroll(text,colour,brightness,speed):
#try:
scroll_word(make_word(load_message(text)),colour,brightness,speed)
#except:
#print 'Enter unicorn_scroll(message,colour,brightness,speed) where '
#print 'message is a string, colour is either red,white,blue,green,pink, yellow, orange or cyan'
#print 'brightness is a integer 0-255 and speed is the time between chars'