From d78d9c8686749fceb908ca058ee04b0a05279041 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 19 Dec 2017 14:26:57 -0800 Subject: [PATCH] basic ports view (#115) * basic ports view * slight adjustement of rendering --- libs/core/_locales/core-jsdoc-strings.json | 1 + libs/core/_locales/core-strings.json | 1 + libs/core/console.ts | 3 +-- libs/core/input.ts | 5 +++++ libs/core/output.ts | 9 ++++++-- libs/core/screen.ts | 26 ++++++++++++++++++++++ libs/ultrasonic-sensor/ultrasonic.ts | 4 ++-- 7 files changed, 43 insertions(+), 6 deletions(-) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 500cd19b..45c8cab1 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -32,6 +32,7 @@ "brick.lightPattern": "Pattern block.", "brick.lightPattern|param|pattern": "the lights pattern to use. eg: LightsPattern.Green", "brick.print": "Show text on the screen.", + "brick.printPorts": "Prints the port states on the screen", "brick.print|param|text": "the text to print on the screen, eg: \"Hello world\"", "brick.print|param|x": "the starting position's x coordinate, eg: 0", "brick.print|param|y": "the starting position's x coordinate, eg: 0", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 8c0a7ea3..7fa5369f 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -36,6 +36,7 @@ "brick.buttonUp|block": "up", "brick.clearScreen|block": "`icons.brickDisplay` clear screen", "brick.lightPattern|block": "%pattern", + "brick.printPorts|block": "print ports", "brick.print|block": "`icons.brickDisplay` print %text| at x: %x| y: %y", "brick.setPixel|block": "`icons.brickDisplay` set pixel %on| at x: %x| y: %y", "brick.setStatusLight|block": "set `icons.brickButtons` to %pattern=led_pattern", diff --git a/libs/core/console.ts b/libs/core/console.ts index 217f46b6..70aa626e 100644 --- a/libs/core/console.ts +++ b/libs/core/console.ts @@ -59,7 +59,6 @@ namespace console { namespace console.screen { const maxLines = 100; const screenLines = 8; - const lineHeight = 12; let lines: string[]; let scrollPosition = 0; @@ -78,7 +77,7 @@ namespace console.screen { for (let i = 0; i < screenLines; ++i) { const line = lines[i + scrollPosition]; if (line) - brick.print(line, 0, 4 + i * lineHeight) + brick.print(line, 0, 4 + i * brick.LINE_HEIGHT) } } diff --git a/libs/core/input.ts b/libs/core/input.ts index 1fa197c4..896c8a3c 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -74,6 +74,11 @@ namespace sensors.internal { } + export function getActiveSensors(): Sensor[] { + init(); + return sensorInfos.filter(si => si.sensor && si.sensor.isActive()).map(si => si.sensor); + } + function readUartInfo(port: number, mode: number) { let buf = output.createBuffer(UartCtlOff.Size) buf[UartCtlOff.Port] = port diff --git a/libs/core/output.ts b/libs/core/output.ts index 4d45d778..cdd980ee 100644 --- a/libs/core/output.ts +++ b/libs/core/output.ts @@ -548,7 +548,7 @@ namespace motors { return 0 } - interface MotorData { + export interface MotorData { actualSpeed: number; // -100..+100 tachoCount: number; count: number; @@ -557,7 +557,7 @@ namespace motors { // only a single output at a time function getMotorData(out: Output): MotorData { init() - let buf = motorMM.slice(outOffset(out), MotorDataOff.Size) + const buf = motorMM.slice(outOffset(out), MotorDataOff.Size) return { actualSpeed: buf.getNumber(NumberFormat.Int8LE, MotorDataOff.Speed), tachoCount: buf.getNumber(NumberFormat.Int32LE, MotorDataOff.TachoCounts), @@ -565,6 +565,11 @@ namespace motors { } } + export function getAllMotorData(): MotorData[] { + init(); + return [Output.A, Output.B, Output.C, Output.D].map(out => getMotorData(out)); + } + interface SyncOptions { useSteps?: boolean; speed: number; diff --git a/libs/core/screen.ts b/libs/core/screen.ts index bee19974..2c189c15 100644 --- a/libs/core/screen.ts +++ b/libs/core/screen.ts @@ -1,4 +1,6 @@ namespace brick { + export const LINE_HEIGHT = 12; + //% shim=screen::_setPixel function _setPixel(p0: uint32, p1: uint32, mode: Draw): void { } @@ -208,5 +210,29 @@ namespace brick { setLineCore(x, x1, y, mode); } + /** + * Prints the port states on the screen + */ + //% blockId=brickPrintPorts block="print ports" + //% weight=1 group="Screen" + export function printPorts() { + clearScreen(); + // motors + const datas = motors.getAllMotorData(); + for(let i = 0; i < datas.length; ++i) { + const x = i * 52; + const data = datas[i]; + print(`${data.actualSpeed}%`, x, brick.LINE_HEIGHT) + print(`${data.count}>`, x, 2 * 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) * 52; + print(`${si._query()}`, x, 9 * brick.LINE_HEIGHT) + } + } } \ No newline at end of file diff --git a/libs/ultrasonic-sensor/ultrasonic.ts b/libs/ultrasonic-sensor/ultrasonic.ts index 8c2473a0..097749ca 100644 --- a/libs/ultrasonic-sensor/ultrasonic.ts +++ b/libs/ultrasonic-sensor/ultrasonic.ts @@ -25,7 +25,7 @@ namespace sensors { } _query(): number { - return (this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) * 0.1; // range is 0..2550, in 0.1 cm increments. + return ((this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) / 10) >> 0; // range is 0..2550, in 0.1 cm increments. } _update(prev: number, curr: number) { @@ -80,7 +80,7 @@ namespace sensors { distance(): number { // it supposedly also has an inch mode, but we stick to cm this._setMode(0) - return (this.getNumber(NumberFormat.UInt16LE, 0) & 0x0fff) * 0.1; // range is 0..2550, in 0.1 cm increments. + return this._query(); } }