Skip to content

Commit

Permalink
Nxt Light Sensor support (#1027)
Browse files Browse the repository at this point in the history
* add-subcategories

Add NXT Sensors subcategorie

* first-support-version

The light sensor gives real values. Mode change not supported...

* update

* Update ns.ts

* sens-sim-support

* min-and-max-metod

* sim-support-new-chenges

* 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.

* Update input.ts

* add-test

* fix-convert-range-bug

* Update light.ts

Setting the range of values for determining black and white in reflection mode and in ambient light mode is now different.

* Update test.ts

* Update test.ts

* Update test.ts

* 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.

* checking-input-values

* set-nxt-light-sensor-svg

* Update light.ts

* update-for-sim

* Update NXT Light Sensor.svg

* resolving-conflict-with-master

* resolving-conflict-with-master-2

* resolving-conflict-with-master-3

* Update nxtLightSensorView.ts

* Update input.ts

* 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.

* Update light.ts

* some-changes-for-the-mode

* 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.

* 4096-to-4095

Fix range - 0..4095

* enum-NXTLightIntensityMode

* light-sensor-svg-updage

* update-light-sensor-ts

* 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.

* range-from-lego-sources

https://github.com/mindboards/ev3sources-xtended/blob/master/ev3sources/lms2012/lms2012/Linux_AM1808/sys/settings/typedata.rcf

* 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...

* query-update-and-add-treshold-blocks

* test-upd

* Update lightWheel.ts

* changes-light-sensor-for-sim

The changes are aimed at supporting reflection and lighting modes, not raw modes.

* deviceType-analog-sens-class

* Update light.ts

* fix-range-for-refLight-and-ambLight

* export-enum-DevConOff

* bug-fix-from-previous-version

* 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.

* 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.

* Update light.ts

* lib-not-include-by-defl

Make sure that the library is not included by default. It will need to be enabled via extensions.

* light-to-bright

* add-docs

* Update nxt-light-sensor.md
  • Loading branch information
THEb0nny authored Feb 2, 2024
1 parent c1d3a5c commit 891a237
Show file tree
Hide file tree
Showing 24 changed files with 951 additions and 43 deletions.
2 changes: 2 additions & 0 deletions libs/core/dal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions libs/core/ev3const.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 71 additions & 24 deletions libs/core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,11 @@ namespace sensors.internal {

function detectDevices() {
control.dmesg(`DETECT DEVICES (hash ${hashDevices()})`);
const conns = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS);
const inDcm = analogMM.slice(AnalogOff.InDcm, DAL.NUM_INPUTS);
const inConn = analogMM.slice(AnalogOff.InConn, DAL.NUM_INPUTS);

for (const sensorInfo of sensorInfos) {
const newConn = conns[sensorInfo.port];
const newConn = inConn[sensorInfo.port];
if (newConn == sensorInfo.connType && sensorInfo.sensor && sensorInfo.sensor.isActive()) {
continue;
}
Expand All @@ -252,9 +253,13 @@ namespace sensors.internal {
sensorInfo.devType = DAL.DEVICE_TYPE_IIC_UNKNOWN;
sensorInfo.iicid = readIICID(sensorInfo.port);
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 port ${sensorInfo.port} dev type ${sensorInfo.devType}`);
} else if (newConn == DAL.CONN_INPUT_DUMB) {
control.dmesg(`new DUMB connection at port ${sensorInfo.port}`);
sensorInfo.devType = DAL.DEVICE_TYPE_TOUCH; // TODO? for now assume touch sensor
//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 {
Expand Down Expand Up @@ -351,13 +356,44 @@ namespace sensors.internal {
}

export class AnalogSensor extends Sensor {

protected mode: number; // the mode user asked for
protected realmode: number;

constructor(port: number) {
super(port)
super(port);
this.mode = 0;
this.realmode = 0;
}

_activated() {
this.realmode = 0;
this._setMode(this.mode);
}

protected _setMode(m: number) {
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;
setAnalogMode(this._port, this._deviceType(), this.mode);
}
}

_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);
}

_deviceType() {
return DAL.DEVICE_TYPE_UNKNOWN;
}
}

Expand All @@ -366,28 +402,29 @@ namespace sensors.internal {
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);
}

getStatus() {
return getUartStatus(this._port);
}

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);
}
}

Expand Down Expand Up @@ -575,19 +612,29 @@ namespace sensors.internal {
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)
}

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;
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) {
Expand Down
4 changes: 2 additions & 2 deletions libs/ev3/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ 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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion libs/ev3/ns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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", "HiTechnic"]'
namespace sensors {
}
Expand Down
3 changes: 3 additions & 0 deletions libs/nxt-light-sensor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NXT Light sensor

The library to interact with the NXT Light Sensor.
10 changes: 10 additions & 0 deletions libs/nxt-light-sensor/docs/reference/sensors/nxt-light-sensor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# NXT light sensor

```cards
sensors.nxtLight1.light(NXTLightIntensityMode.Reflected)
sensors.nxtLight1.light(NXTLightIntensityMode.ReflectedRaw)
```

## See slso

[light](/reference/sensors/nxt-light-sensor/light)
Original file line number Diff line number Diff line change
@@ -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)
}
})
```
Loading

0 comments on commit 891a237

Please sign in to comment.