Advertisement
Guest User

Untitled

a guest
Jul 28th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // home app
  2. var middleBut;
  3. var topBut_on;
  4. var topBut_off;
  5. var nextAttent;
  6. var btState = false;
  7. var voltLast = analogRead(30);
  8. var voltNow = voltLast;
  9. var lcdOnStamp;
  10. var loopy;
  11. if (NRF.getSecurityStatus().connected)
  12.   btState = true;
  13. var colorMode = {
  14.   "normal" : 0b111111000000000000,
  15.   "charging" : 0b000000111111000000,
  16.   "bluetooth" : 0b000000111111111111
  17. };
  18.  
  19. Bangle.on('lcdPower',function(on) {
  20.   if (on) {
  21.       setTimeout(installWatches,3000);
  22.       lcdOnStamp = Date.now();
  23.       onActivateAndInit();
  24.   } else {
  25.       uninstallWatches();
  26.    
  27.   }
  28. });
  29. function uninstallWatches() {
  30.   clearWatch(middleBut);
  31.   clearWatch(topBut_on);
  32.   clearWatch(topBut_off);
  33.   clearInterval(loopy);
  34. }
  35.  
  36. function getBatteryAccurate(){
  37.   v = analogRead(30);
  38.   const vlo = 0.50366211;
  39.   const vhi = 0.63476562;
  40.   pc = (v-vlo)*100/(vhi-vlo);
  41.   if (pc>100) pc=100;
  42.   if (pc<0) pc=0;
  43.   return pc;
  44. }
  45. function updateColor() {
  46.   g.setFont("6x8",3);
  47.   g.setFontAlign(0, 0, 0);
  48.   if ( btState ) g.setColor(colorMode.bluetooth);
  49.   else if (Bangle.isCharging()) g.setColor(colorMode.charging);
  50.   else g.setColor(colorMode.normal);
  51. }
  52. function writeScreenText() {
  53.  
  54.   updateColor();
  55.   g.drawString(" " + getBatteryAccurate().toFixed(2) + "%",120,120,true);
  56.   // var m = process.memory();
  57.   // g.drawString(" " + (m.usage*100/m.total).toFixed(2) + "%",120,160,true);
  58.   g.drawString(voltNow.toFixed(8),120,200,true);
  59.  
  60. }
  61.  
  62. function installWatches() {
  63.   var triggeredLongPress = false;
  64.   var btn1_state = false;
  65.   middleBut = setWatch(()=>{
  66.     Bangle.showLauncher();
  67.   },BTN2,{repeat:false,edge:"falling"});
  68.   var hold_cbk = null;
  69.   topBut_off = setWatch( () => {
  70.       btn1_state = false;
  71.       clearTimeout(hold_cbk);
  72.       if (!triggeredLongPress) Bangle.setLCDPower(false);
  73.       else
  74.         triggeredLongPress = false;
  75.     },BTN1, {repeat:true,edge:"falling"});
  76.   topBut_on = setWatch((o) => {
  77.     btn1_state = true;
  78.     hold_cbk = setTimeout(() => {
  79.       if ( btn1_state ) {
  80.         triggeredLongPress = true;
  81.         btState = !btState;
  82.         if ( btState ) {
  83.           // beep when bt is on
  84.           Bangle.beep(1000,4000);
  85.           NRF.wake();
  86.           Bluetooth.setConsole(true);
  87.         }
  88.         else {
  89.           Bangle.buzz(1000,1);
  90.           E.setConsole(null,{force:true});
  91.           NRF.sleep();
  92.         }
  93.         writeScreenText();
  94.       }
  95.     },5000);
  96.   }, BTN1, {repeat:true,edge:"rising"});
  97. }
  98.  
  99. function displayScreen() {
  100.   Bangle.drawWidgets();
  101.   writeScreenText();
  102. }
  103. function attention() {
  104.   Bangle.buzz(1000,1).then( ()=>
  105.     setTimeout(()=>Bangle.buzz(1000,1),1000));
  106.   nextAttent = E.hwRand()/2147483647;
  107.   nextAttent = 85000*60 + 10000*60 *nextAttent;
  108.   setTimeout(attention,nextAttent);
  109. }
  110. function onActivateAndInit() {
  111.   g.clear(false);
  112.   displayScreen();
  113.   setTimeout(() => {
  114.     voltNow = analogRead(30);
  115.     voltageLast = voltNow;
  116.     writeScreenText();
  117.   },1000);
  118.  
  119.   loopy = setInterval(() => {
  120.     updateColor();
  121.     var num = (((Date.now() - lcdOnStamp)/1000) | 0);
  122.     var second = '' + num;
  123.     if ( second.length > ('' + (num-1)).length ) {
  124.       // force redraw screen
  125.       g.clear(false);
  126.       displayScreen();
  127.     }
  128.     g.drawString(second,120,80,true);
  129.   },500);
  130. }
  131. global.WIDGETS={};
  132. require("gbridge.wid.js");
  133. require("widbt.wid.js");
  134. nextAttent = E.hwRand()/2147483647;
  135. nextAttent = 85000*60 + 10000*60 *nextAttent;
  136. setTimeout(attention,nextAttent);
  137. onActivateAndInit();
  138. installWatches();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement