-
Notifications
You must be signed in to change notification settings - Fork 0
/
HWTest.py
1016 lines (869 loc) · 38 KB
/
HWTest.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: us-ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
#
# hardware_test - a simple PyGame hardware tester
# Copyright (C) 2012 Chris Clark
# Ported by bbruno5 for PAP KIII Plus
"""Hardware test app for PyGame.
Targetted at GCW0 buttons and analog nub.
Could be made more generic via config files.
"""
import os
import sys
import time
import glob
import subprocess
from math import sin, cos, pi
import pygame
import pygame.locals
DEBUG = False
no_secs = False
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PRESSED_DONE = BLUE
PRESSED_ACTIVE = GREEN
BOX_OUTLINE = WHITE
class TextRectException(Exception):
# TextRect from http://www.pygame.org/pcr/text_rect/index.php
def __init__(self, message=None):
self.message = message
def __str__(self):
return self.message
def render_textrect(string, font, rect, text_color, background_color=None, justification=0, surface=None):
"""Returns a surface containing the passed text string, reformatted
to fit within the given rect, word-wrapping as necessary. The text
will be anti-aliased.
Takes the following arguments:
string - the text you wish to render. \n begins a new line.
font - a Font object
rect - a rectstyle giving the size of the surface requested.
text_color - a three-byte tuple of the rgb value of the
text color. ex (0, 0, 0) = BLACK
background_color - a three-byte tuple of the rgb value of the surface.
justification - 0 (default) left-justified
1 horizontally centered
2 right-justified
Returns the following values:
Success - a surface object with the text rendered onto it.
Failure - raises a TextRectException if the text won't fit onto the surface.
"""
import pygame
final_lines = []
requested_lines = string.splitlines()
# Create a series of lines that will fit on the provided
# rectangle.
for requested_line in requested_lines:
if font.size(requested_line)[0] > rect.width:
words = requested_line.split(' ')
# if any of our words are too long to fit, return.
for word in words:
if font.size(word)[0] >= rect.width:
raise TextRectException("The word " + word + " is too long to fit in the rect passed.")
# Start a new line
accumulated_line = ""
for word in words:
test_line = accumulated_line + word + " "
# Build the line while the words fit.
if font.size(test_line)[0] < rect.width:
accumulated_line = test_line
else:
final_lines.append(accumulated_line)
accumulated_line = word + " "
final_lines.append(accumulated_line)
else:
final_lines.append(requested_line)
# Let's try to write the text out on the surface.
surface = surface or pygame.Surface(rect.size)
if background_color:
surface.fill(background_color)
accumulated_height = 0
for line in final_lines:
if accumulated_height + font.size(line)[1] >= rect.height:
raise TextRectException("Once word-wrapped, the text string was too tall to fit in the rect.")
if line != "":
tempsurface = font.render(line, 1, text_color)
if justification == 0:
surface.blit(tempsurface, (0, accumulated_height))
elif justification == 1:
surface.blit(tempsurface, ((rect.width - tempsurface.get_width()) / 2, accumulated_height))
elif justification == 2:
surface.blit(tempsurface, (rect.width - tempsurface.get_width(), accumulated_height))
else:
raise TextRectException("Invalid justification argument: " + str(justification))
accumulated_height += font.size(line)[1]
return surface
def get_time_surface(background, font_time):
if no_secs:
time_str = time.strftime('%H:%M')
else:
time_str = time.strftime('%H:%M:%S')
time_surface = font_time.render(time_str, True, WHITE,)
textRect = time_surface.get_rect()
textRect.centerx = background.get_rect().centerx
textRect.centery = background.get_rect().centery
return time_surface, textRect
def get_countdown_surface(background, font_time, timer_count):
time_str = 'Done in %d' % timer_count
time_surface = font_time.render(time_str, True, WHITE,)
textRect = time_surface.get_rect()
#textRect.left = background.get_rect().left
#textRect.top = background.get_rect().top
textRect.left, textRect.top = 0, 0
return time_surface, textRect
# OpenDingux SDL button mappings
BTN_DPAD_UP = pygame.locals.K_UP
BTN_DPAD_DOWN = pygame.locals.K_DOWN
BTN_DPAD_LEFT = pygame.locals.K_LEFT
BTN_DPAD_RIGHT = pygame.locals.K_RIGHT
BTN_A = pygame.locals.K_LCTRL
BTN_B = pygame.locals.K_LALT
BTN_X = pygame.locals.K_SPACE
BTN_Y = pygame.locals.K_LSHIFT
BTN_START = pygame.locals.K_RETURN
BTN_SELECT = pygame.locals.K_ESCAPE
BTN_LEFT_SHOULDER = pygame.locals.K_TAB
BTN_RIGHT_SHOULDER = pygame.locals.K_BACKSPACE
BTN_HOLD = pygame.locals.K_END # NOTE OpenDingux=hold_slide
BTN_VOL_DOWN = pygame.locals.K_1
BTN_VOL_UP = pygame.locals.K_2
papk3 = {
'name': 'PAP KIII',
'background': 'pap.png',
'test_buttons': {
BTN_DPAD_UP: (40, 40, 20, 20),
BTN_DPAD_DOWN: (40, 100, 20, 20),
BTN_DPAD_LEFT: (10, 70, 20, 20),
BTN_DPAD_RIGHT: (70, 70, 20, 20),
BTN_A: (450, 70, 20, 20),
BTN_B: (420, 100, 20, 20),
BTN_X: (420, 40, 20, 20),
BTN_Y: (390, 70, 20, 20),
BTN_START: (435, 200, 20, 20),
BTN_SELECT: (405, 200, 20, 20),
BTN_LEFT_SHOULDER: (30, 10, 40, 20),
BTN_RIGHT_SHOULDER: (410, 10, 40, 20),
BTN_HOLD: (220, 10, 40, 20),
BTN_VOL_DOWN: (405, 170, 20, 20),
BTN_VOL_UP: (435, 170, 20, 20)
},
}
a320 = {
'name': 'Dingoo A320',
'background': 'a320.png',
'test_buttons': {
BTN_DPAD_UP: (40, 40, 20, 20),
BTN_DPAD_DOWN: (40, 100, 20, 20),
BTN_DPAD_LEFT: (10, 70, 20, 20),
BTN_DPAD_RIGHT: (70, 70, 20, 20),
BTN_A: (280, 70, 20, 20),
BTN_B: (250, 100, 20, 20),
BTN_X: (250, 40, 20, 20),
BTN_Y: (220, 70, 20, 20),
BTN_START: (220, 170, 20, 20),
BTN_SELECT: (70, 170, 20, 20),
BTN_LEFT_SHOULDER: (30, 10, 40, 20),
BTN_RIGHT_SHOULDER: (240, 10, 40, 20),
BTN_HOLD: (280, 130, 20, 20),
0: (280, 100, 20, 20), # Power
},
}
sound_buttons = {
BTN_START: 'audiocheck.net_c.wav',
BTN_LEFT_SHOULDER: 'audiocheck.net_l.wav',
BTN_RIGHT_SHOULDER: 'audiocheck.net_r.wav',
}
ESCAPE_IS_QUIT = True
ESCAPE_IS_QUIT = False
TEST_TIMEOUT = 10 * 1000 # 10 seconds
def test_sound(clock, screen, font_time, font_text, j):
image_filename = 'wallpaper.png'
my_rect = screen.get_rect()
try:
# NOTE image needs to match screen res
# TODO handle images too small/large
background = pygame.image.load(image_filename)
background = background.convert()
except pygame.error:
background = pygame.Surface(my_rect.size)
text_str = '''
START=both
Left shoulder=left
Right shoulder=right
SELECT=quit'''
rendered_text = render_textrect(text_str, font_text, my_rect, WHITE, surface=background)
if rendered_text:
background.blit(rendered_text, my_rect.topleft)
keepGoing = True
while keepGoing:
#clock.tick(4) # 4 times a second
clock.tick(60) # 60 times a second
bg = background.copy()
# update an on screen clock to show activity (and not hung)
# TODO replace with a count down timer and have button test auto quit?
time_surface, textRect = get_time_surface(bg, font_time)
bg.blit(time_surface, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False # Quit
elif event.type == pygame.KEYDOWN:
try:
sound = sound_buttons[event.key]
sound.play()
except KeyError:
# TODO display to screen too?
print 'Unsupported button/key pressed', event.key
if event.key == BTN_SELECT:
keepGoing = False # Quit
screen.blit(bg, (0, 0))
pygame.display.flip()
class BaseException(Exception):
'''Base exception'''
class SpawnError(BaseException):
'''Spawned process exception'''
def execute(command):
errors_output = subprocess.PIPE
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
data = p.stdout.read()
data_err = p.stderr.read()
rc = p.wait()
if rc:
raise SpawnError('error spawning rc=%r %r stderr=%r stdout=%r' % (rc, command, data_err, data))
return data, data_err
def test_mic(clock, screen, font_time, font_text, j):
temp_filename = '/tmp/delme.wav'
num_secs = 3
command = ['arecord', '--nonblock', '--format=S16_LE', '-d %d' % num_secs, '--rate=11025', '--channels=1', temp_filename]
class FakeSound(object):
def play(self):
pass
sound = FakeSound()
image_filename = 'wallpaper.png'
my_rect = screen.get_rect()
try:
# NOTE image needs to match screen res
# TODO handle images too small/large
background = pygame.image.load(image_filename)
background = background.convert()
except pygame.error:
background = pygame.Surface(my_rect.size)
text_str = '''Microphone Test
Left shoulder=record %d secs
Right shoulder=play
SELECT=quit''' % num_secs
rendered_text = render_textrect(text_str, font_text, my_rect, WHITE, surface=background)
if rendered_text:
background.blit(rendered_text, my_rect.topleft)
keepGoing = True
while keepGoing:
#clock.tick(4) # 4 times a second
clock.tick(60) # 60 times a second
bg = background.copy()
# update an on screen clock to show activity (and not hung)
# TODO replace with a count down timer and have button test auto quit?
time_surface, textRect = get_time_surface(bg, font_time)
bg.blit(time_surface, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False # Quit
elif event.type == pygame.KEYDOWN:
if event.key == BTN_SELECT:
keepGoing = False # Quit
elif event.key == BTN_LEFT_SHOULDER:
d = execute(command)
# TODO sound a ping noise to show recording completed
print 'DEBUG', d
sound = pygame.mixer.Sound(temp_filename)
#sound = pygame.mixer.Sound('audiocheck.net_c.wav') # DEBUG
elif event.key == BTN_RIGHT_SHOULDER:
sound.play()
else:
# TODO display to screen too?
print 'Unsupported button/key pressed', event.key
screen.blit(bg, (0, 0))
pygame.display.flip()
# FIXME TODO delete temp_filename
analog_deadzone = 0.01 # basically error margin to ignore
def test_buttons(clock, screen, font_time, font_text, j):
test_hardware = dumb_system_id()
image_filename = test_hardware['background']
test_buttons = test_hardware['test_buttons']
my_rect = screen.get_rect()
no_buttons_pressed = pygame.time.get_ticks()
try:
# NOTE image needs to match screen res
# TODO handle images too small/large
background = pygame.image.load(image_filename)
background = background.convert()
except pygame.error:
background = pygame.Surface(my_rect.size)
# draw one pixel line around edge
pygame.draw.rect(background, BOX_OUTLINE, my_rect, 1)
bg = background.copy()
# TODO? Display system name test_hardware['name']
time_surface, textRect = get_time_surface(bg, font_time)
bg.blit(time_surface, textRect)
for x in test_buttons:
box_details = test_buttons[x]
pygame.draw.rect(background, BOX_OUTLINE, box_details)
keepGoing = True
really_quit = False
while keepGoing:
#clock.tick(4) # 4 times a second
clock.tick(60) # 60 times a second
bg = background.copy()
# update an on screen clock to show activity (and not hung)
# TODO replace with a count down timer and have button test auto quit?
time_surface, textRect = get_time_surface(bg, font_time)
bg.blit(time_surface, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False # Quit
really_quit = True
elif event.type == pygame.KEYDOWN:
no_buttons_pressed = pygame.time.get_ticks()
#print pygame.key.get_pressed()
try:
box_details = test_buttons[event.key]
if event.key != 0:
pygame.draw.ellipse(background, PRESSED_ACTIVE, box_details)
else:
# OpenDingux hack
pygame.draw.rect(background, RED, box_details)
except KeyError:
# TODO display to screen too?
print 'WARNING Unsupported button/key pressed', event.key
if ESCAPE_IS_QUIT and event.key == pygame.locals.K_ESCAPE:
# FIXME better quit option, I'm tempted to NOT have one and let OS do it but screen clean up under OpenDingux is not great when abnormally terminating processes
keepGoing = False # Quit
elif event.type == pygame.KEYUP:
try:
box_details = test_buttons[event.key]
pygame.draw.rect(background, PRESSED_DONE, box_details)
except KeyError:
print 'WARNING Unsupported button/key released', event.key
else:
print 'WARNING unknown event occurred'
# Joystick
if j:
# FIXME this is dirty... TODO use render_textrect() and check ALL Joystick axis/buttons
jstick_str = ''
i = 0
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = 'Axis %i reads %.2f' % (i, axisread)
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = bg.get_rect().bottom - textRect.height
if jstick_str:
bg.blit(text, textRect)
temp_y = textRect.centery
jstick_str = ''
i = 1
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = 'Axis %i reads %.2f' % (i, axisread)
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = temp_y - textRect.height
if jstick_str:
bg.blit(text, textRect)
else:
# no joystick
jstick_str = 'no joystick found'
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = bg.get_rect().bottom - textRect.height
if jstick_str:
bg.blit(text, textRect)
screen.blit(bg, (0, 0))
pygame.display.flip()
if not ESCAPE_IS_QUIT:
if (pygame.time.get_ticks() - no_buttons_pressed) >= TEST_TIMEOUT:
keepGoing = False # Quit
def test_analog1(clock, screen, font_time, font_text, j):
test_hardware = dumb_system_id()
image_filename = test_hardware['background']
my_rect = screen.get_rect()
no_buttons_pressed = pygame.time.get_ticks()
try:
# NOTE image needs to match screen res
# TODO handle images too small/large
background = pygame.image.load(image_filename)
background = background.convert()
except pygame.error:
background = pygame.Surface(my_rect.size)
bg = background.copy()
screen_centerx = bg.get_rect().centerx
screen_centery = bg.get_rect().centery
# draw one pixel line around edge of analog display range box
#box_factor = 1
box_factor = 2 # 1/2 (0.5)
#box_factor = 3 # 1/3 (0.33)
box_factor = 100 / box_factor
pygame.draw.rect(background, BOX_OUTLINE, ((screen_centerx - 1) - box_factor, (screen_centery - 1) - box_factor, (box_factor * 2) + 3, (box_factor * 2) + 3), 1)
num_axes = 0
if j:
num_axes = j.get_numaxes()
#num_axes = 2 # DEBUG pretend to be gcw0
# TODO? Display system name test_hardware['name']
time_to_quit = TEST_TIMEOUT - (pygame.time.get_ticks() - no_buttons_pressed)
time_surface, textRect = get_countdown_surface(bg, font_time, time_to_quit)
bg.blit(time_surface, textRect)
keepGoing = True
really_quit = False
#pygame.key.set_repeat(int(1000 * 0.1), int(1000 * 0.1))
#pygame.key.set_repeat(1, 50)
#pygame.key.set_repeat(40, 30)
pygame.key.set_repeat(500, 30)
global analog_deadzone
while keepGoing:
#clock.tick(4) # 4 times a second
clock.tick(60) # 60 times a second
bg = background.copy()
# update an on screen clock to show activity (and not hung)
# TODO replace with a count down timer and have button test auto quit?
time_to_quit = TEST_TIMEOUT - (pygame.time.get_ticks() - no_buttons_pressed)
time_surface, textRect = get_countdown_surface(bg, font_time, time_to_quit)
bg.blit(time_surface, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False # Quit
really_quit = True
elif event.type == pygame.KEYDOWN:
no_buttons_pressed = pygame.time.get_ticks()
if event.key in [pygame.locals.K_UP, BTN_A, pygame.locals.K_a]:
analog_deadzone += 0.01
analog_deadzone = min(analog_deadzone, 1.0)
elif event.key in [pygame.locals.K_DOWN, BTN_B, ]:
analog_deadzone -= 0.01
analog_deadzone = max(analog_deadzone, 0.0)
elif event.type == pygame.KEYUP:
if event.key == pygame.locals.K_ESCAPE:
keepGoing = False # Quit
elif event.key in [pygame.locals.K_RIGHT, BTN_X, ]:
analog_deadzone += 0.01
analog_deadzone = min(analog_deadzone, 1.0)
elif event.key in [pygame.locals.K_LEFT, BTN_Y, ]:
analog_deadzone -= 0.01
analog_deadzone = max(analog_deadzone, 0.0)
# Joystick
if j:
if num_axes == 2:
axis_x, axis_y = 0, 0
jstick_str = ''
i = 0
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
axis_x = int(axisread * box_factor)
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = 'Axis %i reads %.2f' % (i, axisread)
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = bg.get_rect().bottom - textRect.height
if jstick_str:
bg.blit(text, textRect)
temp_y = textRect.centery
jstick_str = ''
i = 1
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
axis_y = int(axisread * box_factor)
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = 'Axis %i reads %.2f' % (i, axisread)
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = temp_y - textRect.height
if jstick_str:
bg.blit(text, textRect)
pygame.draw.rect(bg, RED, ((screen_centerx - 1) + axis_x, (screen_centery - 1) + axis_y, 3, 3))
text = font_text.render('Deadzone %.2f' % analog_deadzone, True, WHITE)
textRect = text.get_rect()
textRect.left = 5
textRect.top = 30
bg.blit(text, textRect)
else:
jstick_str = ''
for i in range(0, j.get_numaxes()):
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = jstick_str +'\n' + 'Axis %i reads %.2f' % (i, axisread)
for i in range(0, j.get_numbuttons()):
buttonread = j.get_button(i)
if buttonread != 0:
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = jstick_str +'\n' + 'Button %i reads %i' % (i, buttonread)
if jstick_str:
rendered_text = render_textrect(jstick_str, font_text, my_rect, WHITE, surface=None)
if rendered_text:
bg.blit(rendered_text, my_rect.topleft)
else:
# no joystick
jstick_str = 'no joystick found'
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = bg.get_rect().bottom - textRect.height
if jstick_str:
bg.blit(text, textRect)
bg.set_at((screen_centerx, screen_centery), WHITE) # draw single pixel dot at center
screen.blit(bg, (0, 0))
pygame.display.flip()
if not ESCAPE_IS_QUIT:
if time_to_quit <= 0:
keepGoing = False # Quit
pygame.key.set_repeat()
def test_analog2(clock, screen, font_time, font_text, j):
"""Tests second joystick (i.e. #1, #0 is the first one).
On GCW0 device this is the gsensor if the gsensor userspace driver
has been successfully installed and ran.
"""
test_hardware = dumb_system_id()
image_filename = test_hardware['background']
try:
j = pygame.joystick.Joystick(1)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
except pygame.error:
j = None
# open number 2 joystick (on GCW) gsensor driver stick)
my_rect = screen.get_rect()
no_buttons_pressed = pygame.time.get_ticks()
try:
# NOTE image needs to match screen res
# TODO handle images too small/large
background = pygame.image.load(image_filename)
background = background.convert()
except pygame.error:
background = pygame.Surface(my_rect.size)
bg = background.copy()
screen_centerx = bg.get_rect().centerx
screen_centery = bg.get_rect().centery
# draw one pixel line around edge of analog display range box
#box_factor = 1
box_factor = 2 # 1/2 (0.5)
#box_factor = 3 # 1/3 (0.33)
box_factor = 100 / box_factor
pygame.draw.rect(background, BOX_OUTLINE, ((screen_centerx - 1) - box_factor, (screen_centery - 1) - box_factor, (box_factor * 2) + 3, (box_factor * 2) + 3), 1)
num_axes = 0
if j:
num_axes = j.get_numaxes()
#num_axes = 2 # DEBUG pretend to be gcw0
# TODO? Display system name test_hardware['name']
time_to_quit = TEST_TIMEOUT - (pygame.time.get_ticks() - no_buttons_pressed)
time_surface, textRect = get_countdown_surface(bg, font_time, time_to_quit)
bg.blit(time_surface, textRect)
keepGoing = True
really_quit = False
#pygame.key.set_repeat(int(1000 * 0.1), int(1000 * 0.1))
#pygame.key.set_repeat(1, 50)
#pygame.key.set_repeat(40, 30)
pygame.key.set_repeat(500, 30)
global analog_deadzone
while keepGoing:
#clock.tick(4) # 4 times a second
clock.tick(60) # 60 times a second
bg = background.copy()
# update an on screen clock to show activity (and not hung)
# TODO replace with a count down timer and have button test auto quit?
time_to_quit = TEST_TIMEOUT - (pygame.time.get_ticks() - no_buttons_pressed)
time_surface, textRect = get_countdown_surface(bg, font_time, time_to_quit)
bg.blit(time_surface, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False # Quit
really_quit = True
elif event.type == pygame.KEYDOWN:
no_buttons_pressed = pygame.time.get_ticks()
if event.key in [pygame.locals.K_UP, BTN_A, pygame.locals.K_a]:
analog_deadzone += 0.01
analog_deadzone = min(analog_deadzone, 1.0)
elif event.key in [pygame.locals.K_DOWN, BTN_B, ]:
analog_deadzone -= 0.01
analog_deadzone = max(analog_deadzone, 0.0)
elif event.type == pygame.KEYUP:
if event.key == pygame.locals.K_ESCAPE:
keepGoing = False # Quit
elif event.key in [pygame.locals.K_RIGHT, BTN_X, ]:
analog_deadzone += 0.01
analog_deadzone = min(analog_deadzone, 1.0)
elif event.key in [pygame.locals.K_LEFT, BTN_Y, ]:
analog_deadzone -= 0.01
analog_deadzone = max(analog_deadzone, 0.0)
# Joystick
if j:
if num_axes == 2:
axis_x, axis_y = 0, 0
jstick_str = ''
i = 0
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
axis_x = int(axisread * box_factor)
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = 'Axis %i reads %.2f' % (i, axisread)
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = bg.get_rect().bottom - textRect.height
if jstick_str:
bg.blit(text, textRect)
temp_y = textRect.centery
jstick_str = ''
i = 1
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
axis_y = int(axisread * box_factor)
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = 'Axis %i reads %.2f' % (i, axisread)
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = temp_y - textRect.height
if jstick_str:
bg.blit(text, textRect)
pygame.draw.rect(bg, RED, ((screen_centerx - 1) + axis_x, (screen_centery - 1) + axis_y, 3, 3))
text = font_text.render('Deadzone %.2f' % analog_deadzone, True, WHITE)
textRect = text.get_rect()
textRect.left = 5
textRect.top = 30
bg.blit(text, textRect)
else:
jstick_str = ''
for i in range(0, j.get_numaxes()):
axisread = j.get_axis(i)
if abs(axisread) > analog_deadzone:
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = jstick_str +'\n' + 'Axis %i reads %.2f' % (i, axisread)
for i in range(0, j.get_numbuttons()):
buttonread = j.get_button(i)
if buttonread != 0:
no_buttons_pressed = pygame.time.get_ticks()
jstick_str = jstick_str +'\n' + 'Button %i reads %i' % (i, buttonread)
if jstick_str:
rendered_text = render_textrect(jstick_str, font_text, my_rect, WHITE, surface=None)
if rendered_text:
bg.blit(rendered_text, my_rect.topleft)
else:
# no joystick
jstick_str = 'no joystick found'
text = font_text.render(jstick_str, True, WHITE)
textRect = text.get_rect()
textRect.centerx = bg.get_rect().centerx
textRect.centery = bg.get_rect().bottom - textRect.height
if jstick_str:
bg.blit(text, textRect)
bg.set_at((screen_centerx, screen_centery), WHITE) # draw single pixel dot at center
screen.blit(bg, (0, 0))
pygame.display.flip()
if not ESCAPE_IS_QUIT:
if time_to_quit <= 0:
keepGoing = False # Quit
pygame.key.set_repeat()
def dumb_system_id():
f = open('/proc/cpuinfo')
line = f.readline() # cheat, only read first line and expect it to be in order....
f.close()
if 'JZ4740' in line:
return a320
return papk3 # default
##########################################################################
def sinInterpolation(start, end, steps=30):
values = [start]
delta = end - start
for i in range(1, steps):
n = (pi / 2.0) * (i / float(steps - 1))
values.append(start + delta * sin(n))
return values
class RotatingMenu(object):
def __init__(self, x, y, radius, arc=pi * 2, defaultAngle=0, wrap=False):
"""
@param x:
The horizontal center of this menu in pixels.
@param y:
The vertical center of this menu in pixels.
@param radius:
The radius of this menu in pixels(note that this is the size of
the circular path in which the elements are placed, the actual
size of the menu may vary depending on item sizes.
@param arc:
The arc in radians which the menu covers. pi*2 is a full circle.
@param defaultAngle:
The angle at which the selected item is found.
@param wrap:
Whether the menu should select the first item after the last one
or stop.
"""
self.x = x
self.y = y
self.radius = radius
self.arc = arc
self.defaultAngle = defaultAngle
self.wrap = wrap
self.rotation = 0
self.rotationTarget = 0
self.rotationSteps = [] # Used for interpolation
self.items = []
self.selectedItem = None
self.selectedItemNumber = 0
def addItem(self, item):
self.items.append(item)
if len(self.items) == 1:
self.selectedItem = item
def selectItem(self, itemNumber):
if self.wrap == True:
if itemNumber > len(self.items) - 1:
itemNumber = 0
if itemNumber < 0:
itemNumber = len(self.items) - 1
else:
itemNumber = min(itemNumber, len(self.items) - 1)
itemNumber = max(itemNumber, 0)
self.selectedItem.deselect()
self.selectedItem = self.items[itemNumber]
self.selectedItem.select()
self.selectedItemNumber = itemNumber
self.rotationTarget = - self.arc * (itemNumber / float(len(self.items) - 1))
self.rotationSteps = sinInterpolation(self.rotation,
self.rotationTarget, 45)
def rotate(self, angle):
"""@param angle: The angle in radians by which the menu is rotated.
"""
for i in range(len(self.items)):
item = self.items[i]
n = i / float(len(self.items) - 1)
rot = self.defaultAngle + angle + self.arc * n
item.x = self.x + cos(rot) * self.radius
item.y = self.y + sin(rot) * self.radius
def update(self):
if len(self.rotationSteps) > 0:
self.rotation = self.rotationSteps.pop(0)
self.rotate(self.rotation)
def draw(self, display):
"""@param display: A pyGame display object
"""
for item in self.items:
item.draw(display)
class MenuItem(object):
def __init__(self, text):
self.text = text
self.defaultColor = (255, 255, 255)
self.selectedColor = (255, 0, 0)
self.color = self.defaultColor
self.x = 0
self.y = 0 # The menu will edit these
self.font = pygame.font.Font(None, 20)
self.image = self.font.render(self.text, True, self.color)
size = self.font.size(self.text)
self.xOffset = size[0] / 2
self.yOffset = size[1] / 2
def select(self):
"""Just visual stuff"""
self.color = self.selectedColor
self.redrawText()
def deselect(self):
"""Just visual stuff"""
self.color = self.defaultColor
self.redrawText()
def redrawText(self):
self.font = pygame.font.Font(None, 20)
self.image = self.font.render(self.text, True, self.color)
size = self.font.size(self.text)
self.xOffset = size[0] / 2
self.yOffset = size[1] / 2
def draw(self, display):
display.blit(self.image, (self.x - self.xOffset, self.y - self.yOffset))
##########################################################################
def doit(do_sound_test=False):
window_res = (480, 272) # FIXME use device res?
pygame.mixer.init()
for x in sound_buttons:
sound_buttons[x] = pygame.mixer.Sound(sound_buttons[x])
pygame.init()
# set up the screen/window
screen = pygame.display.set_mode(window_res)
pygame.mouse.set_visible(False)
pygame.display.set_caption("Hardware Test")
my_rect = screen.get_rect()
width = my_rect.width
height = my_rect.height
# set up fonts
font_text = pygame.font.SysFont(None, 20)
font_time = pygame.font.SysFont(None, 40)
clock = pygame.time.Clock()
try:
j = pygame.joystick.Joystick(0)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
except pygame.error:
j = None
#######################
fps_limit = 90
menu_mapping = [
('Button test', test_buttons),
('Analog test', test_analog1),
('gsensor test', test_analog2),
('Sound test', test_sound),
#('Mic test', test_mic),
('Exit', None),
]
menu = RotatingMenu(x=width / 2, y=height / 2, radius=(min(width, height) / 2) - 20, arc=pi, defaultAngle=pi / 2.0, wrap=True)
for i, menu_entry in enumerate(menu_mapping):
menu.addItem(MenuItem(menu_entry[0]))
menu.selectItem(0)
#test_buttons(clock, screen, font_time, font_text)
# Loop
while True:
# Handle events
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
elif event.key in [pygame.K_LEFT, pygame.K_UP]:
menu.selectItem(menu.selectedItemNumber + 1)
elif event.key in [pygame.K_RIGHT, pygame.K_DOWN]:
menu.selectItem(menu.selectedItemNumber - 1)
elif event.type == pygame.KEYUP:
if event.key in [pygame.K_LEFT, pygame.K_UP, pygame.K_RIGHT, pygame.K_DOWN, pygame.K_ESCAPE]:
pass
else:
menu_func = menu_mapping[menu.selectedItemNumber][1]
if menu_func:
menu_func(clock, screen, font_time, font_text, j)
menu.selectItem(menu.selectedItemNumber + 1)
else:
return False # Quit
# Update stuff
menu.update()
# Draw stuff
screen.fill((0, 0, 0))
menu.draw(screen)
pygame.display.flip() # Show the updated scene
clock.tick(fps_limit)
if j:
j.quit()
#pygame.image.save(screen, "screenshot.png")