Skip to content

Commit 38af3e1

Browse files
committed
Cleaned up lint. Updated README with instructions.
1 parent f895278 commit 38af3e1

File tree

10 files changed

+327
-327
lines changed

10 files changed

+327
-327
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ Connect the components as shown to your board. Note that you can use a 220 Ohm o
5959
.. image:: https://github.com/adafruit/Adafruit_CircuitPython_BoardTest/blob/master/docs/test_jig.png
6060
:alt: Test jig Fritzing diagram
6161

62-
To use each test, import the library, find the pins available on your board, and call ``boardtest_<test name>.run_test(pins)``. To run the GPIO test, for example:
62+
To use each test, copy the individual .py or .mpy test(s) into a folder named adafruit_boardtest in the CIRCUITPY drive, import the library, find the pins available on your board, and call ``boardtest_<test name>.run_test(pins)``. To run the GPIO test, for example:
6363

6464
.. code:: python
6565
6666
import board
67-
import boardtest_gpio
67+
from adafruit_boardtest import boardtest_gpio
6868
6969
# List out all the pins available to us
7070
pins = [p for p in dir(board)]
7171
print()
7272
print("All pins found:", end=' ')
73-
73+
7474
# Print pins
7575
for p in pins:
7676
print(p, end=' ')

adafruit_boardtest/boardtest_gpio.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Toggles all available GPIO on a board. Verify their operation with an LED,
2626
multimeter, another microcontroller, etc.
2727
28-
Run this script as its own main.py to individually run the test, or compile
28+
Run this script as its own main.py to individually run the test, or compile
2929
with mpy-cross and call from separate test script.
3030
3131
* Author(s): Shawn Hymel for Adafruit Industries
@@ -40,10 +40,11 @@
4040
4141
"""
4242

43+
import time
44+
4345
import board
4446
import digitalio
4547
import supervisor
46-
import time
4748

4849
__version__ = "0.0.0-auto.0"
4950
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
@@ -59,9 +60,9 @@
5960
NA = "N/A"
6061

6162
# Determine if given value is a number
62-
def _is_number(s):
63+
def _is_number(val):
6364
try:
64-
float(s)
65+
float(val)
6566
return True
6667
except ValueError:
6768
return False
@@ -73,9 +74,7 @@ def _deinit_pins(gpios):
7374

7475
# Toggle IO pins while waiting for answer
7576
def _toggle_wait(gpios):
76-
77-
global test_results
78-
77+
7978
timestamp = time.monotonic()
8079
led_state = False
8180
print("Are the pins listed above toggling? [y/n]")
@@ -92,21 +91,17 @@ def _toggle_wait(gpios):
9291
gpio.value = led_state
9392
if supervisor.runtime.serial_bytes_available:
9493
answer = input()
95-
if answer == 'y':
96-
return True
97-
else:
98-
return False
99-
break
94+
return bool(answer == 'y')
10095

10196
def run_test(pins):
10297

10398
"""
10499
Toggles all available GPIO on and off repeatedly.
105-
100+
106101
:param list[str] pins: list of pins to run the test on
107102
:return: tuple(str, list[str]): test result followed by list of pins tested
108103
"""
109-
104+
110105
# Create a list of analog GPIO pins
111106
analog_pins = [p for p in pins if p[0] == 'A' and _is_number(p[1])]
112107

@@ -116,14 +111,14 @@ def run_test(pins):
116111
# Toggle LEDs if we find any
117112
gpio_pins = analog_pins + digital_pins
118113
if gpio_pins:
119-
114+
120115
# Create a list of IO objects for us to toggle
121116
gpios = [digitalio.DigitalInOut(getattr(board, p)) for p in gpio_pins]
122117

123118
# Print out the LEDs found
124119
print("GPIO pins found:", end=' ')
125-
for p in gpio_pins:
126-
print(p, end=' ')
120+
for pin in gpio_pins:
121+
print(pin, end=' ')
127122
print('\n')
128123

129124
# Set all IO to output
@@ -132,29 +127,29 @@ def run_test(pins):
132127

133128
# Toggle pins while waiting for user to verify LEDs blinking
134129
result = _toggle_wait(gpios)
135-
130+
136131
# Release pins
137132
_deinit_pins(gpios)
138-
133+
139134
if result:
140135
return PASS, gpio_pins
141-
else:
142-
return FAIL, gpio_pins
143-
144-
else:
145-
print("No GPIO pins found")
146-
return NA, []
136+
137+
return FAIL, gpio_pins
138+
139+
# Else (no pins found)
140+
print("No GPIO pins found")
141+
return NA, []
147142

148143
def _main():
149-
144+
150145
# List out all the pins available to us
151146
pins = [p for p in dir(board)]
152147
print()
153148
print("All pins found:", end=' ')
154-
149+
155150
# Print pins
156-
for p in pins:
157-
print(p, end=' ')
151+
for pin in pins:
152+
print(pin, end=' ')
158153
print('\n')
159154

160155
# Run test

adafruit_boardtest/boardtest_i2c.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
====================================================
2525
Performs random writes and reads to I2C EEPROM.
2626
27-
Run this script as its own main.py to individually run the test, or compile
27+
Run this script as its own main.py to individually run the test, or compile
2828
with mpy-cross and call from separate test script.
2929
3030
* Author(s): Shawn Hymel for Adafruit Industries
@@ -34,7 +34,8 @@
3434
3535
**Hardware:**
3636
37-
* `Microchip AT24HC04B I2C EEPROM <https://www.digikey.com/product-detail/en/microchip-technology/AT24HC04B-PU/AT24HC04B-PU-ND/1886137>`_
37+
* `Microchip AT24HC04B I2C EEPROM <https://www.digikey.com/product-detail/en/\
38+
microchip-technology/AT24HC04B-PU/AT24HC04B-PU-ND/1886137>`_
3839
3940
**Software and Dependencies:**
4041
@@ -43,11 +44,12 @@
4344
4445
"""
4546

