From 279caf49b54f2ee1ab638a4dd81d24e3bd7da8ca Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 4 Jul 2021 17:23:35 +0200 Subject: [PATCH] add button inputs --- requirements.txt | 3 +- src/controller.py | 75 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index 30b84da..e6de3bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ paho-mqtt -adafruit-circuitpython-ht16k33 \ No newline at end of file +adafruit-circuitpython-ht16k33 +gpiozero \ No newline at end of file diff --git a/src/controller.py b/src/controller.py index 77b2a16..13d898a 100644 --- a/src/controller.py +++ b/src/controller.py @@ -3,9 +3,14 @@ import paho.mqtt.client as mqtt import time import board import busio + from adafruit_ht16k33 import segments from enum import Enum +from gpiozero import Button + +import socket #to get local ip + __TOPIC_BRIGHTNESS__ = "brightness" __TOPIC_SCROLL_INTERVAL__ = "scrollinterval" __TOPIC_TEXT_STATIC__ = "textstatic" @@ -14,6 +19,8 @@ __TOPIC_TEXT_SCROLL__ = "textscroll" __TOPIC_TEXT_MARQUEE__ = "textmarquee" __TOPIC_TEXT_PAN__ = "textpan" +__TOPIC_BUTTON__ = "button" + NUMDIGITS = 8 class Mode(Enum): @@ -28,7 +35,6 @@ class Controller: displayL = None displayR = None - def __init__(self, base_topic, mqtt_host, mqtt_port = 1883): @@ -39,6 +45,17 @@ class Controller: self.last_scrollupdate = 0 self.mode = Mode.STATIC + self.buttonLeft = Button(23, pull_up=False, hold_time=1) # https://gpiozero.readthedocs.io/en/stable/api_input.html + self.buttonRight = Button(24, pull_up=False, hold_time=1) # with deactivated pullup input is active high + + self.buttonLeft.when_released = self.buttonLeft_released + self.buttonLeft.when_held = self.buttonLeft_held + self.buttonLeft_heldFlag = False #if true, next released will be ignored + + self.buttonRight.when_released = self.buttonRight_released + self.buttonRight.when_held = self.buttonRight_held + self.buttonRight_heldFlag = False #if true, next released will be ignored + # Create the I2C interface. self.i2c = busio.I2C(board.SCL, board.SDA) @@ -63,8 +80,7 @@ class Controller: self.mqtt.connect(mqtt_host, mqtt_port) self.topic = base_topic - self.displayL.print("LEFT") - self.displayR.print("RIGH") + def on_disconnect(self, client, userdata, rc): @@ -138,8 +154,27 @@ class Controller: def on_connect(self, client, userdata, flags, rc): print("Connected to MQTT Broker") self.mqtt.subscribe(self.topic + "/#") + self.connected = True + self.mqtt.publish(self.topic + "/" + "$state", "ready", 1 ) + + self.mqtt.publish(self.topic + "/" + "$hostname", socket.gethostname() , 1 ) + self.mqtt.publish(self.topic + "/" + "$localip", self.get_ip() , 1 ) + + + def get_ip(self): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + # doesn't even have to be reachable + s.connect(('10.255.255.255', 1)) + IP = s.getsockname()[0] + except Exception: + IP = '127.0.0.1' + finally: + s.close() + return IP + def writeSegment(self, _digit, _char, clear=False): if _digit>=0 and _digit<4: if clear: @@ -208,6 +243,7 @@ class Controller: while run: self.mqtt.loop(0.1) #with block timeout + if self.displayL is not None and self.displayR is not None: #displays initialized @@ -258,4 +294,35 @@ class Controller: def clearAndStop(self): self.mode == Mode.STATIC #Stop animation self.text=" ".ljust(NUMDIGITS) #empty - self.displayTextOffset(self.text, 0) #update immediately \ No newline at end of file + self.displayTextOffset(self.text, 0) #update immediately + + + def buttonLeft_released(self): + if self.buttonLeft_heldFlag: + self.buttonLeft_heldFlag = False + else: + print("button left pressed") + if self.connected: + self.mqtt.publish(self.topic + "/" + __TOPIC_BUTTON__, "lpress", 1 ) + + + def buttonLeft_held(self): + print("button left held") + self.buttonLeft_heldFlag = True + if self.connected: + self.mqtt.publish(self.topic + "/" + __TOPIC_BUTTON__, "lhold", 1 ) + + def buttonRight_released(self): + if self.buttonRight_heldFlag: + self.buttonRight_heldFlag = False + else: + print("button right pressed") + if self.connected: + self.mqtt.publish(self.topic + "/" + __TOPIC_BUTTON__, "rpress", 1 ) + + def buttonRight_held(self): + print("button right held") + self.buttonRight_heldFlag = True + if self.connected: + self.mqtt.publish(self.topic + "/" + __TOPIC_BUTTON__, "rhold", 1 ) + \ No newline at end of file