2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
.. moduleauthor:: Fabio Manganiello <blacklight86@gmail.com>
|
|
|
|
"""
|
|
|
|
|
2018-03-24 03:05:46 +01:00
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-03-24 03:05:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GpioPlugin(Plugin):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
Plugin to handle raw read/write operation on the Raspberry Pi GPIO pins.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
* **RPi.GPIO** (`pip install RPi.GPIO`)
|
|
|
|
"""
|
|
|
|
|
2018-07-06 02:34:12 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-06 02:08:38 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
@action
|
2018-03-24 03:05:46 +01:00
|
|
|
def write(self, pin, val):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
Write a byte value to a pin.
|
|
|
|
|
|
|
|
:param pin: PIN number
|
|
|
|
:type pin: int
|
|
|
|
|
|
|
|
:param val: Value to write
|
|
|
|
:type val: int
|
|
|
|
|
|
|
|
:returns: dict
|
|
|
|
|
|
|
|
Response::
|
|
|
|
|
|
|
|
output = {
|
|
|
|
"pin": <pin>,
|
|
|
|
"val": <val>,
|
|
|
|
"method": "write"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
import RPi.GPIO as gpio
|
|
|
|
|
2018-03-24 03:05:46 +01:00
|
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
gpio.setup(pin, gpio.OUT)
|
|
|
|
gpio.output(pin, val)
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return {
|
2018-03-24 03:05:46 +01:00
|
|
|
'pin': pin,
|
|
|
|
'val': val,
|
|
|
|
'method': 'write',
|
2018-07-06 02:08:38 +02:00
|
|
|
}
|
2018-03-24 03:05:46 +01:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-06-23 01:00:43 +02:00
|
|
|
def read(self, pin):
|
|
|
|
"""
|
|
|
|
Reads a value from a PIN.
|
|
|
|
|
|
|
|
:param pin: PIN number
|
|
|
|
:type pin: int
|
|
|
|
|
|
|
|
:returns: dict
|
|
|
|
|
|
|
|
Response::
|
|
|
|
|
|
|
|
output = {
|
|
|
|
"pin": <pin>,
|
|
|
|
"val": <val>,
|
|
|
|
"method": "read"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
import RPi.GPIO as gpio
|
|
|
|
|
2018-03-24 03:05:46 +01:00
|
|
|
gpio.setmode(gpio.BCM)
|
|
|
|
gpio.setup(pin, gpio.IN)
|
|
|
|
val = gpio.input(pin)
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return {
|
2018-03-24 03:05:46 +01:00
|
|
|
'pin': pin,
|
|
|
|
'val': val,
|
|
|
|
'method': 'read',
|
2018-07-06 02:08:38 +02:00
|
|
|
}
|
2018-03-24 03:05:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|