/ adafruit_boardtest / boardtest_led.py
boardtest_led.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_led` 24 ==================================================== 25 Toggles all available onboard LEDs. You will need to manually verify their 26 operation by watching them. 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 import time 43 44 import board 45 import digitalio 46 import supervisor 47 48 __version__ = "0.0.0-auto.0" 49 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git" 50 51 # Constants 52 LED_ON_DELAY_TIME = 0.2 # Seconds 53 LED_OFF_DELAY_TIME = 0.2 # Seconds 54 LED_PIN_NAMES = ["L", "LED", "RED_LED", "YELLOW_LED", "GREEN_LED", "BLUE_LED"] 55 56 # Test result strings 57 PASS = "PASS" 58 FAIL = "FAIL" 59 NA = "N/A" 60 61 # Toggle IO pins while waiting for answer 62 def _toggle_wait(led_pins): 63 timestamp = time.monotonic() 64 led_state = False 65 print("Are the pins listed above toggling? [y/n]") 66 while True: 67 68 # Cycle through each pin in the list 69 for pin in led_pins: 70 led = digitalio.DigitalInOut(getattr(board, pin)) 71 led.direction = digitalio.Direction.OUTPUT 72 blinking = True 73 74 # Blink each LED once while looking for input 75 while blinking: 76 if led_state: 77 if time.monotonic() > timestamp + LED_ON_DELAY_TIME: 78 led_state = False 79 led.value = led_state 80 led.deinit() 81 blinking = False 82 timestamp = time.monotonic() 83 else: 84 if time.monotonic() > timestamp + LED_OFF_DELAY_TIME: 85 led_state = True 86 led.value = led_state 87 timestamp = time.monotonic() 88 89 # Look for user input 90 if supervisor.runtime.serial_bytes_available: 91 answer = input() 92 if answer == "y": 93 return True 94 return False 95 96 97 def run_test(pins): 98 99 """ 100 Toggles the onboard LED(s) on and off. 101 102 :param list[str] pins: list of pins to run the test on 103 :return: tuple(str, list[str]): test result followed by list of pins tested 104 """ 105 106 # Look for pins with LED names 107 led_pins = list(set(pins).intersection(set(LED_PIN_NAMES))) 108 109 # Toggle LEDs if we find any 110 if led_pins: 111 112 # Print out the LEDs found 113 print("LEDs found:", end=" ") 114 for pin in led_pins: 115 print(pin, end=" ") 116 print("\n") 117 118 # Blink LEDs and wait for user to verify test 119 result = _toggle_wait(led_pins) 120 121 if result: 122 return PASS, led_pins 123 124 return FAIL, led_pins 125 126 # Else (no pins found) 127 print("No LED pins found") 128 return NA, [] 129 130 131 def _main(): 132 133 # List out all the pins available to us 134 pins = list(dir(board)) 135 print() 136 print("All pins found:", end=" ") 137 138 # Print pins 139 for pin in pins: 140 print(pin, end=" ") 141 print("\n") 142 143 # Run test 144 result = run_test(pins) 145 print() 146 print(result[0]) 147 print("Pins tested: " + str(result[1])) 148 149 150 # Execute only if run as main.py or code.py 151 if __name__ == "__main__": 152 _main()