Welcome! Log In Create A New Profile

Advanced

Problem with reading key press on BV4618

Posted by FrankenPC 
Problem with reading key press on BV4618
July 01, 2013 08:49PM
Hi.

I'm using an Arduino Mega with the BV4618 in SERIAL mode. I'm new to the game so I couldn't figure out how to use the BV libraries. Therefore, I rolled my own. The LCD works GREAT. No problems with any command strings or printing to the LCD. I created routines to read the response for a key press and I DO get the correct key back but not before the LCD prints out a little garbage:

First I send: "\n[k" to initiate a read:

LCDCommand("[k");

void LCDCommand(String text)
{
LCDPrintChar(27);
LCDPrint(text);
}

Then, I proceed to read and convert the output to the actual key label (1-9, ABCD, *,#)

When I execute the main routine, the LCD first prints "[kk" then the proper label. So for key "A" for example,
LCD looks like this:
"[kkA"

I can't get rid of the "[kk" no matter what I do. It's nothing in my code as far as I can tell. No other LCDCommand issued does this. JUST the "\n[k" command.

It is some kind of delay problem? Like I need to inject a delay after issuing the "\n[k" command?

THANKS for any advice you can offer.


FYI: My library so far:

/*

Key Map
1 2 3 A
4 5 6 B
7 8 9 C
* 0 # D

EE DE BE 7E
ED DD BD 7D
EB DB BB 7B
E7 D7 B7 77

238 222 190 126
237 221 189 125
235 219 187 123
231 215 183 119
*/


int ackChar = 0;


void InitLCD()
{
Serial2.begin(38400);
LCDPrintChar(13);
delay(300);
Set20X4Mode();
delay(50);
LCDReset();
delay(50);
//LCDSetAck(42); //42 = * (0=off)
}

//Base operations----------------------
void LCDPrint (String text)
{
Serial2.print(text);
}
void LCDPrintChar (int chr)
{
Serial2.write(chr);
}
void LCDCommand(String text)
{
LCDPrintChar(27);
LCDPrint(text);
}
void LCDSetAck(int chr)
{
String a = "k";
String command = "[";
command = command + chr + a;
LCDCommand(command);
ackChar = chr;
}
int LCDRead()
{
int in = -1;
int timeout = 0;
int i = 500; //~500ms delay for port data return

while(LCDSerialDataAvailable() == 0)
{
delay(1);
i -= 1; if (i == 0)
{
timeout = 1;
break;
}
}

if (timeout == 1) return -1;

in = Serial2.read();
if (in <= 47 || in >= 58 ) //char must be 0-9 only
{in = -1; return in;}

in -= 48;
return in;
}
String LCDReadStream()
{
int in = 0;
String result = "";

//must return a valid positive 3 digit integer
in = LCDRead();
if (in < 1) return "";
result += in;

in = LCDRead();
if (in < 0) return "";
result += in;

in = LCDRead();
if (in < 0) return "";
result += in;

return result;
}
int LCDReadAck()
{
LCDCommand("a");
delay(50);
return LCDRead();
}
int LCDSerialDataAvailable()
{
return Serial2.available();
}

// Keyboard operations-------------------
int LCDKeysInBuffer()
{
String in = "";
LCDCommand("[n");
in = LCDReadStream();
}
void LCDClearKeyBuffer()
{
LCDCommand("[c");
}
char LCDMapKey(String text)
{
//values between 119 and 238
if (text == "238") return '1';
if (text == "237") return '4';
if (text == "235") return '7';
if (text == "231") return '*';
if (text == "222") return '2';
if (text == "221") return '5';
if (text == "219") return '8';
if (text == "215") return '0';
if (text == "190") return '3';
if (text == "189") return '6';
if (text == "187") return '9';
if (text == "183") return '#';
if (text == "126") return 'A';
if (text == "125") return 'B';
if (text == "123") return 'C';
if (text == "119") return 'D';
return 'N';
}
char LCDGetKey()
{
String msg = "";
char key = 'N';
LCDCommand("[k");
msg = LCDReadStream();
key = LCDMapKey(msg);
return key;
}


//Cursor position operations------------
void LCDPrintXY(int x, int y, String text)
{
String a = ";";
String b = "H";
String command = "[";
command = command + y + a + x + b + text;
LCDCommand(command);
}
void LCDCursorUp(int i)
{
String a = "A";
String command = "[";
command = command + i + a;
LCDCommand(command);
}
void LCDCursorDown(int i)
{
String a = "B";
String command = "[";
command = command + i + a;
LCDCommand(command);
}
void LCDCursorLeft(int i)
{
String a = "D";
String command = "[";
command = command + i + a;
LCDCommand(command);
}
void LCDCursorRight(int i)
{
String a = "C";
String command = "[";
command = command + i + a;
LCDCommand(command);
}

//Basic screen operations---------------
void LCDCRLF()
{
LCDPrintChar(10);
LCDPrintChar(13);
}
void Set20X4Mode()
{
LCDCommand("[4L");
LCDCommand("[20c");
}
void LCDLightOff()
{
LCDCommand("[?26I");
}
void LCDLightOn()
{
LCDCommand("[?26h");
}
void LCDHideCursor()
{
LCDCommand("[?25I");
}
void LCDShowCursor()
{
LCDCommand("[?25h");
}

//Screen clearing operations-------------
void LCDClear()
{
LCDCommand("[2J");
}
void LCDReset()
{
LCDClear();
LCDPrintXY(1,1,"");
}
void LCDClearLeft()
{
LCDCommand("[1K");
}
void LCDClearRight()
{
LCDCommand("[K");
}
void LCDClearLine()
{
LCDCommand("[2K");
}
Re: Problem with reading key press on BV4618
July 01, 2013 10:29PM
Problem appears to be with 38400 baud. 9600, 19200 work fine.
Sorry, only registered users may post in this forum.

Click here to login