-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm_bar.py
138 lines (107 loc) · 2.72 KB
/
tm_bar.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
#!/usr/bin/env python3
import unicornhat as unicorn
import time
import sys,subprocess
import _thread
import os
# set max bandwidth in bytes per second
maxin = 5000000
maxout = 1000000
#set colors
colorIn = [0, 255, 0]
colorOut = [255, 0, 0]
colorError = [255, 255, 0]
# init unicorn hat
unicorn.set_layout(unicorn.AUTO)
unicorn.rotation(180)
unicorn.brightness(0.2)
# get size
width,height=unicorn.get_shape()
steps = 100 / (width + 1)
# target number of active LEDs
ledIn = 0
ledOut = 0
def getRate():
global ledIn, ledOut
while 1:
error = False
rate = ""
# get xml from router
try:
rate = str(subprocess.check_output([os.getcwd() + "/connection_rate.sh"]))
except subprocess.CalledProcessError:
error = True
if rate == "":
error = True
else:
sendrate = rate.split('NewByteSendRate', maxsplit=3)
receiverate = rate.split('NewByteReceiveRate', maxsplit=3)
if len(sendrate) != 3:
error = True
if len(receiverate) != 3:
error = True
if error:
ledIn = -1
ledOut = -1
else:
sendrate = sendrate[1][1:-2]
receiverate = receiverate[1][1:-2]
percin = 0
percout = 0
# calc percentage of max bandwidth used
if receiverate != 0:
percin = float(receiverate) / maxin * 100
if sendrate != 0:
percout = float(sendrate) / maxout * 100
# calc number of LEDs to light
ledIn = int( percin / steps)
ledOut = int( percout / steps)
def paint():
# number of active LEDs
curIn = 0
curOut = 0
while 1:
curInPre = curIn
curOutPre = curOut
if ledIn < 0:
for x in range(8):
for y in range(4):
unicorn.set_pixel(x, y, colorError[0], colorError[1], colorError[2])
unicorn.show()
else:
if curIn < ledIn:
curIn = curIn + 1
if curIn > ledIn:
curIn = curIn - 1
if curOut < ledOut:
curOut = curOut + 1
if curOut > ledOut:
curOut = curOut - 1
#print (str(curIn))
#skip painting if nothing changed
if curOutPre == curOut and curInPre == curIn:
continue
# turn all off
for x in range(8):
for y in range(4):
unicorn.set_pixel(x,y,0,0,0)
# paint input
for x in range(curIn):
unicorn.set_pixel(x, 0, colorIn[0], colorIn[1], colorIn[2])
unicorn.set_pixel(x, 1, colorIn[0], colorIn[1], colorIn[2])
# paint output
for x in range(curOut):
unicorn.set_pixel(x, 2, colorOut[0], colorOut[1], colorOut[2])
unicorn.set_pixel(x, 3, colorOut[0], colorOut[1], colorOut[2])
unicorn.show()
time.sleep(0.07)
#create unicorn tread
#create upnp query thread
try:
_thread.start_new_thread( getRate, () )
_thread.start_new_thread( paint, () )
# keep alive
while 1:
pass
except:
print ("Error: unable to start thread")