1
0
Fork 0

fix time formatting

master
7kasper 2022-01-02 20:29:28 +01:00
parent 602679c6bc
commit 10a50fdde2
No known key found for this signature in database
GPG Key ID: 4AD2FD3E13BFB86A
1 changed files with 254 additions and 246 deletions

View File

@ -63,81 +63,80 @@ const SpecialReport = new Uint8Array([
]); ]);
const MouseButton = { const MouseButton = {
NONE : 0, NONE : 0,
LEFT : 1, LEFT : 1,
RIGHT : 2, RIGHT : 2,
MIDDLE : 4, MIDDLE : 4,
BACK : 8, BACK : 8,
FORWARD: 16 FORWARD: 16
}; };
const kb = require("ble_hid_keyboard"); const kb = require("ble_hid_keyboard");
const Layout = require("Layout"); const Layout = require("Layout");
const Locale = require("locale"); const Locale = require("locale");
let mainLayout = new Layout( { let mainLayout = new Layout({
'type': 'v', 'type': 'v',
filly: 1, filly: 1,
c: [ c: [
{ {
type: 'txt', type: 'txt',
font: '6x8', font: '6x8',
label: 'Presentor', label: 'Presentor',
valign: -1, valign: -1,
halign: 0, halign: 0,
col: g.theme.fg1, col: g.theme.fg1,
// bgCol: g.theme.bg2, // bgCol: g.theme.bg2,
bgCol: '#00F', bgCol: '#00F',
fillx: 1, fillx: 1,
}, { }, {
type: 'h', type: 'h',
fillx: 1, fillx: 1,
c: [ c: [
{ {
type: 'txt', type: 'txt',
font: '15%', font: '15%',
label: '00:00', label: '00:00',
id: 'Time', id: 'Time',
halign: -1, halign: -1,
pad: 3 pad: 3
}, { }, {
fillx: 1 fillx: 1
}, { }, {
type: 'txt', type: 'txt',
font: '15%', font: '15%',
label: '00:00', label: '00:00',
id: 'Timer', id: 'Timer',
halign: 1, halign: 1,
pad: 3 pad: 3
} }
] ]
}, { }, {
type: 'txt', type: 'txt',
font: '10%', font: '10%',
label: '+00:00', label: '+00:00',
id: 'RestTime', id: 'RestTime',
col: '#fff' col: '#fff'
}, { }, {
type: 'txt', type: 'txt',
font: '10%', font: '10%',
label: '--------------' label: '--------------'
}, { }, {
type: 'txt', type: 'txt',
font: '15%', font: '15%',
label: 'Presenting', label: 'Presenting',
id: 'Subject' id: 'Subject'
}, { }, {
type: 'txt', type: 'txt',
font: '6x8', font: '6x8',
label: 'Swipe down to start the time.', label: 'Swipe down to start the time.',
id: 'Notes', id: 'Notes',
col: '#ff0', col: '#ff0',
fillx: 1, fillx: 1,
filly: 1, filly: 1,
valign: 1 valign: 1
} }
]
]
}, {lazy:true}); }, {lazy:true});
let settings = {pparts: [], sversion: 0}; let settings = {pparts: [], sversion: 0};
@ -172,84 +171,93 @@ let clearToSend = true;
// Presentation Timers // Presentation Timers
let ptimers = []; let ptimers = [];
function loadSettings() { function formatTimePart(time) {
settings = require("Storage").readJSON('presentor.json'); time = Math.floor(Math.abs(time));
for (let i = 0; i < settings.pparts.length; i++) { return time < 10 ? `0${time}` : `${time}`;
ptimers[i] = {
active: false,
tracked: -1,
left: settings.pparts[i].minutes * 60 + settings.pparts[i].seconds
};
} }
function formatTime(time, doPlus) {
return `${time < 0 ? '-' : (doPlus ? '+' : '')}${formatTimePart(time/60)}:${formatTimePart(time%60)}`;
}
function loadSettings() {
settings = require("Storage").readJSON('presentor.json');
for (let i = 0; i < settings.pparts.length; i++) {
ptimers[i] = {
active: false,
tracked: -1,
left: settings.pparts[i].minutes * 60 + settings.pparts[i].seconds
};
}
} }
function getCurrentTimer() { function getCurrentTimer() {
if (pparti < 0) return '00:00'; if (pparti < 0) return '00:00';
if (!settings.pparts || pparti >= settings.pparts.length) return '00:00'; if (!settings.pparts || pparti >= settings.pparts.length) return '00:00';
if (ptimers[pparti].tracked == -1) return '00:00'; if (ptimers[pparti].tracked == -1) return '00:00';
ptimers[pparti].left -= (getTime() - ptimers[pparti].tracked); ptimers[pparti].left -= (getTime() - ptimers[pparti].tracked);
ptimers[pparti].tracked = getTime(); ptimers[pparti].tracked = getTime();
return `${Math.floor(ptimers[pparti].left/60)}:${Math.floor(ptimers[pparti].left%60)}`; return formatTime(ptimers[pparti].left);
} }
function getRestTime() { function getRestTime() {
let rem = 0; let rem = 0;
// Add all remaining time from previous presentation parts. // Add all remaining time from previous presentation parts.
for (let i = 0; i < pparti; i++) { for (let i = 0; i < pparti; i++) {
rem += ptimers[i].left; rem += ptimers[i].left;
} }
return `${rem >= 0 ? '+':'-'}${Math.floor(rem/60)}:${Math.floor(rem%60)}`; return formatTime(rem, true);
} }
function drawMainFrame() { function drawMainFrame() {
var d = new Date(); var d = new Date();
// update time // update time
mainLayout.Time.label = Locale.time(d,1); mainLayout.Time.label = Locale.time(d,1);
// update timer // update timer
mainLayout.Timer.label = getCurrentTimer(); mainLayout.Timer.label = getCurrentTimer();
mainLayout.RestTime.label = getRestTime(); mainLayout.RestTime.label = getRestTime();
mainLayout.render(); mainLayout.render();
// schedule a draw for the next minute // schedule a draw for the next minute
if (timeoutDraw != -1) clearTimeout(timeoutDraw); if (timeoutDraw != -1) clearTimeout(timeoutDraw);
timeoutDraw = setTimeout(function() { timeoutDraw = setTimeout(function() {
timeoutDraw = -1; timeoutDraw = -1;
drawMainFrame(); drawMainFrame();
}, 1000 - (Date.now() % 1000)); }, 1000 - (Date.now() % 1000));
} }
function drawMain() { function drawMain() {
g.clear(); g.clear();
mainLayout.forgetLazyState(); mainLayout.forgetLazyState();
drawMainFrame(); drawMainFrame();
// mainLayout.render(); // mainLayout.render();
// E.showMessage('Presentor'); // E.showMessage('Presentor');
} }
function doPPart(r) { function doPPart(r) {
pparti += r; pparti += r;
if (pparti < 0) return; if (pparti < 0) return;
if (!settings.pparts || pparti >= settings.pparts.length) return; if (!settings.pparts || pparti >= settings.pparts.length) return;
let ppart = settings.pparts[pparti]; let ppart = settings.pparts[pparti];
mainLayout.Subject.label = ppart.subject; mainLayout.Subject.label = ppart.subject;
mainLayout.Notes.label = ppart.notes; mainLayout.Notes.label = ppart.notes;
ptimers[pparti].tracked = getTime(); ptimers[pparti].tracked = getTime();
drawMainFrame(); drawMainFrame();
} }
NRF.setServices(undefined, { hid : SpecialReport }); NRF.setServices(undefined, { hid : SpecialReport });
// TODO: figure out how to detect HID. // TODO: figure out how to detect HID.
NRF.on('HID', function() { NRF.on('HID', function() {
HIDenabled = true; HIDenabled = true;
}); });
function moveMouse(x,y,b,wheel,hwheel,callback) { function moveMouse(x,y,b,wheel,hwheel,callback) {
if (!HIDenabled) return; if (!HIDenabled) return;
if (!b) b = 0; if (!b) b = 0;
if (!wheel) wheel = 0; if (!wheel) wheel = 0;
if (!hwheel) hwheel = 0; if (!hwheel) hwheel = 0;
NRF.sendHIDReport([1,b,x,y,wheel,hwheel,0,0], function() { NRF.sendHIDReport([1,b,x,y,wheel,hwheel,0,0], function() {
if (callback) callback(); if (callback) callback();
}); });
} }
// function getSign(x) { // function getSign(x) {
@ -257,160 +265,160 @@ NRF.sendHIDReport([1,b,x,y,wheel,hwheel,0,0], function() {
// } // }
function scroll(wheel,hwheel,callback) { function scroll(wheel,hwheel,callback) {
moveMouse(0,0,0,wheel,hwheel,callback); moveMouse(0,0,0,wheel,hwheel,callback);
} }
// Single click a certain button (immidiatly release). // Single click a certain button (immidiatly release).
function clickMouse(b, callback) { function clickMouse(b, callback) {
if (!HIDenabled) return; if (!HIDenabled) return;
NRF.sendHIDReport([1,b,0,0,0,0,0,0], function() { NRF.sendHIDReport([1,b,0,0,0,0,0,0], function() {
NRF.sendHIDReport([1,0,0,0,0,0,0,0], function() { NRF.sendHIDReport([1,0,0,0,0,0,0,0], function() {
if (callback) callback(); if (callback) callback();
});
}); });
});
} }
function pressKey(keyCode, modifiers, callback) { function pressKey(keyCode, modifiers, callback) {
if (!HIDenabled) return; if (!HIDenabled) return;
if (!modifiers) modifiers = 0; if (!modifiers) modifiers = 0;
NRF.sendHIDReport([2, modifiers,0,keyCode,0,0,0,0], function() { NRF.sendHIDReport([2, modifiers,0,keyCode,0,0,0,0], function() {
NRF.sendHIDReport([2,0,0,0,0,0,0,0], function() { NRF.sendHIDReport([2,0,0,0,0,0,0,0], function() {
if (callback) callback(); if (callback) callback();
});
}); });
});
} }
function handleAcc(acc) { function handleAcc(acc) {
let rRoll = acc.y * -50; let rRoll = acc.y * -50;
let rPitch = acc.x * -100; let rPitch = acc.x * -100;
if (mCal > 10) { if (mCal > 10) {
//console.log("x: " + (rRoll - homeRoll) + " y:" + (rPitch - homePitch)); //console.log("x: " + (rRoll - homeRoll) + " y:" + (rPitch - homePitch));
moveMouse(acc.y * -50 - homeRoll, acc.x * -100 - homePitch); moveMouse(acc.y * -50 - homeRoll, acc.x * -100 - homePitch);
} else { } else {
//console.log("homeroll: " +homeRoll +"homepitch: " + homePitch); //console.log("homeroll: " +homeRoll +"homepitch: " + homePitch);
homeRoll = rRoll * 0.7 + homeRoll * 0.3; homeRoll = rRoll * 0.7 + homeRoll * 0.3;
homePitch = rPitch * 0.7 + homePitch * 0.3; homePitch = rPitch * 0.7 + homePitch * 0.3;
mCal = mCal + 1; mCal = mCal + 1;
} }
} }
Bangle.on('lock', function(on) { Bangle.on('lock', function(on) {
if (on && holding) { if (on && holding) {
Bangle.setLocked(false); Bangle.setLocked(false);
Bangle.setLCDPower(1); Bangle.setLCDPower(1);
} }
}); });
function startHolding() { function startHolding() {
pressKey(kb.KEY.F10); pressKey(kb.KEY.F10);
holding = true; holding = true;
Bangle.buzz(); Bangle.buzz();
E.showMessage('Holding'); E.showMessage('Holding');
Bangle.on('accel', handleAcc); Bangle.on('accel', handleAcc);
Bangle.setLCDPower(1); Bangle.setLCDPower(1);
} }
function stopHolding() { function stopHolding() {
clearTimeout(timeoutId); clearTimeout(timeoutId);
if (holding) { if (holding) {
pressKey(kb.KEY.F10); pressKey(kb.KEY.F10);
homePitch = 0; homePitch = 0;
homeRoll = 0; homeRoll = 0;
holding = false; holding = false;
mCal = 0; mCal = 0;
Bangle.removeListener('accel', handleAcc); Bangle.removeListener('accel', handleAcc);
Bangle.buzz(); Bangle.buzz();
drawMain(); drawMain();
} else { } else {
timeoutId = setTimeout(drawMain, 1000); timeoutId = setTimeout(drawMain, 1000);
} }
clearTimeout(timeoutHolding); clearTimeout(timeoutHolding);
timeoutHolding = -1; timeoutHolding = -1;
} }
Bangle.on('drag', function(e) { Bangle.on('drag', function(e) {
if (cttl == 0) { cttl = getTime(); } if (cttl == 0) { cttl = getTime(); }
if (trackPadMode) { if (trackPadMode) {
if (lastx + lasty == 0) { if (lastx + lasty == 0) {
lastx = e.x; lastx = e.x;
lasty = e.y; lasty = e.y;
mttl = getTime(); mttl = getTime();
}
if (clearToSend) {
clearToSend = false;
let difX = e.x - lastx, difY = e.y - lasty;
let dT = getTime() - mttl;
let vX = difX / dT, vY = difY / dT;
//let qX = getSign(difX) * Math.pow(Math.abs(difX), 1.2);
//let qY = getSign(difY) * Math.pow(Math.abs(difY), 1.2);
let qX = difX + 0.02 * vX, qY = difY + 0.02 * vY;
moveMouse(qX, qY, 0, 0, 0, function() {
setTimeout(function() {clearToSend = true;}, 50);
});
lastx = e.x;
lasty = e.y;
mttl = getTime();
console.log("Dx: " + (qX) + " Dy: " + (qY));
}
if (!e.b) {
// short press
if (getTime() - cttl < 0.2) {
clickMouse(MouseButton.LEFT);
console.log("click left");
} }
// longer press in center if (clearToSend) {
else if (getTime() - cttl < 0.6 && e.x > g.getWidth()/4 && e.x < 3 * g.getWidth()/4 && e.y > g.getHeight() / 4 && e.y < 3 * g.getHeight() / 4) { clearToSend = false;
clickMouse(MouseButton.RIGHT); let difX = e.x - lastx, difY = e.y - lasty;
console.log("click right"); let dT = getTime() - mttl;
let vX = difX / dT, vY = difY / dT;
//let qX = getSign(difX) * Math.pow(Math.abs(difX), 1.2);
//let qY = getSign(difY) * Math.pow(Math.abs(difY), 1.2);
let qX = difX + 0.02 * vX, qY = difY + 0.02 * vY;
moveMouse(qX, qY, 0, 0, 0, function() {
setTimeout(function() {clearToSend = true;}, 50);
});
lastx = e.x;
lasty = e.y;
mttl = getTime();
console.log("Dx: " + (qX) + " Dy: " + (qY));
} }
cttl = 0; if (!e.b) {
lastx = 0; // short press
lasty = 0; if (getTime() - cttl < 0.2) {
} clickMouse(MouseButton.LEFT);
} else { console.log("click left");
if(!e.b){ }
Bangle.buzz(100); // longer press in center
if(lasty > 40){ else if (getTime() - cttl < 0.6 && e.x > g.getWidth()/4 && e.x < 3 * g.getWidth()/4 && e.y > g.getHeight() / 4 && e.y < 3 * g.getHeight() / 4) {
doPPart(1); clickMouse(MouseButton.RIGHT);
E.showMessage('down'); console.log("click right");
} else if(lasty < -40){ }
doPPart(-1); cttl = 0;
E.showMessage('up'); lastx = 0;
} else if(lastx > 40){ lasty = 0;
E.showMessage('right');
//kb.tap(kb.KEY.RIGHT, 0);
scroll(-1);
} else if(lastx < -40){
E.showMessage('left');
//kb.tap(kb.KEY.LEFT, 0);
scroll(1);
} else if(lastx==0 && lasty==0 && holding == false){
E.showMessage('press');
clickMouse(MouseButton.LEFT);
} }
stopHolding(); } else {
lastx = 0; if(!e.b){
lasty = 0; Bangle.buzz(100);
} else{ if(lasty > 40){
lastx = lastx + e.dx; doPPart(1);
lasty = lasty + e.dy; E.showMessage('down');
if (timeoutHolding == -1) { } else if(lasty < -40){
timeoutHolding = setTimeout(startHolding, 500); doPPart(-1);
E.showMessage('up');
} else if(lastx > 40){
E.showMessage('right');
//kb.tap(kb.KEY.RIGHT, 0);
scroll(-1);
} else if(lastx < -40){
E.showMessage('left');
//kb.tap(kb.KEY.LEFT, 0);
scroll(1);
} else if(lastx==0 && lasty==0 && holding == false){
E.showMessage('press');
clickMouse(MouseButton.LEFT);
}
stopHolding();
lastx = 0;
lasty = 0;
} else{
lastx = lastx + e.dx;
lasty = lasty + e.dy;
if (timeoutHolding == -1) {
timeoutHolding = setTimeout(startHolding, 500);
}
} }
} }
}
}); });
function onBtn() { function onBtn() {
if (trackPadMode) { if (trackPadMode) {
trackPadMode = false; trackPadMode = false;
stopHolding(); stopHolding();
drawMain(); drawMain();
} else { } else {
clearToSend = true; clearToSend = true;
trackPadMode = true; trackPadMode = true;
E.showMessage('Mouse'); E.showMessage('Mouse');
} }
Bangle.buzz(); Bangle.buzz();
} }
setWatch(onBtn, (process.env.HWVERSION==2) ? BTN1 : BTN2, {repeat: true}); setWatch(onBtn, (process.env.HWVERSION==2) ? BTN1 : BTN2, {repeat: true});