Added support for Reflection Raw mode for the color sensor to the simulator (#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.
This commit is contained in:
parent
2ca706df70
commit
9be35a1034
@ -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
|
* @param sensor the color sensor port
|
||||||
*/
|
*/
|
||||||
//% help=sensors/color-sensor/light
|
//% help=sensors/color-sensor/light
|
||||||
|
@ -21,7 +21,7 @@ namespace pxsim {
|
|||||||
export class ColorSensorNode extends UartSensorNode {
|
export class ColorSensorNode extends UartSensorNode {
|
||||||
id = NodeType.ColorSensor;
|
id = NodeType.ColorSensor;
|
||||||
|
|
||||||
private color: number = 50;
|
private color: number = 0;
|
||||||
|
|
||||||
constructor(port: number) {
|
constructor(port: number) {
|
||||||
super(port);
|
super(port);
|
||||||
@ -40,5 +40,13 @@ namespace pxsim {
|
|||||||
getValue() {
|
getValue() {
|
||||||
return this.color;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -241,6 +241,8 @@ namespace pxsim.visuals {
|
|||||||
view = new ColorGridControl(this.element, this.defs, state, port);
|
view = new ColorGridControl(this.element, this.defs, state, port);
|
||||||
} else if (state.getMode() == ColorSensorMode.Reflected) {
|
} else if (state.getMode() == ColorSensorMode.Reflected) {
|
||||||
view = new ColorWheelControl(this.element, this.defs, state, port);
|
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) {
|
} else if (state.getMode() == ColorSensorMode.Ambient) {
|
||||||
view = new ColorWheelControl(this.element, this.defs, state, port);
|
view = new ColorWheelControl(this.element, this.defs, state, port);
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,12 @@ namespace pxsim.visuals {
|
|||||||
return 131;
|
return 131;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getMaxValue() {
|
private getMaxValue(state: ColorSensorMode) {
|
||||||
return 100;
|
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() {
|
updateState() {
|
||||||
@ -35,10 +39,12 @@ namespace pxsim.visuals {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const node = this.state;
|
const node = this.state;
|
||||||
const percentage = node.getValue();
|
const value = node.getValue();
|
||||||
const inversePercentage = this.getMaxValue() - percentage;
|
let inverseValue = this.getMaxValue(node.getMode()) - value;
|
||||||
svg.setGradientValue(this.colorGradient, inversePercentage + "%");
|
if (node.getMode() == ColorSensorMode.RefRaw) inverseValue = this.mapValue(inverseValue, 0, 1023, 0, 100);
|
||||||
this.reporter.textContent = `${parseFloat((percentage).toString()).toFixed(0)}%`;
|
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) {
|
updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) {
|
||||||
@ -47,7 +53,7 @@ namespace pxsim.visuals {
|
|||||||
const height = bBox.height;
|
const height = bBox.height;
|
||||||
let t = Math.max(0, Math.min(1, (height + bBox.top / this.scaleFactor - cur.y / this.scaleFactor) / height));
|
let t = Math.max(0, Math.min(1, (height + bBox.top / this.scaleFactor - cur.y / this.scaleFactor) / height));
|
||||||
const state = this.state;
|
const state = this.state;
|
||||||
state.setColor(t * this.getMaxValue());
|
state.setColor(t * this.getMaxValue(state.getMode()));
|
||||||
}
|
}
|
||||||
|
|
||||||
getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) {
|
getInnerView(parent: SVGSVGElement, globalDefs: SVGDefsElement) {
|
||||||
|
@ -29,6 +29,7 @@ namespace pxsim.visuals {
|
|||||||
switch (mode) {
|
switch (mode) {
|
||||||
case ColorSensorMode.Colors: this.updateSensorLightVisual('#0062DD'); return; // blue
|
case ColorSensorMode.Colors: this.updateSensorLightVisual('#0062DD'); return; // blue
|
||||||
case ColorSensorMode.Reflected: this.updateSensorLightVisual('#F86262'); return; // red
|
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
|
case ColorSensorMode.Ambient: this.updateSensorLightVisual('#67C3E2'); return; // light blue
|
||||||
}
|
}
|
||||||
this.updateSensorLightVisual('#ffffff');
|
this.updateSensorLightVisual('#ffffff');
|
||||||
|
Loading…
Reference in New Issue
Block a user