Advertisement
zmatt

trivial-gpio.py

Apr 23rd, 2021 (edited)
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # These examples assume /dev/gpio contains symlinks for sysfs gpios.
  2. #
  3. # Example udev rule:
  4. #
  5. #   SUBSYSTEM=="subsystem", KERNEL=="gpio", ACTION=="add", \
  6. #       RUN+="/bin/mkdir -p /dev/gpio"
  7. #
  8. #   SUBSYSTEM=="gpio", ACTION=="add", TEST=="value", ATTR{label}!="sysfs", \
  9. #       RUN+="/bin/ln -sT '/sys/class/gpio/%k' /dev/gpio/%s{label}"
  10. #
  11. # save as e.g. /etc/udev/rules.d/gpio-symlinks.rules
  12. # then "sudo update-initramfs -u" (unless you don't use initramfs) and reboot
  13. #
  14. #
  15. # This code is written to be simple and correct, not necessarily efficient.
  16. # Performance can probably be greatly improved by keeping a file descriptor
  17. # open for the 'value' attribute and using os.pread() and os.write().  This
  18. # would also be the first step towards supporting input change events.
  19. #
  20. # This also assumes that the direction (input/output) of gpios has already
  21. # been setup, e.g. in DT.  Attempting to set the value of an input will not
  22. # change it to output, it will simply fail.  For a slightly more elaborate
  23. # version of 'variant 1' that includes changing the direction of gpios and
  24. # also has more comments and error checking, see https://pastebin.com/cVHktYPn
  25.  
  26.  
  27. ##### variant 1
  28.  
  29. from pathlib import Path
  30.  
  31. def gpio_get_value( name ):
  32.     return int( Path('/dev/gpio', name, 'value').read_text() )
  33.  
  34. def gpio_set_value( name, value ):
  35.     Path('/dev/gpio', name, 'value').write_text( str(value) )
  36.  
  37. print( gpio_get_value( 'my-input' ) )
  38. print( gpio_get_value( 'my-output' ) )
  39. gpio_set_value( 'my-output', 1 )
  40.  
  41.  
  42.  
  43. ##### variant 2
  44.  
  45. from pathlib import Path
  46.  
  47. class Gpio:
  48.     def __init__( self, name ):
  49.         self.name = name
  50.         self._value_path = Path( '/dev/gpio', name, 'value' )
  51.  
  52.     def get_value( self ):
  53.         return int( self._value_path.read_text() )
  54.  
  55.     def set_value( self, value ):
  56.         self._value_path.write_text( str( value ) )
  57.  
  58. my_input = Gpio('my-input')
  59. my_output = Gpio('my-output')
  60.  
  61. print( my_input.get_value() )
  62. print( my_output.get_value() )
  63. my_output.set_value( 1 )
  64.  
  65.  
  66.  
  67. ##### variant 3
  68.  
  69. from pathlib import Path
  70.  
  71. class Gpio:
  72.     def __init__( self, name ):
  73.         self.name = name
  74.         self._value_path = Path( '/dev/gpio', name, 'value' )
  75.  
  76.     @property
  77.     def value( self ):
  78.         return int( self._value_path.read_text() )
  79.  
  80.     @value.setter
  81.     def value( self, value ):
  82.         self._value_path.write_text( str( value ) )
  83.  
  84. my_input = Gpio('my-input')
  85. my_output = Gpio('my-output')
  86.  
  87. print( my_input.value )
  88. print( my_output.value )
  89. my_output.value = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement