feat: add a global "mode" that can be set via ui_config and is published to mqtt.

This commit is contained in:
s3lph 2023-09-10 23:11:28 +02:00
parent acadc3d380
commit 761c47da10
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 18 additions and 9 deletions

View file

@ -25,13 +25,14 @@ The touch events detected by the flow3r are published to the topic `flow3r-<maca
Each event is a JSON string that looks like this: Each event is a JSON string that looks like this:
```json ```json
{"petal":"2","event":"touch_tip","dx":8083,"dy":6960,"duration":150} {"mode":"Numbers","petal":"2","event":"touch_tip","dx":8083,"dy":6960,"duration":150}
``` ```
The keys and their values are defined as follows: The keys and their values are defined as follows:
| Key | Value | | 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). | | `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. | | `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]. | | `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]. |
@ -44,10 +45,14 @@ The keys and their values are defined as follows:
The app subscribes to the topic `flow3r-<macaddress>/ui_config`, at which it listens for JSON arrays such as below: The app subscribes to the topic `flow3r-<macaddress>/ui_config`, at which it listens for JSON arrays such as below:
```json ```json
["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"] {
"mode": "Numbers",
"labels": ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"]
}
``` ```
Each position in the array corrensponds to one of the petals, and the text for each petal is shown on the display close to the corresponding petal. * `labels`: Each position in the array corrensponds to one of the petals, and the text for each petal is shown on the display close to the corresponding petal.
* `mode`: The text is shown in big in the center of the display. It is also sent with each event, so that subscribers can perform differnt actions depending on the active mode.
## Integration into OpenHAB ## Integration into OpenHAB
@ -92,7 +97,7 @@ actions:
id: "2" id: "2"
configuration: configuration:
itemName: flow3r_ui_config # item needs to be created and linked to the ui_config topic beforehand itemName: flow3r_ui_config # item needs to be created and linked to the ui_config topic beforehand
command: '["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"]' command: '{"labels":["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"]}'
type: core.ItemCommandAction type: core.ItemCommandAction
``` ```
@ -115,7 +120,7 @@ actions:
type: application/javascript type: application/javascript
script: >- script: >-
var payload = JSON.parse(this.event.event); var payload = JSON.parse(this.event.event);
if (payload.petal == "10" and payload.duration > 1000) { if (payload.petal == "10" && payload.duration > 1000) {
console.log("longpress!"); console.log("longpress!");
} }
``` ```

View file

@ -32,7 +32,7 @@ class Flow3rOpenhabMqtt(Application):
self.touch_base = [False]*10 self.touch_base = [False]*10
self.touch_tip = [False]*10 self.touch_tip = [False]*10
self.touch_log = [[]]*10 self.touch_log = [[]]*10
self.ui_config = [""]*10 self.ui_config = {}
def mqtt_cb(self, topic, msg): def mqtt_cb(self, topic, msg):
print(topic, msg) print(topic, msg)
@ -54,12 +54,16 @@ class Flow3rOpenhabMqtt(Application):
ctx.text_align = ctx.CENTER ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE ctx.text_baseline = ctx.MIDDLE
ctx.font_size = 20 ctx.font_size = 20
for i, text in enumerate(self.ui_config): for i, text in enumerate(self.ui_config.get('labels', [])):
phi = (2*math.pi/10)*(i+1)-(math.pi/2) phi = (2*math.pi/10)*(i+1)-(math.pi/2)
radius = 50 if i%2 else 80 radius = 50 if i%2 else 80
x = radius*math.cos(phi) x = radius*math.cos(phi)
y = radius*math.sin(phi) y = radius*math.sin(phi)
ctx.rgb(1, 1, 1).move_to(x, y).text(text) ctx.rgb(1, 1, 1).move_to(x, y).text(text)
mode = self.ui_config.get('mode')
if mode:
ctx.font_size = 50
ctx.rgb(1, 1, 1).move_to(0, 0).text(mode)
if CONFIG.get('debug_touch', False): if CONFIG.get('debug_touch', False):
ctx.line_width =1 ctx.line_width =1
for i in range(10): for i in range(10):
@ -129,8 +133,8 @@ class Flow3rOpenhabMqtt(Application):
event = 'swipe_left' event = 'swipe_left'
if event is not None: if event is not None:
human_petal = 10-(-i%10) human_petal = 10-(-i%10)
msg = b'{{"petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'\ msg = b'{{"mode":"{}","petal":"{}","event":"{}","dx":{},"dy":{},"duration":{}}}'\
.format(human_petal, event, d_x, d_y, duration) .format(self.ui_config.get('mode'), human_petal, event, d_x, d_y, duration)
self.mqtt.publish(self.base_topic + 'event', msg) self.mqtt.publish(self.base_topic + 'event', msg)
self.touch_time[i] = None self.touch_time[i] = None
self.touch_begin[i] = None self.touch_begin[i] = None