Skip to content

Commit

Permalink
Merge branch 'master' into changes-in-sensors-category
Browse files Browse the repository at this point in the history
  • Loading branch information
jwunderl authored Feb 2, 2024
2 parents 518b411 + 2c8d3d5 commit 94ac596
Show file tree
Hide file tree
Showing 25 changed files with 952 additions and 44 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"]'
//% groups='["Touch Sensor", "Color Sensor", "Ultrasonic Sensor", "Gyro Sensor", "Infrared Sensor", "Remote Infrared Beacon", "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 94ac596

Please sign in to comment.