• Hi Gordon, thanks a lot!

    The switch/break error seems fixed; now something creeped in giving me

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     1v99 (c) 2018 G.Williams
    >setDisplayMode:  menu
    Uncaught Error: Cannot read property 'draw' of undefined
     at line 143 col 20
               pixlmenu.draw();
                       ^
    in function "setDisplayMode" called from line 115 col 24
      setDisplayMode("menu");
                           ^
    in function "onInit" called from line 1 col 8
    onInit();
           ^
    

    (also not working if renaming pixlmenu back to "m", as I had in my code before).
    This destroys the larger menu font you helped me with yesterday.

    This being the code right now:

    var actual_menu, gatt;
    var last_wheel_event_time = 0, actual_wheel_event_time;
    var last_cumulative_wheel_revolutions = 0, actual_cumulative_wheel_revolutions = 0, track_cumulative_revolutions = 0;
    var delta_revolutions, delta_event_time, timer = -1;
    var wheel_circumference = 2340;
    var delta_time_in_ms, connected = false;
    
    Pixl._menu = Pixl.menu; //Bug Fix von Gordon, s. Forum
    Pixl.menu = function(m) { Pixl._menu(m); }; //Bug Fix
    
    function connect() {
      print("Starting scan...");
      g.clear();
      var s = "Connecting...";
      g.drawString(s ,0 ,0); g.flip();
      try {
      NRF.requestDevice({ timeout:20000, filters: [{ namePrefix: 'Wahoo' }] }).then(function(device) {
      console.log(device);
      return device.gatt.connect();
      }).then(function(gatt_object) {
        gatt = gatt_object;
        s = "Looking for service...";
        g.clear(); g.drawString(s ,0 ,0); g.flip();
        return gatt.getPrimaryService("1816");
        }).then(function(service) {
          s = "Looking for char...";
          g.clear(); g.drawString(s ,0 ,0); g.flip();
          return service.getCharacteristic("0x2A5B");
        }).then(function(characteristic) {
          characteristic.on('characteristicvaluechanged', OnNotify);
          return characteristic.startNotifications();
          }).then(function() {
            connected = true;
            setDisplayMode("livedata");
            console.log("Done!");
            // Then call gatt.disconnect(); if/when you want to disconnect
            });
      } catch(error) {
          print("Sensor not found", error);
          g.clear(); g.drawString("No sensor found..." ,0 ,0); g.flip();
        }
    }
    
    function disconnect() {
    ...
    }
    
    function OnNotify(event) {
        ...
    }
    
    var mainmenu = {
      "" : {
        "title" : "-- Main Menu --",
        "fontHeight" : 10
      },
    
      "Connect" : function () { setDisplayMode("connecting"); },
      "Disconnect" : function () { disconnect(); },
      "Live Data" : function() {setDisplayMode("livedata"); },
      "New Track" : function () {},
      "Toggle Backlight" : function() { LED1.toggle(); },
      "-> Settings" : function () {Pixl.menu(SettingsMenu); },
      //"Exit" : function() { Pixl.menu(); },
    };
    
    var SettingsMenu = {
      "" : {
        "title" : "-- Settings --",
        "fontHeight" : 10
      },
      "Set Wheel Circumference" : undefined, // do nothing
      "Set Total Distance Counter" : undefined, // do nothing
      "< Back" : function() { Pixl.menu(mainmenu); },
    };
    
    function showLiveData() {
     
    }
    
    function onInit () {
      require("Font8x12").add(Graphics);
      setDisplayMode("menu");
      setWatch(function () { setDisplayMode("menu"); }, BTN2, {repeat:true, debounce:50});
    }
    
    function setDisplayMode(displaymode) {
     print("setDisplayMode: ", displaymode);
     switch (displaymode) {
             case "switchToConnected":
               Pixl.menu();
               break;
             case "connecting":
               if (timer!==-1) {clearInterval(timer); timer = -1;}
               Pixl.menu();
               connect();
               break;
             case "livedata":
               print("livedata");
               Pixl.menu();
               if (timer==-1) {
                 print("Set timer"); 
                 timer = setInterval( function () {showLiveData(); }, 2000); 
                 print("timer = ", timer);
               }
               break;
             case "menu":
               if (timer!==-1) {clearInterval(timer); timer = -1;}
               pixlmenu=Pixl.menu(mainmenu);
               g.setFont8x12();
               pixlmenu.draw();
               }
    }
    
    onInit();
    
    
About

Avatar for Joost @Joost started