add button inputs

This commit is contained in:
interfisch 2021-07-04 17:23:35 +02:00
parent 77cdf4b118
commit 279caf49b5
2 changed files with 73 additions and 5 deletions

View File

@ -1,2 +1,3 @@
paho-mqtt
adafruit-circuitpython-ht16k33
gpiozero

View File

@ -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):
@ -29,7 +36,6 @@ class Controller:
displayR = None
def __init__(self, base_topic, mqtt_host, mqtt_port = 1883):
self.scroll_interval = 0.25
@ -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:
@ -209,6 +244,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
if self.mode == Mode.STATIC or self.mode == Mode.ONCE:
@ -259,3 +295,34 @@ class Controller:
self.mode == Mode.STATIC #Stop animation
self.text=" ".ljust(NUMDIGITS) #empty
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 )