1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 
   3 # SPDX-License-Identifier: MIT
 
   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 *
 
  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)
 
  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)
 
  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
 
  30             self.assertEqual(dio.direction, Direction.INPUT)
 
  31             self.assertEqual(dio.pull, None)
 
  34 class TestDigitalInOutInteractive(unittest.TestCase):
 
  36         """LED blinks when proper attributes set"""
 
  38         from adafruit_blinka.agnostic import sleep
 
  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)
 
  51                 led.value = not (led.value)
 
  53             self.assertTrue(yes_no("Did LED wink twice"))
 
  55     def test_button_pull_up(self):
 
  57         """Pull-up button configured and detected"""
 
  58         with DigitalInOut(default_pin) as button:
 
  59             # button.direction = Direction.INPUT # implied
 
  62             except NotImplementedError as e:
 
  64                 return  # pull unsupported, test trivially passed
 
  65             except Exception as 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)
 
  71                     await_true("button pressed", lambda: button.value == False)
 
  74     def test_button_pull_down(self):
 
  76         """Pull-down button configured and detected"""
 
  77         with DigitalInOut(default_pin) as button:
 
  78             # button.direction = Direction.INPUT # implied
 
  80                 button.pull = Pull.DOWN
 
  81             except NotImplementedError as e:
 
  83                 return  # pull unsupported, test trivially passed
 
  84             except Exception as 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)
 
  90                     await_true("button pressed", lambda: button.value == True)