feat: add app button events

This commit is contained in:
s3lph 2023-09-11 00:13:28 +02:00
parent 761c47da10
commit 597daf33f0
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 55 additions and 5 deletions

View file

@ -20,19 +20,28 @@ with other subscribers as well.
## Events
The touch events detected by the flow3r are published to the topic `flow3r-<macaddress>/event`.
All events are published as JSON stringsto the topic `flow3r-<macaddress>/event`.
Each event is a JSON string that looks like this:
Keys common to all types of events are:
| Key | Value |
|:---|:---|
| `mode` | The mode that has been set via the `ui_config` topic, see "UI Config" below. |
| `type` | The type of event. Currently one of `button` or `touch`. |
### Touch Events
Example of a touch event:
```json
{"mode":"Numbers","petal":"2","event":"touch_tip","dx":8083,"dy":6960,"duration":150}
{"mode":"Numbers","type":"touch","petal":"2","event":"touch_tip","dx":8083,"dy":6960,"duration":150}
```
The keys and their values are defined as follows:
| Key | Value |
|:---|:---|
| `mode` | The mode that has been set via the `ui_config` topic, see "UI Config" below. |
| `petal` | The petal the event was detected on. Uses the numbers printed on the PCB (1-10), rather than the `captouch` indexes (0-9). |
| `event` | The type of gesture that was detected. One of `touch_tip`, `touch_base`, `swipe_up`, `swipe_right`, `swipe_down`, `swipe_left`, whereas `swipe_up` always means a swipe towards the tip of the petal, and `swipe_down` towards its base. |
| `dx` | The distance between start and end of the touch event in `x` (base-to-tip) direction. Unitless number, as returned by [captouch's `position()` function][captouch]. |
@ -40,6 +49,24 @@ The keys and their values are defined as follows:
| `duration` | The duration between start and end of the touch event in `ms`. |
### Button Events
Presses on the app button (by default the left one) are published to MQTT as well.
Example of a button event:
```json
{"mode":"Numbers","type":"button","button":"left","repeat":false}
```
The keys and their values are defined as follows:
| Key | Value |
|:---|:---|
| `button` | The direction in which the button was pressed. One of `left`, `right`, `down`. |
| `repeat` | If the button is held for a longer time, the button event is repeated every 500ms. This value is `false` for the inital event and `true` for repeat events. |
## UI Config
The app subscribes to the topic `flow3r-<macaddress>/ui_config`, at which it listens for JSON arrays such as below:

View file

@ -32,6 +32,7 @@ class Flow3rOpenhabMqtt(Application):
self.touch_base = [False]*10
self.touch_tip = [False]*10
self.touch_log = [[]]*10
self.button_app = None
self.ui_config = {}
def mqtt_cb(self, topic, msg):
@ -94,6 +95,28 @@ class Flow3rOpenhabMqtt(Application):
self.state = 1
if self.state == 1:
self.mqtt.check_msg()
if ins.buttons.app != ins.buttons.NOT_PRESSED:
now = time.ticks_ms()
btn = None
if ins.buttons.app == ins.buttons.PRESSED_LEFT:
btn = 'left'
elif ins.buttons.app == ins.buttons.PRESSED_RIGHT:
btn = 'right'
elif ins.buttons.app == ins.buttons.PRESSED_DOWN:
btn = 'down'
if self.button_app is None:
self.button_app = now
msg = b'{{"mode":"{}","type":"button","button":"{}","repeat":false}}'\
.format(self.ui_config.get('mode'), btn)
self.mqtt.publish(self.base_topic + 'event', msg)
elif now - self.button_app > 500:
msg = b'{{"mode":"{}","type":"button","button":"{}","repeat":true}}'\
.format(self.ui_config.get('mode'), btn)
self.mqtt.publish(self.base_topic + 'event', msg)
self.button_app = now
else:
self.button_app = None
for i in range(10):
if ins.captouch.petals[i].pressed:
# Ignore the first few ms of touch events, as position data appears to be unrealiable at first
@ -133,7 +156,7 @@ class Flow3rOpenhabMqtt(Application):
event = 'swipe_left'
if event is not None:
human_petal = 10-(-i%10)
msg = b'{{"mode":"{}","petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'\
msg = b'{{"mode":"{}","type":"touch","petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'\
.format(self.ui_config.get('mode'), human_petal, event, d_x, d_y, duration)
self.mqtt.publish(self.base_topic + 'event', msg)
self.touch_time[i] = None