-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_testAll.py
More file actions
1678 lines (1336 loc) · 49 KB
/
_testAll.py
File metadata and controls
1678 lines (1336 loc) · 49 KB
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 python3
"""Test script for JoyPiNoteBetterLib components.
This script tests all hardware components and their methods to verify
functionality. Each component is tested in isolation with visual/interactive
feedback where possible.
Usage:
python testAll.py [component]
Components: all, led, lcd, scrollinglcd, seg7, button, joystick, touch, tilt,
buzzer, pwmbuzzer, vibrator, pwmvibrator, humtemp, sound, light,
ultrasonic, nfc, servo, stepmotor, relay, clear, shared, imports
Default: all
"""
import sys
import time
# Test results tracking
_testResults: dict = {"passed": 0, "failed": 0, "skipped": 0}
doSoundTest = True
def printHeader(title: str) -> None:
"""Print a formatted section header."""
print(f"\n{'=' * 50}")
print(f" {title}")
print(f"{'=' * 50}")
def printTest(name: str, passed: bool, message: str = "") -> None:
"""Print test result and update counters."""
status = "✓ PASS" if passed else "✗ FAIL"
_testResults["passed" if passed else "failed"] += 1
msg = f" - {message}" if message else ""
print(f" {status}: {name}{msg}")
def printSkip(name: str, reason: str = "") -> None:
"""Print skipped test."""
_testResults["skipped"] += 1
msg = f" - {reason}" if reason else ""
print(f" ⊘ SKIP: {name}{msg}")
def testImports() -> bool:
"""Test that all modules can be imported."""
printHeader("Testing Imports")
allPassed = True
try:
from JoyPiNoteBetterLib import LedMatrix
printTest("Import LedMatrix", True)
except ImportError as e:
printTest("Import LedMatrix", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import LcdDisplay
printTest("Import LcdDisplay", True)
except ImportError as e:
printTest("Import LcdDisplay", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Seg7x4
printTest("Import Seg7x4", True)
except ImportError as e:
printTest("Import Seg7x4", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import ButtonMatrix
printTest("Import ButtonMatrix", True)
except ImportError as e:
printTest("Import ButtonMatrix", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Direction, Joystick
printTest("Import Joystick, Direction", True)
except ImportError as e:
printTest("Import Joystick, Direction", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import TouchSensor
printTest("Import TouchSensor", True)
except ImportError as e:
printTest("Import TouchSensor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import TiltSensor
printTest("Import TiltSensor", True)
except ImportError as e:
printTest("Import TiltSensor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib.Shared.SharedSpi import getSharedSpi, releaseSharedSpi
printTest("Import SharedSpi functions", True)
except ImportError as e:
printTest("Import SharedSpi functions", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Buzzer
printTest("Import Buzzer", True)
except ImportError as e:
printTest("Import Buzzer", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Vibrator
printTest("Import Vibrator", True)
except ImportError as e:
printTest("Import Vibrator", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import HumidityTemperatureSensor
printTest("Import HumidityTemperatureSensor", True)
except ImportError as e:
printTest("Import HumidityTemperatureSensor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import SoundSensor
printTest("Import SoundSensor", True)
except ImportError as e:
printTest("Import SoundSensor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import LightSensor
printTest("Import LightSensor", True)
except ImportError as e:
printTest("Import LightSensor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import UltrasonicSensor
printTest("Import UltrasonicSensor", True)
except ImportError as e:
printTest("Import UltrasonicSensor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import NfcReader
printTest("Import NfcReader", True)
except ImportError as e:
printTest("Import NfcReader", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import PwmBuzzer
printTest("Import PwmBuzzer", True)
except ImportError as e:
printTest("Import PwmBuzzer", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import PwmVibrator
printTest("Import PwmVibrator", True)
except ImportError as e:
printTest("Import PwmVibrator", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Servomotor
printTest("Import Servo", True)
except ImportError as e:
printTest("Import Servo", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Stepmotor
printTest("Import Stepmotor", True)
except ImportError as e:
printTest("Import Stepmotor", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import ModuleReset
printTest("Import ModuleReset", True)
except ImportError as e:
printTest("Import ModuleReset", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import Relay
printTest("Import Relay", True)
except ImportError as e:
printTest("Import Relay", False, str(e))
allPassed = False
try:
from JoyPiNoteBetterLib import ScrollingLinesLcd
printTest("Import ScrollingLinesLcd", True)
except ImportError as e:
printTest("Import ScrollingLinesLcd", False, str(e))
allPassed = False
return allPassed
def testRelay() -> None:
"""Test Relay component."""
printHeader("Testing Relay")
try:
from JoyPiNoteBetterLib import Relay
# Test instantiation
relay = Relay(pin=23)
printTest("Relay instantiation", True)
try:
relay.turnOn()
time.sleep(0.2)
printTest("turnOn", True)
except Exception as e:
printTest("turnOn", False, str(e))
try:
relay.turnOff()
printTest("turnOff", True)
except Exception as e:
printTest("turnOff", False, str(e))
except Exception as e:
printTest("Relay initialization", False, str(e))
def testScrollingLinesLcd() -> None:
"""Test ScrollingLinesLcd component."""
printHeader("Testing ScrollingLinesLcd")
try:
from JoyPiNoteBetterLib import LcdDisplay, ScrollingLinesLcd
# Test instantiation with existing LcdDisplay
lcd = LcdDisplay(cols=16, rows=2)
scroller = ScrollingLinesLcd(lcdDisplay=lcd)
printTest("ScrollingLinesLcd instantiation (with LCD)", True)
# Test instantiation without LcdDisplay (creates its own)
try:
scroller2 = ScrollingLinesLcd()
printTest("ScrollingLinesLcd instantiation (auto LCD)", True)
scroller2.stop()
except Exception as e:
printTest("ScrollingLinesLcd instantiation (auto LCD)", False, str(e))
# Test show method
try:
scroller.show(["Hello", "World", "Test"], line=0, delay=0.5)
printTest("show (line 0)", True)
except Exception as e:
printTest("show (line 0)", False, str(e))
# Test show on second line
try:
scroller.show(["Line2A", "Line2B"], line=1)
printTest("show (line 1)", True)
except Exception as e:
printTest("show (line 1)", False, str(e))
# Let it scroll for a bit
time.sleep(1.5)
# Test stop method
try:
scroller.stop(clearMessages=True)
printTest("stop (with clear)", True)
except Exception as e:
printTest("stop (with clear)", False, str(e))
# Test start method
try:
scroller.lineMessages["0"] = ["Restart", "Test"]
scroller.start()
time.sleep(0.5)
scroller.stop()
printTest("start", True)
except Exception as e:
printTest("start", False, str(e))
# Clean up
lcd.clear()
lcd.displayMessage("Scroller done!", line=0)
except Exception as e:
printTest("ScrollingLinesLcd initialization", False, str(e))
def testLedMatrix() -> None:
"""Test LedMatrix component."""
printHeader("Testing LedMatrix")
try:
from JoyPiNoteBetterLib import LedMatrix
# Test instantiation
matrix = LedMatrix(brightness=50)
printTest("LedMatrix instantiation", True)
# Test setBrightness
try:
matrix.setBrightness(100)
printTest("setBrightness(100)", True)
except Exception as e:
printTest("setBrightness(100)", False, str(e))
# Test setBrightness validation
try:
matrix.setBrightness(300) # Should raise ValueError
printTest(
"setBrightness validation", False, "Should have raised ValueError"
)
except ValueError:
printTest(
"setBrightness validation", True, "Correctly rejected invalid value"
)
except Exception as e:
printTest("setBrightness validation", False, str(e))
# Test setPixel
try:
matrix.setPixel(0, (255, 0, 0))
matrix.setPixel(63, (0, 255, 0))
printTest("setPixel", True)
except Exception as e:
printTest("setPixel", False, str(e))
# Test setPixel validation
try:
matrix.setPixel(100, (255, 0, 0)) # Should raise ValueError
printTest("setPixel validation", False, "Should have raised ValueError")
except ValueError:
printTest(
"setPixel validation", True, "Correctly rejected invalid position"
)
except Exception as e:
printTest("setPixel validation", False, str(e))
# Test setAll
try:
matrix.setAll((0, 0, 255))
printTest("setAll", True)
except Exception as e:
printTest("setAll", False, str(e))
# Test update
try:
matrix.update()
printTest("update", True)
except Exception as e:
printTest("update", False, str(e))
# Test clean
try:
matrix.clear()
printTest("clean", True)
except Exception as e:
printTest("clean", False, str(e))
# Test showChar
try:
matrix.showChar("A", (255, 255, 0))
time.sleep(0.3)
printTest("showChar", True)
except Exception as e:
printTest("showChar", False, str(e))
# Test showText
try:
matrix.showText("Hi", (0, 255, 255))
time.sleep(0.3)
printTest("showText", True)
except Exception as e:
printTest("showText", False, str(e))
# Test scrollText (short)
try:
matrix.scrollText("AB", (255, 0, 255), delay=0.05, loops=1)
printTest("scrollText", True)
except Exception as e:
printTest("scrollText", False, str(e))
# Clean up
matrix.clear()
except Exception as e:
printTest("LedMatrix initialization", False, str(e))
def testLcdDisplay() -> None:
"""Test LcdDisplay component."""
printHeader("Testing LcdDisplay")
try:
from JoyPiNoteBetterLib import LcdDisplay
# Test instantiation
lcd = LcdDisplay(cols=16, rows=2)
printTest("LcdDisplay instantiation", True)
# Test clear
try:
lcd.clear()
printTest("clear", True)
except Exception as e:
printTest("clear", False, str(e))
# Test displayMessage
try:
lcd.displayMessage("Test Line 1", line=0)
lcd.displayMessage("Test Line 2", line=1)
printTest("displayMessage", True)
except Exception as e:
printTest("displayMessage", False, str(e))
time.sleep(0.5)
# Test setBacklight
try:
lcd.setBacklight(False)
time.sleep(0.2)
lcd.setBacklight(True)
printTest("setBacklight", True)
except Exception as e:
printTest("setBacklight", False, str(e))
# Test setDisplay
try:
lcd.setDisplay(False)
time.sleep(0.2)
lcd.setDisplay(True)
printTest("setDisplay", True)
except Exception as e:
printTest("setDisplay", False, str(e))
# Test setCursor
try:
lcd.setCursor(True)
time.sleep(0.2)
lcd.setCursor(False)
printTest("setCursor", True)
except Exception as e:
printTest("setCursor", False, str(e))
# Test setBlink
try:
lcd.setBlink(True)
time.sleep(0.3)
lcd.setBlink(False)
printTest("setBlink", True)
except Exception as e:
printTest("setBlink", False, str(e))
# Test setCursorPosition
try:
lcd.setCursorPosition(0, 0)
printTest("setCursorPosition", True)
except Exception as e:
printTest("setCursorPosition", False, str(e))
# Test setCursorPosition validation
try:
lcd.setCursorPosition(100, 100)
printTest(
"setCursorPosition validation", False, "Should have raised ValueError"
)
except ValueError:
printTest(
"setCursorPosition validation",
True,
"Correctly rejected invalid position",
)
except Exception as e:
printTest("setCursorPosition validation", False, str(e))
# Test setColumnAlign
try:
lcd.setColumnAlign(True)
lcd.setColumnAlign(False)
printTest("setColumnAlign", True)
except Exception as e:
printTest("setColumnAlign", False, str(e))
# Test setDirection
try:
lcd.setDirection(False) # Left to right
printTest("setDirection", True)
except Exception as e:
printTest("setDirection", False, str(e))
# Test wordWrap property
try:
lcd.wordWrap = True
lcd.displayMessage("This is a longer text that should wrap")
time.sleep(0.5)
lcd.wordWrap = False
printTest("wordWrap", True)
except Exception as e:
printTest("wordWrap", False, str(e))
# Clean up
lcd.clear()
lcd.displayMessage("Tests complete!", line=0)
except Exception as e:
printTest("LcdDisplay initialization", False, str(e))
def testSeg7x4() -> None:
"""Test Seg7x4 component."""
printHeader("Testing Seg7x4")
try:
from JoyPiNoteBetterLib import Seg7x4
# Test instantiation
seg = Seg7x4()
printTest("Seg7x4 instantiation", True)
# Test clear
try:
seg.clear()
seg.update()
printTest("clear", True)
except Exception as e:
printTest("clear", False, str(e))
# Test setFull with integer
try:
seg.setFull(1234)
printTest("setFull(int)", True)
except Exception as e:
printTest("setFull(int)", False, str(e))
time.sleep(0.5)
# Test setFull with float
try:
seg.setFull(12.34, decimal=2)
printTest("setFull(float)", True)
except Exception as e:
printTest("setFull(float)", False, str(e))
time.sleep(0.5)
# Test setFull with string
try:
seg.setFull("AbCd")
printTest("setFull(str)", True)
except Exception as e:
printTest("setFull(str)", False, str(e))
time.sleep(0.5)
# Test set single position
try:
seg.clear()
seg.set(0, 1)
seg.set(1, 2)
seg.set(2, 3)
seg.set(3, 4)
seg.update()
printTest("set(position, value)", True)
except Exception as e:
printTest("set(position, value)", False, str(e))
time.sleep(0.5)
# Test set validation
try:
seg.set(10, 1) # Should raise ValueError
printTest("set validation", False, "Should have raised ValueError")
except ValueError:
printTest("set validation", True, "Correctly rejected invalid position")
except Exception as e:
printTest("set validation", False, str(e))
# Test setBrightness
try:
seg.setBrightness(0.6)
printTest("setBrightness", True)
except Exception as e:
printTest("setBrightness", False, str(e))
# Test setBlinkRate
try:
seg.setBlinkRate(1)
time.sleep(0.5)
seg.setBlinkRate(0)
printTest("setBlinkRate", True)
except Exception as e:
printTest("setBlinkRate", False, str(e))
# Test showColon
try:
seg.setFull("12:34")
seg.showColon()
seg.update()
printTest("showColon", True)
except Exception as e:
printTest("showColon", False, str(e))
time.sleep(0.5)
# Test setDigitRaw
try:
seg.clear()
seg.setDigitRaw(0, 0x3F) # Display "0"
seg.setDigitRaw(1, 0x06) # Display "1"
seg.update()
printTest("setDigitRaw", True)
except Exception as e:
printTest("setDigitRaw", False, str(e))
# Test setDigitRaw validation
try:
seg.setDigitRaw(10, 0x3F) # Should raise ValueError
printTest("setDigitRaw validation", False, "Should have raised ValueError")
except ValueError:
printTest(
"setDigitRaw validation", True, "Correctly rejected invalid index"
)
except Exception as e:
printTest("setDigitRaw validation", False, str(e))
# Clean up
time.sleep(0.5)
seg.clear()
seg.update()
except Exception as e:
printTest("Seg7x4 initialization", False, str(e))
def testButtonMatrix() -> None:
"""Test ButtonMatrix component."""
printHeader("Testing ButtonMatrix")
try:
from JoyPiNoteBetterLib import ButtonMatrix
# Test instantiation
buttons = ButtonMatrix(keyChannel=4)
printTest("ButtonMatrix instantiation", True)
# Test readChannel
try:
value = buttons.readChannel(4)
printTest("readChannel", True, f"Value: {value}")
except Exception as e:
printTest("readChannel", False, str(e))
# Test getAdcValue
try:
value = buttons.getAdcValue()
printTest("getAdcValue", True, f"Value: {value}")
except Exception as e:
printTest("getAdcValue", False, str(e))
# Test getPressedKey
try:
key = buttons.getPressedKey()
adcVal = buttons.getAdcValue()
if key is None:
printTest("getPressedKey", True, f"No key pressed (ADC: {adcVal})")
else:
printTest(
"getPressedKey",
True,
f"Key: {key} (ADC: {adcVal}) - Note: unexpected if not pressing",
)
except Exception as e:
printTest("getPressedKey", False, str(e))
# Interactive test hint
print("\n ℹ Interactive test: Press a button within 3 seconds...")
startTime = time.time()
keyPressed = None
while time.time() - startTime < 3:
key = buttons.getPressedKey()
if key is not None:
keyPressed = key
break
time.sleep(0.05)
if keyPressed is not None:
printTest("Interactive button press", True, f"Button {keyPressed} detected")
else:
printSkip("Interactive button press", "No button pressed (timeout)")
except Exception as e:
printTest("ButtonMatrix initialization", False, str(e))
def testJoystick() -> None:
"""Test Joystick component."""
printHeader("Testing Joystick")
try:
from JoyPiNoteBetterLib import Direction, Joystick
# Test instantiation
joy = Joystick(xChannel=1, yChannel=0)
printTest("Joystick instantiation", True)
# Test Direction enum
try:
_ = Direction.CENTER
_ = Direction.N
_ = Direction.NE
printTest("Direction enum", True)
except Exception as e:
printTest("Direction enum", False, str(e))
# Test getX
try:
x = joy.getX()
printTest("getX", True, f"Value: {x}")
except Exception as e:
printTest("getX", False, str(e))
# Test getY
try:
y = joy.getY()
printTest("getY", True, f"Value: {y}")
except Exception as e:
printTest("getY", False, str(e))
# Test getXY
try:
x, y = joy.getXY()
# Values should be near 512 when centered
if 400 < x < 600 and 400 < y < 600:
printTest("getXY", True, f"Values: ({x}, {y}) - centered")
elif x == 0 and y == 0:
printTest(
"getXY", False, f"Values: ({x}, {y}) - likely SPI buffer issue"
)
else:
printTest("getXY", True, f"Values: ({x}, {y}) - joystick moved?")
except Exception as e:
printTest("getXY", False, str(e))
# Test getDirection (4 directions)
try:
x, y = joy.getXY()
direction = joy.getDirection(do8Directions=False, threshold=100)
if direction == Direction.CENTER:
printTest(
"getDirection (4-dir)",
True,
f"Direction: {direction.name} (X:{x}, Y:{y})",
)
else:
printTest(
"getDirection (4-dir)",
True,
f"Direction: {direction.name} (X:{x}, Y:{y}) - Note: expected CENTER if idle",
)
except Exception as e:
printTest("getDirection (4-dir)", False, str(e))
# Test getDirection (8 directions)
try:
direction = joy.getDirection(do8Directions=True, threshold=100)
printTest("getDirection (8-dir)", True, f"Direction: {direction.name}")
except Exception as e:
printTest("getDirection (8-dir)", False, str(e))
# Interactive test hint
print("\n ℹ Interactive test: Move joystick within 3 seconds...")
startTime = time.time()
directionMoved = None
while time.time() - startTime < 3:
direction = joy.getDirection(do8Directions=True, threshold=100)
if direction != Direction.CENTER:
directionMoved = direction
break
time.sleep(0.05)
if directionMoved is not None:
printTest(
"Interactive joystick move", True, f"Direction: {directionMoved.name}"
)
else:
printSkip("Interactive joystick move", "No movement detected (timeout)")
except Exception as e:
printTest("Joystick initialization", False, str(e))
def testTouch() -> None:
"""Test Touch component."""
printHeader("Testing Touch")
try:
from JoyPiNoteBetterLib import TouchSensor
# Test instantiation
touch = TouchSensor(pin=17)
printTest("Touch instantiation", True)
# Test isTouched
try:
touched = touch.isTouched()
printTest("isTouched", True, f"State: {touched}")
except Exception as e:
printTest("isTouched", False, str(e))
# Interactive test hint
print("\n ℹ Interactive test: Touch the sensor within 3 seconds...")
startTime = time.time()
touchDetected = False
while time.time() - startTime < 3:
if touch.isTouched():
touchDetected = True
break
time.sleep(0.05)
if touchDetected:
printTest("Interactive touch detection", True, "Touch detected")
else:
printSkip("Interactive touch detection", "No touch detected (timeout)")
# Note: waitForTouch is blocking, so we skip it in automated tests
printSkip("waitForTouch", "Blocking method - skipped in automated test")
except Exception as e:
printTest("Touch initialization", False, str(e))
def testTilt() -> None:
"""Test TiltSensor component."""
printHeader("Testing TiltSensor")
try:
from JoyPiNoteBetterLib import TiltSensor
# Test instantiation
tilt = TiltSensor(pin=22)
printTest("TiltSensor instantiation", True)
# Test isTilted
try:
tilted = tilt.isTilted()
printTest("isTilted", True, f"State: {tilted}")
except Exception as e:
printTest("isTilted", False, str(e))
# Interactive test hint
print("\n ℹ Interactive test: Tilt the sensor within 3 seconds...")
startTime = time.time()
tiltDetected = False
while time.time() - startTime < 3:
if tilt.isTilted():
tiltDetected = True
break
time.sleep(0.05)
if tiltDetected:
printTest("Interactive tilt detection", True, "Tilt detected")
else:
printSkip("Interactive tilt detection", "No tilt detected (timeout)")
except Exception as e:
printTest("TiltSensor initialization", False, str(e))
def testSharedResources() -> None:
"""Test shared SPI and I2C resources."""
printHeader("Testing Shared Resources")
# Test SharedSpi
try:
from JoyPiNoteBetterLib.Shared.SharedSpi import getSharedSpi, releaseSharedSpi
spi1 = getSharedSpi()
spi2 = getSharedSpi()
# Both should return the same instance
if spi1 is spi2:
printTest("SharedSpi singleton", True, "Same instance returned")
else:
printTest("SharedSpi singleton", False, "Different instances returned")
releaseSharedSpi()
releaseSharedSpi()
printTest("SharedSpi release", True)
except Exception as e:
printTest("SharedSpi", False, str(e))
# Test shared I2C
try:
from JoyPiNoteBetterLib.Shared.SharedI2C import getSharedI2C, releaseSharedI2C
i2c1 = getSharedI2C()
i2c2 = getSharedI2C()
if i2c1 is i2c2:
printTest("SharedI2C singleton", True, "Same instance returned")
else:
printTest("SharedI2C singleton", False, "Different instances returned")
releaseSharedI2C()
releaseSharedI2C()
printTest("SharedI2C release", True)
except Exception as e:
printTest("SharedI2C", False, str(e))
def testBuzzer() -> None:
"""Test Buzzer and PwmBuzzer components."""
printHeader("Testing Buzzer")
if not doSoundTest:
printSkip("Buzzer tests", "Sound tests disabled")
return
try:
from JoyPiNoteBetterLib import Buzzer
# Test instantiation
buzzer = Buzzer(pin=18)
printTest("Buzzer instantiation", True)
# Test setBuzz (activate)
try:
buzzer.setBuzz(True)
time.sleep(0.1)
buzzer.setBuzz(False)
printTest("setBuzz", True)
except Exception as e:
printTest("setBuzz", False, str(e))
# Test buzz with duration
try:
buzzer.buzz(duration=0.1)
printTest("buzz(duration)", True)
except Exception as e:
printTest("buzz(duration)", False, str(e))
# Test stop
try:
buzzer.stop()
printTest("stop", True)
except Exception as e:
printTest("stop", False, str(e))
except Exception as e:
printTest("Buzzer initialization", False, str(e))
def testVibrator() -> None:
"""Test Vibrator component."""
printHeader("Testing Vibrator")
if not doSoundTest:
printSkip("Vibrator tests", "Sound tests disabled")
return
try: