Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RGB raw color sensor simulator support #1023

Merged
merged 8 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
final-changes
  • Loading branch information
THEb0nny committed May 13, 2023
commit 333b487bb649710833d3665722cdcae115156912
21 changes: 20 additions & 1 deletion libs/color-sensor/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@ namespace sensors {
|| this.mode == ColorSensorMode.AmbientLightIntensity
|| this.mode == ColorSensorMode.ReflectedLightIntensity)
return this.getNumber(NumberFormat.UInt8LE, 0)
if (this.mode == ColorSensorMode.RefRaw || this.mode == ColorSensorMode.RgbRaw)
if (this.mode == ColorSensorMode.RefRaw)
return this.getNumber(NumberFormat.UInt16LE, 0)
return 0
}

_queryArr(): number[] {
if (this.mode == ColorSensorMode.RgbRaw) {
return [this.getNumber(NumberFormat.UInt16LE, 0), this.getNumber(NumberFormat.UInt16LE, 2), this.getNumber(NumberFormat.UInt16LE, 4)];
}
return [0, 0, 0];
}

_info(): string {
switch (this.mode) {
case ColorSensorMode.Color:
Expand All @@ -106,11 +113,23 @@ namespace sensors {
case ColorSensorMode.AmbientLightIntensity:
case ColorSensorMode.ReflectedLightIntensity:
return `${this._query()}%`;
case ColorSensorMode.RgbRaw:
return "array";
default:
return this._query().toString();
}
}

_infoArr(): string[] {
switch (this.mode) {
case ColorSensorMode.RgbRaw:
const queryArr = this._queryArr().map(number => number.toString());
return queryArr;
default:
return ["0", "0", "0"];
}
}

_update(prev: number, curr: number) {
if (this.calibrating) return; // simply ignore data updates while calibrating
if (this.mode == ColorSensorMode.Color || this.mode == ColorSensorMode.RgbRaw || this.mode == ColorSensorMode.RefRaw)
Expand Down
4 changes: 4 additions & 0 deletions libs/core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ void cUiUpdatePower(void)
return this._query().toString();
}

_infoArr(): string[] {
return [this._query().toString()];
}

_update(prev: number, curr: number) {
}

Expand Down
9 changes: 8 additions & 1 deletion libs/screen/targetoverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,14 @@ namespace brick {
const x = (si.port() - 1) * col + 2;
const inf = si._info();
if (screenMode != ScreenMode.Ports) return;
if (inf) screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8);
if (inf == "array") {
let infArr = si._infoArr();
for (let data = 0, str = Math.min(infArr.length + 1, 4); data < Math.min(infArr.length, 3); data++, str--) {
screen.print(infArr[data], x, h - str * lineHeight8, 1, infArr[data].length > 4 ? image.font5 : image.font8);
}
} else if (inf) {
screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8);
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions sim/state/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ namespace pxsim {
this.setChangedState();
}

setColor(colors: number) {
this.color = colors;
setColor(color: number) {
this.color = color;
this.setChangedState();
}

Expand All @@ -60,12 +60,15 @@ namespace pxsim {
this.mode = mode;
if (this.mode == ColorSensorMode.RefRaw) {
this.color = 512;
this.colors = [0, 0, 0];
this.modeReturnArr = false;
} else if (this.mode == ColorSensorMode.RgbRaw) {
this.color = 0;
this.colors = [128, 128, 128];
this.modeReturnArr = true;
} else { // Reflection or ambiend light
this.color = 50;
this.colors = [0, 0, 0];
this.modeReturnArr = false;
}
this.changed = true;
Expand Down
12 changes: 7 additions & 5 deletions sim/state/uart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ namespace pxsim {
if (node && node.isUart()) {
// Actual
const index = 0; //UartOff.Actual + port * 2;
//console.log(node.isModeReturnArr()); // Узнать возвращает ли режим датчика массив значений
let value, values;
if (!node.isModeReturnArr()) {
value = Math.floor(node.getValue());
const value = Math.floor(node.getValue());
util.map16Bit(data, UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index, value);
} else values = node.getValues();
//util.map16Bit(data, UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index, value);
} else {
const values = node.getValues();
for (let i = 0, offset = 0; i < values.length; i++, offset += 2) {
util.map16Bit(data, UartOff.Raw + DAL.MAX_DEVICE_DATALENGTH * 300 * port + DAL.MAX_DEVICE_DATALENGTH * index + offset, Math.floor(values[i]));
}
}
// Status
data[UartOff.Status + port] = node.valueChange() ? UartStatus.UART_PORT_CHANGED : UartStatus.UART_DATA_READY;
}
Expand Down
20 changes: 2 additions & 18 deletions sim/visuals/controls/colorRGBWheel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace pxsim.visuals {
private colorGradient: SVGLinearGradientElement[] = [];
private reporter: SVGTextElement[] = [];
private rect: SVGElement[] = [];

private printOffsetH = 16;
private rgbLetters: string[] = ["R", "G", "B"];
private rectNames: string[] = ["rectR", "rectG", "rectB"];
Expand Down Expand Up @@ -33,7 +32,7 @@ namespace pxsim.visuals {
}

private getMaxValue() {
return 511;
return 512;
}

private mapValue(x: number, inMin: number, inMax: number, outMin: number, outMax: number) {
Expand All @@ -44,8 +43,6 @@ namespace pxsim.visuals {
if (!this.visible) return;
const node = this.state;
const values = node.getValues();
//console.log("values: ");
//console.log(values);
let inverseValue: number[] = [];
for (let i = 0; i < 3; i++) {
inverseValue[i] = this.getMaxValue() - values[i];
Expand All @@ -56,13 +53,9 @@ namespace pxsim.visuals {
}

updateColorLevel(pt: SVGPoint, parent: SVGSVGElement, ev: MouseEvent) {
//console.log(ev);
//console.log(ev.target);
if (!this.classVal) this.classVal = (ev.target as HTMLElement).classList.value;
//console.log("classVal: " + this.classVal);
let cur = svg.cursorPoint(pt, parent, ev);
let index = this.rectNames.findIndex(i => i == this.classVal);
//console.log("index: " + index);
const bBox = this.rect[index].getBoundingClientRect();
const height = bBox.height;
let t = Math.max(0, Math.min(1, (height + bBox.top / this.scaleFactor - cur.y / this.scaleFactor) / height));
Expand All @@ -84,38 +77,29 @@ namespace pxsim.visuals {
svg.setGradientColors(this.colorGradient[i], "black", "yellow");
}

let pt = parent.createSVGPoint();

let reporterGroup: SVGElement[] = [];
for (let i = 0; i < 3; i++) {
reporterGroup[i] = pxsim.svg.child(this.group, "g");
//console.log(`reporterGroup[${i}]:`);
//console.log(reporterGroup[i]);

reporterGroup[i].setAttribute("transform", `translate(${this.getWidth() / 2}, ${18 + this.printOffsetH * i})`);
this.reporter[i] = pxsim.svg.child(reporterGroup[i], "text", { 'text-anchor': 'middle', 'class': 'sim-text number large inverted', 'style': 'font-size: 18px;' }) as SVGTextElement;
//console.log(`this.reporter[${i}]:`);
//console.log(this.reporter[0]);
}

let sliderGroup: SVGElement[] = [];
for (let i = 0; i < 3; i++) {
sliderGroup[i] = pxsim.svg.child(this.group, "g");
const translateX = (this.getWidth() / 2 - this.getSliderWidth() / 2 - 36) + 36 * i;
sliderGroup[i].setAttribute("transform", `translate(${translateX}, ${this.getReporterHeight()})`);
//console.log(`sliderGroup[${i}]:`);
//console.log(sliderGroup[i]);

this.rect[i] = pxsim.svg.child(sliderGroup[i], "rect", {
"width": this.getSliderWidth(),
"height": this.getSliderHeight(),
"style": `fill: url(#${gc + "-" + i})`
}
);
//console.log(`this.rect[${i}]:`);
//console.log(this.rect[i]);
}

let pt = parent.createSVGPoint();
for (let i = 0; i < 3; i++) {
touchEvents(this.rect[i], ev => {
if (this.captured && (ev as MouseEvent).clientY) {
Expand Down