renderPorts-extra-checks-before-print (#1020)

When the program starts, when the showBoot() function is launched, the screenMode is checked and the showPorts function is launched, which in turn executes the renderPorts() function in a loop. While renderPorts is running, the screenMode may change, for example to ShowLines (when showString() was run). It may turn out that the mode has changed, and the renderPorts method has not been fully executed ... in the future, it will draw its own graphics on the screen, although we do not expect to see them already, because. a regime change has occurred. I added checks inside the function before displaying.
This commit is contained in:
Dmitriy Antipov 2023-05-06 01:46:43 +03:00 committed by GitHub
parent 20581513b1
commit dd415019c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,7 @@ namespace _screen_internal {
namespace brick { namespace brick {
const textOffset = 4; const textOffset = 4;
const lineOffset = 2; const lineOffset = 2;
enum ScreenMode { enum ScreenMode {
None, None,
ShowLines, ShowLines,
@ -29,6 +30,7 @@ namespace brick {
Ports, Ports,
Custom Custom
} }
let screenMode = ScreenMode.None; let screenMode = ScreenMode.None;
export let font = image.font8; export let font = image.font8;
@ -126,8 +128,8 @@ namespace brick {
//% weight=10 group="Screen" //% weight=10 group="Screen"
export function showPorts() { export function showPorts() {
if (screenMode == ScreenMode.Ports) return; if (screenMode == ScreenMode.Ports) return;
screenMode = ScreenMode.Ports; screenMode = ScreenMode.Ports;
renderPorts(); renderPorts();
control.runInParallel(function () { control.runInParallel(function () {
while (screenMode == ScreenMode.Ports) { while (screenMode == ScreenMode.Ports) {
@ -146,11 +148,14 @@ namespace brick {
for (let i = 0; i < 4; ++i) { for (let i = 0; i < 4; ++i) {
const x = i * col + 2; const x = i * col + 2;
screen.print("ABCD"[i], x, 1 * lineHeight8, 1, image.font8) if (screenMode != ScreenMode.Ports) return;
screen.print((i + 1).toString(), x, h - lineHeight8, 1, image.font8) screen.print("ABCD"[i], x, 1 * lineHeight8, 1, image.font8);
screen.print((i + 1).toString(), x, h - lineHeight8, 1, image.font8);
} }
if (screenMode != ScreenMode.Ports) return;
screen.drawLine(0, 5 * lineHeight8, screen.width, 5 * lineHeight8, 1); screen.drawLine(0, 5 * lineHeight8, screen.width, 5 * lineHeight8, 1);
screen.drawLine(0, h - 5 * lineHeight8, screen.width, h - 5 * lineHeight8, 1) screen.drawLine(0, h - 5 * lineHeight8, screen.width, h - 5 * lineHeight8, 1);
function scale(x: number) { function scale(x: number) {
if (Math.abs(x) >= 5000) { if (Math.abs(x) >= 5000) {
@ -167,8 +172,9 @@ namespace brick {
const data = datas[i]; const data = datas[i];
const x = i * col + 2; const x = i * col + 2;
if (!data.actualSpeed && !data.count) continue; if (!data.actualSpeed && !data.count) continue;
screen.print(`${scale(data.actualSpeed)}%`, x, 3 * lineHeight8, 1, image.font8) if (screenMode != ScreenMode.Ports) return;
screen.print(`${scale(data.count)}>`, x, 4 * lineHeight8, 1, image.font8) screen.print(`${scale(data.actualSpeed)}%`, x, 3 * lineHeight8, 1, image.font8);
screen.print(`${scale(data.count)}>`, x, 4 * lineHeight8, 1, image.font8);
} }
// sensors // sensors
@ -177,16 +183,16 @@ namespace brick {
const si = sis[i]; const si = sis[i];
const x = (si.port() - 1) * col + 2; const x = (si.port() - 1) * col + 2;
const inf = si._info(); const inf = si._info();
if (inf) if (screenMode != ScreenMode.Ports) return;
screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8); if (inf) screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8);
} }
} }
export function showBoot() { export function showBoot() {
// pulse green, play startup sound, turn off light // pulse green, play startup sound, turn off light
brick.setStatusLight(StatusLight.GreenPulse); brick.setStatusLight(StatusLight.GreenPulse);
// We pause for 100ms to give time to read sensor values, so they work in on_start block // We pause to give time to read sensor values, so they work in on_start block
pause(400) pause(400); // It turns out that this time is not enough for the simulator to display the LED change
// and we're ready // and we're ready
brick.setStatusLight(StatusLight.Off); brick.setStatusLight(StatusLight.Off);
// always show port by default if no UI is set // always show port by default if no UI is set