Various optimizations
This commit is contained in:
parent
9d7f23ef78
commit
51b6846b30
1 changed files with 26 additions and 32 deletions
58
c4ctrl.py
58
c4ctrl.py
|
@ -42,7 +42,6 @@ class C4Interface():
|
||||||
|
|
||||||
if self.debug: return print("[DEBUG] inhibited messages:", cmd)
|
if self.debug: return print("[DEBUG] inhibited messages:", cmd)
|
||||||
|
|
||||||
print(cmd)
|
|
||||||
publish.multiple(cmd,
|
publish.multiple(cmd,
|
||||||
hostname=self.broker,
|
hostname=self.broker,
|
||||||
port=self.port)
|
port=self.port)
|
||||||
|
@ -62,13 +61,15 @@ class C4Interface():
|
||||||
def fetch(self, topic=[]):
|
def fetch(self, topic=[]):
|
||||||
"""Return current state of topic."""
|
"""Return current state of topic."""
|
||||||
from paho.mqtt import subscribe
|
from paho.mqtt import subscribe
|
||||||
|
topic = topic or self.topic
|
||||||
|
# <topic> must be a list
|
||||||
|
if type(topic) == str:
|
||||||
|
topic = [topic]
|
||||||
|
|
||||||
if self.debug:
|
if self.debug:
|
||||||
print("[DEBUG] inhibited query for:", topic)
|
print("[DEBUG] inhibited query for:", topic)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# <topic> must be a list
|
|
||||||
if type(topic) == str:
|
|
||||||
topic = [topic]
|
|
||||||
return subscribe.simple(topic,
|
return subscribe.simple(topic,
|
||||||
msg_count=len(topic),
|
msg_count=len(topic),
|
||||||
qos=self.qos,
|
qos=self.qos,
|
||||||
|
@ -106,48 +107,44 @@ class C4Interface():
|
||||||
class Dmx:
|
class Dmx:
|
||||||
"""Abstraction of the 3 Channel LED Cans in the Club."""
|
"""Abstraction of the 3 Channel LED Cans in the Club."""
|
||||||
|
|
||||||
|
template = "000000"
|
||||||
|
|
||||||
def __init__(self, topic, color=None):
|
def __init__(self, topic, color=None):
|
||||||
self.topic = topic
|
self.topic = topic
|
||||||
if color:
|
self.set_color(color or self.template)
|
||||||
self.set_color(color)
|
|
||||||
else:
|
|
||||||
self.color = None
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.topic + " : " + str(self.color)
|
return "<dmx '{}'>".format(self.topic)
|
||||||
|
|
||||||
def _pad_color(self, color, template):
|
def _pad_color(self, color):
|
||||||
"""Merge hex color value into hex template."""
|
"""Merge hex color value into hex template."""
|
||||||
# Expand shorthand hex codes (eg. #f0f) and pad with template
|
# Expand shorthand hex codes (eg. #f0f) and pad with template
|
||||||
if len(color) > len(template): # Truncate
|
if len(color) > len(self.template): # Truncate
|
||||||
print("Warning: truncating color value {} to {}".format(
|
print("Warning: truncating color value {} to {}".format(
|
||||||
color, color[:len(template)]))
|
color, color[:len(self.template)]))
|
||||||
return color[:len(template)]
|
return color[:len(self.template)]
|
||||||
|
|
||||||
# Expand 3 char codes and codes of half the required length.
|
# Expand 3 char codes and codes of half the required length.
|
||||||
# Yet, lets presume that a 6-char code should never be expanded.
|
# Yet, lets presume that a 6-char code should never be expanded.
|
||||||
if len(color) != 6 and len(color) == 3 or len(color) == (len(template) / 2):
|
if len(color) != 6 and len(color) == 3 or len(color) == (len(self.template) / 2):
|
||||||
expanded = ""
|
expanded = ""
|
||||||
for c in color:
|
for c in color:
|
||||||
expanded += c*2
|
expanded += c*2
|
||||||
color = expanded
|
color = expanded
|
||||||
|
|
||||||
if len(color) == len(template): # Nothing to do
|
if len(color) == len(self.template): # Nothing to do
|
||||||
return color
|
return color
|
||||||
|
|
||||||
# Add padding
|
# Add padding
|
||||||
color = color + template[-(len(template) - len(color)):]
|
color = color + self.template[-(len(self.template) - len(color)):]
|
||||||
return color
|
return color
|
||||||
|
|
||||||
def set_color(self, color):
|
def set_color(self, color):
|
||||||
"""Set color (hex) for this instance.
|
"""Set color (hex) for this instance.
|
||||||
|
|
||||||
The color is then available via the color variable."""
|
The color is then available via the color variable."""
|
||||||
|
color = self._pad_color(color)
|
||||||
|
|
||||||
if not color:
|
|
||||||
self.color = None
|
|
||||||
return
|
|
||||||
color = self._pad_color(color, "000000")
|
|
||||||
self.color = color
|
self.color = color
|
||||||
self.payload = bytearray.fromhex(color)
|
self.payload = bytearray.fromhex(color)
|
||||||
|
|
||||||
|
@ -155,19 +152,13 @@ class Dmx:
|
||||||
class Dmx5(Dmx):
|
class Dmx5(Dmx):
|
||||||
"""Abstraction of the 5 Channel LED Cans in the Club."""
|
"""Abstraction of the 5 Channel LED Cans in the Club."""
|
||||||
|
|
||||||
def set_color(self, color):
|
template = "00000000ff"
|
||||||
color = self._pad_color(color, "000000ff")
|
|
||||||
self.color = color
|
|
||||||
self.payload = bytearray.fromhex(color)
|
|
||||||
|
|
||||||
|
|
||||||
class Dmx7(Dmx):
|
class Dmx7(Dmx):
|
||||||
"""Abstraction of the 7 Channel LED Cans in the Club."""
|
"""Abstraction of the 7 Channel LED Cans in the Club."""
|
||||||
|
|
||||||
def set_color(self, color):
|
template = "000000000000ff"
|
||||||
color = self._pad_color(color, "000000000000ff")
|
|
||||||
self.color = color
|
|
||||||
self.payload = bytearray.fromhex(color)
|
|
||||||
|
|
||||||
|
|
||||||
class C4Room:
|
class C4Room:
|
||||||
|
@ -428,6 +419,7 @@ class Kitchenlight:
|
||||||
|
|
||||||
def matrix(self, lines=8):
|
def matrix(self, lines=8):
|
||||||
"""Set to mode "matrix"."""
|
"""Set to mode "matrix"."""
|
||||||
|
if int(lines) > 30: lines = 30 # Maximal line count
|
||||||
d = bytearray(8)
|
d = bytearray(8)
|
||||||
v = memoryview(d)
|
v = memoryview(d)
|
||||||
# Screen 2
|
# Screen 2
|
||||||
|
@ -467,7 +459,7 @@ class Kitchenlight:
|
||||||
v[13:17] = int(10000).to_bytes(4, "little")
|
v[13:17] = int(10000).to_bytes(4, "little")
|
||||||
self._switch(d)
|
self._switch(d)
|
||||||
|
|
||||||
def openchaos(self, delay=4000):
|
def openchaos(self, delay=1000):
|
||||||
"""Set to mode "openchaos"."""
|
"""Set to mode "openchaos"."""
|
||||||
d = bytearray(8)
|
d = bytearray(8)
|
||||||
v = memoryview(d)
|
v = memoryview(d)
|
||||||
|
@ -493,14 +485,16 @@ class Kitchenlight:
|
||||||
|
|
||||||
def text(self, text="Hello World", delay=200):
|
def text(self, text="Hello World", delay=200):
|
||||||
"""Set to mode "text"."""
|
"""Set to mode "text"."""
|
||||||
|
text = text.encode("latin1", "ignore")
|
||||||
d = bytearray(8 + len(text) + 1)
|
d = bytearray(8 + len(text) + 1)
|
||||||
v = memoryview(d)
|
v = memoryview(d)
|
||||||
# Screen 8
|
# Screen 8
|
||||||
v[0:4] = int(8).to_bytes(4, "little")
|
v[0:4] = int(8).to_bytes(4, "little")
|
||||||
v[4:8] = int(delay).to_bytes(4, "little")
|
v[4:8] = int(delay).to_bytes(4, "little")
|
||||||
for i in range(len(text)):
|
v[8:8 + len(text)] = text
|
||||||
v[i+8:i+9] = int(ord(text[i])).to_bytes(1, "little")
|
#for i in range(len(text)):
|
||||||
v[len(d)-1:len(d)] = bytes(1)
|
# v[i+8:i+9] = int(text[i]).to_bytes(1, "little")
|
||||||
|
v[len(d) - 1:len(d)] = bytes(1)
|
||||||
self._switch(d)
|
self._switch(d)
|
||||||
|
|
||||||
def flood(self):
|
def flood(self):
|
||||||
|
|
Loading…
Reference in a new issue