Among others, the conversation about 2 questions regarding Button and touch event stirred me to take yet another - 3rd - stab at that subject of a Bangle Touch Controller. The first one was in the Bangle emulator when a Bangle was not yet available to me.
First and second failed because the nested condition checking and tracking of state became unbearable: lots of code, slow, and still a lot of unresolved (corner) cases, brittle and of limited functions.
This 3rd time, I use a state machine with direct addressed / called functions for the state and transitions. It consists of 2 implementations:
Simple controller that provides event times and callbacks, but is only touch driven
Enhanced controller that provides additional - time driven - callbacks.
In this conversations it's about the simple controller. Main design goal was to enable easy exchange of the application callbacks to reuse the one-time setup of the controller. Therefore, the callbacks are passed as object properties in an object. The property names map to the events, states and transitions of he state machine of the controller and are called Touch Signatures. They can have these values:
2. ```r``` - right button touched and is touching as the only one, marking the start of a touch sequence
2. ```L``` - left button untouched as the only button that was touched, completing a left touch
2. ```r``` - right button untouched as the only button that was touched, completing a right touch
3. ```b``` - second button touch down and both buttons are touched, initiating a both touch
4. ```p``` - the first of two buttons of a both touch untouched, initiating the end of a both touch
5. ```B``` - the second of two buttons of a both touch untouched, completing a both touch
6. ```e``` - error sequence - for example - retouching of first untouched button of a both touch
7. ```E``` - recovery from an error sequence - usually achieved with untouching both buttons
Each callback is called with four (4) arguments:
1. ```ts``` - touch signature (l, r, L, R, b, p, B, e, E)
2. ```tt``` - touch start time in secs - from getTime() when first touch of touch sequence happened.
3. ```te``` - touch elapsed time in secs - can be used to determine short, medium, and long touches
4. ```ctl``` - the touch controller object to provide access to additional information, such as state and detailed timings of the both button touches. Latter allow to determine which of the buttons was touched or untouched first and at what time relative to the touch begin (and corollary, within the elapsed time). ***Foremost*** though it is for expedient change of the callbacks object to enter in a new conversation context of the application without complicated state management.
For additional information see the inline comments in the constructor.
Running the controller usage example - code in next post running connected on BantleJS -produces this output in the Espruno IDE console:
Right touch for 0.17495727539 [sec]
Left touch for 0.11331176757 [sec] at 1607677068.38404655456
Right touch for 0.83169555664 [sec]
Left touch for 1.75540161132 [sec] at 1607677075.57887077331
B Both w/ details abt 2nd btn touch and 1st btn untouch
at 0:58:0
for 1.31875610351
2nd touch after 1st t 0.02020263671
1st untouch aft 1st t 1.31472778320
2nd untouch aft 1st u 0.00402832031
2nd untouch aft 1st t 1.31875610351 =elapsed
ign: bRr r 0
err e 3
ign: ebRrR R 3
err e 2
ign: e..Rr r 2
err e 2
ign: e..rR R 2
err e 3
ign: e..Rr r 3
err e 3
ign: e..rL L 3
err e 1
ign: e..LR R 1
err E 0
- Lines 2..5 show single button touches of left and right touch button with different touch times.
- Lines 6..12 show details about a both button touch
- Lines 13..26 show re-occurring error cycle that started with touching again the 1st untouched button of a both button touch. Line 13 show as in the second item on the line state (```bR```) and transition (```r```) as concatenated (```bRr```) - (temp) ```.s``` property of the controller. State ```bR``` means that a *both button touch* had begun and a *right button rouch* had started to wind it down, when 'all of a sudden' the right button is touched again. The dispatcher could not find a *state and transition* method/function and has directed it to the ```.ign()``` *error* method/function. The output producing statement ( ```console.log("ign: ",_.s,e,_.sb);``` - between code lines 56 and 57 has since been removed. It produced all odd lines with error output ```ign:...```. Th even lines in between are the output from the ```e``` and ```E``` callback.
The third - numeric - item in the ```.ign()```error message shows the buttons still touched in binary coding: left button bit 1 with value 2, and right button with value 1. As soon as this value goes to 0 the error is resolved and uppercase ```E``` touch signature shows and status is set to start (```"s"```). Lowercase l and r - touch events - set the bits, uperase L and R - untouch events - clear the bits... as simple as that - controller state binary (line 59):
A suitable application of the Simple Bangle Touch Controller will be a BangleJS Soft Keyboard as outlined in post #14 of above mentioned conversation about touch buttons and events, but without the issues of the (integrated) touch controller implementation. It will be presented in a separate conversation.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Among others, the conversation about 2 questions regarding Button and touch event stirred me to take yet another - 3rd - stab at that subject of a Bangle Touch Controller. The first one was in the Bangle emulator when a Bangle was not yet available to me.
First and second failed because the nested condition checking and tracking of state became unbearable: lots of code, slow, and still a lot of unresolved (corner) cases, brittle and of limited functions.
This 3rd time, I use a state machine with direct addressed / called functions for the state and transitions. It consists of 2 implementations:
In this conversations it's about the simple controller. Main design goal was to enable easy exchange of the application callbacks to reuse the one-time setup of the controller. Therefore, the callbacks are passed as object properties in an object. The property names map to the events, states and transitions of he state machine of the controller and are called Touch Signatures. They can have these values:
Right touch for 0.17495727539 [sec] Left touch for 0.11331176757 [sec] at 1607677068.38404655456 Right touch for 0.83169555664 [sec] Left touch for 1.75540161132 [sec] at 1607677075.57887077331 B Both w/ details abt 2nd btn touch and 1st btn untouch at 0:58:0 for 1.31875610351 2nd touch after 1st t 0.02020263671 1st untouch aft 1st t 1.31472778320 2nd untouch aft 1st u 0.00402832031 2nd untouch aft 1st t 1.31875610351 =elapsed ign: bRr r 0 err e 3 ign: ebRrR R 3 err e 2 ign: e..Rr r 2 err e 2 ign: e..rR R 2 err e 3 ign: e..Rr r 3 err e 3 ign: e..rL L 3 err e 1 ign: e..LR R 1 err E 0
.sb=(.sb^("RL".indexOf(e)+1))||("rl".indexOf(e)+1)```
A suitable application of the Simple Bangle Touch Controller will be a BangleJS Soft Keyboard as outlined in post #14 of above mentioned conversation about touch buttons and events, but without the issues of the (integrated) touch controller implementation. It will be presented in a separate conversation.