-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframealert.py
executable file
·129 lines (107 loc) · 4.03 KB
/
framealert.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
#!/usr/bin/env python3
from rgbmatrix import RGBMatrix, RGBMatrixOptions
from widget import Widget
from PIL import Image, ImageDraw, ImageFont
from threading import Timer
import time
import unicodedata
class FrameAlert(Widget):
def __init__(self,x=0,y=0,color=None,width=64,height=64):
if color is None:
self.color = (255,0,0)
else:
self.color = color
super(FrameAlert,self).__init__(x,y,self.color,width,height)
def blinkon(self):
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
draw.rectangle([self.x,self.y,self.width-1,self.height-1],outline=self.color)
self.changed = True
def blinkoff(self):
self.image = Image.new("RGBA",(self.width,self.height))
draw = ImageDraw.Draw(self.image)
draw.rectangle([self.x,self.y,self.width-1,self.height-1],outline=(0,0,0))
self.changed = True
def update(self,count=3,duration=0.1,pause=0.1):
if count > 0:
self.blinkon()
t = Timer(duration,self.blinkoff)
t.start()
for x in range(count):
s = Timer(pause*x,self.blinkon)
s.start()
o = Timer(pause*x+duration,self.blinkoff)
o.start()
class ImageAlert(Widget):
def __init__(self,x=0,y=0,color=None,size=32,width=64,height=64,\
filename=None,howlong=5):
super(ImageAlert,self).__init__(x,y,color,size,width,height)
self.filename = filename
self.alertimage = self.image.copy()
loadimage = Image.open(self.filename)
if loadimage and (loadimage.size[0] > size or loadimage.size[1] > size):
loadimage.thumbnail((size,size))
if loadimage:
self.alertimage.paste(loadimage,(x,y))
self.howlong = howlong
self.ison = False
def off(self):
self.image = Image.new("RGBA",(self.width,self.height))
self.ison = False
self.changed = True
def on(self):
self.image = self.alertimage.copy()
self.ison = True
self.changed = True
self.onat = time.time()
def update(self):
if (self.ison and time.time() > self.onat + self.howlong):
self.off()
class UnicodeAlert(Widget):
def __init__(self,x=0,y=0,color=None,size=32,width=64,height=64,\
description=None,howlong=5):
super(UnicodeAlert,self).__init__(x,y,color,size,width,height)
self.alertimage = self.image.copy()
draw = ImageDraw.Draw(self.alertimage)
font = ImageFont.truetype("TwitterColorEmoji-SVGinOT.ttf",self.size)
self.description = description
self.ison = False
self.howlong = howlong
if description != None:
try:
t = unicodedata.lookup(description)
except KeyError:
print("symbol "+description+" not found")
t = None
if t == None:
return
draw.text((self.x,self.y),t,font=font,fill=self.color)
def off(self):
self.image = Image.new("RGBA",(self.width,self.height))
self.ison = False
self.changed = True
def on(self):
self.image = self.alertimage.copy()
self.ison = True
self.changed = True
self.onat = time.time()
def update(self):
if (self.ison and time.time() > self.onat + self.howlong):
self.off()
if __name__ == "__main__":
options = RGBMatrixOptions()
options.rows = 64
options.cols = 64
matrix = RGBMatrix(options = options)
alertframe = FrameAlert()
alertimage = UnicodeAlert(description="black telephone",size=32, color=(255,0,0))
# alertframe.update(count=5,duration=0.1,pause=0.2)
alertimage.on()
while True:
alertimage.update()
if (alertimage.changed):
alertimage.changed = False
im = Image.new("RGBA",(64,64))
im.alpha_composite(alertimage.image)
matrix.SetImage(im.convert("RGB"))
time.sleep(0.1)