import { Device } from "./device" ; import { IDeviceInterface } from "../interfaces" ; import * as Consts from "../consts" ; import { parseColor } from "../utils" ; class extends Device export class ColorDistanceSensor extends Device { constructor ( hub , portId ) { super ( hub , portId , ModeMap , Consts . DeviceType . COLOR_DISTANCE_SENSOR ) ; } public receive ( message ) { const mode = this . _mode ; switch ( mode ) { case Mode . COLOR : if ( message [ this . isWeDo2SmartHub ? 2 : 4 ] <= 10 ) { const color = parseColor ( message [ this . isWeDo2SmartHub ? 2 : 4 ] ) ; event type { object } param { Color } color this . notify ( "color" , { color } ) ; } break ; case Mode . DISTANCE : if ( this . isWeDo2SmartHub ) { break ; } if ( message [ 4 ] <= 10 ) { let distance = Math . floor ( message [ 4 ] * 25.4 ) ; if ( distance < 0 ) { distance = 0 ; } event type { object } param { number } distance this . notify ( "distance" , { distance } ) ; } break ; case Mode . COLOR_AND_DISTANCE : if ( this . isWeDo2SmartHub ) { break ; } let distance = message [ 5 ] ; const partial = message [ 7 ] ; if ( partial > 0 ) { distance += 1.0 / partial ; } distance = Math . floor ( distance * 25.4 ) - 20 ; event type { object } param { Color } color param { number } distance if ( message [ 4 ] <= 10 ) { const color = message [ 4 ] ; this . notify ( "colorAndDistance" , { color , distance } ) ; } break ; } } method param { number } channel returns { Promise } public setPFExtendedChannel ( channel ) { let address = 0 ; if ( channel >= 4 ) { channel -= 4 ; address = 1 ; } const message = Buffer . alloc ( 2 ) ; message [ 0 ] = ( ( channel - 1 ) << 4 ) + ( address << 3 ) ; message [ 1 ] = 6 << 4 ; return this . sendPFIRMessage ( message ) ; } method param { number } channel param { string } output param { number } power returns { Promise } public setPFPower ( channel , output , power ) { let address = 0 ; if ( channel > 4 ) { channel -= 4 ; address = 1 ; } const message = Buffer . alloc ( 2 ) ; message [ 0 ] = ( ( channel - 1 ) << 4 ) + ( address << 3 ) + ( output === "RED" ? 4 : 5 ) ; message [ 1 ] = this . _pfPowerToPWM ( power ) << 4 ; return this . sendPFIRMessage ( message ) ; } method param { Buffer } channel param { Buffer } powerA param { Buffer } powerB returns { Promise } public startPFMotors ( channel , powerBlue , powerRed ) { let address = 0 ; if ( channel > 4 ) { channel -= 4 ; address = 1 ; } const message = Buffer . alloc ( 2 ) ; message [ 0 ] = ( ( ( channel - 1 ) + 4 + ( address << 3 ) ) << 4 ) + this . _pfPowerToPWM ( powerBlue ) ; message [ 1 ] = this . _pfPowerToPWM ( powerRed ) << 4 ; return this . sendPFIRMessage ( message ) ; } method param { Buffer } message returns { Promise } public sendPFIRMessage ( message ) { if ( this . isWeDo2SmartHub ) { throw new Error ( "Power Functions IR is not available on the WeDo 2.0 Smart Hub" ) ; } else { const payload = Buffer . alloc ( 2 ) ; payload [ 0 ] = ( message [ 0 ] << 4 ) + ( message [ 1 ] >> 4 ) ; payload [ 1 ] = message [ 0 ] >> 4 ; this . subscribe ( Mode . PF_IR ) ; return this . writeDirect ( 0x07 , payload ) ; } } method param { Color } color returns { Promise } public setColor ( color ) { return new Promise < void > ( ( resolve ) => { if ( color === false ) { color = 0 ; } if ( this . isWeDo2SmartHub ) { throw new Error ( "Setting LED color is not available on the WeDo 2.0 Smart Hub" ) ; } else { this . subscribe ( Mode . LED ) ; this . writeDirect ( 0x05 , Buffer . from ( [ color as number ] ) ) ; } return resolve ( ) ; } ) ; } private _pfPowerToPWM ( power ) { return power & 15 ; } } export enum Mode { COLOR = 0x00 , DISTANCE = 0x01 , LED = 0x05 , PF_IR = 0x07 , COLOR_AND_DISTANCE = 0x08 } export const ModeMap = { "color" : Mode . COLOR , "distance" : Mode . DISTANCE , "colorAndDistance" : Mode . COLOR_AND_DISTANCE } ; export enum Output { RED = "RED" , BLUE = "BLUE" }