From 9397641cec2a8ee8ce77607c42943bbc95887406 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Wed, 7 Jun 2023 23:54:55 +0300 Subject: [PATCH 01/53] add-subcategories Add NXT Sensors subcategorie --- libs/ev3/ns.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/ev3/ns.ts b/libs/ev3/ns.ts index 89e02bc1f..7b8000077 100644 --- a/libs/ev3/ns.ts +++ b/libs/ev3/ns.ts @@ -9,6 +9,7 @@ namespace brick { //% color="#C8509B" weight=95 icon="\uf10f" //% labelLineWidth=100 //% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Calibration"]' +//% subcategories='["NXT Sensors"]' namespace sensors { } From 8596741689a01a6d3777e684bb8804376488073e Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Tue, 11 Jul 2023 22:42:33 +0300 Subject: [PATCH 02/53] first-support-version The light sensor gives real values. Mode change not supported... --- libs/core/dal.d.ts | 2 + libs/core/ev3const.h | 2 + libs/core/input.ts | 136 ++++++++++++++++---------------- libs/ev3/console.ts | 5 +- libs/ev3/ns.ts | 2 +- libs/ev3/pxt.json | 3 +- libs/nxt-light-sensor/README.md | 3 + libs/nxt-light-sensor/light.ts | 125 +++++++++++++++++++++++++++++ libs/nxt-light-sensor/pxt.json | 15 ++++ libs/nxt-light-sensor/test.ts | 0 libs/screen/targetoverrides.ts | 7 ++ pxtarget.json | 1 + 12 files changed, 228 insertions(+), 73 deletions(-) create mode 100644 libs/nxt-light-sensor/README.md create mode 100644 libs/nxt-light-sensor/light.ts create mode 100644 libs/nxt-light-sensor/pxt.json create mode 100644 libs/nxt-light-sensor/test.ts diff --git a/libs/core/dal.d.ts b/libs/core/dal.d.ts index 4507f801b..837de14d0 100644 --- a/libs/core/dal.d.ts +++ b/libs/core/dal.d.ts @@ -309,6 +309,8 @@ declare const enum DAL { DEVICE_TYPE_NXT_LIGHT = 2, DEVICE_TYPE_NXT_SOUND = 3, DEVICE_TYPE_NXT_COLOR = 4, + DEVICE_TYPE_NXT_ULTRASONIC = 5, + DEVICE_TYPE_NXT_TEMPERATURE = 6, DEVICE_TYPE_TACHO = 7, DEVICE_TYPE_MINITACHO = 8, DEVICE_TYPE_NEWTACHO = 9, diff --git a/libs/core/ev3const.h b/libs/core/ev3const.h index 6c222ff34..53881bf6b 100644 --- a/libs/core/ev3const.h +++ b/libs/core/ev3const.h @@ -11,6 +11,8 @@ #define DEVICE_TYPE_NXT_LIGHT 2 #define DEVICE_TYPE_NXT_SOUND 3 #define DEVICE_TYPE_NXT_COLOR 4 +#define DEVICE_TYPE_NXT_ULTRASONIC 5 +#define DEVICE_TYPE_NXT_TEMPERATURE 6 #define DEVICE_TYPE_TACHO 7 #define DEVICE_TYPE_MINITACHO 8 #define DEVICE_TYPE_NEWTACHO 9 diff --git a/libs/core/input.ts b/libs/core/input.ts index 9dc18e4d1..1d4717f96 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -220,42 +220,7 @@ namespace sensors.internal { const CinCnt = batteryInfo.CinCnt; const CoutCnt = batteryInfo.CoutCnt; const VinCnt = batteryInfo.VinCnt; - /* -void cUiUpdatePower(void) -{ -#ifndef Linux_X86 - DATAF CinV; - DATAF CoutV; - - if ((UiInstance.Hw == FINAL) || (UiInstance.Hw == FINALB)) - { - CinV = CNT_V(UiInstance.CinCnt) / AMP_CIN; - UiInstance.Vbatt = (CNT_V(UiInstance.VinCnt) / AMP_VIN) + CinV + VCE; - - UiInstance.Ibatt = CinV / SHUNT_IN; - CoutV = CNT_V(UiInstance.CoutCnt) / AMP_COUT; - UiInstance.Imotor = CoutV / SHUNT_OUT; - - } - else - { - CinV = CNT_V(UiInstance.CinCnt) / EP2_AMP_CIN; - UiInstance.Vbatt = (CNT_V(UiInstance.VinCnt) / AMP_VIN) + CinV + VCE; - - UiInstance.Ibatt = CinV / EP2_SHUNT_IN; - UiInstance.Imotor = 0; - - } - -#endif -#ifdef DEBUG_TEMP_SHUTDOWN - - UiInstance.Vbatt = 7.0; - UiInstance.Ibatt = 5.0; - -#endif -} - */ + const CinV = CNT_V(CinCnt) / AMP_CIN; const Vbatt = CNT_V(VinCnt) / AMP_VIN + CinV + VCE; const Ibatt = CinV / SHUNT_IN; @@ -283,13 +248,14 @@ void cUiUpdatePower(void) let nonActivated = 0; function detectDevices() { - control.dmesg(`detect devices (hash ${hashDevices()})`) - const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS) + control.dmesg(`detect devices (hash ${hashDevices()})`); + const inDcm = analogMM.slice(AnalogOff.InDcm, DAL.NUM_INPUTS); + const inConn = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS); let numChanged = 0; const uartSensors: SensorInfo[] = []; for (const sensorInfo of sensorInfos) { - const newConn = conns[sensorInfo.port] + const newConn = inConn[sensorInfo.port] if (newConn == sensorInfo.connType && sensorInfo.sensor && sensorInfo.sensor.isActive()) { @@ -297,26 +263,30 @@ void cUiUpdatePower(void) uartSensors.push(sensorInfo); continue; } - numChanged++ - sensorInfo.connType = newConn - sensorInfo.devType = DAL.DEVICE_TYPE_NONE + numChanged++; + sensorInfo.connType = newConn; + sensorInfo.devType = DAL.DEVICE_TYPE_NONE; if (newConn == DAL.CONN_INPUT_UART) { - control.dmesg(`new UART connection at ${sensorInfo.port}`) + control.dmesg(`new UART connection at ${sensorInfo.port}`); updateUartMode(sensorInfo.port, 0); uartSensors.push(sensorInfo); } else if (newConn == DAL.CONN_NXT_IIC) { - control.dmesg(`new IIC connection at ${sensorInfo.port}`) - sensorInfo.devType = DAL.DEVICE_TYPE_IIC_UNKNOWN - sensorInfo.iicid = readIICID(sensorInfo.port) - control.dmesg(`IIC ID ${sensorInfo.iicid.length}`) + control.dmesg(`new IIC connection at ${sensorInfo.port}`); + sensorInfo.devType = DAL.DEVICE_TYPE_IIC_UNKNOWN; + sensorInfo.iicid = readIICID(sensorInfo.port); + control.dmesg(`IIC ID ${sensorInfo.iicid.length}`); + } else if (newConn == DAL.CONN_NXT_DUMB) { + control.dmesg(`new NXT analog connection at ${sensorInfo.port}`); + sensorInfo.devType = inDcm[sensorInfo.port]; + control.dmesg(`NXT analog dev type ${sensorInfo.devType}`); } else if (newConn == DAL.CONN_INPUT_DUMB) { - control.dmesg(`new DUMB connection at ${sensorInfo.port}`) + control.dmesg(`new DUMB connection at ${sensorInfo.port}`); // TODO? for now assume touch - sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH + sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH; } else if (newConn == DAL.CONN_NONE || newConn == 0) { //control.dmesg(`disconnect at port ${sensorInfo.port}`) } else { - control.dmesg(`unknown connection type: ${newConn} at ${sensorInfo.port}`) + control.dmesg(`unknown connection type: ${newConn} at ${sensorInfo.port}`); } } @@ -417,13 +387,35 @@ void cUiUpdatePower(void) } export class AnalogSensor extends Sensor { + protected mode: number // the mode user asked for + protected realmode: number // the mode the hardware is in + constructor(port: number) { - super(port) + super(port); + this.mode = 0; + this.realmode = 0; + } + + protected _setMode(m: number) { + //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`); + let v = m | 0; + this.mode = v; + if (!this.isActive()) return; + if (this.realmode != this.mode) { + control.dmesg(`_setMode p=${this._port} m: ${this.realmode} -> ${v}`); + this.realmode = v; + setUartMode(this._port, v); + } + } + + _readPin1() { + if (!this.isActive()) return 0; + return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin1 + 2 * this._port); } _readPin6() { - if (!this.isActive()) return 0 - return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this._port) + if (!this.isActive()) return 0; + return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this._port); } } @@ -432,24 +424,25 @@ void cUiUpdatePower(void) protected realmode: number // the mode the hardware is in constructor(port: number) { - super(port) - this.mode = 0 - this.realmode = 0 + super(port); + this.mode = 0; + this.realmode = 0; } _activated() { - this.realmode = 0 - this._setMode(this.mode) + this.realmode = 0; + this._setMode(this.mode); } protected _setMode(m: number) { - //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`) - let v = m | 0 - this.mode = v - if (!this.isActive()) return + //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`); + let v = m | 0; + this.mode = v; + if (!this.isActive()) return; if (this.realmode != this.mode) { - this.realmode = v - setUartMode(this._port, v) + control.dmesg(`_setMode p=${this._port} m: ${this.realmode} -> ${v}`); + this.realmode = v; + setUartMode(this._port, v); } } @@ -516,7 +509,10 @@ void cUiUpdatePower(void) } } - export const iicsensor = new IICSensor(3) + export const i2cSensor1 = new IICSensor(1); + export const i2cSensor2 = new IICSensor(2); + export const i2cSensor3 = new IICSensor(3); + export const i2cSensor4 = new IICSensor(4); function uartReset(port: number) { if (port < 0) return @@ -588,8 +584,9 @@ void cUiUpdatePower(void) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode) } - const UART_PORT_CHANGED = 1 - const UART_DATA_READY = 8 + const UART_PORT_CHANGED = 1; + const UART_DATA_READY = 8; + function setUartMode(port: number, mode: number) { while (true) { if (port < 0) return @@ -617,15 +614,16 @@ void cUiUpdatePower(void) DAL.MAX_DEVICE_DATALENGTH) } - function getUartNumber(fmt: NumberFormat, off: number, port: number) { + function getUartNumber(fmt: NumberFormat, off: number, port: number): number { if (port < 0) return 0 - let index = uartMM.getNumber(NumberFormat.UInt16LE, UartOff.Actual + port * 2) + const index = uartMM.getNumber(NumberFormat.UInt16LE, UartOff.Actual + port * 2) return uartMM.getNumber(fmt, UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index + off) } export function setIICMode(port: number, type: number, mode: number) { if (port < 0) return; + control.dmesg(`iic set type ${type} mode ${mode} at ${port}`); devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, DAL.CONN_NXT_IIC) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, type) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode) diff --git a/libs/ev3/console.ts b/libs/ev3/console.ts index 8c95a185f..aa6be16fe 100644 --- a/libs/ev3/console.ts +++ b/libs/ev3/console.ts @@ -19,8 +19,9 @@ namespace console._screen { if (!lines) { lines = []; console.addListener(log); - brick.buttonUp.onEvent(ButtonEvent.Bumped, () => scroll(-3)) - brick.buttonDown.onEvent(ButtonEvent.Bumped, () => scroll(3)) + brick.buttonUp.onEvent(ButtonEvent.Bumped, () => scroll(-3)); + brick.buttonDown.onEvent(ButtonEvent.Bumped, () => scroll(3)); + brick.showConsole(); } } diff --git a/libs/ev3/ns.ts b/libs/ev3/ns.ts index 7b8000077..53e9b08d2 100644 --- a/libs/ev3/ns.ts +++ b/libs/ev3/ns.ts @@ -8,7 +8,7 @@ namespace brick { //% color="#C8509B" weight=95 icon="\uf10f" //% labelLineWidth=100 -//% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Calibration"]' +//% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Calibration", "Light Sensor"]' //% subcategories='["NXT Sensors"]' namespace sensors { } diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index a54af080a..115501897 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -24,7 +24,8 @@ "touch-sensor": "file:../touch-sensor", "ultrasonic-sensor": "file:../ultrasonic-sensor", "gyro-sensor": "file:../gyro-sensor", - "infrared-sensor": "file:../infrared-sensor" + "infrared-sensor": "file:../infrared-sensor", + "nxt-light-sensor": "file:../nxt-light-sensor" }, "palette": [ "#ffffff", diff --git a/libs/nxt-light-sensor/README.md b/libs/nxt-light-sensor/README.md new file mode 100644 index 000000000..d0b65957c --- /dev/null +++ b/libs/nxt-light-sensor/README.md @@ -0,0 +1,3 @@ +# NXT Light sensor + +The library to interact with the NXT Light Sensor. \ No newline at end of file diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts new file mode 100644 index 000000000..f15735c3c --- /dev/null +++ b/libs/nxt-light-sensor/light.ts @@ -0,0 +1,125 @@ +const enum NXTLightSensorMode { + //% block="reflected light (raw)" + ReflectedLightRaw = 0, + //% block="reflected light" + ReflectedLight = 1, + //% block="ambient light" + AmbientLight = 2 +} + +enum Light { + //% block="dark" + High = sensors.ThresholdState.Low, + //% block="bright" + Low = sensors.ThresholdState.High +} + +namespace sensors { + + //% fixedInstances + export class NXTLightSensor extends internal.AnalogSensor { + + constructor(port: number) { + super(port); + } + + _query() { + return this._readPin1(); + } + + _info(): string { + return this._query().toString(); + } + + _update(prev: number, curr: number) { + return this._readPin1(); + } + + _deviceType() { + return DAL.DEVICE_TYPE_NXT_LIGHT; + } + + setMode(m: number) { + this._setMode(m); + } + + /** + * Gets the current light mode + */ + lightMode() { + 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 4096. + * @param sensor the color sensor port + */ + //% help=sensors/nxt-light-sensor/light + //% block="**nxt light sensor** %this|%mode" + //% blockId=nxtLight + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% this.fieldEditor="ports" + //% weight=99 blockGap=8 + //% subcategory="NXT Sensors" + //% group="Light Sensor" + light(mode: NXTLightSensorMode) { + //this.setMode(mode); + this.poke(); + switch (mode) { + case NXTLightSensorMode.ReflectedLightRaw: + return this.reflectetLightRaw(); + case NXTLightSensorMode.ReflectedLight: + return this.reflectetLight(); + case NXTLightSensorMode.AmbientLight: + return this.ambientLight(); + default: + return 0; + } + } + + /** + * Gets the raw light value. + */ + //% + readValue() { + return this._readPin1(); + } + + /** + * Gets the raw reflection light value. + */ + //% + reflectetLightRaw() { + this.poke(); + return this.readValue(); + } + + /** + * Gets the reflection light value. + */ + //% + reflectetLight() { + this.poke(); + return this.readValue(); + } + + /** + * Gets the ambient light value. + */ + //% + ambientLight() { + this.poke(); + return this.readValue(); + } + } + + //% whenUsed block="2" weight=95 fixedInstance jres=icons.port2 + export const nxtLight2: NXTLightSensor = new NXTLightSensor(2) + //% whenUsed block="1" weight=90 fixedInstance jres=icons.port1 + export const nxtLight1: NXTLightSensor = new NXTLightSensor(1) + //% whenUsed block="3" weight=90 fixedInstance jres=icons.port3 + export const nxtLight3: NXTLightSensor = new NXTLightSensor(3) + //% whenUsed block="4" weight=90 fixedInstance jres=icons.port4 + export const nxtLight4: NXTLightSensor = new NXTLightSensor(4) +} diff --git a/libs/nxt-light-sensor/pxt.json b/libs/nxt-light-sensor/pxt.json new file mode 100644 index 000000000..ab363255c --- /dev/null +++ b/libs/nxt-light-sensor/pxt.json @@ -0,0 +1,15 @@ +{ + "name": "nxt-light-sensor", + "description": "NXT Light Sensor support", + "files": [ + "README.md", + "light.ts" + ], + "testFiles": [ + "test.ts" + ], + "public": true, + "dependencies": { + "core": "file:../core" + } +} \ No newline at end of file diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts new file mode 100644 index 000000000..e69de29bb diff --git a/libs/screen/targetoverrides.ts b/libs/screen/targetoverrides.ts index 0d0005a91..61ff35063 100644 --- a/libs/screen/targetoverrides.ts +++ b/libs/screen/targetoverrides.ts @@ -28,6 +28,7 @@ namespace brick { ShowLines, Image, Ports, + Console, Custom } @@ -298,6 +299,12 @@ namespace brick { }) } + export function showConsole() { + console.sendToScreen(); + screenMode = ScreenMode.Console; + clearScreen(); + } + /** * An image * @param image the image diff --git a/pxtarget.json b/pxtarget.json index 4ffb81191..589c8266c 100644 --- a/pxtarget.json +++ b/pxtarget.json @@ -15,6 +15,7 @@ "libs/ultrasonic-sensor", "libs/infrared-sensor", "libs/gyro-sensor", + "libs/nxt-light-sensor", "libs/screen", "libs/ev3", "libs/storage", From 61cb6cc56c8897332fda29720ed17d1af00626c8 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 13 Jul 2023 12:17:46 +0300 Subject: [PATCH 03/53] update --- libs/core/input.ts | 5 ++--- libs/nxt-light-sensor/light.ts | 24 ++++++++++++------------ libs/screen/targetoverrides.ts | 4 ++-- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index 1d4717f96..577d100d3 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -255,7 +255,7 @@ namespace sensors.internal { const uartSensors: SensorInfo[] = []; for (const sensorInfo of sensorInfos) { - const newConn = inConn[sensorInfo.port] + const newConn = inConn[sensorInfo.port]; if (newConn == sensorInfo.connType && sensorInfo.sensor && sensorInfo.sensor.isActive()) { @@ -397,14 +397,13 @@ namespace sensors.internal { } protected _setMode(m: number) { - //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`); let v = m | 0; this.mode = v; if (!this.isActive()) return; if (this.realmode != this.mode) { control.dmesg(`_setMode p=${this._port} m: ${this.realmode} -> ${v}`); this.realmode = v; - setUartMode(this._port, v); + // setAnalogMode() } } diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index f15735c3c..62e708631 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -1,10 +1,15 @@ const enum NXTLightSensorMode { - //% block="reflected light (raw)" - ReflectedLightRaw = 0, //% block="reflected light" - ReflectedLight = 1, + ReflectedLight = 0, //% block="ambient light" - AmbientLight = 2 + AmbientLight = 1 +} + +enum ReflectedLightMode { + //% block="reflected light" + Reflected = 0, + //% block="reflected light (raw)" + ReflectedRaw = 1 } enum Light { @@ -24,7 +29,7 @@ namespace sensors { } _query() { - return this._readPin1(); + return this.readValue(); } _info(): string { @@ -32,7 +37,7 @@ namespace sensors { } _update(prev: number, curr: number) { - return this._readPin1(); + return this.readValue(); } _deviceType() { @@ -67,10 +72,8 @@ namespace sensors { //this.setMode(mode); this.poke(); switch (mode) { - case NXTLightSensorMode.ReflectedLightRaw: - return this.reflectetLightRaw(); case NXTLightSensorMode.ReflectedLight: - return this.reflectetLight(); + return this.reflectetLightRaw(); case NXTLightSensorMode.AmbientLight: return this.ambientLight(); default: @@ -91,7 +94,6 @@ namespace sensors { */ //% reflectetLightRaw() { - this.poke(); return this.readValue(); } @@ -100,7 +102,6 @@ namespace sensors { */ //% reflectetLight() { - this.poke(); return this.readValue(); } @@ -109,7 +110,6 @@ namespace sensors { */ //% ambientLight() { - this.poke(); return this.readValue(); } } diff --git a/libs/screen/targetoverrides.ts b/libs/screen/targetoverrides.ts index 61ff35063..fd17d257e 100644 --- a/libs/screen/targetoverrides.ts +++ b/libs/screen/targetoverrides.ts @@ -300,9 +300,9 @@ namespace brick { } export function showConsole() { - console.sendToScreen(); + //console.sendToScreen(); screenMode = ScreenMode.Console; - clearScreen(); + //clearScreen(); } /** From 5753d386ef259a0484eca53035c55aef9807835d Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Tue, 26 Sep 2023 14:10:56 +0300 Subject: [PATCH 04/53] Update ns.ts --- libs/ev3/ns.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/libs/ev3/ns.ts b/libs/ev3/ns.ts index 53e9b08d2..93550c7bf 100644 --- a/libs/ev3/ns.ts +++ b/libs/ev3/ns.ts @@ -9,7 +9,6 @@ namespace brick { //% color="#C8509B" weight=95 icon="\uf10f" //% labelLineWidth=100 //% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Calibration", "Light Sensor"]' -//% subcategories='["NXT Sensors"]' namespace sensors { } From 6414efe4a80723927260654cbe94a0286d43b6d9 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Tue, 17 Oct 2023 21:27:05 +0300 Subject: [PATCH 05/53] sens-sim-support --- libs/nxt-light-sensor/light.ts | 37 ++++----- sim/dalboard.ts | 1 + sim/state/nodeTypes.ts | 3 +- sim/state/nxtlight.ts | 45 ++++++++++ sim/visuals/board.ts | 7 ++ sim/visuals/controls/lightWheel.ts | 105 ++++++++++++++++++++++++ sim/visuals/nodes/nxtLightSensorView.ts | 45 ++++++++++ 7 files changed, 220 insertions(+), 23 deletions(-) create mode 100644 sim/state/nxtlight.ts create mode 100644 sim/visuals/controls/lightWheel.ts create mode 100644 sim/visuals/nodes/nxtLightSensorView.ts diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 62e708631..f86637409 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -1,22 +1,10 @@ const enum NXTLightSensorMode { + //% block="reflected light (raw)" + ReflectedLightRaw = 0, //% block="reflected light" - ReflectedLight = 0, + ReflectedLight = 1, //% block="ambient light" - AmbientLight = 1 -} - -enum ReflectedLightMode { - //% block="reflected light" - Reflected = 0, - //% block="reflected light (raw)" - ReflectedRaw = 1 -} - -enum Light { - //% block="dark" - High = sensors.ThresholdState.Low, - //% block="bright" - Low = sensors.ThresholdState.High + AmbientLight = 2, } namespace sensors { @@ -66,14 +54,16 @@ namespace sensors { //% blockNamespace=sensors //% this.fieldEditor="ports" //% weight=99 blockGap=8 - //% subcategory="NXT Sensors" + //% subcategory="NXT" //% group="Light Sensor" light(mode: NXTLightSensorMode) { //this.setMode(mode); this.poke(); switch (mode) { - case NXTLightSensorMode.ReflectedLight: + case NXTLightSensorMode.ReflectedLightRaw: return this.reflectetLightRaw(); + case NXTLightSensorMode.ReflectedLight: + return this.reflectetLight(); case NXTLightSensorMode.AmbientLight: return this.ambientLight(); default: @@ -115,11 +105,14 @@ namespace sensors { } //% whenUsed block="2" weight=95 fixedInstance jres=icons.port2 - export const nxtLight2: NXTLightSensor = new NXTLightSensor(2) + export const nxtLight2: NXTLightSensor = new NXTLightSensor(2); + //% whenUsed block="1" weight=90 fixedInstance jres=icons.port1 - export const nxtLight1: NXTLightSensor = new NXTLightSensor(1) + export const nxtLight1: NXTLightSensor = new NXTLightSensor(1); + //% whenUsed block="3" weight=90 fixedInstance jres=icons.port3 - export const nxtLight3: NXTLightSensor = new NXTLightSensor(3) + export const nxtLight3: NXTLightSensor = new NXTLightSensor(3); + //% whenUsed block="4" weight=90 fixedInstance jres=icons.port4 - export const nxtLight4: NXTLightSensor = new NXTLightSensor(4) + export const nxtLight4: NXTLightSensor = new NXTLightSensor(4); } diff --git a/sim/dalboard.ts b/sim/dalboard.ts index c778866c9..de79d221b 100644 --- a/sim/dalboard.ts +++ b/sim/dalboard.ts @@ -159,6 +159,7 @@ namespace pxsim { case DAL.DEVICE_TYPE_TOUCH: this.inputNodes[port] = new TouchSensorNode(port); break; case DAL.DEVICE_TYPE_ULTRASONIC: this.inputNodes[port] = new UltrasonicSensorNode(port); break; case DAL.DEVICE_TYPE_IR: this.inputNodes[port] = new InfraredSensorNode(port); break; + case DAL.DEVICE_TYPE_NXT_LIGHT: this.inputNodes[port] = new NXTLightSensorNode(port); break; } } return this.inputNodes[port]; diff --git a/sim/state/nodeTypes.ts b/sim/state/nodeTypes.ts index 53d4c9c78..255353cd4 100644 --- a/sim/state/nodeTypes.ts +++ b/sim/state/nodeTypes.ts @@ -8,7 +8,8 @@ namespace pxsim { GyroSensor = 5, ColorSensor = 6, UltrasonicSensor = 7, - InfraredSensor = 8 + InfraredSensor = 8, + NXTLightSensor = 9 } export interface Node { diff --git a/sim/state/nxtlight.ts b/sim/state/nxtlight.ts new file mode 100644 index 000000000..725ebd061 --- /dev/null +++ b/sim/state/nxtlight.ts @@ -0,0 +1,45 @@ +/// + +namespace pxsim { + + export enum NXTLightSensorMode { + ReflectedLightRaw = 0, + ReflectedLight = 1, + AmbientLight = 2, + } + + export class NXTLightSensorNode extends AnalogSensorNode { + id = NodeType.NXTLightSensor; + + private value: number = 0; + + constructor(port: number) { + super(port); + this.mode = -1; + } + + getDeviceType() { + return DAL.DEVICE_TYPE_NXT_LIGHT; + } + + setColor(value: number) { + this.value = value; + this.setChangedState(); + } + + getValue() { + return this.value; + } + + setMode(mode: number) { + this.mode = mode; + if (this.mode == NXTLightSensorMode.ReflectedLightRaw) { + this.value = 2048; + } else { // Reflection or ambiend light + this.value = 50; + } + this.changed = true; + this.modeChanged = true; + } + } +} \ No newline at end of file diff --git a/sim/visuals/board.ts b/sim/visuals/board.ts index 69d3e9525..bf5896278 100644 --- a/sim/visuals/board.ts +++ b/sim/visuals/board.ts @@ -268,6 +268,11 @@ namespace pxsim.visuals { view = new RotationSliderControl(this.element, this.defs, state, port); break; } + case NodeType.NXTLightSensor: { + const state = ev3board().getInputNodes()[port] as NXTLightSensorNode; + view = new LightWheelControl(this.element, this.defs, state, port); + break; + } case NodeType.MediumMotor: case NodeType.LargeMotor: { const state = ev3board().getMotors()[port]; @@ -306,6 +311,8 @@ namespace pxsim.visuals { view = new UltrasonicSensorView(port); break; case NodeType.InfraredSensor: view = new InfraredView(port); break; + case NodeType.NXTLightSensor: + view = new NXTLightSensorView(port); break; case NodeType.Brick: //return new BrickView(0); view = this.layoutView.getBrick(); break; diff --git a/sim/visuals/controls/lightWheel.ts b/sim/visuals/controls/lightWheel.ts new file mode 100644 index 000000000..a7bd5598a --- /dev/null +++ b/sim/visuals/controls/lightWheel.ts @@ -0,0 +1,105 @@ +namespace pxsim.visuals { + + export class LightWheelControl extends ControlView { + private group: SVGGElement; + private colorGradient: SVGLinearGradientElement; + private reporter: SVGTextElement; + private rect: SVGElement; + + getInnerWidth() { + return 111; + } + + getInnerHeight() { + return 192; + } + + private getReporterHeight() { + return 38; + } + + private getSliderWidth() { + return 62; + } + + private getSliderHeight() { + return 131; + } + + private getMaxValue(state: NXTLightSensorMode) { + return (state == NXTLightSensorMode.ReflectedLightRaw ? 4096 : 100); + } + + private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) { + return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; + } + + updateState() { + if (!this.visible) { + return; + } + const node = this.state; + const value = node.getValue(); + let inverseValue = this.getMaxValue(node.getMode()) - value; + if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw) inverseValue = this.mapValue(inverseValue, 0, 4096, 0, 100); + svg.setGradientValue(this.colorGradient, inverseValue + "%"); + this.reporter.textContent = `${parseFloat((value).toString()).toFixed(0)}`; + if (node.getMode() != NXTLightSensorMode.ReflectedLightRaw) this.reporter.textContent += `%`; + } + + updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) { + let cur = svg.cursorPoint(pt, parent, ev); + const bBox = this.rect.getBoundingClientRect(); + const height = bBox.height; + let t = Math.max(0, Math.min(1, (height + bBox.top / this.scaleFactor - cur.y / this.scaleFactor) / height)); + const state = this.state; + state.setColor(t * this.getMaxValue(state.getMode())); + } + + getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) { + this.group = svg.elt("g") as SVGGElement; + + let gc = "gradient-color-" + this.getPort(); + const prevColorGradient = globalDefs.querySelector(`#${gc}`) as SVGLinearGradientElement; + this.colorGradient = prevColorGradient ? prevColorGradient : svg.linearGradient(globalDefs, gc, false); + svg.setGradientValue(this.colorGradient, "50%"); + svg.setGradientColors(this.colorGradient, "black", "yellow"); + + const reporterGroup = pxsim.svg.child(this.group, "g"); + reporterGroup.setAttribute("transform", `translate(${this.getWidth() / 2}, 20)`); + this.reporter = pxsim.svg.child(reporterGroup, "text", { 'text-anchor': 'middle', 'x': 0, 'y': '0', 'class': 'sim-text number large inverted' }) as SVGTextElement; + + const sliderGroup = pxsim.svg.child(this.group, "g"); + sliderGroup.setAttribute("transform", `translate(${this.getWidth() / 2 - this.getSliderWidth() / 2}, ${this.getReporterHeight()})`); + + const rect = pxsim.svg.child(sliderGroup, "rect", + { + "width": this.getSliderWidth(), + "height": this.getSliderHeight(), + "style": `fill: url(#${gc})` + } + ) + this.rect = rect; + + let pt = parent.createSVGPoint(); + let captured = false; + touchEvents(rect, ev => { + if (captured && (ev as MouseEvent).clientY) { + ev.preventDefault(); + this.updateColorLevel(pt, parent, ev as MouseEvent); + } + }, ev => { + captured = true; + if ((ev as MouseEvent).clientY) { + rect.setAttribute('cursor', '-webkit-grabbing'); + this.updateColorLevel(pt, parent, ev as MouseEvent); + } + }, () => { + captured = false; + rect.setAttribute('cursor', '-webkit-grab'); + }) + + return this.group; + } + } +} \ No newline at end of file diff --git a/sim/visuals/nodes/nxtLightSensorView.ts b/sim/visuals/nodes/nxtLightSensorView.ts new file mode 100644 index 000000000..3b9fa55b7 --- /dev/null +++ b/sim/visuals/nodes/nxtLightSensorView.ts @@ -0,0 +1,45 @@ +/// + +namespace pxsim.visuals { + export class NXTLightSensorView extends SensorView implements LayoutElement { + + private control: LightWheelControl; + + private static sensor_hole_id = 'color_sensor_white_big'; + + constructor(port: number) { + super(COLOR_SENSOR_SVG, "color", NodeType.NXTLightSensor, port); + } + + protected optimizeForLightMode() { + (this.content.getElementById(this.normalizeId('color_bigbox-2_path')) as SVGElement).style.fill = '#a8aaa8'; + } + + public getPaddingRatio() { + return 1 / 4; + } + + public updateState() { + super.updateState(); + + const lightState = ev3board().getInputNodes()[this.port]; + if (!lightState) return; + const mode = lightState.getMode(); + + switch (mode) { + case NXTLightSensorMode.ReflectedLightRaw: this.updateSensorLightVisual('#F86262'); return; // red + case NXTLightSensorMode.ReflectedLight: this.updateSensorLightVisual('#F86262'); return; // red + case NXTLightSensorMode.AmbientLight: this.updateSensorLightVisual('#0062DD'); return; // blue + } + this.updateSensorLightVisual('#ffffff'); + } + + private updateSensorLightVisual(color: string) { + const sensorHole = this.content.getElementById(this.normalizeId(NXTLightSensorView.sensor_hole_id)) as SVGCircleElement; + sensorHole.style.stroke = color; + if (color != '#ffffff') { + sensorHole.style.strokeWidth = '2px'; + } + } + } +} \ No newline at end of file From 5dacf1aaf419b0890321a191183d05f78b77eae1 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 19 Oct 2023 23:08:41 +0300 Subject: [PATCH 06/53] min-and-max-metod --- libs/nxt-light-sensor/light.ts | 32 ++++++++++++++++++++++++++++---- sim/state/nxtlight.ts | 1 + 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index f86637409..af956b758 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -12,6 +12,9 @@ namespace sensors { //% fixedInstances export class NXTLightSensor extends internal.AnalogSensor { + darkValue: number; + lightValue: number; + constructor(port: number) { super(port); } @@ -20,7 +23,8 @@ namespace sensors { return this.readValue(); } - _info(): string { + _info() { + console.log(this._query().toString()); return this._query().toString(); } @@ -48,7 +52,7 @@ namespace sensors { * @param sensor the color sensor port */ //% help=sensors/nxt-light-sensor/light - //% block="**nxt light sensor** %this|%mode" + //% block="**nxt light sensor** $this|$mode" //% blockId=nxtLight //% parts="nxtlightsensor" //% blockNamespace=sensors @@ -57,7 +61,7 @@ namespace sensors { //% subcategory="NXT" //% group="Light Sensor" light(mode: NXTLightSensorMode) { - //this.setMode(mode); + this.setMode(mode); this.poke(); switch (mode) { case NXTLightSensorMode.ReflectedLightRaw: @@ -71,11 +75,31 @@ namespace sensors { } } + /** + * Set the minimum and maximum range of values for determining dark and light. This must be done so that the reflection and ambient lighting mode determines the value in the range from 0 to 100 percent. + * @param sensor the color sensor port + * @param dark the value of dark + * @param light the value of light + */ + //% help=sensors/nxt-light-sensor/light + //% block="**nxt light sensor** $this| set dark $dark|light $light" + //% blockId=setRange + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% this.fieldEditor="ports" + //% weight=89 blockGap=8 + //% subcategory="NXT" + //% group="Light Sensor" + setRange(dark: number, light: number) { + this.darkValue = dark; + this.lightValue = light; + } + /** * Gets the raw light value. */ //% - readValue() { + private readValue() { return this._readPin1(); } diff --git a/sim/state/nxtlight.ts b/sim/state/nxtlight.ts index 725ebd061..4eabeb71d 100644 --- a/sim/state/nxtlight.ts +++ b/sim/state/nxtlight.ts @@ -3,6 +3,7 @@ namespace pxsim { export enum NXTLightSensorMode { + None = -1, ReflectedLightRaw = 0, ReflectedLight = 1, AmbientLight = 2, From 5087eb89b92209fc18a792f8336cc0778be52fb4 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Sat, 21 Oct 2023 00:44:31 +0300 Subject: [PATCH 07/53] sim-support-new-chenges --- libs/core/input.ts | 8 +++++--- libs/nxt-light-sensor/light.ts | 23 ++++++++++++++--------- sim/state/analog.ts | 6 +++--- sim/state/nxtlight.ts | 8 ++++++++ sim/state/sensor.ts | 4 ++++ 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index 577d100d3..960a0e35b 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -256,6 +256,7 @@ namespace sensors.internal { for (const sensorInfo of sensorInfos) { const newConn = inConn[sensorInfo.port]; + console.log("newConn: " + newConn); if (newConn == sensorInfo.connType && sensorInfo.sensor && sensorInfo.sensor.isActive()) { @@ -276,8 +277,9 @@ namespace sensors.internal { sensorInfo.iicid = readIICID(sensorInfo.port); control.dmesg(`IIC ID ${sensorInfo.iicid.length}`); } else if (newConn == DAL.CONN_NXT_DUMB) { - control.dmesg(`new NXT analog connection at ${sensorInfo.port}`); + control.dmesg(`new NXT analog connection at port ${sensorInfo.port}`); sensorInfo.devType = inDcm[sensorInfo.port]; + console.log("sensorInfo.devType: " + sensorInfo.devType); control.dmesg(`NXT analog dev type ${sensorInfo.devType}`); } else if (newConn == DAL.CONN_INPUT_DUMB) { control.dmesg(`new DUMB connection at ${sensorInfo.port}`); @@ -408,12 +410,12 @@ namespace sensors.internal { } _readPin1() { - if (!this.isActive()) return 0; + //if (!this.isActive()) return 0; return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin1 + 2 * this._port); } _readPin6() { - if (!this.isActive()) return 0; + //if (!this.isActive()) return 0; return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this._port); } } diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index af956b758..644654768 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -17,6 +17,8 @@ namespace sensors { constructor(port: number) { super(port); + this.darkValue = 4096; + this.lightValue = 0; } _query() { @@ -24,7 +26,6 @@ namespace sensors { } _info() { - console.log(this._query().toString()); return this._query().toString(); } @@ -78,8 +79,8 @@ namespace sensors { /** * Set the minimum and maximum range of values for determining dark and light. This must be done so that the reflection and ambient lighting mode determines the value in the range from 0 to 100 percent. * @param sensor the color sensor port - * @param dark the value of dark - * @param light the value of light + * @param dark the value of dark, eg: 0 + * @param light the value of light, eg: 4096 */ //% help=sensors/nxt-light-sensor/light //% block="**nxt light sensor** $this| set dark $dark|light $light" @@ -116,7 +117,9 @@ namespace sensors { */ //% reflectetLight() { - return this.readValue(); + let reflectedVal = Math.map(this.readValue(), this.lightValue, this.darkValue, 0, 100); + reflectedVal = Math.constrain(reflectedVal, 0, 100); + return reflectedVal; } /** @@ -124,16 +127,18 @@ namespace sensors { */ //% ambientLight() { - return this.readValue(); + let ambientVal = Math.map(this.readValue(), this.lightValue, this.darkValue, 0, 100); + ambientVal = Math.constrain(ambientVal, 0, 100); + return ambientVal; } } - //% whenUsed block="2" weight=95 fixedInstance jres=icons.port2 - export const nxtLight2: NXTLightSensor = new NXTLightSensor(2); - - //% whenUsed block="1" weight=90 fixedInstance jres=icons.port1 + //% whenUsed block="1" weight=95 fixedInstance jres=icons.port1 export const nxtLight1: NXTLightSensor = new NXTLightSensor(1); + //% whenUsed block="2" weight=90 fixedInstance jres=icons.port2 + export const nxtLight2: NXTLightSensor = new NXTLightSensor(2); + //% whenUsed block="3" weight=90 fixedInstance jres=icons.port3 export const nxtLight3: NXTLightSensor = new NXTLightSensor(3); diff --git a/sim/state/analog.ts b/sim/state/analog.ts index 7122b4f15..40cb76f97 100644 --- a/sim/state/analog.ts +++ b/sim/state/analog.ts @@ -1,6 +1,6 @@ namespace pxsim { - enum AnalogOff { + export enum AnalogOff { InPin1 = 0, // int16[4] InPin6 = 8, // int16[4] OutPin5 = 16, // int16[4] @@ -36,10 +36,10 @@ namespace pxsim { for (let port = 0; port < DAL.NUM_INPUTS; port++) { const node = inputNodes[port]; if (node) { - data[AnalogOff.InConn + port] = node.isUart() ? DAL.CONN_INPUT_UART : DAL.CONN_INPUT_DUMB; + data[AnalogOff.InConn + port] = node.isUart() ? DAL.CONN_INPUT_UART : DAL.CONN_INPUT_DUMB; // CONN_NXT_DUMB if (node.isAnalog() && node.hasData()) { //data[AnalogOff.InPin6 + 2 * port] = node.getValue(); - util.map16Bit(data, AnalogOff.InPin6 + 2 * port, Math.floor(node.getValue())); + util.map16Bit(data, node.getAnalogReadPin() + 2 * port, Math.floor(node.getValue())); } } } diff --git a/sim/state/nxtlight.ts b/sim/state/nxtlight.ts index 4eabeb71d..bb1756296 100644 --- a/sim/state/nxtlight.ts +++ b/sim/state/nxtlight.ts @@ -42,5 +42,13 @@ namespace pxsim { this.changed = true; this.modeChanged = true; } + + public hasData() { + return this.value > 0; + } + + getAnalogReadPin() { + return AnalogOff.InPin1; + } } } \ No newline at end of file diff --git a/sim/state/sensor.ts b/sim/state/sensor.ts index 42e2df017..7c7c957d1 100644 --- a/sim/state/sensor.ts +++ b/sim/state/sensor.ts @@ -32,6 +32,10 @@ namespace pxsim { return [0]; } + public getAnalogReadPin() { + return AnalogOff.InPin6; // Defl for ev3 sensor + } + setMode(mode: number) { this.mode = mode; this.changed = true; From 9ff419d49dc23ce29cf376326159b59d0069a5f1 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 13:44:09 +0300 Subject: [PATCH 08/53] for-sim The simulator now understands that it has an EV3 or NXT analog sensor enabled. The devType value of the virtual sensor will be passed so that the sensor becomes active in the simulator and transmits its values to the simulator. --- libs/nxt-light-sensor/light.ts | 5 +++++ sim/state/analog.ts | 3 ++- sim/state/nxtlight.ts | 8 ++++---- sim/state/sensor.ts | 4 ++++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 644654768..8f80e1e49 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -48,6 +48,11 @@ namespace sensors { return this.mode; } + // This pin is not used by the NXT Analog Sensor + _readPin6() { + return 0; + } + /** * Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest). For raw reflection values, the range can be from 0 to 4096. * @param sensor the color sensor port diff --git a/sim/state/analog.ts b/sim/state/analog.ts index 40cb76f97..eeafca96b 100644 --- a/sim/state/analog.ts +++ b/sim/state/analog.ts @@ -36,7 +36,8 @@ namespace pxsim { for (let port = 0; port < DAL.NUM_INPUTS; port++) { const node = inputNodes[port]; if (node) { - data[AnalogOff.InConn + port] = node.isUart() ? DAL.CONN_INPUT_UART : DAL.CONN_INPUT_DUMB; // CONN_NXT_DUMB + if (node.isAnalog()) data[AnalogOff.InDcm + port] = node.getDeviceType(); + data[AnalogOff.InConn + port] = node.isUart() ? DAL.CONN_INPUT_UART : (!node.isNXT() ? DAL.CONN_INPUT_DUMB : DAL.CONN_NXT_DUMB); if (node.isAnalog() && node.hasData()) { //data[AnalogOff.InPin6 + 2 * port] = node.getValue(); util.map16Bit(data, node.getAnalogReadPin() + 2 * port, Math.floor(node.getValue())); diff --git a/sim/state/nxtlight.ts b/sim/state/nxtlight.ts index bb1756296..cc65214b2 100644 --- a/sim/state/nxtlight.ts +++ b/sim/state/nxtlight.ts @@ -43,12 +43,12 @@ namespace pxsim { this.modeChanged = true; } - public hasData() { - return this.value > 0; - } - getAnalogReadPin() { return AnalogOff.InPin1; } + + isNXT() { + return true; + } } } \ No newline at end of file diff --git a/sim/state/sensor.ts b/sim/state/sensor.ts index 7c7c957d1..55127f7dd 100644 --- a/sim/state/sensor.ts +++ b/sim/state/sensor.ts @@ -20,6 +20,10 @@ namespace pxsim { return false; } + public isNXT() { + return false; + } + public isModeReturnArr() { return this.modeReturnArr; } From 1387d1a21296eddd28b416268df4f739ad74b962 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 13:44:31 +0300 Subject: [PATCH 09/53] Update input.ts --- libs/core/input.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index 960a0e35b..546b83f8f 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -256,7 +256,6 @@ namespace sensors.internal { for (const sensorInfo of sensorInfos) { const newConn = inConn[sensorInfo.port]; - console.log("newConn: " + newConn); if (newConn == sensorInfo.connType && sensorInfo.sensor && sensorInfo.sensor.isActive()) { @@ -277,14 +276,12 @@ namespace sensors.internal { sensorInfo.iicid = readIICID(sensorInfo.port); control.dmesg(`IIC ID ${sensorInfo.iicid.length}`); } else if (newConn == DAL.CONN_NXT_DUMB) { - control.dmesg(`new NXT analog connection at port ${sensorInfo.port}`); sensorInfo.devType = inDcm[sensorInfo.port]; - console.log("sensorInfo.devType: " + sensorInfo.devType); - control.dmesg(`NXT analog dev type ${sensorInfo.devType}`); + control.dmesg(`new NXT DUMB connection at ${sensorInfo.port} dev type ${sensorInfo.devType}`); } else if (newConn == DAL.CONN_INPUT_DUMB) { - control.dmesg(`new DUMB connection at ${sensorInfo.port}`); - // TODO? for now assume touch - sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH; + //sensorInfo.devType = inDcm[sensorInfo.port]; // We get the result DEVICE_TYPE_UNKNOWN + sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH; // TODO? for now assume touch + control.dmesg(`new DUMB connection at ${sensorInfo.port} dev type ${sensorInfo.devType}`); } else if (newConn == DAL.CONN_NONE || newConn == 0) { //control.dmesg(`disconnect at port ${sensorInfo.port}`) } else { @@ -410,12 +407,12 @@ namespace sensors.internal { } _readPin1() { - //if (!this.isActive()) return 0; + if (!this.isActive()) return 0; return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin1 + 2 * this._port); } _readPin6() { - //if (!this.isActive()) return 0; + if (!this.isActive()) return 0; return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this._port); } } From 98dc3b4e91c218689f717b20e32b4fc94d62bf55 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 13:44:48 +0300 Subject: [PATCH 10/53] add-test --- libs/nxt-light-sensor/test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts index e69de29bb..14f9bab34 100644 --- a/libs/nxt-light-sensor/test.ts +++ b/libs/nxt-light-sensor/test.ts @@ -0,0 +1,19 @@ +forever(function () { + control.timer1.reset(); + let rrnls1 = sensors.nxtLight1._readPin1(); + let rrnls2 = sensors.nxtLight1._readPin6(); + let rrnls3 = sensors.nxtLight1.reflectetLightRaw(); + let rrlcs = sensors.color2.light(LightIntensityMode.ReflectedRaw); + let rrrcs = sensors.color3.light(LightIntensityMode.ReflectedRaw); + let t = sensors.touch4.isPressed() ? 1 : 0; + brick.clearScreen(); + brick.printValue("rrnls1", rrnls1, 1); + brick.printValue("rrnls2", rrnls2, 2); + brick.printValue("rrnls3", rrnls3, 3); + brick.printValue("rrlcs", rrlcs, 4); + brick.printValue("rrrcs", rrrcs, 5); + brick.printValue("t", t, 6); + brick.printValue("active1", sensors.nxtLight1.isActive() ? 1 : 0, 11); + brick.printValue("active2", sensors.touch4.isActive() ? 1 : 0, 12); + control.timer1.pauseUntil(10); +}) \ No newline at end of file From 2e32bb12ab30660669e513b899464aa006179b93 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 13:52:46 +0300 Subject: [PATCH 11/53] fix-convert-range-bug --- libs/nxt-light-sensor/light.ts | 4 ++-- libs/nxt-light-sensor/test.ts | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 8f80e1e49..e3a237aed 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -122,7 +122,7 @@ namespace sensors { */ //% reflectetLight() { - let reflectedVal = Math.map(this.readValue(), this.lightValue, this.darkValue, 0, 100); + let reflectedVal = Math.map(this.readValue(), this.darkValue, this.lightValue, 0, 100); reflectedVal = Math.constrain(reflectedVal, 0, 100); return reflectedVal; } @@ -132,7 +132,7 @@ namespace sensors { */ //% ambientLight() { - let ambientVal = Math.map(this.readValue(), this.lightValue, this.darkValue, 0, 100); + let ambientVal = Math.map(this.readValue(), this.darkValue, this.lightValue, 0, 100); ambientVal = Math.constrain(ambientVal, 0, 100); return ambientVal; } diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts index 14f9bab34..6f9cc9774 100644 --- a/libs/nxt-light-sensor/test.ts +++ b/libs/nxt-light-sensor/test.ts @@ -1,15 +1,17 @@ +sensors.nxtLight1.setRange(2600, 1600); + forever(function () { control.timer1.reset(); - let rrnls1 = sensors.nxtLight1._readPin1(); - let rrnls2 = sensors.nxtLight1._readPin6(); - let rrnls3 = sensors.nxtLight1.reflectetLightRaw(); + let nls1 = sensors.nxtLight1._readPin1(); + let nls2 = sensors.nxtLight1.reflectetLightRaw(); + let nls3 = sensors.nxtLight1.light(NXTLightSensorMode.ReflectedLight); let rrlcs = sensors.color2.light(LightIntensityMode.ReflectedRaw); let rrrcs = sensors.color3.light(LightIntensityMode.ReflectedRaw); let t = sensors.touch4.isPressed() ? 1 : 0; brick.clearScreen(); - brick.printValue("rrnls1", rrnls1, 1); - brick.printValue("rrnls2", rrnls2, 2); - brick.printValue("rrnls3", rrnls3, 3); + brick.printValue("nls1", nls1, 1); + brick.printValue("nls2", nls2, 2); + brick.printValue("nls3", nls3, 3); brick.printValue("rrlcs", rrlcs, 4); brick.printValue("rrrcs", rrrcs, 5); brick.printValue("t", t, 6); From 44825ed132ab1bb5cd165e9e39ae89f0b182cdbe Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 14:02:27 +0300 Subject: [PATCH 12/53] Update light.ts Setting the range of values for determining black and white in reflection mode and in ambient light mode is now different. --- libs/nxt-light-sensor/light.ts | 48 +++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index e3a237aed..d43b6374c 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -12,13 +12,17 @@ namespace sensors { //% fixedInstances export class NXTLightSensor extends internal.AnalogSensor { - darkValue: number; - lightValue: number; + darkRefLight: number; + lightRefLight: number; + darkAmbientLight: number; + lightAmbientLight: number; constructor(port: number) { super(port); - this.darkValue = 4096; - this.lightValue = 0; + this.darkRefLight = 4096; + this.lightRefLight = 0; + this.darkAmbientLight = 4096; + this.lightAmbientLight = 0; } _query() { @@ -82,23 +86,43 @@ namespace sensors { } /** - * Set the minimum and maximum range of values for determining dark and light. This must be done so that the reflection and ambient lighting mode determines the value in the range from 0 to 100 percent. + * Set the range of values for determining dark and light in light reflection mode. This must be done so that the reflection mode defines a value in the range from 0 to 100 percent. * @param sensor the color sensor port * @param dark the value of dark, eg: 0 * @param light the value of light, eg: 4096 */ //% help=sensors/nxt-light-sensor/light - //% block="**nxt light sensor** $this| set dark $dark|light $light" - //% blockId=setRange + //% block="**nxt light sensor** $this| set reflected range dark $dark|light $light" + //% blockId=setReflectedLightRange //% parts="nxtlightsensor" //% blockNamespace=sensors //% this.fieldEditor="ports" //% weight=89 blockGap=8 //% subcategory="NXT" //% group="Light Sensor" - setRange(dark: number, light: number) { - this.darkValue = dark; - this.lightValue = light; + setReflectedLightRange(dark: number, light: number) { + this.darkRefLight = dark; + this.lightRefLight = light; + } + + /** + * Set the value range for dark and light detection in ambient light mode. This must be done so that the ambient light mode determines the value in the range from 0 to 100 percent. + * @param sensor the color sensor port + * @param dark the value of dark, eg: 0 + * @param light the value of light, eg: 4096 + */ + //% help=sensors/nxt-light-sensor/light + //% block="**nxt light sensor** $this| set ambient range dark $dark|light $light" + //% blockId=setAmbientLightRange + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% this.fieldEditor="ports" + //% weight=88 blockGap=8 + //% subcategory="NXT" + //% group="Light Sensor" + setAmbientLightRange(dark: number, light: number) { + this.darkAmbientLight = dark; + this.lightAmbientLight = light; } /** @@ -122,7 +146,7 @@ namespace sensors { */ //% reflectetLight() { - let reflectedVal = Math.map(this.readValue(), this.darkValue, this.lightValue, 0, 100); + let reflectedVal = Math.map(this.readValue(), this.darkRefLight, this.lightRefLight, 0, 100); reflectedVal = Math.constrain(reflectedVal, 0, 100); return reflectedVal; } @@ -132,7 +156,7 @@ namespace sensors { */ //% ambientLight() { - let ambientVal = Math.map(this.readValue(), this.darkValue, this.lightValue, 0, 100); + let ambientVal = Math.map(this.readValue(), this.darkAmbientLight, this.lightAmbientLight, 0, 100); ambientVal = Math.constrain(ambientVal, 0, 100); return ambientVal; } From a093f15428f7dc303c57d01c5034c3c947fb43f9 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 14:05:05 +0300 Subject: [PATCH 13/53] Update test.ts --- libs/nxt-light-sensor/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts index 6f9cc9774..cd96e14e6 100644 --- a/libs/nxt-light-sensor/test.ts +++ b/libs/nxt-light-sensor/test.ts @@ -1,4 +1,4 @@ -sensors.nxtLight1.setRange(2600, 1600); +sensors.nxtLight1.setReflectedLightRange(2600, 1600); forever(function () { control.timer1.reset(); From a78beb8a3c4b031043ed89ba2b276d026e16fbe0 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 14:08:43 +0300 Subject: [PATCH 14/53] Update test.ts --- libs/nxt-light-sensor/test.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts index cd96e14e6..3d5a02280 100644 --- a/libs/nxt-light-sensor/test.ts +++ b/libs/nxt-light-sensor/test.ts @@ -2,19 +2,17 @@ sensors.nxtLight1.setReflectedLightRange(2600, 1600); forever(function () { control.timer1.reset(); - let nls1 = sensors.nxtLight1._readPin1(); - let nls2 = sensors.nxtLight1.reflectetLightRaw(); - let nls3 = sensors.nxtLight1.light(NXTLightSensorMode.ReflectedLight); + let nls1 = sensors.nxtLight1.reflectetLightRaw(); + let nls2 = sensors.nxtLight1.light(NXTLightSensorMode.ReflectedLight); let rrlcs = sensors.color2.light(LightIntensityMode.ReflectedRaw); let rrrcs = sensors.color3.light(LightIntensityMode.ReflectedRaw); let t = sensors.touch4.isPressed() ? 1 : 0; brick.clearScreen(); brick.printValue("nls1", nls1, 1); brick.printValue("nls2", nls2, 2); - brick.printValue("nls3", nls3, 3); - brick.printValue("rrlcs", rrlcs, 4); - brick.printValue("rrrcs", rrrcs, 5); - brick.printValue("t", t, 6); + brick.printValue("rrlcs", rrlcs, 3); + brick.printValue("rrrcs", rrrcs, 4); + brick.printValue("t", t, 5); brick.printValue("active1", sensors.nxtLight1.isActive() ? 1 : 0, 11); brick.printValue("active2", sensors.touch4.isActive() ? 1 : 0, 12); control.timer1.pauseUntil(10); From d475ec5c6297cc01a9319753d9f767ce1a5671f8 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 14:09:06 +0300 Subject: [PATCH 15/53] Update test.ts --- libs/nxt-light-sensor/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts index 3d5a02280..627390ddc 100644 --- a/libs/nxt-light-sensor/test.ts +++ b/libs/nxt-light-sensor/test.ts @@ -14,6 +14,6 @@ forever(function () { brick.printValue("rrrcs", rrrcs, 4); brick.printValue("t", t, 5); brick.printValue("active1", sensors.nxtLight1.isActive() ? 1 : 0, 11); - brick.printValue("active2", sensors.touch4.isActive() ? 1 : 0, 12); + brick.printValue("active4", sensors.touch4.isActive() ? 1 : 0, 12); control.timer1.pauseUntil(10); }) \ No newline at end of file From e1ceb06d7d4b82d724477e58b6f93e89fc7fc35a Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 14:18:12 +0300 Subject: [PATCH 16/53] removing-function-doesnt-work I'm removing an unnecessary function that doesn't work, which I took from the pelikhan change. It does not apply to this change with the sensor and data input. --- libs/ev3/console.ts | 1 - libs/screen/targetoverrides.ts | 7 ------- 2 files changed, 8 deletions(-) diff --git a/libs/ev3/console.ts b/libs/ev3/console.ts index aa6be16fe..ef28f8b4d 100644 --- a/libs/ev3/console.ts +++ b/libs/ev3/console.ts @@ -21,7 +21,6 @@ namespace console._screen { console.addListener(log); brick.buttonUp.onEvent(ButtonEvent.Bumped, () => scroll(-3)); brick.buttonDown.onEvent(ButtonEvent.Bumped, () => scroll(3)); - brick.showConsole(); } } diff --git a/libs/screen/targetoverrides.ts b/libs/screen/targetoverrides.ts index fd17d257e..0d0005a91 100644 --- a/libs/screen/targetoverrides.ts +++ b/libs/screen/targetoverrides.ts @@ -28,7 +28,6 @@ namespace brick { ShowLines, Image, Ports, - Console, Custom } @@ -299,12 +298,6 @@ namespace brick { }) } - export function showConsole() { - //console.sendToScreen(); - screenMode = ScreenMode.Console; - //clearScreen(); - } - /** * An image * @param image the image From a387ff2f7404a266dfb433489cebb9ecf3039428 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 23 Oct 2023 22:18:12 +0300 Subject: [PATCH 17/53] checking-input-values --- libs/nxt-light-sensor/light.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index d43b6374c..d5aaadcc5 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -101,8 +101,9 @@ namespace sensors { //% subcategory="NXT" //% group="Light Sensor" setReflectedLightRange(dark: number, light: number) { - this.darkRefLight = dark; - this.lightRefLight = light; + if (dark <= light) return; + this.darkRefLight = Math.constrain(dark, 0, 4096); + this.lightRefLight = Math.constrain(light, 0, 4096); } /** @@ -121,8 +122,9 @@ namespace sensors { //% subcategory="NXT" //% group="Light Sensor" setAmbientLightRange(dark: number, light: number) { - this.darkAmbientLight = dark; - this.lightAmbientLight = light; + if (dark <= light) return; + this.darkAmbientLight = Math.constrain(dark, 0, 4096); + this.lightAmbientLight = Math.constrain(light, 0, 4096); } /** From 68b036bf0061da9e92da86c1b6b2c77530937e12 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Tue, 24 Oct 2023 01:14:59 +0300 Subject: [PATCH 18/53] set-nxt-light-sensor-svg --- sim/visuals/assets/NXT Light Sensor.svg | 90 +++++++++++++++++++++++++ sim/visuals/assets/NXT Light Sensor.ts | 90 +++++++++++++++++++++++++ sim/visuals/nodes/nxtLightSensorView.ts | 6 +- 3 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 sim/visuals/assets/NXT Light Sensor.svg create mode 100644 sim/visuals/assets/NXT Light Sensor.ts diff --git a/sim/visuals/assets/NXT Light Sensor.svg b/sim/visuals/assets/NXT Light Sensor.svg new file mode 100644 index 000000000..732e1a909 --- /dev/null +++ b/sim/visuals/assets/NXT Light Sensor.svg @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sim/visuals/assets/NXT Light Sensor.ts b/sim/visuals/assets/NXT Light Sensor.ts new file mode 100644 index 000000000..144a4a6ae --- /dev/null +++ b/sim/visuals/assets/NXT Light Sensor.ts @@ -0,0 +1,90 @@ +namespace pxsim { + export const NXT_LIGHT_SENSOR_SVG = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; +} \ No newline at end of file diff --git a/sim/visuals/nodes/nxtLightSensorView.ts b/sim/visuals/nodes/nxtLightSensorView.ts index 3b9fa55b7..941a68149 100644 --- a/sim/visuals/nodes/nxtLightSensorView.ts +++ b/sim/visuals/nodes/nxtLightSensorView.ts @@ -5,14 +5,14 @@ namespace pxsim.visuals { private control: LightWheelControl; - private static sensor_hole_id = 'color_sensor_white_big'; + private static sensor_hole_id = 'light_sensor_light_red'; constructor(port: number) { - super(COLOR_SENSOR_SVG, "color", NodeType.NXTLightSensor, port); + super(NXT_LIGHT_SENSOR_SVG, "color", NodeType.NXTLightSensor, port); } protected optimizeForLightMode() { - (this.content.getElementById(this.normalizeId('color_bigbox-2_path')) as SVGElement).style.fill = '#a8aaa8'; + (this.content.getElementById(this.normalizeId('box')) as SVGElement).style.fill = '#a8aaa8'; } public getPaddingRatio() { From c9bec99fbf4de09a0c9358a4e58150e8be7e3020 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Tue, 24 Oct 2023 12:42:50 +0300 Subject: [PATCH 19/53] Update light.ts --- libs/nxt-light-sensor/light.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index d5aaadcc5..bb34c2253 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -3,8 +3,10 @@ const enum NXTLightSensorMode { ReflectedLightRaw = 0, //% block="reflected light" ReflectedLight = 1, + //% block="ambient light (raw)" + AmbientLightRaw = 2, //% block="ambient light" - AmbientLight = 2, + AmbientLight = 3, } namespace sensors { @@ -122,6 +124,7 @@ namespace sensors { //% subcategory="NXT" //% group="Light Sensor" setAmbientLightRange(dark: number, light: number) { + // ToDo: the red LED should be turned off in ambient lighting mode if (dark <= light) return; this.darkAmbientLight = Math.constrain(dark, 0, 4096); this.lightAmbientLight = Math.constrain(light, 0, 4096); From 515cb303458a742e41e0cf1bba8de3220810e1f3 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Wed, 25 Oct 2023 00:18:50 +0300 Subject: [PATCH 20/53] update-for-sim --- libs/nxt-light-sensor/light.ts | 23 +++- sim/visuals/assets/NXT Light Sensor.svg | 155 +++++++++++----------- sim/visuals/assets/NXT Light Sensor.ts | 166 +++++++++++------------- sim/visuals/nodes/nxtLightSensorView.ts | 11 +- 4 files changed, 175 insertions(+), 180 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index bb34c2253..a776cb749 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -32,7 +32,11 @@ namespace sensors { } _info() { - return this._query().toString(); + if (this.mode == NXTLightSensorMode.ReflectedLight || this.mode == NXTLightSensorMode.AmbientLightRaw) { + return `${this._query().toString()}%`; + } else { + return this._query().toString(); + } } _update(prev: number, curr: number) { @@ -80,6 +84,8 @@ namespace sensors { return this.reflectetLightRaw(); case NXTLightSensorMode.ReflectedLight: return this.reflectetLight(); + case NXTLightSensorMode.AmbientLightRaw: + return this.ambientLightRaw(); case NXTLightSensorMode.AmbientLight: return this.ambientLight(); default: @@ -124,7 +130,6 @@ namespace sensors { //% subcategory="NXT" //% group="Light Sensor" setAmbientLightRange(dark: number, light: number) { - // ToDo: the red LED should be turned off in ambient lighting mode if (dark <= light) return; this.darkAmbientLight = Math.constrain(dark, 0, 4096); this.lightAmbientLight = Math.constrain(light, 0, 4096); @@ -143,11 +148,21 @@ namespace sensors { */ //% reflectetLightRaw() { + // ToDo: the red LED should be turned off in ambient lighting mode + return this.readValue(); + } + + /** + * Gets the raw ambient light value. + */ + //% + ambientLightRaw() { + // ToDo: the red LED should be turned off in ambient lighting mode return this.readValue(); } /** - * Gets the reflection light value. + * Gets the normalize reflection light value. */ //% reflectetLight() { @@ -157,7 +172,7 @@ namespace sensors { } /** - * Gets the ambient light value. + * Gets the normalize ambient light value. */ //% ambientLight() { diff --git a/sim/visuals/assets/NXT Light Sensor.svg b/sim/visuals/assets/NXT Light Sensor.svg index 732e1a909..83191f763 100644 --- a/sim/visuals/assets/NXT Light Sensor.svg +++ b/sim/visuals/assets/NXT Light Sensor.svg @@ -1,90 +1,81 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - - - - - - - - - - - - - - - - - + + + + - - - - - + + + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sim/visuals/assets/NXT Light Sensor.ts b/sim/visuals/assets/NXT Light Sensor.ts index 144a4a6ae..71b24cdad 100644 --- a/sim/visuals/assets/NXT Light Sensor.ts +++ b/sim/visuals/assets/NXT Light Sensor.ts @@ -1,90 +1,80 @@ namespace pxsim { - export const NXT_LIGHT_SENSOR_SVG = ` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -`; + export const NXT_LIGHT_SENSOR_SVG = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + `; } \ No newline at end of file diff --git a/sim/visuals/nodes/nxtLightSensorView.ts b/sim/visuals/nodes/nxtLightSensorView.ts index 941a68149..e91424f4b 100644 --- a/sim/visuals/nodes/nxtLightSensorView.ts +++ b/sim/visuals/nodes/nxtLightSensorView.ts @@ -5,7 +5,7 @@ namespace pxsim.visuals { private control: LightWheelControl; - private static sensor_hole_id = 'light_sensor_light_red'; + private static sensor_hole_id = 'led'; constructor(port: number) { super(NXT_LIGHT_SENSOR_SVG, "color", NodeType.NXTLightSensor, port); @@ -26,12 +26,11 @@ namespace pxsim.visuals { if (!lightState) return; const mode = lightState.getMode(); - switch (mode) { - case NXTLightSensorMode.ReflectedLightRaw: this.updateSensorLightVisual('#F86262'); return; // red - case NXTLightSensorMode.ReflectedLight: this.updateSensorLightVisual('#F86262'); return; // red - case NXTLightSensorMode.AmbientLight: this.updateSensorLightVisual('#0062DD'); return; // blue + if (mode == NXTLightSensorMode.ReflectedLightRaw || mode == NXTLightSensorMode.ReflectedLight) { + this.updateSensorLightVisual('#eb0c0c'); + } else { + this.updateSensorLightVisual('#ffffff'); } - this.updateSensorLightVisual('#ffffff'); } private updateSensorLightVisual(color: string) { From 5306528cd9ca62b72d18fa10cb2ce2d918d49986 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 3 Nov 2023 21:39:19 +0300 Subject: [PATCH 21/53] Update NXT Light Sensor.svg --- sim/visuals/assets/NXT Light Sensor.svg | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/sim/visuals/assets/NXT Light Sensor.svg b/sim/visuals/assets/NXT Light Sensor.svg index 83191f763..76fa3a4be 100644 --- a/sim/visuals/assets/NXT Light Sensor.svg +++ b/sim/visuals/assets/NXT Light Sensor.svg @@ -11,7 +11,7 @@ c1.7,0,3,1.3,3,3v11.6"/> - + @@ -20,7 +20,7 @@ - @@ -30,37 +30,37 @@ c-4.2,0-7.5,3.2-7.5,7.5c0,0.3,0,5.7,0,10c0,4.3,0.3,4.1,0.2,5.5c0.5-0.2,7.2-0.2,7.2-0.2s6.9-0.1,7.4,0.2 c-0.1-1.5,0.1-1.2,0.2-5.4C24.1,26.2,24.1,17.7,24.1,17.4z"/> - + - + - + - - + - - + - + - + @@ -69,7 +69,7 @@ - From 415288708cb30afead7ed79fb19417c118049ae1 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 3 Nov 2023 22:30:13 +0300 Subject: [PATCH 22/53] resolving-conflict-with-master --- libs/core/input.ts | 202 +++++++++++++++++++++------------------------ libs/ev3/ns.ts | 1 + 2 files changed, 97 insertions(+), 106 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index 546b83f8f..dcd98802f 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -1,4 +1,8 @@ namespace sensors.internal { + + const UART_PORT_CHANGED = 1; + const UART_DATA_READY = 8; + export class Poller { private query: () => number; private update: (previous: number, current: number) => void; @@ -7,7 +11,7 @@ namespace sensors.internal { private previousValue: number; private currentValue: number; private lastQuery: number; // track down the last time we did a query/update cycle - private lastPause: number; // track down the last time we pause in the sensor polling loop + private lastPause: number; // track down the last time we pause in the sensor polling loop constructor(interval: number, query: () => number, update: (previous: number, current: number) => void) { this.interval = interval | 0; @@ -35,6 +39,7 @@ namespace sensors.internal { private poll() { control.runInBackground(() => { + pause(this.interval); this.lastQuery = this.lastPause = control.millis(); this.previousValue = this.currentValue = this.query(); this.update(this.previousValue, this.currentValue); @@ -55,12 +60,12 @@ namespace sensors.internal { return s } - let analogMM: MMap - let uartMM: MMap - let IICMM: MMap - let powerMM: MMap - let devcon: Buffer - let devPoller: Poller + let analogMM: MMap; + let uartMM: MMap; + let IICMM: MMap; + let powerMM: MMap; + let devcon: Buffer; + let devPoller: Poller; let sensorInfos: SensorInfo[]; let batteryInfo: { @@ -94,7 +99,7 @@ namespace sensors.internal { } private query() { - if (this.sensor) return this.sensor._query(); + if (this.sensor) return this.sensor._query()[0]; return 0; } @@ -120,10 +125,11 @@ namespace sensors.internal { powerMM = control.mmap("/dev/lms_power", 2, 0) - devPoller = new Poller(250, () => { return hashDevices(); }, + devPoller = new Poller(900, () => { return hashDevices(); }, (prev, curr) => { detectDevices(); - }); + } + ); } export function getActiveSensors(): Sensor[] { @@ -131,25 +137,6 @@ namespace sensors.internal { return sensorInfos.filter(si => si.sensor && si.sensor.isActive()).map(si => si.sensor); } - function readUartInfo(port: number, mode: number) { - let buf = output.createBuffer(UartCtlOff.Size) - buf[UartCtlOff.Port] = port - buf[UartCtlOff.Mode] = mode - uartMM.ioctl(IO.UART_READ_MODE_INFO, buf) - return buf - //let info = `t:${buf[TypesOff.Type]} c:${buf[TypesOff.Connection]} m:${buf[TypesOff.Mode]} n:${buf.slice(0, 12).toHex()}` - //serial.writeLine("UART " + port + " / " + mode + " - " + info) - } - - export function readIICID(port: number) { - const buf = output.createBuffer(IICStr.Size) - buf[IICStr.Port] = port - IICMM.ioctl(IO.IIC_READ_TYPE_INFO, buf) - const manufacturer = bufferToString(buf.slice(IICStr.Manufacturer, 8)) - const sensorType = bufferToString(buf.slice(IICStr.SensorType, 8)) - return manufacturer + sensorType; - } - const ADC_REF = 5000 //!< [mV] maximal value on ADC const ADC_RES = 4095 //!< [CNT] maximal count on ADC // see c_ui.c @@ -246,35 +233,26 @@ namespace sensors.internal { return r; } - let nonActivated = 0; function detectDevices() { - control.dmesg(`detect devices (hash ${hashDevices()})`); + control.dmesg(`DETECT DEVICES (hash ${hashDevices()})`); const inDcm = analogMM.slice(AnalogOff.InDcm, DAL.NUM_INPUTS); const inConn = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS); - let numChanged = 0; - const uartSensors: SensorInfo[] = []; for (const sensorInfo of sensorInfos) { const newConn = inConn[sensorInfo.port]; - if (newConn == sensorInfo.connType - && sensorInfo.sensor - && sensorInfo.sensor.isActive()) { - if (newConn == DAL.CONN_INPUT_UART) - uartSensors.push(sensorInfo); + if (newConn == sensorInfo.connType && sensorInfo.sensor && sensorInfo.sensor.isActive()) { continue; } - numChanged++; sensorInfo.connType = newConn; sensorInfo.devType = DAL.DEVICE_TYPE_NONE; if (newConn == DAL.CONN_INPUT_UART) { - control.dmesg(`new UART connection at ${sensorInfo.port}`); + const status = getUartStatus(sensorInfo.port); + control.dmesg(`new UART connection at port ${sensorInfo.port} with status ${status}`); updateUartMode(sensorInfo.port, 0); - uartSensors.push(sensorInfo); } else if (newConn == DAL.CONN_NXT_IIC) { - control.dmesg(`new IIC connection at ${sensorInfo.port}`); sensorInfo.devType = DAL.DEVICE_TYPE_IIC_UNKNOWN; sensorInfo.iicid = readIICID(sensorInfo.port); - control.dmesg(`IIC ID ${sensorInfo.iicid.length}`); + control.dmesg(`new IIC connection at port ${sensorInfo.port} with ID ${sensorInfo.iicid.length}`); } else if (newConn == DAL.CONN_NXT_DUMB) { sensorInfo.devType = inDcm[sensorInfo.port]; control.dmesg(`new NXT DUMB connection at ${sensorInfo.port} dev type ${sensorInfo.devType}`); @@ -283,49 +261,45 @@ namespace sensors.internal { sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH; // TODO? for now assume touch control.dmesg(`new DUMB connection at ${sensorInfo.port} dev type ${sensorInfo.devType}`); } else if (newConn == DAL.CONN_NONE || newConn == 0) { - //control.dmesg(`disconnect at port ${sensorInfo.port}`) + control.dmesg(`disconnect at port ${sensorInfo.port}`); } else { - control.dmesg(`unknown connection type: ${newConn} at ${sensorInfo.port}`); + control.dmesg(`unknown connection type ${newConn} at port ${sensorInfo.port}`); } } - if (uartSensors.length > 0) { + const uartSens = sensorInfos.filter(si => si.connType == DAL.CONN_INPUT_UART).length; + if (uartSens > 0) { setUartModes(); - for (const sensorInfo of uartSensors) { - let uinfo = readUartInfo(sensorInfo.port, 0) - sensorInfo.devType = uinfo[TypesOff.Type] + for (const sensorInfo of sensorInfos.filter(si => si.connType == DAL.CONN_INPUT_UART && si.devType == DAL.CONN_NONE)) { + const uinfo = readUartInfo(sensorInfo.port, 0); + sensorInfo.devType = uinfo[TypesOff.Type]; const mode = uinfo[TypesOff.Mode]; - control.dmesg(`UART type ${sensorInfo.devType} mode ${mode}`) + control.dmesg(`UART type ${sensorInfo.devType} mode ${mode} at port ${sensorInfo.port}`); } } - if (numChanged == 0 && nonActivated == 0) - return - - //control.dmesg(`updating sensor status`) - nonActivated = 0; - for (const sensorInfo of sensorInfos) { + control.dmesg(`UPDATE SENSOR STATUS`); + for (const sensorInfo of sensorInfos.filter(si => !si.sensor)) { if (sensorInfo.devType == DAL.DEVICE_TYPE_IIC_UNKNOWN) { - sensorInfo.sensor = sensorInfo.sensors.filter(s => s._IICId() == sensorInfo.iicid)[0] + sensorInfo.sensor = sensorInfo.sensors.filter(s => s._IICId() == sensorInfo.iicid)[0]; if (!sensorInfo.sensor) { - control.dmesg(`sensor not found for iicid=${sensorInfo.iicid} at ${sensorInfo.port}`) - nonActivated++; + control.dmesg(`sensor not found for iicid=${sensorInfo.iicid} at port ${sensorInfo.port}`); } else { - control.dmesg(`sensor connected iicid=${sensorInfo.iicid} at ${sensorInfo.port}`) - sensorInfo.sensor._activated() + control.dmesg(`sensor connected iicid=${sensorInfo.iicid} at port ${sensorInfo.port}`); + sensorInfo.sensor._activated(); } } else if (sensorInfo.devType != DAL.DEVICE_TYPE_NONE) { - sensorInfo.sensor = sensorInfo.sensors.filter(s => s._deviceType() == sensorInfo.devType)[0] + sensorInfo.sensor = sensorInfo.sensors.filter(s => s._deviceType() == sensorInfo.devType)[0]; if (!sensorInfo.sensor) { - control.dmesg(`sensor not found for type=${sensorInfo.devType} at ${sensorInfo.port}`) - nonActivated++; + control.dmesg(`sensor not found for type=${sensorInfo.devType} at port ${sensorInfo.port}`); } else { - control.dmesg(`sensor connected type=${sensorInfo.devType} at ${sensorInfo.port}`) - sensorInfo.sensor._activated() + control.dmesg(`sensor connected type=${sensorInfo.devType} at port ${sensorInfo.port}`); + sensorInfo.sensor._activated(); } } } - //control.dmesg(`detect devices done`) + + control.dmesg(`DETECT DEVICES DONE`); } export class Sensor extends control.Component { @@ -361,16 +335,12 @@ namespace sensors.internal { return sensorInfos[this._port].sensor == this } - _query() { - return 0 - } - - _info(): string { - return this._query().toString(); + _query(): number[] { + return [0]; } - _infoArr(): string[] { - return [this._query().toString()]; + _info(): string[] { + return [this._query()[0].toString()]; } _update(prev: number, curr: number) { @@ -432,6 +402,10 @@ namespace sensors.internal { this._setMode(this.mode); } + getStatus() { + return getUartStatus(this._port); + } + protected _setMode(m: number) { //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`); let v = m | 0; @@ -507,10 +481,28 @@ namespace sensors.internal { } } - export const i2cSensor1 = new IICSensor(1); - export const i2cSensor2 = new IICSensor(2); - export const i2cSensor3 = new IICSensor(3); - export const i2cSensor4 = new IICSensor(4); + export const i2csensor1 = new IICSensor(1); + export const i2csensor2 = new IICSensor(2); + export const i2csensor3 = new IICSensor(3); + export const i2csensor4 = new IICSensor(4); + + function readUartInfo(port: number, mode: number) { + let buf = output.createBuffer(UartCtlOff.Size); + buf[UartCtlOff.Port] = port; + buf[UartCtlOff.Mode] = mode; + uartMM.ioctl(IO.UART_READ_MODE_INFO, buf); + //control.dmesg(`UART_READ_MODE p:${port} t:${buf[TypesOff.Type]} c:${buf[TypesOff.Connection]} m:${buf[TypesOff.Mode]} n:${buf.slice(0, 12).toHex()}`); + return buf; + } + + export function readIICID(port: number) { + const buf = output.createBuffer(IICStr.Size); + buf[IICStr.Port] = port; + IICMM.ioctl(IO.IIC_READ_TYPE_INFO, buf); + const manufacturer = bufferToString(buf.slice(IICStr.Manufacturer, 8)); + const sensorType = bufferToString(buf.slice(IICStr.SensorType, 8)); + return manufacturer + sensorType; + } function uartReset(port: number) { if (port < 0) return @@ -527,16 +519,18 @@ namespace sensors.internal { } function waitNonZeroUartStatus(port: number) { - while (true) { - if (port < 0) return 0 - let s = getUartStatus(port) - if (s) return s - pause(25) + let retry = 20; + while (retry-- > 0) { + const s = getUartStatus(port); + if (s) return s; + control.dmesg(`UART status 0 at port ${port}, waiting...`); + pause(25); } + return 0; } function uartClearChange(port: number) { - control.dmesg(`UART clear change`); + control.dmesg(`UART clear change at port ${port}`); while (true) { let status = getUartStatus(port) if (port < 0) break @@ -547,18 +541,17 @@ namespace sensors.internal { devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, DAL.CONN_INPUT_UART) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, 0) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, 0) - + control.dmesg(`UART_CLEAR_CHANGED status ${status} ${devcon.toHex()}`) uartMM.ioctl(IO.UART_CLEAR_CHANGED, devcon) - uartMM.setNumber(NumberFormat.Int8LE, UartOff.Status + port, - getUartStatus(port) & 0xfffe) + uartMM.setNumber(NumberFormat.Int8LE, UartOff.Status + port, getUartStatus(port) & 0xfffe) pause(10) } } function setUartModes() { - control.dmesg(`UART set modes 0x${devcon.toHex()}`) - uartMM.ioctl(IO.UART_SET_CONN, devcon) + control.dmesg(`UART_SET_CONN ${devcon.toHex()}`); + uartMM.ioctl(IO.UART_SET_CONN, devcon); const ports: number[] = []; for (let port = 0; port < DAL.NUM_INPUTS; ++port) { if (devcon.getNumber(NumberFormat.Int8LE, DevConOff.Connection + port) == DAL.CONN_INPUT_UART) { @@ -568,39 +561,36 @@ namespace sensors.internal { while (ports.length) { const port = ports.pop(); - const status = waitNonZeroUartStatus(port) - control.dmesg(`UART status ${status} at ${port}`); + const status = waitNonZeroUartStatus(port); + control.dmesg(`UART status ${status} at port ${port}`); if (!(status & UART_DATA_READY)) setUartMode(port, devcon[DevConOff.Mode + port]); } } function updateUartMode(port: number, mode: number) { - control.dmesg(`UART update mode to ${mode} at ${port}`) + control.dmesg(`UART update mode to ${mode} at port ${port}`); devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, DAL.CONN_INPUT_UART) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, 33) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode) } - - const UART_PORT_CHANGED = 1; - const UART_DATA_READY = 8; - + function setUartMode(port: number, mode: number) { while (true) { - if (port < 0) return + if (port < 0) return; updateUartMode(port, mode); - control.dmesg(`UART set mode 0x${devcon.toHex()}`) - uartMM.ioctl(IO.UART_SET_CONN, devcon) - let status = waitNonZeroUartStatus(port) + control.dmesg(`UART_SET_CONN ${devcon.toHex()}`); + uartMM.ioctl(IO.UART_SET_CONN, devcon); + let status = waitNonZeroUartStatus(port); if (status & UART_PORT_CHANGED) { - control.dmesg(`UART clear changed at ${port}`) - uartClearChange(port) + control.dmesg(`UART clear changed at port ${port}`); + uartClearChange(port); } else { - control.dmesg(`UART status ${status}`); + control.dmesg(`UART status ${status} at port ${port}`); if (status & UART_DATA_READY) break; } - pause(10) + pause(10); } } @@ -630,7 +620,7 @@ namespace sensors.internal { export function transactionIIC(port: number, deviceAddress: number, writeBuf: number[], readLen: number) { if (port < 0) return; - let iicdata = output.createBuffer(IICDat.Size) + const iicdata = output.createBuffer(IICDat.Size) iicdata.setNumber(NumberFormat.Int8LE, IICDat.Port, port) iicdata.setNumber(NumberFormat.Int8LE, IICDat.Repeat, 0) iicdata.setNumber(NumberFormat.Int16LE, IICDat.Time, 0) diff --git a/libs/ev3/ns.ts b/libs/ev3/ns.ts index 93550c7bf..925a04e58 100644 --- a/libs/ev3/ns.ts +++ b/libs/ev3/ns.ts @@ -9,6 +9,7 @@ namespace brick { //% color="#C8509B" weight=95 icon="\uf10f" //% labelLineWidth=100 //% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "Calibration", "Light Sensor"]' +//% subcategories='["NXT", "HiTechnic"]' namespace sensors { } From 59d977622a20cbdea7160121d7d50d7d5ef7622c Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 3 Nov 2023 22:36:33 +0300 Subject: [PATCH 23/53] resolving-conflict-with-master-2 --- libs/core/input.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/core/input.ts b/libs/core/input.ts index dcd98802f..c7226cfe8 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -406,6 +406,10 @@ namespace sensors.internal { return getUartStatus(this._port); } + getStatus() { + return getUartStatus(this._port); + } + protected _setMode(m: number) { //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`); let v = m | 0; From 2c72a0be88c6fa61b6c252579de8c9a5a60c11c6 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 3 Nov 2023 22:53:32 +0300 Subject: [PATCH 24/53] resolving-conflict-with-master-3 --- libs/core/input.ts | 8 -------- libs/nxt-light-sensor/light.ts | 6 +++--- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index a9e8cc0e1..dcd98802f 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -406,14 +406,6 @@ namespace sensors.internal { return getUartStatus(this._port); } - getStatus() { - return getUartStatus(this._port); - } - - getStatus() { - return getUartStatus(this._port); - } - protected _setMode(m: number) { //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`); let v = m | 0; diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index a776cb749..6f61b081d 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -28,14 +28,14 @@ namespace sensors { } _query() { - return this.readValue(); + return [this.readValue()]; } _info() { if (this.mode == NXTLightSensorMode.ReflectedLight || this.mode == NXTLightSensorMode.AmbientLightRaw) { - return `${this._query().toString()}%`; + return [`${this._query()[0].toString()}%`]; } else { - return this._query().toString(); + return [this._query()[0].toString()]; } } From a5f30eb157dce404bc2d8d9762188022d94f162d Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 3 Nov 2023 23:01:33 +0300 Subject: [PATCH 25/53] Update nxtLightSensorView.ts --- sim/visuals/nodes/nxtLightSensorView.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/sim/visuals/nodes/nxtLightSensorView.ts b/sim/visuals/nodes/nxtLightSensorView.ts index e91424f4b..890815f08 100644 --- a/sim/visuals/nodes/nxtLightSensorView.ts +++ b/sim/visuals/nodes/nxtLightSensorView.ts @@ -28,8 +28,6 @@ namespace pxsim.visuals { if (mode == NXTLightSensorMode.ReflectedLightRaw || mode == NXTLightSensorMode.ReflectedLight) { this.updateSensorLightVisual('#eb0c0c'); - } else { - this.updateSensorLightVisual('#ffffff'); } } From 9ec23844dd4f5836df6f9a1b990c185c1c6f8049 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 16:58:03 +0300 Subject: [PATCH 26/53] Update input.ts --- libs/core/input.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index dcd98802f..df076503d 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -255,7 +255,7 @@ namespace sensors.internal { control.dmesg(`new IIC connection at port ${sensorInfo.port} with ID ${sensorInfo.iicid.length}`); } else if (newConn == DAL.CONN_NXT_DUMB) { sensorInfo.devType = inDcm[sensorInfo.port]; - control.dmesg(`new NXT DUMB connection at ${sensorInfo.port} dev type ${sensorInfo.devType}`); + control.dmesg(`new NXT DUMB connection at port ${sensorInfo.port} dev type ${sensorInfo.devType}`); } else if (newConn == DAL.CONN_INPUT_DUMB) { //sensorInfo.devType = inDcm[sensorInfo.port]; // We get the result DEVICE_TYPE_UNKNOWN sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH; // TODO? for now assume touch @@ -372,7 +372,7 @@ namespace sensors.internal { if (this.realmode != this.mode) { control.dmesg(`_setMode p=${this._port} m: ${this.realmode} -> ${v}`); this.realmode = v; - // setAnalogMode() + // setAnalogMode(); } } From cc28b071ee9256ee3e7159cee815622c920e79b6 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 17:05:56 +0300 Subject: [PATCH 27/53] value-range-update These numbers were obtained by testing on 4 sensors. The black value is the sensor aimed at the void, and the light value is aimed at the white Lego brick. --- libs/nxt-light-sensor/light.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 6f61b081d..b22456f58 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -21,10 +21,10 @@ namespace sensors { constructor(port: number) { super(port); - this.darkRefLight = 4096; - this.lightRefLight = 0; - this.darkAmbientLight = 4096; - this.lightAmbientLight = 0; + this.darkRefLight = 3064; + this.lightRefLight = 1016; + this.darkAmbientLight = 3064; + this.lightAmbientLight = 1016; } _query() { From 791515844bb4e70ec2429ec2bd533f2d512c4eab Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 17:12:48 +0300 Subject: [PATCH 28/53] Update light.ts --- libs/nxt-light-sensor/light.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index b22456f58..0dfc8e571 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -32,7 +32,7 @@ namespace sensors { } _info() { - if (this.mode == NXTLightSensorMode.ReflectedLight || this.mode == NXTLightSensorMode.AmbientLightRaw) { + if (this.mode == NXTLightSensorMode.ReflectedLight || this.mode == NXTLightSensorMode.AmbientLight) { return [`${this._query()[0].toString()}%`]; } else { return [this._query()[0].toString()]; From b255a9115f522a84f3e900262830fe04e45bc776 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 18:56:44 +0300 Subject: [PATCH 29/53] some-changes-for-the-mode --- libs/nxt-light-sensor/light.ts | 4 ++-- sim/state/nxtlight.ts | 5 +++-- sim/state/sensor.ts | 1 - sim/visuals/controls/lightWheel.ts | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 0dfc8e571..fea4bfe07 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -100,7 +100,7 @@ namespace sensors { * @param light the value of light, eg: 4096 */ //% help=sensors/nxt-light-sensor/light - //% block="**nxt light sensor** $this| set reflected range dark $dark|light $light" + //% block="**nxt light sensor** $this|set reflected range dark $dark|light $light" //% blockId=setReflectedLightRange //% parts="nxtlightsensor" //% blockNamespace=sensors @@ -121,7 +121,7 @@ namespace sensors { * @param light the value of light, eg: 4096 */ //% help=sensors/nxt-light-sensor/light - //% block="**nxt light sensor** $this| set ambient range dark $dark|light $light" + //% block="**nxt light sensor** $this|set ambient range dark $dark|light $light" //% blockId=setAmbientLightRange //% parts="nxtlightsensor" //% blockNamespace=sensors diff --git a/sim/state/nxtlight.ts b/sim/state/nxtlight.ts index cc65214b2..466dccd03 100644 --- a/sim/state/nxtlight.ts +++ b/sim/state/nxtlight.ts @@ -6,7 +6,8 @@ namespace pxsim { None = -1, ReflectedLightRaw = 0, ReflectedLight = 1, - AmbientLight = 2, + AmbientLightRaw = 2, + AmbientLight = 3, } export class NXTLightSensorNode extends AnalogSensorNode { @@ -34,7 +35,7 @@ namespace pxsim { setMode(mode: number) { this.mode = mode; - if (this.mode == NXTLightSensorMode.ReflectedLightRaw) { + if (this.mode == NXTLightSensorMode.ReflectedLightRaw || this.mode == NXTLightSensorMode.AmbientLightRaw) { this.value = 2048; } else { // Reflection or ambiend light this.value = 50; diff --git a/sim/state/sensor.ts b/sim/state/sensor.ts index 55127f7dd..3aa75659e 100644 --- a/sim/state/sensor.ts +++ b/sim/state/sensor.ts @@ -1,4 +1,3 @@ - namespace pxsim { export class SensorNode extends BaseNode { diff --git a/sim/visuals/controls/lightWheel.ts b/sim/visuals/controls/lightWheel.ts index a7bd5598a..03ff10348 100644 --- a/sim/visuals/controls/lightWheel.ts +++ b/sim/visuals/controls/lightWheel.ts @@ -41,10 +41,10 @@ namespace pxsim.visuals { const node = this.state; const value = node.getValue(); let inverseValue = this.getMaxValue(node.getMode()) - value; - if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw) inverseValue = this.mapValue(inverseValue, 0, 4096, 0, 100); + if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw || node.getMode() == NXTLightSensorMode.AmbientLightRaw) inverseValue = this.mapValue(inverseValue, 0, 4096, 0, 100); svg.setGradientValue(this.colorGradient, inverseValue + "%"); this.reporter.textContent = `${parseFloat((value).toString()).toFixed(0)}`; - if (node.getMode() != NXTLightSensorMode.ReflectedLightRaw) this.reporter.textContent += `%`; + if (node.getMode() == NXTLightSensorMode.ReflectedLight || node.getMode() == NXTLightSensorMode.AmbientLight) this.reporter.textContent += `%`; } updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) { From aaa09d80ef8f861bb6469e99c4438eaef83e6d39 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 20:20:38 +0300 Subject: [PATCH 30/53] LightWheelControl-is-always-activated-except-in-none-mode LightWheelControl is always activated except in NONE mode. Otherwise, the mode was activated immediately when the sensor was turned on in the simulator, when the operating mode had not yet been activated. --- sim/visuals/board.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sim/visuals/board.ts b/sim/visuals/board.ts index bf5896278..541bd25ae 100644 --- a/sim/visuals/board.ts +++ b/sim/visuals/board.ts @@ -270,7 +270,9 @@ namespace pxsim.visuals { } case NodeType.NXTLightSensor: { const state = ev3board().getInputNodes()[port] as NXTLightSensorNode; - view = new LightWheelControl(this.element, this.defs, state, port); + if (state.getMode() != NXTLightSensorMode.None) { + view = new LightWheelControl(this.element, this.defs, state, port); + } break; } case NodeType.MediumMotor: From 70503a10c2e538d07988b9de67b0101f467cecfb Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 21:08:04 +0300 Subject: [PATCH 31/53] 4096-to-4095 Fix range - 0..4095 --- libs/nxt-light-sensor/light.ts | 14 +++++++------- sim/visuals/controls/lightWheel.ts | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index fea4bfe07..8b6386895 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -64,7 +64,7 @@ namespace sensors { } /** - * Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest). For raw reflection values, the range can be from 0 to 4096. + * 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 */ //% help=sensors/nxt-light-sensor/light @@ -97,7 +97,7 @@ namespace sensors { * Set the range of values for determining dark and light in light reflection mode. This must be done so that the reflection mode defines a value in the range from 0 to 100 percent. * @param sensor the color sensor port * @param dark the value of dark, eg: 0 - * @param light the value of light, eg: 4096 + * @param light the value of light, eg: 4095 */ //% help=sensors/nxt-light-sensor/light //% block="**nxt light sensor** $this|set reflected range dark $dark|light $light" @@ -110,15 +110,15 @@ namespace sensors { //% group="Light Sensor" setReflectedLightRange(dark: number, light: number) { if (dark <= light) return; - this.darkRefLight = Math.constrain(dark, 0, 4096); - this.lightRefLight = Math.constrain(light, 0, 4096); + this.darkRefLight = Math.constrain(dark, 0, 4095); + this.lightRefLight = Math.constrain(light, 0, 4095); } /** * Set the value range for dark and light detection in ambient light mode. This must be done so that the ambient light mode determines the value in the range from 0 to 100 percent. * @param sensor the color sensor port * @param dark the value of dark, eg: 0 - * @param light the value of light, eg: 4096 + * @param light the value of light, eg: 4095 */ //% help=sensors/nxt-light-sensor/light //% block="**nxt light sensor** $this|set ambient range dark $dark|light $light" @@ -131,8 +131,8 @@ namespace sensors { //% group="Light Sensor" setAmbientLightRange(dark: number, light: number) { if (dark <= light) return; - this.darkAmbientLight = Math.constrain(dark, 0, 4096); - this.lightAmbientLight = Math.constrain(light, 0, 4096); + this.darkAmbientLight = Math.constrain(dark, 0, 4095); + this.lightAmbientLight = Math.constrain(light, 0, 4095); } /** diff --git a/sim/visuals/controls/lightWheel.ts b/sim/visuals/controls/lightWheel.ts index 03ff10348..55dbbf232 100644 --- a/sim/visuals/controls/lightWheel.ts +++ b/sim/visuals/controls/lightWheel.ts @@ -27,7 +27,7 @@ namespace pxsim.visuals { } private getMaxValue(state: NXTLightSensorMode) { - return (state == NXTLightSensorMode.ReflectedLightRaw ? 4096 : 100); + return (state == NXTLightSensorMode.ReflectedLightRaw ? 4095 : 100); } private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) { From 2f7fac2bb7189d73a99c32d29e2b5725d8acda28 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 6 Nov 2023 21:21:23 +0300 Subject: [PATCH 32/53] enum-NXTLightIntensityMode --- libs/nxt-light-sensor/light.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 8b6386895..0e32ec6fc 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -9,6 +9,17 @@ const enum NXTLightSensorMode { AmbientLight = 3, } +enum NXTLightIntensityMode { + //% block="reflected light (raw)" + ReflectedRaw = NXTLightSensorMode.ReflectedLightRaw, + //% block="ambient light (raw)" + Reflected = NXTLightSensorMode.ReflectedLight, + //% block="ambient light (raw)" + AmbientRaw = NXTLightSensorMode.AmbientLightRaw, + //% block="ambient light" + Ambient = NXTLightSensorMode.AmbientLight +} + namespace sensors { //% fixedInstances @@ -76,17 +87,17 @@ namespace sensors { //% weight=99 blockGap=8 //% subcategory="NXT" //% group="Light Sensor" - light(mode: NXTLightSensorMode) { + light(mode: NXTLightIntensityMode) { this.setMode(mode); this.poke(); switch (mode) { - case NXTLightSensorMode.ReflectedLightRaw: + case NXTLightIntensityMode.ReflectedRaw: return this.reflectetLightRaw(); - case NXTLightSensorMode.ReflectedLight: + case NXTLightIntensityMode.Reflected: return this.reflectetLight(); - case NXTLightSensorMode.AmbientLightRaw: + case NXTLightIntensityMode.AmbientRaw: return this.ambientLightRaw(); - case NXTLightSensorMode.AmbientLight: + case NXTLightIntensityMode.Ambient: return this.ambientLight(); default: return 0; From 161f44dcf003c0f0bf039326d4ac112d239bbaf4 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 10 Nov 2023 20:52:46 +0300 Subject: [PATCH 33/53] light-sensor-svg-updage --- sim/visuals/assets/NXT Light Sensor.svg | 192 ++++++++++++++++-------- 1 file changed, 131 insertions(+), 61 deletions(-) diff --git a/sim/visuals/assets/NXT Light Sensor.svg b/sim/visuals/assets/NXT Light Sensor.svg index 76fa3a4be..f39772bd0 100644 --- a/sim/visuals/assets/NXT Light Sensor.svg +++ b/sim/visuals/assets/NXT Light Sensor.svg @@ -1,17 +1,39 @@ - - - + + + + - + + - + @@ -20,62 +42,110 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - From a688fea13ab67400b9815ef434e6f0ffb1beb8bc Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 10 Nov 2023 21:37:44 +0300 Subject: [PATCH 34/53] update-light-sensor-ts --- sim/visuals/assets/NXT Light Sensor.ts | 176 +++++++++++++++++-------- 1 file changed, 123 insertions(+), 53 deletions(-) diff --git a/sim/visuals/assets/NXT Light Sensor.ts b/sim/visuals/assets/NXT Light Sensor.ts index 71b24cdad..610c2ab29 100644 --- a/sim/visuals/assets/NXT Light Sensor.ts +++ b/sim/visuals/assets/NXT Light Sensor.ts @@ -1,80 +1,150 @@ namespace pxsim { export const NXT_LIGHT_SENSOR_SVG = ` - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - + + + + + + + - + + + + + + + + + + + - + + + + + + + + + - + - + + + + + + + + + - + - - - - - - - - - - - - + - - - - + + + + + + + - + + `; } \ No newline at end of file From 43d9e1f48bc05c54f233a8b1a571bc741c5bdf5b Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Sun, 12 Nov 2023 18:07:28 +0300 Subject: [PATCH 35/53] ambient-modes-set-invisible Disable the visibility of ambient blocks so that they are not used, because There is no implementation of disabling LED lighting. --- libs/nxt-light-sensor/light.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 0e32ec6fc..11dbe60de 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -3,9 +3,9 @@ const enum NXTLightSensorMode { ReflectedLightRaw = 0, //% block="reflected light" ReflectedLight = 1, - //% block="ambient light (raw)" + //% block="ambient light (raw)" blockHidden=true AmbientLightRaw = 2, - //% block="ambient light" + //% block="ambient light" blockHidden=true AmbientLight = 3, } @@ -14,9 +14,9 @@ enum NXTLightIntensityMode { ReflectedRaw = NXTLightSensorMode.ReflectedLightRaw, //% block="ambient light (raw)" Reflected = NXTLightSensorMode.ReflectedLight, - //% block="ambient light (raw)" + //% block="ambient light (raw)" blockHidden=true AmbientRaw = NXTLightSensorMode.AmbientLightRaw, - //% block="ambient light" + //% block="ambient light" blockHidden=true Ambient = NXTLightSensorMode.AmbientLight } @@ -25,6 +25,8 @@ namespace sensors { //% fixedInstances export class NXTLightSensor extends internal.AnalogSensor { + // https://github.com/mindboards/ev3sources-xtended/blob/master/ev3sources/lms2012/lms2012/Linux_AM1808/sys/settings/typedata.rcf + darkRefLight: number; lightRefLight: number; darkAmbientLight: number; @@ -140,6 +142,7 @@ namespace sensors { //% weight=88 blockGap=8 //% subcategory="NXT" //% group="Light Sensor" + //% blockHidden=true setAmbientLightRange(dark: number, light: number) { if (dark <= light) return; this.darkAmbientLight = Math.constrain(dark, 0, 4095); From 79b7d2a397e18215b492912562acebf1ef3ed0c2 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Sun, 12 Nov 2023 19:12:43 +0300 Subject: [PATCH 36/53] range-from-lego-sources https://github.com/mindboards/ev3sources-xtended/blob/master/ev3sources/lms2012/lms2012/Linux_AM1808/sys/settings/typedata.rcf --- libs/nxt-light-sensor/light.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 11dbe60de..9297d8bfc 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -34,10 +34,10 @@ namespace sensors { constructor(port: number) { super(port); - this.darkRefLight = 3064; - this.lightRefLight = 1016; - this.darkAmbientLight = 3064; - this.lightAmbientLight = 1016; + this.darkRefLight = 3372; + this.lightRefLight = 445; + this.darkAmbientLight = 3411; + this.lightAmbientLight = 633; } _query() { From 42d8eb04b1b35adf7c3d8eb6c94374ae71b3cd80 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Sun, 12 Nov 2023 22:55:56 +0300 Subject: [PATCH 37/53] sim-fix-for-sensor Solving the problem that the field in the simulator and the value from the sensor on the screen could be different. And one more thing... --- sim/visuals/controls/lightWheel.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sim/visuals/controls/lightWheel.ts b/sim/visuals/controls/lightWheel.ts index 55dbbf232..0d910b0ed 100644 --- a/sim/visuals/controls/lightWheel.ts +++ b/sim/visuals/controls/lightWheel.ts @@ -27,7 +27,7 @@ namespace pxsim.visuals { } private getMaxValue(state: NXTLightSensorMode) { - return (state == NXTLightSensorMode.ReflectedLightRaw ? 4095 : 100); + return (state == NXTLightSensorMode.ReflectedLightRaw || state == NXTLightSensorMode.AmbientLightRaw ? 4095 : 100); } private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) { @@ -41,10 +41,14 @@ namespace pxsim.visuals { const node = this.state; const value = node.getValue(); let inverseValue = this.getMaxValue(node.getMode()) - value; - if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw || node.getMode() == NXTLightSensorMode.AmbientLightRaw) inverseValue = this.mapValue(inverseValue, 0, 4096, 0, 100); + if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw || node.getMode() == NXTLightSensorMode.AmbientLightRaw) { + inverseValue = this.mapValue(inverseValue, 0, 4095, 0, 100); + } svg.setGradientValue(this.colorGradient, inverseValue + "%"); - this.reporter.textContent = `${parseFloat((value).toString()).toFixed(0)}`; - if (node.getMode() == NXTLightSensorMode.ReflectedLight || node.getMode() == NXTLightSensorMode.AmbientLight) this.reporter.textContent += `%`; + this.reporter.textContent = `${Math.floor(parseFloat(value.toString()))}`; + if (node.getMode() == NXTLightSensorMode.ReflectedLight || node.getMode() == NXTLightSensorMode.AmbientLight) { + this.reporter.textContent += `%`; + } } updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) { From 261fd0451036358b6d052df794340f51ad645c81 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Sun, 12 Nov 2023 23:02:56 +0300 Subject: [PATCH 38/53] query-update-and-add-treshold-blocks --- libs/nxt-light-sensor/light.ts | 114 ++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 8 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 9297d8bfc..5b7484d8c 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -3,20 +3,20 @@ const enum NXTLightSensorMode { ReflectedLightRaw = 0, //% block="reflected light" ReflectedLight = 1, - //% block="ambient light (raw)" blockHidden=true + //% block="ambient light (raw)" AmbientLightRaw = 2, - //% block="ambient light" blockHidden=true + //% block="ambient light" AmbientLight = 3, } enum NXTLightIntensityMode { //% block="reflected light (raw)" ReflectedRaw = NXTLightSensorMode.ReflectedLightRaw, - //% block="ambient light (raw)" + //% block="reflected light" Reflected = NXTLightSensorMode.ReflectedLight, - //% block="ambient light (raw)" blockHidden=true + //% block="ambient light (raw)" AmbientRaw = NXTLightSensorMode.AmbientLightRaw, - //% block="ambient light" blockHidden=true + //% block="ambient light" Ambient = NXTLightSensorMode.AmbientLight } @@ -27,6 +27,7 @@ namespace sensors { // https://github.com/mindboards/ev3sources-xtended/blob/master/ev3sources/lms2012/lms2012/Linux_AM1808/sys/settings/typedata.rcf + thresholdDetector: sensors.ThresholdDetector; darkRefLight: number; lightRefLight: number; darkAmbientLight: number; @@ -34,6 +35,7 @@ namespace sensors { constructor(port: number) { super(port); + this.thresholdDetector = new sensors.ThresholdDetector(this.id()); this.darkRefLight = 3372; this.lightRefLight = 445; this.darkAmbientLight = 3411; @@ -41,7 +43,16 @@ namespace sensors { } _query() { - return [this.readValue()]; + if (this.mode == NXTLightSensorMode.ReflectedLight) { + return [this.reflectetLight()]; + } else if (this.mode == NXTLightSensorMode.AmbientLight) { + return [this.ambientLight()]; + } else if (this.mode == NXTLightSensorMode.ReflectedLightRaw) { + return [this.reflectetLightRaw()]; + } else if (this.mode == NXTLightSensorMode.AmbientLightRaw) { + return [this.ambientLightRaw()]; + } + return [0]; } _info() { @@ -181,7 +192,7 @@ namespace sensors { //% reflectetLight() { let reflectedVal = Math.map(this.readValue(), this.darkRefLight, this.lightRefLight, 0, 100); - reflectedVal = Math.constrain(reflectedVal, 0, 100); + reflectedVal = Math.round(Math.constrain(reflectedVal, 0, 100)); return reflectedVal; } @@ -191,9 +202,96 @@ namespace sensors { //% ambientLight() { let ambientVal = Math.map(this.readValue(), this.darkAmbientLight, this.lightAmbientLight, 0, 100); - ambientVal = Math.constrain(ambientVal, 0, 100); + ambientVal = Math.round(Math.constrain(ambientVal, 0, 100)); return ambientVal; } + + /** + * Set a threshold value + * @param condition the dark or bright light condition + * @param value the value threshold, eg: 10 + */ + //% help=sensors/nxt-light-sensor/set-threshold + //% blockId=nxtLightSetThreshold + //% block="set **nxt light sensor** %this|%condition|to %value" + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% this.fieldEditor="ports" + //% value.min=0 value.max=100 + //% weight=90 blockGap=8 + //% subcategory="NXT" + //% group="Light Sensor" + setThreshold(condition: Light, value: number) { + // threshold is used in ambient or reflected modes + if (this.mode != NXTLightIntensityMode.Ambient && this.mode != NXTLightIntensityMode.Reflected) { + this.setMode(NXTLightSensorMode.ReflectedLight); + } + if (condition == Light.Dark) { + this.thresholdDetector.setLowThreshold(value) + } else { + this.thresholdDetector.setHighThreshold(value); + } + } + + /** + * Registers code to run when the ambient light changes. + * @param condition the light condition + * @param handler the code to run when detected + */ + //% help=sensors/nxt-light-sensor/on-light-detected + //% block="on **nxt light sensor** %this|%mode|%condition" + //% blockId=nxtLightOnLightDetected + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% this.fieldEditor="ports" + //% weight=89 blockGap=12 + //% subcategory="NXT" + //% group="Light Sensor" + onLightDetected(mode: NXTLightIntensityMode, condition: Light, handler: () => void) { + this.setMode(mode); + control.onEvent(this._id, condition, handler); + } + + /** + * Wait for the given color to be detected + * @param color the color to detect + */ + //% help=sensors/nxt-light-sensor/pause-until-light-detected + //% block="pause until **nxt light sensor** %this|%mode|%condition" + //% blockId=nxtLightPauseUntilLightDetected + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% this.fieldEditor="ports" + //% weight=88 blockGap=8 + //% subcategory="NXT" + //% group="Light Sensor" + pauseUntilLightDetected(mode: NXTLightIntensityMode, condition: Light) { + this.setMode(mode); + if (this.thresholdDetector.state != condition) { + control.waitForEvent(this._id, condition); + } + } + + /** + * Get a threshold value + * @param condition the light condition + */ + //% help=sensors/nxt-light-sensor/threshold + //% blockId=nxtLightGetThreshold + //% parts="nxtlightsensor" + //% blockNamespace=sensors + //% block="**nxt light sensor** %this|%condition" + //% this.fieldEditor="ports" + //% weight=89 + //% subcategory="NXT" + //% group="Light Sensor" + threshold(condition: Light): number { + // threshold is used in ambient or reflected modes + if (this.mode != NXTLightIntensityMode.Ambient && this.mode != NXTLightIntensityMode.Reflected) { + this.setMode(NXTLightSensorMode.ReflectedLight); + } + return this.thresholdDetector.threshold(condition); + } } //% whenUsed block="1" weight=95 fixedInstance jres=icons.port1 From d507dce3a195724e25b073039664c552d6c4ca63 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 23 Nov 2023 11:31:30 +0300 Subject: [PATCH 39/53] test-upd --- libs/nxt-light-sensor/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/nxt-light-sensor/test.ts b/libs/nxt-light-sensor/test.ts index 627390ddc..27e422456 100644 --- a/libs/nxt-light-sensor/test.ts +++ b/libs/nxt-light-sensor/test.ts @@ -3,7 +3,7 @@ sensors.nxtLight1.setReflectedLightRange(2600, 1600); forever(function () { control.timer1.reset(); let nls1 = sensors.nxtLight1.reflectetLightRaw(); - let nls2 = sensors.nxtLight1.light(NXTLightSensorMode.ReflectedLight); + let nls2 = sensors.nxtLight1.light(NXTLightIntensityMode.ReflectedRaw); let rrlcs = sensors.color2.light(LightIntensityMode.ReflectedRaw); let rrrcs = sensors.color3.light(LightIntensityMode.ReflectedRaw); let t = sensors.touch4.isPressed() ? 1 : 0; From 368d3a3a85bd6d94db5274b081e79a5163c7dc34 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 27 Nov 2023 14:28:16 +0300 Subject: [PATCH 40/53] Update lightWheel.ts --- sim/visuals/controls/lightWheel.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sim/visuals/controls/lightWheel.ts b/sim/visuals/controls/lightWheel.ts index 0d910b0ed..3d218271d 100644 --- a/sim/visuals/controls/lightWheel.ts +++ b/sim/visuals/controls/lightWheel.ts @@ -1,6 +1,7 @@ namespace pxsim.visuals { export class LightWheelControl extends ControlView { + private group: SVGGElement; private colorGradient: SVGLinearGradientElement; private reporter: SVGTextElement; @@ -45,9 +46,11 @@ namespace pxsim.visuals { inverseValue = this.mapValue(inverseValue, 0, 4095, 0, 100); } svg.setGradientValue(this.colorGradient, inverseValue + "%"); - this.reporter.textContent = `${Math.floor(parseFloat(value.toString()))}`; - if (node.getMode() == NXTLightSensorMode.ReflectedLight || node.getMode() == NXTLightSensorMode.AmbientLight) { - this.reporter.textContent += `%`; + //console.log(`node.getMode(): ${node.getMode()}`); + if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw || node.getMode() == NXTLightSensorMode.AmbientLightRaw) { + this.reporter.textContent = `${Math.floor(parseFloat(value.toString()))}`; + } else { + this.reporter.textContent = `${this.mapValue(Math.floor(parseFloat(value.toString())), 0, 4095, 0, 100)}%`; } } From 229af21a694060932fd4855beb80e39a9fd7c9a9 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 8 Dec 2023 12:01:03 +0300 Subject: [PATCH 41/53] changes-light-sensor-for-sim The changes are aimed at supporting reflection and lighting modes, not raw modes. --- sim/state/nxtlight.ts | 15 +++++++++------ sim/visuals/controls/lightWheel.ts | 31 +++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/sim/state/nxtlight.ts b/sim/state/nxtlight.ts index 466dccd03..fce1d9d7e 100644 --- a/sim/state/nxtlight.ts +++ b/sim/state/nxtlight.ts @@ -11,9 +11,14 @@ namespace pxsim { } export class NXTLightSensorNode extends AnalogSensorNode { + id = NodeType.NXTLightSensor; private value: number = 0; + public darkReflectedLight: number = 3372; + public brightReflectedLight: number = 445; + public darkAmbientLight: number = 3411; + public brightAmbientLight: number = 633; constructor(port: number) { super(port); @@ -24,7 +29,7 @@ namespace pxsim { return DAL.DEVICE_TYPE_NXT_LIGHT; } - setColor(value: number) { + setValue(value: number) { this.value = value; this.setChangedState(); } @@ -35,11 +40,9 @@ namespace pxsim { setMode(mode: number) { this.mode = mode; - if (this.mode == NXTLightSensorMode.ReflectedLightRaw || this.mode == NXTLightSensorMode.AmbientLightRaw) { - this.value = 2048; - } else { // Reflection or ambiend light - this.value = 50; - } + if (this.mode == NXTLightSensorMode.ReflectedLight) this.value = 1908; + else if (this.mode == NXTLightSensorMode.AmbientLight) this.value = 2022; + else this.value = 2048; this.changed = true; this.modeChanged = true; } diff --git a/sim/visuals/controls/lightWheel.ts b/sim/visuals/controls/lightWheel.ts index 3d218271d..a2c3585eb 100644 --- a/sim/visuals/controls/lightWheel.ts +++ b/sim/visuals/controls/lightWheel.ts @@ -27,8 +27,21 @@ namespace pxsim.visuals { return 131; } - private getMaxValue(state: NXTLightSensorMode) { - return (state == NXTLightSensorMode.ReflectedLightRaw || state == NXTLightSensorMode.AmbientLightRaw ? 4095 : 100); + private getMinValue(state: NXTLightSensorNode) { + if (state.getMode() == NXTLightSensorMode.ReflectedLight) return state.brightReflectedLight; + else if (state.getMode() == NXTLightSensorMode.AmbientLight) return state.brightAmbientLight; + return 0; + } + + private getMaxValue(state: NXTLightSensorNode) { + if (state.getMode() == NXTLightSensorMode.ReflectedLightRaw || state.getMode() == NXTLightSensorMode.AmbientLightRaw) { + return 4095; + } else if (state.getMode() == NXTLightSensorMode.ReflectedLight) { + return state.darkReflectedLight; + } else if (state.getMode() == NXTLightSensorMode.AmbientLight) { + return state.darkAmbientLight; + } + return 100; } private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) { @@ -41,26 +54,30 @@ namespace pxsim.visuals { } const node = this.state; const value = node.getValue(); - let inverseValue = this.getMaxValue(node.getMode()) - value; + let inverseValue = this.getMaxValue(node) - value + this.getMinValue(node); if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw || node.getMode() == NXTLightSensorMode.AmbientLightRaw) { inverseValue = this.mapValue(inverseValue, 0, 4095, 0, 100); + } else if (node.getMode() == NXTLightSensorMode.ReflectedLight) { + inverseValue = this.mapValue(inverseValue, node.darkReflectedLight, node.brightReflectedLight, 0, 100); + } else if (node.getMode() == NXTLightSensorMode.AmbientLight) { + inverseValue = this.mapValue(inverseValue, node.darkAmbientLight, node.brightAmbientLight, 0, 100); } svg.setGradientValue(this.colorGradient, inverseValue + "%"); - //console.log(`node.getMode(): ${node.getMode()}`); if (node.getMode() == NXTLightSensorMode.ReflectedLightRaw || node.getMode() == NXTLightSensorMode.AmbientLightRaw) { this.reporter.textContent = `${Math.floor(parseFloat(value.toString()))}`; } else { - this.reporter.textContent = `${this.mapValue(Math.floor(parseFloat(value.toString())), 0, 4095, 0, 100)}%`; + this.reporter.textContent = `${Math.floor(this.mapValue(parseFloat(value.toString()), this.getMaxValue(node), this.getMinValue(node), 0, 100))}%`; } } updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) { + const state = this.state; let cur = svg.cursorPoint(pt, parent, ev); const bBox = this.rect.getBoundingClientRect(); const height = bBox.height; let t = Math.max(0, Math.min(1, (height + bBox.top / this.scaleFactor - cur.y / this.scaleFactor) / height)); - const state = this.state; - state.setColor(t * this.getMaxValue(state.getMode())); + if (state.getMode() == NXTLightSensorMode.ReflectedLight || state.getMode() == NXTLightSensorMode.AmbientLight) t = 1 - t; + state.setValue(this.getMinValue(state) + t * (this.getMaxValue(state) - this.getMinValue(state))); } getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) { From b24c6ba98282e4d5dfbc33cdc568bf6e67d29665 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 8 Dec 2023 12:15:40 +0300 Subject: [PATCH 42/53] deviceType-analog-sens-class --- libs/core/input.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index df076503d..61fae72bd 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -372,7 +372,7 @@ namespace sensors.internal { if (this.realmode != this.mode) { control.dmesg(`_setMode p=${this._port} m: ${this.realmode} -> ${v}`); this.realmode = v; - // setAnalogMode(); + //setAnalogMode(this._port, this._deviceType(), this.mode); } } @@ -385,6 +385,10 @@ namespace sensors.internal { if (!this.isActive()) return 0; return analogMM.getNumber(NumberFormat.Int16LE, AnalogOff.InPin6 + 2 * this._port); } + + _deviceType() { + return DAL.DEVICE_TYPE_UNKNOWN; + } } export class UartSensor extends Sensor { @@ -611,11 +615,11 @@ namespace sensors.internal { export function setIICMode(port: number, type: number, mode: number) { if (port < 0) return; - control.dmesg(`iic set type ${type} mode ${mode} at ${port}`); - devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, DAL.CONN_NXT_IIC) - devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, type) - devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode) - IICMM.ioctl(IO.IIC_SET_CONN, devcon) + control.dmesg(`iic set type ${type} mode ${mode} at port ${port}`); + devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, DAL.CONN_NXT_IIC); + devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, type); + devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode); + IICMM.ioctl(IO.IIC_SET_CONN, devcon); } export function transactionIIC(port: number, deviceAddress: number, writeBuf: number[], readLen: number) { From a2eb4623b1810a2ace89499bcd546ab0e9f8bd50 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Fri, 8 Dec 2023 12:16:59 +0300 Subject: [PATCH 43/53] Update light.ts --- libs/nxt-light-sensor/light.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 5b7484d8c..023035df9 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -27,19 +27,15 @@ namespace sensors { // https://github.com/mindboards/ev3sources-xtended/blob/master/ev3sources/lms2012/lms2012/Linux_AM1808/sys/settings/typedata.rcf - thresholdDetector: sensors.ThresholdDetector; - darkRefLight: number; - lightRefLight: number; - darkAmbientLight: number; - lightAmbientLight: number; + private thresholdDetector: sensors.ThresholdDetector; + private darkReflectedLight: number = 3372; + private brightReflectedLight: number = 445; + private darkAmbientLight: number = 3411; + private brightAmbientLight: number = 633; constructor(port: number) { super(port); this.thresholdDetector = new sensors.ThresholdDetector(this.id()); - this.darkRefLight = 3372; - this.lightRefLight = 445; - this.darkAmbientLight = 3411; - this.lightAmbientLight = 633; } _query() { @@ -134,8 +130,8 @@ namespace sensors { //% group="Light Sensor" setReflectedLightRange(dark: number, light: number) { if (dark <= light) return; - this.darkRefLight = Math.constrain(dark, 0, 4095); - this.lightRefLight = Math.constrain(light, 0, 4095); + this.darkReflectedLight = Math.constrain(dark, 0, 4095); + this.brightReflectedLight = Math.constrain(light, 0, 4095); } /** @@ -157,7 +153,7 @@ namespace sensors { setAmbientLightRange(dark: number, light: number) { if (dark <= light) return; this.darkAmbientLight = Math.constrain(dark, 0, 4095); - this.lightAmbientLight = Math.constrain(light, 0, 4095); + this.brightAmbientLight = Math.constrain(light, 0, 4095); } /** @@ -191,7 +187,7 @@ namespace sensors { */ //% reflectetLight() { - let reflectedVal = Math.map(this.readValue(), this.darkRefLight, this.lightRefLight, 0, 100); + let reflectedVal = Math.map(this.readValue(), this.brightReflectedLight, this.darkReflectedLight, 0, 100); reflectedVal = Math.round(Math.constrain(reflectedVal, 0, 100)); return reflectedVal; } @@ -201,7 +197,7 @@ namespace sensors { */ //% ambientLight() { - let ambientVal = Math.map(this.readValue(), this.darkAmbientLight, this.lightAmbientLight, 0, 100); + let ambientVal = Math.map(this.readValue(), this.brightAmbientLight, this.darkAmbientLight, 0, 100); ambientVal = Math.round(Math.constrain(ambientVal, 0, 100)); return ambientVal; } From 08c6c8676c2aa336a228ec88aa7e8d75e05aef0e Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 11 Dec 2023 14:21:36 +0300 Subject: [PATCH 44/53] fix-range-for-refLight-and-ambLight --- libs/nxt-light-sensor/light.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 023035df9..c80b4af21 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -187,7 +187,7 @@ namespace sensors { */ //% reflectetLight() { - let reflectedVal = Math.map(this.readValue(), this.brightReflectedLight, this.darkReflectedLight, 0, 100); + let reflectedVal = Math.map(this.readValue(), this.darkReflectedLight, this.brightReflectedLight, 0, 100); reflectedVal = Math.round(Math.constrain(reflectedVal, 0, 100)); return reflectedVal; } @@ -197,7 +197,7 @@ namespace sensors { */ //% ambientLight() { - let ambientVal = Math.map(this.readValue(), this.brightAmbientLight, this.darkAmbientLight, 0, 100); + let ambientVal = Math.map(this.readValue(), this.darkAmbientLight, this.brightAmbientLight, 0, 100); ambientVal = Math.round(Math.constrain(ambientVal, 0, 100)); return ambientVal; } From 8cbe42972877f207cb6b5905fef4603415e0fb96 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Mon, 11 Dec 2023 14:25:18 +0300 Subject: [PATCH 45/53] export-enum-DevConOff --- sim/state/uart.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sim/state/uart.ts b/sim/state/uart.ts index 820097a8c..c9a99683f 100644 --- a/sim/state/uart.ts +++ b/sim/state/uart.ts @@ -39,7 +39,7 @@ namespace pxsim { } - enum DevConOff { + export enum DevConOff { Connection = 0, // int8[4] Type = 4, // int8[4] Mode = 8, // int8[4] @@ -106,13 +106,13 @@ namespace pxsim { } }, read: buf => { - let v = "vSIM" + let v = "vSIM"; // for (let i = 0; i < buf.data.length; ++i) // buf.data[i] = v.charCodeAt(i) || 0 - return buf.data.length + return buf.data.length; }, write: buf => { - return 2 + return 2; }, ioctl: (id, buf) => { switch (id) { From 18b3f88e33cba034880171ae49da236094242cda Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 00:34:28 +0300 Subject: [PATCH 46/53] bug-fix-from-previous-version --- libs/touch-sensor/touch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/touch-sensor/touch.ts b/libs/touch-sensor/touch.ts index 8c3fb9798..be0fd7b55 100644 --- a/libs/touch-sensor/touch.ts +++ b/libs/touch-sensor/touch.ts @@ -12,11 +12,11 @@ namespace sensors { } _query() { - return this._readPin6() > 2500 ? [1] : [0]; + return [this._readPin6() > 2500 ? 1 : 0]; } _info() { - return this._query() ? ["pres"] : ["rel"]; + return [this._query()[0] ? "pres" : "rel"]; } _update(prev: number, curr: number) { From e6e2e6a58676074e48ff8b91b92d034ebf16131d Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 00:38:50 +0300 Subject: [PATCH 47/53] sensor-activation Setting the mode so that the sensor starts working in the simulator. Otherwise, it, like a touch sensor, does not create view control, because in the touch sensor this was not necessary. Without this change, lightView would only activate if a sensor with type uart was used in the code. --- libs/core/input.ts | 21 ++++++++++++++++++--- sim/state/analog.ts | 19 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/libs/core/input.ts b/libs/core/input.ts index 61fae72bd..b25abe34f 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -356,8 +356,9 @@ namespace sensors.internal { } export class AnalogSensor extends Sensor { - protected mode: number // the mode user asked for - protected realmode: number // the mode the hardware is in + + protected mode: number; // the mode user asked for + protected realmode: number; constructor(port: number) { super(port); @@ -365,6 +366,11 @@ namespace sensors.internal { this.realmode = 0; } + _activated() { + this.realmode = 0; + this._setMode(this.mode); + } + protected _setMode(m: number) { let v = m | 0; this.mode = v; @@ -372,7 +378,7 @@ namespace sensors.internal { if (this.realmode != this.mode) { control.dmesg(`_setMode p=${this._port} m: ${this.realmode} -> ${v}`); this.realmode = v; - //setAnalogMode(this._port, this._deviceType(), this.mode); + setAnalogMode(this._port, this._deviceType(), this.mode); } } @@ -613,6 +619,15 @@ namespace sensors.internal { UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index + off) } + function setAnalogMode(port: number, type: number, mode: number) { + if (port < 0) return; + control.dmesg(`analog set type ${type} mode ${mode} at port ${port}`); + devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, DAL.CONN_NXT_DUMB); + devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, type); + devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode); + analogMM.ioctl(0, devcon); + } + export function setIICMode(port: number, type: number, mode: number) { if (port < 0) return; control.dmesg(`iic set type ${type} mode ${mode} at port ${port}`); diff --git a/sim/state/analog.ts b/sim/state/analog.ts index eeafca96b..529312ef0 100644 --- a/sim/state/analog.ts +++ b/sim/state/analog.ts @@ -25,7 +25,7 @@ namespace pxsim { export class EV3AnalogState { constructor() { - let data = new Uint8Array(5172) + let data = new Uint8Array(5172); MMapMethods.register("/dev/lms_analog", { data, beforeMemRead: () => { @@ -46,15 +46,24 @@ namespace pxsim { } }, read: buf => { - let v = "vSIM" + let v = "vSIM"; for (let i = 0; i < buf.data.length; ++i) - buf.data[i] = v.charCodeAt(i) || 0 - return buf.data.length + buf.data[i] = v.charCodeAt(i) || 0; + return buf.data.length; }, write: buf => { - return 2 + return 2; }, ioctl: (id, buf) => { + //console.log("ioctl: " + id); + for (let port = 0; port < DAL.NUM_INPUTS; port++) { + const connection = buf.data[DevConOff.Connection + port]; + const type = buf.data[DevConOff.Type + port]; + const mode = buf.data[DevConOff.Mode + port]; + //console.log(`${port}, mode: ${mode}`); + const node = ev3board().getInputNodes()[port]; + if (node) node.setMode(mode); + } return 2; } }) From a5bd2c1a517d10c176e847f028eab1ccf81e4d80 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 00:51:25 +0300 Subject: [PATCH 48/53] Update light.ts Support blocks have been removed, which may be available later. The ambient mode has been removed, because There is no implementation of turning off the backlight LED. Now it is always on. --- libs/nxt-light-sensor/light.ts | 100 +++------------------------------ 1 file changed, 8 insertions(+), 92 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index c80b4af21..6d500cdbf 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -15,8 +15,10 @@ enum NXTLightIntensityMode { //% block="reflected light" Reflected = NXTLightSensorMode.ReflectedLight, //% block="ambient light (raw)" + //% blockHidden=true AmbientRaw = NXTLightSensorMode.AmbientLightRaw, //% block="ambient light" + //% blockHidden=true Ambient = NXTLightSensorMode.AmbientLight } @@ -128,10 +130,10 @@ namespace sensors { //% weight=89 blockGap=8 //% subcategory="NXT" //% group="Light Sensor" - setReflectedLightRange(dark: number, light: number) { - if (dark <= light) return; + setReflectedLightRange(dark: number, bright: number) { + if (dark <= bright) return; this.darkReflectedLight = Math.constrain(dark, 0, 4095); - this.brightReflectedLight = Math.constrain(light, 0, 4095); + this.brightReflectedLight = Math.constrain(bright, 0, 4095); } /** @@ -150,10 +152,10 @@ namespace sensors { //% subcategory="NXT" //% group="Light Sensor" //% blockHidden=true - setAmbientLightRange(dark: number, light: number) { - if (dark <= light) return; + setAmbientLightRange(dark: number, bright: number) { + if (dark <= bright) return; this.darkAmbientLight = Math.constrain(dark, 0, 4095); - this.brightAmbientLight = Math.constrain(light, 0, 4095); + this.brightAmbientLight = Math.constrain(bright, 0, 4095); } /** @@ -202,92 +204,6 @@ namespace sensors { return ambientVal; } - /** - * Set a threshold value - * @param condition the dark or bright light condition - * @param value the value threshold, eg: 10 - */ - //% help=sensors/nxt-light-sensor/set-threshold - //% blockId=nxtLightSetThreshold - //% block="set **nxt light sensor** %this|%condition|to %value" - //% parts="nxtlightsensor" - //% blockNamespace=sensors - //% this.fieldEditor="ports" - //% value.min=0 value.max=100 - //% weight=90 blockGap=8 - //% subcategory="NXT" - //% group="Light Sensor" - setThreshold(condition: Light, value: number) { - // threshold is used in ambient or reflected modes - if (this.mode != NXTLightIntensityMode.Ambient && this.mode != NXTLightIntensityMode.Reflected) { - this.setMode(NXTLightSensorMode.ReflectedLight); - } - if (condition == Light.Dark) { - this.thresholdDetector.setLowThreshold(value) - } else { - this.thresholdDetector.setHighThreshold(value); - } - } - - /** - * Registers code to run when the ambient light changes. - * @param condition the light condition - * @param handler the code to run when detected - */ - //% help=sensors/nxt-light-sensor/on-light-detected - //% block="on **nxt light sensor** %this|%mode|%condition" - //% blockId=nxtLightOnLightDetected - //% parts="nxtlightsensor" - //% blockNamespace=sensors - //% this.fieldEditor="ports" - //% weight=89 blockGap=12 - //% subcategory="NXT" - //% group="Light Sensor" - onLightDetected(mode: NXTLightIntensityMode, condition: Light, handler: () => void) { - this.setMode(mode); - control.onEvent(this._id, condition, handler); - } - - /** - * Wait for the given color to be detected - * @param color the color to detect - */ - //% help=sensors/nxt-light-sensor/pause-until-light-detected - //% block="pause until **nxt light sensor** %this|%mode|%condition" - //% blockId=nxtLightPauseUntilLightDetected - //% parts="nxtlightsensor" - //% blockNamespace=sensors - //% this.fieldEditor="ports" - //% weight=88 blockGap=8 - //% subcategory="NXT" - //% group="Light Sensor" - pauseUntilLightDetected(mode: NXTLightIntensityMode, condition: Light) { - this.setMode(mode); - if (this.thresholdDetector.state != condition) { - control.waitForEvent(this._id, condition); - } - } - - /** - * Get a threshold value - * @param condition the light condition - */ - //% help=sensors/nxt-light-sensor/threshold - //% blockId=nxtLightGetThreshold - //% parts="nxtlightsensor" - //% blockNamespace=sensors - //% block="**nxt light sensor** %this|%condition" - //% this.fieldEditor="ports" - //% weight=89 - //% subcategory="NXT" - //% group="Light Sensor" - threshold(condition: Light): number { - // threshold is used in ambient or reflected modes - if (this.mode != NXTLightIntensityMode.Ambient && this.mode != NXTLightIntensityMode.Reflected) { - this.setMode(NXTLightSensorMode.ReflectedLight); - } - return this.thresholdDetector.threshold(condition); - } } //% whenUsed block="1" weight=95 fixedInstance jres=icons.port1 From 085e511ec2e550e9e28b260c015b068fb6aaa838 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 00:52:36 +0300 Subject: [PATCH 49/53] Update light.ts --- libs/nxt-light-sensor/light.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index 6d500cdbf..dbdfb11e6 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -121,7 +121,7 @@ namespace sensors { * @param dark the value of dark, eg: 0 * @param light the value of light, eg: 4095 */ - //% help=sensors/nxt-light-sensor/light + //% help=sensors/nxt-light-sensor/set-reflected-range //% block="**nxt light sensor** $this|set reflected range dark $dark|light $light" //% blockId=setReflectedLightRange //% parts="nxtlightsensor" @@ -142,7 +142,7 @@ namespace sensors { * @param dark the value of dark, eg: 0 * @param light the value of light, eg: 4095 */ - //% help=sensors/nxt-light-sensor/light + //% help=sensors/nxt-light-sensor/set-ambient-range //% block="**nxt light sensor** $this|set ambient range dark $dark|light $light" //% blockId=setAmbientLightRange //% parts="nxtlightsensor" From c615b734aa402c4c480bf9a6a1e51eee97a076ab Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 01:00:13 +0300 Subject: [PATCH 50/53] lib-not-include-by-defl Make sure that the library is not included by default. It will need to be enabled via extensions. --- libs/ev3/pxt.json | 3 +-- libs/nxt-light-sensor/pxt.json | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/libs/ev3/pxt.json b/libs/ev3/pxt.json index 115501897..a54af080a 100644 --- a/libs/ev3/pxt.json +++ b/libs/ev3/pxt.json @@ -24,8 +24,7 @@ "touch-sensor": "file:../touch-sensor", "ultrasonic-sensor": "file:../ultrasonic-sensor", "gyro-sensor": "file:../gyro-sensor", - "infrared-sensor": "file:../infrared-sensor", - "nxt-light-sensor": "file:../nxt-light-sensor" + "infrared-sensor": "file:../infrared-sensor" }, "palette": [ "#ffffff", diff --git a/libs/nxt-light-sensor/pxt.json b/libs/nxt-light-sensor/pxt.json index ab363255c..4068bd03e 100644 --- a/libs/nxt-light-sensor/pxt.json +++ b/libs/nxt-light-sensor/pxt.json @@ -1,6 +1,6 @@ { "name": "nxt-light-sensor", - "description": "NXT Light Sensor support", + "description": "NXT Light Sensor support - beta", "files": [ "README.md", "light.ts" @@ -10,6 +10,6 @@ ], "public": true, "dependencies": { - "core": "file:../core" + "core": "file:../core" } } \ No newline at end of file From a58f178d8fb983518196b32482cd78a605530c1e Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 12:53:45 +0300 Subject: [PATCH 51/53] light-to-bright --- libs/nxt-light-sensor/light.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/nxt-light-sensor/light.ts b/libs/nxt-light-sensor/light.ts index dbdfb11e6..6f177ac88 100644 --- a/libs/nxt-light-sensor/light.ts +++ b/libs/nxt-light-sensor/light.ts @@ -119,10 +119,10 @@ namespace sensors { * Set the range of values for determining dark and light in light reflection mode. This must be done so that the reflection mode defines a value in the range from 0 to 100 percent. * @param sensor the color sensor port * @param dark the value of dark, eg: 0 - * @param light the value of light, eg: 4095 + * @param bright the value of bright, eg: 4095 */ //% help=sensors/nxt-light-sensor/set-reflected-range - //% block="**nxt light sensor** $this|set reflected range dark $dark|light $light" + //% block="**nxt light sensor** $this|set reflected range dark $dark|bright $bright" //% blockId=setReflectedLightRange //% parts="nxtlightsensor" //% blockNamespace=sensors @@ -140,10 +140,10 @@ namespace sensors { * Set the value range for dark and light detection in ambient light mode. This must be done so that the ambient light mode determines the value in the range from 0 to 100 percent. * @param sensor the color sensor port * @param dark the value of dark, eg: 0 - * @param light the value of light, eg: 4095 + * @param bright the value of bright, eg: 4095 */ //% help=sensors/nxt-light-sensor/set-ambient-range - //% block="**nxt light sensor** $this|set ambient range dark $dark|light $light" + //% block="**nxt light sensor** $this|set ambient range dark $dark|bright $bright" //% blockId=setAmbientLightRange //% parts="nxtlightsensor" //% blockNamespace=sensors From 0161690e4eef2f6c6a7baf1d439778268a401e9b Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 13:07:28 +0300 Subject: [PATCH 52/53] add-docs --- .../reference/sensors/nxt-light-sensor.md | 9 ++++++ .../sensors/nxt-light-sensor/light.md | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md create mode 100644 libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor/light.md diff --git a/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md b/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md new file mode 100644 index 000000000..d87fc7de2 --- /dev/null +++ b/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md @@ -0,0 +1,9 @@ +# NXT light sensor + +```cards +sensors.color1.light(LightIntensityMode.Ambient) +``` + +## See slso + +[light](/reference/sensors/nxt-light-sensor/light) diff --git a/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor/light.md b/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor/light.md new file mode 100644 index 000000000..a714da2d9 --- /dev/null +++ b/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor/light.md @@ -0,0 +1,31 @@ +# light + +Get the amount of ambient or reflected light measured by the sensor. + +```sig +sensors.nxtLight1.light(NXTLightIntensityMode.Reflected) +``` + +The light sensor adjusts itself to more accurately measure light depending on the source of the light. You decide if you want to measure _ambient_ light (light all around or direct light) or if you want to know how much light is reflected from a surface. The amount of light measured is in the range of `0` (darkest) to `100` (brightest). + +## Parameters + +* **mode**: the type of measurement for light. This is either ``ambient`` or ``reflected`` light. + +## Returns + +* a number that is the amount of light measured. No light (darkness) is `0` and the brightest light is `100`. + +## Example + +Make the status light show ``green`` if the ambient light is greater than `20`. + +```blocks +forever(function () { + if (sensors.nxtLight1.light(NXTLightIntensityMode.Reflected) > 20) { + brick.setStatusLight(StatusLight.Green) + } else { + brick.setStatusLight(StatusLight.Orange) + } +}) +``` From bc48fe039321d5c8c1f5c0e2e6074561fa9ff424 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Thu, 14 Dec 2023 13:08:11 +0300 Subject: [PATCH 53/53] Update nxt-light-sensor.md --- .../docs/reference/sensors/nxt-light-sensor.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md b/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md index d87fc7de2..9f5fb2f14 100644 --- a/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md +++ b/libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md @@ -1,7 +1,8 @@ # NXT light sensor ```cards -sensors.color1.light(LightIntensityMode.Ambient) +sensors.nxtLight1.light(NXTLightIntensityMode.Reflected) +sensors.nxtLight1.light(NXTLightIntensityMode.ReflectedRaw) ``` ## See slso