Merge pull request #64 from Microsoft/color-sensor

Color sensor refactoring
This commit is contained in:
Peli de Halleux 2017-11-30 08:55:50 -08:00 committed by GitHub
commit e365e3d1be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 385 additions and 160 deletions

View File

@ -4,4 +4,5 @@
## See Also
[touch sensor](/reference/sensors/touch-sensor)
[touch sensor](/reference/sensors/touch-sensor),
[color sensor](/reference/sensors/color-sensor)

View File

@ -0,0 +1,3 @@
# Color sensor
The library to interact with the Touch Sensor.

View File

@ -0,0 +1,12 @@
{
"sensors.ColorSensor": "The color sensor is a digital sensor that can detect the color or intensity\nof light that enters the small window on the face of the sensor.",
"sensors.ColorSensor.color": "Get the current color from the color sensor.",
"sensors.ColorSensor.colorMode": "Gets the current color mode",
"sensors.ColorSensor.light": "Measures the ambient or reflected light value from 0 (darkest) to 100 (brightest).",
"sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected.",
"sensors.ColorSensor.onColorDetected|param|color": "the color to detect, eg: ColorSensorColor.Blue",
"sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected",
"sensors.ColorSensor.onLightChanged": "Registers code to run when the ambient light changes.",
"sensors.ColorSensor.onLightChanged|param|condition": "the light condition",
"sensors.ColorSensor.onLightChanged|param|handler": "the code to run when detected"
}

View File

@ -0,0 +1,27 @@
{
"ColorSensorColor.Black|block": "black",
"ColorSensorColor.Blue|block": "blue",
"ColorSensorColor.Brown|block": "brown",
"ColorSensorColor.Green|block": "green",
"ColorSensorColor.None|block": "none",
"ColorSensorColor.Red|block": "red",
"ColorSensorColor.White|block": "white",
"ColorSensorColor.Yellow|block": "yellow",
"ColorSensorMode.AmbientLightIntensity|block": "ambient light intensity",
"ColorSensorMode.Color|block": "color",
"ColorSensorMode.ReflectedLightIntensity|block": "reflected light intensity",
"LightCondition.Dark|block": "dark",
"LightIntensityMode.Ambient|block": "ambient light",
"LightIntensityMode.Reflected|block": "reflected light",
"sensors.ColorSensor.color|block": "`icons.colorSensor` %color| color",
"sensors.ColorSensor.light|block": "`icons.colorSensor` %color|%mode",
"sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected color %color",
"sensors.ColorSensor.onLightChanged|block": "on `icons.colorSensor` %sensor|%mode|%condition",
"sensors.colorSensor1|block": "1",
"sensors.colorSensor2|block": "2",
"sensors.colorSensor3|block": "3",
"sensors.colorSensor4|block": "4",
"sensors|block": "sensors",
"{id:category}Sensors": "Sensors",
"{id:group}Color Sensor": "Color Sensor"
}

185
libs/color-sensor/color.ts Normal file
View File

