pxt-ev3/libs/ev3/console.ts
Michał Moskal 8ed79e7133 Swap X/y in images (#528)
* Swap X/Y in image encoding

* Fix PNG decoder

* Now really fix png

* reducing deps

* moving png.cpp into ev3

* bumped pxt

* updated shims

* fixing c++ compilation

* updated shims

* Fix pixel order

* update pxt

* Fix C++ image decoding

* Add expanded PNG images

* Generate E1 format images (in libs/ev3 run 'pxt buildsprites images')

* Go back to white background

* Remove PNG support
2018-04-19 12:08:16 -07:00

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(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();
}
}