Add files via upload

master
CarlR9 2023-02-04 16:05:00 +13:00 committed by GitHub
parent 230baf7b24
commit aedac6a429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 395 additions and 396 deletions

View File

@ -1,2 +1,2 @@
0.01: 3/Feb/2023 Added 'Temperature Graph' app to depository. 0.01: 3/Feb/2023 Added 'Temperature Graph' app to depository.
0.02: 4/Feb/2023 Rewrote the widget handling after discovering there's a 'widget_utils' module to properly hide and show them.

View File

@ -1,395 +1,394 @@
// Temperature Graph // Temperature Graph
// BangleJS Script // BangleJS Script
Bangle.setBarometerPower(true,"tempgraph"); Bangle.setBarometerPower(true,"tempgraph");
Bangle.loadWidgets(); Bangle.loadWidgets();
var wids=WIDGETS; var widsOn=true;
var widsOn=true; var rm=null;
var rm=null; var gt=null;
var gt=null; var dg=null;
var dg=null; var Layout=require("Layout");
var Layout=require("Layout"); var C=true;
var C=true; var temp,tempMode,readErrCnt,watchButton2;
var temp,tempMode,readErrCnt,watchButton2;
var graph=require("Storage").readJSON("tempgraph.json",true);
var graph=require("Storage").readJSON("tempgraph.json",true); if(graph==undefined) {
if(graph==undefined) { graph=[];
graph=[]; }
}
var timesData=[
var timesData=[ // dur=duration, u=time units, d=divisions on graph, s=seconds per unit.
// dur=duration, u=time units, d=divisions on graph, s=seconds per unit. {dur:10,u:"Mins",d:5,s:60},
{dur:10,u:"Mins",d:5,s:60}, {dur:20,u:"Mins",d:4,s:60},
{dur:20,u:"Mins",d:4,s:60}, {dur:30,u:"Mins",d:3,s:60},
{dur:30,u:"Mins",d:3,s:60}, {dur:40,u:"Mins",d:4,s:60},
{dur:40,u:"Mins",d:4,s:60}, {dur:1,u:"Hr",d:4,s:3600},
{dur:1,u:"Hr",d:4,s:3600}, {dur:2,u:"Hrs",d:4,s:3600},
{dur:2,u:"Hrs",d:4,s:3600}, {dur:3,u:"Hrs",d:3,s:3600},
{dur:3,u:"Hrs",d:3,s:3600}, {dur:4,u:"Hrs",d:4,s:3600},
{dur:4,u:"Hrs",d:4,s:3600}, {dur:6,u:"Hrs",d:6,s:3600},
{dur:6,u:"Hrs",d:6,s:3600}, {dur:8,u:"Hrs",d:4,s:3600},
{dur:8,u:"Hrs",d:4,s:3600}, {dur:12,u:"Hrs",d:6,s:3600},
{dur:12,u:"Hrs",d:6,s:3600}, {dur:16,u:"Hrs",d:4,s:3600},
{dur:16,u:"Hrs",d:4,s:3600}, {dur:20,u:"Hrs",d:5,s:3600},
{dur:20,u:"Hrs",d:5,s:3600}, {dur:1,u:"Day",d:4,s:3600},
{dur:1,u:"Day",d:4,s:3600}, {dur:2,u:"Days",d:4,s:86400},
{dur:2,u:"Days",d:4,s:86400}, {dur:3,u:"Days",d:3,s:86400},
{dur:3,u:"Days",d:3,s:86400}, {dur:4,u:"Days",d:4,s:86400},
{dur:4,u:"Days",d:4,s:86400}, {dur:5,u:"Days",d:5,s:86400},
{dur:5,u:"Days",d:5,s:86400}, {dur:6,u:"Days",d:6,s:86400},
{dur:6,u:"Days",d:6,s:86400}, {dur:7,u:"Days",d:7,s:86400}
{dur:7,u:"Days",d:7,s:86400} ];
]; var times=[];
var times=[]; for(n=0;n<timesData.length;n++){
for(n=0;n<timesData.length;n++){ times.push(timesData[n].dur+" "+timesData[n].u);
times.push(timesData[n].dur+" "+timesData[n].u); }
} var durInd=0;
var durInd=0; var duration=times[durInd];
var duration=times[durInd];
function drawWids(){
function drawWids(){ g.clear();
g.clear(); if(widsOn){
if(widsOn){ Bangle.drawWidgets();
WIDGETS=wids; require("widget_utils").show();
Bangle.drawWidgets(); } else {
} else { require("widget_utils").hide();
WIDGETS={}; }
} }
}
function openMenu(){
function openMenu(){ drawWids();
drawWids(); E.showMenu(menu);
E.showMenu(menu); }
}
function redoMenu(){
function redoMenu(){ clearInterval(rm);
clearInterval(rm); E.showMenu();
E.showMenu(); openMenu();
openMenu(); }
}
function refreshMenu(){
function refreshMenu(){ rm = setInterval(redoMenu,100);
rm = setInterval(redoMenu,100); }
} function getF(c){
function getF(c){ // Get Fahrenheit temperature from Celsius.
// Get Fahrenheit temperature from Celsius. return c*1.8+32;
return c*1.8+32; }
}
function getT(){
function getT(){ Bangle.getPressure().then(p=>{
Bangle.getPressure().then(p=>{ temp=p.temperature;
temp=p.temperature; if(tempMode=="drawGraph"&&graph.length>0&&Math.abs(graph[graph.length-1].temp-temp)>10&&readErrCnt<2){
if(tempMode=="drawGraph"&&graph.length>0&&Math.abs(graph[graph.length-1].temp-temp)>10&&readErrCnt<2){ // A large change in temperature may be a reading error. ie. A 0C or less reading after
// A large change in temperature may be a reading error. ie. A 0C or less reading after // a 20C reading. So if this happens, the reading is repeated up to 2 times to hopefully
// a 20C reading. So if this happens, the reading is repeated up to 2 times to hopefully // skip such errors.
// skip such errors. readErrCnt++;
readErrCnt++; print("readErrCnt "+readErrCnt);
print("readErrCnt "+readErrCnt); return;
return; }
} clearInterval(gt);
clearInterval(gt); readErrCnt=0;
readErrCnt=0; switch (tempMode){
switch (tempMode){ case "showTemp":
case "showTemp": showT();
showT(); break;
break; case "drawGraph":
case "drawGraph": var date=new Date();
var date=new Date(); var dateStr=require("locale").date(date).trim();
var dateStr=require("locale").date(date).trim(); var hrs=date.getHours();
var hrs=date.getHours(); var mins=date.getMinutes();
var mins=date.getMinutes(); var secs=date.getSeconds();
var secs=date.getSeconds(); graph.push({
graph.push({ temp:temp,
temp:temp, date:dateStr,
date:dateStr, hrs:hrs,
hrs:hrs, mins:mins,
mins:mins, secs:secs
secs:secs });
}); if(graph.length==1){
if(graph.length==1){ graph[0].dur=durInd;
graph[0].dur=durInd; }
} require("Storage").writeJSON("tempgraph.json", graph);
require("Storage").writeJSON("tempgraph.json", graph); if(graph.length==150){
if(graph.length==150){ clearInterval(dg);
clearInterval(dg); }
} drawG();
drawG(); }
} });
}); }
}
function getTemp(){
function getTemp(){ readErrCnt=0;
readErrCnt=0; gt = setInterval(getT,800);
gt = setInterval(getT,800); }
}
function setButton(){
function setButton(){ var watchButton=setWatch(function(){
var watchButton=setWatch(function(){ clearInterval(gt);
clearInterval(gt); clearInterval(dg);
clearInterval(dg); clearWatch(watchButton);
clearWatch(watchButton); Bangle.removeListener("touch",screenTouch);
Bangle.removeListener("touch",screenTouch); openMenu();
openMenu(); },BTN);
},BTN); Bangle.on('touch',screenTouch);
Bangle.on('touch',screenTouch); }
}
function setButton2(){
function setButton2(){ watchButton2=setWatch(function(){
watchButton2=setWatch(function(){ clearWatch(watchButton2);
clearWatch(watchButton2); openMenu();
openMenu(); },BTN);
},BTN); }
}
function zPad(n){
function zPad(n){ return n.toString().padStart(2,0);
return n.toString().padStart(2,0); }
}
function screenTouch(n,ev){
function screenTouch(n,ev){ if(ev.y>23&&ev.y<152){
if(ev.y>23&&ev.y<152){ C=C==false;
C=C==false; drawG(false);
drawG(false); }
} }
}
function drawG(){
function drawG(){ function cf(t){
function cf(t){ if(C){
if(C){ return t;
return t; }
} return getF(t);
return getF(t); }
} drawWids();
drawWids(); var top=1;
var top=1; var bar=21;
var bar=21; var barBot=175-22;
var barBot=175-22; if(widsOn){
if(widsOn){ top=25;
top=25; bar=bar+24;
bar=bar+24; barBot=barBot-24;
barBot=barBot-24; }
} var low=graph[0].temp;
var low=graph[0].temp; var hi=low;
var hi=low; for(n=0;n<graph.length;n++){
for(n=0;n<graph.length;n++){ var t=graph[n].temp;
var t=graph[n].temp; if(low>t){
if(low>t){ low=t;
low=t; }
} if(hi<t){
if(hi<t){ hi=t;
hi=t; }
} }
} var tempHi=Math.ceil((cf(hi)+2)/10)*10;
var tempHi=Math.ceil((cf(hi)+2)/10)*10; var tempLow=Math.floor((cf(low)-2)/10)*10;
var tempLow=Math.floor((cf(low)-2)/10)*10; var div=2;
var div=2; if(tempHi-tempLow>10){
if(tempHi-tempLow>10){ div=5;
div=5; }
} if(C){
if(C){ g.setColor(1,0,0);
g.setColor(1,0,0); }else{
}else{ g.setColor(0,0,1);
g.setColor(0,0,1); }
} var step=(barBot-bar)/((tempHi-tempLow)/div);
var step=(barBot-bar)/((tempHi-tempLow)/div); for(n=0;n<graph.length;n++){
for(n=0;n<graph.length;n++){ var pos=tempLow-cf(graph[n].temp);
var pos=tempLow-cf(graph[n].temp); g.drawLine(n+3,pos*(step/div)+barBot,n+3,barBot+3);
g.drawLine(n+3,pos*(step/div)+barBot,n+3,barBot+3); }
} g.fillRect(161,barBot+5,174,barBot+20);
g.fillRect(161,barBot+5,174,barBot+20); g.setColor(1,1,1);
g.setColor(1,1,1); g.setFont("6x8:2");
g.setFont("6x8:2"); if(C){
if(C){ g.drawString("C",163,barBot+5);
g.drawString("C",163,barBot+5); }else{
}else{ g.drawString("F",163,barBot+5);
g.drawString("F",163,barBot+5); }
} g.setColor(0,0,0);
g.setColor(0,0,0); g.setFont6x15();
g.setFont6x15(); g.drawString("Temperature Graph - "+times[graph[0].dur],1,top);
g.drawString("Temperature Graph - "+times[graph[0].dur],1,top); g.drawRect(2,bar-4,153,barBot+4);
g.drawRect(2,bar-4,153,barBot+4); g.setFont("6x8:1");
g.setFont("6x8:1"); var num=tempHi;
var num=tempHi; for(n=bar;n<=barBot;n=n+step){
for(n=bar;n<=barBot;n=n+step){ g.drawLine(3,n,152,n);
g.drawLine(3,n,152,n); g.drawString(num.toString().padStart(3," "),155,n-4);
g.drawString(num.toString().padStart(3," "),155,n-4); num=num-div;
num=num-div; }
} step=151/timesData[graph[0].dur].d;
step=151/timesData[graph[0].dur].d; for(n=step+2;n<152;n=n+step){
for(n=step+2;n<152;n=n+step){ g.drawLine(n,bar-4,n,barBot+4);
g.drawLine(n,bar-4,n,barBot+4); }
} grSt=graph[0];
grSt=graph[0]; g.drawString("Start: "+grSt.date+" "+grSt.hrs+":"+zPad(grSt.mins),1,barBot+6);
g.drawString("Start: "+grSt.date+" "+grSt.hrs+":"+zPad(grSt.mins),1,barBot+6); var lastT=graph[graph.length-1].temp;
var lastT=graph[graph.length-1].temp; g.drawString("Last Reading:",1,barBot+14);
g.drawString("Last Reading:",1,barBot+14); g.setColor(1,0,0);
g.setColor(1,0,0); g.drawString(lastT.toFixed(1)+"C",85,barBot+14);
g.drawString(lastT.toFixed(1)+"C",85,barBot+14); g.setColor(0,0,1);
g.setColor(0,0,1); g.drawString(getF(lastT).toFixed(1)+"F",121,barBot+14);
g.drawString(getF(lastT).toFixed(1)+"F",121,barBot+14); process.memory(true);
process.memory(true); }
}
function drawGraph(){
function drawGraph(){ setButton();
setButton(); tempMode="drawGraph";
tempMode="drawGraph"; durInd=times.indexOf(duration);
durInd=times.indexOf(duration); graph=[];
graph=[]; getTemp();
getTemp(); dg=setInterval(getTemp,1000*timesData[durInd].dur*timesData[durInd].s/150);
dg=setInterval(getTemp,1000*timesData[durInd].dur*timesData[durInd].s/150); }
}
function showGraph(){
function showGraph(){ setButton();
setButton(); drawG();
drawG(); }
}
function noBluetooth(){
function noBluetooth(){ if(NRF.getSecurityStatus().connected){
if(NRF.getSecurityStatus().connected){ return false;
return false; }else{
}else{ message("Error! Your\nBangle Watch\ncurrently has\nno Bluetooth\nconnection.");
message("Error! Your\nBangle Watch\ncurrently has\nno Bluetooth\nconnection."); return true;
return true; }
} }
}
function saveGraph(){
function saveGraph(){ if(noBluetooth()){
if(noBluetooth()){ return;
return; }
} drawG();
drawG(); g.flip();
g.flip(); g.dump();
g.dump(); message("Graph has\nbeen sent\nto Web IDE\nfor saving.\n");
message("Graph has\nbeen sent\nto Web IDE\nfor saving.\n"); }
}
function saveData(){
function saveData(){ if(noBluetooth()){
if(noBluetooth()){ return;
return; }
} drawG();
drawG(); g.flip();
g.flip(); print("Temperature Graph - "+times[graph[0].dur]+"\n");
print("Temperature Graph - "+times[graph[0].dur]+"\n"); print("\"Date\",\"Time\",\"Celsius\",\"Fahrenheit\"");
print("\"Date\",\"Time\",\"Celsius\",\"Fahrenheit\""); for(n=0;n<graph.length;n++){
for(n=0;n<graph.length;n++){ var gr=graph[n];
var gr=graph[n]; print("\""+gr.date+"\",\""+gr.hrs+":"+zPad(gr.mins)+":"+zPad(gr.secs)+"\","+gr.temp+","+getF(gr.temp));
print("\""+gr.date+"\",\""+gr.hrs+":"+zPad(gr.mins)+":"+zPad(gr.secs)+"\","+gr.temp+","+getF(gr.temp)); }
} message("Data has\nbeen sent\nto Web IDE\nfor saving.\n");
message("Data has\nbeen sent\nto Web IDE\nfor saving.\n"); }
}
function message(mes){
function message(mes){ setButton2();
setButton2(); var messageLO=new Layout({
var messageLO=new Layout({ type:"v",c:[
type:"v",c:[ {type:"txt",font:"6x8:2",width:171,label:mes,id:"label"},
{type:"txt",font:"6x8:2",width:171,label:mes,id:"label"}, {type:"btn",font:"6x8:2",pad:3,label:"OK",cb:l=>exit()},
{type:"btn",font:"6x8:2",pad:3,label:"OK",cb:l=>exit()}, ],lazy:true
],lazy:true });
}); drawWids();
drawWids(); messageLO.render();
messageLO.render(); }
}
function showT(){
function showT(){ tempLO.lab1.label=tempLO.lab3.label;
tempLO.lab1.label=tempLO.lab3.label; tempLO.lab2.label=tempLO.lab4.label;
tempLO.lab2.label=tempLO.lab4.label; tempLO.lab3.label=tempLO.lab5.label;
tempLO.lab3.label=tempLO.lab5.label; tempLO.lab4.label=tempLO.lab6.label;
tempLO.lab4.label=tempLO.lab6.label; tempLO.lab5.label=temp.toFixed(2)+"C";
tempLO.lab5.label=temp.toFixed(2)+"C"; tempLO.lab6.label=getF(temp).toFixed(2)+"F";
tempLO.lab6.label=getF(temp).toFixed(2)+"F"; tempLO.render();
tempLO.render(); }
}
function exit(){
function exit(){ clearWatch(watchButton2);
clearWatch(watchButton2); openMenu();
openMenu(); }
}
function showTemp(){
function showTemp(){ tempMode="showTemp";
tempMode="showTemp"; setButton2();
setButton2(); tempLO=new Layout({
tempLO=new Layout({ type:"v",c:[
type:"v",c:[ {type:"h",c:[
{type:"h",c:[ {type:"txt",pad:5,col:"#f77",font:"6x8:2",label:" ",id:"lab1"},
{type:"txt",pad:5,col:"#f77",font:"6x8:2",label:" ",id:"lab1"}, {type:"txt",pad:5,col:"#77f",font:"6x8:2",label:" ",id:"lab2"}
{type:"txt",pad:5,col:"#77f",font:"6x8:2",label:" ",id:"lab2"} ]},
]}, {type:"h",c:[
{type:"h",c:[ {type:"txt",pad:5,col:"#f77",font:"6x8:2",label:" ",id:"lab3"},
{type:"txt",pad:5,col:"#f77",font:"6x8:2",label:" ",id:"lab3"}, {type:"txt",pad:5,col:"#77f",font:"6x8:2",label:" ",id:"lab4"}
{type:"txt",pad:5,col:"#77f",font:"6x8:2",label:" ",id:"lab4"} ]},
]}, {type:"h",c:[
{type:"h",c:[ {type:"txt",pad:5,col:"#f00",font:"6x8:2",label:" ",id:"lab5"},
{type:"txt",pad:5,col:"#f00",font:"6x8:2",label:" ",id:"lab5"}, {type:"txt",pad:5,col:"#00f",font:"6x8:2",label:" ",id:"lab6"}
{type:"txt",pad:5,col:"#00f",font:"6x8:2",label:" ",id:"lab6"} ]},
]}, {type:"h",c:[
{type:"h",c:[ {type:"btn",pad:2,font:"6x8:2",label:"Temp",cb:l=>getTemp()},
{type:"btn",pad:2,font:"6x8:2",label:"Temp",cb:l=>getTemp()}, {type:"btn",pad:2,font:"6x8:2",label:"Exit",cb:l=>exit()}
{type:"btn",pad:2,font:"6x8:2",label:"Exit",cb:l=>exit()} ]}
]} ]
] },{lazy:true});
},{lazy:true}); tempLO.render();
tempLO.render(); getTemp();
getTemp(); }
}
var menu={
var menu={ "":{
"":{ "title":" Temp. Graph"
"title":" Temp. Graph" },
},
"Widgets":{
"Widgets":{ value:widsOn,
value:widsOn, format:vis=>vis?"Hide":"Show",
format:vis=>vis?"Hide":"Show", onchange:vis=>{
onchange:vis=>{ widsOn=vis;
widsOn=vis; refreshMenu();
refreshMenu(); }
} },
},
"Duration":{
"Duration":{ value:times.indexOf(duration),
value:times.indexOf(duration), min:0,max:times.length-1,step:1,wrap:true,
min:0,max:times.length-1,step:1,wrap:true, format:tim=>times[tim],
format:tim=>times[tim], onchange:(dur)=>{
onchange:(dur)=>{ duration=times[dur];
duration=times[dur]; }
} },
},
"Draw Graph":function(){
"Draw Graph":function(){ E.showMenu();
E.showMenu(); drawGraph();
drawGraph(); },
},
"Show Graph" : function(){
"Show Graph" : function(){ E.showMenu();
E.showMenu(); if(graph.length>0){
if(graph.length>0){ showGraph();
showGraph(); }else{
}else{ message("No graph to\nshow as no\ngraph has been\ndrawn yet.");
message("No graph to\nshow as no\ngraph has been\ndrawn yet."); }
} },
},
"Save Graph" : function(){
"Save Graph" : function(){ E.showMenu();
E.showMenu(); if(graph.length>0){
if(graph.length>0){ saveGraph();
saveGraph(); }else{
}else{ message("No graph to\nsave as no\ngraph has been\ndrawn yet.");
message("No graph to\nsave as no\ngraph has been\ndrawn yet."); }
} },
},
"Save Data" : function(){
"Save Data" : function(){ E.showMenu();
E.showMenu(); if(graph.length>0){
if(graph.length>0){ saveData();
saveData(); }else{
}else{ message("No data to\nsave as no\ngraph has been\ndrawn yet.");
message("No data to\nsave as no\ngraph has been\ndrawn yet."); }
} },
},
"Show Temp":function(){
"Show Temp":function(){ E.showMenu();
E.showMenu(); showTemp();
showTemp(); }
} };
};
openMenu();
openMenu();