Created
September 4, 2024 03:35
-
-
Save magicwenli/b65a5bb597e22a74357d8b87d03474ed to your computer and use it in GitHub Desktop.
python3 control gpio with sysfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from time import sleep | |
import warnings | |
def make_wide(formatter, w=120, h=36): | |
"""Return a wider HelpFormatter, if possible.""" | |
try: | |
# https://stackoverflow.com/questions/5462873/ | |
# https://stackoverflow.com/a/5464440 | |
# beware: "Only the name of this class is considered a public API." | |
kwargs = {"width": w, "max_help_position": h} | |
formatter(None, **kwargs) | |
return lambda prog: formatter(prog, **kwargs) | |
except TypeError: | |
warnings.warn("argparse help formatter failed, falling back.") | |
return formatter | |
def parse_args(): | |
import argparse | |
parser = argparse.ArgumentParser( | |
description="Control GPIO pins on the uis7720 board", | |
formatter_class=make_wide(argparse.ArgumentDefaultsHelpFormatter, w=156, h=38), | |
) | |
parser.add_argument("gpio", type=int, help="The GPIO pin to control") | |
parser.add_argument( | |
"action", choices=["pulse", "blink", "on", "off"], help="The action to perform" | |
) | |
parser.add_argument( | |
"--pulse-time", | |
type=float, | |
default=1.0, | |
help="The time to pulse the GPIO pin for in seconds", | |
) | |
parser.add_argument( | |
"--pulse-from", | |
choices=["low", "high"], | |
default="low", | |
help="The starting state of the pulse", | |
) | |
parser.add_argument( | |
"--blink-time", | |
type=float, | |
default=1.0, | |
help="The time to blink the GPIO pin for in seconds", | |
) | |
return parser.parse_args() | |
def export_gpio(gpio): | |
try: | |
with open("/sys/class/gpio/export", "w") as f: | |
f.write(str(gpio)) | |
except Exception as e: | |
print("Error exporting GPIO: " + str(e)) | |
def unexport_gpio(gpio): | |
try: | |
with open("/sys/class/gpio/unexport", "w") as f: | |
f.write(str(gpio)) | |
except Exception as e: | |
print("Error unexporting GPIO: " + str(e)) | |
def direction_output(gpio, val): | |
try: | |
with open("/sys/class/gpio/gpio" + str(gpio) + "/direction", "w") as f: | |
f.write("out") | |
except Exception as e: | |
print("Error setting GPIO direction: " + str(e)) | |
try: | |
with open("/sys/class/gpio/gpio" + str(gpio) + "/value", "w") as f: | |
f.write(str(val)) | |
except Exception as e: | |
print("Error setting GPIO value: " + str(e)) | |
def direction_input(gpio): | |
try: | |
with open("/sys/class/gpio/gpio" + str(gpio) + "/direction", "w") as f: | |
f.write("in") | |
except Exception as e: | |
print("Error setting GPIO direction: " + str(e)) | |
def pulse_gpio(gpio, time, from_low): | |
direction_output(gpio, 0 if from_low else 1) | |
sleep(time) | |
direction_output(gpio, 1 if from_low else 0) | |
sleep(time) | |
direction_output(gpio, 0 if from_low else 1) | |
def blink_gpio(gpio, time): | |
while True: | |
direction_output(gpio, 1) | |
sleep(time) | |
direction_output(gpio, 0) | |
sleep(time) | |
def main(): | |
args = parse_args() | |
gpio = args.gpio | |
action = args.action | |
export_gpio(gpio) | |
if action == "pulse": | |
pulse_gpio(gpio, args.pulse_time, args.pulse_from == "low") | |
elif action == "blink": | |
blink_gpio(gpio, args.blink_time) | |
elif action == "on": | |
direction_output(gpio, 1) | |
elif action == "off": | |
direction_output(gpio, 0) | |
unexport_gpio(gpio) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment