A set of pretty APIs for some features of Puck.js.
Modules:
Examples:
Button.on("double", ()=>console.log("I got double clicked!"));Emits events for button presses:
up,down- for push and releasepress- single presseslong- long pressdouble- double pressmulti- multi-press sequence, passing an argument with the number of presses
Lights.pulsing().color(Lights.BLUE).when(Button, "down").until(Button, "up");
Lights.blip(Lights.CYAN, 500);Simple API for controlling the LEDs. (Currently WIP - started hitting performance limits of the board, so optimizations underway, but generally works.)
Each call adds a program to a stack. For example, the following will result in a steady red color for five seconds, with a blue light blinking at the same time.
Lights.steady().color(Lights.RED).for(5000).now();
Lights.blinking().color(Lights.BLUE).for(5000).now();Each program must have a brightness, color, start, and end function.
blip(color,duration): same asLights.steady().color(color).now().for(duration)
steady(): constant 100% brightnesspulsing(speed, lower_bound): sinusoidal pulsing effect, with a adjustable speed (default 1s per cycle) and lower bound (default 0%)blinking(speed, lower_bound): simple blinking (high-low-high-low...) effect, with a adjustable speed (default 1s per cycle) and lower bound (default 0%)
color(clr): single color, as an array of[R, G, B]as values from<0,1>, or constants defined onLights(BLACK,RED,GREEN,BLUE,CYAN,MAGENTA,YELLOW)
More functions are available in a separate LightsExtra module.
var Lights = require("https://raw.githubusercontent.com/rsmeral/puck.js/master/modules/Lights.js")();
require("https://raw.githubusercontent.com/rsmeral/puck.js/master/modules/LightsExtra.js")(Lights);
Lights.steady().rainbow(0.5).now().for(5000);rainbow(speed): cycle through the color spectrum, with a adjustable speed (default 1s per cycle)pearly(axis,transition): (Experimental) Emulates a pearlescent effect by changing colors based on device rotation
now(): start immediatelyafter(ms): start after given number of millisecondswhen(object,event): triggered by the given event (string) emitted by the given object
for(ms): stop after given number of millisecondsuntil(object,event): stops when the given event (string) is emitted by the given object
Prox.on("near", ()=>console.log("Light suddenly obstructed - some object most likely got close to me."));
Prox.on("far", ()=>console.log("Ahh, light back on - obstruction removed."));Emulates a proximity sensor by using the light sensor. Continuously adapts to lighting conditions. (Obvious caveat - doesn't work well in dimly lit environments).
Emits two events:
near- when an object approachesfar- when an object moves away
Knob.on("plus", ()=>hid.volumeUp());
Knob.on("minus", ()=>hid.volumeDown());
Knob.start();Emulates an incremental rotary encoder (scroll wheel) using magnetometer readings. The number of notches per circle is configurable. Works horizontally and vertically (or in any other plane).
Needs calibration - during 4 seconds after calling start(), rotate the Puck 360 degrees in a single plane to make at least one full circle.
If Puck is moved after calibration, it will most likely need re-calibration (stop();start()).
Emits events when Puck is rotated:
plus- clockwiseminus- counterclockwisenotch- both directions, passing an argument with the number of the current notch (for detecting absolute rotation - like compass).
Also emits an event when calibration finishes:
calibrated
Rotating the Puck like a knob changes volume, pressing the button issues a Play/Pause event. Works on Mac, Linux, Android.
Demonstrates four modules: Button, Lights, Proximity, Scroll Wheel. After uploading, rotation needs to be calibrated, see Scroll Wheel.
The experimental POWER_SAVE switch, if enabled, stops the rotation detection (which consumes battery) if your hand is not near the Puck, and starts it up again when Puck is mostly covered by a hand.
In power-save mode, consumption should be very low - only 2 light sensor readings per second as opposed to 10 magnetometer reading and processing cycles.