From 0cee31908614af8b8e02f7c9fd1f12073c95a2ab Mon Sep 17 00:00:00 2001 From: Purple-Tentacle <59914607+Purple-Tentacle@users.noreply.github.com> Date: Mon, 13 Apr 2020 13:04:18 +0200 Subject: [PATCH] Initial transfer --- apps/activepedom/10600.png | Bin 0 -> 370 bytes apps/activepedom/1600.png | Bin 0 -> 374 bytes apps/activepedom/600.png | Bin 0 -> 338 bytes apps/activepedom/README.md | 38 ++++++++ apps/activepedom/app-icon.js | 1 + apps/activepedom/app.png | Bin 0 -> 836 bytes apps/activepedom/settings.js | 81 ++++++++++++++++ apps/activepedom/widget.js | 180 +++++++++++++++++++++++++++++++++++ 8 files changed, 300 insertions(+) create mode 100644 apps/activepedom/10600.png create mode 100644 apps/activepedom/1600.png create mode 100644 apps/activepedom/600.png create mode 100644 apps/activepedom/README.md create mode 100644 apps/activepedom/app-icon.js create mode 100644 apps/activepedom/app.png create mode 100644 apps/activepedom/settings.js create mode 100644 apps/activepedom/widget.js diff --git a/apps/activepedom/10600.png b/apps/activepedom/10600.png new file mode 100644 index 0000000000000000000000000000000000000000..36de436df0021f74082396aab83d959120af1fc5 GIT binary patch literal 370 zcmV-&0ge8NP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGqB>(^xB>_oNB=7(L0S8G$K~z{r?Um6E zgCGn=+2Vu$|Fu4tY}8Rx8ZJsPW;C9pK??|$J0qJFSh zwJEg-q^^!TJ6|J?E1{J|${}S@{81&0m2!AHkrHI2M57uT*7(kK9ah4qL^G^>X9XCC zm9WyPD34>~cq1+Px#1ZP1_K>z@;j|==^1poj532;bRa{vGqB>(^xB>_oNB=7(L0Sie)K~z{r?UmaO z!ypJi-O`7Cwx8XHCYvmhDGKUlt<|1{*uvx@mOYeGFC#~PSMJ9Pc#?w$3f$8@rAr5`=*eiI>q^;c6_G@ z`AqshRSKLece?)oopD8oGuiY@!QE-x&)E?!4&9gX7qL$|{VoVw)V6PGT~_<{1@wSx zLmiN#ZvhnLiAm-oF}Xc U&O83Y0{{R307*qoM6N<$f?Z6Z`Tzg` literal 0 HcmV?d00001 diff --git a/apps/activepedom/600.png b/apps/activepedom/600.png new file mode 100644 index 0000000000000000000000000000000000000000..4d2c625c709002faa5e3e5207e0caa12782fe1d9 GIT binary patch literal 338 zcmV-Y0j>UtP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGqB>(^xB>_oNB=7(L0Ov_WK~z{r-IdV} z!ypJn-O`7Cc0aohO*(13hEj2oY^`zBSl+OwdPlF{`^8AX;833oOi^Eb=Ix< z4Y=t@{U4EN>is*v0p}eVkm5nsZ9V)v&4(utBU<85KnKYd3p?#)jMms|@h9wbM`9t_ zW6hyhNp&rLCzs1k1;I*(>6PYJtLJVIm&?w(zzej-@5asZn@5Y3MF(QJU*xBe^AR;z`8dFFL zUsbR>Fo;!9EEKUY+GwFz*~Jh=379B|Xkj6W8t)RLCYKaGh{f!Zz1f-B$?ap3KYZk| z|M|Y(cQZRXdyu~JzyMJ8;xUlRpl%|%2+V4t$03)M^626HSkN;uKy(BB&gd*OMRWs` zaf{T(O%PMFaTBE3zzYk{aEkR~D@XJaI^c}XQa$M5FF53ymvV^RX3C3x7QrXK=v4Oj831Y|-y`(Ee+f6AiE4qTQt>jdQS#LQW9S>WA^klD>`fmZ&M06`DSVq0 zzF=$sN{asv0f$KO-9#=rs3~^b0RPGJ^cpD^$2Dk^Frv>r7kZGn$ zKu3}dv?I$NL4Kg zfMBeS7{3Pb3@IXJr8g~5lU|~4;Iw6qdSH1fbQU^{zJXuBdRJgJs`q{t&^K}437Mt( zfK}e_1@uj{xqypO9pY#)g1DJM-^546%g}<-4(0$~F*g(4sGGR!fYGQER07(}L}%ud z23;czxIJ*aptwE(G-OI?&@h#7PZS>2ayph3ph>$BFX=(BBO31=$@e@9K$BJ?j=({v zabT&&znkzZw5E@#*F6Q$(TFi4lK*O&fF^YjhQDbs?xZy4Ck}4rR>H^MHZ@bSDW;E(H;iWU*PcZo8_9K(;h&mO8`>M<}B<0EEX8tM_eM|K( zA01MzViJH+8VwYjA>JO|0DKSR8;wn8p*-*zF%wCg>)U{D2>5)pN&W+*T-arpW9jGs O0000 { + var stepTimeDiff = 9999; //Time difference between two steps + var startTimeStep = new Date(); //set start time + var stopTimeStep = 0; //Time after one step + var timerResetActive = 0; //timer to reset active + var steps = 0; //steps taken + var stepsCounted = 0; //active steps counted + var active = 0; //x steps in y seconds achieved + var stepGoalPercent = 0; //percentage of step goal + var stepGoalBarLength = 0; //length og progress bar + var lastUpdate = new Date(); + var width = 45; + + var stepsTooShort = 0; + var stepsTooLong = 0; + var stepsOutsideTime = 0; + + //define default settings + const DEFAULTS = { + 'cMaxTime' : 1100, + 'cMinTime' : 240, + 'stepThreshold' : 30, + 'intervalResetActive' : 30000, + 'stepSensitivity' : 80, + 'stepGoal' : 10000, + }; + const SETTINGS_FILE = 'activepedom.settings.json'; + const PEDOMFILE = "activepedom.steps.json"; + + let settings; + //load settings + function loadSettings() { + settings = require('Storage').readJSON(SETTINGS_FILE, 1) || {}; + } + //return setting + function setting(key) { + if (!settings) { loadSettings(); } + return (key in settings) ? settings[key] : DEFAULTS[key]; + } + + function setStepSensitivity(s) { + function sqr(x) { return x*x; } + var X=sqr(8192-s); + var Y=sqr(8192+s); + Bangle.setOptions({stepCounterThresholdLow:X,stepCounterThresholdHigh:Y}); + } + + //format number to make them shorter + function kFormatter(num) { + if (num <= 999) return num; //smaller 1.000, return 600 as 600 + if (num >= 1000 && num < 10000) { //between 1.000 and 10.000 + num = Math.floor(num/100)*100; + return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'k'; //return 1600 as 1.6k + } + if (num >= 10000) { //greater 10.000 + num = Math.floor(num/1000)*1000; + return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'k'; //return 10.600 as 10k + } + } + + //Set Active to 0 + function resetActive() { + active = 0; + steps = 0; + if (Bangle.isLCDOn()) WIDGETS["activepedom"].draw(); + } + + function calcSteps() { + stopTimeStep = new Date(); //stop time after each step + stepTimeDiff = stopTimeStep - startTimeStep; //time between steps in milliseconds + startTimeStep = new Date(); //start time again + + //Remove step if time between first and second step is too long + if (stepTimeDiff >= setting('cMaxTime')) { //milliseconds + stepsTooLong++; //count steps which are note counted, because time too long + steps--; + } + + //Remove step if time between first and second step is too short + if (stepTimeDiff <= setting('cMinTime')) { //milliseconds + stepsTooShort++; //count steps which are note counted, because time too short + steps--; + } + + if (steps >= setting('stepThreshold')) { + if (active == 0) { + stepsCounted = stepsCounted + (stepThreshold -1) ; //count steps needed to reach active status, last step is counted anyway, so treshold -1 + stepsOutsideTime = stepsOutsideTime - 10; //substract steps needed to reac active status + } + active = 1; + clearInterval(timerResetActive); //stop timer which resets active + timerResetActive = setInterval(resetActive, setting('intervalResetActive')); //reset active after timer runs out + steps = 0; + } + + if (active == 1) { + stepsCounted++; //count steps + } + else { + stepsOutsideTime++; + } + } + + function draw() { + var height = 23; //width is deined globally + var stepsDisplayLarge = kFormatter(stepsCounted); + + //Check if same day + let date = new Date(); + if (lastUpdate.getDate() == date.getDate()){ //if same day + } + else { + stepsCounted = 1; //set stepcount to 1 + } + lastUpdate = date; + + g.reset(); + g.clearRect(this.x, this.y, this.x+width, this.y+height); + + //draw numbers + if (active == 1) g.setColor(0x07E0); //green + else g.setColor(0xFFFF); //white + g.setFont("6x8", 2); + g.drawString(stepsDisplayLarge,this.x+1,this.y); //first line, big number + g.setFont("6x8", 1); + g.setColor(0xFFFF); //white + g.drawString(stepsCounted,this.x+1,this.y+14); //second line, small number + + //draw step goal bar + stepGoalPercent = (stepsCounted / setting('stepGoal')) * 100; + stepGoalBarLength = width / 100 * stepGoalPercent; + if (stepGoalBarLength > width) stepGoalBarLength = width; //do not draw across width of widget + g.setColor(0x7BEF); //grey + g.fillRect(this.x, this.y+height, this.x+width, this.y+height); // draw background bar + g.setColor(0xFFFF); //white + g.fillRect(this.x, this.y+height, this.x+1, this.y+height-1); //draw start of bar + g.fillRect(this.x+width, this.y+height, this.x+width-1, this.y+height-1); //draw end of bar + g.fillRect(this.x, this.y+height, this.x+stepGoalBarLength, this.y+height); // draw progress bar + } + + //This event is called just before the device shuts down for commands such as reset(), load(), save(), E.reboot() or Bangle.off() + E.on('kill', () => { + let d = { //define array to write to file + lastUpdate : lastUpdate.toISOString(), + stepsToday : stepsCounted, + stepsTooShort : stepsTooShort, + stepsTooLong : stepsTooLong, + stepsOutsideTime : stepsOutsideTime + }; + require("Storage").write(PEDOMFILE,d); //write array to file + }); + + //When Step is registered by firmware + Bangle.on('step', (up) => { + steps++; //increase step count + calcSteps(); + if (Bangle.isLCDOn()) WIDGETS["activepedom"].draw(); + }); + + // redraw when the LCD turns on + Bangle.on('lcdPower', function(on) { + if (on) WIDGETS["activepedom"].draw(); + }); + + //Read data from file and set variables + let pedomData = require("Storage").readJSON(PEDOMFILE,1); + if (pedomData) { + if (pedomData.lastUpdate) lastUpdate = new Date(pedomData.lastUpdate); + stepsCounted = pedomData.stepsToday|0; + stepsTooShort = pedomData.stepsTooShort; + stepsTooLong = pedomData.stepsTooLong; + stepsOutsideTime = pedomData.stepsOutsideTime; + } + + setStepSensitivity(setting('stepSensitivity')); //set step sensitivity (80 is standard, 400 is muss less sensitive) + + //Add widget + WIDGETS["activepedom"]={area:"tl",width:width,draw:draw}; + +})(); \ No newline at end of file