-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcocorokit.min.js
1 lines (1 loc) · 7.75 KB
/
cocorokit.min.js
1
class CocoroKit{static get CONST(){return{SPIN:{RIGHT:{HIGH_PIN:1,LOW_PIN:2},LEFT:{HIGH_PIN:4,LOW_PIN:3}},REVERSE:{RIGHT:{HIGH_PIN:2,LOW_PIN:1},LEFT:{HIGH_PIN:3,LOW_PIN:4}},STOP:{RIGHT:[1,2],LEFT:[3,4]},COLOR:{RED:"5",GREEN:"6",BLUE:"7"},RESET:{ALL:"all",MOTOR:"motor",LED:"led"}}}constructor(t){this._cocoro=new Cocoro(t)}async connect(){await this._cocoro.connect("cocorokit").catch(t=>{console.log(t)})}async spin(t,i){await this._cocoro.setPwmRatio(t.HIGH_PIN,i),await this._cocoro.setPwmRatio(t.LOW_PIN,0)}async reverse(t,i){await this._cocoro.setPwmRatio(t.HIGH_PIN,i),await this._cocoro.setPwmRatio(t.LOW_PIN,0)}async stop(t){await this._cocoro.setPwmRatio(t[0],0),await this._cocoro.setPwmRatio(t[1],0)}async color(t,i){await this._cocoro.setPwmRatio(t,i)}async reset(t){await this._cocoro.reset(t)}}class _PioPin{constructor(t){this.number=t,this.pwmMode=Konashi.PWM_DISABLE,this.pwmRatio=0}}const _BLE_DELAYS={NORMAL:50,LONG:200};class Cocoro{constructor(t){this._konashi=null,this._name=null,this._isSending=0,this._onConnected=t,this._pioPins=[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)]}async sleep(t){return new Promise(i=>setTimeout(i,t))}async connect(t="cocorokit"){if(this._konashi&&this._konashi.disconnect(),this._konashi=await Konashi.find(!0,{filters:[{namePrefix:t}],optionalServices:[Konashi._serviceUUID]}).catch(()=>null),null!=this._konashi){this._name=this._konashi.deviceName,this._isSending=0;for(let t in this._pioPins){let i=this._pioPins[t];await this._konashi.pwmMode(i.number,Konashi.PWM_ENABLE_LED_MODE),await this.sleep(_BLE_DELAYS.LONG),i.pwmMode=Konashi.PWM_ENABLE_LED_MODE,await this._konashi.pwmWrite(i.number,0),await this.sleep(_BLE_DELAYS.LONG),i.pwmRatio=0,7==i.number&&this._onConnected()}}else console.log("Could not find device")}async setPwmRatio(t,i){t<0||7<t||this._isSending>=3||i!=this._pioPins[t].pwmRatio&&(this._isSending++,await this._konashi.pwmWrite(t,i).catch(t=>{console.log("cocorokit: "+t)}),await this.sleep(_BLE_DELAYS.NORMAL),this._pioPins[t].pwmRatio=i,this._isSending>0&&this._isSending--)}async reset(t){if(this._konashi){if("all"===t)for(let t in this._pioPins){let i=this._pioPins[t];await this._konashi.pwmWrite(i.number,0),await this.sleep(_BLE_DELAYS.NORMAL),i.pwmRatio=0}if("motor"===t)for(let t=0;t<5;t++){let i=this._pioPins[t];await this._konashi.pwmWrite(i.number,0),await this.sleep(_BLE_DELAYS.NORMAL),i.pwmRatio=0}if("led"===t)for(let t=5;t<8;t++){let i=this._pioPins[t];await this._konashi.pwmWrite(i.number,0),await this.sleep(_BLE_DELAYS.NORMAL),i.pwmRatio=0}}}getPwmRatio(t){if(!(t<0||7<t))return this._pioPins[t].pwmRatio}}class Konashi{static get HIGH(){return 1}static get LOW(){return 0}static get OUTPUT(){return 1}static get INPUT(){return 0}static get PULLUP(){return 1}static get NO_PULLS(){return 0}static get PIO0(){return 0}static get PIO1(){return 1}static get PIO2(){return 2}static get PIO3(){return 3}static get PIO4(){return 4}static get PIO5(){return 5}static get PIO6(){return 6}static get PIO7(){return 7}static get AIO0(){return 0}static get AIO1(){return 1}static get AIO2(){return 2}static get I2C_SDA(){return 6}static get I2C_SCL(){return 7}static get PWM_DISABLE(){return 0}static get PWM_ENABLE(){return 1}static get PWM_ENABLE_LED_MODE(){return 2}static get PWM_LED_PERIOD(){return 1e4}static get ANALOG_REFERENCE(){return 1300}static _createUUID(t){return"229b"+t+"-03fb-40da-98a7-b0def65c2d4b"}static get _serviceUUID(){return Konashi._createUUID("ff00")}static get defaultFilter(){return{filters:[{namePrefix:"konashi"}],optionalServices:[Konashi._serviceUUID]}}static async find(t=!0,i=Konashi.defaultFilter){const a=await navigator.bluetooth.requestDevice(i),e=new Konashi(a);return t&&await e.connect().catch(async t=>{/retrieve services/.test(t)&&await e.connect()}),e}constructor(t){this._device=t,this._gatt=null,this._service=null,this._c12c={},this._pioOutputs=0}get _c12cUUIDs(){return{pioSetting:Konashi._createUUID("3000"),pioPullUp:Konashi._createUUID("3001"),pioOutput:Konashi._createUUID("3002"),pioInputNotification:Konashi._createUUID("3003"),pwmConfig:Konashi._createUUID("3004"),pwmParameter:Konashi._createUUID("3005"),pwmDuty:Konashi._createUUID("3006"),analogDrive:Konashi._createUUID("3007"),analogInput:Konashi._createUUID("3008"),analogRead0:Konashi._createUUID("3008"),analogRead1:Konashi._createUUID("3009"),analogRead2:Konashi._createUUID("300a"),i2cConfig:Konashi._createUUID("300b"),i2cStartStop:Konashi._createUUID("300c"),i2cWrite:Konashi._createUUID("300d"),i2cReadParameter:Konashi._createUUID("300e"),i2cRead:Konashi._createUUID("300f"),uartConfig:Konashi._createUUID("3010"),uartBaudRate:Konashi._createUUID("3011"),uartTx:Konashi._createUUID("3012"),uartRxNotification:Konashi._createUUID("3013"),hardwareReset:Konashi._createUUID("3014"),hardwareLowBatteryNotification:Konashi._createUUID("3015")}}async connect(){this._gatt=await this._device.gatt.connect().catch(t=>this._throwError(t)),this._service=await this._gatt.getPrimaryService(Konashi._serviceUUID).catch(t=>this._throwError(t));for(const t in this._c12cUUIDs){const i=await this._service.getCharacteristic(this._c12cUUIDs[t]).catch(t=>this._throwError(t));this._c12c[t]=i}}disconnect(){this._gatt.disconnect()}get isConnected(){let t=!1;return this._gatt&&(t=this._gatt.connected),t}get deviceName(){return this._device.name}async pinMode(t,i){let a=(await this._c12c.pioSetting.readValue().catch(t=>this._throwError(t))).getUint8(0);i===Konashi.OUTPUT?a|=1<<t:a&=255&~(1<<t),await this._c12c.pioSetting.writeValue(new Uint8Array([a])).catch(t=>this._throwError(t))}async pinModeAll(t){t>=0&&t<=255&&await this._c12c.pioSetting.writeValue(new Uint8Array([t])).catch(t=>this._throwError(t))}async pinPullUp(t,i){let a=(await this._c12c.pioPullUp.readValue().catch(t=>this._throwError(t))).getUint8(0);i===Konashi.PULLUP?a|=1<<t:a&=255&~(1<<t),await this._c12c.pioPullUp.writeValue(new Uint8Array([a])).catch(t=>this._throwError(t))}async digitalWrite(t,i){i===Konashi.HIGH?this._pioOutputs|=1<<t:this._pioOutputs&=255&~(1<<t),await this._c12c.pioOutput.writeValue(new Uint8Array([this._pioOutputs])).catch(t=>this._throwError(t))}async digitalWriteAll(t){t>=0&&t<=255&&await this._c12c.pioOutput.writeValue(new Uint8Array([t])).catch(t=>this._throwError(t))}async pwmMode(t,i){let a=(await this._c12c.pwmConfig.readValue().catch(t=>this._throwError(t))).getUint8(0);i===Konashi.PWM_ENABLE||i===Konashi.PWM_ENABLE_LED_MODE?a|=1<<t:a&=255&~(1<<t),i===Konashi.PWM_ENABLE_LED_MODE?(await this._c12c.pwmConfig.writeValue(new Uint8Array([a])).catch(t=>this._throwError(t)),await this.pwmPeriod(t,Konashi.PWM_LED_PERIOD).catch(t=>this._throwError(t)),await this.pwmDuty(t,0).catch(t=>this._throwError(t))):await this._c12c.pwmConfig.writeValue(new Uint8Array([a])).catch(t=>this._throwError(t))}async pwmPeriod(t,i){const a=new Uint8Array([t,i>>24&255,i>>16&255,i>>8&255,i>>0&255]);await this._c12c.pwmParameter.writeValue(a).catch(t=>this._throwError(t))}async pwmDuty(t,i){const a=parseInt(i),e=new Uint8Array([t,a>>24&255,a>>16&255,a>>8&255,a>>0&255]);await this._c12c.pwmDuty.writeValue(e).catch(t=>this._throwError(t))}async pwmWrite(t,i){const a=Math.min(100,Math.max(0,i)),e=Konashi.PWM_LED_PERIOD*a/100;await this.pwmDuty(t,e).catch(t=>this._throwError(t))}async reset(){await this._c12c.hardwareReset.writeValue(new Uint8Array([1])).catch(t=>this._throwError(t))}async readBatteryLevel(){const t=await this._gatt.getPrimaryService("battery_service").catch(t=>this._throwError(t)),i=await t.getCharacteristic("battery_level").catch(t=>this._throwError(t));return(await i.readValue().catch(t=>this._throwError(t))).getUint8(0)}async readSignalStrength(){return 0}_throwError(t){throw console.log(t),t}}module.exports=CocoroKit,module.exports.default=CocoroKit;