2023-09-10 05:22:07 +02:00
|
|
|
|
|
|
|
import gc
|
2023-09-10 16:02:36 +02:00
|
|
|
import json
|
2023-09-10 21:29:06 +02:00
|
|
|
import math
|
2023-09-10 05:22:07 +02:00
|
|
|
import network
|
2023-09-10 16:02:36 +02:00
|
|
|
import os
|
2023-09-10 05:22:07 +02:00
|
|
|
import socket
|
2023-09-10 16:02:36 +02:00
|
|
|
import time
|
2023-09-10 05:22:07 +02:00
|
|
|
|
|
|
|
from st3m.application import Application, ApplicationContext
|
|
|
|
|
|
|
|
from umqtt.robust import MQTTClient
|
|
|
|
|
|
|
|
|
2023-09-10 16:02:36 +02:00
|
|
|
with open(os.path.join(os.path.dirname(__file__), 'config.json'), 'r') as f:
|
|
|
|
CONFIG = json.load(f)
|
2023-09-10 05:22:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Flow3rOpenhabMqtt(Application):
|
|
|
|
|
|
|
|
def __init__(self, app_ctx: ApplicationContext) -> None:
|
|
|
|
super().__init__(app_ctx)
|
|
|
|
self.state = 0
|
|
|
|
self.nic = network.WLAN(network.STA_IF)
|
2023-09-10 16:02:36 +02:00
|
|
|
client_id = CONFIG['mqtt_client_id'].format(self.nic.config('mac').hex())
|
2023-09-10 21:29:06 +02:00
|
|
|
self.base_topic = CONFIG['mqtt_topic_base'].format(self.nic.config('mac').hex()).encode()
|
2023-09-10 16:02:36 +02:00
|
|
|
self.mqtt = MQTTClient(client_id, CONFIG['mqtt_broker_ip'])
|
2023-09-10 21:29:06 +02:00
|
|
|
self.mqtt.set_callback(self.mqtt_cb)
|
2023-09-10 06:55:09 +02:00
|
|
|
self.touch_time = [None]*10
|
|
|
|
self.touch_begin = [None]*10
|
|
|
|
self.touch_last = [None]*10
|
|
|
|
self.touch_base = [False]*10
|
|
|
|
self.touch_tip = [False]*10
|
2023-09-10 20:19:53 +02:00
|
|
|
self.touch_log = [[]]*10
|
2023-09-10 21:29:06 +02:00
|
|
|
self.ui_config = [""]*10
|
|
|
|
|
|
|
|
def mqtt_cb(self, topic, msg):
|
|
|
|
print(topic, msg)
|
|
|
|
if topic == self.base_topic + b'ui_config':
|
|
|
|
try:
|
|
|
|
print(msg)
|
|
|
|
self.ui_config = json.loads(msg.decode())
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2023-09-10 05:22:07 +02:00
|
|
|
|
|
|
|
def draw(self, ctx: Context) -> None:
|
|
|
|
if self.state == 0:
|
|
|
|
return
|
|
|
|
ctx.line_width = 18
|
|
|
|
ctx.line_join = ctx.BEVEL
|
|
|
|
ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
|
|
|
|
ctx.rgb(0.3, 0.3, 0.3).arc(0, 0, 90, 0, 2.39, 0).arc(0, 0, 90, 2.79, 6.29, 0).stroke()
|
|
|
|
ctx.rgb(0.9, 0.3, 0.1).move_to(-96, 53).line_to(-1, -43).line_to(71, 29).stroke()
|
2023-09-10 21:29:06 +02:00
|
|
|
ctx.text_align = ctx.CENTER
|
|
|
|
ctx.text_baseline = ctx.MIDDLE
|
|
|
|
ctx.font_size = 20
|
|
|
|
for i, text in enumerate(self.ui_config):
|
|
|
|
phi = (2*math.pi/10)*(i+1)-(math.pi/2)
|
|
|
|
radius = 50 if i%2 else 80
|
|
|
|
x = radius*math.cos(phi)
|
|
|
|
y = radius*math.sin(phi)
|
|
|
|
ctx.rgb(1, 1, 1).move_to(x, y).text(text)
|
2023-09-10 20:19:53 +02:00
|
|
|
if CONFIG.get('debug_touch', False):
|
|
|
|
ctx.line_width =1
|
|
|
|
for i in range(10):
|
|
|
|
if len(self.touch_log[i]) > 0:
|
|
|
|
ctx.rgb(1, 0, 0).move_to(int(self.touch_log[i][0][1]*0.0024), -int(self.touch_log[i][0][0]*0.0024))
|
|
|
|
for j in range(1, len(self.touch_log[i])):
|
|
|
|
ctx.line_to(int(self.touch_log[i][j][1]*0.0024), -int(self.touch_log[i][j][0]*0.0024))
|
|
|
|
ctx.stroke()
|
2023-09-10 05:22:07 +02:00
|
|
|
|
|
|
|
def do_connect(self):
|
|
|
|
self.nic.active(True)
|
|
|
|
if not self.nic.isconnected():
|
|
|
|
print('connecting to network...')
|
2023-09-10 16:02:36 +02:00
|
|
|
self.nic.connect(CONFIG['wifi_ssid'], CONFIG['wifi_psk'])
|
2023-09-10 05:22:07 +02:00
|
|
|
while not self.nic.isconnected():
|
|
|
|
pass
|
|
|
|
print('network config:', self.nic.ifconfig())
|
|
|
|
|
|
|
|
def think(self, ins: InputState, delta_ms: int) -> None:
|
|
|
|
if self.nic.status() not in [network.STAT_CONNECTING, network.STAT_GOT_IP]:
|
|
|
|
self.do_connect()
|
|
|
|
if self.nic.status() == network.STAT_GOT_IP and self.state == 0:
|
|
|
|
gc.collect()
|
2023-09-10 21:29:06 +02:00
|
|
|
self.mqtt.set_last_will(self.base_topic + 'status', 'Offline', retain=True)
|
2023-09-10 05:22:07 +02:00
|
|
|
self.mqtt.connect()
|
2023-09-10 21:29:06 +02:00
|
|
|
self.mqtt.subscribe(self.base_topic + 'ui_config')
|
|
|
|
self.mqtt.publish(self.base_topic + 'status', 'Online', retain=True)
|
2023-09-10 05:22:07 +02:00
|
|
|
self.state = 1
|
|
|
|
if self.state == 1:
|
2023-09-10 21:29:06 +02:00
|
|
|
self.mqtt.check_msg()
|
2023-09-10 06:55:09 +02:00
|
|
|
for i in range(10):
|
|
|
|
if ins.captouch.petals[i].pressed:
|
2023-09-10 20:19:53 +02:00
|
|
|
# Ignore the first few ms of touch events, as position data appears to be unrealiable at first
|
|
|
|
if self.touch_time[i] is None:
|
2023-09-10 06:55:09 +02:00
|
|
|
self.touch_time[i] = time.ticks_ms()
|
2023-09-10 20:19:53 +02:00
|
|
|
elif time.ticks_ms() - self.touch_time[i] > 25:
|
|
|
|
if self.touch_begin[i] is None:
|
|
|
|
self.touch_begin[i] = ins.captouch.petals[i].position
|
|
|
|
self.touch_last[i] = ins.captouch.petals[i].position
|
|
|
|
if CONFIG.get('debug_touch', False):
|
|
|
|
self.touch_log[i].append(ins.captouch.petals[i].position)
|
|
|
|
self.touch_base[i] = ins.captouch.petals[i].pads.base
|
|
|
|
if hasattr(ins.captouch.petals[i].pads, 'tip'):
|
|
|
|
self.touch_tip[i] = ins.captouch.petals[i].pads.tip
|
|
|
|
else:
|
|
|
|
self.touch_tip[i] = ins.captouch.petals[i].pads.cw or ins.captouch.petals[i].pads.ccw
|
2023-09-10 05:22:07 +02:00
|
|
|
else:
|
2023-09-10 06:55:09 +02:00
|
|
|
if self.touch_begin[i] is not None:
|
|
|
|
duration = time.ticks_ms() - self.touch_time[i]
|
|
|
|
d_x = self.touch_last[i][0] - self.touch_begin[i][0]
|
|
|
|
d_y = self.touch_last[i][1] - self.touch_begin[i][1]
|
|
|
|
event = None
|
2023-09-10 20:19:53 +02:00
|
|
|
if abs(d_x) < 15000 and abs(d_y) < 15000:
|
2023-09-10 06:55:09 +02:00
|
|
|
if self.touch_base[i]:
|
|
|
|
event = 'touch_base'
|
|
|
|
elif self.touch_tip[i]:
|
|
|
|
event = 'touch_tip'
|
|
|
|
elif abs(d_x) > abs(d_y):
|
|
|
|
if d_x > 0:
|
|
|
|
event = 'swipe_up'
|
|
|
|
else:
|
|
|
|
event = 'swipe_down'
|
|
|
|
else:
|
|
|
|
if d_y > 0:
|
|
|
|
event = 'swipe_right'
|
|
|
|
else:
|
|
|
|
event = 'swipe_left'
|
|
|
|
if event is not None:
|
|
|
|
human_petal = 10-(-i%10)
|
2023-09-10 21:29:06 +02:00
|
|
|
msg = b'{{"petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'\
|
|
|
|
.format(human_petal, event, d_x, d_y, duration)
|
|
|
|
self.mqtt.publish(self.base_topic + 'event', msg)
|
2023-09-10 06:55:09 +02:00
|
|
|
self.touch_time[i] = None
|
|
|
|
self.touch_begin[i] = None
|
|
|
|
self.touch_last[i] = None
|
|
|
|
self.touch_base[i] = False
|
|
|
|
self.touch_tip[i] = False
|
2023-09-10 20:19:53 +02:00
|
|
|
self.touch_log[i].clear()
|
|
|
|
gc.collect()
|
2023-09-10 05:22:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import st3m.run
|
|
|
|
st3m.run.run_view(Flow3rOpenhabMqtt(ApplicationContext()))
|