Erase on showstring + showports improvements (#402)

* erase line before show string + show ports upgrade

* fixed font size

* fixing console output
This commit is contained in:
Peli de Halleux 2018-04-03 04:30:23 -07:00 committed by GitHub
parent 3339a7660a
commit 69194b0b76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 54 deletions

View File

@ -1,40 +0,0 @@
# Print Ports
```typescript
/**
* Print the port states on the screen
*/
//% blockId=brickPrintPorts block="print ports"
//% help=brick/print-ports
//% weight=1 group="Screen"
function printPorts() {
const col = 44;
clearScreen();
function scale(x: number) {
if (Math.abs(x) > 1000) return Math.round(x / 100) / 10 + "k";
return ("" + (x >> 0));
}
// motors
const datas = motors.getAllMotorData();
for(let i = 0; i < datas.length; ++i) {
const data = datas[i];
if (!data.actualSpeed && !data.count) continue;
const x = i * col;
print(`${scale(data.actualSpeed)}%`, x, brick.LINE_HEIGHT)
print(`${scale(data.count)}>`, x, 2 * brick.LINE_HEIGHT)
print(`${scale(data.tachoCount)}|`, x, 3 * brick.LINE_HEIGHT)
}
// sensors
const sis = sensors.internal.getActiveSensors();
for(let i =0; i < sis.length; ++i) {
const si = sis[i];
const x = (si.port() - 1) * col;
const v = si._query();
print(`${scale(v)}`, x, 9 * brick.LINE_HEIGHT)
}
}
```

View File

@ -59,7 +59,6 @@ namespace console {
namespace console._screen { namespace console._screen {
const maxLines = 100; const maxLines = 100;
const screenLines = 10;
let lines: string[]; let lines: string[];
let scrollPosition = 0; let scrollPosition = 0;
@ -75,10 +74,11 @@ namespace console._screen {
function printLog() { function printLog() {
brick.clearScreen() brick.clearScreen()
if (!lines) return; if (!lines) return;
const screenLines = brick.lineCount();
for (let i = 0; i < screenLines; ++i) { for (let i = 0; i < screenLines; ++i) {
const line = lines[i + scrollPosition]; const line = lines[i + scrollPosition];
if (line) if (line)
screen.print(line, 0, 4 + i * brick.LINE_HEIGHT) screen.print(line, 0, 4 + i * brick.lineHeight(), 1, brick.font)
} }
} }
@ -98,6 +98,7 @@ namespace console._screen {
scrollPosition = Math.min(scrollPosition, lines.length - 1) scrollPosition = Math.min(scrollPosition, lines.length - 1)
} }
// move down scroll once it gets large than the screen // move down scroll once it gets large than the screen
const screenLines = brick.lineCount();
if (lines.length > screenLines if (lines.length > screenLines
&& lines.length >= scrollPosition + screenLines) { && lines.length >= scrollPosition + screenLines) {
scrollPosition++; scrollPosition++;

View File

@ -20,22 +20,45 @@ namespace _screen_internal {
} }
namespace brick { namespace brick {
export const LINE_HEIGHT = 12; const textOffset = 4;
const lineOffset = 2;
export let font = image.font8;
/**
* Gets the text line height
*/
//%
export function lineHeight(): number {
return font.charHeight + lineOffset;
}
/**
* Number of lines
*/
//%
export function lineCount(): number {
return ((screen.height - textOffset) / lineHeight()) >> 0
}
/** /**
* Show text on the screen at a specific line. * Show text on the screen at a specific line.
* @param text the text to print on the screen, eg: "Hello world" * @param text the text to print on the screen, eg: "Hello world"
* @param line the line number to print the text at, eg: 1 * @param line the line number to print the text at (starting at 1), eg: 1
*/ */
//% blockId=screen_print block="show string %text|at line %line" //% blockId=screen_print block="show string %text|at line %line"
//% weight=98 group="Screen" inlineInputMode="inline" blockGap=8 //% weight=98 group="Screen" inlineInputMode="inline" blockGap=8
//% help=brick/show-string //% help=brick/show-string
//% line.min=1 line.max=10 //% line.min=1 line.max=10
export function showString(text: string, line: number) { export function showString(text: string, line: number) {
const NUM_LINES = 9; // line indexing starts at 1.
const offset = 5; line = (line - 1) >> 0;
const y = offset + (Math.clamp(0, NUM_LINES, line - 1) / (NUM_LINES + 2)) * DAL.LCD_HEIGHT; const nlines = lineCount();
screen.print(text, offset, y); if (line < 0 || line > nlines) return; // out of screen
const h = lineHeight();
const y = textOffset + h * line;
screen.fillRect(0, y, screen.width, h, 0); // clear background
screen.print(text, textOffset, y, 1, font);
} }
/** /**
@ -85,6 +108,7 @@ namespace brick {
//% weight=10 group="Screen" //% weight=10 group="Screen"
export function showPorts() { export function showPorts() {
const col = 44; const col = 44;
const lineHeight8 = image.font8.charHeight + 2;
clearScreen(); clearScreen();
function scale(x: number) { function scale(x: number) {
@ -98,19 +122,22 @@ namespace brick {
const data = datas[i]; const data = datas[i];
if (!data.actualSpeed && !data.count) continue; if (!data.actualSpeed && !data.count) continue;
const x = i * col; const x = i * col;
screen.print("ABCD"[i], x, brick.LINE_HEIGHT) screen.print("ABCD"[i], x + 2, 1 * lineHeight8, 1, image.font8)
screen.print(`${scale(data.actualSpeed)}%`, x, 2* brick.LINE_HEIGHT) screen.print(`${scale(data.actualSpeed)}%`, x + 2, 3 * lineHeight8, 1, image.font8)
screen.print(`${scale(data.count)}>`, x, 3 * brick.LINE_HEIGHT) screen.print(`${scale(data.count)}>`, x + 2, 4 * lineHeight8, 1, image.font5)
} }
screen.drawLine(0, 5 * lineHeight8, screen.width, 5 * lineHeight8, 1);
// sensors // sensors
const sis = sensors.internal.getActiveSensors(); const sis = sensors.internal.getActiveSensors();
const h = screen.height;
screen.drawLine(0, h - 5 * lineHeight8, screen.width, h - 5 * lineHeight8, 1)
for (let i = 0; i < sis.length; ++i) { for (let i = 0; i < sis.length; ++i) {
const si = sis[i]; const si = sis[i];
const x = (si.port() - 1) * col; const x = (si.port() - 1) * col;
const inf = si._info(); const inf = si._info();
screen.print(si.port() + "", x, 8 * brick.LINE_HEIGHT) screen.print(si.port() + "", x, h - 4 * lineHeight8, 1, image.font8)
screen.print(inf, x, 9 * brick.LINE_HEIGHT) screen.print(inf, x, h - 2 * lineHeight8, 1, inf.length > 4 ? image.font5 : image.font8);
} }
} }