46-
import board
47-
import busio
4847
import random
4948
import time
5049

50+
import board
51+
import busio
52+
5153
__version__ = "0.0.0-auto.0"
5254
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
5355

@@ -66,60 +68,60 @@
6668
NA = "N/A"
6769

6870
# Open comms to I2C EEPROM by trying a write to memory address
69-
def _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout = 1.0):
70-
71+
def _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout=1.0):
72+
7173
# Try to access the I2C EEPROM (it becomes unresonsive during a write)
7274
timestamp = time.monotonic()
7375
while time.monotonic() < timestamp + timeout:
7476
try:
7577
i2c.writeto(i2c_addr, bytearray([mem_addr]), end=1, stop=False)
7678
return True
77-
except:
79+
except OSError:
7880
pass
79-
81+
8082
return False
8183

8284
# Write to address. Returns status (True for successful write, False otherwise)
83-
def _eeprom_i2c_write_byte(i2c, i2c_addr, mem_addr, mem_data, timeout = 1.0):
84-
85+
def _eeprom_i2c_write_byte(i2c, i2c_addr, mem_addr, mem_data):
86+
8587
# Make sure address is only one byte:
8688
if mem_addr > 255:
8789
return False
88-
90+
8991
# Make sure data is only one byte:
9092
if mem_data > 255:
9193
return False
92-
94+
9395
# Write data to memory at given address
9496
try:
9597
i2c.writeto(i2c_addr, bytearray([mem_addr, mem_data]))
96-
except:
98+
except OSError:
9799
return False
98-
100+
99101
return True
100102

101103
# Read from address. Returns tuple [status, result]
102-
def _eeprom_i2c_read_byte(i2c, i2c_addr, mem_addr, timeout = 1.0):
103-
104+
def _eeprom_i2c_read_byte(i2c, i2c_addr, mem_addr, timeout=1.0):
105+
104106
# Make sure address is only one byte:
105107
if mem_addr > 255:
106108
return False, bytearray()
107-
109+
108110
# Try writing to address (EEPROM is unresponsive while writing)
109-
if _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout) == False:
111+
if not _eeprom_i2c_wait(i2c, i2c_addr, mem_addr, timeout):
110112
return False, bytearray()
111-
113+
112114
# Finish the read
113115
buf = bytearray(1)
114116
i2c.readfrom_into(i2c_addr, buf)
115-
117+
116118
return True, buf
117119

118120
def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
119-
121+
120122
"""
121123
Performs random writes and reads to I2C EEPROM.
122-
124+
123125
:param list[str] pins: list of pins to run the test on
124126
:param str sda_pin: pin name of I2C SDA
125127
:param str scl_pin: pin name of I2C SCL
@@ -131,7 +133,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
131133

132134
# Tell user to connect EEPROM chip
133135
print("Connect a Microchip AT24HC04B EEPROM I2C chip. " +
134-
"Press enter to continue.")
136+
"Press enter to continue.")
135137
input()
136138

137139
# Set up I2C
@@ -143,7 +145,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
143145

144146
# Pick a random address, write to it, read from it, and see if they match
145147
pass_test = True
146-
for i in range(NUM_I2C_TESTS):
148+
for _ in range(NUM_I2C_TESTS):
147149

148150
# Randomly pick an address and a data value (one byte)
149151
mem_addr = random.randint(0, EEPROM_I2C_MAX_ADDR)
@@ -153,7 +155,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
153155

154156
# Try writing this random value to the random address
155157
result = _eeprom_i2c_write_byte(i2c, EEPROM_I2C_ADDR, mem_addr, mem_data)
156-
if result == False:
158+
if not result:
157159
print("FAIL: I2C could not communicate")
158160
pass_test = False
159161
break
@@ -162,7 +164,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
162164
result = _eeprom_i2c_read_byte(i2c, EEPROM_I2C_ADDR, mem_addr)
163165
print("Read:\t\t" + hex(result[1][0]))
164166
print()
165-
if result[0] == False:
167+
if not result[0]:
166168
print("FAIL: I2C could not communicate")
167169
pass_test = False
168170
break
@@ -179,23 +181,23 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
179181
# Store results
180182
if pass_test:
181183
return PASS, [sda_pin, scl_pin]
182-
else:
183-
return FAIL, [sda_pin, scl_pin]
184-
185-
else:
186-
print("No I2C pins found")
187-
return NA, []
184+
185+
return FAIL, [sda_pin, scl_pin]
186+
187+
# Else (no pins found)
188+
print("No I2C pins found")
189+
return NA, []
188190

189191
def _main():
190-
192+
191193
# List out all the pins available to us
192194
pins = [p for p in dir(board)]
193195
print()
194196
print("All pins found:", end=' ')
195-
197+
196198
# Print pins
197-
for p in pins:
198-
print(p, end=' ')
199+
for pin in pins:
200+
print(pin, end=' ')
199201
print('\n')
200202

201203
# Run test

0 commit comments

Comments
 (0)