-
• #27
thank you so much, it works great ;)
-
• #29
This code tried to become a module in http://espruino.com/modules a long time ago - at a time when I was not yet familiar enough to pull that thru. Even though the code is now pretty 'old', it is still in demand. Therefore, as a first step, I make a condensed version (w/ examples) that you can copy into your application to take advantage of SWButtons without having to get rid of all the noise by the comments. Second, I'll add files you can copy into your sandbox project modules folder to use the code as real module using
var SWBtn = require("SWButton");
.NOTE for previous users: I switched to
.enable()
from.disable()
.The example code does the following:
- uses BTN (or BTN1 when more than one button is on the board)
- short press toggles the red LED
- long press toggles the green LED
- short-long press sequence turns the red LED on
- short-short press sequence turns the red LED off
- long-long press sequence turns the green LED on
- long-short press sequence turns the green LED off
First, in-line module code w/ example:
// SWButtonInline.js // 2022.0410 - (c) allObjects; var SWBtn = // inline module emulation of // var SWBtn = require("SWButton"); (function(){ var exports = {}; // module begin: var SWBtn = function(f,b,e) { this.f = (f) ? f : function(){}; this.b = (b) ? b : BTN1; this.t = null; this.k = null; this.w = null; this.enable(e); }; SWBtn.prototype.C = // Config shared by all instances of SWBtn: { B: 20 // debounce [ms] , L: 0.250 // min Long press [s] , P: 220 // min Pause [ms] , D: 10 // delay of fnc function invocation [ms] }; SWBtn.prototype.enable = function(e) { if (e === undefined || e) { if (!this.w) { this.d = false; this.k = ""; var _this = this; this.w = setWatch( function(e){ _this.c(e); }, this.b , { repeat:true , edge:"both" // <--- required for built-in buttons! , debounce:_this.C.B } ); } } else { if (this.w) { this.d = true; this.w = clearWatch(this.w); if (this.t) this.t = clearTimeout(this.t); } } }; SWBtn.prototype.c = function(e){ // change of state - called by set watch if (e.state) { if (this.t) this.t = clearTimeout(this.t); } else { this.k = this.k + ((e.time - e.lastTime < this.C.L) ? "S" :"L"); var _this = this; this.t = setTimeout(function(){ _this.e(); }, this.C.P); } }; SWBtn.prototype.e = function() { this.t = null; var _k = this.k; if (_k.length > 0) { this.k = ""; var _this = this; setTimeout(function(){ _this.f(_k); },this.C.D); } }; exports = SWBtn; // module end return exports; })(); // :in-line module emulation end var functs = // function names match Short/Long key press pattern { S: function(){ LED1.toggle(); } , L: function(){ LED2.toggle(); } , SL: function(){ LED1.set(); } , SS: function(){ LED1.reset(); } , LL: function(){ LED2.set(); } , LS: function(){ LED2.reset(); } }; var mySWBtn = new SWBtn(function(k){ console.log("BTN1 detected " + k); // log detected key pattern and... if (functs[k]) { functs[k](); } // ...dispatch if defined },BTN1,false); // set it up disabled function onInit() { mySWBtn.enable(); } setTimeout(onInit,999); // for dev only; remove before upload for save()
- uses BTN (or BTN1 when more than one button is on the board)
-
• #30
can the button be made into like receiving morse code?
then you can have 26 (or more?) conditions?
thanks
-
• #31
Sure... the challenge is syncing to the timings the emitting side uses.
Some 'AI' has to detect over some time what the emitter's times are for short, long, break between 'beeps' and then characters and then words, and then apply it retrospectively. On the Web there is some code that already does this. After initial syncing, adjusting the sync has then to happen continuously.
-
• #32
youtube:
https://www.youtube.com/watch?v=zx6Rh2AQBcQ
https://www.reddit.com/r/nextfuckinglevel/comments/p0qryj/this_guy_is_coding_with_one_key/?rdt=40995【只有一个键的键盘,靠输入摩斯密码来写代码,程序员都看哭了!-哔哩哔哩】 https://b23.tv/P3jlGza
use 1 button to input morse code.
see if you can view this video.
thanks
Updated github... version at https://raw.githubusercontent.com/muet/EspruinoDocs/master/modules/SWButton.js is back to work... ;-)
Lesson(s) learned: Don't trust any defaults! ...they may change and make your life a misery.