@ -0,0 +1,185 @@
const enum ColorSensorMode {
None = -1,
//% block="reflected light intensity"
ReflectedLightIntensity = 0,
//% block="ambient light intensity"
AmbientLightIntensity = 1,
//% block="color"
Color = 2,
RefRaw = 3,
RgbRaw = 4,
ColorCal = 5,
}
enum LightIntensityMode {
//% block="reflected light"
Reflected = ColorSensorMode.ReflectedLightIntensity,
//% block="ambient light"
Ambient = ColorSensorMode.AmbientLightIntensity
}
const enum ColorSensorColor {
//% block="none"
None,
//% block="black"
Black,
//% block="blue"
Blue,
//% block="green"
Green,
//% block="yellow"
Yellow,
//% block="red"
Red,
//% block="white"
White,
//% block="brown"
Brown,
}
enum LightCondition {
//% block="dark"
Dark = sensors.internal.ThresholdState.Low,
//$ block="bright"
Bright = sensors.internal.ThresholdState.High
}
namespace sensors {
/**
* The color sensor is a digital sensor that can detect the color or intensity
* of light that enters the small window on the face of the sensor.
*/
//% fixedInstances
export class ColorSensor extends internal.UartSensor {
thresholdDetector: sensors.internal.ThresholdDetector;
constructor(port: number) {
super(port)
this.thresholdDetector = new sensors.internal.ThresholdDetector(this.id());
}
_colorEventValue(value: number) {
return 0xff00 | value;
}
_deviceType() {
return DAL.DEVICE_TYPE_COLOR
}
setMode(m: ColorSensorMode) {
this._setMode(m)
}
/**
* Gets the current color mode
*/
colorMode() {
return <ColorSensorMode>this.mode;
}
_query() {
if (this.mode == ColorSensorMode.Color)
return this.getNumber(NumberFormat.UInt8LE, 0)
return 0
}
_update(prev: number, curr: number) {
if (this.mode == ColorSensorMode.Color)
control.raiseEvent(this._id, this._colorEventValue(curr));
else
this.thresholdDetector.setLevel(curr);
}
/**
* Registers code to run when the given color is detected.
* @param color the color to detect, eg: ColorSensorColor.Blue
* @param handler the code to run when detected
*/
//% help=sensors/color-sensor/on-color-detected
//% block="on `icons.colorSensor` %sensor|detected color %color"
//% blockId=colorOnColorDetected
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=100 blockGap=8
//% group="Color Sensor"
onColorDetected(color: ColorSensorColor, handler: () => void) {
const v = this._colorEventValue(<number>color);
control.onEvent(this._id, v, handler);
this.setMode(ColorSensorMode.Color)
if (this.color() == color)
control.raiseEvent(this._id, v);
}
/**
* Get the current color from the color sensor.
* @param color the color sensor to query the request
*/
//% help=sensors/color-sensor/color
//% block="`icons.colorSensor` %color| color"
//% blockId=colorGetColor
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=99
//% group="Color Sensor"
color(): ColorSensorColor {
this.setMode(ColorSensorMode.Color)
return this.getNumber(NumberFormat.UInt8LE, 0)
}
/**
* Registers code to run when the ambient light changes.
* @param condition the light condition
* @param handler the code to run when detected
*/
//% help=sensors/color-sensor/on-light-changed
//% block="on `icons.colorSensor` %sensor|%mode|%condition"
//% blockId=colorOnLightChanged
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=89 blockGap=8
//% group="Color Sensor"
onLightChanged(mode: LightIntensityMode, condition: LightCondition, handler: () => void) {
control.onEvent(this._id, <number>condition, handler);
this.setMode(<ColorSensorMode><number>mode)
}
/**
* Measures the ambient or reflected light value from 0 (darkest) to 100 (brightest).
* @param color the color sensor port
*/
//% help=sensors/color-sensor/light
//% block="`icons.colorSensor` %color|%mode"
//% blockId=colorLight
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=88
//% group="Color Sensor"
light(mode: LightIntensityMode) {
this.setMode(<ColorSensorMode><number>mode)
return this.getNumber(NumberFormat.UInt8LE, 0)
}
//%
ambientLight() {
return this.light(LightIntensityMode.Ambient);
}
//%
reflectedLight() {
return this.light(LightIntensityMode.Reflected);
}
}
//% whenUsed block="1" weight=95 fixedInstance
export const colorSensor1: ColorSensor = new ColorSensor(1)
//% whenUsed block="3" weight=90 fixedInstance
export const colorSensor3: ColorSensor = new ColorSensor(3)
//% whenUsed block="2" weight=90 fixedInstance
export const colorSensor2: ColorSensor = new ColorSensor(2)
//% whenUsed block="4" weight=90 fixedInstance
export const colorSensor4: ColorSensor = new ColorSensor(4)
}

View File

@ -0,0 +1,16 @@
# Color Sensor
```cards
sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () {
})
sensors.colorSensor1.color();
sensors.colorSensor1.ambientLight();
sensors.colorSensor1.reflectedLight();
```
## See Also
[on color detected](/reference/sensors/color-sensor/on-color-detected),
[color](/reference/sensors/color-sensor/color),
[ambient light](/reference/sensors/color-sensor/ambient-light),
[reflected light](/reference/sensors/color-sensor/reflected-light),

View File

@ -0,0 +1,11 @@
# Ambient Light
```blocks
loops.forever(function () {
if (sensors.colorSensor1.ambientLight() > 20) {
brick.setStatusLight(LightsPattern.Green)
} else {
brick.setStatusLight(LightsPattern.Orange)
}
})
```

View File

@ -0,0 +1,11 @@
# color
```blocks
loops.forever(function () {
if (sensors.colorSensor1.color() == ColorSensorColor.Green) {
brick.setStatusLight(LightsPattern.Green)
} else {
brick.setStatusLight(LightsPattern.Orange)
}
})
```

View File

@ -0,0 +1,16 @@
# On Color Detected
```sig
sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () { })
```
# Parameters
## Examples
```blocks
sensors.colorSensor1.onColorDetected(ColorSensorColor.Blue, function () {
brick.showImage(images.expressionsSick)
})
```

View File

@ -0,0 +1,11 @@
# Reflected Light
```blocks
loops.forever(function () {
if (sensors.colorSensor1.reflectedLight() > 20) {
brick.setStatusLight(LightsPattern.Green)
} else {
brick.setStatusLight(LightsPattern.Orange)
}
})
```

View File

@ -0,0 +1,15 @@
{
"name": "color-sensor",
"description": "Color Sensor support",
"files": [
"README.md",
"color.ts"
],
"testFiles": [
"test.ts"
],
"public": true,
"dependencies": {
"core": "file:../core"
}
}

