wrapper.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of DigiLib.
  5. #
  6. # DigiLib is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # DigiLib is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with DigiLib. If not, see <http://www.gnu.org/licenses/>.
  18. lgpio = logging.getLogger(__name__)
  19. try:
  20. import RPi.gpio as _gpio
  21. OUT = _gpio.OUT
  22. IN = _gpio.IN
  23. BCM = _gpio.BCM
  24. except ImportError:
  25. lgpio.debug("failed to import RPi.gpio")
  26. _gpio = None
  27. OUT = "out"
  28. IN = "in"
  29. BCM = "bcm"
  30. _pins_for_cleanup = set()
  31. def cleanup(self,*args):
  32. """
  33. Calls _gpio.cleanup for every pin used. The module automatically registers this function to the `atexit` handler, so the user doesn't need to call it.
  34. """
  35. lgpio.debug("cleanup! ({})".format(args))
  36. if _gpio:
  37. # _gpio.cleanup wants a list or tuple, but _pins_for_cleanup is
  38. # a set. we have to convert it first.
  39. _gpio.cleanup(list(_pins_for_cleanup))
  40. def write(self,pins,state):
  41. """
  42. sets pin `pin` to `state`.
  43. Parameters
  44. ----------
  45. pins: list or int
  46. the pins which will be written to.
  47. state: int or float
  48. what will be written to the pins. float is only for analog pins.
  49. """
  50. lgpio.debug("setting pin(s) {} to value {}"
  51. .format(pins,state))
  52. if type(pins) is int:
  53. pins = [pins]
  54. _pins_for_cleanup.update(pins)
  55. if _gpio:
  56. _gpio.output(pins,state)
  57. def read(self,pins):
  58. """
  59. sets pin `pin` to `state`.
  60. Parameters
  61. ----------
  62. pins: list or int
  63. the pins which will be read from.
  64. Returns
  65. -------
  66. value: list
  67. the values read from the gpio pins in the same order as `pins`. if RPi.GPIO could not be imported, -1 is used instead of the pins actual value.
  68. """
  69. values = []
  70. for p in pins:
  71. if _gpio:
  72. values.append(_gpio.input(p))
  73. else:
  74. values.append(-1)
  75. lgpio.debug("reading pins {}: {}".format(
  76. pins,values))
  77. return values
  78. def configure():
  79. """
  80. Sets the gpio numbering mode to broadcom.
  81. """
  82. lgpio.debug("setting pin numbering to broadcom")
  83. atexit.register(cleanup)
  84. if _gpio:
  85. _gpio.setmode(_gpio.BCM)
  86. def setup(self,pins,value):
  87. """
  88. wrapper for ``gpio.setup()``. used to configure pins as input or output
  89. Parameters
  90. ----------
  91. pins: list
  92. the pins which will be configured.
  93. value: `IN` or `OUT`
  94. wether the pins will be configured as input or output.
  95. """
  96. lgpio.debug("setting pin(s) {} to {}".format(pins,value))
  97. if _gpio:
  98. _gpio.setup(pins,value)
  99. #