-
Notifications
You must be signed in to change notification settings - Fork 9
/
Header.py
119 lines (78 loc) · 3.72 KB
/
Header.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
import RenderObject, Configuration, SelectionMenu, FileChooser, Runner, TaskHandler
import os, Common, RenderControl
import pygame, sys
class Header():
config = Configuration.getConfiguration()
theme = Configuration.getTheme()
headerHeight = 24
battery = None
batteryLevel = 0
currentBatteryImage = None
usbDevice = None
def render(self, screen):
screen.blit(self.header, (0,0))
def updateHeader(self):
self.header = pygame.Surface((self.config["screenWidth"], self.headerHeight),pygame.SRCALPHA)
#self.header.fill(Configuration.toColor(self.theme["header"]["color"]))
self.currentBatteryImage = self.getCurrentBatteryImage()
battery = Common.loadCachedImage(self.currentBatteryImage)
self.header.blit(battery, (self.config["screenWidth"] - 40, (self.headerHeight - battery.get_height()) / 2))
def getCurrentBatteryImage(self):
if(self.isUsbConnected()):
return "theme/battery_charging.png"
if(self.batteryLevel == 0):
return "theme/battery_0.png"
if(self.batteryLevel >= 0 and self.batteryLevel < 20):
return "theme/battery_1.png"
if(self.batteryLevel >= 20 and self.batteryLevel < 60):
return "theme/battery_2.png"
if(self.batteryLevel >= 60):
return "theme/battery_3.png"
def isUsbConnected(self):
if(self.usbDevice == None):
return False
try:
self.usbDevice.seek(0)
first_line = self.usbDevice.readline().strip()
if(first_line == "usb" or first_line == "1"):
return True
except Exception as ex:
print(str(ex))
return False
def updateBattery(self):
self.readBatteryLevel()
if(self.currentBatteryImage != self.getCurrentBatteryImage()):
self.updateHeader()
RenderControl.setDirty()
def readBatteryLevel(self):
if(self.battery == None):
return
try:
self.battery.seek(0)
first_line = self.battery.readline()
batteryLevelMVolt = int(first_line)
if (not "batteryLow" in self.config):
self.config["batteryLow"] = 3900
self.config["batteryHigh"] = 4500
if(batteryLevelMVolt < self.config["batteryLow"]):
self.config["batteryLow"] = batteryLevelMVolt
if(batteryLevelMVolt > self.config["batteryHigh"] and batteryLevelMVolt < 10000 ):
self.config["batteryHigh"] = batteryLevelMVolt
self.batteryLevel = float( (int(batteryLevelMVolt) - int(self.config["batteryLow"]) ) ) / float( ( int(self.config["batteryHigh"]) - int(self.config["batteryLow"]) ) ) * 100.0
except Exception as ex:
print(str(ex))
def __init__(self, height):
self.headerHeight = height
self.updateHeader()
try:
if(not Configuration.isOpenDinguX()):
self.usbDevice = open("/sys/devices/platform/musb_hdrc.0/uh_cable", "r")
if(Configuration.isOpenDinguX()):
self.battery = open("/sys/class/power_supply/battery/voltage_now", "r")
self.usbDevice = open("/sys/devices/platform/gpio-charger.1/power_supply/usb/online", "r")
else:
self.battery = open("/proc/jz/battery", "r")
self.updateBattery()
except Exception as ex:
print("Could not open devices" + str(ex))
TaskHandler.addPeriodicTask(1000, self.updateBattery)