Add ultrasonic and gyro (untested)

This commit is contained in:
Michal Moskal 2017-07-10 15:07:23 +01:00
parent babdf45fc3
commit d3263d8456
5 changed files with 60 additions and 1 deletions

View File

@ -24,7 +24,6 @@ namespace input {
export class ColorSensor extends internal.UartSensor { export class ColorSensor extends internal.UartSensor {
constructor() { constructor() {
super() super()
this.mode = ColorSensorMode.Reflect
} }
_deviceType() { _deviceType() {

View File

@ -29,6 +29,8 @@ const enum LMS {
DEVICE_TYPE_NEWTACHO = 9, DEVICE_TYPE_NEWTACHO = 9,
DEVICE_TYPE_TOUCH = 16, DEVICE_TYPE_TOUCH = 16,
DEVICE_TYPE_COLOR = 29, DEVICE_TYPE_COLOR = 29,
DEVICE_TYPE_ULTRASONIC = 30,
DEVICE_TYPE_GYRO = 32,
DEVICE_TYPE_IR = 33, DEVICE_TYPE_IR = 33,
DEVICE_TYPE_THIRD_PARTY_START = 50, DEVICE_TYPE_THIRD_PARTY_START = 50,
DEVICE_TYPE_THIRD_PARTY_END = 99, DEVICE_TYPE_THIRD_PARTY_END = 99,

34
libs/core/gyro.ts Normal file
View File

@ -0,0 +1,34 @@
const enum GyroSensorMode {
None = -1,
Angle = 0,
Rate = 1,
}
namespace input {
export class GyroSensor extends internal.UartSensor {
constructor() {
super()
}
_deviceType() {
return LMS.DEVICE_TYPE_GYRO
}
setMode(m: GyroSensorMode) {
this._setMode(m)
}
getAngle() {
this.setMode(GyroSensorMode.Angle)
return this.getNumber(NumberFormat.Int16LE, 0)
}
getRate() {
this.setMode(GyroSensorMode.Rate)
return this.getNumber(NumberFormat.Int16LE, 0)
}
}
//% whenUsed
export const gyro: GyroSensor = new GyroSensor()
}

View File

@ -18,6 +18,8 @@
"input.ts", "input.ts",
"ir.ts", "ir.ts",
"color.ts", "color.ts",
"gyro.ts",
"ultrasonic.ts",
"touch.ts", "touch.ts",
"shims.d.ts", "shims.d.ts",
"enums.d.ts", "enums.d.ts",

22
libs/core/ultrasonic.ts Normal file
View File

@ -0,0 +1,22 @@
namespace input {
export class UltraSonicSensor extends internal.UartSensor {
constructor() {
super()
}
_deviceType() {
return LMS.DEVICE_TYPE_ULTRASONIC
}
/** Get distance in mm */
getDistance() {
// it supposedly also has an inch mode, but we stick to mm
this._setMode(0)
return this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff
}
}
//% whenUsed
export const ultrasonic: UltraSonicSensor = new UltraSonicSensor()
}