Skip to content

Commit

Permalink
feat: new module for interacting with Raspberry Pi GPIO outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
activeshadow committed Aug 4, 2023
1 parent fe25896 commit 39c729e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
Empty file.
100 changes: 100 additions & 0 deletions src/python/otsim/rpi_gpio/rpi_gpio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from __future__ import annotations

import json, logging, signal, sys, threading, typing

import otsim.msgbus.envelope as envelope
import xml.etree.ElementTree as ET

from otsim.msgbus.envelope import Envelope
from otsim.msgbus.subscriber import Subscriber

import RPi.GPIO as GPIO


class RPiGPIO:
def __init__(self: RPiGPIO, pub: str, pull: str, el: ET.Element):
# map tags --> pin number
self.outputs: typing.Dict[str, int] = {}

self.name = el.get('name', default='ot-sim-rpi-gpio')
mode = el.get('mode', default='BOARD')

if mode.upper() == 'BOARD':
GPIO.setmode(GPIO.BOARD)
elif mode.upper() == 'BCM':
GPIO.setmode(GPIO.BCM)
else:
print(f"unknown GPIO mode '{mode}' - defaulting to 'BOARD' mode")
GPIO.setmode(GPIO.BOARD)

pub_endpoint = el.findtext('pub-endpoint', default=pub)
self.subscriber = Subscriber(pub_endpoint)

for o in el.findall('output'):
pin = int(o.get('pin'))
tag = o.findtext('tag')

self.outputs[tag] = pin
GPIO.setup(pin, GPIO.OUT)

self.subscriber.add_update_handler(self.handle_msgbus_update)


def start(self: RPiGPIO):
self.subscriber.start('RUNTIME')


def stop(self: RPiGPIO):
self.subscriber.stop()


def handle_msgbus_update(self: RPiGPIO, env: Envelope):
update = envelope.update_from_envelope(env)

if update:
for point in update['updates']:
tag = point['tag']

if tag in self.outputs:
GPIO.output(self.outputs[tag], point['value'])


def main():
logging.basicConfig(level=logging.ERROR)

if len(sys.argv) < 2:
print('no config file provided')
sys.exit(1)

tree = ET.parse(sys.argv[1])

root = tree.getroot()
assert root.tag == 'ot-sim'

mb = root.find('message-bus')

if mb:
pub = mb.findtext('pub-endpoint')
pull = mb.findtext('pull-endpoint')
else:
pub = 'tcp://127.0.0.1:5678'
pull = 'tcp://127.0.0.1:1234'

devices: typing.List[RPiGPIO] = []

for gpio in root.findall('rpi-gpio'):
device = RPiGPIO(pub, pull, gpio)
device.start()

devices.append(device)

waiter = threading.Event()

def handler(*_):
waiter.set()

signal.signal(signal.SIGINT, handler)
waiter.wait()

for device in devices:
device.stop()
2 changes: 2 additions & 0 deletions src/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
'pandas',
'pyzmq',
'requests',
'RPi.GPIO',
'windpowerlib',
]

ENTRIES = {
'console_scripts' : [
'ot-sim-ground-truth-module = otsim.ground_truth.ground_truth:main',
'ot-sim-io-module = otsim.io.io:main',
'ot-sim-rpi-gpio-module = otsim.rpi_gpio.rpi_gpio:main',
'ot-sim-wind-turbine-anemometer-module = otsim.wind_turbine.anemometer.anemometer:main',
'ot-sim-wind-turbine-power-output-module = otsim.wind_turbine.power_output.power_output:main',
]
Expand Down

0 comments on commit 39c729e

Please sign in to comment.