-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipdisplay.py
213 lines (182 loc) · 7.02 KB
/
ipdisplay.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
import os
import pygame
import time
import random
import socket
import fcntl
import struct
import fnmatch
from pygame.locals import *
os.environ["SDL_FBDEV"] = "/dev/fb1"
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
global ethdisp
class Icon:
def __init__(self, name):
self.name = name
try:
self.bitmap = pygame.image.load(iconPath + '/' + name + '.png')
except:
pass
class Button:
def __init__(self, rect, **kwargs):
self.rect = rect # Bounds
self.color = None # Background fill color, if any
self.iconBg = None # Background Icon (atop color fill)
self.iconFg = None # Foreground Icon (atop background)
self.bg = None # Background Icon name
self.fg = None # Foreground Icon name
self.callback = None # Callback function
self.value = None # Value passed to callback
for key, value in kwargs.iteritems():
if key == 'color': self.color = value
elif key == 'bg' : self.bg = value
elif key == 'fg' : self.fg = value
elif key == 'cb' : self.callback = value
elif key == 'value': self.value = value
def selected(self, pos):
x1 = self.rect[0]
y1 = self.rect[1]
x2 = x1 + self.rect[2] - 1
y2 = y1 + self.rect[3] - 1
if ((pos[0] >= x1) and (pos[0] <= x2) and
(pos[1] >= y1) and (pos[1] <= y2)):
if self.callback:
if self.value is None: self.callback()
else: self.callback(self.value)
return True
return False
def draw(self, screen):
if self.color:
screen.fill(self.color, self.rect)
if self.iconBg:
screen.blit(self.iconBg.bitmap,
(self.rect[0]+(self.rect[2]-self.iconBg.bitmap.get_width())/2,
self.rect[1]+(self.rect[3]-self.iconBg.bitmap.get_height())/2))
if self.iconFg:
screen.blit(self.iconFg.bitmap,
(self.rect[0]+(self.rect[2]-self.iconFg.bitmap.get_width())/2,
self.rect[1]+(self.rect[3]-self.iconFg.bitmap.get_height())/2))
def setBg(self, name):
if name is None:
self.iconBg = None
else:
for i in icons:
if name == i.name:
self.iconBg = i
break
# Global stuff -------------------------------------------------------------
screenMode = 3 # Current screen mode; default = viewfinder
iconPath = 'icons' # Subdirectory containing UI bitmaps (PNG format)
icons = [] # This list gets populated at startup
buttons = [
# Screen mode 0 is photo playback
[Button(( 0,188,320, 52), bg='done'),# , cb=doneCallback),
Button(( 0, 0, 80, 52), bg='prev'),# , cb=imageCallback, value=-1),
Button((240, 0, 80, 52), bg='next'),# , cb=imageCallback, value= 1),
Button(( 88, 70,157,102)), # 'Working' label (when enabled)
Button((148,129, 22, 22)), # Spinner (when enabled)
Button((121, 0, 78, 52), bg='trash',)],# cb=imageCallback, value= 0)],
# Screen mode 1 is delete confirmation
[Button(( 0,35,320, 33), bg='delete'),
Button(( 32,86,120,100), bg='yn', fg='yes'),
#cb=deleteCallback, value=True),
Button((168,86,120,100), bg='yn', fg='no')],
#cb=deleteCallback, value=False)],
# Screen mode 2 is 'No Images'
[Button((0, 0,320,240)), #cb=doneCallback), # Full screen = button
Button((0,188,320, 52), bg='done'), # Fake 'Done' button
Button((0, 53,320, 80), bg='empty')], # 'Empty' message
# Screen mode 3 is viewfinder / snapshot
[Button(( 0,188,156, 52), bg='gear',),# cb=viewCallback, value=0),
Button((164,188,156, 52), bg='play',),# cb=viewCallback, value=1),
Button(( 0, 0,320,240) ,),# cb=viewCallback, value=2),
Button(( 88, 51,157,102)), # 'Working' label (when enabled)
Button((148, 110,22, 22))], # Spinner (when enabled)
]
ifname=0
class IPdisplay:
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
font = pygame.font.Font(None, 30)
try:
eth = get_ip_address('eth0')
except IOError:
eth = ('0.0.0.0')
try:
wl = get_ip_address('wlan0')
except IOError:
wl = ("0.0.0.0")
if eth == ("0.0.0.0"):
ethdisp = font.render(ethip, 1, (255,0,0))
else:
ethdisp = font.render(ethip, 1, (0, 255, 0))
screen.blit( ethdisp, (0,0) )
ethrect = ethdisp.get_rect()
if wl == ("0.0.0.0"):
wldisp = font.render(wlip, 1, (255, 0, 0))
else:
wldisp = font.render(wlip, 1, (0, 255, 0))
screen.blit(wldisp, (0,1*font.get_linesize()) )
wlrect = wldisp.get_rect()
#pygame.init()
#size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
#screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
#pygame.mouse.set_visible(0)
#black = (0, 0, 0)
#screen.fill(black)
#screen.blit(ethdisp,(0,0))
#screen.blit(wldisp, (0,1*font.get_linesize()) )
#pygame.display.flip()
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV' , '/dev/fb1')
os.putenv('SDL_MOUSEDRV' , 'TSLIB')
os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen')
pygame.init()
#scope = IPdisplay()
#scope.getips()
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
# Load all icons at startup.
for file in os.listdir(iconPath):
if fnmatch.fnmatch(file, '*.png'):
icons.append(Icon(file.split('.')[0]))
# Assign Icons to Buttons, now that they're loaded
for s in buttons: # For each screenful of buttons...
for b in s: # For each button on screen...
for i in icons: # For each icon...
if b.bg == i.name: # Compare names; match?
b.iconBg = i # Assign Icon to Button
b.bg = None # Name no longer used; allow garbage collection
if b.fg == i.name:
b.iconFg = i
b.fg = None
while (True): # your main loop
# get all events
ev = pygame.event.get()
screen.blit(ethdisp,(0,0))
screen.blit(wldisp, (0,1*font.get_linesize()) )
# proceed events
for event in ev:
# handle MOUSEBUTTONUP
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
key = pygame.mouse.get_pressed()
# print pos
# print key
scope = IPdisplay()
scope.__init__()
if event.type == pygame.KEYUP:
if event.key == K_ESCAPE:
print "Escape Pressed"
pygame.quit()
else:
print (event.key)
print "you go nowhere, looping"
for i,b in enumerate(buttons[screenMode]):
b.draw(screen)
pygame.display.update()