|
|
|
@@ -1,4 +1,5 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
""" base gpio actors """ |
|
|
|
import time |
|
|
|
|
|
|
|
from modules import cbpi |
|
|
|
@@ -6,19 +7,25 @@ from modules.core.hardware import ActorBase, SensorPassive, SensorActive |
|
|
|
from modules.core.props import Property |
|
|
|
|
|
|
|
try: |
|
|
|
import RPi.GPIO as GPIO |
|
|
|
import RPi.GPIO as GPIO # pylint: disable=import-error |
|
|
|
|
|
|
|
GPIO.setmode(GPIO.BCM) |
|
|
|
except Exception as e: |
|
|
|
print(e) |
|
|
|
pass |
|
|
|
|
|
|
|
except ModuleNotFoundError as exp: |
|
|
|
print(exp) |
|
|
|
|
|
|
|
|
|
|
|
@cbpi.actor |
|
|
|
class GPIOSimple(ActorBase): |
|
|
|
|
|
|
|
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") |
|
|
|
""" |
|
|
|
Simple GPIO Actor |
|
|
|
""" |
|
|
|
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") |
|
|
|
|
|
|
|
def init(self): |
|
|
|
GPIO.setup(int(self.gpio), GPIO.OUT) |
|
|
|
@@ -32,20 +39,28 @@ class GPIOSimple(ActorBase): |
|
|
|
print("GPIO OFF") |
|
|
|
GPIO.output(int(self.gpio), 0) |
|
|
|
|
|
|
|
|
|
|
|
@cbpi.actor |
|
|
|
class GPIOPWM(ActorBase): |
|
|
|
|
|
|
|
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") |
|
|
|
""" |
|
|
|
GPIO Actor with PWM support |
|
|
|
""" |
|
|
|
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") |
|
|
|
frequency = Property.Number("Frequency (Hz)", configurable=True) |
|
|
|
|
|
|
|
p = None |
|
|
|
gpio_inst = None |
|
|
|
power = 100 # duty cycle |
|
|
|
|
|
|
|
def init(self): |
|
|
|
GPIO.setup(int(self.gpio), GPIO.OUT) |
|
|
|
GPIO.output(int(self.gpio), 0) |
|
|
|
|
|
|
|
|
|
|
|
def on(self, power=None): |
|
|
|
if power is not None: |
|
|
|
self.power = int(power) |
|
|
|
@@ -53,29 +68,37 @@ class GPIOPWM(ActorBase): |
|
|
|
if self.frequency is None: |
|
|
|
self.frequency = 0.5 # 2 sec |
|
|
|
|
|
|
|
self.p = GPIO.PWM(int(self.gpio), float(self.frequency)) |
|
|
|
self.p.start(int(self.power)) |
|
|
|
self.gpio_inst = GPIO.PWM(int(self.gpio), float(self.frequency)) |
|
|
|
self.gpio_inst.start(int(self.power)) |
|
|
|
|
|
|
|
def set_power(self, power): |
|
|
|
''' |
|
|
|
Optional: Set the power of your actor |
|
|
|
:param power: int value between 0 - 100 |
|
|
|
:return: |
|
|
|
:return: |
|
|
|
''' |
|
|
|
if self.p: |
|
|
|
if self.gpio_inst: |
|
|
|
if power is not None: |
|
|
|
self.power = int(power) |
|
|
|
self.p.ChangeDutyCycle(self.power) |
|
|
|
self.gpio_inst.ChangeDutyCycle(self.power) |
|
|
|
|
|
|
|
def off(self): |
|
|
|
print("GPIO OFF") |
|
|
|
self.p.stop() |
|
|
|
self.gpio_inst.stop() |
|
|
|
|
|
|
|
|
|
|
|
@cbpi.actor |
|
|
|
class RelayBoard(ActorBase): |
|
|
|
|
|
|
|
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") |
|
|
|
""" |
|
|
|
Relay board Actor |
|
|
|
""" |
|
|
|
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") |
|
|
|
|
|
|
|
def init(self): |
|
|
|
GPIO.setup(int(self.gpio), GPIO.OUT) |
|
|
|
@@ -89,24 +112,30 @@ class RelayBoard(ActorBase): |
|
|
|
|
|
|
|
GPIO.output(int(self.gpio), 1) |
|
|
|
|
|
|
|
|
|
|
|
@cbpi.actor |
|
|
|
class Dummy(ActorBase): |
|
|
|
|
|
|
|
""" |
|
|
|
Simple Dummy Actor |
|
|
|
""" |
|
|
|
|
|
|
|
def on(self, power=100): |
|
|
|
''' |
|
|
|
Code to switch on the actor |
|
|
|
:param power: int value between 0 - 100 |
|
|
|
:return: |
|
|
|
:return: |
|
|
|
''' |
|
|
|
print("ON") |
|
|
|
|
|
|
|
def off(self): |
|
|
|
print("OFF") |
|
|
|
|
|
|
|
|
|
|
|
@cbpi.actor |
|
|
|
class DummyPWM(ActorBase): |
|
|
|
|
|
|
|
""" |
|
|
|
Dummy Actor with PWM support |
|
|
|
""" |
|
|
|
power = 100 |
|
|
|
|
|
|
|
def on(self, power=100): |
|
|
|
@@ -116,12 +145,12 @@ class DummyPWM(ActorBase): |
|
|
|
:return: |
|
|
|
''' |
|
|
|
self.power = int(power) if power is not None else 100 |
|
|
|
print "DummyPWM ON %s" % self.power |
|
|
|
print("DummyPWM ON %s" % self.power) |
|
|
|
|
|
|
|
def off(self): |
|
|
|
self.power = 100 |
|
|
|
print "OFF" |
|
|
|
print("OFF") |
|
|
|
|
|
|
|
def set_power(self, power): |
|
|
|
self.power = int(power) |
|
|
|
print "DummyPWM POWER %s" % self.power |
|
|
|
print("DummyPWM POWER %s" % self.power) |