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