From 955a2c97579e6a20337295a8fadfe7eb944e1108 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Tue, 2 Jan 2018 22:51:12 -0800 Subject: [PATCH] Better fix for null dereferencing issue. Fix initial Color sensor mode (default). --- libs/color-sensor/color.ts | 2 +- libs/core/input.ts | 2 +- sim/state/color.ts | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index c1c624c1..391f7c52 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -56,6 +56,7 @@ namespace sensors { constructor(port: number) { super(port) + this._setMode(ColorSensorMode.None); this.thresholdDetector = new sensors.internal.ThresholdDetector(this.id()); } @@ -94,7 +95,6 @@ namespace sensors { } _update(prev: number, curr: number) { - if (curr == null) return; if (this.mode == ColorSensorMode.Color) control.raiseEvent(this._id, this._colorEventValue(curr)); else diff --git a/libs/core/input.ts b/libs/core/input.ts index 896c8a3c..440dfbe8 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -68,7 +68,7 @@ namespace sensors.internal { if (info.sensor) return info.sensor._query() return 0 }, (prev, curr) => { - if (info.sensor) info.sensor._update(prev, curr) + if (info.sensor && curr !== null) info.sensor._update(prev, curr) }) } diff --git a/sim/state/color.ts b/sim/state/color.ts index ad5f7ac5..47392084 100644 --- a/sim/state/color.ts +++ b/sim/state/color.ts @@ -3,12 +3,13 @@ namespace pxsim { export enum ColorSensorMode { + None = -1, Reflected = 0, Ambient = 1, Colors = 2, RefRaw = 3, RgbRaw = 4, - ColorCal = 5 + ColorCal = 5, } export enum ThresholdState { @@ -24,6 +25,7 @@ namespace pxsim { constructor(port: number) { super(port); + this.mode = -1; } getDeviceType() {