View File

View File

@ -68,12 +68,6 @@
"screen.clear": "Clear screen and reset font to normal.",
"screen.imageOf": "Makes an image bound to a buffer.",
"screen.unpackPNG": "Decompresses a 1-bit gray scale PNG image to image format.",
"sensors.ColorSensor.ambientLight": "Get current ambient light value from the color sensor.",
"sensors.ColorSensor.color": "Get the current color from the color sensor.",
"sensors.ColorSensor.onColorDetected": "Registers code to run when the given color is detected",
"sensors.ColorSensor.onColorDetected|param|color": "the color to dtect",
"sensors.ColorSensor.onColorDetected|param|handler": "the code to run when detected",
"sensors.ColorSensor.reflectedLight": "Get current reflected light value from the color sensor.",
"sensors.GyroSensor.angle": "Get the current angle from the gyroscope.",
"sensors.GyroSensor.rate": "Get the current rotation rate from the gyroscope.",
"sensors.InfraredSensor.on": "Registers code to run when an object is getting near.",

View File

@ -2,14 +2,6 @@
"ButtonEvent.Click|block": "click",
"ButtonEvent.Down|block": "down",
"ButtonEvent.Up|block": "up",
"ColorSensorColor.Black|block": "black",
"ColorSensorColor.Blue|block": "blue",
"ColorSensorColor.Brown|block": "brown",
"ColorSensorColor.Green|block": "green",
"ColorSensorColor.None|block": "none",
"ColorSensorColor.Red|block": "red",
"ColorSensorColor.White|block": "white",
"ColorSensorColor.Yellow|block": "yellow",
"InfraredSensorEvent.ObjectDetected|block": "object detected",
"InfraredSensorEvent.ObjectNear|block": "object near",
"LightsPattern.GreenFlash|block": "Flashing Green",
@ -66,10 +58,6 @@
"motors|block": "motors",
"output|block": "output",
"screen|block": "screen",
"sensors.ColorSensor.ambientLight|block": "`icons.colorSensor` %color| ambient light",
"sensors.ColorSensor.color|block": "`icons.colorSensor` %color| color",
"sensors.ColorSensor.onColorDetected|block": "on `icons.colorSensor` %sensor|detected %color",
"sensors.ColorSensor.reflectedLight|block": "`icons.colorSensor` %color| reflected light",
"sensors.GyroSensor.angle|block": "`icons.gyroSensor` %sensor|angle",
"sensors.GyroSensor.rate|block": "`icons.gyroSensor` %sensor|rotation rate",
"sensors.InfraredSensor.on|block": "on `icons.infraredSensor` %sensor|%event",
@ -82,10 +70,6 @@
"sensors.UltraSonicSensor.distance|block": "`icons.ultrasonicSensor` %sensor|distance",
"sensors.UltraSonicSensor.on|block": "on `icons.ultrasonicSensor` %sensor|%event",
"sensors.UltraSonicSensor.wait|block": "wait `icons.ultrasonicSensor` %sensor|for %event",
"sensors.color1|block": "1",
"sensors.color2|block": "2",
"sensors.color3|block": "3",
"sensors.color4|block": "4",
"sensors.gyro1|block": "1",
"sensors.gyro2|block": "2",
"sensors.gyro3|block": "3",
@ -116,7 +100,6 @@
"{id:category}Sensors": "Sensors",
"{id:category}Serial": "Serial",
"{id:group}Buttons": "Buttons",
"{id:group}Color Sensor": "Color Sensor",
"{id:group}Gyro Sensor": "Gyro Sensor",
"{id:group}Infrared Sensor": "Infrared Sensor",
"{id:group}Light": "Light",

View File

