forked from FOSS/BangleApps
commit
cedc21d3a9
|
@ -0,0 +1 @@
|
|||
0.01: Initial version
|
|
@ -0,0 +1,34 @@
|
|||
# Cycling
|
||||
> Displays data from a BLE Cycling Speed and Cadence sensor.
|
||||
|
||||
*This is a fork of the CSCSensor app using the layout library and separate module for CSC functionality. It also drops persistence of total distance on the Bangle, as this information is also persisted on the sensor itself. Further, it allows configuration of display units (metric/imperial) independent of chosen locale. Finally, multiple sensors can be used and wheel circumference can be configured for each sensor individually.*
|
||||
|
||||
The following data are displayed:
|
||||
- curent speed
|
||||
- moving time
|
||||
- average speed
|
||||
- maximum speed
|
||||
- trip distance
|
||||
- total distance
|
||||
|
||||
Other than in the original version of the app, total distance is not stored on the Bangle, but instead is calculated from the CWR (cumulative wheel revolutions) reported by the sensor. This metric is, according to the BLE spec, an absolute value that persists throughout the lifetime of the sensor and never rolls over.
|
||||
|
||||
**Cadence / Crank features are currently not implemented**
|
||||
|
||||
## Usage
|
||||
Open the app and connect to a CSC sensor.
|
||||
|
||||
Upon first connection, close the app afain and enter the settings app to configure the wheel circumference. The total circumference is (cm + mm) - it is split up into two values for ease of configuration. Check the status screen inside the Cycling app while connected to see the address of the currently connected sensor (if you need to differentiate between multiple sensors).
|
||||
|
||||
Inside the Cycling app, use button / tap screen to:
|
||||
- cycle through screens (if connected)
|
||||
- reconnect (if connection aborted)
|
||||
|
||||
## TODO
|
||||
* Sensor battery status
|
||||
* Implement crank events / show cadence
|
||||
* Bangle.js 1 compatibility
|
||||
* Allow setting CWR on the sensor (this is a feature intended by the BLE CSC spec, in case the sensor is replaced or transferred to a different bike)
|
||||
|
||||
## Development
|
||||
There is a "mock" version of the `blecsc` module, which can be used to test features in the emulator. Check `blecsc-emu.js` for usage.
|
|
@ -0,0 +1,111 @@
|
|||
// UUID of the Bluetooth CSC Service
|
||||
const SERVICE_UUID = "1816";
|
||||
// UUID of the CSC measurement characteristic
|
||||
const MEASUREMENT_UUID = "2a5b";
|
||||
|
||||
// Wheel revolution present bit mask
|
||||
const FLAGS_WREV_BM = 0x01;
|
||||
// Crank revolution present bit mask
|
||||
const FLAGS_CREV_BM = 0x02;
|
||||
|
||||
/**
|
||||
* Fake BLECSC implementation for the emulator, where it's hard to test
|
||||
* with actual hardware. Generates "random" wheel events (no crank).
|
||||
*
|
||||
* To upload as a module, paste the entire file in the console using this
|
||||
* command: require("Storage").write("blecsc-emu",`<FILE CONTENT HERE>`);
|
||||
*/
|
||||
class BLECSCEmulator {
|
||||
constructor() {
|
||||
this.timeout = undefined;
|
||||
this.interval = 500;
|
||||
this.ccr = 0;
|
||||
this.lwt = 0;
|
||||
this.handlers = {
|
||||
// value
|
||||
// disconnect
|
||||
// wheelEvent
|
||||
// crankEvent
|
||||
};
|
||||
}
|
||||
|
||||
getDeviceAddress() {
|
||||
return 'fa:ke:00:de:vi:ce';
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for the GATT characteristicvaluechanged event.
|
||||
* Consumers must not call this method!
|
||||
*/
|
||||
onValue(event) {
|
||||
// Not interested in non-CSC characteristics
|
||||
if (event.target.uuid != "0x" + MEASUREMENT_UUID) return;
|
||||
|
||||
// Notify the generic 'value' handler
|
||||
if (this.handlers.value) this.handlers.value(event);
|
||||
|
||||
const flags = event.target.value.getUint8(0, true);
|
||||
// Notify the 'wheelEvent' handler
|
||||
if ((flags & FLAGS_WREV_BM) && this.handlers.wheelEvent) this.handlers.wheelEvent({
|
||||
cwr: event.target.value.getUint32(1, true), // cumulative wheel revolutions
|
||||
lwet: event.target.value.getUint16(5, true), // last wheel event time
|
||||
});
|
||||
|
||||
// Notify the 'crankEvent' handler
|
||||
if ((flags & FLAGS_CREV_BM) && this.handlers.crankEvent) this.handlers.crankEvent({
|
||||
ccr: event.target.value.getUint16(7, true), // cumulative crank revolutions
|
||||
lcet: event.target.value.getUint16(9, true), // last crank event time
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an event handler.
|
||||
*
|
||||
* @param {string} event value|disconnect
|
||||
* @param {function} handler handler function that receives the event as its first argument
|
||||
*/
|
||||
on(event, handler) {
|
||||
this.handlers[event] = handler;
|
||||
}
|
||||
|
||||
fakeEvent() {
|
||||
this.interval = Math.max(50, Math.min(1000, this.interval + Math.random()*40-20));
|
||||
this.lwt = (this.lwt + this.interval) % 0x10000;
|
||||
this.ccr++;
|
||||
|
||||
var buffer = new ArrayBuffer(8);
|
||||
var view = new DataView(buffer);
|
||||
view.setUint8(0, 0x01); // Wheel revolution data present bit
|
||||
view.setUint32(1, this.ccr, true); // Cumulative crank revolutions
|
||||
view.setUint16(5, this.lwt, true); // Last wheel event time
|
||||
|
||||
this.onValue({
|
||||
target: {
|
||||
uuid: "0x2a5b",
|
||||
value: view,
|
||||
},
|
||||
});
|
||||
|
||||
this.timeout = setTimeout(this.fakeEvent.bind(this), this.interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and connect to a device which exposes the CSC service.
|
||||
*
|
||||
* @return {Promise}
|
||||
*/
|
||||
connect() {
|
||||
this.timeout = setTimeout(this.fakeEvent.bind(this), this.interval);
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect the device.
|
||||
*/
|
||||
disconnect() {
|
||||
if (!this.timeout) return;
|
||||
clearTimeout(this.timeout);
|
||||
}
|
||||
}
|
||||
|
||||
exports = BLECSCEmulator;
|
|
@ -0,0 +1,150 @@
|
|||
const SERVICE_UUID = "1816";
|
||||
// UUID of the CSC measurement characteristic
|
||||
const MEASUREMENT_UUID = "2a5b";
|
||||
|
||||
// Wheel revolution present bit mask
|
||||
const FLAGS_WREV_BM = 0x01;
|
||||
// Crank revolution present bit mask
|
||||
const FLAGS_CREV_BM = 0x02;
|
||||
|
||||
/**
|
||||
* This class communicates with a Bluetooth CSC peripherial using the Espruino NRF library.
|
||||
*
|
||||
* ## Usage:
|
||||
* 1. Register event handlers using the \`on(eventName, handlerFunction)\` method
|
||||
* You can subscribe to the \`wheelEvent\` and \`crankEvent\` events or you can
|
||||
* have raw characteristic values passed through using the \`value\` event.
|
||||
* 2. Search and connect to a BLE CSC peripherial by calling the \`connect()\` method
|
||||
* 3. To tear down the connection, call the \`disconnect()\` method
|
||||
*
|
||||
* ## Events
|
||||
* - \`wheelEvent\` - the peripharial sends a notification containing wheel event data
|
||||
* - \`crankEvent\` - the peripharial sends a notification containing crank event data
|
||||
* - \`value\` - the peripharial sends any CSC characteristic notification (including wheel & crank event)
|
||||
* - \`disconnect\` - the peripherial ends the connection or the connection is lost
|
||||
*
|
||||
* Each event can only have one handler. Any call to \`on()\` will
|
||||
* replace a previously registered handler for the same event.
|
||||
*/
|
||||
class BLECSC {
|
||||
constructor() {
|
||||
this.device = undefined;
|
||||
this.ccInterval = undefined;
|
||||
this.gatt = undefined;
|
||||
this.handlers = {
|
||||
// wheelEvent
|
||||
// crankEvent
|
||||
// value
|
||||
// disconnect
|
||||
};
|
||||
}
|
||||
|
||||
getDeviceAddress() {
|
||||
if (!this.device || !this.device.id)
|
||||
return '00:00:00:00:00:00';
|
||||
return this.device.id.split(" ")[0];
|
||||
}
|
||||
|
||||
checkConnection() {
|
||||
if (!this.device)
|
||||
console.log("no device");
|
||||
// else
|
||||
// console.log("rssi: " + this.device.rssi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for the GATT characteristicvaluechanged event.
|
||||
* Consumers must not call this method!
|
||||
*/
|
||||
onValue(event) {
|
||||
// Not interested in non-CSC characteristics
|
||||
if (event.target.uuid != "0x" + MEASUREMENT_UUID) return;
|
||||
|
||||
// Notify the generic 'value' handler
|
||||
if (this.handlers.value) this.handlers.value(event);
|
||||
|
||||
const flags = event.target.value.getUint8(0, true);
|
||||
// Notify the 'wheelEvent' handler
|
||||
if ((flags & FLAGS_WREV_BM) && this.handlers.wheelEvent) this.handlers.wheelEvent({
|
||||
cwr: event.target.value.getUint32(1, true), // cumulative wheel revolutions
|
||||
lwet: event.target.value.getUint16(5, true), // last wheel event time
|
||||
});
|
||||
|
||||
// Notify the 'crankEvent' handler
|
||||
if ((flags & FLAGS_CREV_BM) && this.handlers.crankEvent) this.handlers.crankEvent({
|
||||
ccr: event.target.value.getUint16(7, true), // cumulative crank revolutions
|
||||
lcet: event.target.value.getUint16(9, true), // last crank event time
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for the NRF disconnect event.
|
||||
* Consumers must not call this method!
|
||||
*/
|
||||
onDisconnect(event) {
|
||||
console.log("disconnected");
|
||||
if (this.ccInterval)
|
||||
clearInterval(this.ccInterval);
|
||||
|
||||
if (!this.handlers.disconnect) return;
|
||||
this.handlers.disconnect(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an event handler.
|
||||
*
|
||||
* @param {string} event wheelEvent|crankEvent|value|disconnect
|
||||
* @param {function} handler function that will receive the event as its first argument
|
||||
*/
|
||||
on(event, handler) {
|
||||
this.handlers[event] = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and connect to a device which exposes the CSC service.
|
||||
*
|
||||
* @return {Promise}
|
||||
*/
|
||||
connect() {
|
||||
// Register handler for the disconnect event to be passed throug
|
||||
NRF.on('disconnect', this.onDisconnect.bind(this));
|
||||
|
||||
// Find a device, then get the CSC Service and subscribe to
|
||||
// notifications on the CSC Measurement characteristic.
|
||||
// NRF.setLowPowerConnection(true);
|
||||
return NRF.requestDevice({
|
||||
timeout: 5000,
|
||||
filters: [{ services: [SERVICE_UUID] }],
|
||||
}).then(device => {
|
||||
this.device = device;
|
||||
this.device.on('gattserverdisconnected', this.onDisconnect.bind(this));
|
||||
this.ccInterval = setInterval(this.checkConnection.bind(this), 2000);
|
||||
return device.gatt.connect();
|
||||
}).then(gatt => {
|
||||
this.gatt = gatt;
|
||||
return gatt.getPrimaryService(SERVICE_UUID);
|
||||
}).then(service => {
|
||||
return service.getCharacteristic(MEASUREMENT_UUID);
|
||||
}).then(characteristic => {
|
||||
characteristic.on('characteristicvaluechanged', this.onValue.bind(this));
|
||||
return characteristic.startNotifications();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect the device.
|
||||
*/
|
||||
disconnect() {
|
||||
if (this.ccInterval)
|
||||
clearInterval(this.ccInterval);
|
||||
|
||||
if (!this.gatt) return;
|
||||
try {
|
||||
this.gatt.disconnect();
|
||||
} catch {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports = BLECSC;
|
|
@ -0,0 +1,453 @@
|
|||
const Layout = require('Layout');
|
||||
const storage = require('Storage');
|
||||
|
||||
const SETTINGS_FILE = 'cycling.json';
|
||||
const SETTINGS_DEFAULT = {
|
||||
sensors: {},
|
||||
metric: true,
|
||||
};
|
||||
|
||||
const RECONNECT_TIMEOUT = 4000;
|
||||
const MAX_CONN_ATTEMPTS = 2;
|
||||
|
||||
class CSCSensor {
|
||||
constructor(blecsc, display) {
|
||||
// Dependency injection
|
||||
this.blecsc = blecsc;
|
||||
this.display = display;
|
||||
|
||||
// Load settings
|
||||
this.settings = storage.readJSON(SETTINGS_FILE, true) || SETTINGS_DEFAULT;
|
||||
this.wheelCirc = undefined;
|
||||
|
||||
// CSC runtime variables
|
||||
this.movingTime = 0; // unit: s
|
||||
this.lastBangleTime = Date.now(); // unit: ms
|
||||
this.lwet = 0; // last wheel event time (unit: s/1024)
|
||||
this.cwr = -1; // cumulative wheel revolutions
|
||||
this.cwrTrip = 0; // wheel revolutions since trip start
|
||||
this.speed = 0; // unit: m/s
|
||||
this.maxSpeed = 0; // unit: m/s
|
||||
this.speedFailed = 0;
|
||||
|
||||
// Other runtime variables
|
||||
this.connected = false;
|
||||
this.failedAttempts = 0;
|
||||
this.failed = false;
|
||||
|
||||
// Layout configuration
|
||||
this.layout = 0;
|
||||
this.display.useMetricUnits(true);
|
||||
this.deviceAddress = undefined;
|
||||
this.display.useMetricUnits((this.settings.metric));
|
||||
}
|
||||
|
||||
onDisconnect(event) {
|
||||
console.log("disconnected ", event);
|
||||
|
||||
this.connected = false;
|
||||
this.wheelCirc = undefined;
|
||||
|
||||
this.setLayout(0);
|
||||
this.display.setDeviceAddress("unknown");
|
||||
|
||||
if (this.failedAttempts >= MAX_CONN_ATTEMPTS) {
|
||||
this.failed = true;
|
||||
this.display.setStatus("Connection failed after " + MAX_CONN_ATTEMPTS + " attempts.");
|
||||
} else {
|
||||
this.display.setStatus("Disconnected");
|
||||
setTimeout(this.connect.bind(this), RECONNECT_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
loadCircumference() {
|
||||
if (!this.deviceAddress) return;
|
||||
|
||||
// Add sensor to settings if not present
|
||||
if (!this.settings.sensors[this.deviceAddress]) {
|
||||
this.settings.sensors[this.deviceAddress] = {
|
||||
cm: 223,
|
||||
mm: 0,
|
||||
};
|
||||
storage.writeJSON(SETTINGS_FILE, this.settings);
|
||||
}
|
||||
|
||||
const high = this.settings.sensors[this.deviceAddress].cm || 223;
|
||||
const low = this.settings.sensors[this.deviceAddress].mm || 0;
|
||||
this.wheelCirc = (10*high + low) / 1000;
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.connected = false;
|
||||
this.setLayout(0);
|
||||
this.display.setStatus("Connecting");
|
||||
console.log("Trying to connect to BLE CSC");
|
||||
|
||||
// Hook up events
|
||||
this.blecsc.on('wheelEvent', this.onWheelEvent.bind(this));
|
||||
this.blecsc.on('disconnect', this.onDisconnect.bind(this));
|
||||
|
||||
// Scan for BLE device and connect
|
||||
this.blecsc.connect()
|
||||
.then(function() {
|
||||
this.failedAttempts = 0;
|
||||
this.failed = false;
|
||||
this.connected = true;
|
||||
this.deviceAddress = this.blecsc.getDeviceAddress();
|
||||
console.log("Connected to " + this.deviceAddress);
|
||||
|
||||
this.display.setDeviceAddress(this.deviceAddress);
|
||||
this.display.setStatus("Connected");
|
||||
|
||||
this.loadCircumference();
|
||||
|
||||
// Switch to speed screen in 2s
|
||||
setTimeout(function() {
|
||||
this.setLayout(1);
|
||||
this.updateScreen();
|
||||
}.bind(this), 2000);
|
||||
}.bind(this))
|
||||
.catch(function(e) {
|
||||
this.failedAttempts++;
|
||||
this.onDisconnect(e);
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.blecsc.disconnect();
|
||||
this.reset();
|
||||
this.setLayout(0);
|
||||
this.display.setStatus("Disconnected");
|
||||
}
|
||||
|
||||
setLayout(num) {
|
||||
this.layout = num;
|
||||
if (this.layout == 0) {
|
||||
this.display.updateLayout("status");
|
||||
} else if (this.layout == 1) {
|
||||
this.display.updateLayout("speed");
|
||||
} else if (this.layout == 2) {
|
||||
this.display.updateLayout("distance");
|
||||
}
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.connected = false;
|
||||
this.failed = false;
|
||||
this.failedAttempts = 0;
|
||||
this.wheelCirc = undefined;
|
||||
}
|
||||
|
||||
interact(d) {
|
||||
// Only interested in tap / center button
|
||||
if (d) return;
|
||||
|
||||
// Reconnect in failed state
|
||||
if (this.failed) {
|
||||
this.reset();
|
||||
this.connect();
|
||||
} else if (this.connected) {
|
||||
this.setLayout((this.layout + 1) % 3);
|
||||
}
|
||||
}
|
||||
|
||||
updateScreen() {
|
||||
var tripDist = this.cwrTrip * this.wheelCirc;
|
||||
var avgSpeed = this.movingTime > 3 ? tripDist / this.movingTime : 0;
|
||||
|
||||
this.display.setTotalDistance(this.cwr * this.wheelCirc);
|
||||
this.display.setTripDistance(tripDist);
|
||||
this.display.setSpeed(this.speed);
|
||||
this.display.setAvg(avgSpeed);
|
||||
this.display.setMax(this.maxSpeed);
|
||||
this.display.setTime(Math.floor(this.movingTime));
|
||||
}
|
||||
|
||||
onWheelEvent(event) {
|
||||
// Calculate number of revolutions since last wheel event
|
||||
var dRevs = (this.cwr > 0 ? event.cwr - this.cwr : 0);
|
||||
this.cwr = event.cwr;
|
||||
|
||||
// Increment the trip revolutions counter
|
||||
this.cwrTrip += dRevs;
|
||||
|
||||
// Calculate time delta since last wheel event
|
||||
var dT = (event.lwet - this.lwet)/1024;
|
||||
var now = Date.now();
|
||||
var dBT = (now-this.lastBangleTime)/1000;
|
||||
this.lastBangleTime = now;
|
||||
if (dT<0) dT+=64; // wheel event time wraps every 64s
|
||||
if (Math.abs(dT-dBT)>3) dT = dBT; // not sure about the reason for this
|
||||
this.lwet = event.lwet;
|
||||
|
||||
// Recalculate current speed
|
||||
if (dRevs>0 && dT>0) {
|
||||
this.speed = dRevs * this.wheelCirc / dT;
|
||||
this.speedFailed = 0;
|
||||
this.movingTime += dT;
|
||||
} else {
|
||||
this.speedFailed++;
|
||||
if (this.speedFailed>3) {
|
||||
this.speed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Update max speed
|
||||
if (this.speed>this.maxSpeed
|
||||
&& (this.movingTime>3 || this.speed<20)
|
||||
&& this.speed<50
|
||||
) this.maxSpeed = this.speed;
|
||||
|
||||
this.updateScreen();
|
||||
}
|
||||
}
|
||||
|
||||
class CSCDisplay {
|
||||
constructor() {
|
||||
this.metric = true;
|
||||
this.fontLabel = "6x8";
|
||||
this.fontSmall = "15%";
|
||||
this.fontMed = "18%";
|
||||
this.fontLarge = "32%";
|
||||
this.currentLayout = "status";
|
||||
this.layouts = {};
|
||||
this.layouts.speed = new Layout({
|
||||
type: "v",
|
||||
c: [
|
||||
{
|
||||
type: "h",
|
||||
id: "speed_g",
|
||||
fillx: 1,
|
||||
filly: 1,
|
||||
pad: 4,
|
||||
bgCol: "#fff",
|
||||
c: [
|
||||
{type: undefined, width: 32, halign: -1},
|
||||
{type: "txt", id: "speed", label: "00.0", font: this.fontLarge, bgCol: "#fff", col: "#000", width: 122},
|
||||
{type: "txt", id: "speed_u", label: " km/h", font: this.fontLabel, col: "#000", width: 22, r: 90},
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "h",
|
||||
id: "time_g",
|
||||
fillx: 1,
|
||||
pad: 4,
|
||||
bgCol: "#000",
|
||||
height: 36,
|
||||
c: [
|
||||
{type: undefined, width: 32, halign: -1},
|
||||
{type: "txt", id: "time", label: "00:00", font: this.fontMed, bgCol: "#000", col: "#fff", width: 122},
|
||||
{type: "txt", id: "time_u", label: "mins", font: this.fontLabel, bgCol: "#000", col: "#fff", width: 22, r: 90},
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "h",
|
||||
id: "stats_g",
|
||||
fillx: 1,
|
||||
bgCol: "#fff",
|
||||
height: 36,
|
||||
c: [
|
||||
{
|
||||
type: "v",
|
||||
pad: 4,
|
||||
bgCol: "#fff",
|
||||
c: [
|
||||
{type: "txt", id: "max_l", label: "MAX", font: this.fontLabel, col: "#000"},
|
||||
{type: "txt", id: "max", label: "00.0", font: this.fontSmall, bgCol: "#fff", col: "#000", width: 69},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "v",
|
||||
pad: 4,
|
||||
bgCol: "#fff",
|
||||
c: [
|
||||
{type: "txt", id: "avg_l", label: "AVG", font: this.fontLabel, col: "#000"},
|
||||
{type: "txt", id: "avg", label: "00.0", font: this.fontSmall, bgCol: "#fff", col: "#000", width: 69},
|
||||
],
|
||||
},
|
||||
{type: "txt", id: "stats_u", label: " km/h", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 22, r: 90},
|
||||
]
|
||||
},
|
||||
],
|
||||
});
|
||||
this.layouts.distance = new Layout({
|
||||
type: "v",
|
||||
bgCol: "#fff",
|
||||
c: [
|
||||
{
|
||||
type: "h",
|
||||
id: "tripd_g",
|
||||
fillx: 1,
|
||||
pad: 4,
|
||||
bgCol: "#fff",
|
||||
height: 32,
|
||||
c: [
|
||||
{type: "txt", id: "tripd_l", label: "TRP", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 36},
|
||||
{type: "txt", id: "tripd", label: "0", font: this.fontMed, bgCol: "#fff", col: "#000", width: 118},
|
||||
{type: "txt", id: "tripd_u", label: "km", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 22, r: 90},
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "h",
|
||||
id: "totald_g",
|
||||
fillx: 1,
|
||||
pad: 4,
|
||||
bgCol: "#fff",
|
||||
height: 32,
|
||||
c: [
|
||||
{type: "txt", id: "totald_l", label: "TTL", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 36},
|
||||
{type: "txt", id: "totald", label: "0", font: this.fontMed, bgCol: "#fff", col: "#000", width: 118},
|
||||
{type: "txt", id: "totald_u", label: "km", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 22, r: 90},
|
||||
]
|
||||
},
|
||||
],
|
||||
});
|
||||
this.layouts.status = new Layout({
|
||||
type: "v",
|
||||
c: [
|
||||
{
|
||||
type: "h",
|
||||
id: "status_g",
|
||||
fillx: 1,
|
||||
bgCol: "#fff",
|
||||
height: 100,
|
||||
c: [
|
||||
{type: "txt", id: "status", label: "Bangle Cycling", font: this.fontSmall, bgCol: "#fff", col: "#000", width: 176, wrap: 1},
|
||||
]
|
||||
},
|
||||
{
|
||||
type: "h",
|
||||
id: "addr_g",
|
||||
fillx: 1,
|
||||
pad: 4,
|
||||
bgCol: "#fff",
|
||||
height: 32,
|
||||
c: [
|
||||
{ type: "txt", id: "addr_l", label: "ADDR", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 36 },
|
||||
{ type: "txt", id: "addr", label: "unknown", font: this.fontLabel, bgCol: "#fff", col: "#000", width: 140 },
|
||||
]
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
updateLayout(layout) {
|
||||
this.currentLayout = layout;
|
||||
|
||||
g.clear();
|
||||
this.layouts[layout].update();
|
||||
this.layouts[layout].render();
|
||||
Bangle.drawWidgets();
|
||||
}
|
||||
|
||||
renderIfLayoutActive(layout, node) {
|
||||
if (layout != this.currentLayout) return;
|
||||
this.layouts[layout].render(node);
|
||||
}
|
||||
|
||||
useMetricUnits(metric) {
|
||||
this.metric = metric;
|
||||
|
||||
// console.log("using " + (metric ? "metric" : "imperial") + " units");
|
||||
|
||||
var speedUnit = metric ? "km/h" : "mph";
|
||||
this.layouts.speed.speed_u.label = speedUnit;
|
||||
this.layouts.speed.stats_u.label = speedUnit;
|
||||
|
||||
var distanceUnit = metric ? "km" : "mi";
|
||||
this.layouts.distance.tripd_u.label = distanceUnit;
|
||||
this.layouts.distance.totald_u.label = distanceUnit;
|
||||
|
||||
this.updateLayout(this.currentLayout);
|
||||
}
|
||||
|
||||
convertDistance(meters) {
|
||||
if (this.metric) return meters / 1000;
|
||||
return meters / 1609.344;
|
||||
}
|
||||
|
||||
convertSpeed(mps) {
|
||||
if (this.metric) return mps * 3.6;
|
||||
return mps * 2.23694;
|
||||
}
|
||||
|
||||
setSpeed(speed) {
|
||||
this.layouts.speed.speed.label = this.convertSpeed(speed).toFixed(1);
|
||||
this.renderIfLayoutActive("speed", this.layouts.speed.speed_g);
|
||||
}
|
||||
|
||||
setAvg(speed) {
|
||||
this.layouts.speed.avg.label = this.convertSpeed(speed).toFixed(1);
|
||||
this.renderIfLayoutActive("speed", this.layouts.speed.stats_g);
|
||||
}
|
||||
|
||||
setMax(speed) {
|
||||
this.layouts.speed.max.label = this.convertSpeed(speed).toFixed(1);
|
||||
this.renderIfLayoutActive("speed", this.layouts.speed.stats_g);
|
||||
}
|
||||
|
||||
setTime(seconds) {
|
||||
var time = '';
|
||||
var hours = Math.floor(seconds/3600);
|
||||
if (hours) {
|
||||
time += hours + ":";
|
||||
this.layouts.speed.time_u.label = " hrs";
|
||||
} else {
|
||||
this.layouts.speed.time_u.label = "mins";
|
||||
}
|
||||
|
||||
time += String(Math.floor((seconds%3600)/60)).padStart(2, '0') + ":";
|
||||
time += String(seconds % 60).padStart(2, '0');
|
||||
|
||||
this.layouts.speed.time.label = time;
|
||||
this.renderIfLayoutActive("speed", this.layouts.speed.time_g);
|
||||
}
|
||||
|
||||
setTripDistance(distance) {
|
||||
this.layouts.distance.tripd.label = this.convertDistance(distance).toFixed(1);
|
||||
this.renderIfLayoutActive("distance", this.layouts.distance.tripd_g);
|
||||
}
|
||||
|
||||
setTotalDistance(distance) {
|
||||
distance = this.convertDistance(distance);
|
||||
if (distance >= 1000) {
|
||||
this.layouts.distance.totald.label = String(Math.round(distance));
|
||||
} else {
|
||||
this.layouts.distance.totald.label = distance.toFixed(1);
|
||||
}
|
||||
this.renderIfLayoutActive("distance", this.layouts.distance.totald_g);
|
||||
}
|
||||
|
||||
setDeviceAddress(address) {
|
||||
this.layouts.status.addr.label = address;
|
||||
this.renderIfLayoutActive("status", this.layouts.status.addr_g);
|
||||
}
|
||||
|
||||
setStatus(status) {
|
||||
this.layouts.status.status.label = status;
|
||||
this.renderIfLayoutActive("status", this.layouts.status.status_g);
|
||||
}
|
||||
}
|
||||
|
||||
var BLECSC;
|
||||
if (process.env.BOARD === "EMSCRIPTEN" || process.env.BOARD === "EMSCRIPTEN2") {
|
||||
// Emulator
|
||||
BLECSC = require("blecsc-emu");
|
||||
} else {
|
||||
// Actual hardware
|
||||
BLECSC = require("blecsc");
|
||||
}
|
||||
var blecsc = new BLECSC();
|
||||
var display = new CSCDisplay();
|
||||
var sensor = new CSCSensor(blecsc, display);
|
||||
|
||||
E.on('kill',()=>{
|
||||
sensor.disconnect();
|
||||
});
|
||||
|
||||
Bangle.setUI("updown", d => {
|
||||
sensor.interact(d);
|
||||
});
|
||||
|
||||
Bangle.loadWidgets();
|
||||
sensor.connect();
|
|
@ -0,0 +1 @@
|
|||
require("heatshrink").decompress(atob("mEwxH+AH4A/AH4A/AH/OAAIuuGFYuEGFQv/ADOlwV8wK/qwN8AAelGAguiFogACWsulFw6SERcwAFSISLnSMuAFZWCGENWllWLRSZC0vOAAovWmUslkyvbqJwIuHGC4uBAARiDdAwueL4YACMQLmfX5IAFqwwoMIowpMQ4wpGIcywDiYAA2IAAgwGq2kFwIvGC5YtPDJIuCF4gXPFxQHLF44XQFxAKOF4oXRBg4LOFwYvEEag7OBgReQNZzLNF5IXPBJlXq4vVC5Qv8R9TXQFwbvYJBgLlNbYXRBoYOEA44XfCAgAFCxgXYDI4VPC7IA/AH4A/AH4AWA"))
|
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"id": "cycling",
|
||||
"name": "Bangle Cycling",
|
||||
"shortName": "Cycling",
|
||||
"version": "0.01",
|
||||
"description": "Display live values from a BLE CSC sensor",
|
||||
"icon": "icons8-cycling-48.png",
|
||||
"tags": "outdoors,exercise,ble,bluetooth",
|
||||
"supports": ["BANGLEJS2"],
|
||||
"readme": "README.md",
|
||||
"storage": [
|
||||
{"name":"cycling.app.js","url":"cycling.app.js"},
|
||||
{"name":"cycling.settings.js","url":"settings.js"},
|
||||
{"name":"blecsc","url":"blecsc.js"},
|
||||
{"name":"cycling.img","url":"cycling.icon.js","evaluate": true}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
// This file should contain exactly one function, which shows the app's settings
|
||||
/**
|
||||
* @param {function} back Use back() to return to settings menu
|
||||
*/
|
||||
(function(back) {
|
||||
const storage = require('Storage')
|
||||
const SETTINGS_FILE = 'cycling.json'
|
||||
|
||||
// Set default values and merge with stored values
|
||||
let settings = Object.assign({
|
||||
metric: true,
|
||||
sensors: {},
|
||||
}, (storage.readJSON(SETTINGS_FILE, true) || {}));
|
||||
|
||||
const menu = {
|
||||
'': { 'title': 'Cycling' },
|
||||
'< Back': back,
|
||||
'Units': {
|
||||
value: settings.metric,
|
||||
format: v => v ? 'metric' : 'imperial',
|
||||
onchange: (metric) => {
|
||||
settings.metric = metric;
|
||||
storage.writeJSON(SETTINGS_FILE, settings);
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const sensorMenus = {};
|
||||
for (var addr of Object.keys(settings.sensors)) {
|
||||
// Define sub menu
|
||||
sensorMenus[addr] = {
|
||||
'': { title: addr },
|
||||
'< Back': () => E.showMenu(menu),
|
||||
'cm': {
|
||||
value: settings.sensors[addr].cm,
|
||||
min: 80, max: 240, step: 1,
|
||||
onchange: (v) => {
|
||||
settings.sensors[addr].cm = v;
|
||||
storage.writeJSON(SETTINGS_FILE, settings);
|
||||
},
|
||||
},
|
||||
'+ mm': {
|
||||
value: settings.sensors[addr].mm,
|
||||
min: 0, max: 9, step: 1,
|
||||
onchange: (v) => {
|
||||
settings.sensors[addr].mm = v;
|
||||
storage.writeJSON(SETTINGS_FILE, settings);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Add entry to main menu
|
||||
menu[addr] = () => E.showMenu(sensorMenus[addr]);
|
||||
}
|
||||
|
||||
E.showMenu(menu);
|
||||
})
|
Loading…
Reference in New Issue