You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from machine import Pin
  2. from adafruit_blinka.agnostic import board as boardId
  3. from adafruit_blinka import Enum, ContextManaged
  4. class DriveMode(Enum):
  5. PUSH_PULL = None
  6. OPEN_DRAIN = None
  7. DriveMode.PUSH_PULL = DriveMode()
  8. DriveMode.OPEN_DRAIN = DriveMode()
  9. class Direction(Enum):
  10. INPUT = None
  11. OUTPUT = None
  12. Direction.INPUT = Direction()
  13. Direction.OUTPUT = Direction()
  14. class Pull(Enum):
  15. UP = None
  16. DOWN = None
  17. #NONE=None
  18. Pull.UP = Pull()
  19. Pull.DOWN = Pull()
  20. #Pull.NONE = Pull()
  21. class DigitalInOut(ContextManaged):
  22. _pin = None
  23. def __init__(self, pin):
  24. self._pin = Pin(pin.id)
  25. self.direction = Direction.INPUT
  26. def switch_to_output(self, value=False, drive_mode=DriveMode.PUSH_PULL):
  27. self.direction = Direction.OUTPUT
  28. self.value = value
  29. self.drive_mode = drive_mode
  30. def switch_to_input(self, pull=None):
  31. self.direction = Direction.INPUT
  32. self.pull = pull
  33. def deinit(self):
  34. del self._pin
  35. @property
  36. def direction(self):
  37. return self.__direction
  38. @direction.setter
  39. def direction(self, dir):
  40. self.__direction = dir
  41. if dir is Direction.OUTPUT:
  42. self._pin.init(mode=Pin.OUT)
  43. self.value = False
  44. self.drive_mode = DriveMode.PUSH_PULL
  45. elif dir is Direction.INPUT:
  46. self._pin.init(mode=Pin.IN)
  47. self.pull = None
  48. else:
  49. raise AttributeError("Not a Direction")
  50. @property
  51. def value(self):
  52. return self._pin.value() is 1
  53. @value.setter
  54. def value(self, val):
  55. if self.direction is Direction.OUTPUT:
  56. self._pin.value(1 if val else 0)
  57. else:
  58. raise AttributeError("Not an output")
  59. @property
  60. def pull(self):
  61. if self.direction is Direction.INPUT:
  62. return self.__pull
  63. else:
  64. raise AttributeError("Not an input")
  65. @pull.setter
  66. def pull(self, pul):
  67. if self.direction is Direction.INPUT:
  68. self.__pull = pul
  69. if pul is Pull.UP:
  70. self._pin.init(mode=Pin.IN, pull=Pin.PULL_UP)
  71. elif pul is Pull.DOWN:
  72. if hasattr(Pin, "PULL_DOWN"):
  73. self._pin.init(mode=Pin.IN, pull=Pin.PULL_DOWN)
  74. else:
  75. raise NotImplementedError("{} unsupported on {}".format(
  76. Pull.DOWN, boardId))
  77. elif pul is None:
  78. self._pin.init(mode=Pin.IN, pull=None)
  79. else:
  80. raise AttributeError("Not a Pull")
  81. else:
  82. raise AttributeError("Not an input")
  83. @property
  84. def drive_mode(self):
  85. if self.direction is Direction.OUTPUT:
  86. return self.__drive_mode #
  87. else:
  88. raise AttributeError("Not an output")
  89. @drive_mode.setter
  90. def drive_mode(self, mod):
  91. self.__drive_mode = mod
  92. if mod is DriveMode.OPEN_DRAIN:
  93. self._pin.init(mode=Pin.OPEN_DRAIN)
  94. elif mod is DriveMode.PUSH_PULL:
  95. self._pin.init(mode=Pin.OUT)