Skip to content

Commit fa043d3

Browse files
refactor gpio driver and examples
1 parent 7981e74 commit fa043d3

8 files changed

+354
-241
lines changed

examples/qwiic_gpio_ex1.py renamed to examples/qwiic_gpio_ex1a_write.py

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env python
22
#-----------------------------------------------------------------------------
3-
# qwiic_gpio_ex1.py
3+
# qwiic_gpio_ex1a.py
44
#
5-
# Simple Example for the Qwiic GPIO Device, toggles GPIO 0 on and off.
5+
# This script sets up a GPIO 0 as an output and toggles
6+
# the output HIGH and LOW.
7+
#
8+
# This device sinks current. When an output is LOW then current will flow.
69
#------------------------------------------------------------------------
710
#
811
# Written by SparkFun Electronics, May 2019
@@ -36,10 +39,7 @@
3639
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3740
# SOFTWARE.
3841
#==================================================================================
39-
# Example 1
40-
#
4142

42-
from __future__ import print_function
4343
import qwiic_gpio
4444
import time
4545
import sys
@@ -55,41 +55,16 @@ def runExample():
5555
return
5656

5757
myGPIO.begin()
58-
myGPIO.mode_0 = myGPIO.GPIO_OUT
59-
myGPIO.mode_1 = myGPIO.GPIO_OUT
60-
myGPIO.mode_2 = myGPIO.GPIO_OUT
61-
myGPIO.mode_3 = myGPIO.GPIO_OUT
62-
myGPIO.mode_4 = myGPIO.GPIO_OUT
63-
myGPIO.mode_5 = myGPIO.GPIO_OUT
64-
myGPIO.mode_6 = myGPIO.GPIO_OUT
65-
myGPIO.mode_7 = myGPIO.GPIO_OUT
66-
myGPIO.setMode()
58+
myGPIO.pinMode(0, myGPIO.GPIO_OUT)
6759

6860
while True:
69-
myGPIO.out_status_0 = myGPIO.GPIO_HI
70-
myGPIO.out_status_1 = myGPIO.GPIO_HI
71-
myGPIO.out_status_2 = myGPIO.GPIO_HI
72-
myGPIO.out_status_3 = myGPIO.GPIO_HI
73-
myGPIO.out_status_4 = myGPIO.GPIO_HI
74-
myGPIO.out_status_5 = myGPIO.GPIO_HI
75-
myGPIO.out_status_6 = myGPIO.GPIO_HI
76-
myGPIO.out_status_7 = myGPIO.GPIO_HI
77-
myGPIO.setGPIO()
78-
print("set hi")
61+
myGPIO.digitalWrite(0, myGPIO.GPIO_HI)
62+
print("set pin 0 hi!")
7963
time.sleep(1)
80-
myGPIO.out_status_0 = myGPIO.GPIO_LO
81-
myGPIO.out_status_1 = myGPIO.GPIO_LO
82-
myGPIO.out_status_2 = myGPIO.GPIO_LO
83-
myGPIO.out_status_3 = myGPIO.GPIO_LO
84-
myGPIO.out_status_4 = myGPIO.GPIO_LO
85-
myGPIO.out_status_5 = myGPIO.GPIO_LO
86-
myGPIO.out_status_6 = myGPIO.GPIO_LO
87-
myGPIO.out_status_7 = myGPIO.GPIO_LO
88-
myGPIO.setGPIO()
89-
print("set lo")
64+
myGPIO.digitalWrite(0, myGPIO.GPIO_LO)
65+
print("set pin 0 lo!")
9066
time.sleep(1)
9167

92-
9368
if __name__ == '__main__':
9469
try:
9570
runExample()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
#-----------------------------------------------------------------------------
3+
# qwiic_gpio_ex1b.py
4+
#
5+
# This script flips all 8 outputs on and off every second.
6+
#------------------------------------------------------------------------
7+
#
8+
# Written by SparkFun Electronics, May 2019
9+
#
10+
# This python library supports the SparkFun Electroncis qwiic
11+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12+
# board computers.
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) 2019 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+
40+
import qwiic_gpio
41+
import time
42+
import sys
43+
44+
# This function is used to toggle the GPIO pin settings in a list of GPIO_HI or GPIO_LO
45+
def flipGPIO( gpioConfig ):
46+
for i in range(len(gpioConfig)):
47+
if gpioConfig[i] == qwiic_gpio.QwiicGPIO.GPIO_HI:
48+
gpioConfig[i] = qwiic_gpio.QwiicGPIO.GPIO_LO
49+
else:
50+
gpioConfig[i] = qwiic_gpio.QwiicGPIO.GPIO_HI
51+
52+
def runExample():
53+
54+
print("\nSparkFun Qwiic GPIO Example 1\n")
55+
myGPIO = qwiic_gpio.QwiicGPIO()
56+
57+
if myGPIO.isConnected() == False:
58+
print("The Qwiic GPIO isn't connected to the system. Please check your connection", \
59+
file=sys.stderr)
60+
return
61+
62+
myGPIO.begin()
63+
myGPIO.pinModePort( [ myGPIO.GPIO_OUT] * myGPIO.NUM_GPIO ) # Set all 8 pins to output
64+
65+
gpioConfig = [ myGPIO.GPIO_HI, myGPIO.GPIO_LO, myGPIO.GPIO_HI, myGPIO.GPIO_LO, myGPIO.GPIO_HI, myGPIO.GPIO_LO, myGPIO.GPIO_HI, myGPIO.GPIO_LO ]
66+
67+
while True:
68+
print("Writing new GPIO port outputs!")
69+
myGPIO.digitalWritePort( gpioConfig )
70+
flipGPIO( gpioConfig )
71+
time.sleep(1)
72+
73+
if __name__ == '__main__':
74+
try:
75+
runExample()
76+
except (KeyboardInterrupt, SystemExit) as exErr:
77+
print("\nEnding Example 1")
78+
sys.exit(0)
Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# qwiic_gpio_ex2.py
3+
# qwiic_gpio_ex2a.py
44
#
55

