From 3c2e6725fa0a65cee468bb04e647218feca4812b Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:29:01 +0100 Subject: [PATCH 01/24] ha: always apply lazy icons to triggers --- apps/ha/ha.lib.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 7b26b02b2..9a48d60eb 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -40,21 +40,21 @@ exports.getTriggers = function(){ try{ triggers = require("Storage").read("ha.trigger.json"); triggers = JSON.parse(triggers); - - // We lazy load all icons, otherwise, we have to keep - // all the icons n times in memory which can be - // problematic for embedded devices. Therefore, - // we lazy load icons only if needed using the getIcon - // method of each trigger... - triggers.forEach(trigger => { - trigger.getIcon = function(){ - return _getIcon(trigger); - } - }) } catch(e) { // In case there are no user triggers yet, we show the default... } + // We lazy load all icons, otherwise, we have to keep + // all the icons n times in memory which can be + // problematic for embedded devices. Therefore, + // we lazy load icons only if needed using the getIcon + // method of each trigger... + triggers.forEach(trigger => { + trigger.getIcon = function(){ + return _getIcon(trigger); + } + }) + return triggers; } @@ -78,4 +78,4 @@ exports.sendTrigger = function(triggerName){ retries--; } } -} \ No newline at end of file +} From e2a62eabcab51b04b2233bcb9005aead0a3e706b Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:29:27 +0100 Subject: [PATCH 02/24] ha: log trigger errors and default after error --- apps/ha/ha.lib.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 9a48d60eb..036fba391 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -33,15 +33,17 @@ function _getIcon(trigger){ } exports.getTriggers = function(){ - var triggers = [ - {display: "Empty", trigger: "NOP", icon: "ha"}, - ]; + var triggers; try{ triggers = require("Storage").read("ha.trigger.json"); triggers = JSON.parse(triggers); } catch(e) { // In case there are no user triggers yet, we show the default... + console.log("ha: error loading triggers:", e); + triggers = [ + {display: "Empty", trigger: "NOP", icon: "ha"}, + ]; } // We lazy load all icons, otherwise, we have to keep From 60c6f351518b405b32e5cf61493ab31d36d94498 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:29:44 +0100 Subject: [PATCH 03/24] ha: merge read & parse logic --- apps/ha/ha.lib.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 036fba391..07797e075 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -36,8 +36,7 @@ exports.getTriggers = function(){ var triggers; try{ - triggers = require("Storage").read("ha.trigger.json"); - triggers = JSON.parse(triggers); + triggers = JSON.parse(require("Storage").read("ha.trigger.json")); } catch(e) { // In case there are no user triggers yet, we show the default... console.log("ha: error loading triggers:", e); From e993711eb20c35743cf6abe305c1131ca2031852 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:29:50 +0100 Subject: [PATCH 04/24] ha: whitespace --- apps/ha/ha.lib.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 07797e075..5a87387b1 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -67,11 +67,11 @@ exports.sendTrigger = function(triggerName){ // Now lets send the trigger that we sould send. Bluetooth.println(""); Bluetooth.println(JSON.stringify({ - t:"intent", - action:"com.espruino.gadgetbridge.banglejs.HA", - extra:{ - trigger: triggerName - }}) + t:"intent", + action:"com.espruino.gadgetbridge.banglejs.HA", + extra:{ + trigger: triggerName + }}) ); retries = -1; From 32072f37d66c45dbd8e749cb842b3a2464663c3e Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:30:29 +0100 Subject: [PATCH 05/24] ha: getIcon() uses `this` --- apps/ha/ha.lib.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 5a87387b1..0e15f9db9 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -2,8 +2,8 @@ * This library can be used to read all triggers that a user * configured and send a trigger to homeassistant. */ -function _getIcon(trigger){ - const icon = trigger.icon; +function _getIcon(){ + const icon = this.icon; if(icon == "light"){ return { width : 48, height : 48, bpp : 1, @@ -51,9 +51,7 @@ exports.getTriggers = function(){ // we lazy load icons only if needed using the getIcon // method of each trigger... triggers.forEach(trigger => { - trigger.getIcon = function(){ - return _getIcon(trigger); - } + trigger.getIcon = _getIcon.bind(trigger); }) return triggers; From bcffdef85b346159b90c6b17db6bf41895c69cad Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:30:38 +0100 Subject: [PATCH 06/24] ha: break instead of setting retries --- apps/ha/ha.lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 0e15f9db9..9aa13303d 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -71,7 +71,7 @@ exports.sendTrigger = function(triggerName){ trigger: triggerName }}) ); - retries = -1; + break; } catch(e){ retries--; From a610833bd767f512717a7b6ad84a8d859a19ce4a Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:30:52 +0100 Subject: [PATCH 07/24] ha: add lib.sendValue() --- apps/ha/ha.lib.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 9aa13303d..9b16a25cc 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -78,3 +78,13 @@ exports.sendTrigger = function(triggerName){ } } } + +exports.sendValue = function(trigger, value){ + Bluetooth.println( + JSON.stringify({ + t: "intent", + action: "com.espruino.gadgetbridge.banglejs.HA", + extra: { trigger, value }, + }) + ); +}; From c0ea3138e51e0687463a08ca1cd7b1cc3af4b43a Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:32:44 +0100 Subject: [PATCH 08/24] ha: merge conditionals --- apps/ha/ha.app.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index 14c6dc3be..d63230137 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -71,23 +71,20 @@ Bangle.on('touch', function(btn, e){ if(isLeft){ toLeft(); - } - if(isRight){ + }else if (isRight){ toRight(); - } - if(!isRight && !isLeft){ + }else{ sendTrigger(); } }); Bangle.on("swipe", (lr,ud) => { - if (lr == -1) { - toLeft(); - } - if (lr == 1) { - toRight(); - } - }); + if (lr == -1) { + toLeft(); + } else if (lr == 1) { + toRight(); + } +}); // Send intent that the we started the app. From 216b30d6404a76b3d306ee68e08af107ccc07687 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:32:53 +0100 Subject: [PATCH 09/24] ha: remove parseInt() --- apps/ha/ha.app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index d63230137..adb2551aa 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -64,7 +64,7 @@ function sendTrigger() { } Bangle.on('touch', function(btn, e){ - var left = parseInt(g.getWidth() * 0.3); + var left = g.getWidth() * 0.3; var right = g.getWidth() - left; var isLeft = e.x < left; var isRight = e.x > right; From 8d8008658a520ee5c7df5dcc165080cd2bee2d2e Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:33:00 +0100 Subject: [PATCH 10/24] ha: function -> arrow --- apps/ha/ha.app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index adb2551aa..47a236626 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -63,7 +63,7 @@ function sendTrigger() { }); } -Bangle.on('touch', function(btn, e){ +Bangle.on('touch', (btn, e) => { var left = g.getWidth() * 0.3; var right = g.getWidth() - left; var isLeft = e.x < left; From cacd6c7ac2ebb1bb25f34d21c8e95857a7a8bc25 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:35:14 +0100 Subject: [PATCH 11/24] ha: add slider functionality --- apps/ha/ha.app.js | 66 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index 47a236626..676665dc0 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -5,7 +5,7 @@ var ha = require("ha.lib.js"); var W = g.getWidth(), H = g.getHeight(); var position=0; var triggers = ha.getTriggers(); - +var slider; function draw() { g.reset().clearRect(Bangle.appRect); @@ -17,15 +17,60 @@ function draw() { g.setFontAlign(-1,-1); var icon = trigger.getIcon(); - g.setColor(g.theme.fg).drawImage(icon, 12, H/5-2-5); - g.drawString("Home", icon.width + 20, H/5-5); - g.drawString("Assistant", icon.width + 18, H/5+24-5); + var iconY = H / 5 - 2 - 5; + g.setColor(g.theme.fg).drawImage(icon, 12, iconY); - g.setFontAlign(0,0); - var ypos = H/5*3+23; - g.drawRect(W/2-w/2-8, ypos-h/2-8, W/2+w/2+5, ypos+h/2+5); - g.fillRect(W/2-w/2-6, ypos-h/2-6, W/2+w/2+3, ypos+h/2+3); - g.setColor(g.theme.bg).drawString(trigger.display, W/2, ypos); + if (trigger.value) { + if (!slider) { + const R = Bangle.appRect; + console.log("R", R); + const w = 50; + + slider = require("Slider").create(onSlide, { + initLevel: 0, // TODO: fetch this? + + mode: "map", + steps: 100, + timeout: false, + + width: w, + xStart: R.w / 2 - w / 2, + yStart: R.y, + height: R.h - 24, + + dragRect: { + x: R.w * 0.3, + x2: R.w * 0.6, + y: R.y, + y2: R.h, + }, + }); + const onDrag = e => { + slider.f.dragSlider(e); + if(e.b === 0) + ha.sendValue(trigger.trigger, slider.v.level); + }; + Bangle.prependListener('drag', onDrag); + } + + const r = slider.c.borderRect; + g.setColor(g.theme.fg) + .setFontAlign(0, 0) + .drawString("HA", (r.x + r.w + W) / 2, iconY + g.imageMetrics(icon).height / 2) + .setFontAlign(0, 1) + .drawString(trigger.display, W / 2, H); + + slider.f.draw(slider.v.level); + }else{ + g.drawString("Home", icon.width + 20, H/5-5); + g.drawString("Assistant", icon.width + 18, H/5+24-5); + + g.setFontAlign(0,0); + var ypos = H/5*3+23; + g.drawRect(W/2-w/2-8, ypos-h/2-8, W/2+w/2+5, ypos+h/2+5); + g.fillRect(W/2-w/2-6, ypos-h/2-6, W/2+w/2+3, ypos+h/2+3); + g.setColor(g.theme.bg).drawString(trigger.display, W/2, ypos); + } // draw arrows g.setColor(g.theme.fg); @@ -39,6 +84,9 @@ function draw() { } } +function onSlide(mode, feedback) { +} + function toLeft() { Bangle.buzz(40, 0.6); position -= 1; From a28b2008c38280f555c2302d725f1b335f1fb04b Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 18:09:38 +0100 Subject: [PATCH 12/24] Slider.js: don't hold onto cbObj --- modules/Slider.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/Slider.js b/modules/Slider.js index 33bda1245..25fa5e317 100644 --- a/modules/Slider.js +++ b/modules/Slider.js @@ -125,6 +125,7 @@ exports.create = function(cb, conf) { if (o.v.timeoutID) {clearTimeout(o.v.timeoutID); o.v.timeoutID = undefined;} if (e.b==0 && !o.v.timeoutID && (o.c.timeout || o.c.timeout===0)) o.v.timeoutID = setTimeout(o.f.remove, 1000*o.c.timeout); + let cbObj; if (useMap && o.f.wasOnIndicator(o.v.exFirst)) { // If draging starts on the indicator, adjust one-to-one. let input = !o.c.horizontal? @@ -134,7 +135,7 @@ exports.create = function(cb, conf) { o.v.level = Math.min(Math.max(input,0),o.c.steps); - o.v.cbObj = {mode:"map", value:o.v.level}; + cbObj = {mode:"map", value:o.v.level}; } else if (useIncr) { // Heavily inspired by "updown" mode of setUI. @@ -149,14 +150,13 @@ exports.create = function(cb, conf) { o.v.level = Math.min(Math.max(o.v.level-incr,0),o.c.steps); - o.v.cbObj = {mode:"incr", value:incr}; + cbObj = {mode:"incr", value:incr}; } } - if (o.v.cbObj && (o.v.level!==o.v.prevLevel||o.v.level===0||o.v.level===o.c.steps)) { - cb(o.v.cbObj.mode, o.v.cbObj.value); + if (cbObj && (o.v.level!==o.v.prevLevel||o.v.level===0||o.v.level===o.c.steps)) { + cb(cbObj.mode, cbObj.value); o.f.draw&&o.f.draw(o.v.level); } - o.v.cbObj = null; o.v.prevLevel = o.v.level; o.v.ebLast = e.b; } From 95366ebc5d7ca36476248bfcfd52f91eb6e7da8c Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 18:10:51 +0100 Subject: [PATCH 13/24] ha: bump version --- apps/ha/ChangeLog | 1 + apps/ha/metadata.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/ha/ChangeLog b/apps/ha/ChangeLog index 1ae0d9dce..e87e70eab 100644 --- a/apps/ha/ChangeLog +++ b/apps/ha/ChangeLog @@ -9,3 +9,4 @@ 0.09: Improve web interface, arrows in UI 0.10: Issue newline before GB commands (solves issue with console.log and ignored commands) 0.11: Minor code improvements +0.12: Add slider functionality diff --git a/apps/ha/metadata.json b/apps/ha/metadata.json index 4983aed7c..3ddf9b858 100644 --- a/apps/ha/metadata.json +++ b/apps/ha/metadata.json @@ -1,7 +1,7 @@ { "id": "ha", "name": "Home Assistant", - "version": "0.11", + "version": "0.12", "description": "Integrates your Bangle.js into Home Assistant using Android Integration/Gadgetbridge", "icon": "ha.png", "type": "app", From 19ff8316e4e63ea56b5caeea20bf96c6a4e3062a Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 18:43:49 +0100 Subject: [PATCH 14/24] ha: use readJSON --- apps/ha/ha.lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ha/ha.lib.js b/apps/ha/ha.lib.js index 9b16a25cc..89802b28d 100644 --- a/apps/ha/ha.lib.js +++ b/apps/ha/ha.lib.js @@ -36,7 +36,7 @@ exports.getTriggers = function(){ var triggers; try{ - triggers = JSON.parse(require("Storage").read("ha.trigger.json")); + triggers = require("Storage").readJSON("ha.trigger.json"); } catch(e) { // In case there are no user triggers yet, we show the default... console.log("ha: error loading triggers:", e); From 621f58feb6877626acd0c514dc1f76f90d4ca02d Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 18:44:04 +0100 Subject: [PATCH 15/24] Slider: reset dragActive --- modules/Slider.js | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/Slider.js b/modules/Slider.js index 25fa5e317..01d5ef79c 100644 --- a/modules/Slider.js +++ b/modules/Slider.js @@ -159,6 +159,7 @@ exports.create = function(cb, conf) { } o.v.prevLevel = o.v.level; o.v.ebLast = e.b; + if (e.b==0) o.v.dragActive = false; } }; From 0dc61519a72c5a994230b31de3660e1065f16929 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 18:44:09 +0100 Subject: [PATCH 16/24] Slider: pass event through --- modules/Slider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Slider.js b/modules/Slider.js index 01d5ef79c..4a9dce263 100644 --- a/modules/Slider.js +++ b/modules/Slider.js @@ -154,7 +154,7 @@ exports.create = function(cb, conf) { } } if (cbObj && (o.v.level!==o.v.prevLevel||o.v.level===0||o.v.level===o.c.steps)) { - cb(cbObj.mode, cbObj.value); + cb(cbObj.mode, cbObj.value, e); o.f.draw&&o.f.draw(o.v.level); } o.v.prevLevel = o.v.level; From 10f248ac10ecee944e5e4051a9ef6907fade0dfe Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 18:44:23 +0100 Subject: [PATCH 17/24] ha: only send level change when it changes --- apps/ha/ha.app.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index 676665dc0..7ff236988 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -45,12 +45,7 @@ function draw() { y2: R.h, }, }); - const onDrag = e => { - slider.f.dragSlider(e); - if(e.b === 0) - ha.sendValue(trigger.trigger, slider.v.level); - }; - Bangle.prependListener('drag', onDrag); + Bangle.prependListener('drag', slider.f.dragSlider); } const r = slider.c.borderRect; @@ -84,7 +79,19 @@ function draw() { } } -function onSlide(mode, feedback) { +var lastLevel; + +function onSlide(mode, level, e) { + if (e.b !== 0) { + if (lastLevel == null) + lastLevel = level; + } else { + if (lastLevel != null && lastLevel !== level) { + // we've had a drag and level has changed + ha.sendValue(triggers[position].trigger, level); + lastLevel = null; + } + } } function toLeft() { From b0aaf8f69f2b2ef9ca07fb35f5ca8267099be797 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:25:56 +0100 Subject: [PATCH 18/24] Slider: always emit final event --- modules/Slider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Slider.js b/modules/Slider.js index 4a9dce263..49ba56c26 100644 --- a/modules/Slider.js +++ b/modules/Slider.js @@ -153,7 +153,7 @@ exports.create = function(cb, conf) { cbObj = {mode:"incr", value:incr}; } } - if (cbObj && (o.v.level!==o.v.prevLevel||o.v.level===0||o.v.level===o.c.steps)) { + if (cbObj && (o.v.level!==o.v.prevLevel||o.v.level===0||o.v.level===o.c.steps||e.b===0)) { cb(cbObj.mode, cbObj.value, e); o.f.draw&&o.f.draw(o.v.level); } From 4b47fbc15fa70431ea131b75191c3fee99633d0e Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:26:26 +0100 Subject: [PATCH 19/24] ha: prevent triggering of 'touch' straight after slider change --- apps/ha/ha.app.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index 7ff236988..b1271a91b 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -80,8 +80,11 @@ function draw() { } var lastLevel; +var lastTouch; function onSlide(mode, level, e) { + lastTouch = Date.now(); + if (e.b !== 0) { if (lastLevel == null) lastLevel = level; @@ -119,6 +122,9 @@ function sendTrigger() { } Bangle.on('touch', (btn, e) => { + if (Date.now() - lastTouch < 250) return; + lastTouch = Date.now(); + var left = g.getWidth() * 0.3; var right = g.getWidth() - left; var isLeft = e.x < left; From 9566176d2c6a3111fca2311969259818238c159f Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Sun, 21 Apr 2024 19:40:56 +0100 Subject: [PATCH 20/24] ha: update README --- apps/ha/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/ha/README.md b/apps/ha/README.md index b0309b040..53bb64899 100644 --- a/apps/ha/README.md +++ b/apps/ha/README.md @@ -3,11 +3,13 @@ This app integrates your Bangle.js into the Home Assistant. # How to use -Click on the left or right side of the screen to select the triggers that you configured. +Click on the left or right side of the screen to select the triggers that you configured. Swiping left or right works as well. Click in the middle of the screen to send the trigger to Home Assistant via Gadgetbridge. +If the trigger is a value one (has `.value = true` in the config), a slider will be displayed for you to alter the value that's sent along with the trigger. + ![](screenshot.png) From 611fd7aaf1e545eda3b5bf955baecc9bfce7fdbb Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 22 Apr 2024 08:18:49 +0100 Subject: [PATCH 21/24] Slider: update docs --- modules/Slider.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/Slider.md b/modules/Slider.md index eb2291d25..8e3571b5e 100644 --- a/modules/Slider.md +++ b/modules/Slider.md @@ -18,12 +18,16 @@ Bangle.on("drag", slider.f.dragSlider); // Bangle.prependListener("drag", slider.f.dragSlider); ``` -`callbackFunction` (`cb`) (first argument) determines what `slider` is used for. `slider` will pass two arguments, `mode` and `feedback` (`fb`), into `callbackFunction` (if `slider` is interactive or auto progressing). The different `mode`/`feedback` combinations to expect are: +`callbackFunction` (`cb`) (first argument) determines what `slider` is used for. `slider` will pass three arguments, `mode`, `feedback` (`fb`) and (if fired from an input event) `event` (`e`), into `callbackFunction` (if `slider` is interactive or auto progressing). The different `mode`/`feedback` combinations to expect are: - `"map", o.v.level` | current level when interacting by mapping interface. - `"incr", incr` | where `incr` == +/-1, when interacting by incrementing interface. - `"remove", o.v.level` | last level when the slider times out. - `"auto", o.v.level` | when auto progressing. +The event will be a drag, from the `Bangle.on('drag', ...)` event. + +The callback function will always be called for the "final" event, which is when the user lifts their finger from the screen. This can be detected by looking for `e.b == 0`. + `configObject` (`conf`) (second argument, optional) has the following defaults: ```js @@ -91,7 +95,7 @@ slider = require("Slider").create(()=>{}, {autoProgress:true}) r: 22 } } } -> +> ``` Tips ---- From 13997ca678179890888f1d00a6853f59c5abf847 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 22 Apr 2024 08:38:17 +0100 Subject: [PATCH 22/24] ha: hide slider when going from value -> trigger and handle the 'remove' event / eventless callbacks --- apps/ha/ha.app.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index b1271a91b..220bc4799 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -57,6 +57,11 @@ function draw() { slider.f.draw(slider.v.level); }else{ + if (slider) { + slider.f.remove(); + slider = undefined; + } + g.drawString("Home", icon.width + 20, H/5-5); g.drawString("Assistant", icon.width + 18, H/5+24-5); @@ -85,6 +90,8 @@ var lastTouch; function onSlide(mode, level, e) { lastTouch = Date.now(); + if (!e) return; + if (e.b !== 0) { if (lastLevel == null) lastLevel = level; From cce28d967524d5074575b95dd0beaffe8cb34cf1 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 22 Apr 2024 08:38:58 +0100 Subject: [PATCH 23/24] ha: disable swiping on sliders and prevent swipe event propagation --- apps/ha/ha.app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index 220bc4799..0da903d22 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -147,11 +147,14 @@ Bangle.on('touch', (btn, e) => { }); Bangle.on("swipe", (lr,ud) => { + if (slider) return; // "disable" swiping on sliders + if (lr == -1) { toLeft(); } else if (lr == 1) { toRight(); } + E.stopEventPropagation && E.stopEventPropagation(); }); From 1eae81e7f6a11ff463cf63cbca2c18904b139292 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Mon, 22 Apr 2024 08:39:18 +0100 Subject: [PATCH 24/24] ha: prevent touch/swipe/drag overlap events after a swipe --- apps/ha/ha.app.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/ha/ha.app.js b/apps/ha/ha.app.js index 0da903d22..3f6e606e7 100644 --- a/apps/ha/ha.app.js +++ b/apps/ha/ha.app.js @@ -148,6 +148,8 @@ Bangle.on('touch', (btn, e) => { Bangle.on("swipe", (lr,ud) => { if (slider) return; // "disable" swiping on sliders + if (Date.now() - lastTouch < 250) return; + lastTouch = Date.now(); if (lr == -1) { toLeft();