/ adafruit_boardtest / boardtest_gpio.py
boardtest_gpio.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2018 Shawn Hymel for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_boardtest.boardtest_gpio`
 24  ====================================================
 25  Toggles all available GPIO on a board. Verify their operation with an LED,
 26  multimeter, another microcontroller, etc.
 27  
 28  Run this script as its own main.py to individually run the test, or compile
 29  with mpy-cross and call from separate test script.
 30  
 31  * Author(s): Shawn Hymel for Adafruit Industries
 32  
 33  Implementation Notes
 34  --------------------
 35  
 36  **Software and Dependencies:**
 37  
 38  * Adafruit CircuitPython firmware for the supported boards:
 39    https://github.com/adafruit/circuitpython/releases
 40  
 41  """
 42  
 43  import time
 44  
 45  import board
 46  import digitalio
 47  import supervisor
 48  
 49  __version__ = "0.0.0-auto.0"
 50  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
 51  
 52  # Constants
 53  LED_ON_DELAY_TIME = 0.2  # Seconds
 54  LED_OFF_DELAY_TIME = 0.2  # Seconds
 55  LED_PIN_NAMES = ["L", "LED", "RED_LED", "GREEN_LED", "BLUE_LED"]
 56  
 57  # Test result strings
 58  PASS = "PASS"
 59  FAIL = "FAIL"
 60  NA = "N/A"
 61  
 62  # Determine if given value is a number
 63  def _is_number(val):
 64      try:
 65          float(val)
 66          return True
 67      except ValueError:
 68          return False
 69  
 70  
 71  # Release pins
 72  def _deinit_pins(gpios):
 73      for g in gpios:
 74          g.deinit()
 75  
 76  
 77  # Toggle IO pins while waiting for answer
 78  def _toggle_wait(gpios):
 79  
 80      timestamp = time.monotonic()
 81      led_state = False
 82      print("Are the pins listed above toggling? [y/n]")
 83      while True:
 84          if led_state:
 85              if time.monotonic() > timestamp + LED_ON_DELAY_TIME:
 86                  led_state = False
 87                  timestamp = time.monotonic()
 88          else:
 89              if time.monotonic() > timestamp + LED_OFF_DELAY_TIME:
 90                  led_state = True
 91                  timestamp = time.monotonic()
 92          for gpio in gpios:
 93              gpio.value = led_state
 94          if supervisor.runtime.serial_bytes_available:
 95              answer = input()
 96              return bool(answer == "y")
 97  
 98  
 99  def run_test(pins):
100  
101      """
102      Toggles all available GPIO on and off repeatedly.
103  
104      :param list[str] pins: list of pins to run the test on
105      :return: tuple(str, list[str]): test result followed by list of pins tested
106      """
107  
108      # Create a list of analog GPIO pins
109      analog_pins = [p for p in pins if p[0] == "A" and _is_number(p[1])]
110  
111      # Create a list of digital GPIO
112      digital_pins = [p for p in pins if p[0] == "D" and _is_number(p[1])]
113  
114      # Toggle LEDs if we find any
115      gpio_pins = analog_pins + digital_pins
116      if gpio_pins:
117  
118          # Create a list of IO objects for us to toggle
119          gpios = [digitalio.DigitalInOut(getattr(board, p)) for p in gpio_pins]
120  
121          # Print out the LEDs found
122          print("GPIO pins found:", end=" ")
123          for pin in gpio_pins:
124              print(pin, end=" ")
125          print("\n")
126  
127          # Set all IO to output
128          for gpio in gpios:
129              gpio.direction = digitalio.Direction.OUTPUT
130  
131          # Toggle pins while waiting for user to verify LEDs blinking
132          result = _toggle_wait(gpios)
133  
134          # Release pins
135          _deinit_pins(gpios)
136  
137          if result:
138              return PASS, gpio_pins
139  
140          return FAIL, gpio_pins
141  
142      # Else (no pins found)
143      print("No GPIO pins found")
144      return NA, []
145  
146  
147  def _main():
148  
149      # List out all the pins available to us
150      pins = list(dir(board))
151      print()
152      print("All pins found:", end=" ")
153  
154      # Print pins
155      for pin in pins:
156          print(pin, end=" ")
157      print("\n")
158  
159      # Run test
160      result = run_test(pins)
161      print()
162      print(result[0])
163      print("Pins tested: " + str(result[1]))
164  
165  
166  # Execute only if run as main.py or code.py
167  if __name__ == "__main__":
168      _main()