diff --git a/libs/color-sensor/color.ts b/libs/color-sensor/color.ts index 9883b0d8..a6a7c626 100644 --- a/libs/color-sensor/color.ts +++ b/libs/color-sensor/color.ts @@ -250,7 +250,7 @@ namespace sensors { } /** - * Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest). + * Measure the ambient or reflected light value from 0 (darkest) to 100 (brightest). In raw reflection light mode, the range will be different. * @param sensor the color sensor port */ //% help=sensors/color-sensor/light diff --git a/sim/state/color.ts b/sim/state/color.ts index 47392084..9a8e43cf 100644 --- a/sim/state/color.ts +++ b/sim/state/color.ts @@ -21,7 +21,7 @@ namespace pxsim { export class ColorSensorNode extends UartSensorNode { id = NodeType.ColorSensor; - private color: number = 50; + private color: number = 0; constructor(port: number) { super(port); @@ -40,5 +40,13 @@ namespace pxsim { getValue() { return this.color; } + + setMode(mode: number) { + this.mode = mode; + if (this.mode == ColorSensorMode.RefRaw) this.color = 512; + else this.color = 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 fcd049bc..64bb6ead 100644 --- a/sim/visuals/board.ts +++ b/sim/visuals/board.ts @@ -241,6 +241,8 @@ namespace pxsim.visuals { view = new ColorGridControl(this.element, this.defs, state, port); } else if (state.getMode() == ColorSensorMode.Reflected) { view = new ColorWheelControl(this.element, this.defs, state, port); + } else if (state.getMode() == ColorSensorMode.RefRaw) { + view = new ColorWheelControl(this.element, this.defs, state, port); } else if (state.getMode() == ColorSensorMode.Ambient) { view = new ColorWheelControl(this.element, this.defs, state, port); } diff --git a/sim/visuals/controls/colorWheel.ts b/sim/visuals/controls/colorWheel.ts index 9cbe4cdb..0edb9a03 100644 --- a/sim/visuals/controls/colorWheel.ts +++ b/sim/visuals/controls/colorWheel.ts @@ -26,8 +26,12 @@ namespace pxsim.visuals { return 131; } - private getMaxValue() { - return 100; + private getMaxValue(state: ColorSensorMode) { + return (state == ColorSensorMode.RefRaw ? 1023 : 100); + } + + private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) { + return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; } updateState() { @@ -35,10 +39,12 @@ namespace pxsim.visuals { return; } const node = this.state; - const percentage = node.getValue(); - const inversePercentage = this.getMaxValue() - percentage; - svg.setGradientValue(this.colorGradient, inversePercentage + "%"); - this.reporter.textContent = `${parseFloat((percentage).toString()).toFixed(0)}%`; + const value = node.getValue(); + let inverseValue = this.getMaxValue(node.getMode()) - value; + if (node.getMode() == ColorSensorMode.RefRaw) inverseValue = this.mapValue(inverseValue, 0, 1023, 0, 100); + svg.setGradientValue(this.colorGradient, inverseValue + "%"); + this.reporter.textContent = `${parseFloat((value).toString()).toFixed(0)}`; + if (node.getMode() != ColorSensorMode.RefRaw) this.reporter.textContent += `%`; } updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) { @@ -47,7 +53,7 @@ namespace pxsim.visuals { 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.setColor(t * this.getMaxValue(state.getMode())); } getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) { diff --git a/sim/visuals/nodes/colorSensorView.ts b/sim/visuals/nodes/colorSensorView.ts index 8539ce4e..4cebc17b 100644 --- a/sim/visuals/nodes/colorSensorView.ts +++ b/sim/visuals/nodes/colorSensorView.ts @@ -29,6 +29,7 @@ namespace pxsim.visuals { switch (mode) { case ColorSensorMode.Colors: this.updateSensorLightVisual('#0062DD'); return; // blue case ColorSensorMode.Reflected: this.updateSensorLightVisual('#F86262'); return; // red + case ColorSensorMode.RefRaw: this.updateSensorLightVisual('#F86262'); return; // red case ColorSensorMode.Ambient: this.updateSensorLightVisual('#67C3E2'); return; // light blue } this.updateSensorLightVisual('#ffffff');