crimson-sdk
v1.4.9
Published
CrimsonSdk for NodeJS
Downloads
4
Readme
CrimsonSDK NodeJS
Requirement
- BLE 4.2 or later
- NodeJS ^16.14.0 || >=18.0.0
- Mac 10.15 or later
- Windows 10 build 10.0.15063 or later
- Raspberrypi OS 5.10
Integration
package.json
"dependencies": {
"crimson-sdk": "^1.4.0"
},
Usage
Init
let _cmsnSDK;
async function initSDK(onAdapterStateChanged) {
if (_cmsnSDK) return;
CrimsonLogger.i('CrimsonSDK.init');
// eslint-disable-next-line require-atomic-updates
_cmsnSDK = await CrimsonSDK.init(CMSNLogLevel.enum('info')); //info/error/warn
_cmsnSDK.on('error', (e) => _runErrorCB(e));
_cmsnSDK.on('onAdapterStateChanged', async (available) => {
if (available) {
// if (cmsnDeviceMap.size > 0) _startReconnectTimer();
} else {
_runErrorCB(CMSNError.enum('ble_power_off'));
await stopScan();
//NOTE: 在cmsnDeviceMap中保留,不删除,用于设备重连
cmsnDeviceMap.forEach(device => device.disconnect(false));
}
if (onAdapterStateChanged) onAdapterStateChanged(available);
});
if (_cmsnSDK.adapter.available) onAdapterStateChanged(true);
CrimsonLogger.i('CrimsonSDK.init done');
};
(async function main() {
CrimsonLogger.i('------------- Example Main -------------');
await initSDK(async (adapterAvailable) => {
if (adapterAvailable) await startScan();
});
})();
Scan 扫描
首次配对新设备时,需要先将头环设置为配对模式-->蓝灯快闪
_cmsnSDK.startScan(async device => {
console.log(`found device, [${p.name}] ${p.address}`);
});
Connect 连接
device.listener = exampleListener;
await device.connect();
Disconnect 断开连接
// disconnect device
await device.disconnect();
async function disposeSDK(cb) {
disconnectAll();
await CrimsonSDK.dispose();
_cmsnSDK = null;
if (cb) cb();
};
// when exit application, disconnect all devices & clean resources
process.on('SIGINT', async () => {
CrimsonLogger.i({ message: `SIGINT signal received.` });
await disposeSDK();
CrimsonLogger.i('End program');
process.exit(0);
});
async function disconnectAll() {
cmsnDeviceMap.forEach(device => device.disconnect());
cmsnDeviceMap.clear();
await stopScan();
}
ENUM
// 头环连接状态
const CONNECTIVITY = createEnum({
connecting: 0,
connected: 1,
disconnecting: 2,
disconnected: 3,
});
// 佩戴状态,电极与皮肤接触良好
const CONTACT_STATE = createEnum({
unknown: 0,
contact: 1, //佩戴好
no_contact: 2, //未戴好
});
// 佩戴方向,检测是否佩戴反
const ORIENTATION = createEnum({
unknown: 0,
normal: 1, //头环戴正
upsideDown: 2, //头环戴反
});
StartIMU 开启传输陀螺仪数据
// IMU SampleRate
const IMU = {
SAMPLE_RATE: createEnum({
unused: 0,
sr125: 0x10,
sr26: 0x20,
sr52: 0x30,
sr104: 0x40,
}),
};
device.listener = exampleListener;
device.startIMU(IMU.SAMPLE_RATE.enum('sr104'));
More
class CMSNDevice
id,
name,
connectivity,
connect()
disconnect()
pair(cb)
startDataStream(cb)
stopDataStream(cb)
startIMU(sampleRate, cb)
stopIMU(cb)
getSystemInfo(cb)
shutdown(cb)
// @param name length should be 4 ~ 15,单个汉字占3个字符
setDeviceName(name, cb)
// @param {(string|number[])} color e.g. string '#FFAABB' or array [255, 0, 0]
setLEDColor(color, cb)
// @param timeSec => seconds to put device into sleep mode, 0 for no sleep
setSleepIdleTime(timeSec, cb)
// @param intensity => vibration intensity, 0 ~ 100
setVibrationIntensity(intensity, cb)
}
Electron Docs
see[https://github.com/BrainCoTech/crimson-sdk-docs/blob/main/en/node_electron.md]