diff --git a/libs/nxt-temperature-sensor/temperature.ts b/libs/nxt-temperature-sensor/temperature.ts index 11660ced..ae95ac0a 100644 --- a/libs/nxt-temperature-sensor/temperature.ts +++ b/libs/nxt-temperature-sensor/temperature.ts @@ -7,6 +7,8 @@ const enum NXTTempSensorMode { namespace sensors { + const MODE_SWITCH_DELAY = 100; + //% fixedInstances export class NXTTemperatureSensor extends internal.IICSensor { @@ -22,9 +24,35 @@ namespace sensors { return DAL.DEVICE_TYPE_NXT_TEMPERATURE; } + setMode(m: NXTTempSensorMode) { + this._setMode(m); + } + + _setMode(m: number) { + let v = m | 0; + this.mode = v; + if (!this.isActive()) return; + if (this.realmode != this.mode) { + this.realmode = v; + if (m == NXTTempSensorMode.Celsius) { + this.transaction(1, [76, NXTTempSensorMode.Celsius], 0); + } else if (m == NXTTempSensorMode.Fahrenheit) { + this.transaction(1, [76, NXTTempSensorMode.Fahrenheit], 0); + } + pause(MODE_SWITCH_DELAY); + } + } + + /** + * Gets the current temp mode + */ + tempMode() { + return this.mode; + } + _IICId() { - // return 'LEGO'; - return 'LEGOTemp.'; + // return 'LEGO'; + return 'LEGOTemp.'; } _query() { @@ -47,17 +75,6 @@ namespace sensors { } - setMode(m: number) { - this._setMode(m); - } - - /** - * Gets the current temp mode - */ - tempMode() { - return this.mode; - } - /** * Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest). For raw reflection values, the range can be from 0 to 4095. * @param sensor the color sensor port