forked from nchronas/piTFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic.py
395 lines (288 loc) · 11.5 KB
/
pic.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import os
import pygame
import time
import urllib2
import json
from signal import alarm, signal, SIGALRM
import picamera
import RPi.GPIO as GPIO
import sh
import io
import yuv2rgb
from threading import Timer
liveFlag = 0
os.chdir("/data/piTFT_mBeast")
def redB(channel):
global liveFlag
print "event", channel, GPIO.input(channel)
if liveFlag == 3:
liveFlag = 1
print "live feed is " + str(liveFlag)
def greenB(channel):
global liveFlag
print "event", channel, GPIO.input(channel)
if liveFlag == 1:
liveFlag = 2
elif liveFlag == 3:
liveFlag = 4
elif liveFlag == 5:
liveFlag = 6
elif liveFlag == 7:
liveFlag = 8
elif liveFlag == 9:
liveFlag = 1
print "live feed is " + str(liveFlag)
#set up the screen so we can push stuff onto it.
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
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)
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
signal(SIGALRM, alarm_handler)
alarm(3)
try:
pygame.display.init()
print 'getting screen size'
size = (pygame.display.Info().current_w, pygame.display.Info().current_h)
self.screen = pygame.display.set_mode(size, 0, 32)
print size
alarm(0)
except Alarm:
raise KeyboardInterrupt
print 'setting up framebuffer'
# Clear the screen to start
self.screen.fill((0, 0, 0))
# Initialise font support
pygame.font.init()
# Render the screen
pygame.display.update()
def __del__(self):
"Destructor to make sure pygame shuts down, etc."
def main():
global liveFlag
#this path is where we store our arrow icons
installPath = "/usr/src/app/img/"
# font colours
colourWhite = (255, 255, 255)
colourBlack = (0, 0, 0)
colourGreen = (3, 192, 60)
colourRed = (220, 69, 69)
# Create an instance of the pitft class
mytft = pitft()
#hide the mouse from screen
pygame.mouse.set_visible(False)
# set up the fonts
# choose the font
fontpath = pygame.font.match_font('dejavusansmono')
font = pygame.font.Font(fontpath, 10)
#read the ENV VAR, use GE if 'STOCK' isn't there
#companyName = os.getenv('STOCK', "GE")
#print 'company name: '+companyName
logo = pygame.image.load( "/usr/src/app/resin.png")
mytft.screen.blit(logo, (0, 0))
pygame.display.update()
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# GPIO.add_event_detect(18, GPIO.RISING, callback=picView) # add rising edge detection on a channel
# GPIO.add_event_detect(27, GPIO.RISING, callback=liveFeed) # add rising edge detection on a channel
GPIO.add_event_detect(23, GPIO.RISING, callback=redB) # add rising edge detection on a channel
GPIO.add_event_detect(22, GPIO.RISING, callback=greenB) # add rising edge detection on a channel
sizeMode = 0
sizeData = [ # Camera parameters for different size settings
# Full res Viewfinder Crop window
[(2592, 1944), (320, 240), (0.0 , 0.0 , 1.0 , 1.0 )], # Large
[(1920, 1080), (320, 180), (0.1296, 0.2222, 0.7408, 0.5556)], # Med
[(1440, 1080), (320, 240), (0.2222, 0.2222, 0.5556, 0.5556)]] # Small
# Buffers for viewfinder data
rgb = bytearray(320 * 240 * 3)
yuv = bytearray(320 * 240 * 3 / 2)
if os.path.isfile('update_count.txt'):
with open('update_count.txt','rb') as f:
updates = int(f.readline())
else:
updates = 0
stripped = lambda s: "".join(i for i in s if 31 < ord(i) < 127)
colourWhite = (255, 255, 255)
colourBlack = (0, 0, 0)
colourGreen = (3, 192, 60)
colourRed = (220, 69, 69)
colourPink = (255, 20, 147)
textAnchorX = 10
textAnchorY = 10
textYoffset = 10
listMax = 240/textYoffset
while True:
if liveFlag == 0:
camera = picamera.PiCamera()
camera.resolution = sizeData[sizeMode][1]
camera.crop = (0.0, 0.0, 1.0, 1.0)
liveFlag = 1
elif liveFlag == 1:
stream = io.BytesIO() # Capture into in-memory stream
camera.capture(stream, use_video_port=True, format='raw')
stream.seek(0)
stream.readinto(yuv) # stream -> YUV buffer
stream.close()
yuv2rgb.convert(yuv, rgb, sizeData[sizeMode][1][0], sizeData[sizeMode][1][1])
img = pygame.image.frombuffer(rgb[0: (sizeData[sizeMode][1][0] * sizeData[sizeMode][1][1] * 3)], sizeData[sizeMode][1], 'RGB')
if img is None or img.get_height() < 240: # Letterbox, clear background
mytft.screen.fill(0)
if img:
mytft.screen.blit(img,((320 - img.get_width() ) / 2, (240 - img.get_height()) / 2))
pygame.display.update()
elif liveFlag == 2:
print "taking pic"
#camera.resolution = (1024,768)
name = 'image.jpg'
camera.capture(name, resize=(320, 240))
#time.sleep(0.1)
print "Pic to screen"
logo = pygame.image.load(name)
mytft.screen.blit(logo, (0, 0))
#pygame.display.flip()
# # refresh the screen with all the changes
pygame.display.update()
print "Screen updated"
time.sleep(0.1)
camera.resolution = sizeData[sizeMode][1]
camera.crop = (0.0, 0.0, 1.0, 1.0)
liveFlag = 3
elif liveFlag == 4:
# clear the screen
mytft.screen.fill(colourBlack)
# set the anchor/positions for the current stock data text
textAnchorX = 10
textAnchorY = 10
line = "git status"
print line
text_surface = font.render(line, True, colourRed)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
textAnchorY += textYoffset
pygame.display.update()
for line in sh.git( "status", _iter=True):
print(line)
line = stripped(line)
text_surface = font.render(line, True, colourWhite)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
pygame.display.update()
textAnchorY += textYoffset
if textAnchorY + textYoffset > 240:
textAnchorY = 10
mytft.screen.fill(colourBlack)
time.sleep(0.1)
time.sleep(3)
updates += 1
with open('update_count.txt','wb') as f:
f.write(str(updates))
mytft.screen.fill(colourBlack)
textAnchorX = 10
textAnchorY = 10
line = "git add ."
print line
text_surface = font.render(line, True, colourRed)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
textAnchorY += textYoffset
pygame.display.update()
for line in sh.git.add ("image.jpg", "update_count.txt", _iter=True):
print(line)
line = stripped(line)
text_surface = font.render(line, True, colourWhite)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
pygame.display.update()
textAnchorY += textYoffset
if textAnchorY + textYoffset > 240:
textAnchorY = 10
mytft.screen.fill(colourBlack)
time.sleep(0.1)
liveFlag = 5
elif liveFlag == 6:
mytft.screen.fill(colourBlack)
textAnchorX = 10
textAnchorY = 10
line = "git commit -m "
print line
text_surface = font.render(line, True, colourRed)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
textAnchorY += textYoffset
pygame.display.update()
for line in sh.git( "commit", "-m", "Auto commit", _iter="out"):
print(line)
line = stripped(line)
text_surface = font.render(line, True, colourWhite)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
pygame.display.update()
textAnchorY += textYoffset
if textAnchorY + textYoffset > 240:
textAnchorY = 10
mytft.screen.fill(colourBlack)
time.sleep(0.1)
liveFlag = 7
elif liveFlag == 8:
mytft.screen.fill(colourBlack)
textAnchorX = 10
textAnchorY = 10
line = "git push resin master"
print line
text_surface = font.render(line, True, colourRed)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
textAnchorY += textYoffset
pygame.display.update()
color = colourWhite
sc = []
sc.append("git push resin master")
for line in sh.git( "push", "resin", "master", "--force", _iter="err"):
print(line)
line = stripped(line)
color = colourWhite
sc.append(line)
if len(sc) >= listMax :
sc.pop(1)
textAnchorY = 0
mytft.screen.fill(colourBlack)
for idx, lines in enumerate(sc):
if idx == 0:
try:
text_surface = font.render(lines, True, colourRed)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
textAnchorY += textYoffset
except:
print "Could not render"
else:
if lines.find("Build took") >= 0 or lines.find(">\\/7") >= 0 :
print "Unicorn found"
color = colourPink
text_surface = font.render(lines, True, color)
mytft.screen.blit(text_surface, (textAnchorX, textAnchorY))
textAnchorY += textYoffset
pygame.display.update()
time.sleep(0.1)
liveFlag = 9
elif liveFlag == 10:
camera.close()
logo = pygame.image.load( "/usr/src/app/resin.png")
mytft.screen.blit(logo, (0, 0))
pygame.display.update()
liveFlag = 11
else :
time.sleep(1)
if __name__ == '__main__':
print 'starting main()'
main()