Created
January 14, 2020 20:06
-
-
Save rozek/b91843476e46f6fc4fb2154fa11b2943 to your computer and use it in GitHub Desktop.
Bangle.js: just a simple TiltWatcher example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
//-- TiltWatcher -- | |
//------------------------------------------------------------------------------ | |
(function () { | |
let _Sensitivity = 1; // factor to amplify or damp acceleration | |
const TakerList = []; | |
/**** setWatchFor ****/ | |
function setWatchFor (Taker) { | |
if ((Taker == null) || (typeof Taker !== 'object')) { | |
throw new TypeError('"Taker" should be an object'); | |
} | |
let TakerIndex = TakerList.indexOf(Taker); | |
if (TakerIndex < 0) { | |
TakerList.push(Taker); | |
if (TakerList.length === 1) { | |
Bangle.on('accel',handleTilt); | |
} | |
} | |
return this; | |
} | |
/**** clearWatchFor ****/ | |
function clearWatchFor (Taker) { | |
if ((Taker == null) || (typeof Taker !== 'object')) { | |
throw new TypeError('"Taker" should be an object'); | |
} | |
let TakerIndex = TakerList.indexOf(Taker); | |
if (TakerIndex >= 0) { | |
TakerList.splice(TakerIndex,1) | |
if (TakerList.length === 0) { | |
Bangle.removeListener('accel',handleTilt); | |
} | |
} | |
return this; | |
} | |
/**** clearWatches ****/ | |
function clearWatches () { | |
if (TakerList.length > 0) { | |
TakerList.splice(0,TakerList.length); | |
Bangle.removeListener('accel',handleTilt); | |
} | |
return this; | |
} | |
/**** informTakers ****/ | |
function informTakers (x,y) { | |
for (let i = 0, l = TakerList.length; i < l; i++) { | |
let Method = TakerList[i]['onTilt']; | |
if (typeof Method === 'function') { | |
try { | |
Method.call(TakerList[i],x,y); | |
} catch (Signal) { /* nop */ } | |
} | |
} | |
} | |
/**** TiltWatcher object ****/ | |
TiltWatcher = { | |
get Sensitivity () { return _Sensitivity }, | |
set Sensitivity (Value) { | |
switch (true) { | |
case (Value == null): | |
_Sensitivity = 1; | |
break; | |
case (typeof Value === 'number') || (Value instanceof Number): | |
_Sensitivity = (Value <= 0 ? 1 : Value); | |
break; | |
default: | |
throw new TypeError('number expected'); | |
} | |
}, | |
setWatchFor:setWatchFor, clearWatchFor:clearWatchFor, | |
clearWatches:clearWatches | |
}; | |
/**** let TiltWatcher actually watch the device's tilt ****/ | |
function handleTilt (ValueSet) { | |
let Width = g.getWidth(); | |
let Height = g.getHeight(); | |
let x = E.clip(Math.round((1 + _Sensitivity*ValueSet.x)*Width/2), 0,Width); | |
let y = E.clip(Math.round((1 + _Sensitivity*ValueSet.y)*Height/2), 0,Height); | |
informTakers(x,y); | |
} | |
})(); | |
Bangle.setLCDMode('120x120'); | |
g.clear(); | |
let Cross = Graphics.createImage(` | |
** | |
** | |
****** | |
****** | |
** | |
** | |
`); | |
/**** Tilt Monitor ****/ | |
let oldX = g.getWidth()/2, oldY = g.getHeight()/2; | |
let TiltMonitor = { | |
onTilt: function onTilt (x,y) { | |
if ((oldX != null) && (oldY != null)) { | |
g.clearRect(oldX-3,oldY-3, oldX+3,oldY+3); | |
} | |
g.drawImage(Cross,x-3,y-3); | |
g.flip(); | |
oldX = x; oldY = y; | |
} | |
}; | |
TiltWatcher.setWatchFor(TiltMonitor); | |
g.drawImage(Cross,oldX,oldY); | |
g.flip(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment