-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.py
230 lines (176 loc) · 6.47 KB
/
display.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
import sys
import time
import threading
import os, syslog, sys, signal
import pygame
import string
import json
maxLineLength = 36;
display_width = 480;
display_height = 320;
class pitft :
screen = None;
colourBlack = (0, 0, 0)
def __init__(self):
"Ininitializes a new pygame screen using the framebuffer"
# Based on "Python GUI in Linux frame buffer"
# http://www.karoltomala.com/blog/?p=679
print "Setting up TFT Screen"
disp_no = os.getenv("DISPLAY")
if disp_no:
print "I'm running under X display = {0}".format(disp_no)
os.putenv('SDL_FBDEV', '/dev/fb1')
# Select frame buffer driver
# Make sure that SDL_VIDEODRIVER is set
driver = 'fbcon'
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print 'Driver: {0} failed.'.format(driver)
exit(0)
print "Screen Initialized"
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
print "Got Size"
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
print "Mode is set"
# Clear the screen to start
self.screen.fill((0, 0, 0))
print "Screen is black"
# Initialise font support
pygame.font.init()
print "Fonts Initialized"
# Render the screen
pygame.display.update()
print "Loading Fonts"
#u'droidsans', u'freemono', u'droidsansthai', u'droidsansarmenian', u'droidsansmono', u'freeserif', u'roboto', u'dejavuserif', u'droidsansethiopic', u'droidsanshebrew', u'dejavusans', u'droidsansgeorgian', u'droidsansfallback', u'robotocondensed', u'freesans', u'dejavusansmono', u'droidserif', u'droidsansjapanese', u'droidarabicnaskh'
#self.fontpath = pygame.font.match_font('dejavusansmono')
self.fontpath = "fonts/Roboto-Light.ttf"
self.fontpathNormal = "fonts/Roboto-Regular.ttf"
self.fontXxl = pygame.font.Font(self.fontpathNormal, 34)
self.fontXl = pygame.font.Font(self.fontpathNormal, 34)
self.fontMd = pygame.font.Font(self.fontpathNormal, 20)
self.fontSm = pygame.font.Font(self.fontpathNormal, 18)
print "Fonts Loaded"
def __del__(self):
"Destructor to make sure pygame shuts down, etc."
def color_surface(self, surface, red, green, blue):
arr = pygame.surfarray.pixels3d(surface)
arr[:,:,0] = red
arr[:,:,1] = green
arr[:,:,2] = blue
def text_objects(self, text, font, color):
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
def message_display(self, text, size, x, y, color):
global display_width, display_height
if size == "sm":
sizeText = self.fontSm
elif size == "md":
sizeText = self.fontMd
elif size == "xl":
sizeText = self.fontXl
elif size == "xxl":
sizeText = self.fontXxl
TextSurf, TextRect = self.text_objects(text, sizeText, color)
if x == False:
TextRect.center = ((display_width/2),y+TextRect.height/2)
elif x < 0:
TextRect.center = (display_width - TextRect.width/2 +x ,y+TextRect.height/2)
else:
TextRect.center = (x+TextRect.width/2,y+TextRect.height/2)
self.screen.blit(TextSurf, TextRect)
def draw_rect(self, xs, ys, x, y, color, width = 1):
pygame.draw.rect(self.screen, color, [x,y,xs,ys], width)
def draw_circle(self, x, y, r, color, width = 0):
pygame.draw.circle(self.screen, color, [x,y], r, width)
def showimage(self, image, x,y, xscale=False,yscale=False, color=False):
global display_width, display_height
icon = image
logo = pygame.image.load(icon).convert_alpha()
if xscale != False and yscale != False:
logo = pygame.transform.scale(logo, (xscale,yscale))
if color != False:
self.color_surface(logo, color["r"],color["g"],color["b"])
self.screen.blit(logo, (x, y))
class Display(threading.Thread):
def __init__(self, comm):
threading.Thread.__init__(self)
self.comm = comm;
self.stopped = False
self.mytft = pitft()
def stop(self):
print "Display Ending"
self.stopped = True
def run(self):
(linux, hostname, kernelversion, message, processor) = os.uname();
if not processor.startswith("arm"):
print "Not running on Raspberry Pi, not Initializing Display"
colourBlack = (0, 0, 0)
colourGreen = (0, 200, 0)
colourGreenBright = (0, 225, 0)
colourYellow = (225, 200, 0)
colourRed = (200, 0, 0)
colourRedBright = (255, 0, 0)
colourWhite = (255, 255, 255)
blink = True
comm = self.comm
while not self.stopped:
try:
self.mytft.screen.fill(colourBlack)
self.mytft.message_display("TUDS ArtNet Media Player", "xl", False, 10, colourWhite)
dmx = "Not Configured"
if not comm.get("signal", False):
dmx = "DMX: %s.%s - Kein Signal" % (comm.get("universe", "-"), comm.get("address", "-"))
if blink:
self.mytft.draw_circle(10, 300, 5, colourRed)
blink = False
else:
self.mytft.draw_circle(10, 300, 5, colourRedBright)
blink = True
elif comm.get("universe", None) is not None and comm.get("address", None) is not None:
dmx = "DMX: %s.%s" % (comm.get("universe"), comm.get("address"))
if blink:
self.mytft.draw_circle(10, 300, 5, colourGreenBright)
blink = False
else:
self.mytft.draw_circle(10, 300, 5, colourGreen)
blink = True
else:
dmx = "DMX: -/-"
self.mytft.message_display(dmx, "sm", 25, 290, colourWhite)
self.mytft.message_display("IP: %s" % comm.get("ip"), "sm", -10, 290, colourWhite)
for playerId in comm.get("audio", []):
player = comm.get("audio").get(playerId)
startOffset = (playerId - 1) * 56 + 80
self.mytft.message_display("Audio Player %s" % playerId, "md", 25, startOffset, colourWhite)
if player:
state = player.get("state")
file = "%s/%s" % (player.get("folder"), player.get("fileNo"))
fileName = player.get("file")
if fileName:
try:
fileName = os.path.basename(fileName)
except Exception as e:
pass
volume = player.get("volume")
self.mytft.message_display(state, "md", 180, startOffset, colourWhite)
self.mytft.message_display(file, "md", 260, startOffset, colourWhite)
self.mytft.draw_rect(104, 16, 340, startOffset+5, colourWhite)
self.mytft.draw_rect(volume, 12, 342, startOffset+7, colourWhite, width=0)
self.mytft.message_display(fileName, "md", 40, startOffset+22, colourWhite)
pass
pygame.display.update()
time.sleep(.25)
except KeyboardInterrupt:
return False
print "Display Terminated"
if __name__ == "__main__":
disp = Display({
"ip": "10.0.2.3",
"universe": 0,
"address": 100,
})
disp.run()