forked from JackD83/PyMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrightnessVolumeControl.py
169 lines (110 loc) · 5.18 KB
/
BrightnessVolumeControl.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
import pygame, sys, Common, TaskHandler, Configuration
import os,subprocess, RenderObject, Keys,RenderControl
from threading import Thread
class BrightnessVolume(RenderObject.RenderObject):
config = Configuration.getConfiguration()
textFont = pygame.font.Font('theme/NotoSansSymbols-Regular.ttf', 19)
brightSymbol = textFont.render(u"\u26ef", True, (255,255,255))
volSymbol = Common.loadCachedImage("theme/speaker.png")
BRIGHTNESS_LEVELS = [10,30,50,70,100]
brightnessIndex = 0
showBrightness = False
showVolume = False
menuAlpha = 255
currentVolumeLevel = 0
width = 200
height = 30
animationId = None
def render(self, screen):
if(self.showBrightness or self.showVolume):
overlay = pygame.Surface((self.width + 4, self.height + 4),pygame.SRCALPHA)
backgroundShadow = pygame.Surface(((self.width,self.height)),pygame.SRCALPHA)
backgroundShadow.fill((0,0,0, 120))
overlay.blit(backgroundShadow, (4,4))
background = pygame.Surface((self.width,self.height),pygame.SRCALPHA)
background.fill((137,137,137, 255))
if(self.showBrightness):
barWidth = (self.width - 40) / len(self.BRIGHTNESS_LEVELS) * self.brightnessIndex
if(self.brightnessIndex == 0):
barWidth = 5
else:
barWidth = (self.width - 80) * self.currentVolumeLevel / 100
if(self.currentVolumeLevel < 0):
barWidth = 1
bar = pygame.Surface((barWidth,10))
bar.fill((255,255,255, 255))
x = (self.config["screenWidth"] - self.width + 2) / 2
background.blit(bar, (50,10))
if(self.showBrightness):
background.blit(self.brightSymbol, (12,-5))
else:
background.blit(self.volSymbol, (15,11))
overlay.blit(background, (0,0))
overlayCopy = overlay.convert_alpha().copy()
overlayCopy.fill((255, 255, 255, self.menuAlpha), None, pygame.BLEND_RGBA_MULT)
screen.blit(overlayCopy, (x,20))
def animationCallback(self, start, target, current, finished):
self.menuAlpha = current
if(finished):
self.showBrightness = False
self.showVolume = False
RenderControl.setDirty()
def handleEvents(self, events):
for event in events:
if event.type == pygame.KEYUP:
if event.key == Keys.DINGOO_BUTTON_BRIGHTNESS:
self.adjustBrightness()
if event.key == Keys.DINGOO_BUTTON_VOL_DOWN:
self.volumeDown()
if event.key == Keys.DINGOO_BUTTON_VOL_UP:
self.volumeUp()
def volumeUp(self):
self.currentVolumeLevel = self.getCurrentVolume()
self.showVolume = True
self.showBrightness = False
self.resetAnimation()
def volumeDown(self):
self.currentVolumeLevel = self.getCurrentVolume()
self.showVolume = True
self.showBrightness = False
self.resetAnimation()
def resetAnimation(self):
self.menuAlpha = 255
if(self.animationId is not None):
TaskHandler.stopAnimation(self.animationId)
self.animationId = TaskHandler.addAnimation(255, 20, 600, self.animationCallback, 1500)
RenderControl.setDirty()
def adjustBrightness(self):
self.brightnessIndex = self.brightnessIndex + 1
if(self.brightnessIndex >= len(self.BRIGHTNESS_LEVELS)):
self.brightnessIndex = 0
level = self.BRIGHTNESS_LEVELS[self.brightnessIndex]
thread = Thread(target = self.setBrightness, args = (level,))
thread.start()
self.showBrightness = True
self.showVolume = False
self.resetAnimation()
def setBrightness(self, level, store=True):
print("Setting brightness " + str(level))
os.system('echo ' + str(level) + ' > /proc/jz/lcd_backlight')
if(store):
self.config["lcd_backlight"] = level
Configuration.saveConfiguration()
def getCurrentVolume(self):
try:
with open("config/volume.cfg") as f:
vol = f.readlines()
return int(float(str(vol[0])))
except Exception as ex:
print("Could not get volume: " + str(ex))
return 0;
def initVolume(self):
print("Setting initial volume: " + str(self.getCurrentVolume()))
os.system('./setVolume ' + str(self.getCurrentVolume()))
def __init__(self):
if("lcd_backlight" in self.config and self.config["lcd_backlight"] in self.BRIGHTNESS_LEVELS):
self.brightnessIndex = self.BRIGHTNESS_LEVELS.index(self.config["lcd_backlight"])
self.setBrightness(self.config["lcd_backlight"], False)
else:
self.setBrightness(30)
self.initVolume()