BangleApps/apps/calibration/app.js

152 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-05-11 09:09:34 +00:00
class BanglejsApp {
constructor() {
2022-05-27 10:20:09 +00:00
this.maxSamples = 16;
this.target = {
xMin: Math.floor(0.1 * g.getWidth()),
xMax: Math.floor(0.9 * g.getWidth()),
yMin: Math.floor(0.1 * g.getHeight()),
yMax: Math.floor(0.9 * g.getHeight()),
};
2022-05-11 09:09:34 +00:00
this.x = 0;
this.y = 0;
this.step = 0;
2022-05-11 09:09:34 +00:00
this.settings = {
2022-05-27 10:20:09 +00:00
xoffset: [0],
yoffset: [0],
xMaxActual: [this.target.xMax],
yMaxActual: [this.target.yMax],
2022-05-11 09:09:34 +00:00
};
}
load_settings() {
let settings = require('Storage').readJSON('calibration.json', true) || {active: false};
console.log('loaded settings:');
console.log(settings);
return settings;
}
2022-05-27 10:20:09 +00:00
getMedian(array){
array.sort();
let i = Math.floor(array.length/2);
if ( array.length % 2 && array.length > 1 ){
return (array[i]+array[i+1])/2;
} else {
return array[i];
}
}
getMedianSettings(){
let medianSettings = {
xoffset: this.getMedian(this.settings.xoffset),
yoffset: this.getMedian(this.settings.yoffset)
};
medianSettings.xscale = this.target.xMax / (medianSettings.xoffset + this.getMedian(this.settings.xMaxActual));
medianSettings.yscale = this.target.yMax / (medianSettings.yoffset + this.getMedian(this.settings.yMaxActual));
return medianSettings;
}
2022-05-11 09:09:34 +00:00
save_settings() {
2022-05-27 10:20:09 +00:00
let settingsToSave = this.getMedianSettings();
settingsToSave.active = true;
settingsToSave.reload = false;
require('Storage').writeJSON('calibration.json', settingsToSave);
2022-05-11 09:09:34 +00:00
2022-05-27 10:20:09 +00:00
console.log('saved settings:', settingsToSave);
2022-05-11 09:09:34 +00:00
}
explain() {
/*
* TODO:
* Present how to use the application
*
*/
}
drawTarget() {
switch (this.step){
case 0:
2022-05-27 10:20:09 +00:00
this.x = this.target.xMin;
this.y = this.target.yMin;
break;
case 1:
2022-05-27 10:20:09 +00:00
this.x = this.target.xMax;
this.y = this.target.yMin;
break;
case 2:
2022-05-27 10:20:09 +00:00
this.x = this.target.xMin;
this.y = this.target.yMax;
break;
case 3:
2022-05-27 10:20:09 +00:00
this.x = this.target.xMax;
this.y = this.target.yMax;
break;
}
2022-05-11 09:09:34 +00:00
2022-05-27 10:20:09 +00:00
g.clearRect(0, 0, g.getWidth(), g.getHeight());
g.setColor(g.theme.fg);
2022-05-11 09:09:34 +00:00
g.drawLine(this.x, this.y - 5, this.x, this.y + 5);
g.drawLine(this.x - 5, this.y, this.x + 5, this.y);
g.setFont('Vector', 10);
2022-05-27 10:20:09 +00:00
let medianSettings = this.getMedianSettings();
g.drawString('current offset: ' + medianSettings.xoffset.toFixed(3) + ', ' + medianSettings.yoffset.toFixed(3), 2, (g.getHeight()/2)-6);
g.drawString('current scale: ' + medianSettings.xscale.toFixed(3) + ', ' + medianSettings.yscale.toFixed(3), 2, (g.getHeight()/2)+6);
2022-05-11 09:09:34 +00:00
}
2022-05-27 10:20:09 +00:00
2022-05-11 09:09:34 +00:00
setOffset(xy) {
switch (this.step){
case 0:
2022-05-27 10:20:09 +00:00
this.settings.xoffset.push(this.x - xy.x);
this.settings.yoffset.push(this.y - xy.y);
break;
case 1:
2022-05-27 10:20:09 +00:00
this.settings.xMaxActual.push(xy.x);
this.settings.yoffset.push(this.y - xy.y);
break;
case 2:
2022-05-27 10:20:09 +00:00
this.settings.xoffset.push(this.x - xy.x);
this.settings.yMaxActual.push(xy.y);
break;
case 3:
this.settings.xMaxActual.push(xy.x);
this.settings.yMaxActual.push(xy.y);
break;
}
2022-05-27 10:20:09 +00:00
for (let c in this.settings){
if (this.settings[c].length > this.maxSamples) this.settings[c] = this.settings[c].slice(1, this.maxSamples);
}
}
2022-05-27 10:20:09 +00:00
nextStep() {
this.step++;
2022-05-27 10:20:09 +00:00
if ( this.step == 4 ) this.step = 0;
2022-05-11 09:09:34 +00:00
}
}
E.srand(Date.now());
2024-03-13 10:51:40 +00:00
const calibration = new BanglejsApp();
2022-05-11 09:09:34 +00:00
calibration.load_settings();
Bangle.disableCalibration = true;
function touchHandler (btn, xy){
if (xy) calibration.setOffset(xy);
calibration.nextStep();
2022-05-27 10:20:09 +00:00
calibration.drawTarget();
}
2022-05-11 09:09:34 +00:00
let modes = {
mode : 'custom',
btn : function(n) {
calibration.save_settings(this.settings);
load();
},
touch : touchHandler,
2022-05-11 09:09:34 +00:00
};
Bangle.setUI(modes);
2022-05-27 10:20:09 +00:00
calibration.drawTarget();