diff --git a/apps/cscsensor/cscsensor.app.js b/apps/cscsensor/cscsensor.app.js index 65b50dfe7..7b6546343 100644 --- a/apps/cscsensor/cscsensor.app.js +++ b/apps/cscsensor/cscsensor.app.js @@ -26,8 +26,10 @@ class CSCSensor { this.speedUnit = this.qMetric ? "km/h" : "mph"; this.distUnit = this.qMetric ? "km" : "mi"; this.distFactor = this.qMetric ? 1.609344 : 1; + this.screenInit = true; this.batteryLevel = -1; } + reset() { this.settings.totaldist = this.totaldist; storage.writeJSON(SETTINGS_FILE, this.settings); @@ -35,10 +37,30 @@ class CSCSensor { this.movingTime = 0; this.lastRevsStart = this.lastRevs; this.maxSpeed = 0; + this.screenInit = true; } + setBatteryLevel(level) { - this.batteryLevel = level; + if (level!=this.batteryLevel) { + this.batteryLevel = level; + this.drawBatteryIcon(); + } } + + updateBatteryLevel(event) { + if (event.target.uuid == "0x2a19") this.setBatteryLevel(event.target.value.getUint8(0)); + } + + drawBatteryIcon() { + if (this.batteryLevel!=-1) { + g.setColor(1, 1, 1).drawRect(10, 55, 20, 75).fillRect(14, 53, 16, 55); + if (this.batteryLevel<25) g.setColor(1, 0, 0); + else if (this.batteryLevel<50) g.setColor(1, 0.5, 0); + else g.setColor(0, 1, 0); + g.fillRect(11, 74-18*this.batteryLevel/100, 19, 74); + } + } + updateScreen() { var dist = this.distFactor*(this.lastRevs-this.lastRevsStart)*this.wheelCirc/63360.0; var ddist = Math.round(100*dist)/100; @@ -50,39 +72,41 @@ class CSCSensor { if (dsecs.length<2) dsecs = "0"+dsecs; var avespeed = (this.movingTime>2 ? Math.round(10*dist/(this.movingTime/3600))/10 : 0); var maxspeed = Math.round(10*this.distFactor*this.maxSpeed)/10; - for (var i=0; i<6; ++i) { - if ((i&1)==0) g.setColor(0, 0, 0); - else g.setColor(0.2, 0.1, 0.4); - g.fillRect(0, 48+i*32, 86, 48+(i+1)*32); - if ((i&1)==1) g.setColor(0, 0, 0); - else g.setColor(0.2, 0.1, 0.4); - g.fillRect(87, 48+i*32, 239, 48+(i+1)*32); - g.setColor(0.5, 0.5, 0.5).drawRect(87, 48+i*32, 239, 48+(i+1)*32).drawLine(0, 239, 239, 239).drawRect(0, 48, 87, 239); - } - g.setFontAlign(1, 0, 0).setFontVector(19).setColor(1, 1, 0); - g.drawString("Time:", 87, 66); - g.drawString("Speed:", 87, 98); - g.drawString("Ave spd:", 87, 130); - g.drawString("Max spd:", 87, 162); - g.drawString("Trip:", 87, 194); - g.drawString("Total:", 87, 226); - g.setFontAlign(-1, 0, 0).setFontVector(26).setColor(1, 1, 1);//.clearRect(92, 60, 239, 239); - g.drawString(dmins+":"+dsecs, 92, 66); - g.drawString(dspeed+" "+this.speedUnit, 92, 98); - g.drawString(avespeed + " " + this.speedUnit, 92, 130); - g.drawString(maxspeed + " " + this.speedUnit, 92, 162); - g.drawString(ddist + " " + this.distUnit, 92, 194); - g.drawString(tdist + " " + this.distUnit, 92, 226); - if (this.batteryLevel!=-1) { - g.setColor(1, 1, 1).drawRect(10, 55, 20, 75).fillRect(14, 53, 16, 55); - if (this.batteryLevel<25) g.setColor(1, 0, 0); - else if (this.batteryLevel<50) g.setColor(1, 0.5, 0); - else g.setColor(0, 1, 0); - g.fillRect(11, 74-18*this.batteryLevel/100, 19, 74); - console.log(this.batteryLevel); - this.batteryLevel = -1; + if (this.screenInit) { + for (var i=0; i<6; ++i) { + if ((i&1)==0) g.setColor(0, 0, 0); + else g.setColor(0x30cd); + g.fillRect(0, 48+i*32, 86, 48+(i+1)*32); + if ((i&1)==1) g.setColor(0); + else g.setColor(0x30cd); + g.fillRect(87, 48+i*32, 239, 48+(i+1)*32); + g.setColor(0.5, 0.5, 0.5).drawRect(87, 48+i*32, 239, 48+(i+1)*32).drawLine(0, 239, 239, 239).drawRect(0, 48, 87, 239); + } + g.setFontAlign(1, 0, 0).setFontVector(19).setColor(1, 1, 0); + g.drawString("Time:", 87, 66); + g.drawString("Speed:", 87, 98); + g.drawString("Ave spd:", 87, 130); + g.drawString("Max spd:", 87, 162); + g.drawString("Trip:", 87, 194); + g.drawString("Total:", 87, 226); + this.drawBatteryIcon(); + this.screenInit = false; } + g.setFontAlign(-1, 0, 0).setFontVector(26); + g.setColor(0x30cd).fillRect(88, 49, 238, 79); + g.setColor(0xffff).drawString(dmins+":"+dsecs, 92, 66); + g.setColor(0).fillRect(88, 81, 238, 111); + g.setColor(0xffff).drawString(dspeed+" "+this.speedUnit, 92, 98); + g.setColor(0x30cd).fillRect(88, 113, 238, 143); + g.setColor(0xffff).drawString(avespeed + " " + this.speedUnit, 92, 130); + g.setColor(0).fillRect(88, 145, 238, 175); + g.setColor(0xffff).drawString(maxspeed + " " + this.speedUnit, 92, 162); + g.setColor(0x30cd).fillRect(88, 177, 238, 207); + g.setColor(0xffff).drawString(ddist + " " + this.distUnit, 92, 194); + g.setColor(0).fillRect(88, 209, 238, 238); + g.setColor(0xffff).drawString(tdist + " " + this.distUnit, 92, 226); } + updateSensor(event) { var qChanged = false; if (event.target.uuid == "0x2a5b") { @@ -103,7 +127,7 @@ class CSCSensor { var dBT = (Date.now()-this.lastBangleTime)/1000; this.lastBangleTime = Date.now(); if (dT<0) dT+=64; - if (Math.abs(dT-dBT)>2) dT = dBT; + if (Math.abs(dT-dBT)>3) dT = dBT; this.lastTime = wheelTime; this.speed = this.lastSpeed; if (dRevs>0 && dT>0) { @@ -132,9 +156,8 @@ function getSensorBatteryLevel(gatt) { gatt.getPrimaryService("180f").then(function(s) { return s.getCharacteristic("2a19"); }).then(function(c) { - return c.readValue(); - }).then(function(d) { - mySensor.setBatteryLevel(d.buffer[0]); + c.on('characteristicvaluechanged', (event)=>mySensor.updateBatteryLevel(event)); + return c.startNotifications(); }); } @@ -159,17 +182,18 @@ function parseDevice(d) { mySensor.updateScreen(); }).catch(function(e) { g.clearRect(0, 60, 239, 239).setColor(1, 0, 0).setFontAlign(0, 0, 0).drawString("ERROR"+e, 120, 120).flip(); - console.log(e); + console.log(e); })} function connection_setup() { + NRF.setScan(); NRF.setScan(parseDevice, { filters: [{services:["1816"]}], timeout: 2000}); g.clearRect(0, 60, 239, 239).setFontVector(18).setFontAlign(0, 0, 0).setColor(0, 1, 0); g.drawString("Scanning for CSC sensor...", 120, 120); } connection_setup(); -setWatch(function() { mySensor.reset(); g.clearRect(0, 60, 239, 239); mySensor.updateScreen(); }, BTN1, {repeat:true, debounce:20}); +setWatch(function() { mySensor.reset(); g.clearRect(0, 48, 239, 239); mySensor.updateScreen(); }, BTN1, {repeat:true, debounce:20}); E.on('kill',()=>{ if (gatt!=undefined) gatt.disconnect(); mySensor.settings.totaldist = mySensor.totaldist; storage.writeJSON(SETTINGS_FILE, mySensor.settings); }); NRF.on('disconnect', connection_setup);