You are reading a single comment by @BrianB and its replies. Click here to read the full conversation.
  • Here's the code:

    var controls = require("ble_hid_controls");
    NRF.setServices(undefined, { hid : controls.report });
    
    NRF.on('connect',()=>{
      digitalPulse(LED2,1,1000);
    });
    NRF.on('disconnect',()=>{
      digitalPulse(LED1,1,1000);
      NRF.connect();
      return;
    });
    
    var tempTimer = 0;
    var timerID = null;
    console.log("Battery : "+Puck.getBatteryPercentage()+"%");
    var init_mag = Puck.mag();
    var cur_mag,last_mag,IntervalID,offset_ang,v­olumeChanged;
    function cal_ang(xyz){
      xyz.x -= init_mag.x;
      xyz.y -= init_mag.y;
      xyz.z -= init_mag.z;
      return (Math.atan2(xyz.y, xyz.x) * 180) / Math.PI;
    }
    var cur_ang = cal_ang(Puck.mag());
    var last_ang = cur_ang;
    setWatch(function() {
       digitalWrite(LED3,1);
      start_mag();
    }, BTN, {edge:"rising", debounce:10, repeat:true});
    setWatch(function() {
      stop_mag();
      if(volumeChanged === null){
        onBtnTap();
      }
      all_lights_off();
    }, BTN, {edge:"falling", debounce:10, repeat:true});
    function start_mag(){
      volumeChanged = null;
      IntervalID = setInterval(function () {
        cur_ang = cal_ang(Puck.mag());
        offset_ang = cur_ang - last_ang;
        if(Math.abs(offset_ang) > 3 && Math.abs(offset_ang) <45){
          if(offset_ang < 0){
            console.log(" - :" + offset_ang);
            controls.volumeDown();
          }else{
            console.log(" + :" + offset_ang);
            controls.volumeUp();
          }
          last_ang = cur_ang;
          volumeChanged = 5;
          all_lights_off();
          digitalWrite(LED2,1);
        }else if(Math.abs(offset_ang) > 45){
          last_ang = cur_ang;
        }
      }, 500);
    }
    function stop_mag(){
      clearInterval(IntervalID);
    }
    function onBtnTap(){
      if(timerID !== null){
        clearTimeout(timerID);
      }
      tempTimer+=1;
      timerID = setTimeout(function () {  
        if(tempTimer == 1){
          controls.playpause();
        }else if(tempTimer == 2){
          controls.next();
          console.log("next");
        }else if(tempTimer >= 3){
          controls.prev();
          console.log("prev");
        }
        tempTimer = 0;
        timerID = null;
                              
      }, 500);
    }
    function all_lights_off(){
       digitalWrite(LED1,0);
       digitalWrite(LED2,0);
       digitalWrite(LED3,0);
    }
    
    //process.on('uncaughtException', function() {
    //  digitalWrite(LED1,1);
    //  load();
    //});
    

    After looking at lots of examples, I thought it should be put at the top. But perhaps that's the wrong place.

    Here's the full error I'm getting now:

    >BLE Connected, queueing BLE restart for later
    Battery : 100%
    =undefined
    Uncaught Error: BLE HID not enabled
     at line 1 col 55
    NRF.sendHIDReport(a,function(){NRF.sendH­IDReport(0,b)})
                                                          ^
    in function "b" called from line 1 col 6
    b(8,a)
         ^
    in function "playpause" called from line 72 col 26
          controls.playpause();
                             ^
    in function called from system
    > 
    
About

Avatar for BrianB @BrianB started