Avatar for tc

tc

Member since Jan 2017 • Last active Jan 2017
  • 2 conversations
  • 5 comments

Most recent activity

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tc

    @Ron yours great!!!!

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tc

    I made a Media Controller, please take a look :D

    http://forum.espruino.com/conversations/­298144/

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tc

    Hi Guys I just got my Puck unit today and have a try on it.
    I am a designer so sorry for my dirty code.

    Basically what I want to test is:

    1. single click, double click, triple click (or more)
    2. media control HID ( play/stop/next/prev/volume)
    3. light on!
    4. Magnet

    The magnet part is the most hardest part for me .. and even now not really working good,
    so please! enhance that and let me know!
    After I play a while on my mac... then I connected it to my iPhone .. OMG its working too! miracle!

    • special thanks @gomako for the rotating code!

    How to play//

    1. Single tap to send the Play/Pause singnal (suppose to launch the default player!)
    2. Double tap for next song
    3. Triple tap for Prev song
    4. Press, hold and rotate for volume contorl

      /*
      Puck Media Controller
      By TC 2017.01.04
      
      This is the first test I got my Puck.js today.
      
      1.Single tap to send the Play/Pause singnal (suppose to launch the default player!)
      2.Double tap for next song
      3.Triple tap for Prev song
      4.Press, hold and rotate for volume contorl
      
      *Its working on iOS too!! miracle! 
      
      */
      
      
      var controls = require("ble_hid_controls");
      NRF.setServices(undefined, { hid : controls.report });
      
      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){
      //thanks! @gomako http://forum.espruino.com/profiles/95473­/
      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;
      
      //lisiten on press
      setWatch(function() {
      digitalWrite(LED3,1);
      start_mag();
      }, BTN, {edge:"rising", debounce:10, repeat:true});
      
      //lisiten on release
      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) <90){
        if(offset_ang < 0){
          console.log(" - :" + offset_ang);
          controls.volumeDown();
        }else{
          console.log(" + :" + offset_ang);
          controls.volumeUp();
        }
        last_ang = cur_ang;
        volumeChanged = 1;
        all_lights_off();
        digitalWrite(LED2,1);
      }else if(Math.abs(offset_ang) > 90){
        last_ang = cur_ang;
      }
      }, 300);
      }
      
      function stop_mag(){
      clearInterval(IntervalID);
      }
      
      function onBtnTap(){
      if(timerID !== null){
      clearTimeout(timerID);
      }
      
      tempTimer+=1;
      
      timerID = setTimeout(function () {  
      if(tempTimer == 1){
        //single click
        controls.playpause();
      }else if(tempTimer == 2){
        //double click
        controls.next();
        console.log("next");
      }else if(tempTimer >= 3){
        //trible click
        controls.prev();
        console.log("prev");
      }
      
      //console.log(tempTimer + " clicks in 300ms!");
      //console.log("timerID:"+timerID);
      tempTimer = 0;
      timerID = null;
                                
      }, 300);
      }
      
      function all_lights_off(){
      digitalWrite(LED1,0);
      digitalWrite(LED2,0);
      digitalWrite(LED3,0);
      }
      
      
  • in Puck.js, Pixl.js and MDBT42
    Avatar for tc

    Thanks! ... Sadly...I still don't get it, should I just ..

    var r = Puck.mag();
    var angleToTheNorth = getheading(r.x,r.y,r.x);

    seems not like this ?

  • in Puck.js, Pixl.js and MDBT42
    Avatar for tc

    Hi all!
    I want to make a volume control by rotation the Puckjs.
    basically I want to get the angle value change once use press and rotate the Puck,
    if the value large than a preset value, I call a volume up and then
    reset and wait for another value pass.. I also need to know every time user rotate left or right..

    I got xyz from the magnet sensor but I am not sure what to do next.
    I did some search on the sin cos tan.. its quite complicate for me..

    any hints ? thanks!!!

Actions