Skip to content

Commit 106b2e4

Browse files
committed
Added example 1 and simpletest
1 parent b5567d3 commit 106b2e4

File tree

4 files changed

+203
-14
lines changed

4 files changed

+203
-14
lines changed

README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Coming Soon
99
:target: https://discord.gg/nBQh6qu
1010
:alt: Discord
1111

12-
.. image:: https://travis-ci.com/fourstix/Sparkfun_CircuitPython_QwiicRelay.svg?branch=master
12+
.. image:: https://travis-ci.org/fourstix/Sparkfun_CircuitPython_QwiicRelay.svg?branch=master
1313
:target: https://travis-ci.com/fourstix/Sparkfun_CircuitPython_QwiicRelay
1414
:alt: Build Status
1515

16-
CircuitPython library for Sparkfun Qwiic Relay. This library is ported from examples
17-
at `SparkFun Qwiic Relay <https://github.com/sparkfun/SparkFun_Qwiic_Releay>`_
16+
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>`_
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
@@ -30,7 +30,7 @@ This driver depends on:
3030

3131
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
3232
* `Adafruit Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
33-
* `Qwiic Relay Hardware <https://github.com/sparkfun/Qwiic_Relay>`_
33+
* `Qwiic Single Relay Hardware <https://github.com/sparkfun/Qwiic_Relay>`_
3434

3535
Please ensure all dependencies are available on the CircuitPython filesystem.
3636
This is easily achieved by downloading
@@ -107,7 +107,7 @@ To install in a virtual environment in your current project:
107107
108108
Usage Example
109109
=============
110-
* `Qwiic Relay Hookup Guide <https://learn.sparkfun.com/tutorials/qwiic-single-relay-hookup-guide>`_ - The Arduino examples in the Hookup Guide are available for Python with this library
110+
* `Qwiic Single Relay Hookup Guide <https://learn.sparkfun.com/tutorials/qwiic-single-relay-hookup-guide>`_ - The Arduino examples in the Hookup Guide are available for Python with this library
111111
* `CircuitPython on a Raspberry Pi <https://learn.adafruit.com/circuitpython-on-raspberrypi-linux>`_ - Basic information on how to install CircuitPython on a Raspberry Pi.
112112
* Code Example:
113113

@@ -123,7 +123,7 @@ Usage Example
123123
relay = QwiicRelay(i2c) # default address is 0x20
124124
125125
# For a different address use QwiicRelay(i2c, address)
126-
# relay = QwiicJoystick(i2c, 0x21)
126+
# relay = QwiicRelay(i2c, 0x21)
127127
128128
129129
Contributing

examples/example1_basic_control.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is example is for the SparkFun Qwiic Single Relay.
2+
# SparkFun sells these at its website: www.sparkfun.com
3+
# Do you like this library? Help support SparkFun. Buy a board!
4+
# https://www.sparkfun.com/products/15093
5+
6+
"""
7+
Qwiic Relay Example 1 - example1_basic_control.py
8+
Written by Gaston Williams, June 13th, 2019
9+
Based on Arduino code written by
10+
Kevin Kuwata @ SparkX, March 21, 2018
11+
The Qwiic Single Relay is an I2C controlled relay produced by sparkfun
12+
13+
Example 1 - Basic Control:
14+
This program uses the Qwiic Relay CircuitPython Library to
15+
control the Qwiic Relay breakout over I2C and demonstrate
16+
basic functionality.
17+
"""
18+
19+
from time import sleep
20+
import board
21+
import busio
22+
import sparkfun_qwiicrelay
23+
24+
# Create bus object using our board's I2C port
25+
i2c = busio.I2C(board.SCL, board.SDA)
26+
27+
# Create relay object
28+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c)
29+
30+
print('Qwicc Relay Example 1 Basic Control')
31+
32+
# Check if connected
33+
if relay.connected:
34+
print('Relay connected.')
35+
else:
36+
print('Relay does not appear to be connected. Please check wiring.')
37+
exit()
38+
39+
print('Type Ctrl-C to exit program.')
40+
41+
try:
42+
while True:
43+
relay.on()
44+
sleep(2)
45+
relay.off()
46+
sleep(2)
47+
48+
except KeyboardInterrupt:
49+
pass

examples/qwiicrelay_simpletest.py

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

66
"""
77
Qwiic Relay Simple Test - qwiicrelay_simpletest.py
8-
Written by Gaston Williams, June 13th, 2019
9-
The Qwiic Relay is a I2C controlled analog relay
8+
Written by Gaston Williams, June 17th, 2019
9+
The Qwiic Single Relay is a I2C controlled relay
1010
1111
Simple Test:
12-
This program uses the Qwiic Relay CircuitPython Library to read
13-
and print out the relay position.
12+
This program uses the Qwiic Relay CircuitPython Library to toggle
13+
that status of the Qwiic Single Relay.
1414
"""
1515

16+
from time import sleep
1617
import board
1718
import busio
1819
import sparkfun_qwiicrelay
1920

2021
# Create bus object using our board's I2C port
2122
i2c = busio.I2C(board.SCL, board.SDA)
2223

23-
# Create relay object
24-
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c)
24+
# Create joystick object
25+
relay = sparkfun_qwiicrelay.Sparkfun_QwiicRelay(i2c, debug=True
26+
)
2527

