diff --git a/libs/core/control.cpp b/libs/core/control.cpp index c291a452..9f0042d5 100644 --- a/libs/core/control.cpp +++ b/libs/core/control.cpp @@ -27,4 +27,13 @@ int allocateNotifyEvent() { void dmesg(String s) { DMESG("# %s", s->data); } + } + +namespace serial { + /** Send DMESG debug buffer over serial. */ + //% + void writeDmesg() { + pxt::dumpDmesg(); + } +} \ No newline at end of file diff --git a/libs/core/core.ts b/libs/core/core.ts index 4f896f65..b2f78820 100644 --- a/libs/core/core.ts +++ b/libs/core/core.ts @@ -27,6 +27,7 @@ const enum LMS { DEVICE_TYPE_MINITACHO = 8, DEVICE_TYPE_NEWTACHO = 9, DEVICE_TYPE_TOUCH = 16, + DEVICE_TYPE_IR = 33, DEVICE_TYPE_THIRD_PARTY_START = 50, DEVICE_TYPE_THIRD_PARTY_END = 99, DEVICE_TYPE_IIC_UNKNOWN = 100, diff --git a/libs/core/dmesg.txt b/libs/core/dmesg.txt new file mode 100644 index 00000000..319d31f8 --- /dev/null +++ b/libs/core/dmesg.txt @@ -0,0 +1,6 @@ +[ 1] runtime starting... +[ 53] runtime started [V0.60] +[ 55] mmap /dev/lms_analog len=5172 off=0 +[ 56] mmap /dev/lms_uart len=42744 off=0 +[ 64] Error: 7 [0] +[ 64] PANIC 42 diff --git a/libs/core/input.ts b/libs/core/input.ts index 59d40fd3..7c3475c6 100644 --- a/libs/core/input.ts +++ b/libs/core/input.ts @@ -80,16 +80,19 @@ namespace input.internal { info.connType = newConn info.devType = LMS.DEVICE_TYPE_NONE if (newConn == LMS.CONN_INPUT_UART) { + control.dmesg(`new UART connection at ${info.port}`) setUartMode(info.port, 0) let uinfo = readUartInfo(info.port, 0) info.devType = uinfo[TypesOff.Type] + control.dmesg(`UART type ${info.devType}`) } else if (newConn == LMS.CONN_INPUT_DUMB) { + control.dmesg(`new DUMB connection at ${info.port}`) // TODO? for now assume touch info.devType = LMS.DEVICE_TYPE_TOUCH - } else if (newConn == LMS.CONN_NONE) { - // OK + } else if (newConn == LMS.CONN_NONE || newConn == 0) { + control.dmesg(`disconnect at ${info.port}`) } else { - // ??? + control.dmesg(`unknown connection type: ${newConn} at ${info.port}`) } } @@ -106,12 +109,16 @@ namespace input.internal { for (let info of autos) { if (!info.sensor && info.devType != LMS.DEVICE_TYPE_NONE) { + let found = false for (let s of autoSensors) { if (s.getPort() == 0 && s._deviceType() == info.devType) { s._setPort(info.port + 1) + found = true break } } + if (!found) + control.dmesg(`sensor not found for type=${info.devType} at ${info.port}`) } } } @@ -133,6 +140,8 @@ namespace input.internal { _setPort(port: number, manual = false) { port = Math.clamp(0, 4, port | 0) - 1; if (port == this.port) return + this.port = port + control.dmesg(`sensor set port ${port} on devtype=${this._deviceType()}`) for (let i = 0; i < sensors.length; ++i) { if (i != this.port && sensors[i].sensor == this) { sensors[i] = null @@ -190,27 +199,33 @@ namespace input.internal { export class UartSensor extends Sensor { protected mode: number + protected realmode: number constructor() { super() + this.mode = 0 + this.realmode = -1 } protected _portUpdated() { - this.mode = -1 + this.realmode = -1 if (this.port > 0) { if (this.isManual()) { uartReset(this.port) } else { - this.mode = 0 + this.realmode = 0 } + this._setMode(this.mode) } } protected _setMode(m: number) { - if (this.port < 0) return + //control.dmesg(`_setMode p=${this.port} m: ${this.realmode} -> ${m}`) let v = m | 0 - if (v != this.mode) { - this.mode = v + this.mode = v + if (this.port < 0) return + if (this.realmode != this.mode) { + this.realmode = v setUartMode(this.port, v) } } @@ -226,6 +241,7 @@ namespace input.internal { function uartReset(port: number) { if (port < 0) return + control.dmesg(`UART reset at ${port}`) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, LMS.CONN_NONE) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, 0) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, 0) @@ -272,6 +288,7 @@ namespace input.internal { const UART_PORT_CHANGED = 1 while (true) { if (port < 0) return + control.dmesg(`UART set mode to ${mode} at ${port}`) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Connection + port, LMS.CONN_INPUT_UART) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Type + port, 33) devcon.setNumber(NumberFormat.Int8LE, DevConOff.Mode + port, mode) diff --git a/libs/core/ir.ts b/libs/core/ir.ts index 1ff39308..b9878e93 100644 --- a/libs/core/ir.ts +++ b/libs/core/ir.ts @@ -77,6 +77,10 @@ namespace input { } } + _deviceType() { + return LMS.DEVICE_TYPE_IR + } + setRemoteChannel(c: IrRemoteChannel) { c = Math.clamp(0, 3, c | 0) this.channel = c diff --git a/libs/core/linux.cpp b/libs/core/linux.cpp index a7ee17e1..f1af78d2 100644 --- a/libs/core/linux.cpp +++ b/libs/core/linux.cpp @@ -52,6 +52,7 @@ static struct Thread *allThreads; static struct Event *eventHead, *eventTail; static int usbFD; static int dmesgPtr; +static int dmesgSerialPtr; static char dmesgBuf[4096]; struct Event { @@ -366,9 +367,11 @@ uint32_t afterProgramPage() { return 0; } void dumpDmesg() { - sendSerial("\nDMESG:\n", 8); - sendSerial(dmesgBuf, dmesgPtr); - sendSerial("\n\n", 2); + auto len = dmesgPtr - dmesgSerialPtr; + if (len == 0) + return; + sendSerial(dmesgBuf + dmesgSerialPtr, len); + dmesgSerialPtr = dmesgPtr; } int lmsPid; @@ -458,6 +461,7 @@ void dmesgRaw(const char *buf, uint32_t len) { return; if (dmesgPtr + len > sizeof(dmesgBuf)) { dmesgPtr = 0; + dmesgSerialPtr = 0; } memcpy(dmesgBuf + dmesgPtr, buf, len); dmesgPtr += len; diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index d6da5179..00e0e887 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -56,6 +56,12 @@ declare namespace control { //% shim=control::dmesg function dmesg(s: string): void; } +declare namespace serial { + + /** Send DMESG debug buffer over serial. */ + //% shim=serial::writeDmesg + function writeDmesg(): void; +} declare namespace input { /** diff --git a/libs/core/test.ts b/libs/core/test.ts index a96989d8..722baf3c 100644 --- a/libs/core/test.ts +++ b/libs/core/test.ts @@ -26,12 +26,27 @@ input.buttonUp.onEvent(ButtonEvent.Click, () => { screen.scroll(10) }) -for (let i = 0; i < 3; ++i) { - loops.forever(() => { - let r = Math.randomRange(0, 100) - serial.writeValue("R", r) - //screen.drawText(10, 10, `R=${r}`) - loops.pause(1000) - }) - loops.pause(300) -} + +let num = 0 + +input.touch.button.onEvent(ButtonEvent.Click, () => { + screen.drawText(10, 60, "Click! " + num) + num++ +}) + +input.ir.getRemoteCommand() + +input.ir.button(IrRemoteButton.TopLeft).onEvent(ButtonEvent.Down, () => { + screen.drawText(10, 60, "TOPLEFT " + num) + num++ +}) + +input.ir.button(IrRemoteButton.TopRight).onEvent(ButtonEvent.Down, () => { + screen.drawText(10, 60, "TOPRIGH " + num) + num++ +}) + +loops.forever(() => { + serial.writeDmesg() + loops.pause(400) +}) diff --git a/libs/core/touch.ts b/libs/core/touch.ts index dfdf1865..172bb455 100644 --- a/libs/core/touch.ts +++ b/libs/core/touch.ts @@ -14,6 +14,10 @@ namespace input { _update(prev: number, curr: number) { this.button.update(curr > 0) } + + _deviceType() { + return LMS.DEVICE_TYPE_TOUCH + } } //% whenUsed