-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
executable file
·236 lines (189 loc) · 6.24 KB
/
misc.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3
import re
import os
import sys
import time
import mraa # pylint: disable=import-error
import shutil
import subprocess
import multiprocessing as mp
from collections import defaultdict, OrderedDict
from configparser import ConfigParser
# pin11(bcm17)
try:
pin11 = mraa.Gpio(11)
pin11.dir(mraa.DIR_OUT)
pin11.write(1)
except Exception as ex:
print(ex)
cmds = {
'blk': "lsblk | awk '{print $1}'",
'up': "echo Uptime: `uptime | sed 's/.*up \\([^,]*\\), .*/\\1/'`",
'temp': "cat /sys/class/thermal/thermal_zone0/temp",
'ip': "hostname -I | awk '{printf \"IP %s\", $1}'",
'cpu': "uptime | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'",
'men': "free -m | awk 'NR==2{printf \"Mem: %s/%sMB\", $3,$2}'",
'disk': "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
}
lv2dc = OrderedDict({'lv3': 0, 'lv2': 0.25, 'lv1': 0.5, 'lv0': 0.75})
# pin37(bcm26) sata0, pin22(bcm25) sata1
# GPIO.HIGH = 1
# GPIO.LOW = 0
def set_mode(pin, mode=1):
try:
pin = mraa.Gpio(pin)
pin.dir(mraa.DIR_OUT)
pin.write(mode)
except Exception as ex:
print(ex)
def disk_turn_on():
blk1 = get_blk()
set_mode(37, 1)
time.sleep(0.5)
set_mode(22, 1)
disksOn = wait_blk(20)
blk2 = get_blk()
conf['disk'] = sorted(list(set(blk2) - set(blk1)))
return disksOn
def disk_turn_off():
set_mode(37, 0)
time.sleep(0.5)
set_mode(22, 0)
def check_output(cmd):
return subprocess.check_output(cmd, shell=True).decode().strip()
def check_call(cmd):
return subprocess.check_call(cmd, shell=True)
def wait_blk(t1=10):
t = 0
while t <= t1:
try:
check_call('lsblk /dev/sda > /dev/null 2>&1')
check_call('lsblk /dev/sdb > /dev/null 2>&1')
check_call('lsblk /dev/sdc > /dev/null 2>&1')
check_call('lsblk /dev/sdd > /dev/null 2>&1')
except Exception as ex:
time.sleep(0.1)
t += 0.1
continue
else:
time.sleep(0.5)
return True
return False
def get_blk():
return check_output(cmds['blk']).strip().split('\n')
def get_info(s):
return check_output(cmds[s])
def get_cpu_temp():
t = float(get_info('temp')) / 1000
if conf['oled']['f-temp']:
temp = "CPU Temp: {:.0f}°F".format(t * 1.8 + 32)
else:
temp = "CPU Temp: {:.1f}°C".format(t)
return temp
def read_conf():
conf = defaultdict(dict)
try:
cfg = ConfigParser()
cfg.read('/etc/rockpi-sata.conf')
# fan
conf['fan']['lv0'] = cfg.getfloat('fan', 'lv0')
conf['fan']['lv1'] = cfg.getfloat('fan', 'lv1')
conf['fan']['lv2'] = cfg.getfloat('fan', 'lv2')
conf['fan']['lv3'] = cfg.getfloat('fan', 'lv3')
# key
conf['key']['click'] = cfg.get('key', 'click')
conf['key']['twice'] = cfg.get('key', 'twice')
conf['key']['press'] = cfg.get('key', 'press')
# time
conf['time']['twice'] = cfg.getfloat('time', 'twice')
conf['time']['press'] = cfg.getfloat('time', 'press')
# other
conf['slider']['auto'] = cfg.getboolean('slider', 'auto')
conf['slider']['time'] = cfg.getfloat('slider', 'time')
conf['oled']['rotate'] = cfg.getboolean('oled', 'rotate')
conf['oled']['f-temp'] = cfg.getboolean('oled', 'f-temp')
except Exception:
# fan
conf['fan']['lv0'] = 35
conf['fan']['lv1'] = 40
conf['fan']['lv2'] = 45
conf['fan']['lv3'] = 50
# key
conf['key']['click'] = 'slider'
conf['key']['twice'] = 'switch'
conf['key']['press'] = 'none'
# time
conf['time']['twice'] = 0.7 # second
conf['time']['press'] = 1.8
# other
conf['slider']['auto'] = True
conf['slider']['time'] = 10 # second
conf['oled']['rotate'] = False
conf['oled']['f-temp'] = False
return conf
#pin11(bcm17)
def read_key(pattern, size):
s = ''
pin11 = mraa.Gpio(11)
pin11.dir(mraa.DIR_IN)
while True:
s = s[-size:] + str(pin11.read())
for t, p in pattern.items():
if p.match(s):
return t
time.sleep(0.1)
def watch_key(q=None):
size = int(conf['time']['press'] * 10)
wait = int(conf['time']['twice'] * 10)
pattern = {
'click': re.compile(r'1+0+1{%d,}' % wait),
'twice': re.compile(r'1+0+1+0+1{3,}'),
'press': re.compile(r'1+0{%d,}' % size),
}
while True:
q.put(read_key(pattern, size))
def get_disk_info(cache={}):
if not cache.get('time') or time.time() - cache['time'] > 30:
info = {}
cmd = "df -h | awk '$NF==\"/\"{printf \"%s\", $5}'"
info['root'] = check_output(cmd)
for x in conf['disk']:
cmd = "df -Bg | awk '$1==\"/dev/{}\" {{printf \"%s\", $5}}'".format(x)
info[x] = check_output(cmd)
cache['info'] = list(zip(*info.items()))
cache['time'] = time.time()
return cache['info']
def slider_next(pages):
conf['idx'].value += 1
return pages[conf['idx'].value % len(pages)]
def slider_sleep():
time.sleep(conf['slider']['time'])
def fan_temp2dc(t):
for lv, dc in lv2dc.items():
if t >= conf['fan'][lv]:
return dc
return 0
def fan_switch():
conf['run'].value = not(conf['run'].value)
def get_func(key):
return conf['key'].get(key, 'none')
def open_pwm_i2c():
def replace(filename, raw_str, new_str):
with open(filename, 'r') as f:
content = f.read()
if raw_str in content:
shutil.move(filename, filename + '.bak')
content = content.replace(raw_str, new_str)
with open(filename, 'w') as f:
f.write(content)
replace('/boot/hw_intfc.conf', 'intfc:pwm0=off', 'intfc:pwm0=on')
replace('/boot/hw_intfc.conf', 'intfc:pwm1=off', 'intfc:pwm1=on')
replace('/boot/hw_intfc.conf', 'intfc:i2c7=off', 'intfc:i2c7=on')
os.system('/sbin/modprobe w1-gpio')
os.system('/sbin/modprobe w1-therm')
os.system('/sbin/modprobe i2c-dev')
conf = {'disk': [], 'idx': mp.Value('d', -1), 'run': mp.Value('d', 1)}
conf.update(read_conf())
if __name__ == '__main__':
if sys.argv[-1] == 'open_pwm_i2c':
open_pwm_i2c()