-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackpod.py
263 lines (220 loc) · 7.02 KB
/
hackpod.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import tkinter as tk
from sys import platform
import os
from config import *
from tools import *
import sys_manager
import socket
from select import select
import time
from apps.settings.hp_settings import SettingsPage
from apps.settings.subpages.settings_wifi import WifiPage, WifiFrame
from apps.shared.hp_menupage import Rendering, MenuPage
from apps.crypto.hp_crypto import EthPricePage, EthPriceFrame, EthPriceCommand
from apps.homepage.hp_homepage import StartFrame
class tkinterApp(tk.Tk):
# __init__ function for class tkinterApp
def __init__(self, *args, **kwargs):
# __init__ function for class Tk
tk.Tk.__init__(self, *args, **kwargs)
self.geometry(DIMENSIONS)
self.configure(bg=SPOT_BLACK)
# self.attributes('-fullscreen', True)
self.geometry("320x240")
self.bind('<Escape>', lambda e: self.quit())
# creating a container
container = tk.Frame(self)
container.pack(fill = "both", expand = True)
container.grid_rowconfigure(1, weight = 2)
container.grid_columnconfigure(0, weight = 2)
# initializing frames to an empty array
self.frames = {}
# iterating through a tuple consisting
# of the different page layouts
for F in (StartFrame, EthPriceFrame, WifiFrame):
frame = F(container, self)
# initializing frame of that object from
# startpage, page1, page2 respectively with
# for loop
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky ="nsew")
# self.show_frame(StartFrame)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# Scrollwheel stuff
def processInput(app, input, page):
global wheel_position, last_button, last_interaction
position = input[2]
button = input[0]
button_state = input[1]
if button == 29 and button_state == 0:
wheel_position = -1
elif wheel_position == -1:
wheel_position = position
elif position % 2 != 0:
pass
elif wheel_position <=1 and position > 44:
onDownPressed()
wheel_position = position
elif wheel_position >=44 and position < 1:
onUpPressed()
wheel_position = position
elif abs(wheel_position - position) > 6:
wheel_position = -1
elif wheel_position > position:
onDownPressed()
wheel_position = position
elif wheel_position < position:
onUpPressed()
wheel_position = position
if button_state == 0:
last_button = -1
elif button == last_button:
pass
elif button == 7:
onSelectPressed()
last_button = button
elif button == 11:
onBackPressed()
last_button = button
elif button == 10:
onPlayPressed()
last_button = button
elif button == 8:
onNextPressed()
last_button = button
elif button == 9:
onPrevPressed()
last_button = button
now = time.time()
if (now - last_interaction > SCREEN_TIMEOUT_SECONDS):
print("waking")
sys_manager.screen_wake()
last_interaction = now
def onKeyPress(event):
c = event.keycode
if (c == UP_KEY_CODE):
onUpPressed()
elif (c == DOWN_KEY_CODE):
onDownPressed()
elif (c == RIGHT_KEY_CODE):
onSelectPressed()
elif (c == LEFT_KEY_CODE):
onBackPressed()
elif (c == NEXT_KEY_CODE):
onNextPressed()
elif (c == PREV_KEY_CODE):
onPrevPressed()
elif (c == PLAY_KEY_CODE):
onPlayPressed()
else:
print("unrecognized key: ", c)
def onPlayPressed():
global page, app
page.nav_play()
render(app, page.render())
def onSelectPressed():
global page, app
if (not page.has_sub_page):
return
page.render().unsubscribe()
page = page.nav_select()
render(app, page.render())
def onBackPressed():
global page, app
previous_page = page.nav_back()
if (previous_page):
page.render().unsubscribe()
page = previous_page
render(app, page.render())
def onNextPressed():
global page, app
page.nav_next()
render(app, page.render())
def onPrevPressed():
global page, app
page.nav_prev()
render(app, page.render())
def onUpPressed():
global page, app
page.nav_up()
render(app, page.render())
def onDownPressed():
global page, app
page.nav_down()
render(app, page.render())
class RootPage(MenuPage):
def __init__(self, previous_page):
super().__init__("hackP0dR00t", previous_page, has_sub_page=True)
self.pages = [
EthPricePage(self, "ETH Price", EthPriceCommand()),
SettingsPage(self)
]
self.index = 0
self.page_start = 0
def get_pages(self):
return self.pages
def total_size(self):
return len(self.get_pages())
def page_at(self, index):
return self.get_pages()[index]
def render_menu(app, menu_render):
app.show_frame(StartFrame)
page = app.frames[StartFrame]
if(menu_render.total_count > MENU_PAGE_SIZE):
page.show_scroll(menu_render.page_start, menu_render.total_count)
else:
page.hide_scroll()
for (i, line) in enumerate(menu_render.lines):
page.set_list_item(i, text=line.title, line_type = line.line_type, show_arrow = line.show_arrow)
page.set_header(menu_render.header, menu_render.now_playing, menu_render.has_internet)
def update_eth_price(eth_price):
frame = app.frames[EthPriceFrame]
frame.update_eth_price(eth_price)
def render_eth_price(app, eth_price_render):
app.show_frame(EthPriceFrame)
eth_price_render.subscribe(app, update_eth_price)
def update_wifi():
frame = app.frames[WifiPage]
frame.update_wifi()
def render_wifi(app, wifi_render):
app.show_frame(WifiFrame)
wifi_render.subscribe(app, update_wifi)
def render(app, render):
if (render.type == MENU_RENDER_TYPE):
render_menu(app, render)
elif (render.type == ETH_PRICE_RENDER):
render_eth_price(app, render)
elif (render.type == WIFI_RENDER):
render_wifi(app, render)
# Driver Code
app = tkinterApp()
app.overrideredirect(False)
app.configure(bg=SPOT_BLACK)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
sock.setblocking(0)
socket_list = [sock]
loop_count = 0
page = RootPage(None)
def app_main_loop():
global app, page, loop_count, last_interaction, screen_on
try:
read_sockets = select(socket_list, [], [], 0)[0]
for socket in read_sockets:
data = socket.recv(128)
processInput(app, data, page)
loop_count += 1
if (loop_count >= 300):
# print('start render')
render(app, page.render())
# loop_count = 0
except:
pass
finally:
app.after(2, app_main_loop)
app.bind('<KeyPress>', onKeyPress)
app.after(5, app_main_loop)
app.mainloop()