incyclist-ant-plus
v0.3.3
Published
Incyclist library for ANT+ - originally forked from longhorn/ant-plus
Downloads
533
Readme
incyclist-ant-plus
Module to interface ANT+ Sensors/Devices, developed and maintained for Incyclist Indoor Cycling App
Prerequisites
Libusb is included as a submodule. On Linux, you'll need libudev to build libusb. On Ubuntu/Debian: sudo apt-get install build-essential libudev-dev
Windows
Using the AntDevice binding
Use Zadig to install the WinUSB driver for your USB device. Otherwise you will get LIBUSB_ERROR_NOT_SUPPORTED
when attempting to open devices.
Using the AntServerBinding binding
The antserver.exe
needs to be deployed together with your app. You can find a pre-compiled version in the ./bin directory, or you can compile it yourself based on the sources provided in the ./antserver directory
Install
npm install incyclist-ant-plus
Usage
Create and connect to USB Device (stick)
const {AntDevice} = require('incyclist-ant-plus/lib/bindings')
const ant = new AntDevice({startupTimeout:2000})
const success = await ant.open()
The library will automatically detect the matching Stick type ( Garmin2 / Garmin3)
Reserve the next available channel
const channel = ant.getChannel();
Use channel to scan for Sensors
const {BicyclePowerSensor,FitnessEquipmentSensor,HeartRateSensor} = require('incyclist-ant-plus');
//channel.on('detect', (profile, id) => console.log('detected',profile,id))
//channel.on('data', (profile, id, data) => console.log('data',profile,id, data))
channel.attach(new BicyclePowerSensor())
channel.attach(new HeartRateSensor())
channel.attach(new FitnessEquipmentSensor())
const detected = await channel.startScanner({timeout:10000})
console.log(detected)
Stop ongoing scan
might be used when scan should be stopped as soon as first/specific device is detected
has to be called explicitly if scan is done without timeout
channel.stopScanner()
Use channel to connect to a specific Sensor
const {FitnessEquipmentSensor} = require('incyclist-ant-plus');
channel.on('data', (profile, id, data) => console.log('data',profile,id, data))
const sensor = new FitnessEquipmentSensor()
const success = await channel.startSensor(sensor)
console.log(detected)
if (success) {
console.log( "set User Weight = 78kg, Bike Weight= 10kg" );
await sensor.sendUserConfiguration(78,10);
console.log( "set slope to 1.1%" );
await sensor.sendTrackResistance(1.1);
}
Important notes
- In order to avoid problems at next launch, always call ant.close() at the end of the program
Writing your customized Classes
The library was designed so that the implemention of the interfaces can be customized
As Incyclist is developed as React app running in Electron, it will require a special implementions of the IAntDevice interface
- IpcAntDevice: to be used in the renderer process to communicate with the AntDevice class in the Electron main process
- WinAntDevice: A special implementation using ANT.DLL, to remove the dependency to Zadig
Classes
AntDevice (IAntDevice)
Constructor
constructor( props?: {
deviceNo?: number;
startupTimeout?: number;
detailedStartReport?:boolean
debug?: boolean;
logger?: { logEvent?: (event)=>void, log:(...args)=>void};
})
deviceNo: In case you have multiple sticks connected, identifies the stick number. (0=first,1=second,...). Adding/removing a stick will not be recognized during the session ( i.e. after first use of constructor)
startupTimeout: timeout (in ms) after which the startup attempt should be stopped. If no timeout is given, the open()
call will be blocking.
detailedStartReport: if set to true, the open() method will provide more detailed result (AntOpenResult type), otherwise it will return a boolean
debug: enables debug mode ( message logging)
logger: logger to be use for debug logging
Methods
open
open():Promise<boolean|AntOpenResult>
Tries to open the stick.
In case the property detailedStartReport has been set, it will return any of 'Success', 'NoStick', 'StartupError'
Returns true
on success and false
on failure.
close
close():Promise<boolean>
Tries to close the stick and all opened channels.
Returns true
on success and false
on failure.
getMaxChannels
getMaxChannels():number
returns the maximum number of channels that this stick supports; valid only after stick was opened successfully.
getDeviceNumber
getDeviceNumber():number
returns the current device(stick) number (0=first,1=second,...); valid only after stick was opened successfully.
getChannel
getChannel():IChannel
Tries to reserve the next available channel - up to the maximum number of channels as indicated by getMaxChannels()
Returns a Channel object on success and null
on failure.
freeChannel
closeChannel(IChannel):void
removes the reservation for a channel.
Note This should never be called directly. It will be called by the methods of stopScanner()
and stopSensor()
write
write(data:Buffer):void
sends a message to the ANT+Device
Channel (IChannel)
TODO
Sensor
TODO
Available Sensors
TODO