Use the _query() infrastructure for polling

This commit is contained in:
Michal Moskal 2017-10-25 13:34:05 +02:00
parent 1a5992408b
commit 2c72173bfe

View File

@ -31,11 +31,8 @@ namespace input {
//% fixedInstances //% fixedInstances
export class ColorSensor extends internal.UartSensor { export class ColorSensor extends internal.UartSensor {
polling: boolean;
constructor(port: number) { constructor(port: number) {
super(port) super(port)
this.polling = false;
} }
_deviceType() { _deviceType() {
@ -46,15 +43,14 @@ namespace input {
this._setMode(m) this._setMode(m)
} }
_initPolling() { _query() {
if (!this.polling) { if (this.mode == ColorSensorMode.Color)
input.internal.unsafePollForChanges( return this.getNumber(NumberFormat.UInt8LE, 0)
50, return 0
() => this.color(), }
(prev, curr) => {
control.raiseEvent(this._id, curr); _update(prev: number, curr: number) {
}) control.raiseEvent(this._id, curr);
}
} }
/** /**
@ -70,8 +66,10 @@ namespace input {
//% weight=100 blockGap=8 //% weight=100 blockGap=8
//% group="Color Sensor" //% group="Color Sensor"
onColorDetected(color: ColorSensorColor, handler: () => void) { onColorDetected(color: ColorSensorColor, handler: () => void) {
this._initPolling();
control.onEvent(this._id, <number>color, handler); control.onEvent(this._id, <number>color, handler);
this.setMode(ColorSensorMode.Color)
if (this.color() == color)
control.runInBackground(handler)
} }
/** /**