From 92898c67a4da42992a8115ffa7b12781e49f2eae Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Sat, 7 Dec 2019 13:08:46 +0200 Subject: feat(usart): add onLineTransmit callback --- src/usart.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/usart.ts') diff --git a/src/usart.ts b/src/usart.ts index da5cee0..8d5691c 100644 --- a/src/usart.ts +++ b/src/usart.ts @@ -28,6 +28,7 @@ export const usart0Config: USARTConfig = { }; export type USARTTransmitCallback = (value: u8) => void; +export type USARTLineTransmitCallback = (value: string) => void; // Register bits const UCSRA_RXC = 0x80; // USART Receive Complete @@ -57,6 +58,9 @@ const UCSRC_UCPOL = 0x1; // Clock Polarity export class AVRUSART { public onByteTransmit: USARTTransmitCallback | null = null; + public onLineTransmit: USARTLineTransmitCallback | null = null; + + private lineBuffer: string = ''; constructor(private cpu: CPU, private config: USARTConfig, private freqMHz: number) { this.cpu.writeHooks[config.UCSRA] = (value) => { @@ -73,6 +77,15 @@ export class AVRUSART { if (this.onByteTransmit) { this.onByteTransmit(value); } + if (this.onLineTransmit) { + const ch = String.fromCharCode(value); + if (ch === '\n') { + this.onLineTransmit(this.lineBuffer); + this.lineBuffer = ''; + } else { + this.lineBuffer += ch; + } + } this.cpu.data[config.UCSRA] |= UCSRA_UDRE | UCSRA_TXC; }; } -- cgit v1.2.3