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:
single click, double click, triple click (or more)
media control HID ( play/stop/next/prev/volume)
light on!
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//
Single tap to send the Play/Pause singnal (suppose to launch the default player!)
Double tap for next song
Triple tap for Prev song
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,volumeChanged;
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);
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
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:
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!
How to play//
Press, hold and rotate for volume contorl