6-
# Simple Example for the Qwiic GPIO Device, reads every GPIO
6+
# This script allows the user to read a value from GPIO 0
77
# ------------------------------------------------------------------------
88
#
99
# Written by SparkFun Electronics, May 2019
@@ -37,18 +37,15 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939
# ==================================================================================
40-
# Example 1
41-
#
4240

43-
from __future__ import print_function
4441
import qwiic_gpio
4542
import time
4643
import sys
4744

4845

4946
def runExample():
5047

51-
print("\nSparkFun Qwiic GPIO Example 2\n")
48+
print("\nSparkFun Qwiic GPIO Example 2a\n")
5249
myGPIO = qwiic_gpio.QwiicGPIO()
5350

5451
if myGPIO.isConnected() == False:
@@ -57,39 +54,16 @@ def runExample():
5754
return
5855

5956
myGPIO.begin()
60-
myGPIO.mode_0 = myGPIO.GPIO_IN
61-
myGPIO.mode_1 = myGPIO.GPIO_IN
62-
myGPIO.mode_2 = myGPIO.GPIO_IN
63-
myGPIO.mode_3 = myGPIO.GPIO_IN
64-
myGPIO.mode_4 = myGPIO.GPIO_IN
65-
myGPIO.mode_5 = myGPIO.GPIO_IN
66-
myGPIO.mode_6 = myGPIO.GPIO_IN
67-
myGPIO.mode_7 = myGPIO.GPIO_IN
68-
myGPIO.setMode()
57+
myGPIO.pinMode(0, myGPIO.GPIO_IN)
6958

7059
while True:
71-
myGPIO.getGPIO() #This function updates each in_status_x variable
72-
print("GPIO 0:", end=" ")
73-
print(myGPIO.in_status_0, end=" ")
74-
print("GPIO 1:", end=" ")
75-
print(myGPIO.in_status_1, end=" ")
76-
print("GPIO 2:", end=" ")
77-
print(myGPIO.in_status_2, end=" ")
78-
print("GPIO 3:", end=" ")
79-
print(myGPIO.in_status_3, end=" ")
80-
print("GPIO 4:", end=" ")
81-
print(myGPIO.in_status_4, end=" ")
82-
print("GPIO 5:", end=" ")
83-
print(myGPIO.in_status_5, end=" ")
84-
print("GPIO 6:", end=" ")
85-
print(myGPIO.in_status_6, end=" ")
86-
print("GPIO 7:", end=" ")
87-
print(myGPIO.in_status_7)
60+
gpioState = myGPIO.digitalRead(0)
61+
print("HIGH" if gpioState == myGPIO.GPIO_HI else "LOW")
8862
time.sleep(.25)
8963

9064
if __name__ == '__main__':
9165
try:
9266
runExample()
9367
except (KeyboardInterrupt, SystemExit) as exErr:
94-
print("\nEnding Example 1")
68+
print("\nEnding Example 2a")
9569
sys.exit(0)
Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# qwiic_gpio_ex3.py
3+
# qwiic_gpio_ex2b.py
44
#
55

6-
# Simple Example for the Qwiic GPIO Device, reads every GPIO
6+
# This script allows the user to read the status of all 8 GPIO simultaneously
77
# ------------------------------------------------------------------------
88
#
99
# Written by SparkFun Electronics, May 2019
@@ -37,18 +37,15 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939
# ==================================================================================
40-
# Example 1
41-
#
4240

43-
from __future__ import print_function
4441
import qwiic_gpio
4542
import time
4643
import sys
4744

4845

4946
def runExample():
5047

51-
print("\nSparkFun Qwiic GPIO Example 3\n")
48+
print("\nSparkFun Qwiic GPIO Example 2b\n")
5249
myGPIO = qwiic_gpio.QwiicGPIO()
5350

5451
if myGPIO.isConnected() == False:
@@ -57,49 +54,19 @@ def runExample():
5754
return
5855

