forked from tanzilli/ledpanel-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
www-rpi.py
executable file
·182 lines (139 loc) · 3.87 KB
/
www-rpi.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
#!/usr/bin/python
import tornado.ioloop
import tornado.web
import time
import sys
import os
from datetime import datetime
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import StringIO
import thread
import threading
import signal
import commands
import subprocess
class SlidingMessage(threading.Thread):
message="LedPanel"
stop_request_flag=False
sliding_delay=4
red=0
green=0
blue=1
# setup font
#font1 = ImageFont.truetype('fonts/Ubuntu-B.ttf',30)
#text_w, text_h = font1.getsize(message)
#output = StringIO.StringIO()
def if_stop_requested(self):
return self.stop_request_flag
def stop(self):
self.stop_request_flag=True
def set_message(self,message):
message = message + " "
self.message=message
self.refresh_msg()
def refresh_msg (self):
font = ImageFont.truetype('fonts/Ubuntu-B.ttf', 32)
width, height = font.getsize(self.message)
print "[%s] W: %d H: %d" % (self.message, width, height)
r=self.red << 4
g=self.green << 4
b=self.blue << 4
im = Image.new("RGB", (width, 32), "black")
draw = ImageDraw.Draw(im)
draw.fontmode="1" #No antialias
output = StringIO.StringIO()
out_file = open("/tmp/out.ppm","w")
draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((0,-1), self.message, (r,g,b), font=font)
output.truncate(0)
im.save(output, format='PPM')
buf=output.getvalue()
out_file.seek(0)
out_file.write(buf)
out_file.close()
subprocess.call(["sudo", "pkill", "demo"])
def set_color(self,red,green,blue):
self.red=red
self.green=green
self.blue=blue
self.refresh_msg()
def set_sliding_delay(self,sliding_delay):
self.sliding_delay=sliding_delay+1
self.refresh_msg()
def run(self):
while True:
subprocess.call(["sudo",
"/home/pi/rpi-rgb-led-matrix/examples-api-use/demo",
"--led-chain=10",
"-t 10000000000",
"-D 1",
"-m %d" % self.sliding_delay,
"/tmp/out.ppm",
"&"
])
return
def format_time():
d = datetime.now()
return "{:%H:%M}".format(d)
class set_color(tornado.web.RequestHandler):
def get(self):
red = int(self.get_argument("red", default="0"))
green = int(self.get_argument("green", default="0"))
blue = int(self.get_argument("blue", default="0"))
t.set_color(red,green,blue)
class set_sliding_delay(tornado.web.RequestHandler):
def get(self):
sliding_delay = int(self.get_argument("delay", default="0"))
t.set_sliding_delay(sliding_delay)
class send_message(tornado.web.RequestHandler):
def get(self):
global t
message = self.get_argument("message", default="")
t.set_message(message)
class do_shutdown(tornado.web.RequestHandler):
def get(self):
global t
t.set_message("Shutdown....")
subprocess.call(["sudo",
"shutdown",
"-H",
"-h",
"now",
"&"
])
class do_reboot(tornado.web.RequestHandler):
def get(self):
global t
t.set_message("Reboot....")
subprocess.call(["sudo",
"reboot",
"&"
])
def signal_handler(signal, frame):
global sliding_message
print 'You pressed Ctrl+C!'
t.stop()
t.join()
sys.exit(0)
application = tornado.web.Application([
(r"/send_message", send_message),
(r"/set_color", set_color),
(r"/set_sliding_delay", set_sliding_delay),
(r"/do_shutdown", do_shutdown),
(r"/do_reboot", do_reboot),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": ".","default_filename": "index-rpi.html"}),
])
t=SlidingMessage()
if __name__ == "__main__":
ips = commands.getoutput("ifconfig | grep -i inet | grep -iv inet6 | " +
"grep -iv 127.0.0.1 | grep -iv 192.168.10.10 |" +
"awk {'print $2'} | sed -ne 's/addr\:/ /p'")
iplist=ips.splitlines()
t.set_message ("myIP" + " -".join(iplist))
t.start()
signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C to exit'
application.listen(8080,"0.0.0.0")
tornado.ioloop.IOLoop.instance().start()