Skip to content

Commit

Permalink
Added support for Reflection Raw mode for the color sensor to the sim…
Browse files Browse the repository at this point in the history
…ulator (#1017)

* 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.
  • Loading branch information
THEb0nny authored May 5, 2023
1 parent 2ca706d commit 9be35a1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libs/color-sensor/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion sim/state/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
}
2 changes: 2 additions & 0 deletions sim/visuals/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 13 additions & 7 deletions sim/visuals/controls/colorWheel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ 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() {
if (!this.visible) {
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) {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions sim/visuals/nodes/colorSensorView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 9be35a1

Please sign in to comment.