Implemented blinkenlights

This commit is contained in:
Pünkösd Marcell 2020-09-30 00:04:50 +02:00
parent 86c0e6be35
commit 6fccadf8fa
8 changed files with 138 additions and 1 deletions

1
.gitignore vendored
View File

@ -129,3 +129,4 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
.idea/

View File

@ -1,3 +1,3 @@
# libbirbpy # iot-platform-raspberry
Library handling low level hardware interactions for iot-logic Library handling low level hardware interactions for iot-logic

View File

@ -0,0 +1 @@
from .led_stuff import BirbnetesIoTPlatformStatusDriver

View File

@ -0,0 +1,95 @@
from threading import Thread
from queue import Queue, Empty
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
class BlinkenLight:
def __init__(self, pin):
self._pin = pin
self._pattern_queue = Queue()
self._active_pattern = None
self._pointer = 0
GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)
def clk(self):
if not self._active_pattern: # No pattern loaded
if self._pointer == -5: # Skip 5 ticks before new pattern
try:
pattern = self._pattern_queue.get(block=False)
self._active_pattern = pattern
self._pointer = 0
except Empty:
return
else:
self._pointer -= 1
# Meanwhile a pattern loaded
if self._active_pattern: # There is a pattern loaded
if self._pointer == len(self._active_pattern):
self._active_pattern = None
self._pointer = 0
GPIO.output(self._pin, False)
else:
GPIO.output(self._pin, bool(self._active_pattern[self._pointer]))
self._pointer += 1
def enqueue_pattern(self, pattern: list):
self._pattern_queue.put(pattern)
def cleanup(self):
GPIO.cleanup(self._pin)
class BlinkenLights(Thread):
LED_TARGETS = {
'red': 23,
'green': 24
}
def __init__(self):
super().__init__()
self._led_controllers = {}
for name, pin in self.LED_TARGETS.items():
self._led_controllers[name] = BlinkenLight(pin)
self._active = True
def run(self):
while self._active:
time.sleep(200)
for name, led in self._led_controllers:
led.clk()
for name, led in self._led_controllers:
led.cleanup()
def enqueue_pattern(self, target: str, pattern: list):
self._led_controllers[target].enqueue_pattern(pattern)
def stop(self):
self._active = False
class BirbnetesIoTPlatformStatusDriver:
blinken_lights = None
@classmethod
def init(cls):
cls.blinken_lights = BlinkenLights()
cls.blinken_lights.start()
@classmethod
def enqueue_pattern(cls, target: str, pattern: list):
cls.blinken_lights.enqueue_pattern(target, pattern)

View File

@ -0,0 +1 @@
import alsaaudio

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
RPi.GPIO
pyalsaaudio

37
setup.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
"""The setup script."""
from setuptools import setup, find_packages
with open('README.md') as readme_file:
readme = readme_file.read()
requirements = ['RPi.GPIO', 'pyalsaaudio']
setup_requirements = ['pytest-runner', ]
test_requirements = ['pytest>=3', ]
setup(
author="Marcell Pünkösd",
author_email='punkosdmarcell@rocketmail.com',
python_requires='>=3.7',
classifiers=[
'Development Status :: 3 - Alpha',
],
description="Platform abstraction layer for Birbnetes IoT device",
install_requires=requirements,
license="MIT license",
long_description=readme,
include_package_data=True,
keywords='birbnetes',
name='birbnetes_iot_platform_raspberry',
packages=find_packages(include=['birbnetes_iot_platform_raspberry', 'birbnetes_iot_platform_raspberry.*']),
setup_requires=setup_requirements,
test_suite='tests',
tests_require=test_requirements,
url='https://git.kmlabz.com/birbnetes/iot-platform-raspberry',
version='0.1.0',
zip_safe=False,
)