Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

106 lines
2.5KB

  1. # -*- coding: utf-8 -*-
  2. import time
  3. from modules import cbpi
  4. from modules.core.hardware import ActorBase, SensorPassive, SensorActive
  5. from modules.core.props import Property
  6. try:
  7. import RPi.GPIO as GPIO
  8. GPIO.setmode(GPIO.BCM)
  9. except Exception as e:
  10. print e
  11. pass
  12. @cbpi.actor
  13. class GPIOSimple(ActorBase):
  14. gpio = Property.Select("GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], description="GPIO to which the actor is connected")
  15. def init(self):
  16. GPIO.setup(int(self.gpio), GPIO.OUT)
  17. GPIO.output(int(self.gpio), 0)
  18. def on(self, power=0):
  19. print "GPIO ON %s" % str(self.gpio)
  20. GPIO.output(int(self.gpio), 1)
  21. def off(self):
  22. print "GPIO OFF"
  23. GPIO.output(int(self.gpio), 0)
  24. @cbpi.actor
  25. class GPIOPWM(ActorBase):
  26. gpio = Property.Select("GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], description="GPIO to which the actor is connected")
  27. frequency = Property.Number("Frequency (Hz)", configurable=True)
  28. p = None
  29. power = 100 # duty cycle
  30. def init(self):
  31. GPIO.setup(int(self.gpio), GPIO.OUT)
  32. GPIO.output(int(self.gpio), 0)
  33. def on(self, power=None):
  34. if power is not None:
  35. self.power = int(power)
  36. if self.frequency is None:
  37. self.frequency = 0.5 # 2 sec
  38. if self.p is None:
  39. self.p = GPIO.PWM(int(self.gpio), float(self.frequency))
  40. self.p.start(int(self.power))
  41. def set_power(self, power):
  42. '''
  43. Optional: Set the power of your actor
  44. :param power: int value between 0 - 100
  45. :return:
  46. '''
  47. if power is not None:
  48. self.power = int(power)
  49. self.p.ChangeDutyCycle(self.power)
  50. def off(self):
  51. print "GPIO OFF"
  52. self.p.stop()
  53. @cbpi.actor
  54. class RelayBoard(ActorBase):
  55. gpio = Property.Select("GPIO", options=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27], description="GPIO to which the actor is connected")
  56. def init(self):
  57. GPIO.setup(int(self.gpio), GPIO.OUT)
  58. GPIO.output(int(self.gpio), 1)
  59. def on(self, power=0):
  60. GPIO.output(int(self.gpio), 0)
  61. def off(self):
  62. GPIO.output(int(self.gpio), 1)
  63. @cbpi.actor
  64. class Dummy(ActorBase):
  65. def on(self, power=100):
  66. '''
  67. Code to switch on the actor
  68. :param power: int value between 0 - 100
  69. :return:
  70. '''
  71. print "ON"
  72. def off(self):
  73. print "OFF"