Syntax Error While Working With AccelLog Program

Posted on
  • Hi,
    I am a new programmer. I am working on a app which uses accelerometer values and I have used the AccelLog program in my app. When I copy the command to the IDE it shows an error like

    Even if I just copy only the AccelLog program onto the IDE without my edits for the app, it still shows the same error.
    I have tried to upload the app to the Bangle with the json Title and Image bits, but it doesn't show on the Bangle. How do I get over this problem?


    1 Attachment

    • Error 1.jpg
  • Hi - yes, that's expected. It's just a warning though, not an error - you don't have to worry about that.

    What do you mean by "json Title and Image bits"? These? https://www.espruino.com/Bangle.js+First­+App#app-info-timer-app-info

    If you've uploaded those as a your_app_name.info file then it should be fine.

    But... if you're just wanting to modify the existing app, I'd say best thing is to just install it from https://banglejs.com/apps/#accellog and then you can upload and overwrite the accellog.app.js file, then the correct info and icon files should already be there

  • Thanks Gordan .
    I also need some help with buttons.
    I need to record only the highest accel value of the three for a particular motion. So when I press BTN2 only that value be logged and not continuous recording.
    Also how do I set BTN3 to EXIT and go back to the menu.
    Is there any app that I could use to work the buttons?

  • I'm not sure I understand what you want? So when BTN2 is pressed you just want to write a single value (whichever is bigger out of X, Y or Z?).

    Do you want BTN3 to exit the app completely and go back to the clock, or just to go back to the app's main menu?

  • Yes that is correct. I have built the program to identify the highest value so I want to save only that single value when BTN2 is pressed. And then reset the variable value to 0 for next measurement.
    On BTN3 press I would like to go to the app's main menu.

  • Ok, well it seems there's code already to go back to the main menu when BTN2 is pressed, so all you need to do is change BTN2 to BTN3 in this code:

      setWatch(()=>{
        Bangle.removeListener('accel', accelHandler);
        showMenu();
      }, BTN2);
    

    In terms of saving the measurement, I guess you want everything to start when you choose Start from the menu? So in that case it's the startRecord function.

    Right now, after recording is started, accelHandler is called for each measurement, so you just need to change that do something like this:

    function accelHandler(accel) {
        var t = getTime()-start;
        // get max value from all axes
        var maxValue = Math.max(Math.abs(accel.x), Math.abs(accel.y), Math.abs(accel.z));
        // write time and max value
        f.write([
          t*1000,
          maxValue].join(",")+"\n");
        /* we only want to save one value and return? If so remove
        our acceleration listener and go back to the menu... */
        Bangle.removeListener('accel', accelHandler);
        showMenu();
      }
    
  • Thanks Gordan.
    BTN2:
    I want to display the highest value and when I press BTN2 I want to save that value and then reset it to 0 and then save the next measurement and so on to build a single log file.

  • In that case something like this should do it:

    // draw the value to the screen - this could definitely be improved!
    function updateScreen() {
      g.reset();
      g.clearRect(0,100,240,140);
      g.setFontVector(20).setFontAlign(0,0);
      g.drawString(maxValue.toFixed(1),120,120­)
    }
    
    function accelHandler(accel) {
      var t = getTime()-start;
      // get the actual magnitude
      var mag = Math.sqrt(accel.x*accel.x + accel.y*accel.y + accel.z*accel.z);
      // if it's bigger, update the max value and screen
      if (mag > maxValue) {
        maxValue = mag;
        updateScreen();
      }
    }
    
    setWatch(function() {
      // write time and max value
      f.write([
        t*1000,
        maxValue].join(",")+"\n");
      // reset the values
      maxValue = 0;
      updateScreen();
    }, BTN2, {repeat:true});
    
  • Thanks Gordan.
    However maxvalue does not display on the watch screen. The screen in blank and there is just no display. Is there some other way to display the value?

  • The code by itself won't do it (if that's what you're trying?). You'd have to integrate it with the existing accelerometer app, copying it into the startRecord function to overwrite the existing accelHandler function: https://github.com/espruino/BangleApps/b­lob/master/apps/accellog/app.js

    You'd also need to change the existing setWatch function from BTN2 to BTN3.

  • Sorry Im not getting this. Could you show me how?

  • ?

  • This is the complete app:

    var fileNumber = 0;
    var MAXLOGS = 9;
    
    function getFileName(n) {
      return "accellog."+n+".csv";
    }
    
    function showMenu() {
      var menu = {
        "" : { title : "Accel Logger" },
        "File No" : {
          value : fileNumber,
          min : 0,
          max : MAXLOGS,
          onchange : v => { fileNumber=v; }
        },
        "Start" : function() {
          E.showMenu();
          startRecord();
        },
        "View Logs" : function() {
          viewLogs();
        },
        "Exit" : function() {
          load();
        },
      };
      E.showMenu(menu);
    }
    
    function viewLog(n) {
      E.showMessage("Loading...");
      var f = require("Storage").open(getFileName(n), "r");
      var records = 0, l = "", ll="";
      while ((l=f.readLine())!==undefined) {records++;ll=l;}
      var length = 0;
      if (ll) length = (ll.split(",")[0]|0)/1000;
    
      var menu = {
        "" : { title : "Log "+n }
      };
      menu[records+" Records"] = "";
      menu[length+" Seconds"] = "";
      menu["DELETE"] = function() {
        E.showPrompt("Delete Log "+n).then(ok=>{
          if (ok) {
            E.showMessage("Erasing...");
            f.erase();
            viewLogs();
          } else viewLog(n);
        });
      };
      menu["< Back"] = function() {
        viewLogs();
      };
    
      E.showMenu(menu);
    }
    
    function viewLogs() {
      var menu = {
        "" : { title : "Logs" }
      };
    
      var hadLogs = false;
      for (var i=0;i<=MAXLOGS;i++) {
        var f = require("Storage").open(getFileName(i), "r");
        if (f.readLine()!==undefined) {
          (function(i) {
            menu["Log "+i] = () => viewLog(i);
          })(i);
          hadLogs = true;
        }
      }
      if (!hadLogs)
        menu["No Logs Found"] = function(){};
      menu["< Back"] = function() { showMenu(); };
      E.showMenu(menu);
    }
    
    function startRecord(force) {
      if (!force) {
        // check for existing file
        var f = require("Storage").open(getFileName(file­Number), "r");
        if (f.readLine()!==undefined)
          return E.showPrompt("Overwrite Log "+fileNumber+"?").then(ok=>{
            if (ok) startRecord(true); else showMenu();
          });
      }
      var start = getTime();
      var t = 0, maxValue = 0;
    
      function updateScreen() {
        g.reset();
        g.clearRect(0,h/2 - 20,w-20,h/2 + 20);    
        g.setFontVector(40).setFontAlign(0,0);
        g.drawString(maxValue.toFixed(1),w/2,h/2­)
      }
      function accelHandler(accel) {
        t = getTime()-start;
        // get the actual magnitude
        var mag = Math.sqrt(accel.x*accel.x + accel.y*accel.y + accel.z*accel.z);
        // if it's bigger, update the max value and screen
        if (mag > maxValue) {
          maxValue = mag;
          updateScreen();
        }
      }
      
      // display
      g.clear(1);
      Bangle.drawWidgets();
      g.reset();
      var w = g.getWidth();
      var h = g.getHeight();
      g.setFont("6x8").setFontAlign(0,0).drawS­tring("Max Accel:", w/2, h/2 - 25);
      g.setFont("6x8",2).setFontAlign(0,0,1).d­rawString("SAVE",w-10,h/2);
      g.setFont("6x8",2).setFontAlign(0,0,1).d­rawString("STOP",w-10,3*h/4);
      updateScreen();
    
      // now start writing
      f = require("Storage").open(getFileName(file­Number), "w");
      f.write("Time (ms),Accel\n");
    
      // start accel
      Bangle.setPollInterval(80); // 12.5 Hz
      Bangle.on('accel', accelHandler);
      // buttons
      var watch = setWatch(function() {
        // write time and max value
        f.write([
          t*1000,
          maxValue].join(",")+"\n");
        // reset the values
        maxValue = 0;
        updateScreen();
      }, BTN2, {repeat:true});
      setWatch(()=>{
        clearWatch(watch);
        Bangle.removeListener('accel', accelHandler);
        showMenu();
      }, BTN3);
    }
    
    
    Bangle.loadWidgets();
    Bangle.drawWidgets();
    showMenu();
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Syntax Error While Working With AccelLog Program

Posted by Avatar for sp @sp

Actions