From 2c72173bfef9d6cf3eaac3ea19e12908f692468c Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Wed, 25 Oct 2017 13:34:05 +0200 Subject: [PATCH] Use the _query() infrastructure for polling --- libs/core/color.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/libs/core/color.ts b/libs/core/color.ts index 84706d2f..27303f51 100644 --- a/libs/core/color.ts +++ b/libs/core/color.ts @@ -31,11 +31,8 @@ namespace input { //% fixedInstances export class ColorSensor extends internal.UartSensor { - polling: boolean; - constructor(port: number) { super(port) - this.polling = false; } _deviceType() { @@ -46,15 +43,14 @@ namespace input { this._setMode(m) } - _initPolling() { - if (!this.polling) { - input.internal.unsafePollForChanges( - 50, - () => this.color(), - (prev, curr) => { - control.raiseEvent(this._id, curr); - }) - } + _query() { + if (this.mode == ColorSensorMode.Color) + return this.getNumber(NumberFormat.UInt8LE, 0) + return 0 + } + + _update(prev: number, curr: number) { + control.raiseEvent(this._id, curr); } /** @@ -70,8 +66,10 @@ namespace input { //% weight=100 blockGap=8 //% group="Color Sensor" onColorDetected(color: ColorSensorColor, handler: () => void) { - this._initPolling(); control.onEvent(this._id, color, handler); + this.setMode(ColorSensorMode.Color) + if (this.color() == color) + control.runInBackground(handler) } /**