gonzopi_menu.py (8155B)
1 import pydispmanx, time, pygame 2 import os 3 4 os.environ["DISPLAY"] = ":0" 5 pygame.display.init() 6 pygame.font.init() 7 8 print(pydispmanx.getDisplays()) 9 display_size=pydispmanx.getDisplaySize() 10 print(display_size) 11 print(str(round(pydispmanx.getFrameRate(),2))+"Hz") 12 print(pydispmanx.getPixelAspectRatio()) 13 14 display_width=display_size[0] 15 display_height=display_size[1] 16 17 print("display width:"+str(display_width)) 18 print("display height:"+str(display_height)) 19 20 menulayer = pydispmanx.dispmanxLayer(10); 21 vumenulayer = pydispmanx.dispmanxLayer(11); 22 print("Layer successfully created") 23 pygame_surface = pygame.image.frombuffer(menulayer, menulayer.size, 'RGBA') 24 pygame_surface2 = pygame.image.frombuffer(vumenulayer, vumenulayer.size, 'RGBA') 25 pygame_surface.set_alpha(150) 26 pygame_surface2.set_alpha(150) 27 WHITE = (255, 255, 255) 28 WHITEOPAC = (255, 255, 0) 29 OPAC = (255, 255, 255, 0) 30 BLACK = (0, 0, 0, 120) 31 YELLOW = (0, 0, 255) 32 RED = (255, 0, 0) 33 BLUE = (0, 0, 255) 34 GREEN = (255, 255, 0) 35 bakg=BLACK 36 linenr = 0; 37 selected = 0 38 showmenu = 0 39 header = 0 40 menuadd = 1 41 y_offset2 = 0 42 y_offset3 = 21 43 y_offset4 = 42 44 y_offset5 = 421 45 y_offset6 = 442 46 y_offset = 466 47 text_size = 15 48 text_size_selected = 16 49 space = 10 50 morespace = 12 51 rectime = 700 52 color = 3 53 row1 = 0 54 row2 = 0 55 row3 = 0 56 row4 = 0 57 row5 = 0 58 moverow = 0 59 oldmenu="" 60 oldvumeter="" 61 hidemenu=False 62 if display_width == 1920: 63 y_offset2 = 5 64 y_offset3 = 45 65 y_offset4 = 85 66 y_offset5 = 960 67 y_offset6 = 1000 68 y_offset = 1040 69 rectime = 1600 70 text_size = 30 71 text_size_selected = 32 72 space = 23 73 morespace = 27 74 if display_width == 800: 75 y_offset2 = 0 76 y_offset3 = 15 77 y_offset4 = 30 78 y_offset5 = 435 79 y_offset6 = 450 80 y_offset = 465 81 rectime = 700 82 text_size = 15 83 text_size_selected = 11 84 space = 10 85 morespace = 5 86 moverow = 0 87 88 fontObj = pygame.font.Font("/home/pi/gonzopi/gui/VeraMono.ttf", text_size,bold=True) 89 90 def render_menu(text, size, row, y_offset, color, bakg): 91 t = fontObj.render(text, True, color) 92 rect = t.get_rect().move(row,y_offset).inflate(0,-3) 93 #print(rect) 94 pygame.draw.rect(pygame_surface, bakg, rect) 95 #pygame_surface.set_alpha(55) 96 pygame_surface.blit(t, (row, y_offset)) 97 98 def render_vumenu(text, size, row, y_offset, color, bakg): 99 t = fontObj.render(text, True, color) 100 #rect = t.get_rect() 101 #pygame.draw.rect(pygame_surface, BLUE, rect) 102 #pygame_surface2.set_alpha(55) 103 pygame_surface2.blit(t, (row, y_offset)) 104 105 while True: 106 with open('/dev/shm/interface', 'r') as f: 107 if f: 108 menu = [line.rstrip() for line in f] 109 with open('/dev/shm/vumeter', 'r') as f: 110 if f: 111 vumeter = f.read() 112 if vumeter != oldvumeter: 113 pygame_surface2.fill((0,0,0,0)) 114 try: 115 s_vol1 = vumeter[85] 116 s_vol2 = vumeter[86] 117 vol=s_vol1+s_vol2 118 vol = int(vol) 119 if vol >= 0 and vol < 35: 120 color = WHITE 121 if vol >= 35 and vol < 99: 122 color = GREEN 123 if vol >= 99: 124 color = RED 125 except: 126 color=WHITE 127 bakg=BLACK 128 render_vumenu(vumeter, text_size, 0, y_offset, color, bakg) 129 oldvumeter = vumeter 130 vumenulayer.updateLayer() 131 if menu != oldmenu: 132 pygame_surface.fill((0,0,0,0)) 133 #print(menu) 134 #text1 = fontObj.render(menu[3], True, WHITE, BLUE) 135 #text2 = fontObj.render(menu[4], True, WHITE, BLUE) 136 #text3 = fontObj.render(menu[5], True, WHITE, BLUE) 137 #text4 = fontObj.render(menu[6], True, WHITE, BLUE) 138 #text5 = fontObj.render(menu[7], True, WHITE, BLUE) 139 #rect = text1.get_rect() 140 #pygame.draw.rect(pygame_surface, BLUE, rect) 141 142 linenr=0 143 color = WHITE 144 row1 = 0 145 row2 = 0 146 row3 = 0 147 row4 = 0 148 row5 = 0 149 150 for i in menu: 151 #print(i) 152 read = len(i) 153 if linenr == 0: 154 try: 155 selected = int(i.strip()) 156 except: 157 selected = 0 158 if linenr == 1: 159 try: 160 showmenu = int(i.strip()) 161 except: 162 showmenu = 1 163 if linenr == selected + 2 + menuadd: 164 color = BLACK ##selected color 165 bakg = WHITEOPAC 166 hidemenu=False 167 else: 168 if showmenu == 1: 169 color = WHITE ##unselected; 170 bakg=BLACK 171 hidemenu=False 172 if showmenu == 2: 173 color = BLUE 174 bakg=BLACK 175 if showmenu == 0: 176 color = OPAC 177 bakg=OPAC 178 hidemenu=True 179 if linenr == 2 and i == '': 180 header = 0 181 if linenr == 2 and i != '': 182 header = 1 183 if selected == 420: 184 if linenr == 1: 185 render_menu(i, text_size, moverow, y_offset2, WHITE, bakg) 186 if linenr > 1: 187 text1 = fontObj.render(i, True, WHITE, BLUE) 188 render_menu(i, text_size, row1, y_offset3, WHITE, bakg) 189 row1 += read * space + morespace 190 if header == 0 and selected < 420: 191 if linenr == 7+menuadd and i != '': ##show recording time if there is any 192 #render_subtitle(img, line, text_size, text_size_selected, rectime, y_offset2, 3); 193 color = RED 194 render_menu(i, text_size, rectime, y_offset2, color, bakg) 195 if linenr >= 2+menuadd and linenr <= 6+menuadd: 196 render_menu(i, text_size, row1, y_offset2, color, bakg) 197 #render_subtitle(img, line, text_size, text_size_selected, row1, y_offset2, color); 198 row1 += read * space + morespace 199 #print(str(row1)) 200 if linenr >= 8+menuadd and linenr <= 13+menuadd: 201 if hidemenu == False: 202 render_menu(i, text_size, row2, y_offset3, color, bakg) 203 #render_subtitle(img, line, text_size, text_size_selected, row2, y_offset3, color); 204 row2 += read * space + morespace 205 #print(str(row2)) 206 if linenr >= 14+menuadd and linenr <= 20+menuadd: 207 if hidemenu == False: 208 render_menu(i, text_size, row3, y_offset4, color, bakg) 209 #render_subtitle(img, line, text_size, text_size_selected, row3, y_offset4, color); 210 row3 += read * space + morespace 211 #print(str(row3)) 212 if linenr >= 21+menuadd and linenr <= 29+menuadd: 213 if hidemenu == False: 214 render_menu(i, text_size, row4, y_offset5, color, bakg) 215 #render_subtitle(img, line, text_size, text_size_selected, row4, y_offset5, color); 216 row4 += read * space + morespace 217 if linenr >= 30+menuadd and linenr <= 41+menuadd: 218 if hidemenu == False: 219 render_menu(i, text_size, row5, y_offset6, color, bakg) 220 #render_subtitle(img, line, text_size, text_size_selected, row5, y_offset6, color); 221 row5 += read * space + morespace; 222 if header == 1: 223 if linenr == 1+menuadd: 224 render_menu(i, text_size, 0, y_offset2, color, bakg) 225 #render_subtitl(img, line, text_size, text_size_selected, moverow, y_offset2, 5); 226 if linenr > 1+menuadd: 227 render_menu(i, text_size, row1, y_offset3, color, bakg) 228 #render_subtitle(img, line, text_size, text_size_selected, row1, y_offset3, color); 229 row1 += read * space + morespace; 230 linenr+=1 231 232 #render_subtitle(img, vumeter, text_size, text_size_selected , moverow, y_offset, vucolor); 233 menulayer.updateLayer() 234 oldmenu = menu 235 time.sleep(0.05)