@ -1,135 +0,0 @@
const enum ColorSensorMode {
None = -1,
Reflect = 0,
Ambient = 1,
Color = 2,
RefRaw = 3,
RgbRaw = 4,
ColorCal = 5,
}
const enum ColorSensorColor {
//% block="none"
None,
//% block="black"
Black,
//% block="blue"
Blue,
//% block="green"
Green,
//% block="yellow"
Yellow,
//% block="red"
Red,
//% block="white"
White,
//% block="brown"
Brown,
}
namespace sensors {
//% fixedInstances
export class ColorSensor extends internal.UartSensor {
constructor(port: number) {
super(port)
}
_deviceType() {
return DAL.DEVICE_TYPE_COLOR
}
setMode(m: ColorSensorMode) {
this._setMode(m)
}
_query() {
if (this.mode == ColorSensorMode.Color)
return this.getNumber(NumberFormat.UInt8LE, 0)
return 0
}
_update(prev: number, curr: number) {
control.raiseEvent(this._id, curr);
}
/**
* Registers code to run when the given color is detected
* @param color the color to dtect
* @param handler the code to run when detected
*/
//% help=input/color/on-color-detected
//% block="on `icons.colorSensor` %sensor|detected %color"
//% blockId=colorOnColorDetected
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=100 blockGap=8
//% group="Color Sensor"
onColorDetected(color: ColorSensorColor, handler: () => void) {
control.onEvent(this._id, <number>color, handler);
this.setMode(ColorSensorMode.Color)
if (this.color() == color)
control.runInBackground(handler)
}
/**
* Get current ambient light value from the color sensor.
* @param color the color sensor to query the request
*/
//% help=input/color/ambient-light
//% block="`icons.colorSensor` %color| ambient light"
//% blockId=colorGetAmbient
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=65 blockGap=8
//% group="Color Sensor"
ambientLight() {
this.setMode(ColorSensorMode.Ambient)
return this.getNumber(NumberFormat.UInt8LE, 0)
}
/**
* Get current reflected light value from the color sensor.
* @param color the color sensor to query the request
*/
//% help=input/color/refelected-light
//% block="`icons.colorSensor` %color| reflected light"
//% blockId=colorGetReflected
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=64 blockGap=8
//% group="Color Sensor"
reflectedLight(): number {
this.setMode(ColorSensorMode.Reflect)
return this.getNumber(NumberFormat.UInt8LE, 0)
}
/**
* Get the current color from the color sensor.
* @param color the color sensor to query the request
*/
//% help=input/color/color
//% block="`icons.colorSensor` %color| color"
//% blockId=colorGetColor
//% parts="colorsensor"
//% blockNamespace=sensors
//% weight=66 blockGap=8
//% group="Color Sensor"
color(): ColorSensorColor {
this.setMode(ColorSensorMode.Color)
return this.getNumber(NumberFormat.UInt8LE, 0)
}
}
//% whenUsed block="1" weight=95 fixedInstance
export const color1: ColorSensor = new ColorSensor(1)
//% whenUsed block="3" weight=90 fixedInstance
export const color3: ColorSensor = new ColorSensor(3)
//% whenUsed block="2" weight=90 fixedInstance
export const color2: ColorSensor = new ColorSensor(2)
//% whenUsed block="4" weight=90 fixedInstance
export const color4: ColorSensor = new ColorSensor(4)
}

View File

@ -177,7 +177,81 @@ namespace sensors.internal {
}
}
export enum ThresholdState {
Normal = 1,
High = 2,
Low = 3,
}
export class ThresholdDetector {
public id: number;
private min: number;
private max: number;
private lowThreshold: number;
private highThreshold: number;
private level: number;
private state: ThresholdState;
constructor(id: number, min = 0, max = 100, lowThreshold = 20, highThreshold = 80) {
this.id = id;
this.min = min;
this.max = max;
this.lowThreshold = lowThreshold;
this.highThreshold = highThreshold;
this.level = Math.ceil((max - min) / 2);
this.state = ThresholdState.Normal;
}
public setLevel(level: number) {
this.level = this.clampValue(level);
if (this.level >= this.highThreshold) {
this.setState(ThresholdState.High);
}
else if (this.level <= this.lowThreshold) {
this.setState(ThresholdState.Low);
}
else {
this.setState(ThresholdState.Normal);
}
}
public setLowThreshold(value: number) {
this.lowThreshold = this.clampValue(value);
this.highThreshold = Math.max(this.lowThreshold + 1, this.highThreshold);
}
public setHighThreshold(value: number) {
this.highThreshold = this.clampValue(value);
this.lowThreshold = Math.min(this.highThreshold - 1, this.lowThreshold);
}
private clampValue(value: number) {
if (value < this.min) {
return this.min;
}
else if (value > this.max) {
return this.max;
}
return value;
}
private setState(state: ThresholdState) {
if (this.state == state) return;
this.state = state;
switch (state) {
case ThresholdState.High:
control.raiseEvent(this.id, ThresholdState.High);
break;
case ThresholdState.Low:
control.raiseEvent(this.id, ThresholdState.Low);
break;
case ThresholdState.Normal:
break;
}
}
}
export class UartSensor extends Sensor {
protected mode: number // the mode user asked for

View File

@ -19,7 +19,6 @@
"core.ts",
"input.ts",
"ir.ts",
"color.ts",
"gyro.ts",
"ultrasonic.ts",
"shims.d.ts",

View File

@ -9,6 +9,7 @@
"base": "file:../base",
"core": "file:../core",
"music": "file:../music",
"color-sensor": "file:../color-sensor",
"touch-sensor": "file:../touch-sensor"
},
"public": true

View File

@ -10,6 +10,7 @@
"libs/base",
"libs/core",
"libs/music",
"libs/color-sensor",
"libs/touch-sensor",
"libs/ev3"
],