Advertisement
silver2row

some_source_circa_22

Jun 3rd, 2024
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # I think this is from @zmatt, i.e. circa '22.
  4. # I, Seth, have changed some of it...
  5. # Now, it is broken. I am slowly fixing it!
  6.  
  7. import os
  8. from time import sleep
  9. from pathlib import Path
  10.  
  11. class Gpio:
  12.     def __init__(self, gpio, rw=None):
  13.         if type(gpio) is int:
  14.             # gpio number
  15.             self.path = '/sys/class/gpio/' + str(gpio)
  16.         else:
  17.             # gpio name or path
  18.             self.path = os.path.join('/sys/class/gpio/', gpio)
  19.  
  20.         if rw is not None and type(rw) is not bool:
  21.             raise TypeError()
  22.  
  23.         if rw:
  24.             Path(os.path.join(self.path, 'direction')).write_text('out')
  25.             self._f = open(self.path + '/value', 'r+b', buffering=0)
  26.         else:
  27.             Path(os.path.join(self.path, 'direction')).write_text('out')
  28.             self._f = open(self.path + '/value', 'rb', buffering=0)
  29.         self.rw = rw
  30.  
  31.     @property
  32.     def value(self):
  33.         return (b'1', b'0').index(os.pread(self._f.fileno(), 1, 0))
  34.  
  35.     @value.setter
  36.     def value(self, value):
  37.         if self.rw is None:
  38.             self._f = open(self.path + '/value', 'r+b', buffering=0)
  39.             self.rw = True
  40.  
  41.         self._f.write((b'1', b'0')[value])
  42.  
  43. ## example:
  44.  
  45. Enable = Gpio('gpio31', True)
  46. Dir    = Gpio('gpio60', True)
  47. Pulse  = Gpio('gpio30', True)
  48.  
  49. Enable.value = 1
  50. Pulse.value    = 1
  51.  
  52. try:
  53.     racks = int(input("Please enter a 1 or 0: "))
  54.     while True:
  55.         if racks == 1:
  56.             Enable.value = 0
  57.             Dir.value    = 0
  58.             Pulse.value  = 0
  59.             sleep(3)
  60.             print("Sleeping...")
  61.         else:
  62.             Enable.value = 0
  63.             Dir.value    = 1
  64.             Pulse.value  = 0
  65.             sleep(3)
  66.             print("Sleeping...")
  67.  
  68. except KeyboardInterrupt:
  69.     Enable.value = 1
  70.     Pulse.value  = 1
  71.     print(" Trying to Care for the Behemoth!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement