forked from vwillcox/speedtest-cron
/
buttons.py
executable file
·185 lines (164 loc) · 5.57 KB
/
buttons.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
#!/usr/bin/python
###########################################################
# Network Testing Appliance #
###########################################################
# Originally by: Vincent Willcox #
# http://talktech.info #
###########################################################
# Version 1 - 25/10/2015 #
###########################################################
from gpiozero import RGBLED, Button
import smbus
import time
import sys
import os
import subprocess
from PIL import Image
from PIL import ImageOps
import ImageDraw
import ImageFont
import ipgetter
import socket
import subprocess
import shlex
from EPD import EPD
led = RGBLED(red=6, green=12, blue=5)
button1 = Button(16)
button2 = Button(19)
button3 = Button(20)
button4 = Button(26)
if os.name != "nt":
import fcntl
import struct
def get_interface_ip(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s',
ifname[:15]))[20:24])
def get_lan_ip():
ip = socket.gethostbyname(socket.gethostname())
if ip.startswith("127.") and os.name != "nt":
interfaces = [
"eth0",
"eth1",
"eth2",
"wlan0",
"wlan1",
"wifi0",
"ath0",
"ath1",
"ppp0",
]
for ifname in interfaces:
try:
ip = get_interface_ip(ifname)
break
except IOError:
pass
return ip
def remove_rtc_module():
exitCode = subprocess.call(["sudo", "rmmod", "rtc_ds3232"])
if exitCode != 0:
print ("Unable to remove RTC module, halting program");
sys.exit()
else:
print ("RTC module removed");
return;
def restore_rtc_module():
exitCode = subprocess.call(["sudo", "modprobe", "rtc_ds3232"])
if exitCode != 0:
print ("Unable to restore RTC module, halting program")
sys.exit()
else:
print ("RTC module restored");
date = subprocess.check_output(["sudo", "hwclock", "-r"]);
print (date);
return;
def main():
epd = EPD()
try:
while True: # loop forever
# Check if the first button is pressed and reload the screen
if button3.is_active:
display_running(epd)
subprocess.call(shlex.split('/home/pi/speedtest-cron/ImageDemo.py temp.png'))
if button2.is_active:
display_running(epd)
# ToDo: Run this in pure python - for now another shell script is used
subprocess.call(shlex.split('/home/pi/speedtest-cron/speedtest.sh'))
display_eedata(epd)
except KeyboardInterrupt:
print ("Exiting Appliance Code")
# Code to simple refresh the screen
def display_eedata(epd):
w = epd.width
h = epd.height
myip = ipgetter.myip()
# Uncomment this line to display the true external IP
#ext_ip = 'External: ' + myip
# Comment this line to remove the fake external IP
ext_ip = 'External: 255.255.255.255'
int_ip = get_lan_ip()
int_ip = 'Internal: ' + int_ip
#int_ip = int_ip.rstrip(5)
f = open('/home/pi/speedtest-cron/speedtest.txt')
lines = f.readlines()
f.close()
speedline1 = lines[-5].strip()
speedline2 = lines[-4].strip()
speedline3 = lines[-3].strip()
speedline4 = lines[-2].strip()
# initially set all white background
image = Image.new('1', epd.size, WHITE)
# prepare for drawing
draw = ImageDraw.Draw(image)
draw.rectangle((1, 1, w - 1, h - 1), fill=WHITE, outline=BLACK)
draw.rectangle((2, 2, w - 2, h - 2), fill=WHITE, outline=BLACK)
# text
draw.text((5,5), speedline1, fill=BLACK, font=text_font1)
draw.text((10,35), speedline2, fill=BLACK, font=text_font2)
draw.text((10,65), speedline3, fill=BLACK, font=text_font2)
draw.text((10,95), speedline4, fill=BLACK, font=text_font2)
draw.text((10,125), ext_ip, fill=BLACK, font=text_font2)
draw.text((10,155), int_ip, fill=BLACK, font=text_font2)
# display image on the panel
epd.display(image)
epd.update()
# Code to show that a new speed test is running.
def display_running(epd):
w = epd.width
h = epd.height
# initially set all white background
image = Image.new('1', epd.size, WHITE)
# prepare for drawing
draw = ImageDraw.Draw(image)
draw.rectangle((1, 1, w - 1, h - 1), fill=WHITE, outline=BLACK)
draw.rectangle((2, 2, w - 2, h - 2), fill=WHITE, outline=BLACK)
# text
draw.text((10,35), 'Speed Test Running', fill=BLACK, font=text_font2)
draw.text((10,65), 'Please Wait.......', fill=BLACK, font=text_font2)
# display image on the panel
epd.display(image)
epd.update()
#main
if "__main__" == __name__:
#bus = smbus.SMBus(1)
#rtc = 0x68
WHITE = 1
BLACK = 0
possible_fonts = [
'/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', # R.Pi
'/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono-Bold.ttf', # R.Pi
'/usr/share/fonts/truetype/freefont/FreeMono.ttf', # R.Pi
]
FONT_FILE = ''
for f in possible_fonts:
if os.path.exists(f):
FONT_FILE = f
break
if '' == FONT_FILE:
raise 'no font file found'
TEXT_FONT_SIZE1 = 11
TEXT_FONT_SIZE2 = 17
text_font1 = ImageFont.truetype(FONT_FILE, TEXT_FONT_SIZE1)
text_font2 = ImageFont.truetype(FONT_FILE, TEXT_FONT_SIZE2)
main()