Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

158 linhas
4.0KB

  1. # -*- coding: utf-8 -*-
  2. """ base gpio actors """
  3. import time
  4. from modules import cbpi
  5. from modules.core.hardware import ActorBase, SensorPassive, SensorActive
  6. from modules.core.props import Property
  7. try:
  8. import RPi.GPIO as GPIO # pylint: disable=import-error
  9. GPIO.setmode(GPIO.BCM)
  10. except ImportError as exp:
  11. print(exp)
  12. @cbpi.actor
  13. class GPIOSimple(ActorBase):
  14. """
  15. Simple GPIO Actor
  16. """
  17. gpio = Property.Select("GPIO",
  18. options=[
  19. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  20. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  21. 26, 27
  22. ],
  23. description="GPIO to which the actor is connected")
  24. def init(self):
  25. GPIO.setup(int(self.gpio), GPIO.OUT)
  26. GPIO.output(int(self.gpio), 0)
  27. def on(self, power=0):
  28. print(("GPIO ON %s" % str(self.gpio)))
  29. GPIO.output(int(self.gpio), 1)
  30. def off(self):
  31. print("GPIO OFF")
  32. GPIO.output(int(self.gpio), 0)
  33. @cbpi.actor
  34. class GPIOPWM(ActorBase):
  35. """
  36. GPIO Actor with PWM support
  37. """
  38. gpio = Property.Select("GPIO",
  39. options=[
  40. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  41. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  42. 26, 27
  43. ],
  44. description="GPIO to which the actor is connected")
  45. frequency = Property.Number("Frequency (Hz)", configurable=True)
  46. gpio_inst = None
  47. power = 100 # duty cycle
  48. def init(self):
  49. GPIO.setup(int(self.gpio), GPIO.OUT)
  50. GPIO.output(int(self.gpio), 0)
  51. def on(self, power=None):
  52. if power is not None:
  53. self.power = int(power)
  54. if self.frequency is None:
  55. self.frequency = 0.5 # 2 sec
  56. if self.gpio_inst is None:
  57. self.gpio_inst = GPIO.PWM(int(self.gpio), float(self.frequency))
  58. self.gpio_inst.start(int(self.power))
  59. def set_power(self, power):
  60. '''
  61. Optional: Set the power of your actor
  62. :param power: int value between 0 - 100
  63. :return:
  64. '''
  65. if self.gpio_inst:
  66. if power is not None:
  67. self.power = int(power)
  68. self.gpio_inst.ChangeDutyCycle(self.power)
  69. def off(self):
  70. print("GPIO OFF")
  71. self.gpio_inst.stop()
  72. @cbpi.actor
  73. class RelayBoard(ActorBase):
  74. """
  75. Relay board Actor
  76. """
  77. gpio = Property.Select("GPIO",
  78. options=[
  79. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  80. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  81. 26, 27
  82. ],
  83. description="GPIO to which the actor is connected")
  84. def init(self):
  85. GPIO.setup(int(self.gpio), GPIO.OUT)
  86. GPIO.output(int(self.gpio), 1)
  87. def on(self, power=0):
  88. GPIO.output(int(self.gpio), 0)
  89. def off(self):
  90. GPIO.output(int(self.gpio), 1)
  91. @cbpi.actor
  92. class Dummy(ActorBase):
  93. """
  94. Simple Dummy Actor
  95. """
  96. def on(self, power=100):
  97. '''
  98. Code to switch on the actor
  99. :param power: int value between 0 - 100
  100. :return:
  101. '''
  102. print("ON")
  103. def off(self):
  104. print("OFF")
  105. @cbpi.actor
  106. class DummyPWM(ActorBase):
  107. """
  108. Dummy Actor with PWM support
  109. """
  110. power = 100
  111. def on(self, power=100):
  112. '''
  113. Code to switch on the actor
  114. :param power: int value between 0 - 100
  115. :return:
  116. '''
  117. self.power = int(power) if power is not None else 100
  118. print("DummyPWM ON %s" % self.power)
  119. def off(self):
  120. self.power = 100
  121. print("OFF")
  122. def set_power(self, power):
  123. self.power = int(power)
  124. print("DummyPWM POWER %s" % self.power)