c5cec3a6ba
* bump pxt * fix build issues * Auto-gen of projects/summary * removing feild editors moved to pxt * various typing fixes * more typing fixes * fixing various typing issues * Start on integration of new pxt * serial number fixes * gc-ify MMap object * Re-build generated files * fix console listeners * clear lf() warnings * More generated files * also auto-generated * Compilation fixes * fix merge * mostly fixing blocks * fix sim * fix field motors * enable a few features * moving to tsx * try to fix edtiro compilation * more defs * removing commands * removing extra $ * fix blockly warning * hiding images * enabling more pxt features * hide images * setup autorun * add lock on target_reset * update deps * return trylock result * updated pxt * rename video section * add alpha channel * upgraded pxt * bump pxt/version * removed alpha ref * var ceanup * don't do major bump
61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
namespace console {
|
|
/**
|
|
* Sends the log messages to the brick screen and uses the brick up and down buttons to scroll.
|
|
*/
|
|
//% blockId=logsendtostreen block="send console to screen"
|
|
//% weight=1
|
|
//% help=console/send-to-screen
|
|
export function sendToScreen(): void {
|
|
console._screen.attach();
|
|
}
|
|
}
|
|
|
|
namespace console._screen {
|
|
const maxLines = 100;
|
|
let lines: string[];
|
|
let scrollPosition = 0;
|
|
|
|
export function attach() {
|
|
if (!lines) {
|
|
lines = [];
|
|
console.addListener(log);
|
|
brick.buttonUp.onEvent(ButtonEvent.Bumped, () => scroll(-3))
|
|
brick.buttonDown.onEvent(ButtonEvent.Bumped, () => scroll(3))
|
|
}
|
|
}
|
|
|
|
function printLog() {
|
|
brick.clearScreen()
|
|
if (!lines) return;
|
|
const screenLines = brick.lineCount();
|
|
for (let i = 0; i < screenLines; ++i) {
|
|
const line = lines[i + scrollPosition];
|
|
if (line)
|
|
screen.print(line, 0, 4 + i * brick.lineHeight(), 1, brick.font)
|
|
}
|
|
}
|
|
|
|
function scroll(pos: number) {
|
|
if (!pos) return;
|
|
|
|
scrollPosition += pos >> 0;
|
|
if (scrollPosition >= lines.length) scrollPosition = lines.length - 1;
|
|
if (scrollPosition < 0) scrollPosition = 0;
|
|
printLog();
|
|
}
|
|
|
|
function log(priority: ConsolePriority, msg: string): void {
|
|
lines.push(msg);
|
|
if (lines.length + 5 > maxLines) {
|
|
lines.splice(0, maxLines - lines.length);
|
|
scrollPosition = Math.min(scrollPosition, lines.length - 1)
|
|
}
|
|
// move down scroll once it gets large than the screen
|
|
const screenLines = brick.lineCount();
|
|
if (lines.length > screenLines
|
|
&& lines.length >= scrollPosition + screenLines) {
|
|
scrollPosition++;
|
|
}
|
|
printLog();
|
|
}
|
|
} |