• I'm almost hesitant to post this, because I recognize that the code is sloppy (my javascript fluency is cursory at best) and there are probably several much better ways of doing what I did (particularly the LED stuff), but just in case anyone might find this useful, here's the code I'm running on the puck:

    //
    // toggle LED states, based on bits in ldn
    // 1: Red, 2: Green, 4: Blue
    //
    function toggle(ldn) {
        if(ldn&1) digitalWrite(LED1,digitalRead(LED1)?0:1)­;
        if(ldn&2) digitalWrite(LED2,digitalRead(LED2)?0:1)­;
        if(ldn&4) digitalWrite(LED3,digitalRead(LED3)?0:1)­;
    }
    
    //
    // Flash LEDs based on bits in z
    // one time for i milliseconds
    //
    function wink(z,i) {
        digitalWrite([LED3,LED2,LED1],z);
        setTimeout("digitalWrite([LED1,LED2,LED3­], 0);", i);
      
        // one more LED turnoff, just to be sure
        setTimeout("digitalWrite([LED3,LED2,LED1­], 0);", (i*2)+1000);
    }
    
    //
    // flash each LED in sequence for i milliseconds 
    // repeat lp times
    //
    //
    function lightshow(i,lp) {
        for(x=0; x<lp; x++) {
            setTimeout("digitalWrite([LED3,LED2,LED1­], 4);", (i*((3*x)+0)));
            setTimeout("digitalWrite([LED3,LED2,LED1­], 2);", (i*((3*x)+1)));
            setTimeout("digitalWrite([LED3,LED2,LED1­], 1);", (i*((3*x)+2)));
        }
        
        setTimeout("digitalWrite([LED3,LED2,LED1­], 0);", (i*((3*lp)+0)));
    
        // one more LED turnoff, just to be sure
        setTimeout("digitalWrite([LED3,LED2,LED1­], 0);", (i*(3*lp))+1000);
    }
    
    //
    // flash LED based on bits in l, i times, for d milliseconds each time
    //
    function blink(l,i,d) {
        var tf1 = "toggle("+l+");";
        inid = setInterval(tf1,i);
        
        var tf2 = "clearInterval("+inid+");";
        setTimeout(tf2,d*i*2);
    
        // one more LED turnoff, just to be sure
        setTimeout("digitalWrite([LED3,LED2,LED1­], 0);", (d*i*2)+1000);
    }
    
    //
    // periodically tell the LEDs to turn off in case an LED 
    // is left on because I wrote the above functions poorly
    //
    // every 15 minutes
    //
    var autoLEDOffIntID = setInterval("digitalWrite([LED3,LED2,LED­1],0);",1000*60*15); 
    
    
    //
    // Code to whitelist devices for connections
    // Only allow:
    //      11:22:33:44:55:66 (my phone)
    //      aa:bb:cc:dd:ee:ff (BLE adapter on my laptop)
    //
    var curConnect = "x";
    NRF.on('connect',function(addr) {
      if ((addr!="11:22:33:44:55:66 public") &&
          (addr!="aa:bb:cc:dd:ee:ff public"))
          {
            blink(1,100,20);                    // OUTPUT - 20 quick red flashes when rejecting a non-whitelisted connection 
            NRF.disconnect();
          }
        
      curConnect = addr;
    
      if (curConnect == "11:22:33:44:55:66 public") 
        lightshow(100,10);                      // OUTPUT - celebration lights when connecting to my phone
      else wink(4,1500);                        // OUTPUT - 1 long blue when connecting to anything else
    
    });
    
    NRF.on('disconnect',function() {
        curConnect = "x";
        blink(5,500,5);                         // OUTPUT - 5 medium red&blue flashes when disconnecting
    });
    
    //
    // Code to toggle Bluetooth on long button press
    //
    var locked = false;
    setWatch(function() {
      locked = !locked;
      wink(locked?1:2,300);                     // OUTPUT - 1 quick red or green for inactive/active
      if (locked) 
        NRF.sleep();
      else NRF.wake();
    }, BTN, {repeat:true, edge:"rising", debounce:1000});
    
    
    //
    // Code to report connection status quick button press
    //
    setWatch(function() {
      if (locked) wink(1,2000);                 // OUTPUT - very long red if BT off
      else {
          if (curConnect == "x") wink(4,2000);  // OUTPUT - very long blue if BT on but not connected
          else wink(2,2000);                    // OUTPUT - very long green if BT on and connected
      }
    }, BTN, {repeat:true, edge:"rising", debounce:50});
    
    
About

Avatar for FuzzyBumble @FuzzyBumble started