Added a new Module: a spirit level

This commit is contained in:
Blue Fox 2023-03-07 17:13:36 +01:00
parent b698383f73
commit ce4b8dabac
2 changed files with 60 additions and 0 deletions

View File

@ -21,6 +21,7 @@ function drawFace() {
// Some initialization
Bangle.setGPSPower(true);
g.setFontAlign(0,0);
g.setColor(g.theme.bg);
g.fillRect(0,0,g.getWidth(), g.getHeight());
g.setColor(g.theme.fg2);

59
spiritlevel.trailsense.js Normal file
View File

@ -0,0 +1,59 @@
(() => {
old = { 'x':0, 'y':0 };
function drawSpiritLevel(v,W,H,offsetX,offsetY) {
/* ---------------------------------
v is the current velocity given when the
event 'accel' fires - W,H and the offsets
are width, height, and offsets of the
displayed spirit level
--------------------------------- */
// calculate sizes of forms
var outlineRectULCx = offsetX+W/2-(W-40)/2; // upper left corner (ULC) of the outline rect
var outlineRectULCy = offsetY+H/2-(H-40)/2; // upper left corner of the outline rect
var outlineRectLRCx = offsetX+W/2+(W-40)/2; // lower right corner (LRC) of the outline rect
var outlineRectLRCy = offsetY+H/2+(H-40)/2; // lower right corner of the outline rect
// draw outlines (cross and circle
g.setColor(g.theme.fg2);
g.drawRect(outlineRectULCx,outlineRectULCy, outlineRectLRCx,outlineRectLRCy);
g.fillRect(offsetX+W/2-20,offsetY+H/2-3, offsetX+W/2+20,offsetY+H/2+3); // horizontal line of the cross
g.fillRect(offsetX+W/2-3,offsetY+H/2-20, offsetX+W/2+3,offsetY+H/2+20); // vertical line of the cross
var n = { x:E.clip(W/2+v.x*256,29,W-29),
y:E.clip(H/2+v.y*256,29,H-29), };
var dX = v.x*90;
var dY = v.y*90;
g.reset();
g.clearRect(offsetX,offsetY,offsetX+W,offsetY+16); // clear behind text
g.clearRect(offsetX+W-16,offsetY,offsetX+W,offsetY+H); // clear behind text
g.setFont("Vector",15).setFontAlign(0,-1).drawString("Y: " + dY.toFixed(1), offsetX+W/2,offsetY+2);
g.setFont("Vector",15).setFontAlign(0,-1,1).drawString("X: " + dX.toFixed(1), offsetX+W-2,offsetY+H/2);
g.setColor(g.theme.bg).fillCircle(offsetX+old.x,offsetY+old.y, 8); // clear old marker
g.setColor(g.theme.fg).fillCircle(offsetX+n.x,offsetY+n.y, 8); // draw new marker
// vibrate if it's nearly flat
if(Math.abs(v.x)<0.015 && Math.abs(v.y)<0.015) {
Bangle.beep(20,440);
}
old = n;
}
function drawFace() {
Bangle.setLCDTimeout(60);
g.clear();
Bangle.on('accel', (v) => { drawSpiritLevel(v,240,240,0,0); });
}
function stop() {
Bangle.removeAllListeners();
Bangle.setLCDTimeout(10);
g.clear();
}
return { drawFace:drawFace,stop:stop };
})();