-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from YUKAI/async-await
内部の非同期処理を Promise から async/await へと変更
- Loading branch information
Showing
5 changed files
with
792 additions
and
713 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,62 @@ | ||
# cocoro kit javascript SDK | ||
[English](./README_en.md) | ||
|
||
You can run cocorokit with javascript. | ||
# ココロキット javascript SDK | ||
|
||
## Requirements | ||
ココロキットを JavaScript から動かすための SDK です. | ||
|
||
This module uses Web Bluetooth API. The support of API is limited. | ||
Please confirm your device following the link (https://caniuse.com/#feat=web-bluetooth). | ||
## 必要な環境 | ||
|
||
## Usage | ||
この SDK は Web Bluetooth API という機能を使っています. | ||
Web Bluetooth API は対応しているデバイスに制限があります.一例として,Android は標準の Chrome で Web Bluetooth API を利用できますが,iPhone では別途アプリのインストールが必要です. | ||
|
||
Before run below script, please get cocorokit board and turn on. | ||
## 使い方 | ||
|
||
以下は動かすサンプルです. | ||
|
||
```javascript | ||
import CocoroKit from "@ux-xu/cocorokit-js-sdk"; | ||
|
||
window.cocorokit = new CococoKit(onConnected); // callback on connected. | ||
window.cocorokit = new CococoKit(onConnected); // 接続したときのCallback関数を設定します. | ||
window.cocorokit.connect(); | ||
|
||
// after connected | ||
window.cocorokit.spin(CocoroKit.CONST.SPIN.RIGHT, 100); // Spin right motor by 100% power. | ||
window.cocorokit.reverse(CocoroKit.CONST.REVERSE.RIGHT, 100); // Reverse right motor by 100% power. | ||
window.cocorokit.spin(CocoroKit.CONST.SPIN.RIGHT, 100); // 右のモータを100%のパワーで動かします | ||
window.cocorokit.reverse(CocoroKit.CONST.REVERSE.RIGHT, 100); // 右のモータを100%のパワーで逆回転させます. | ||
``` | ||
|
||
## Functions | ||
## 関数についての説明 | ||
|
||
```javascript | ||
/** | ||
* Spin motor | ||
* モータを正回転させる | ||
* @param {Object} motorSet Cocorokit.CONST.SPIN.RIGHT|LEFT | ||
* @param {Number} ratio 0 - 100 | ||
*/ | ||
spin(motorSet, ratio) {} | ||
spin(motorSet, ratio) | ||
|
||
/** | ||
* Reverse motor | ||
* モータを逆回転させる | ||
* @param {Object} motorSet Cocorokit.CONST.REVERSE.RIGHT|LEFT | ||
* @param {Number} ratio 0 - 100 | ||
*/ | ||
reverse(motorSet, ratio) {} | ||
reverse(motorSet, ratio) | ||
|
||
/** | ||
* Stop motor | ||
* モータを止める | ||
* @param {Array} motorSet Cocorokit.CONST.STOP.RIGHT|LEFT | ||
*/ | ||
stop(motorSet) {} | ||
stop(motorSet) | ||
|
||
/** | ||
* Change LED color | ||
* 付いているLEDの色を変える | ||
* @param {Object} color Cocorokit.CONST.COLOR.RED|GREEN|BLUE | ||
* @param {Number} ratio 0 - 100 | ||
*/ | ||
color(color, ratio) {} | ||
color(color, ratio) | ||
|
||
/** | ||
* Clear current values | ||
* リセット(全て,モータのみ,LEDのみ) | ||
* @param {Object} type Cocorokit.CONST.RESET.ALL|MOTOR|LED | ||
*/ | ||
reset(type) {} | ||
reset(type) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[日本語](./README.md) | ||
|
||
# cocoro kit javascript SDK | ||
|
||
You can run cocorokit with javascript. | ||
|
||
## Requirements | ||
|
||
This module uses Web Bluetooth API. The support of API is limited. | ||
Please confirm your device following the link (https://caniuse.com/#feat=web-bluetooth). | ||
|
||
## Usage | ||
|
||
Before run below script, please get cocorokit board and turn on. | ||
|
||
```javascript | ||
import CocoroKit from "@ux-xu/cocorokit-js-sdk"; | ||
|
||
window.cocorokit = new CococoKit(onConnected); // callback on connected. | ||
window.cocorokit.connect(); | ||
|
||
// after connected | ||
window.cocorokit.spin(CocoroKit.CONST.SPIN.RIGHT, 100); // Spin right motor by 100% power. | ||
window.cocorokit.reverse(CocoroKit.CONST.REVERSE.RIGHT, 100); // Reverse right motor by 100% power. | ||
``` | ||
|
||
## Functions | ||
|
||
```javascript | ||
/** | ||
* Spin motor | ||
* @param {Object} motorSet Cocorokit.CONST.SPIN.RIGHT|LEFT | ||
* @param {Number} ratio 0 - 100 | ||
*/ | ||
spin(motorSet, ratio) {} | ||
|
||
/** | ||
* Reverse motor | ||
* @param {Object} motorSet Cocorokit.CONST.REVERSE.RIGHT|LEFT | ||
* @param {Number} ratio 0 - 100 | ||
*/ | ||
reverse(motorSet, ratio) {} | ||
|
||
/** | ||
* Stop motor | ||
* @param {Array} motorSet Cocorokit.CONST.STOP.RIGHT|LEFT | ||
*/ | ||
stop(motorSet) {} | ||
|
||
/** | ||
* Change LED color | ||
* @param {Object} color Cocorokit.CONST.COLOR.RED|GREEN|BLUE | ||
* @param {Number} ratio 0 - 100 | ||
*/ | ||
color(color, ratio) {} | ||
|
||
/** | ||
* Clear current values | ||
* @param {Object} type Cocorokit.CONST.RESET.ALL|MOTOR|LED | ||
*/ | ||
reset(type) {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,165 +1,131 @@ | ||
import Konashi from './konashi.js'; | ||
import Konashi from "./konashi.js"; | ||
|
||
class _PioPin { | ||
constructor(pin) { | ||
this.number = pin; | ||
this.pwmMode = Konashi.consts.KONASHI_PWM_DISABLE; | ||
this.pwmMode = Konashi.PWM_DISABLE; | ||
this.pwmRatio = 0; | ||
} | ||
} | ||
|
||
class Queue { | ||
constructor() { | ||
this.queue = Promise.resolve(true); | ||
this.stopQueue = false; | ||
} | ||
|
||
add(job, delay) { | ||
var p = function () { | ||
return new Promise((resolve) => { | ||
setTimeout(function () { | ||
job(); | ||
resolve(); | ||
}, delay); | ||
}); | ||
}; | ||
this.queue = this.queue.then(p); | ||
} | ||
} | ||
|
||
const _BLE_DELAYS = { | ||
NONE: 0, | ||
NORMAL: 50, | ||
LONG: 200, | ||
DEBUG: 1000 | ||
} | ||
}; | ||
|
||
class Cocoro { | ||
constructor(callback) { | ||
this._queue = new Queue(); | ||
this._konashi = null; | ||
this._name = null; | ||
this._isSending = 0; | ||
this._onConnected = callback; | ||
|
||
this._pioPins = [ | ||
new _PioPin(Konashi.consts.PIO0), | ||
new _PioPin(Konashi.consts.PIO1), | ||
new _PioPin(Konashi.consts.PIO2), | ||
new _PioPin(Konashi.consts.PIO3), | ||
new _PioPin(Konashi.consts.PIO4), | ||
new _PioPin(Konashi.consts.PIO5), | ||
new _PioPin(Konashi.consts.PIO6), | ||
new _PioPin(Konashi.consts.PIO7) | ||
new _PioPin(Konashi.PIO0), | ||
new _PioPin(Konashi.PIO1), | ||
new _PioPin(Konashi.PIO2), | ||
new _PioPin(Konashi.PIO3), | ||
new _PioPin(Konashi.PIO4), | ||
new _PioPin(Konashi.PIO5), | ||
new _PioPin(Konashi.PIO6), | ||
new _PioPin(Konashi.PIO7), | ||
]; | ||
} | ||
|
||
connect(prefix = 'cocorokit') { | ||
if (this._konashi) this._konashi.disconnect(); | ||
|
||
return Konashi.find(true, { | ||
filters: [{ | ||
namePrefix: prefix | ||
}], | ||
optionalServices: [Konashi._serviceUUID] | ||
}) | ||
.then((k) => { | ||
this._konashi = k; | ||
this._name = k.name(); | ||
this._isSending = 0; | ||
}) | ||
.then(() => { | ||
var that = this; | ||
this._pioPins.forEach(pin => { | ||
this._queue.add(function () { | ||
that._konashi.pwmMode(pin.number, Konashi.consts.KONASHI_PWM_ENABLE_LED_MODE); | ||
pin.pwmMode = Konashi.consts.KONASHI_PWM_ENABLE_LED_MODE; | ||
}, _BLE_DELAYS.LONG); | ||
|
||
this._queue.add(function () { | ||
that._konashi.pwmLedDrive(pin.number, 0); | ||
pin.pwmRatio = 0; | ||
|
||
if (pin.number == 7) { // Last | ||
that._onConnected(); | ||
} | ||
}, _BLE_DELAYS.LONG); | ||
}); | ||
}) | ||
async sleep(ms) { | ||
return new Promise((r) => setTimeout(r, ms)); | ||
} | ||
|
||
// type: 'all', 'motor', 'led' | ||
reset(type) { | ||
if (!this._konashi) return; | ||
async connect(prefix = "cocorokit") { | ||
if (this._konashi) this._konashi.disconnect(); | ||
|
||
if (type === "all") { | ||
this._pioPins.forEach(pin => { | ||
var that = this; | ||
this._queue.add(function () { | ||
that._konashi.pwmLedDrive(pin.number, 0); | ||
pin.pwmRatio = 0; | ||
}, _BLE_DELAYS.NORMAL); | ||
}); | ||
this._konashi = await Konashi.find(true, { | ||
filters: [ | ||
{ | ||
namePrefix: prefix, | ||
}, | ||
], | ||
optionalServices: [Konashi._serviceUUID], | ||
}).catch(() => null); | ||
|
||
if (this._konashi == null) { | ||
console.log("Could not find device"); | ||
return; | ||
} | ||
|
||
if (type === "motor") { | ||
this._pioPins.slice(1, 5).forEach(pin => { | ||
var that = this; | ||
this._queue.add(function () { | ||
that._konashi.pwmLedDrive(pin.number, 0); | ||
pin.pwmRatio = 0; | ||
}, _BLE_DELAYS.NORMAL); | ||
}); | ||
} | ||
this._name = this._konashi.deviceName; | ||
this._isSending = 0; | ||
|
||
if (type === "led") { | ||
this._pioPins.slice(5, 8).forEach(pin => { | ||
var that = this; | ||
this._queue.add(function () { | ||
that._konashi.pwmLedDrive(pin.number, 0); | ||
pin.pwmRatio = 0; | ||
}, _BLE_DELAYS.NORMAL); | ||
}); | ||
for (let i in this._pioPins) { | ||
let pin = this._pioPins[i]; | ||
await this._konashi.pwmMode(pin.number, Konashi.PWM_ENABLE_LED_MODE); | ||
await this.sleep(_BLE_DELAYS.LONG); | ||
pin.pwmMode = Konashi.PWM_ENABLE_LED_MODE; | ||
|
||
await this._konashi.pwmWrite(pin.number, 0); | ||
await this.sleep(_BLE_DELAYS.LONG); | ||
pin.pwmRatio = 0; | ||
|
||
if (pin.number == 7) { | ||
this._onConnected(); | ||
} | ||
} | ||
} | ||
|
||
getPwmRatio(pid) { | ||
async setPwmRatio(pid, ratio) { | ||
if (pid < 0 || 7 < pid) return; | ||
if (this._isSending >= 3) return; | ||
if (ratio == this._pioPins[pid].pwmRatio) return; | ||
|
||
return this._pioPins[pid].pwmRatio; | ||
this._isSending++; | ||
await this._konashi.pwmWrite(pid, ratio).catch((error) => { | ||
console.log("cocorokit: " + error); | ||
}); | ||
await this.sleep(_BLE_DELAYS.NORMAL); | ||
this._pioPins[pid].pwmRatio = ratio; | ||
if (this._isSending > 0) this._isSending--; | ||
} | ||
|
||
_errorCallback(error) { | ||
log.log("cocorokit error: " + error); | ||
/** | ||
* | ||
* @param {*} type "all", "motor", "led" | ||
*/ | ||
async reset(type) { | ||
if (!this._konashi) return; | ||
|
||
const disconnectError = /disconnected/; | ||
if (disconnectError.test(error)) {} | ||
if (type === "all") { | ||
for (let i in this._pioPins) { | ||
let pin = this._pioPins[i]; | ||
await this._konashi.pwmWrite(pin.number, 0); | ||
await this.sleep(_BLE_DELAYS.NORMAL); | ||
pin.pwmRatio = 0; | ||
} | ||
} | ||
|
||
const alreadyUsedError = /already in progress/; | ||
if (alreadyUsedError.test(error)) { | ||
this._queue = new Queue(); | ||
this._isSending = 0; | ||
this._konashi.reset(); | ||
if (type === "motor") { | ||
for (let i = 0; i < 5; i++) { | ||
let pin = this._pioPins[i]; | ||
await this._konashi.pwmWrite(pin.number, 0); | ||
await this.sleep(_BLE_DELAYS.NORMAL); | ||
pin.pwmRatio = 0; | ||
} | ||
} | ||
|
||
if (type === "led") { | ||
for (let i = 5; i < 8; i++) { | ||
let pin = this._pioPins[i]; | ||
await this._konashi.pwmWrite(pin.number, 0); | ||
await this.sleep(_BLE_DELAYS.NORMAL); | ||
pin.pwmRatio = 0; | ||
} | ||
} | ||
} | ||
|
||
setPwmRatio(pid, ratio) { | ||
getPwmRatio(pid) { | ||
if (pid < 0 || 7 < pid) return; | ||
if (this._isSending >= 3) return; | ||
if (ratio == this._pioPins[pid].pwmRatio) return; | ||
|
||
this._isSending++; | ||
var that = this; | ||
this._queue.add(function () { | ||
that._konashi.pwmLedDrive(pid, ratio) | ||
.then(() => { | ||
that._pioPins[pid].pwmRatio = ratio; | ||
if (that._isSending > 0) that._isSending--; | ||
}) | ||
.catch(that._errorCallback); | ||
}, _BLE_DELAYS.NORMAL); | ||
return this._pioPins[pid].pwmRatio; | ||
} | ||
} | ||
|
||
module.exports = Cocoro; | ||
module.exports.default = Cocoro; | ||
module.exports.default = Cocoro; |
Oops, something went wrong.