-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
178 lines (151 loc) · 7.48 KB
/
main.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
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
import subprocess
import fnmatch,os
import re,time
from pyfirmata import *
from serial.tools.list_ports import comports
from math import sin
from kivy.garden.graph import Graph, MeshLinePlot
from components.notification import Notification
from time import strftime
from serial.tools.list_ports import comports
class ClockApp(App):
sw_started = False
sw_seconds = 0
file = ""
charge = "charge"
unit = "A"
current = "current"
status_hardware = False
board = None
def on_start(self):
self.battery_file()
#plot = MeshLinePlot(color=[1, 0, 0, 1])
#plot.points = [(x, sin(x / 10.)) for x in range(0, 101)]
#self.root.ids.my_graph.add_plot(plot)
Clock.schedule_interval(self.update, 0)
#Clock.schedule_interval(self.notify,10)
def update(self, nap):
try:
a = (float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/"+self.charge+"_full"]))/1000000)/(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/"+self.charge+"_full_design"]))/1000000)
a = a*100
b = 100.00-a
except Exception, e:
raise e
percentage = subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/capacity"]).strip()
"""
if self.board is not None:
if int(percentage) > 80:
self.board.digital[8].write(0)
if int(percentage) < 60:
print "fuck"
self.board.digital[8].write(1)
if int(percentage)<80 and int(percentage)>60:
print "fuck1"
self.board.digital[8].write(1)
"""
self.root.ids.status.text = subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/status"])
self.root.ids.battery_type.text = subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/technology"])
self.root.ids.battery_model.text = subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/model_name"])
self.root.ids.manufacturer.text = subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/manufacturer"])
self.root.ids.percentage.text = percentage+"%"
self.root.ids.current_now.text = str(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/"+self.current+"_now"]))/1000000).strip()+self.unit
self.root.ids.voltage_now.text = str(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/voltage_now"]))/1000000).strip()+"V"
self.root.ids.charge_now.text = str(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/voltage_now"]))/1000000).strip()+"Ah"
self.root.ids.design_capacity.text = str(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/"+self.charge+"_full_design"]))/1000000).strip()+self.unit
self.root.ids.last_full_capacity.text = str(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/"+self.charge+"_full"]))/1000000)+self.unit
self.root.ids.last_full_capacity_perc.text = str(a)
self.root.ids.capacity_loss_perc.text = str(b)
#self.root.ids.reset.text = "aoeu"
"""
if self.sw_started:
self.sw_seconds += nap
self.root.ids.time.text = strftime('[b]%H[/b]:%M:%S')
m, s = divmod(self.sw_seconds, 60)
self.root.ids.stopwatch.text = ('%02d:%02d.[size=40]%02d[/size]' %
(int(m), int(s), int(s * 100 % 100)))
"""
def start_stop(self):
self.root.ids.start_stop.text = 'Start' if self.sw_started else 'Stop'
self.sw_started = not self.sw_started
def notify(self, nap):
percentage = subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/capacity"]).strip()
if int(percentage) < 60:
Notification("Battery Status","Please connect the charger").notify()
print "test"
def battery_file(self):
for file in os.listdir('/sys/class/power_supply/'):
if fnmatch.fnmatch(file, 'BAT*'):
print file+" contains all the battery information"
#found += 1
self.file = file
try:
a = (float(subprocess.check_output(["cat", "/sys/class/power_supply/"+file+"/"+self.charge+"_full"]))/1000000)/(float(subprocess.check_output(["cat", "/sys/class/power_supply/"+self.file+"/"+self.charge+"_full_design"]))/1000000)
except Exception, e:
self.charge = "energy"
self.current = "energy"
self.unit = "Wh"
self.root.ids.text_last_full_capacity.text = "Last Full Energy"
self.root.ids.text_capacity_loss_perc.text = "Energy Loss %"
def temp(self):
self.root.ids.temp.text = subprocess.check_output(["echo", "Hello World!"])
def board(self):
boards_file = open('./boards.txt', 'r')
boards_lines = boards_file.readlines()
model = {}
for line in boards_lines:
match = re.search(r'(^[a-z]*)\.name=(.*)', line)
if match:
model[match.group(1)] = [match.group(2), [], []] # the two empty lists are the vid and pid lists
for line in boards_lines:
for k in model.keys():
match_vid = re.search(r'^' + k + r'\.vid\.[0-9]*=0x([a-fA-F0-9]{4})', line)
match_pid = re.search(r'^' + k + r'\.pid\.[0-9]*=0x([a-fA-F0-9]{4})', line)
if match_vid:
model[k][1].append(match_vid.group(1).upper())
elif match_pid:
model[k][2].append(match_pid.group(1).upper())
prods = {'0001':'Arduino Uno' , '0043':'Arduino Uno R3',
'0010':'Arduino Mega 2560', '0042':'Arduino Mega 2560 R3',
'003F':'Arduino Mega ADK' , '0044':'Arduino Mega ADK R3'}
#Arduino vendor ID: 0x2341
#Old Arduinos use the FTDI VID/PID (VID=0x0403)
VIDs = ['2341', '0403']
sdevs = comports()
intdev = []
for dev in sdevs:
if dev[2]!= 'n/a':
intdev.append(dev)
ptn = 'USB VID:PID=([A-F0-9]{4}):([A-F0-9]{4}) '
arduinos = []
for dev in intdev:
match = re.search(ptn,dev[2])
if match and match.group(1) == VIDs[0]:
if match.group(2) in prods:
data = (prods[match.group(2)], dev[0])
else:
data = ('Arduino', dev[0])
elif match and match.group(1) == VIDs[1]:
data = ('FTDI', dev[0])
else:
data = ('Unknown', dev[0])
arduinos.append(data)
if not arduinos:
self.root.ids.start_stop.text = 'No hardware connected !!!'
self.status_hardware = False
else:
print 'Connected Arduino device(s):'
for ard in arduinos:
self.root.ids.start_stop.text = str(ard[0])+' at ' +str(ard[1])
self.status_hardware = True
#self.board = Arduino(ard[1])
if __name__ == '__main__':
Window.clearcolor = get_color_from_hex('#101216')
LabelBase.register(name='Roboto',
fn_regular='Roboto-Thin.ttf',
fn_bold='Roboto-Medium.ttf')
ClockApp().run()