Here’s a slightly more complex and complete example with inputs and cleanup:
#!/usr/bin/env python3
# This script will blink an LEDs attached to pins 32, 36, 38, and 40 and
# accept input from pins 35 and 37. The line number is the 'Linux#' from
# the Pin Map. Buttons must be pulled to ground. Use appropriate
# current limiting resistors with your LEDs.
# Pin 37 is you quit button.
import gpiod
import time
chip = gpiod.Chip('1') # set the chip number
linesOut = chip.get_lines([95, 81, 82, 83]) # set the line numbers
linesIn = chip.get_lines([86, 84])
# set direction and value
linesOut.request(consumer='spam', type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0, 0, 0, 0])
linesIn.request(consumer='spam', type=gpiod.LINE_REQ_DIR_IN, default_vals=[0, 0])
# Not sure what 'consumer' does, so I just set it to nonesense.
# The guide I followed did the likewise.
# define some functions for superior blinkage
# set_value uses the same order we set in linesOut, 1 = 3.3V, 0 = 0V
def chase():
linesOut.set_values([1, 0, 0, 0])
time.sleep(0.25)
linesOut.set_values([0, 1, 0, 0])
time.sleep(0.25)
linesOut.set_values([0, 0, 1, 0])
time.sleep(0.25)
linesOut.set_values([0, 0, 0, 1])
time.sleep(0.25)
def sideFlash():
linesOut.set_values([1, 1, 0, 0])
time.sleep(0.25)
linesOut.set_values([0, 0, 1, 1])
time.sleep(0.25)
linesOut.set_values([1, 1, 0, 0])
time.sleep(0.25)
linesOut.set_values([0, 0, 1, 1])
time.sleep(0.25)
try:
while True:
# quit if you press the big red button
if linesIn.get_values()[1]:
break
# Play one animation if pressed, the other if not.
if linesIn.get_values()[0]:
chase()
else:
sideFlash()
finally:
# Turn the lights off when you're not using them, just like Mom said.
linesOut.set_values([0, 0, 0, 0])
linesIn.release()
linesOut.release()
chip.close()
I know this isn’t the recommended way to handle inputs, and there is a way to enable internal pull up/down resistors, but that is beyond my skill level at the moment. Hope this is helpful, I know a few people were looking for blink and input examples.