Adds scaling to correct for linear mismatches in coordinates

pull/1892/head
Martin Boonk 2022-05-27 10:53:52 +02:00
parent c7231d243e
commit 056adb7873
2 changed files with 70 additions and 23 deletions

View File

@ -1,26 +1,24 @@
class BanglejsApp { class BanglejsApp {
constructor() { constructor() {
this.updateFactor = 0.2;
this.x = 0; this.x = 0;
this.y = 0; this.y = 0;
this.step = 0;
this.settings = { this.settings = {
xoffset: 0, xoffset: 0,
yoffset: 0, yoffset: 0,
xscale: 1,
yscale: 1,
}; };
} }
load_settings() { load_settings() {
let settings = require('Storage').readJSON('calibration.json', true) || {active: false}; let settings = require('Storage').readJSON('calibration.json', true) || {active: false};
// do nothing if the calibration is deactivated
if (settings.active === true) {
// cancel the calibration offset
Bangle.on('touch', function(button, xy) {
xy.x += settings.xoffset;
xy.y += settings.yoffset;
});
}
if (!settings.xoffset) settings.xoffset = 0; if (!settings.xoffset) settings.xoffset = 0;
if (!settings.yoffset) settings.yoffset = 0; if (!settings.yoffset) settings.yoffset = 0;
if (!settings.xscale) settings.xscale = 1;
if (!settings.yscale) settings.yscale = 1;
console.log('loaded settings:'); console.log('loaded settings:');
console.log(settings); console.log(settings);
@ -46,19 +44,63 @@ class BanglejsApp {
} }
drawTarget() { drawTarget() {
this.x = 16 + Math.floor(Math.random() * (g.getWidth() - 32)); switch (this.step){
this.y = 40 + Math.floor(Math.random() * (g.getHeight() - 80)); case 0:
this.x = Math.floor(0.2 * g.getWidth());
this.y = Math.floor(0.2 * g.getHeight());
break;
case 1:
this.x = Math.floor(0.8 * g.getWidth());
this.y = Math.floor(0.2 * g.getHeight());
break;
case 2:
this.x = Math.floor(0.5 * g.getWidth());
this.y = Math.floor(0.5 * g.getHeight());
break;
case 3:
this.x = Math.floor(0.2 * g.getWidth());
this.y = Math.floor(0.8 * g.getHeight());
break;
case 4:
this.x = Math.floor(0.8 * g.getWidth());
this.y = Math.floor(0.8 * g.getHeight());
break;
}
g.clearRect(0, 0, g.getWidth(), g.getHeight()); g.setColor(g.theme.fg);
g.drawLine(this.x, this.y - 5, this.x, this.y + 5); 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.drawLine(this.x - 5, this.y, this.x + 5, this.y);
g.setFont('Vector', 10); g.setFont('Vector', 10);
g.drawString('current offset: ' + this.settings.xoffset + ', ' + this.settings.yoffset, 0, 24); g.drawString('current offset: ' + this.settings.xoffset.toFixed(3) + ', ' + this.settings.yoffset.toFixed(3), 2, 2);
g.drawString('current scale: ' + this.settings.xscale.toFixed(3) + ', ' + this.settings.yscale.toFixed(3), 2, 12);
} }
setOffset(xy) { setOffset(xy) {
this.settings.xoffset = Math.round((this.settings.xoffset + (this.x - Math.floor((this.x + xy.x)/2)))/2); this.last=xy;
this.settings.yoffset = Math.round((this.settings.yoffset + (this.y - Math.floor((this.y + xy.y)/2)))/2); switch (this.step){
case 0:
this.settings.xoffset = this.settings.xoffset * (1-this.updateFactor) + (this.x - xy.x) * this.updateFactor;
this.settings.yoffset = this.settings.yoffset * (1-this.updateFactor) + (this.y - xy.y) * this.updateFactor;
break;
case 1:
this.settings.xscale = this.settings.xscale * (1-this.updateFactor) + ((xy.x + this.settings.xoffset) / this.x) * this.updateFactor;
this.settings.yoffset = this.settings.yoffset * (1-this.updateFactor) + (this.y - xy.y) * this.updateFactor;
break;
case 3:
this.settings.xoffset = this.settings.xoffset * (1-this.updateFactor) + (this.x - xy.x) * this.updateFactor;
this.settings.yscale = this.settings.yscale * (1-this.updateFactor) + ((xy.y + this.settings.yoffset) / this.y) * this.updateFactor;
break;
case 2:
case 4:
this.settings.xscale = this.settings.xscale * (1-this.updateFactor) + ((xy.x + this.settings.xoffset) / this.x) * this.updateFactor;
this.settings.yscale = this.settings.yscale * (1-this.updateFactor) + ((xy.y + this.settings.yoffset) / this.y) * this.updateFactor;
break;
}
}
nextStep() {
this.step++;
if ( this.step == 5 ) this.step = 0;
} }
} }
@ -67,6 +109,14 @@ E.srand(Date.now());
calibration = new BanglejsApp(); calibration = new BanglejsApp();
calibration.load_settings(); calibration.load_settings();
Bangle.disableCalibration = true;
function touchHandler (btn, xy){
g.clearRect(0, 0, g.getWidth(), g.getHeight());
if (xy) calibration.setOffset(xy);
calibration.drawTarget();
calibration.nextStep();
}
let modes = { let modes = {
mode : 'custom', mode : 'custom',
@ -74,10 +124,7 @@ let modes = {
calibration.save_settings(this.settings); calibration.save_settings(this.settings);
load(); load();
}, },
touch : function(btn, xy) { touch : touchHandler,
calibration.setOffset(xy);
calibration.drawTarget();
},
}; };
Bangle.setUI(modes); Bangle.setUI(modes);
calibration.drawTarget(); touchHandler();

View File

@ -1,7 +1,7 @@
let cal_settings = require('Storage').readJSON("calibration.json", true) || {active: false}; let cal_settings = require('Storage').readJSON("calibration.json", true) || {active: false};
Bangle.on('touch', function(button, xy) { Bangle.on('touch', function(button, xy) {
// do nothing if the calibration is deactivated // do nothing if the calibration is deactivated
if (cal_settings.active === false) return; if (cal_settings.active === false || Bangle.disableCalibration) return;
// reload the calibration offset at each touch event /!\ bad for the flash memory // reload the calibration offset at each touch event /!\ bad for the flash memory
if (cal_settings.reload === true) { if (cal_settings.reload === true) {
@ -9,6 +9,6 @@ Bangle.on('touch', function(button, xy) {
} }
// apply the calibration offset // apply the calibration offset
xy.x += cal_settings.xoffset; xy.x = Math.round((xy.x + cal_settings.xoffset) * cal_settings.xscale);
xy.y += cal_settings.yoffset; xy.y = Math.round((xy.y + cal_settings.yoffset) * cal_settings.yscale);
}); });