2020-04-12 11:32:25 +00:00
|
|
|
const GraphXZero = 40;
|
|
|
|
const GraphYZero = 180;
|
|
|
|
const GraphY100 = 80;
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
const GraphMarkerOffset = 5;
|
|
|
|
const MaxValueCount = 144;
|
|
|
|
const GraphXMax = GraphXZero + MaxValueCount;
|
2020-04-13 09:14:50 +00:00
|
|
|
|
|
|
|
const GraphLcdY = GraphYZero + 10;
|
2020-04-14 05:48:07 +00:00
|
|
|
const GraphCompassY = GraphYZero + 16;
|
2020-04-28 09:30:44 +00:00
|
|
|
const GraphBluetoothY = GraphYZero + 22;
|
2020-04-14 05:48:07 +00:00
|
|
|
const GraphGpsY = GraphYZero + 28;
|
|
|
|
const GraphHrmY = GraphYZero + 34;
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
const Storage = require("Storage");
|
2020-04-08 20:28:24 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
function renderCoordinateSystem() {
|
|
|
|
g.setFont("6x8", 1);
|
2020-04-08 20:28:24 +00:00
|
|
|
|
2020-04-13 09:14:50 +00:00
|
|
|
// Left Y axis (Battery)
|
|
|
|
g.setColor(1, 1, 0);
|
|
|
|
g.drawLine(GraphXZero, GraphYZero + GraphMarkerOffset, GraphXZero, GraphY100);
|
|
|
|
g.drawString("%", 39, GraphY100 - 10);
|
2020-04-08 20:28:24 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
g.setFontAlign(1, -1, 0);
|
|
|
|
g.drawString("100", 30, GraphY100 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, GraphY100, GraphXZero, GraphY100);
|
2020-04-08 20:28:24 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
g.drawString("50", 30, GraphYZero - 50 - GraphMarkerOffset);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, 130, GraphXZero, 130);
|
2020-04-08 20:28:24 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
g.drawString("0", 30, GraphYZero - GraphMarkerOffset);
|
2020-04-13 09:14:50 +00:00
|
|
|
|
|
|
|
g.setColor(1,1,1);
|
|
|
|
g.setFontAlign(1, -1, 0);
|
|
|
|
g.drawLine(GraphXZero - GraphMarkerOffset, GraphYZero, GraphXMax + GraphMarkerOffset, GraphYZero);
|
|
|
|
|
|
|
|
// Right Y axis (Temperature)
|
|
|
|
g.setColor(0.4, 0.4, 1);
|
|
|
|
g.drawLine(GraphXMax, GraphYZero + GraphMarkerOffset, GraphXMax, GraphY100);
|
|
|
|
g.drawString("°C", GraphXMax + GraphMarkerOffset, GraphY100 - 10);
|
|
|
|
g.setFontAlign(-1, -1, 0);
|
|
|
|
g.drawString("20", GraphXMax + 2 * GraphMarkerOffset, GraphYZero - GraphMarkerOffset);
|
|
|
|
|
|
|
|
g.drawLine(GraphXMax + GraphMarkerOffset, 130, GraphXMax, 130);
|
|
|
|
g.drawString("30", GraphXMax + 2 * GraphMarkerOffset, GraphYZero - 50 - GraphMarkerOffset);
|
|
|
|
|
|
|
|
g.drawLine(GraphXMax + GraphMarkerOffset, 80, GraphXMax, 80);
|
|
|
|
g.drawString("40", GraphXMax + 2 * GraphMarkerOffset, GraphY100 - GraphMarkerOffset);
|
|
|
|
|
|
|
|
g.setColor(1,1,1);
|
2020-04-12 11:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function decrementDay(dayToDecrement) {
|
|
|
|
return dayToDecrement === 0 ? 6 : dayToDecrement-1;
|
|
|
|
}
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
function loadData() {
|
|
|
|
const startingDay = new Date().getDay();
|
|
|
|
|
|
|
|
// Load data for the current day
|
2020-04-13 09:14:50 +00:00
|
|
|
let logFileName = "bclog" + startingDay;
|
2020-04-12 11:32:25 +00:00
|
|
|
|
2020-04-13 09:14:50 +00:00
|
|
|
let dataLines = loadLinesFromFile(MaxValueCount, logFileName);
|
2020-04-12 11:32:25 +00:00
|
|
|
|
|
|
|
// Top up to MaxValueCount from previous days as required
|
2020-04-13 09:14:50 +00:00
|
|
|
let previousDay = decrementDay(startingDay);
|
2020-04-16 09:23:19 +00:00
|
|
|
while (dataLines.length < MaxValueCount && previousDay !== startingDay) {
|
2020-04-13 09:14:50 +00:00
|
|
|
let topUpLogFileName = "bclog" + previousDay;
|
|
|
|
let remainingLines = MaxValueCount - dataLines.length;
|
|
|
|
let topUpLines = loadLinesFromFile(remainingLines, topUpLogFileName);
|
2020-04-14 19:11:52 +00:00
|
|
|
|
|
|
|
if(topUpLines) {
|
|
|
|
dataLines = topUpLines.concat(dataLines);
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
previousDay = decrementDay(previousDay);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadLinesFromFile(requestedLineCount, fileName) {
|
2020-04-13 09:14:50 +00:00
|
|
|
let allLines = [];
|
|
|
|
let returnLines = [];
|
2020-04-12 11:32:25 +00:00
|
|
|
|
|
|
|
var readFile = Storage.open(fileName, "r");
|
|
|
|
|
|
|
|
while ((nextLine = readFile.readLine())) {
|
|
|
|
if(nextLine) {
|
|
|
|
allLines.push(nextLine);
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 09:14:50 +00:00
|
|
|
|
|
|
|
readFile = null;
|
2020-04-12 11:32:25 +00:00
|
|
|
|
|
|
|
if (allLines.length <= 0) return;
|
|
|
|
|
2020-04-13 09:14:50 +00:00
|
|
|
let linesToReadCount = Math.min(requestedLineCount, allLines.length);
|
|
|
|
let startingLineIndex = Math.max(0, allLines.length - requestedLineCount - 1);
|
2020-04-12 11:32:25 +00:00
|
|
|
|
|
|
|
for (let i = startingLineIndex; i < linesToReadCount + startingLineIndex; i++) {
|
|
|
|
if(allLines[i]) {
|
|
|
|
returnLines.push(allLines[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allLines = null;
|
|
|
|
|
|
|
|
return returnLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderData(dataArray) {
|
2020-04-13 09:14:50 +00:00
|
|
|
const switchableConsumers = {
|
|
|
|
none: 0,
|
|
|
|
lcd: 1,
|
|
|
|
compass: 2,
|
|
|
|
bluetooth: 4,
|
|
|
|
gps: 8,
|
|
|
|
hrm: 16
|
|
|
|
};
|
|
|
|
|
|
|
|
//const timestampIndex = 0;
|
|
|
|
const batteryIndex = 1;
|
|
|
|
const temperatureIndex = 2;
|
|
|
|
const switchabelsIndex = 3;
|
2020-04-16 09:23:19 +00:00
|
|
|
|
|
|
|
const minTemperature = 20;
|
|
|
|
const maxTemparature = 40;
|
|
|
|
|
|
|
|
const belowMinIndicatorValue = minTemperature - 1;
|
|
|
|
const aboveMaxIndicatorValue = maxTemparature + 1;
|
2020-04-13 09:14:50 +00:00
|
|
|
|
|
|
|
var allConsumers = switchableConsumers.none | switchableConsumers.lcd | switchableConsumers.compass | switchableConsumers.bluetooth | switchableConsumers.gps | switchableConsumers.hrm;
|
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
for (let i = 0; i < dataArray.length; i++) {
|
|
|
|
const element = dataArray[i];
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
var dataInfo = element.split(",");
|
|
|
|
|
2020-04-13 09:14:50 +00:00
|
|
|
// Battery percentage
|
|
|
|
g.setColor(1, 1, 0);
|
|
|
|
g.setPixel(GraphXZero + i, GraphYZero - parseInt(dataInfo[batteryIndex]));
|
|
|
|
|
|
|
|
// Temperature
|
|
|
|
g.setColor(0.4, 0.4, 1);
|
2020-04-16 09:23:19 +00:00
|
|
|
|
|
|
|
let datapointTemp = parseFloat(dataInfo[temperatureIndex]);
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-16 09:23:19 +00:00
|
|
|
if (datapointTemp < minTemperature) {
|
|
|
|
datapointTemp = belowMinIndicatorValue;
|
|
|
|
}
|
|
|
|
if (datapointTemp > maxTemparature) {
|
|
|
|
datapointTemp = aboveMaxIndicatorValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale down the range of 20 - 40°C to a 100px y-axis, where 1px = .25°
|
|
|
|
let scaledTemp = Math.floor(((datapointTemp * 100) - 2000) / 20) + ((((datapointTemp * 100) - 2000) % 100) / 25);
|
|
|
|
|
2020-04-13 09:14:50 +00:00
|
|
|
g.setPixel(GraphXZero + i, GraphYZero - scaledTemp);
|
|
|
|
|
|
|
|
// LCD state
|
2020-04-14 15:30:10 +00:00
|
|
|
if (parseInt(dataInfo[switchabelsIndex]) & switchableConsumers.lcd) {
|
2020-04-13 09:14:50 +00:00
|
|
|
g.setColor(1, 1, 1);
|
|
|
|
g.setFontAlign(1, -1, 0);
|
|
|
|
g.drawString("LCD", GraphXZero - GraphMarkerOffset, GraphLcdY - 2, true);
|
|
|
|
g.drawLine(GraphXZero + i, GraphLcdY, GraphXZero + i, GraphLcdY + 1);
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
// Compass state
|
2020-04-14 15:30:10 +00:00
|
|
|
if (parseInt(dataInfo[switchabelsIndex]) & switchableConsumers.compass) {
|
2020-04-14 05:48:07 +00:00
|
|
|
g.setColor(0, 1, 0);
|
|
|
|
g.setFontAlign(-1, -1, 0);
|
|
|
|
g.drawString("Compass", GraphXMax + GraphMarkerOffset, GraphCompassY - 2, true);
|
|
|
|
g.drawLine(GraphXZero + i, GraphCompassY, GraphXZero + i, GraphCompassY + 1);
|
|
|
|
}
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-28 09:30:44 +00:00
|
|
|
// Bluetooth state
|
|
|
|
if (parseInt(dataInfo[switchabelsIndex]) & switchableConsumers.bluetooth) {
|
|
|
|
g.setColor(0, 0, 1);
|
|
|
|
g.setFontAlign(1, -1, 0);
|
|
|
|
g.drawString("BLE", GraphXZero - GraphMarkerOffset, GraphBluetoothY - 2, true);
|
|
|
|
g.drawLine(GraphXZero + i, GraphBluetoothY, GraphXZero + i, GraphBluetoothY + 1);
|
|
|
|
}
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
// Gps state
|
2020-04-14 15:30:10 +00:00
|
|
|
if (parseInt(dataInfo[switchabelsIndex]) & switchableConsumers.gps) {
|
2020-04-14 05:48:07 +00:00
|
|
|
g.setColor(0.8, 0.5, 0.24);
|
|
|
|
g.setFontAlign(-1, -1, 0);
|
|
|
|
g.drawString("GPS", GraphXMax + GraphMarkerOffset, GraphGpsY - 2, true);
|
|
|
|
g.drawLine(GraphXZero + i, GraphGpsY, GraphXZero + i, GraphGpsY + 1);
|
|
|
|
}
|
2020-04-13 09:14:50 +00:00
|
|
|
|
2020-04-14 05:48:07 +00:00
|
|
|
// Hrm state
|
2020-04-14 15:30:10 +00:00
|
|
|
if (parseInt(dataInfo[switchabelsIndex]) & switchableConsumers.hrm) {
|
2020-04-14 05:48:07 +00:00
|
|
|
g.setColor(1, 0, 0);
|
|
|
|
g.setFontAlign(1, -1, 0);
|
|
|
|
g.drawString("HRM", GraphXZero - GraphMarkerOffset, GraphHrmY - 2, true);
|
|
|
|
g.drawLine(GraphXZero + i, GraphHrmY, GraphXZero + i, GraphHrmY + 1);
|
|
|
|
}
|
2020-04-12 11:32:25 +00:00
|
|
|
}
|
2020-04-13 09:14:50 +00:00
|
|
|
|
|
|
|
dataArray = null;
|
2020-04-12 11:32:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:30:10 +00:00
|
|
|
function renderHomeIcon() {
|
|
|
|
//Home for Btn2
|
|
|
|
g.setColor(1, 1, 1);
|
|
|
|
g.drawLine(220, 118, 227, 110);
|
|
|
|
g.drawLine(227, 110, 234, 118);
|
|
|
|
|
|
|
|
g.drawPoly([222,117,222,125,232,125,232,117], false);
|
|
|
|
g.drawRect(226,120,229,125);
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:32:25 +00:00
|
|
|
function renderBatteryChart() {
|
|
|
|
renderCoordinateSystem();
|
2020-04-13 09:14:50 +00:00
|
|
|
let data = loadData();
|
2020-04-12 11:32:25 +00:00
|
|
|
renderData(data);
|
2020-04-13 09:14:50 +00:00
|
|
|
data = null;
|
2020-04-08 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:30:10 +00:00
|
|
|
// Show launcher when middle button pressed
|
|
|
|
function switchOffApp(){
|
|
|
|
Bangle.showLauncher();
|
|
|
|
}
|
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
// special function to handle display switch on
|
|
|
|
Bangle.on('lcdPower', (on) => {
|
|
|
|
if (on) {
|
2020-04-14 15:30:10 +00:00
|
|
|
g.clear();
|
2020-04-12 11:32:25 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2020-04-13 09:14:50 +00:00
|
|
|
renderBatteryChart();
|
2020-04-08 20:28:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-16 09:23:19 +00:00
|
|
|
setWatch(switchOffApp, BTN2, {edge:"falling", debounce:50, repeat:true});
|
2020-04-14 15:30:10 +00:00
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
g.clear();
|
2020-04-09 06:35:11 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2020-04-08 20:28:24 +00:00
|
|
|
|
2020-04-14 15:30:10 +00:00
|
|
|
renderHomeIcon();
|
|
|
|
|
2020-04-08 20:28:24 +00:00
|
|
|
renderBatteryChart();
|