Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # I think this is from @zmatt, i.e. circa '22.
- # I, Seth, have changed some of it...
- # Now, it is broken. I am slowly fixing it!
- import os
- from time import sleep
- from pathlib import Path
- class Gpio:
- def __init__(self, gpio, rw=None):
- if type(gpio) is int:
- # gpio number
- self.path = '/sys/class/gpio/' + str(gpio)
- else:
- # gpio name or path
- self.path = os.path.join('/sys/class/gpio/', gpio)
- if rw is not None and type(rw) is not bool:
- raise TypeError()
- if rw:
- Path(os.path.join(self.path, 'direction')).write_text('out')
- self._f = open(self.path + '/value', 'r+b', buffering=0)
- else:
- Path(os.path.join(self.path, 'direction')).write_text('out')
- self._f = open(self.path + '/value', 'rb', buffering=0)
- self.rw = rw
- @property
- def value(self):
- return (b'1', b'0').index(os.pread(self._f.fileno(), 1, 0))
- @value.setter
- def value(self, value):
- if self.rw is None:
- self._f = open(self.path + '/value', 'r+b', buffering=0)
- self.rw = True
- self._f.write((b'1', b'0')[value])
- ## example:
- Enable = Gpio('gpio31', True)
- Dir = Gpio('gpio60', True)
- Pulse = Gpio('gpio30', True)
- Enable.value = 1
- Pulse.value = 1
- try:
- racks = int(input("Please enter a 1 or 0: "))
- while True:
- if racks == 1:
- Enable.value = 0
- Dir.value = 0
- Pulse.value = 0
- sleep(3)
- print("Sleeping...")
- else:
- Enable.value = 0
- Dir.value = 1
- Pulse.value = 0
- sleep(3)
- print("Sleeping...")
- except KeyboardInterrupt:
- Enable.value = 1
- Pulse.value = 1
- print(" Trying to Care for the Behemoth!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement