// puckD1extPushButton2.js
pinMode(D1,"input_pullup");
var log = function() { console.log(arguments); }
, lon = false // log on / off
, wId
, change = function(evt) {
if (lon) log(evt);
if (evt.state) {
setTimeout(note,1,(Math.floor(evt.time - evt.lastTime)));
}
}
, note = function (t) { // note (for show)
console.log(t);
}
, start = function() {
wId = setWatch(change,D1
,{repeat:true, edge:"both",debounce:10});
}
;
function onInit() {
start();
}
setTimeout(onInit,999); // comment line before upload for save()
If you turn the logging on (lon = true), then you see what is goin on:
The evt is the event object passed to the function change() called by the watch. The watch is now setup repeat:true and edge:"both" : In other words, change() is called on press AND on release of the push button. The event object passed has 3 very convenient properties:
The state tells the state of the pin / what the has changed to - on press it is L/false and on release it is H/true,
The lastTime tells when the watch fired the last time. If it is the first time, then it is undefined
The time tells the time of the current watch firing.
Using the event object simplifies the code a lot. Below show the log of the event object. Notice the first time - when the button was pressed for the first time - lastTime is undefined. On press event of the button we have to do nothing, but on release, we calculate the difference and pass it to the show function. In this example, the show function just logs the value in the console.
Notice, that with this solution, we could run into time binds: new press and release events may happen faster than display can happen. Some queueing and asynchronous display of the result can help to avoid it. Calling note() with a timeout makes it also asynchronous. The function note() would then put t into a FIFO / Queue and - when it is the first entry - start the display process. The display process would then take value by value out of the FIFO / Queue and show the value until the FIFO/Queue is empty. Consuming the queue follows the same asynchronous pattern as in previous example the show function.
Now, @user140111 - Alberto - it's your turn to complete the code and add the display logic with the queue and show. What makes the things so easy to understand is that there is only one thread executing JS... (speaking in os terms...).
(PS: My dad's first name was Albert - the German version of yours...).
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.
And here is the 'short' version:
If you turn the logging on (lon = true), then you see what is goin on:
The
evt
is the event object passed to the functionchange()
called by the watch. The watch is now setuprepeat:true
andedge:"both"
: In other words,change()
is called on press AND on release of the push button. The event object passed has 3 very convenient properties:state
tells the state of the pin / what the has changed to - on press it is L/false and on release it is H/true,lastTime
tells when the watch fired the last time. If it is the first time, then it isundefined
time
tells the time of the current watch firing.Using the event object simplifies the code a lot. Below show the log of the event object. Notice the first time - when the button was pressed for the first time - lastTime is undefined. On press event of the button we have to do nothing, but on release, we calculate the difference and pass it to the show function. In this example, the show function just logs the value in the console.
Notice, that with this solution, we could run into time binds: new press and release events may happen faster than display can happen. Some queueing and asynchronous display of the result can help to avoid it. Calling
note()
with a timeout makes it also asynchronous. The functionnote()
would then putt
into a FIFO / Queue and - when it is the first entry - start the display process. The display process would then take value by value out of the FIFO / Queue and show the value until the FIFO/Queue is empty. Consuming the queue follows the same asynchronous pattern as in previous example the show function.Now, @user140111 - Alberto - it's your turn to complete the code and add the display logic with the queue and show. What makes the things so easy to understand is that there is only one thread executing JS... (speaking in os terms...).
(PS: My dad's first name was Albert - the German version of yours...).