From 3d3b9ab5fbb35aa875d62b5058bd4357874a2ba1 Mon Sep 17 00:00:00 2001 From: "Collin J. Doering" Date: Wed, 7 Oct 2015 22:38:57 -0400 Subject: [PATCH] Working backspace and fixed bug with cursor Before this commit the cursor would be incremented (automatically by hardware) after printing the last character on the line, which would leave it the next line in memory, which doesn't necessarily correspond to the next physical line on the display. This would be corrected when the next character is received but is incorrect behavior; the cursor should always be at where the next character is to inserted. This was due to an off-by-one logical error. This commit solves this bug. Along with the bug fix, keycodes from the serial terminal are now deciphered correctly and the corresponding key or keys are sent to the LCD accordingly. Further work is required with regards to updating the serial console (the connected client) so that their serial console looks exactly like the LCD they are connected to. Signed-off-by: Collin J. Doering --- lcdLib.c | 41 +++++++++++++++++++++++++++++------------ lcdOutput.c | 17 +++++++++++++++-- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/lcdLib.c b/lcdLib.c index a8d3974..41b30ff 100644 --- a/lcdLib.c +++ b/lcdLib.c @@ -200,7 +200,19 @@ void writeCharToLCD(char c) { case '\a': // Alarm break; ; - case '\b': // Backspace + case '\b': // Backspace (non-destructive) + if (currentLineChars == 0 && currentLineNum == 0) { + // At first line, first column; there is no where to move; do nothing + break; + } else if (currentLineChars == 0) { + // At beginning of line, need to move the end of previous line + currentLineChars = LCD_CHARACTERS_PER_LINE - 1; + writeLCDInstr(INSTR_DDRAM_ADDR | (lineBeginnings[--currentLineNum] + currentLineChars)); + } else { + // OK, simply go back one character + writeLCDInstr(INSTR_DDRAM_ADDR | (lineBeginnings[currentLineNum] + --currentLineChars)); + } + break; ; case '\r': // Carriage return @@ -208,19 +220,24 @@ void writeCharToLCD(char c) { currentLineChars = 0; break; ; + case '\f': // Form feed + clearDisplay(); + break; + ; default: - if (currentLineChars == LCD_CHARACTERS_PER_LINE) { - if (currentLineNum == LCD_NUMBER_OF_LINES - 1) { - clearDisplay(); - } else { - writeLCDInstr(INSTR_DDRAM_ADDR | lineBeginnings[++currentLineNum]); - currentLineChars = 0; - } - } + if (currentLineChars == LCD_CHARACTERS_PER_LINE - 1 && currentLineNum == LCD_NUMBER_OF_LINES - 1) { + clearDisplay(); + } else if (currentLineChars == LCD_CHARACTERS_PER_LINE - 1) { + loop_until_LCD_BF_clear(); // Wait until LCD is ready for new instructions + writeCharToLCD_(c); + currentLineChars = 0; - loop_until_LCD_BF_clear(); // Wait until LCD is ready for new instructions - writeCharToLCD_(c); - currentLineChars++; + writeLCDInstr(INSTR_DDRAM_ADDR | lineBeginnings[++currentLineNum]); + } else { + loop_until_LCD_BF_clear(); // Wait until LCD is ready for new instructions + writeCharToLCD_(c); + currentLineChars++; + } ; } } diff --git a/lcdOutput.c b/lcdOutput.c index fc27d20..dbc09d2 100644 --- a/lcdOutput.c +++ b/lcdOutput.c @@ -46,8 +46,21 @@ int main(void) { while (1) { serialChar = receiveByte(); - writeCharToLCD(serialChar); - transmitByte(serialChar); // Echo character back to serial console + + switch (serialChar) { + case '\r': + writeStringToLCD("\r\n"); + break; + ; + case 0x7f: // Backspace (sent as delete) + writeStringToLCD("\b \b"); + break; + ; + default: + writeCharToLCD(serialChar); + transmitByte(serialChar); // Echo character back to serial console + ; + } } return 0;