5956
myGPIO.begin()
60-
myGPIO.mode_0 = myGPIO.GPIO_IN
61-
myGPIO.mode_1 = myGPIO.GPIO_IN
62-
myGPIO.mode_2 = myGPIO.GPIO_IN
63-
myGPIO.mode_3 = myGPIO.GPIO_IN
64-
myGPIO.mode_4 = myGPIO.GPIO_IN
65-
myGPIO.mode_5 = myGPIO.GPIO_IN
66-
myGPIO.mode_6 = myGPIO.GPIO_IN
67-
myGPIO.mode_7 = myGPIO.GPIO_IN
68-
myGPIO.setMode()
69-
70-
myGPIO.inversion_0 = myGPIO.INVERT
71-
myGPIO.inversion_1 = myGPIO.NO_INVERT
72-
myGPIO.inversion_2 = myGPIO.INVERT
73-
myGPIO.inversion_3 = myGPIO.NO_INVERT
74-
myGPIO.inversion_4 = myGPIO.INVERT
75-
myGPIO.inversion_5 = myGPIO.NO_INVERT
76-
myGPIO.inversion_6 = myGPIO.INVERT
77-
myGPIO.inversion_7 = myGPIO.NO_INVERT
78-
myGPIO.setInversion()
57+
myGPIO.pinModePort( [myGPIO.GPIO_IN] * myGPIO.NUM_GPIO ) # pass in a list of 8 GPIO_IN values
7958

8059
while True:
81-
myGPIO.getGPIO() # This function updates each in_status_x variable
82-
print("GPIO 0:", end=" ")
83-
print(myGPIO.in_status_0, end=" ")
84-
print("GPIO 1:", end=" ")
85-
print(myGPIO.in_status_1, end=" ")
86-
print("GPIO 2:", end=" ")
87-
print(myGPIO.in_status_2, end=" ")
88-
print("GPIO 3:", end=" ")
89-
print(myGPIO.in_status_3, end=" ")
90-
print("GPIO 4:", end=" ")
91-
print(myGPIO.in_status_4, end=" ")
92-
print("GPIO 5:", end=" ")
93-
print(myGPIO.in_status_5, end=" ")
94-
print("GPIO 6:", end=" ")
95-
print(myGPIO.in_status_6, end=" ")
96-
print("GPIO 7:", end=" ")
97-
print(myGPIO.in_status_7)
60+
portValues = myGPIO.digitalReadPort()
61+
62+
for i in range(myGPIO.NUM_GPIO):
63+
print("GPIO " + str(i) + ": " + ("HIGH" if portValues[i] == myGPIO.GPIO_HI else "LOW") )
64+
9865
time.sleep(.25)
9966

10067
if __name__ == '__main__':
10168
try:
10269
runExample()
10370
except (KeyboardInterrupt, SystemExit) as exErr:
104-
print("\nEnding Example 1")
71+
print("\nEnding Example 2b")
10572
sys.exit(0)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# -----------------------------------------------------------------------------
3+
# qwiic_gpio_ex3a.py
4+
#
5+
6+
# This script shows how to invert the input signal on a pin
7+
# ------------------------------------------------------------------------
8+
#
9+
# Written by SparkFun Electronics, May 2019
10+
#
11+
# This python library supports the SparkFun Electroncis qwiic
12+
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
13+
# board computers.
14+
#
15+
# More information on qwiic is at https://www.sparkfun.com/qwiic
16+
#
17+
# Do you like this library? Help support SparkFun. Buy a board!
18+
#
19+
# ==================================================================================
20+
# Copyright (c) 2019 SparkFun Electronics
21+
#
22+
# Permission is hereby granted, free of charge, to any person obtaining a copy
23+
# of this software and associated documentation files (the "Software"), to deal
24+
# in the Software without restriction, including without limitation the rights
25+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26+
# copies of the Software, and to permit persons to whom the Software is
27+
# furnished to do so, subject to the following conditions:
28+
#
29+
# The above copyright notice and this permission notice shall be included in all
30+
# copies or substantial portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
# ==================================================================================
40+
41+
import qwiic_gpio
42+
import time
43+
import sys
44+
45+
46+
def runExample():
47+
48+
print("\nSparkFun Qwiic GPIO Example 3a\n")
49+
myGPIO = qwiic_gpio.QwiicGPIO()
50+
51+
if myGPIO.isConnected() == False:
52+
print("The Qwiic GPIO isn't connected to the system. Please check your connection",
53+
file=sys.stderr)
54+
return
55+
56+
myGPIO.begin()
57+
myGPIO.pinMode(0, myGPIO.GPIO_IN)
58+
59+
# Note that inversion of a pin only works on the input register.
60+
myGPIO.invertPin(0, myGPIO.INVERT)
61+
# myGPIO.invertPin(0, myGPIO.NO_INVERT); # Uncomment this line to remove the logic inversion on pin 0.
62+
63+
while True:
64+
logic_level = myGPIO.digitalRead(0)
65+
print("GPIO 0: " + ("HIGH" if logic_level == myGPIO.GPIO_HI else "LOW"))
66+
time.sleep(.25)
67+
68+
if __name__ == '__main__':
69+
try:
70+
runExample()
71+
except (KeyboardInterrupt, SystemExit) as exErr:
72+
print("\nEnding Example 3a")
73+
sys.exit(0)

0 commit comments

Comments
 (0)