diff --git a/apps/calibration/README.md b/apps/calibration/README.md new file mode 100644 index 000000000..37f637d21 --- /dev/null +++ b/apps/calibration/README.md @@ -0,0 +1,11 @@ +# Banglejs - Touchscreen calibration +A simple calibration app for the touchscreen + +## Usage + +Once lauched touch the cross that appear on the screen to make +another spawn elsewhere. + +each new touch on the screen will help to calibrate the offset +of your finger on the screen. After five or more input, press +the button to save the calibration and close the application. \ No newline at end of file diff --git a/apps/calibration/app-icon.js b/apps/calibration/app-icon.js new file mode 100644 index 000000000..af66c3f68 --- /dev/null +++ b/apps/calibration/app-icon.js @@ -0,0 +1 @@ +require("heatshrink").decompress(atob("mEwwkB/4AJ+EPBhQXg+BBDCyJaGGR5zIDBoQEL4QYOLYR3GBIouJR5AYBGBILBU5QMGFwgiFX4wwIEI4XGGBAgHd44+HD44XHNw4XWM5IIHCIoXWV5IXICQgXvLxAAKCYYXh5nMC6n8C4PPC5MAAA8PC4ZxBACAXOI653hU5zvJABASEC5PwHI4XcMBIXICIoXXJBAXHCAwXXJBAXHB5AfGC4ygJEAwXGQ5BoIQxoiDBYgXECwIuIBgb5ECIQJFGBQmCC4QHEDBwAFCxoYICx5ZELZoZJFiIXpA=")) \ No newline at end of file diff --git a/apps/calibration/app.js b/apps/calibration/app.js new file mode 100644 index 000000000..d3823de63 --- /dev/null +++ b/apps/calibration/app.js @@ -0,0 +1,85 @@ +class BanglejsApp { + constructor() { + this.x = 0; + this.y = 0; + this.settings = { + xoffset: 0, + yoffset: 0, + }; + } + + load_settings() { + 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.yoffset) settings.yoffset = 0; + + console.log('loaded settings:'); + console.log(settings); + + return settings; + } + + save_settings() { + this.settings.active = true; + this.settings.reload = false; + require('Storage').writeJSON('calibration.json', this.settings); + + console.log('saved settings:'); + console.log(this.settings); + } + + explain() { + /* + * TODO: + * Present how to use the application + * + */ + } + + drawTarget() { + this.x = 16 + Math.floor(Math.random() * (g.getWidth() - 32)); + this.y = 40 + Math.floor(Math.random() * (g.getHeight() - 80)); + + g.clearRect(0, 24, g.getWidth(), g.getHeight() - 24); + 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); + g.drawString('current offset: ' + this.settings.xoffset + ', ' + this.settings.yoffset, 0, 24); + } + + setOffset(xy) { + this.settings.xoffset = Math.round((this.settings.xoffset + (this.x - Math.floor((this.x + xy.x)/2)))/2); + this.settings.yoffset = Math.round((this.settings.yoffset + (this.y - Math.floor((this.y + xy.y)/2)))/2); + } +} + + +E.srand(Date.now()); +Bangle.loadWidgets(); +Bangle.drawWidgets(); + +calibration = new BanglejsApp(); +calibration.load_settings(); + +let modes = { + mode : 'custom', + btn : function(n) { + calibration.save_settings(this.settings); + load(); + }, + touch : function(btn, xy) { + calibration.setOffset(xy); + calibration.drawTarget(); + }, +}; +Bangle.setUI(modes); +calibration.drawTarget(); diff --git a/apps/calibration/boot.js b/apps/calibration/boot.js new file mode 100644 index 000000000..237fb2e0d --- /dev/null +++ b/apps/calibration/boot.js @@ -0,0 +1,14 @@ +let cal_settings = require('Storage').readJSON("calibration.json", true) || {active: false}; +Bangle.on('touch', function(button, xy) { + // do nothing if the calibration is deactivated + if (cal_settings.active === false) return; + + // reload the calibration offset at each touch event /!\ bad for the flash memory + if (cal_settings.reload === true) { + cal_settings = require('Storage').readJSON("calibration.json", true); + } + + // apply the calibration offset + xy.x += cal_settings.xoffset; + xy.y += cal_settings.yoffset; +}); diff --git a/apps/calibration/calibration.png b/apps/calibration/calibration.png new file mode 100644 index 000000000..3fb44beee Binary files /dev/null and b/apps/calibration/calibration.png differ diff --git a/apps/calibration/metadata.json b/apps/calibration/metadata.json new file mode 100644 index 000000000..122a2c175 --- /dev/null +++ b/apps/calibration/metadata.json @@ -0,0 +1,17 @@ +{ "id": "calibration", + "name": "Touchscreen Calibration", + "shortName":"Calibration", + "icon": "calibration.png", + "version":"1.00", + "description": "A simple calibration app for the touchscreen", + "supports": ["BANGLEJS","BANGLEJS2"], + "readme": "README.md", + "tags": "tool", + "storage": [ + {"name":"calibration.app.js","url":"app.js"}, + {"name":"calibration.boot.js","url":"boot.js"}, + {"name":"calibration.settings.js","url":"settings.js"}, + {"name":"calibration.img","url":"app-icon.js","evaluate":true} + ], + "data": [{"name":"calibration.json"}] +} diff --git a/apps/calibration/settings.js b/apps/calibration/settings.js new file mode 100644 index 000000000..6db8dd3bb --- /dev/null +++ b/apps/calibration/settings.js @@ -0,0 +1,23 @@ +(function(back) { + var FILE = "calibration.json"; + var settings = Object.assign({ + active: true, + }, require('Storage').readJSON(FILE, true) || {}); + + function writeSettings() { + require('Storage').writeJSON(FILE, settings); + } + + E.showMenu({ + "" : { "title" : "Calibration" }, + "< Back" : () => back(), + 'Active': { + value: !!settings.active, + format: v => v? "On":"Off", + onchange: v => { + settings.active = v; + writeSettings(); + } + }, + }); +}) \ No newline at end of file