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.

157 line
3.9KB

  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 ModuleNotFoundError 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. self.gpio_inst = GPIO.PWM(int(self.gpio), float(self.frequency))
  57. self.gpio_inst.start(int(self.power))
  58. def set_power(self, power):
  59. '''
  60. Optional: Set the power of your actor
  61. :param power: int value between 0 - 100
  62. :return:
  63. '''
  64. if self.gpio_inst:
  65. if power is not None:
  66. self.power = int(power)
  67. self.gpio_inst.ChangeDutyCycle(self.power)
  68. def off(self):
  69. print("GPIO OFF")
  70. self.gpio_inst.stop()
  71. @cbpi.actor
  72. class RelayBoard(ActorBase):
  73. """
  74. Relay board Actor
  75. """
  76. gpio = Property.Select("GPIO",
  77. options=[
  78. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  79. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  80. 26, 27
  81. ],
  82. description="GPIO to which the actor is connected")
  83. def init(self):
  84. GPIO.setup(int(self.gpio), GPIO.OUT)
  85. GPIO.output(int(self.gpio), 1)
  86. def on(self, power=0):
  87. GPIO.output(int(self.gpio), 0)
  88. def off(self):
  89. GPIO.output(int(self.gpio), 1)
  90. @cbpi.actor
  91. class Dummy(ActorBase):
  92. """
  93. Simple Dummy Actor
  94. """
  95. def on(self, power=100):
  96. '''
  97. Code to switch on the actor
  98. :param power: int value between 0 - 100
  99. :return:
  100. '''
  101. print("ON")
  102. def off(self):
  103. print("OFF")
  104. @cbpi.actor
  105. class DummyPWM(ActorBase):
  106. """
  107. Dummy Actor with PWM support
  108. """
  109. power = 100
  110. def on(self, power=100):
  111. '''
  112. Code to switch on the actor
  113. :param power: int value between 0 - 100
  114. :return:
  115. '''
  116. self.power = int(power) if power is not None else 100
  117. print("DummyPWM ON %s" % self.power)
  118. def off(self):
  119. self.power = 100
  120. print("OFF")
  121. def set_power(self, power):
  122. self.power = int(power)
  123. print("DummyPWM POWER %s" % self.power)