2628
# Check if connected
2729
if relay.connected:
2830
print('Relay connected.')
2931
else:
3032
print('Relay does not appear to be connected. Please check wiring.')
33+
exit()
34+
35+
print('Relay status ', relay.status)
36+
37+
# Turn the relay on and off
38+
print('Press Ctrl-C to exit program')
39+
while True:
40+
relay.on()
41+
print('Relay status ', relay.status)
42+
sleep(2)
43+
relay.off()
44+
print('Relay status ', relay.status)
45+
sleep(2)

sparkfun_qwiicrelay.py

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
* Adafruit CircuitPython firmware for the supported boards:
4242
https://github.com/adafruit/circuitpython/releases
4343
44-
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
45-
4644
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4745
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
4846
"""
@@ -51,3 +49,130 @@
5149

5250
__version__ = "0.0.0-auto.0"
5351
__repo__ = "https://github.com/fourstix/Sparkfun_CircuitPython_QwiicRelay.git"
52+
53+
from time import sleep
54+
from micropython import const
55+
from adafruit_bus_device.i2c_device import I2CDevice
56+
57+
# public constants
58+
QWIIC_RELAY_ADDR = const(0x18) #default I2C Address
59+
60+
# private constants
61+
_RELAY_OFF = const(0x00)
62+
_RELAY_ON = const(0x01)
63+
64+
65+
66+
_RELAY_CHANGE_ADDRESS = const(0x03)
67+
_RELAY_VERSION = const(0x04)
68+
_RELAY_STATUS = const(0x05)
69+
_RELAY_NOTHING_NEW = const(0x99)
70+
71+
# class
72+
class Sparkfun_QwiicRelay:
73+
"""CircuitPython class for the Sparkfun QwiicRelay"""
74+
75+
def __init__(self, i2c, address=QWIIC_RELAY_ADDR, debug=False):
76+
"""Initialize Qwiic Relay for i2c communication."""
77+
self._device = I2CDevice(i2c, address)
78+
#save handle to i2c bus in case address is changed
79+
self._i2c = i2c
80+
self._debug = debug
81+
82+
# public properites
83+
84+
@property
85+
def connected(self):
86+
"""Check to see of the relay is available. Returns True if successful."""
87+
#Attempt a connection and see if we get an error
88+
try:
89+
with self._device as device:
90+
pass
91+
except ValueError:
92+
return False
93+
94+
return True
95+
96+
@property
97+
def version(self):
98+
"""Return the version string for the Relay firmware."""
99+
#send command to get two bytes for the version string
100+
version = self._command_read(_RELAY_VERSION, 2)
101+
# Compute major and minor values from 16-bit version
102+
minor = version[0] & 0xFF
103+
major = version[1] & 0xFF
104+
return 'v' + str(major) + '.' + str(minor)
105+
106+
@property
107+
def status(self):
108+
"""Return 1 if button pressed between reads. Button status is cleared."""
109+
#read button status (since last check)
110+
status = self._command_read(_RELAY_STATUS, 1)
111+
112+
return status[0] & 0xFF
113+
114+
115+
# public functions
116+
117+
def on(self):
118+
"""Turn the relay on."""
119+
self._command_write(_RELAY_ON)
120+
121+
122+
def off(self):
123+
"""Turn the relay off."""
124+
self._command_write(_RELAY_OFF)
125+
126+
def set_i2c_address(self, new_address):
127+
"""Change the i2c address of Relay snd return True if successful."""
128+
# check range of new address
129+
if (new_address < 8 or new_address > 119):
130+
print('ERROR: Address outside 8-119 range')
131+
return False
132+
# write magic number 0x13 to lock register, to unlock address for update
133+
# self._write_register(_RELAY_I2C_LOCK, 0x13)
134+
# write new address
135+
self._write_register(_RELAY_CHANGE_ADDRESS, new_address)
136+
137+
# wait a second for relay to settle after change
138+
sleep(1)
139+
140+
# try to re-create new i2c device at new address
141+
try:
142+
self._device = I2CDevice(self._i2c, new_address)
143+
except ValueError as err:
144+
print('Address Change Failure')
145+
print(err)
146+
return False
147+
148+
#if we made it here, everything went fine
149+
return True
150+
151+
# No i2c begin function is needed since I2Cdevice class takes care of that
152+
153+
# private functions
154+
155+
def _command_read(self, command, count):
156+
# Send a command then read count number of bytes.
157+
with self._device as device:
158+
device.write(bytes([command]), stop=False)
159+
result = bytearray(count)
160+
device.readinto(result)
161+
if self._debug:
162+
print("$%02X => %s" % (command, [hex(i) for i in result]))
163+
return result
164+
165+
def _command_write(self, command):
166+
# Send a byte command to the device
167+
with self._device as device:
168+
device.write(bytes([command & 0xFF]))
169+
if self._debug:
170+
print("$%02X" % (command))
171+
172+
def _write_register(self, addr, value):
173+
# Write a byte to the specified 8-bit register address
174+
with self._device as device:
175+
device.write(bytes([addr & 0xFF, value & 0xFF]))
176+
if self._debug:
177+
print("$%02X <= 0x%02X" % (addr, value))
178+

0 commit comments

Comments
 (0)