1+ #!/usr/bin/env python
2+ #-----------------------------------------------------------------------------
3+ # qwiic_relay_ex2_quad.py
4+ #
5+ # Example that shows the basics of using the quad relays.
6+ #------------------------------------------------------------------------
7+ #
8+ # Written by SparkFun Electronics, November 2023
9+ #
10+ # This python library supports the SparkFun Electronics Qwiic sensor/board
11+ # ecosystem on Python compatible devices, such as the Raspberry Pi, MicroPython
12+ # and CircuitPython enabled microcontrollers, etc.
13+ #
14+ # More information on qwiic is at https://www.sparkfun.com/qwiic
15+ #
16+ # Do you like this library? Help support SparkFun. Buy a board!
17+ #
18+ #==================================================================================
19+ # Copyright (c) 2023 SparkFun Electronics
20+ #
21+ # Permission is hereby granted, free of charge, to any person obtaining a copy
22+ # of this software and associated documentation files (the "Software"), to deal
23+ # in the Software without restriction, including without limitation the rights
24+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+ # copies of the Software, and to permit persons to whom the Software is
26+ # furnished to do so, subject to the following conditions:
27+ #
28+ # The above copyright notice and this permission notice shall be included in all
29+ # copies or substantial portions of the Software.
30+ #
31+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+ # SOFTWARE.
38+ #==================================================================================
39+ # Example 3 - Quad
40+
41+ from __future__ import print_function
42+ import qwiic_relay
43+ import time
44+ import sys
45+
46+ # Be sure to initialize your relay with the proper address.
47+ myRelays = qwiic_relay .QwiicRelay (qwiic_relay .QUAD_RELAY_DEFUALT_ADDR )
48+ # myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_RELAY_JUMPER_CLOSE_ADDR)
49+ # myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_SOLID_STATE_RELAY_DEFUALT_ADDR)
50+ # myRelays = qwiic_relay.QwiicRelay(qwiic_relay.QUAD_SOLID_STATE_RELAY_JUMPER_CLOSE_ADDR)
51+
52+ def runExample ():
53+
54+ print ("\n SparkFun Qwiic Relay Example 3 - Quad\n " )
55+
56+ if myRelays .begin () == False :
57+ print ("The Qwiic Relay isn't connected to the system. Please check your connection" , \
58+ file = sys .stderr )
59+ return
60+
61+ # Turn on relays one and three
62+ myRelays .set_relay_on (1 )
63+ myRelays .set_relay_on (3 )
64+
65+ # Print the status of all relays
66+ print ("Relay 1 state: " + str (myRelays .get_relay_state (1 )))
67+ print ("Relay 2 state: " + str (myRelays .get_relay_state (2 )))
68+ print ("Relay 3 state: " + str (myRelays .get_relay_state (3 )))
69+ print ("Relay 4 state: " + str (myRelays .get_relay_state (4 )))
70+ print ()
71+
72+ # Wait a moment
73+ time .sleep (1 )
74+
75+ # Turn off 1 and 3, turn on 2 and 4
76+ myRelays .set_relay_off (1 )
77+ myRelays .set_relay_on (2 )
78+ myRelays .set_relay_off (3 )
79+ myRelays .set_relay_on (4 )
80+
81+ # Print the status of all relays
82+ print ("Relay 1 state: " + str (myRelays .get_relay_state (1 )))
83+ print ("Relay 2 state: " + str (myRelays .get_relay_state (2 )))
84+ print ("Relay 3 state: " + str (myRelays .get_relay_state (3 )))
85+ print ("Relay 4 state: " + str (myRelays .get_relay_state (4 )))
86+ print ()
87+
88+ # Wait a moment
89+ time .sleep (1 )
90+
91+ # Turn all relays on
92+ myRelays .set_all_relays_on ()
93+
94+ # Print the status of all relays
95+ print ("Relay 1 state: " + str (myRelays .get_relay_state (1 )))
96+ print ("Relay 2 state: " + str (myRelays .get_relay_state (2 )))
97+ print ("Relay 3 state: " + str (myRelays .get_relay_state (3 )))
98+ print ("Relay 4 state: " + str (myRelays .get_relay_state (4 )))
99+ print ()
100+
101+ # Wait a moment
102+ time .sleep (1 )
103+
104+ # Turn all relays off
105+ myRelays .set_all_relays_off ()
106+
107+ # Print the status of all relays
108+ print ("Relay 1 state: " + str (myRelays .get_relay_state (1 )))
109+ print ("Relay 2 state: " + str (myRelays .get_relay_state (2 )))
110+ print ("Relay 3 state: " + str (myRelays .get_relay_state (3 )))
111+ print ("Relay 4 state: " + str (myRelays .get_relay_state (4 )))
112+ print ()
113+
114+ if __name__ == '__main__' :
115+ try :
116+ runExample ()
117+ except (KeyboardInterrupt , SystemExit ) as exErr :
118+ print ("\n Ending Example 3" )
119+ sys .exit (0 )
0 commit comments