feat: detect touch gestures
This commit is contained in:
parent
2f4e0dd3b3
commit
6dde20af37
1 changed files with 45 additions and 13 deletions
56
__init__.py
56
__init__.py
|
@ -1,6 +1,7 @@
|
|||
|
||||
import gc
|
||||
import network
|
||||
import time
|
||||
import socket
|
||||
|
||||
from st3m.application import Application, ApplicationContext
|
||||
|
@ -24,7 +25,11 @@ class Flow3rOpenhabMqtt(Application):
|
|||
client_id = MQTT_CLIENT_ID.format(self.nic.config('mac').hex())
|
||||
self.base_topic = MQTT_BASE_TOPIC.format(self.nic.config('mac').hex())
|
||||
self.mqtt = MQTTClient(client_id, MQTT_BROKER_IP)
|
||||
self.before = [0]*10
|
||||
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
|
||||
|
||||
def draw(self, ctx: Context) -> None:
|
||||
if self.state == 0:
|
||||
|
@ -52,19 +57,46 @@ class Flow3rOpenhabMqtt(Application):
|
|||
self.mqtt.connect()
|
||||
self.state = 1
|
||||
if self.state == 1:
|
||||
for i in range(5):
|
||||
if ins.captouch.petals[i*2].pads.cw or ins.captouch.petals[i*2].pads.ccw:
|
||||
if self.before[i*2] == 0:
|
||||
self.mqtt.publish(self.base_topic, b'{{"petal":"{}","event":"press_tip"}}'.format(i))
|
||||
self.before[i*2] = 1
|
||||
for i in range(10):
|
||||
if ins.captouch.petals[i].pressed:
|
||||
if self.touch_begin[i] is None:
|
||||
self.touch_begin[i] = ins.captouch.petals[i].position
|
||||
self.touch_time[i] = time.ticks_ms()
|
||||
self.touch_last[i] = 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.before[i*2] = 0
|
||||
if ins.captouch.petals[i*2].pads.base:
|
||||
if self.before[i*2+1] == 0:
|
||||
self.mqtt.publish(self.base_topic, b'{{"petal":"{}","event":"press_base"}}'.format(i))
|
||||
self.before[i*2+1] = 1
|
||||
self.touch_tip[i] = ins.captouch.petals[i].pads.cw or ins.captouch.petals[i].pads.ccw
|
||||
else:
|
||||
self.before[i*2+1] = 0
|
||||
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
|
||||
if abs(d_x) < 25000 and abs(d_y) < 25000:
|
||||
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)
|
||||
self.mqtt.publish(self.base_topic, b'{{"petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'.format(human_petal, event, d_x, d_y, duration))
|
||||
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
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in a new issue