Commit ad4e3df0 authored by chrissi^'s avatar chrissi^
Browse files

RgbChannel: Add PWM function


Signed-off-by: chrissi^'s avatarChris Fiege <chris@tinyhost.de>
parent 8deb180b
......@@ -124,33 +124,66 @@ class RgbChannel:
def __init__(self, mqtt_client, mqtt_topic, Pin_R, Pin_G, Pin_B):
self._rgb =(0,0,0)
self._on = False
self._freq = 1000
self._pwms = [
machine.PWM(machine.Pin(Pin_R)),
machine.PWM(machine.Pin(Pin_G)),
machine.PWM(machine.Pin(Pin_B)),
]
self._apply()
self._topic_on_get = mqtt_topic+"/on/get"
self._topic_on_set = mqtt_topic+"/on/set"
self._topic_rgb_get = mqtt_topic+"/rgb/get"
self._topic_rgb_set = mqtt_topic+"/rgb/set"
self._topic_freq_get = mqtt_topic+"/freq/get"
self._topic_freq_set = mqtt_topic+"/freq/set"
self._mqtt_client = mqtt_client
self._mqtt_client.add_cb(self._cb)
self._mqtt_client.subscribe(self._topic_on_set)
self._mqtt_client.subscribe(self._topic_rgb_set)
def _apply(self):
if self._on:
rgb = [x*4 for x in self._rgb]
else:
rgb = (0,0,0)
self._pwms[0].freq(self._freq)
for i in range(3):
self._pwms[i].duty(rgb[i])
def _cb(self, topic, msg):
if topic == self._topic_on_set:
self._on = "True" if msg == "True" else "False"
self.pub()
self._mqtt_client.publish(self._topic_on_get, str(self._on))
self._apply()
return True
elif topic == self._topic_rgb_set:
try:
self._rgb = [int(x) for x in msg.split(",")]
self.pub()
self._mqtt_client.publish(self._topic_rgb_get, str(self._rgb))
self._apply()
return True
except Exception as e:
print("RgbChannel: Failed to parse value '{}' for topic '{}' with: {}".format(msg, topic, e))
elif topic == self._topic_freq_set:
try:
self._freq = int(msg)
self._mqtt_client.publish(self._topic_freq_get, str(self._freq))
self._apply()
return True
except Exception as e:
print("RgbChannel: Failed to parse value '{}' for topic '{}' with: {}".format(msg, topic, e))
return False
def pub(self):
self._mqtt_client.publish(self._topic_on_get, str(self._on))
self._mqtt_client.publish(self._topic_rgb_get, str(self._rgb))
self._mqtt_client.publish(self._topic_on_get, str(self._on))
self._mqtt_client.publish(self._topic_freq_get, str(self._freq))
s = scheduler.Scheduler()
......@@ -168,10 +201,10 @@ s.register(uptime.increment, timeout=1)
s.register(uptime.pub, timeout=5)
led = OnBoardLed(m, "ledctl/{}/OnBoardLed".format(m.machine_id))
s.register(led.pub, timeout=5)
str1 = RgbChannel(m, "ledctl/{}/String1".format(m.machine_id), None, None, None)
s.register(str1.pub, timeout=5)
s.register(led.pub, timeout=10)
#
str1 = RgbChannel(m, "ledctl/{}/String1".format(m.machine_id), esp8266pinmap.D1, esp8266pinmap.D2, esp8266pinmap.D3)
s.register(str1.pub, timeout=10)
# start scheduler
# runs forever
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment