0.0.2 • Published 5 years ago

jaxcore-spin v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

screenshot

Jaxcore Spin

NodeJS library for Jaxcore Spin, the programmable wifi dial remote control.

Usage

npm install jaxcore-spin

Example Script

var Spin = require('jaxcore-spin');

Spin.connectAll(function (spin) {
	console.log('connected', spin.id);
	
	spin.on('spin', function (direction, position) {
		console.log('spin', direction, position);
	});
	
	spin.on('knob', function (pushed) {
		console.log('knob', pushed);
	});
	
	spin.on('button', function (pushed) {
		console.log('button', pushed);
	});
});

Save as test.js and run:

node test.js

Then turn on your Jaxcore Spin device, configure it for your wifi network, and it will auto-connect to the test script and begin printing the log when the knob is spun, or the button or knob is pushed.

If you do not own a Jaxcore Spin, try the web-based Spin Simulator which will launch a simulation of the device and appear on the network using the same TCP/UDP protocol that the physical devices implement.

Examples

See the /examples directory for test scripts.

Also see:

  • Spin-Synth - an html5 audio keyboard controlled by the Spin

  • Spin-A-Sketch - an html5 canvas etch-a-sketch game controlled by two Spin devices

Static Methods

  • Spin.debug(on) -- turn on verbose debugging
Spin.debug(true);
  • Spin.create(device) -- factory method for instantiating Spin instances, use the device object provided by the scan, connectAll, connectOne, connectTo, connectUSB, or connectUSBDevice static methods

  • Spin.scan(callback) -- low level scan, receives pings from all Jaxcore Spin devices

Spin.scan(function(device, done, rescan) {
	
	console.log('received ping from '+device.id);

	var spin = Spin.create(device);
	spin.on('spin', function(direction) {
		console.log('spinning', direction);
	});
	spin.connect();
		
});
  • Spin.connectAll(callback) -- greedily connect all Jaxcore Spins (wifi and usb), this is a short-hand version of using both scan() and connectUSB()
Spin.connectAll(function(spin) {
	console.log('connected', spin.id);
});
  • Spin.connectOne(callback) -- greedily connect only one Jaxcore Spin then stop scanning, after the device is disconnected or turned off this method will automatically connect next available Jaxcore Spin
Spin.connectOne(function(spin) {
	console.log('connected', spin.id);
});
  • Spin.connectTo(id, callback) - connect to a specific Jaxcore Spin by its unique ID
Spin.connectTo('D3F5A3DS80', function(spin) {
	console.log('connected', spin.id);
});
  • Spin.connectUSB(callback) -- connect any Spin devices connected by USB cable
Spin.connectUSB(function(spin) {
	console.log('connected by USB', spin.id, spin.usbDevice);
});
  • Spin.connectUSB(id, callback) -- connect a specific Spin device by USB
Spin.connectUSB('D3F5A3DS80', function(spin) {
	console.log('connected by USB', spin.id, spin.usbDevice);
});
  • Spin.connectUSBDevice(devicePath, callback) -- connect any Spin to a specific USB device path
Spin.connectUSBDevice('/dev/DFN199', function(spin) {
	console.log('connected by USB', spin.id, spin.usbDevice);
});

Spin Instance Methods

MethodArgumentsDescription
connect()connect the Spin device, the Spin device will open a TCP communication channel to the Spin NodeJS server
disconnect()disconnect the Spin device from the Spin NodeJS server
isConnected()returns boolean whether the device is connected to the NodeJS Server
flash(color, repeat)color array RGB color arrayrepeat integer (0-3) number of times to repeat the flashFlash all LEDs the same color, eg. spin.flash([0,255,0]) will flash green once for 2sspin.flash([255,0,0],2) will flash red twice for 1s each
quickFlash(color)color array RGB color arrayflashes all LEDs for 200ms, eg spin.flash([255,255,0]]
quickFlashSingle(color, index)color array RGB color arrayindex integer the led to flash (0 to 23)flash a single LED for 200ms, eg spin.quickFlashSingle([0,0,255],0)
lightsOn(color)color array RGB color arrayturns all LEDs on until they are specifically turned off
lightsOff()turns all LEDs off
setThrottle(throttle)throttle integer data rate limit in milliseconds, default is 20mseg. spin.setThrottle(100) will restrict TCP communications to every 100ms
setBrightness(brightness)brightness integer 0 to 255sets the LED brightness, all configured color patterns will be scaled by this brightness level
setRotateColors(mode, colorLeft, colorRight)mode integer 0,1,2colorLeft array RGB color array, colorRight array RGB color arrayconfigure the LED rotation pattern colors
rotate(direction, mode)direction integer 1 or -1mode integer 0, 1, 2shine the LED lights in a rotation pattern (eg. navigation)
setScaleColors(mode, colorLow, colorHigh, colorMid)mode integer 0,1,2colorLow array RGB color array for below the scale() percentcolorHigh array RGB color array above the scale() percentcolorMid array RGB color array for the midpointconfigures the LED rotation pattern colors, eg. spin.setScaleColors(0,255,0,0, 0,0,255, 255,255,255) will set mode 0 to be red/blue with a white midpoint
scale(percent, mode)percent float percentage ratio 0.0 to 1.0mode integer 0,1,2shine the LED lights in a linear scale pattern (eg. volume bar)
sleep()immediately go to low-power sleep mode
delaySleep()delays sleep mode (same thing as pressing a button or spinning the knob)
setSleepEnabled(enabled)enabled boolean true for sleep mode, false for never sleepenables or disabled sleep mode
setSleepTimer(ms)ms integer number of milliseconds of inactivitysets amount of inactivity before the device goes into low-power sleep mode

Instance Method Examples

var Spin = require('jaxcore-spin');

Spin.connectAll(function(spin) {

	spin.setScaleColors(0,[255,0,0], [0,0,255], [255,255,255])

	// flash green when connected
	spin.on('connect', function() {
		spin.flash([0,255,0]);
	});
	
	// rotate the LEDs color depending on button/knob pushes
	spin.on('spin', function(direction) {
		if (spin.knobPushed) spin.rotate(direction, 1);
		if (spin.buttonPushed) spin.rotate(direction, 2);
		else spin.rotate(direction, 0);
	});
  	
  	// shine blue when the button is pushed
	spin.on('button', function(pushed) {
		if (pushed) {
			spin.lightsOn([0,0,255]);
		}
		else {
			spin.lightsOff();
		}
	});
	
});

Properties

All public properties are exposed in a ReactJS-style .state child property and can be read at any time or watched for in the events (below).

StateTypeDescription
brightnessintegerbrightness value between 0 and 255
buttonPushedbooleantrue when the button is pushed
buttonPushTimeDatetimestamp of when the button was last pushed
buttonReleaseTimeDatetimestamp of when the button was last released
buttonPushDurationintegerelapsed time the knob has been pushed in milliseconds
buttonHoldbooleantrue when the button has been held down for 2 seconds, configurable with setButtonHoldThreshold()
buttonHoldThresholdintegerdetermines how long pushing the button activates the hold events, configurable with setButtonHoldThreshold()
connectedbooleantrue when the devices is connected to a Spin NodeJS service
spinPositionintegerabsolute position index
spinDirectionintegerdirection (1 or -1) rotated relative to the previous spin event
spinPreviousTimeDatetimestamp of the previous spin event
spinTimeDateDate of the most recent spin event
knobPushedbooleantrue when the knob is pushed
knobPushTimeDatetimestamp of when the knob was last pushed
knobReleaseTimeDatetimestamp of when the knob was last released
knobPushDurationintegerelapsed time the knob has been pushed in milliseconds
knobHoldbooleantrue when the knob has been held down for 2 seconds, configurable with setKnobHoldThreshold()
knobHoldThresholdintegerdetermines how long pushing the knob activates the hold events, configurable with setKnobHoldThreshold()
batteryVoltageintegerbattery voltage, typically between 2.7 and 4.2 Volts, shows as 5 Volts while charging
batteryPercentintegerbattery percentage as a ratio betwen 0.0 (2.7V minimum charge) and 1.0 (4.2V fully charged)
isChargingbooleantrue when the device is plugged into a USB charger
isChargedbooleantrue when the battery is fully charged (charging LED shines green)
sleepEnabledbooleantrue when the sleep mode is enabled, configurable with setSleepEnabled()
sleepingbooleantrue when the device has notified that has gone to sleep
sleepTimerintegersleep timer in milliseconds, configurable with setSleepTimer()
inactivityTimeintegermilliseconds since last activity

Properties Examples

var Spin = require('jaxcore-spin');

Spin.connectAll(function(spin) {

	spin.on('connected', function() {
		console.log('spin battery at '+ (Math.round(spin.state.batteryPercent*100*10)/10) + '%');
	});
	
	spin.on('spin', function(direction) {
		console.log('spin position=' + spin.state.spinPosition);
	});
  	
  	spin.on('button', function(pushed) {
		if (!pushed) { // if released
			console.log('button held for ' + spin.state.buttonPushDuration + ' ms');
		}
	});
	
});

Events

EventParametersDescription (emitted when)
connectthe device has connected to the NodeJS spin service
batterybatteryVoltage integer voltage between 2.7 and 4.2 or 5 while chargingbattery voltage has changed
disconnectthe device has disconnected from the NodeJS spin service
knobpushed booleanknob is pushed or released
knob-holdknob is held for more than 2 seconds
knob-pressknob is released after having been held for less than 2 seconds
knob-longpressknob is released after having been held for more than 2 seconds
buttonpushed booleanbutton is pushed or released
button-holdbutton is held for more than 2 seconds
button-pressbutton is released after having been held for less than 2 seconds
button-longpressbutton is released after having been held for more than 2 seconds
chargedcharged boolean true when fully chargedbattery is fully charged or starts getting drawn down
chargingcharging boolean true while plugged indevice has been plugged or unplugged from a USB charging port
settings-changedchanges objecthardware settings such as brightness, knobHoldThreshold, buttonHoldThreshold, sleepEnabled, or sleepTimer havechanged
sleep-warningthe 15 second sleep warning (yellow flashes) has activated
sleepingdevice has entered sleep mode
spindirection integer (1 or -1) direct knob has been rotatedposition integer absolute position indexthe knob has been rotated
updatechanges objectany property has been changed

Spin Buffer

Jaxcore Spin's resolution of 32 pulses per revolution is often too precise for many things you may want to do with the device, particularly when navigating menus. To reduce the resolution (ie. to slow the device down) the Spin Buffer can be used to absorb some of the spin events.

The Spin Buffer uses a combination of 3 types of buffer strategies (static, kinetic, and momentum) and a delay mechanism giving many ways to improve usability.

Spin Buffer API:

Create a spin buffer:

  • new Spin.Buffer( spin, bufferConfig )

spin: an instance of Spin() supplied by Spin.scan or Spin.connect() bufferConfig: sets alternative configuration options:

var bufferConfig = {
    staticTimeout: 2000,    // how many seconds of inactivity re-activates static buffer (default 3s)
    defaultDelay: 1000     // sets the default .delay() length of time in ms
};

Spin Buffer Methods

  • buffer.spin( direction, kineticBufferSize, staticBufferSize )

Updates the buffer and returns true if either the static or kinetic buffer has has been overflowed.

  • direction: is the direction parameter supplied from a spin event
  • kineticBufferSize: how many spin events to skip until the kinetic buffer is overflowed
  • staticBufferSize: how many spin events to skip before the static buffer is overflowed

The kinetic buffer is active while the Spin's knob is rotating in one direction. When the knob is idle for more than 2 seconds or it's direction of rotation is changed, the static buffer becomes active.

Example:

spin.on('spin', function (direction) {
    if (buffer.spin( direction, 4, 3 )) {
        /* your spin even handler code goes here */
    }
});
  • buffer.delay( delayTime )

    delayTimeamount of time in milliseconds to ignore spin events (optional, default is 500ms)

  • buffer.reset()

Resets the static buffer. This is the same as leaving the knob inactive for more than 3 seconds.

Spin Buffer Example:

var Spin = require('jaxcore-spin');
Spin.connectAll(function (spin) {
	
	var buffer = new Spin.Buffer(spin);
	
    spin.on('button', function(pushed) {
    	if (pushed) {
    		// reset static buffer when the button is pushed
    		buffer.reset();
    	}
    	else {
    	    // ignore all spin events for 2 seconds after releasing the button
    	    buffer.delay(2000);
    	}
    });
	
	spin.on('spin', function (direction) {
		if (buffer.spin(direction, 1, 3)) {
			// kinetic buffer of 1 will skip every other spin event
			// static buffer of 3 will skip 3 spin events when changing directions or idle for 2s
		    
		    console.log('spin', direction);
		}
		else {
			console.log('buffering...');
		}
	});
});

Momentum Timeout

TODO