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);
}
@tc I'm glad it came in useful. I don't think it's super accurate but if it gets the job done then that's ok!
Post a reply
Bold
Italics
Link
Image
List
Quote
code
Preview
Formatting Help
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
Tips:
Create headers by underlining text with ==== or ----
To *italicise* text put one asterisk each side of the word
To **bold** text put two asterisks each side of the word
Embed images by entering: ![](https://www.google.co.uk/images/srpr/logo4w.png) That's the hard one: exclamation, square brackets and then the URL to the image in brackets.
* Create lists by starting lines with asterisks
1. Create numbered lists by starting lines with a number and a dot
> Quote text by starting lines with >
Mention another user by @username
For syntax highlighting, surround the code block with three backticks:
```
Your code goes here
```
Just like Github, a blank line must precede a code block.
If you upload more than 5 files we will display all attachments as thumbnails.
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