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.

108 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. self.p = GPIO.PWM(int(self.gpio), float(self.frequency))
  39. self.p.start(int(self.power))
  40. def set_power(self, power):
  41. '''
  42. Optional: Set the power of your actor
  43. :param power: int value between 0 - 100
  44. :return:
  45. '''
  46. if power is not None:
  47. self.power = int(power)
  48. self.p.ChangeDutyCycle(self.power)
  49. def off(self):
  50. print "GPIO OFF"
  51. self.p.stop()
  52. @cbpi.actor
  53. class RelayBoard(ActorBase):
  54. 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")
  55. def init(self):
  56. GPIO.setup(int(self.gpio), GPIO.OUT)
  57. GPIO.output(int(self.gpio), 1)
  58. def on(self, power=0):
  59. GPIO.output(int(self.gpio), 0)
  60. def off(self):
  61. GPIO.output(int(self.gpio), 1)
  62. @cbpi.actor
  63. class Dummy(ActorBase):
  64. def on(self, power=100):
  65. '''
  66. Code to switch on the actor
  67. :param power: int value between 0 - 100
  68. :return:
  69. '''
  70. print "ON"
  71. def off(self):
  72. print "OFF"