Skip to content

Instantly share code, notes, and snippets.

@gfwilliams
Created December 11, 2019 10:39
Show Gist options
  • Save gfwilliams/c4f3600853f1ad88ebedef297bee1caf to your computer and use it in GitHub Desktop.
Save gfwilliams/c4f3600853f1ad88ebedef297bee1caf to your computer and use it in GitHub Desktop.
/*
Binary search keyboard for typing with
the touchscreen
*/
var storage = require('Storage');
const settings = storage.readJSON('@setting') || { HID: false };
const KEY = {
A : 4 ,
B : 5 ,
C : 6 ,
D : 7 ,
E : 8 ,
F : 9 ,
G : 10,
H : 11,
I : 12,
J : 13,
K : 14,
L : 15,
M : 16,
N : 17,
O : 18,
P : 19,
Q : 20,
R : 21,
S : 22,
T : 23,
U : 24,
V : 25,
W : 26,
X : 27,
Y : 28,
Z : 29,
1 : 30,
2 : 31,
3 : 32,
4 : 33,
5 : 34,
6 : 35,
7 : 36,
8 : 37,
9 : 38,
0 : 39
};
function showChars(x,chars) {
var lines = Math.round(Math.sqrt(chars.length)*2);
g.setFontAlign(0,0);
var sy = Math.round(200/lines);
var sx = sy;
g.setFont("Vector", sy-2);
var y = (240 - lines*sy);
var last = 0;
for (var i=0;i<lines;i++) {
var n = Math.round(chars.length*(i+1)/lines);
var xo = x + (120 - sx*(n-last-1))/2;
for (var j=last;j<n;j++)
g.drawString(chars[j], xo + (j-last)*sx, y + sy*i)
last = n;
}
}
function show(chars,callback) {
g.clear();
if (chars.length==1) {
callback(chars);
return;
}
var m = chars.length/2;
charl=chars.slice(0,m);
charr=chars.slice(m);
showChars(0,charl);
showChars(120,charr);
setWatch(() => {
clearWatch();
show(charl,callback);
}, BTN4);
setWatch(() => {
clearWatch();
show(charr,callback);
}, BTN5);
}
function getCharacter() {
return new Promise(resolve => {
show("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",resolve);
});
}
function startKeyboardHID() {
getCharacter().then(ch => {
console.log("typed "+JSON.stringify(ch));
}).then(startKeyboardHID);
};
startKeyboardHID();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment