import { BasicMotor } from "./basicmotor" ; import { IDeviceInterface } from "../interfaces" ; import * as Consts from "../consts" ; import { mapSpeed } from "../utils" ; class extends BasicMotor export class TachoMotor extends BasicMotor { protected _brakeStyle : Consts . BrakingStyle = Consts . BrakingStyle . BRAKE ; protected _maxPower : number = 100 ; public useAccelerationProfile : boolean = true ; public useDecelerationProfile : boolean = true ; constructor ( hub , portId , modeMap = { } , type = Consts . DeviceType . UNKNOWN ) { super ( hub , portId , Object . assign ( { } , modeMap , ModeMap ) , type ) ; } public receive ( message ) { const mode = this . _mode ; switch ( mode ) { case Mode . ROTATION : const degrees = message . readInt32LE ( this . isWeDo2SmartHub ? 2 : 4 ) ; event type { object } param { number } rotation this . notify ( "rotate" , { degrees } ) ; break ; } } method param { number } style public setBrakingStyle ( style ) { this . _brakeStyle = style ; } method param { number } style public setMaxPower ( maxPower ) { this . _maxPower = maxPower ; } method param { number } time returns { Promise } public setAccelerationTime ( time , profile = 0x00 ) { const message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x05 , 0x00 , 0x00 , profile ] ) ; message . writeUInt16LE ( time , 4 ) ; this . send ( message ) ; } method param { number } time returns { Promise } public setDecelerationTime ( time , profile = 0x00 ) { const message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x06 , 0x00 , 0x00 , profile ] ) ; message . writeUInt16LE ( time , 4 ) ; this . send ( message ) ; } method param { number } speed param { number } time returns { Promise } public setSpeed ( speed , time ) { if ( ! this . isVirtualPort && speed instanceof Array ) { throw new Error ( "Only virtual ports can accept multiple speeds" ) ; } if ( this . isWeDo2SmartHub ) { throw new Error ( "Motor speed is not available on the WeDo 2.0 Smart Hub" ) ; } this . cancelEventTimer ( ) ; return new Promise < void > ( ( resolve ) => { if ( speed === undefined || speed === null ) { speed = 100 ; } let message ; if ( time !== undefined ) { if ( speed instanceof Array ) { message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x0a , 0x00 , 0x00 , mapSpeed ( speed [ 0 ] ) , mapSpeed ( speed [ 1 ] ) , this . _maxPower , this . _brakeStyle , this . useProfile ( ) ] ) ; } else { message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x09 , 0x00 , 0x00 , mapSpeed ( speed ) , this . _maxPower , this . _brakeStyle , this . useProfile ( ) ] ) ; } message . writeUInt16LE ( time , 4 ) ; } else { if ( speed instanceof Array ) { message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x08 , mapSpeed ( speed [ 0 ] ) , mapSpeed ( speed [ 1 ] ) , this . _maxPower , this . useProfile ( ) ] ) ; } else { message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x07 , mapSpeed ( speed ) , this . _maxPower , this . useProfile ( ) ] ) ; } } this . send ( message ) ; this . _finishedCallbacks . push ( ( ) => { return resolve ( ) ; } ) ; } ) ; } method param { number } degrees param { number } speed returns { Promise } public rotateByDegrees ( degrees , speed ) { if ( ! this . isVirtualPort && speed instanceof Array ) { throw new Error ( "Only virtual ports can accept multiple speeds" ) ; } if ( this . isWeDo2SmartHub ) { throw new Error ( "Rotation is not available on the WeDo 2.0 Smart Hub" ) ; } this . cancelEventTimer ( ) ; return new Promise < void > ( ( resolve ) => { if ( speed === undefined || speed === null ) { speed = 100 ; } let message ; if ( speed instanceof Array ) { message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x0c , 0x00 , 0x00 , 0x00 , 0x00 , mapSpeed ( speed [ 0 ] ) , mapSpeed ( speed [ 1 ] ) , this . _maxPower , this . _brakeStyle , this . useProfile ( ) ] ) ; } else { message = Buffer . from ( [ 0x81 , this . portId , 0x11 , 0x0b , 0x00 , 0x00 , 0x00 , 0x00 , mapSpeed ( speed ) , this . _maxPower , this . _brakeStyle , this . useProfile ( ) ] ) ; } message . writeUInt32LE ( degrees , 4 ) ; this . send ( message ) ; this . _finishedCallbacks . push ( ( ) => { return resolve ( ) ; } ) ; } ) ; } protected useProfile ( ) { let value = 0x00 ; if ( this . useAccelerationProfile ) { value += 0x01 ; } if ( this . useDecelerationProfile ) { value += 0x02 ; } return value ; } } export enum Mode { ROTATION = 0x02 } export const ModeMap = { "rotate" : Mode . ROTATION } ;