From 9be35a1034c94ab9fa23c0526ddf9bf5b4e98758 Mon Sep 17 00:00:00 2001 From: Dmitriy Antipov Date: Sat, 6 May 2023 01:54:56 +0300 Subject: [PATCH] Added support for Reflection Raw mode for the color sensor to the simulator (#1017) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * redesigned-code-to-support-ref-raw Code that adds reflection raw support for the color sensor. The range of values for this is from 0 to 1023, because analog to digital converter is 10 bits. In fact, the color sensor gives values ​​of about 400 - 700, but I decided to leave the range, which could theoretically be. For other cases (reflections and ambient lighting), the range remains from 0 to 100. The average value when setting the mode in the simulator is displayed as 512, for other modes 50%. * block-description-update Block description update, as it did not take into account the mode of raw reflection values. --- libs/color-sensor/color.ts | 2 +- sim/state/color.ts | 10 +++++++++- sim/visuals/board.ts | 2 ++ sim/visuals/controls/colorWheel.ts | 20 +++++++++++++------- sim/visuals/nodes/colorSensorView.ts | 1 + 5 files changed, 26 insertions(+), 9 deletions(-) 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');