Skip to content

Commit

Permalink
Add comments to all GPIO Zero examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuftii committed Apr 8, 2019
1 parent f448319 commit 44ace1d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
17 changes: 17 additions & 0 deletions button_led_piezo.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
# Import the necessary libraries.
# gpiozero contains our JamHat object.
from gpiozero import JamHat
from time import sleep

# Initialise the JamHat object.
jh = JamHat()

# Build an array of notes in hertz.
NOTES = [440.000, 391.995, 349.228, 329.628, 293.665, 261.626]

# Initialise our counters for the while loop below.
i = 0
j = 0
note = 0

# Setup a try/except block so we can run until CTRL+C is pressed.
try:
while True:
if(jh.button_2.is_pressed):
# If the second button is pressed, increment the counter into our NOTES array.
note = (note + 1) % 6
# Using modular arithmetic, decrease the light counter.
# Our lights are in a matrix with rows 0-1 and columns 0-2.
# Eg. [0][1] is the top yellow LED.
if(j == 2):
# If we're at the end of the row, increment to the next row.
i = (i + 1) % 2
# Increment the column.
j = (j + 1) % 3
sleep(0.1)
# Turn off the hat
jh.off()
# Turn on the next light in the i/jth slot.
jh[i][j].on()

if(jh.button_1.is_pressed):
# If the button is pressed, play the current note from NOTES out of the buzzer.
jh.buzzer.play(NOTES[note])
sleep(0.1)

except KeyboardInterrupt:
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere.
jh.close()
10 changes: 9 additions & 1 deletion button_piezo.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# Import the necessary libraries.
# gpiozero contains our JamHat object.
from gpiozero import JamHat
from time import sleep

# Initialise the JamHat object.
jh = JamHat()

# Setup a try/except block so we can run until CTRL+C is pressed.
try:
while True:
if(jh.button_1.is_pressed):
# If the button on the left is pressed, beep midi note 80.
jh.buzzer.play(80)
elif(jh.button_2.is_pressed):
# If the button on the right is pressed, beep midi note 60.
jh.buzzer.play(60)
else:
# Turn off the buzzer.
jh.off()
except:
except KeyboardInterrupt:
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere.
jh.close()
14 changes: 13 additions & 1 deletion buttons.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
# Import the necessary libraries.
# gpiozero contains our JamHat object.
from gpiozero import JamHat
from time import sleep

# Initialise the JamHat object.
jh = JamHat()

# Setup a try/except block so we can run until CTRL+C is pressed.
try:
# Initialise counter
counter = 0
while True:
if jh.button_1.is_pressed or jh.button_2.is_pressed:
# If either button is pressed...
if jh.button_1.is_pressed:
# If the left button is pressed, increment the counter.
counter += 1
# Wait until the button is released before moving on.
jh.button_1.wait_for_release()
if jh.button_2.is_pressed:
# If the right button is pressed, increment the counter.
counter -= 1
# Wait until the button is released before moving on.
jh.button_2.wait_for_release()
# Print out the counter value to the screen.
print("Counter: %d" % counter)
sleep(0.1)
except:
except KeyboardInterrupt:
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere.
jh.close()
17 changes: 16 additions & 1 deletion led_ants.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
# Import the necessary libraries.
# gpiozero contains our JamHat object.
from gpiozero import JamHat
from time import sleep

# Initialise the JamHat object.
jh = JamHat()

# i is the counter for the LED row.
i = 0
# j is the counter for the LED column.
j = 0

# Setup a try/except block so we can run until CTRL+C is pressed.
try:
while True:
# Using modular arithmetic, decrease the light counter.
# Our lights are in a matrix with rows 0-1 and columns 0-2.
# Eg. [0][1] is the top yellow LED.
if(j == 2):
# If we're at the end of the column, increment to the next row.
i = (i + 1) % 2
# Increment the column.
j = (j + 1) % 3
sleep(0.2)
# Turn the hat off.
jh.off()
# Turn on the LED at i j on the board.
jh[i][j].on()

except:
except KeyboardInterrupt:
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere.
jh.close()

0 comments on commit 44ace1d

Please sign in to comment.