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 *
 
   6 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)
 
  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)
 
  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
 
  27             self.assertEqual(dio.direction, Direction.INPUT)
 
  28             self.assertEqual(dio.pull, None)
 
  31 class TestDigitalInOutInteractive(unittest.TestCase):
 
  34         """LED blinks when proper attributes set"""
 
  36         from adafruit_blinka.agnostic import sleep
 
  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)
 
  48                 led.value = not(led.value)
 
  50             self.assertTrue(yes_no("Did LED wink twice"))
 
  52     def test_button_pull_up(self):
 
  54         """Pull-up button configured and detected"""
 
  55         with DigitalInOut(default_pin) as button:
 
  56             #button.direction = Direction.INPUT # implied
 
  59             except NotImplementedError as e:
 
  61                 return  # pull unsupported, test trivially passed
 
  62             except Exception as 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(await_true("button pressed", lambda: button.value == False))
 
  69     def test_button_pull_down(self):
 
  71         """Pull-down button configured and detected"""
 
  72         with DigitalInOut(default_pin) as button:
 
  73             #button.direction = Direction.INPUT # implied
 
  75                 button.pull = Pull.DOWN
 
  76             except NotImplementedError as e:
 
  78                 return  # pull unsupported, test trivially passed
 
  79             except Exception as e:
 
  81                 return  # pull unsupported, test trivially passed
 
  82             if (yes_no("Is Button wired from {} to VCC".format(default_pin))):
 
  83                 self.assertTrue(button.value == False)
 
  84                 self.assertTrue(await_true("button pressed", lambda: button.value == True))