]> Repositories - Adafruit_Blinka-hackapet.git/blob - test/src/testing/universal/digitalio.py
Untangle code and remove pylint disables
[Adafruit_Blinka-hackapet.git] / test / src / testing / universal / digitalio.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 import unittest
5 from testing import yes_no, await_true
6 from testing.board import led_pin, default_pin, led_hardwired, led_inverted
7 from digitalio import *
8
9
10 class TestDigitalInOut(unittest.TestCase):
11     def test_default(self):
12         """DigitalInOut is input with no pull when constructed"""
13         with DigitalInOut(default_pin) as dio:
14             self.assertEqual(dio.direction, Direction.INPUT)
15             self.assertEqual(dio.pull, None)
16
17     def test_switch_to_output(self):
18         """Default configuration of switch_to_output is respected"""
19         with DigitalInOut(default_pin) as dio:
20             dio.switch_to_output()
21             self.assertEqual(dio.direction, Direction.OUTPUT)
22             self.assertEqual(dio.value, False)
23             self.assertEqual(dio.drive_mode, DriveMode.PUSH_PULL)
24
25     def test_switch_to_input(self):
26         """Default configuration of switch_to_input is respected"""
27         with DigitalInOut(default_pin) as dio:
28             dio.switch_to_output()  # starts as input anyway
29             dio.switch_to_input()
30             self.assertEqual(dio.direction, Direction.INPUT)
31             self.assertEqual(dio.pull, None)
32
33
34 class TestDigitalInOutInteractive(unittest.TestCase):
35     def test_blink(self):
36         """LED blinks when proper attributes set"""
37         print()
38         from adafruit_blinka.agnostic import sleep
39
40         if not (led_hardwired) and not (yes_no("Is LED wired to {}".format(led_pin))):
41             return  # test trivially passed
42         with DigitalInOut(led_pin) as led:
43             led.direction = Direction.OUTPUT
44             # should now be OUT, PUSH_PULL, value=0, and LED should light
45             led.value = False if led_inverted else True
46             self.assertTrue(yes_no("Is LED lit"))
47             print("Winking LED...")
48             for count in range(2):
49                 led.value = not (led.value)
50                 sleep(0.5)
51                 led.value = not (led.value)
52                 sleep(0.5)
53             self.assertTrue(yes_no("Did LED wink twice"))
54
55     def test_button_pull_up(self):
56         print()
57         """Pull-up button configured and detected"""
58         with DigitalInOut(default_pin) as button:
59             # button.direction = Direction.INPUT # implied
60             try:
61                 button.pull = Pull.UP
62             except NotImplementedError as e:
63                 print(e)
64                 return  # pull unsupported, test trivially passed
65             except Exception as e:
66                 print(e)
67                 return  # pull unsupported, test trivially passed
68             if yes_no("Is Button wired from {} to GND".format(default_pin)):
69                 self.assertTrue(button.value == True)
70                 self.assertTrue(
71                     await_true("button pressed", lambda: button.value == False)
72                 )
73
74     def test_button_pull_down(self):
75         print()
76         """Pull-down button configured and detected"""
77         with DigitalInOut(default_pin) as button:
78             # button.direction = Direction.INPUT # implied
79             try:
80                 button.pull = Pull.DOWN
81             except NotImplementedError as e:
82                 print(e)
83                 return  # pull unsupported, test trivially passed
84             except Exception as e:
85                 print(e)
86                 return  # pull unsupported, test trivially passed
87             if yes_no("Is Button wired from {} to VCC".format(default_pin)):
88                 self.assertTrue(button.value == False)
89                 self.assertTrue(
90                     await_true("button pressed", lambda: button.value == True)
91                 )