Skip to content

Commit

Permalink
for WebUSB
Browse files Browse the repository at this point in the history
  • Loading branch information
8beeeaaat committed Dec 4, 2022
1 parent 0f8f81e commit 7916f5c
Show file tree
Hide file tree
Showing 89 changed files with 12,246 additions and 3,982 deletions.
46 changes: 46 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module.exports = {
env: {
browser: true,
es2021: true,
'w3c-web-usb': true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'airbnb',
'airbnb/hooks',
'prettier',
],
ignorePatterns: ['node_modules/', 'dist/', 'spec/', '*.js'],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
ecmaFeatures: {
jsx: true,
},
project: './tsconfig.json',
},
plugins: ['@typescript-eslint'],
rules: {
'@typescript-eslint/no-floating-promises': ['error', { ignoreIIFE: true }],
'import/extensions': [
'error',
'ignorePackages',
{
ts: 'never',
tsx: 'never',
},
],
'import/prefer-default-export': ['off'],
'no-bitwise': ['off'],
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
},
},
};
12 changes: 0 additions & 12 deletions .github/FUNDING.yml

This file was deleted.

91 changes: 79 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,37 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# Compiled ts files
build
# FuseBox cache
.fusebox/

# Typescript typings
typings
# DynamoDB Local files
.dynamodb/

# VS Code settings
.settings
.vscode
# TernJS port file
.tern-port
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/src
/spec
tsconfig.json
79 changes: 35 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,73 @@
# ant-plus
# web-ant-plus

A node.js module for ANT+
A package for ANT+ on Web browsers.

## Prerequisites
This repository was based on [ant-plus the original module for Node.js](https://github.com/Loghorn/ant-plus) by [@Loghorn](https://github.com/Loghorn).

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`
📝 This package uses the [WebUSB API](https://developer.mozilla.org/en-US/docs/Web/API/WebUSB_API). This API is [not available in some browsers](https://developer.mozilla.org/en-US/docs/Web/API/USB#browser_compatibility), so requires handling.

### Windows

Use [Zadig](http://sourceforge.net/projects/libwdi/files/zadig/) to install the WinUSB driver for your USB device. Otherwise you will get `LIBUSB_ERROR_NOT_SUPPORTED` when attempting to open devices.

### macOS

On macOS (tested on High Sierra and Mojave), installing `ant-plus` will also
install the required `libusb`.

Make sure that Garmin Express is not running,
because it will attach to the ANT+ stick and prevent `ant-plus` from doing so.

## Install
## How to use

```sh
npm install ant-plus
```

## usage

```javascript
var Ant = require('ant-plus');
npm install web-ant-plus
```

#### Create USB stick

```javascript
var stick = new Ant.GarminStick3;
```typescript
import { GarminStick3 } from 'web-ant-plus';
const stick = new GarminStick3();
```

#### Create sensors

```javascript
var sensor = new Ant.HeartRateSensor(stick);
```typescript
const hrSensor = new HeartRateSensor(stick);
```

#### Attach events

```javascript
sensor.on('hbData', function (data) {
console.log(data.DeviceID, data.ComputedHeartRate);
```typescript
hrSensor.on('hbData', function (data: HeartRateSensorState) {
console.log(data.DeviceID, data.ComputedHeartRate);
});

stick.on('startup', function () {
sensor.attach(0, 0);
hrSensor.attach(0, 0);
});
```

#### Open stick

```javascript
```typescript
if (!stick.open()) {
console.log('Stick not found!');
console.log('Stick not found!');
}
```

### scanning

```javascript
sensor.on('hbData', function (data) {
console.log(data.DeviceID, data.ComputedHeartRate);
```typescript
const hrScanner = new HeartRateScanner(stick);

hrScanner.on('hbData', function (data: HeartRateSensorState) {
console.log(data.DeviceID, data.ComputedHeartRate);
});

stick.on('startup', function () {
sensor.scan();
);
hrScanner.scan();
});

if (!stick.open()) {
console.log('Stick not found!');
console.log('Stick not found!');
}
```

## Important notes

* never attach a sensor before receiving the startup event
* never attach a new sensor before receiving the attached or detached event of the previous sensor
* never detach a sensor before receiving the attached or detached event of the previous sensor
- never attach a sensor before receiving the startup event
- never attach a new sensor before receiving the attached or detached event of the previous sensor
- never detach a sensor before receiving the attached or detached event of the previous sensor

## Objects

Expand Down Expand Up @@ -133,7 +118,7 @@ Fired after the stick is correctly closed.

#### methods

##### attach(channel, deviceId)
##### attach(channel: number, deviceID: number)

Attaches the sensors, using the specified channel and deviceId (use 0 to connect to the first device found).

Expand Down Expand Up @@ -233,3 +218,9 @@ Fired when data is received.

The `state.EventCount` value can be used to tell when a new measurement has been made by the sensor -
it's value will have been incremented.

```
This software is subject to the ANT+ Shared Source License www.thisisant.com/swlicenses
Copyright (c) Garmin Canada Inc. 2018
All rights reserved.
```
33 changes: 0 additions & 33 deletions ant-plus.js

This file was deleted.

24 changes: 24 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WebUSB ANT+®</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
1 change: 1 addition & 0 deletions example/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7916f5c

Please sign in to comment.