ココロキット+を JavaScript と TypeScript から動かすための SDK
You can use cocorokit+ from JavaScript and TypeScript
本 SDK は Web Bluetooth API を使っています。Web Bluetooth API は現在(2020-07-25)使えるブラウザが限られているため次のリンクから、ご利用の環境で利用できるかご確認ください(https://caniuse.com/#feat=web-bluetooth)。
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).
ココロキット+に接続して、フルカラーLEDとスイッチを試すサンプルです。
Connect to cocorokit+ and test the full-color LED and switches.
ココロキット+に接続して LED を赤色に光らせるサンプルです。
Connect to cocorokit+ and turn Red LED on.
import { CocorokitPlus } from "https://taisukef.github.io/cocorokit-plus-js-sdk/cocorokit-plus.js";
const ccrpls = await CocorokitPlus.find();
await ccrpls.setLED(CocorokitPlus.LED_R, 100);
ココロキット+との通信を必要とする関数は async / await で実装しています。
cocorokit+ JS SDK uses async / await function.
await cocoro.setPwmDuty(CocorokitPlus.PWM0, 1450); // サーボ0 の duty を 1450 usec に変更 | change servo zero's duty to 1450 usec
await cocoro.setPwmDutyAll(600, 600, 600, 600); // サーボ0 から 3 の duty を同時に 600usec に変更 | change all servos's duty to 600 usec simultaneously
await cocoro.setPwmDutyTime(CocorokitPlus.PWM0, 2300, 2000); // 2000 ミリ秒に duty が 2300 usec になるようにサーボ0 を動かす | change servo zero's duty 2300 usec after 2000 milli seconds.
setPwmDutyTime
は duty が変化する速度をゆっくりにしたいときに利用します。
You can use setPwmDutyTime
when you want to change servo duty slowly.
const onChanged = (data) => {
const input3 = (data >> 3) & 0x01;
console.log(input3); // 0 or 1
const input4 = (data >> 4) & 0x01;
console.log(input4); // 0 or 1
};
await cocoro.startDigitalInputNotification(onChanged);
ココロキット+は、センサーの値が変化すると通知が送ります。
通知された時に動かす関数を設定するコトでセンサの値に応じた操作が可能です。
data は 8bit の Unsigned Int 型で送られてきます。
ココロキット+のセンサ 1 とセンサ 2 は、3bit 目と 4bit 目に割り当てられています。
Cocorokit+ notify when digital input value changed. Please set callback function.
Callback functions arguments (data) is 8 bit unsigned int. Sensor 1 is on 3bit and Sensor 2 is on 4bit.
LED は PWM 制御をしているため、明るさの強さを指定します。
Cocorokit+ control LED using PWM. Please set target LED and brightness (0-100).
await cocoro.setLED(CocorokitPlus.LED_R, 100); // 赤を最大強度で光らせる | bright Red LED by max power
await cocoro.setLED(CocorokitPlus.LED_G, 50); // 緑を50%の強度で光らせる | bright Green LED by 50%
await cocoro.setLED(CocorokitPlus.LED_R, 0); // 赤色を消す | turn Red LED off