Skip to content

Commit 106face

Browse files
committed
fixed pylint issues in examples
1 parent 0e288a6 commit 106face

File tree

5 files changed

+53
-35
lines changed

5 files changed

+53
-35
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Coming Soon
1414
:alt: Build Status
1515

1616
CircuitPython library for Sparkfun Qwiic Single Relay. This library is ported from examples
17-
at `SparkFun Qwiic Single Relay <https://github.com/sparkfun/SparkFun_Qwiic_Releay>`_
17+
at `SparkFun Qwiic Relay <https://github.com/sparkfun/Qwiic_Relay>`_
1818

1919
.. image:: https://cdn.sparkfun.com//assets/parts/1/3/4/5/1/15093-SparkFun_Qwiic_Single_Relay-01.jpg
2020
:target: https://www.sparkfun.com/products/15093

examples/example2_change_i2c_address.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55

66
"""
77
Qwiic Relay Example 2 - example2_change_i2c_address.py
8-
Written by Gaston Williams, June 13th, 2019
8+
Written by Gaston Williams, June 19th, 2019
99
Based on Arduino code written by
10-
Wes Furuya @ SparkFun Electronics, February 5th, 2019
11-
The Qwiic Relay is a I2C controlled analog relay
10+
Kevin Kuwata @ SparkX, March 21, 2019
11+
The Qwiic Single Relay is a I2C controlled relay
1212
1313
Example 2 - Change I2C Address:
1414
This program uses the Qwiic Relay CircuitPython Library to change
1515
the I2C address for the device. You enter in the DEC value (8-119) or
16-
HEX value (0x08-0x77) for the new Relay address. After the address is
17-
You can run i2c_scanner.py to validate the address after the change.
16+
HEX value (0x08-0x77) for the new Relay address. After the i2c address
17+
is changed, you can run the example3_i2c_scanner.py program to validate
18+
the i2c address.
1819
1920
Syntax: python3 change_i2c_address.py [address]
20-
where address is an optional address value in decimal or hex
21+
where address is an optional current address value in decimal or hex
22+
2123
The default value for the address is 24 [0x18]
2224
"""
2325

examples/example3_i2c_scanner.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,66 @@
1010
1111
Example 3 - I2C Scanner
1212
This progam uses CircuitPython BusIO library to find the current
13-
address of the Qwiic Relay. It uses the I2C Scanner Example from
13+
address of the Qwiic Relay. It uses information from
1414
https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/i2c-devices
15+
to manually scan for the Qwiic Single Relay.
16+
17+
Since the Qwiic Single Relay responds to any byte read request after it's
18+
base address is written, i2c.scan()cannot be used.
19+
20+
This code manually scans addresses from 0x03 to 0x80, using only writes,
21+
not writes and reads,to find i2c devices.
1522
1623
The factory default address is 0x18.
1724
"""
1825

19-
import time
26+
from time import sleep
2027

2128
import board
2229
import busio
2330

2431
i2c = busio.I2C(board.SCL, board.SDA)
2532

26-
# For some reason i2c.scan() returns all addresses above the relay address
27-
# So we will manually look for the really address
33+
# Since i2c.scan() returns all addresses above the relay address,
34+
# we look for the relay address using only write requests.
2835

29-
def test_i2c_address(addr):
30-
"test an address to see if there's a device there"""
36+
def test_i2c_write(addr):
37+
"Write to an address to see if there's an device there"""
3138
while not i2c.try_lock():
3239
pass
3340

3441
try:
42+
# Make an empty write request to an address
3543
i2c.writeto(addr, b'')
44+
return True
45+
3646
except OSError:
37-
# some OS's dont like writing an empty bytesting...
38-
# Retry by reading a byte
39-
try:
40-
result = bytearray(1)
41-
i2c.readfrom_into(addr, result)
42-
except OSError:
43-
return False
44-
finally:
45-
i2c.unlock()
47+
return False
48+
49+
# Always unlock the i2c bus, before return
50+
finally:
51+
i2c.unlock()
4652

47-
return True
4853

54+
print('Press Ctrl-C to exit program')
4955

50-
addresses = []
56+
try:
57+
while True:
58+
found = []
5159

52-
for address in range(0x08, 0x80):
53-
if(test_i2c_address(address)):
54-
print('Found relay at address ' + hex(address))
55-
exit()
60+
# scan through all possible i2c addresses doi
61+
for address in range(0x03, 0x80):
62+
if(test_i2c_write(address)):
63+
found.append(address)
5664

57-
print('No I2C device found.')
65+
if(len(found) > 0):
66+
print('I2C addresses found:',
67+
[hex(device_address) for device_address in found])
68+
else:
69+
print('No I2C device found.')
5870

71+
# wait a bit and scan again
72+
sleep(5)
5973

74+
except KeyboardInterrupt:
75+
pass

examples/example4_get_relay_status.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
"""
77
Qwiic Relay Example 4 - example4_get_relay_status.py
8-
Written by Gaston Williams, June 13th, 2019
8+
Written by Gaston Williams, June 18th, 2019
99
Based on Arduino code written by
1010
Kevin Kuwata @ SparkX, March 21, 2018
1111
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
1212
1313
Example 4 - Get Relay Status:
1414
This program uses the Qwiic Relay CircuitPython Library to
15-
get the current status of the Qwiic Relay. The relay responds
15+
get the current status of the Qwiic Relay. The relay responds
1616
with a 1 for on and a 0 for off.
17-
17+
1818
Default Qwiic relay address is 0x18.
1919
"""
2020

examples/example5_get_firmware_version.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
1212
1313
Example 5 - Get Firmware Version:
14-
This program uses the Qwiic Relay CircuitPython Library to get the
14+
This program uses the Qwiic Relay CircuitPython Library to get the
1515
firmware version of Qwiic Single Relay breakout. If using version prior
16-
to (excluding) 1.0 the version number will be 25.5 or 26.5. Starting at
17-
version 1.0, the relay will respond with the correct firmware version.
16+
to version 1.0 the version number will be 25.5 or 26.5. Starting at
17+
version 1.0, the relay will respond with the correct firmware version.
1818
1919
Default Qwiic relay address is 0x18.
2020
"""

0 commit comments

Comments
 (0)