From f7d33efd31c0c4135aaa7db8a5c127e236791195 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sat, 3 Jul 2021 21:14:56 +0200 Subject: [PATCH] scroll text animations --- src/controller.py | 114 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 17 deletions(-) diff --git a/src/controller.py b/src/controller.py index 76c85f2..c5fbbfb 100644 --- a/src/controller.py +++ b/src/controller.py @@ -4,15 +4,31 @@ import time import board import busio from adafruit_ht16k33 import segments +from enum import Enum __TOPIC_BRIGHTNESS__ = "brightness" -__TOPIC_SCROLL_INTERVAL__ = "scroll_interval" -__TOPIC_TEXT__ = "text" +__TOPIC_SCROLL_INTERVAL__ = "scrollinterval" +__TOPIC_TEXT_STATIC__ = "textstatic" +__TOPIC_TEXT_ONCE__ = "textonce" +__TOPIC_TEXT_SCROLL__ = "textscroll" +__TOPIC_TEXT_SCROLL_REPEAT__ = "textscrollrepeat" +__TOPIC_TEXT_SCROLL_LR__ = "textscrolllr" + +NUMDIGITS = 8 + +class Mode(Enum): + STATIC = 1 #show text constantly. may cut digits + ONCE = 3 #show once for some time + SCROLL = 2 #scroll thorugh once + SCROLL_LR = 4 #scroll back and forth + SCROLL_REPEAT = 5 class Controller: connected = False - display = None + displayL = None + displayR = None last_scrollupdate = 0 + mode = Mode.STATIC def __init__(self, base_topic, mqtt_host, mqtt_port = 1883): @@ -25,13 +41,16 @@ class Controller: # Create the LED segment class. # This creates a 14 segment 4 character display: - self.display = segments.Seg14x4(self.i2c, address=0x70) # find address with "sudo i2cdetect -y 1" . you need to install: sudo apt-get install -y python-smbus i2c-tools + self.displayL = segments.Seg14x4(self.i2c, address=0x70) # find address with "sudo i2cdetect -y 1" . you need to install: sudo apt-get install -y python-smbus i2c-tools + self.displayR = segments.Seg14x4(self.i2c, address=0x71) # Clear the display. - self.display.fill(0) + self.displayL.fill(0) + self.displayR.fill(0) # set brightness, range 0-1.0, 1.0 max brightness - self.display.brightness = 1.0 + self.displayL.brightness = 1.0 + self.displayR.brightness = 1.0 self.mqtt = mqtt.Client() self.mqtt.on_connect = self.on_connect @@ -40,7 +59,8 @@ class Controller: self.mqtt.connect(mqtt_host, mqtt_port) self.topic = base_topic - self.display.print("BOOT") + self.displayL.print("LEFT") + self.displayR.print("RIGH") def on_disconnect(self, client, userdata, rc): @@ -54,9 +74,10 @@ class Controller: if message.topic.endswith(__TOPIC_BRIGHTNESS__ + "/set"): val = float(msg) if val >= 0.0 and val <=1.0: - self.display.brightness = val - print("changed brightness to ", self.display.brightness) - self.mqtt.publish(self.topic + "/" + __TOPIC_BRIGHTNESS__, self.display.brightness, 1 ) + self.displayL.brightness = val + self.displayR.brightness = val + print("changed brightness to ", self.displayL.brightness) + self.mqtt.publish(self.topic + "/" + __TOPIC_BRIGHTNESS__, self.displayL.brightness, 1 ) else: print("invalid brightness ", val) @@ -70,13 +91,23 @@ class Controller: print("error not >0.0: ", val) - if message.topic.endswith(__TOPIC_TEXT__ + "/set"): + if message.topic.endswith(__TOPIC_TEXT_SCROLL_REPEAT__ + "/set"): + self.mode = Mode.SCROLL_REPEAT self.poscount = 0 #start at beginning of new text self.text = msg if len(self.text)==0: self.text=" " print("changed text to ", self.text) - self.mqtt.publish(self.topic + "/" + __TOPIC_TEXT__, self.text, 1 ) + self.mqtt.publish(self.topic + "/" + __TOPIC_TEXT_SCROLL_REPEAT__, self.text, 1 ) + + if message.topic.endswith(__TOPIC_TEXT_SCROLL_LR__ + "/set"): + self.mode = Mode.SCROLL_LR + self.poscount = 0 #start at beginning of new text + self.text = msg + if len(self.text)==0: + self.text=" " + print("changed text to ", self.text) + self.mqtt.publish(self.topic + "/" + __TOPIC_TEXT_SCROLL_LR__, self.text, 1 ) def on_connect(self, client, userdata, flags, rc): @@ -84,6 +115,36 @@ class Controller: self.mqtt.subscribe(self.topic + "/#") self.connected = True + def displayTextOffset(self, _text, _offset = 0, _wrap=True): + while len(_text)=0 and _offset+0=0 and _offset+1=0 and _offset+2=0 and _offset+3=0 and _offset+4=0 and _offset+5=0 and _offset+6=0 and _offset+7 self.last_scrollupdate+self.scroll_interval: - self.display.print(self.text[self.poscount]) - self.poscount += 1 - self.poscount %= len(self.text) - self.last_scrollupdate = time.time() + if self.displayL is not None and self.displayR is not None: #displays initialized + if self.mode == Mode.SCROLL_REPEAT: + if time.time() > self.last_scrollupdate+self.scroll_interval: #time to update animation + self.displayTextOffset(self.text, self.poscount, True) + + self.poscount += 1 + self.poscount %= len(self.text) + self.last_scrollupdate = time.time() + + if self.mode == Mode.SCROLL_LR: + if time.time() > self.last_scrollupdate+self.scroll_interval: #time to update animation + print("pos=", self.poscount) + scrolloffset = self.poscount + if scrolloffset >= (len(self.text)-NUMDIGITS): + scrolloffset = (len(self.text)-NUMDIGITS) - (scrolloffset-(len(self.text)-NUMDIGITS)) #reverse + scrolloffset = max(0,scrolloffset) #cap at zero + print("scrolloffset=", scrolloffset) + + self.displayTextOffset(self.text, scrolloffset, False) + + self.poscount += 1 + self.poscount %= (len(self.text)-NUMDIGITS)*2 #if poscount over scrollable width means scroll backwards + self.last_scrollupdate = time.time() +