-
Notifications
You must be signed in to change notification settings - Fork 4
/
pipboyUI.py
350 lines (271 loc) · 10.2 KB
/
pipboyUI.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from pyky040 import pyky040
import threading
import time
import pygame
import RPi.GPIO as GPIO
import sys
import queue
import os
import noise
from gauge import moveGauge
from greenFilter import remap
from menuItem import MenuItem
from pipboyScreen import PipboyScreen
# Setup the GPIO mode
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
screenX=480
screenY=320
halfWidth=int(screenX/2)
dR=60
dG=243
dB=170
dColor = (dR,dG,dB)
screensPath="UIScreens"
#TODO you can get fontsize from get_linesize() so I dont need to pass it everywhere, come clean this up
pygame.font.init()
menuFontSize=20
menuFont = pygame.font.SysFont('Courier New', menuFontSize)
bodyFontSize=16
bodyFont = pygame.font.SysFont('Courier New', bodyFontSize)
screenNames = ["STAT","INV", "MAP", "DATA", "RADIO"]
menu = []
menuItem = 0
pipboyScreen = []
# draw the entire menu and high light the selected item
def drawMenu(s,f,menu,hlItem):
global menuFontSize
yoffset=0
xoffset=0
#miHeight=menuFontSize+10
miWidth=0
selectedBarH=menuFontSize*0.3
selectedBarW=3
for i in range(0,len(menu)):
miWidth=menu[i].drawItem(s,xoffset,yoffset)
if (i==hlItem):
pygame.draw.line(s,dColor,(xoffset,menuFontSize),(xoffset,selectedBarH),1)
pygame.draw.line(s,dColor,(xoffset,selectedBarH),(xoffset+selectedBarW,selectedBarH),1)
pygame.draw.line(s,dColor,(xoffset+miWidth,menuFontSize),(xoffset+miWidth,selectedBarH),1)
pygame.draw.line(s,dColor,(xoffset+miWidth,selectedBarH),(xoffset+miWidth-selectedBarW,selectedBarH),1)
else:
pygame.draw.line(s,dColor,(xoffset,menuFontSize),(xoffset+miWidth,menuFontSize),1)
xoffset+=miWidth
pygame.draw.line(s,dColor,(xoffset,menuFontSize),(480,menuFontSize),1)
# render the text for a status bar
def drawStatusBar(s,f, status):
global menuFontSize
textSurface = f.render(status, False, dColor)
s.blit(textSurface,(0,screenY-menuFontSize))
# END of menu setup section
# START of LED Gauge setup section
pin_green = 16
pin_yellow = 20
pin_red = 21
GPIO.setup(pin_green,GPIO.OUT)
GPIO.setup(pin_yellow,GPIO.OUT)
GPIO.setup(pin_red,GPIO.OUT)
GPIO.output(pin_green,GPIO.LOW)
GPIO.output(pin_yellow,GPIO.LOW)
GPIO.output(pin_red,GPIO.LOW)
# The following vars relate to the 3-led gauge state and update interval
gState = 0
gTicks = 0
gTimeDelta = 0
# END of LED Gauge setup section
# START of button setup section
pin_BTN1 = 12
pin_BTN2 = 5
def buttonPressed(channel):
print("Button {} was pressed".format(channel))
if channel==12:
global mainLoop
mainLoop=False
if channel==5:
global flipDisplayMode
flipDisplayMode=True
GPIO.setup(pin_BTN1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin_BTN1,GPIO.RISING,callback=buttonPressed, bouncetime=500)
GPIO.setup(pin_BTN2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin_BTN2,GPIO.RISING,callback=buttonPressed, bouncetime=500)
# END of button setup section
# START OF Encoder setup section
# Default rotary position
rotaryPosition=0
q = queue.Queue()
# Call back functions for encoder
def encoderClicked():
print("encoder clicked")
def turned(scale_position):
global q
print("turned to {}".format(scale_position))
q.put(scale_position)
# Pins used by the encoder
pin_sw = 19
pin_dt = 13
pin_clk = 6
pin_3v3 = 26
# Define pin initial state
GPIO.setup(pin_3v3,GPIO.OUT)
GPIO.output(pin_3v3,GPIO.HIGH)
# Setup the perlin noise parameters
scale = 100.0
octaves = 4
persistence = 0.5
lacunarity = 2.0
ybaseline=screenY/2
# END of perlin noise parameters
# Init the display and setup for gfx
pygame.init()
#screen = pygame.display.set_mode((screenX,screenY), pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((screenX,screenY),pygame.RESIZABLE)
flipDisplayMode=False
fullScreenMode=False
# If the MAP doesnt exist, then there may not have been a cache built
# so we go ahead and create a filtered version to cache for display
if os.path.exists(os.path.join(screensPath,'MAP.png')) == False:
print("Map cache missing - building")
tempImage = pygame.image.load(os.path.join(screensPath,'rawMap.png')).convert()
remap(tempImage)
screen.blit(tempImage,(0,0))
pygame.display.update()
pygame.image.save(tempImage,os.path.join(screensPath,"MAP.png"))
print("Building UI...")
for item in screenNames:
menu.append(MenuItem(menuFont,item,dColor))
pipboyScreen.append(PipboyScreen(bodyFont,item,dColor))
for item in pipboyScreen:
print(item)
item.loadScreenUsingTextFile(screensPath)
item.loadScreenUsingGfxFile(screensPath)
print("... UI Build complete")
# Create the encoder
encoder = pyky040.Encoder(CLK=pin_clk, DT=pin_dt, SW=pin_sw)
# Setup and launch the thread to monitor the encoder
encoder.setup(scale_min=0,scale_max=len(menu)-1, loop=False, step=1, sw_debounce_time=100, chg_callback=turned, sw_callback=encoderClicked)
encoderThread = threading.Thread(target=encoder.watch)
encoderThread.daemon=True;
encoderThread.start()
# the following vars all relate to the rotary encoder debug status bar
timeStamp_ms = int(time.monotonic_ns()/1000);
firstTimestamp = True
timeDelta = 0
rollingAverage = 0
# END of encoder setup section
print("watching...")
# Main control vars for the main loop and screen
mainLoop = True
updateScreen = True
perlinSeed=0
while mainLoop:
time.sleep(0.01)
perlinSeed=perlinSeed+1
if perlinSeed>screenX:
perlinSeed=0
if (flipDisplayMode==True):
#screen = pygame.display.set_mode((480,320),pygame.RESIZABLE)
print("Flip display called")
if (fullScreenMode==False):
screen = pygame.display.set_mode((screenX,screenY), pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF)
fullScreenMode=True
updateScreen=True
else:
screen = pygame.display.set_mode((480,320),pygame.RESIZABLE)
fullScreenMode=False
updateScreen=True
flipDisplayMode=False
# Check for any pygame events, and react if any
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_SPACE:
mainLoop = False
print("escape or space pressed")
if event.key == pygame.K_0:
q.put(0)
if event.key == pygame.K_1:
q.put(1)
if event.key == pygame.K_2:
q.put(2)
if event.key == pygame.K_3:
q.put(3)
if event.key == pygame.K_4:
q.put(4)
# Deal with tracking timing for the game
gTicks+=1
if (gTicks==50):
gTicks=0
gState=moveGauge(gState)
# Turn off all the lights.
GPIO.output(pin_green,GPIO.LOW)
GPIO.output(pin_yellow,GPIO.LOW)
GPIO.output(pin_red,GPIO.LOW)
if gState == 3:
GPIO.output(pin_red,GPIO.HIGH)
GPIO.output(pin_yellow,GPIO.HIGH)
GPIO.output(pin_green,GPIO.HIGH)
elif gState == 2:
GPIO.output(pin_yellow,GPIO.HIGH)
GPIO.output(pin_green,GPIO.HIGH)
elif gState == 1:
GPIO.output(pin_green,GPIO.HIGH)
newTimeStamp=int(time.monotonic_ns()/1000);
timeDelta = newTimeStamp - timeStamp_ms;
if (firstTimestamp==True):
rollingAverage=timeDelta
firstTimestamp=False
else:
rollingAverage = (rollingAverage + timeDelta)/2
statusBar="T:{:d} A:{:f} S:{:f}".format(timeDelta,rollingAverage,rollingAverage/1000000)
timeStamp_ms=newTimeStamp
# End of time tracking section
if not q.empty():
# if the rotary dial has been turned then lets update the menu
rotaryPosition=q.get()
menuItem=rotaryPosition;
updateScreen = True
else:
# certain menu items that have an animation we always update
if menuItem==4:
updateScreen = True
if updateScreen == True:
# clear the screen
screen.fill((0,0,0))
# redraw the menu and update the game engine
#screenNames = ["STAT","INV", "MAP", "DATA", "RADIO"]
drawMenu(screen,menuFont,menu,menuItem)
if menuItem==0:
#249x216
xStatsOffset = (screenX - 249)/2
yStatsOffset = ((screenY - 216)/2)-20
pipboyScreen[menuItem].drawScreen(screen,bodyFont,xStatsOffset,yStatsOffset,menuFontSize)
if menuItem==1:
pipboyScreen[menuItem].drawScreen(screen,bodyFont,5,0,menuFontSize)
if menuItem==2:
pipboyScreen[menuItem].drawScreen(screen,bodyFont,112,0,menuFontSize)
if menuItem==3:
pipboyScreen[menuItem].drawScreen(screen,bodyFont,0,0,menuFontSize)
if menuItem==4:
pipboyScreen[menuItem].drawScreen(screen,bodyFont,0,0,menuFontSize)
# draw radio frequencies
for i in range (halfWidth,screenX,3):
n = noise.pnoise2(perlinSeed/scale,i/scale,octaves=octaves,persistence=persistence,lacunarity=lacunarity,repeatx=screenX,repeaty=screenX,base=0)
offset=int(n*scale)
pygame.draw.line(screen,dColor,(i,ybaseline+offset-1),(i,ybaseline+offset),1)
#pygame.draw.line(screen,dColor,(i,ybaseline/2),(i,(ybaseline/2)+offset),1)
#n = noise.pnoise2(i/scale,perlinSeed/scale,octaves=octaves,persistence=persistence,lacunarity=lacunarity,repeatx=512,repeaty=512,base=0)
offset=int(n*scale)
#pygame.draw.line(screen,(dR/2,dG/2,dB/2),(i,ybaseline+offset-1),(i,ybaseline+offset),1)
#pygame.draw.line(screen,(dR/2,dG/2,dB/2),(i,ybaseline+(ybaseline/2)),(i,ybaseline+(ybaseline/2)+offset),1)
pygame.draw.line(screen,dColor,((perlinSeed%halfWidth)+halfWidth,bodyFontSize*2),((perlinSeed%halfWidth)+halfWidth,screenY),1)
drawStatusBar(screen,bodyFont,statusBar)
pygame.display.update()
# This means that we only update when strictly necessary
# this will need to be relaxed if we want to do screen effects
# later
updateScreen = False
pygame.quit()
sys.exit()
exit()