Basic Array/Char manipulation

02 Mar 2010

Hi all,

Please forgive how basic this is, I'm aware the knowledge needed can be found in books etc but I can't grasp this on my own.

I'm trying to take input from a PS/2 keyboard (which works printing char's to an LCD just fine), and write each character into the array 'message', until it is full.

I've tried compiling the code below, but the ouput says a 'const char*' can't be written to an entity of type 'char'. Does anyone know why this is please? The main line of interest is line 20, with the note next to it.

 

Any help would be much appreciated. Thanks.

 

Simon

 

 

 

#include "mbed.h"
#include "PS2ASCII.h"
#include "TextLCD.h"

PS2ASCII myKeyboard(p9, p10);
TextLCD lcd(p24, p25, p26, p27, p28, p29, p30,20,4);
DigitalOut LED(LED1);
Serial mbed2(p13 ,p14);

char message[20];
int i;
int charcount;

int main()
{
while (1) {
char symbol = myKeyboard.getChar();
//insert something here to watch for a carriage return line feed
//insert something here to initialise write loop for the message array, to send over serial
//do this loop for however many characters charcount says
for (i=0; message[i]; i++)                                //move to the next progressive char of the message array
message[i]="%c", symbol;                        //*NOT SURE ON THIS LINE* write symbol to this char in the message array
//increase charcount by one
if(myKeyboard.E0flag() == true) {                  //is the make code one or two bytes long?
lcd.printf(" E0 ");
LED = 1;
}
lcd.printf("%c", symbol);
}
}

03 Mar 2010

Not sure if understand your problem correctly but would this work?

charcount=0;

maxmessage=20;

while(1) {

char symbol = myKeyboard.getChar();

message(charcount) = symbol;

charcount++;

if (charcount > maxmessage) {

//message array full

}

 

}

03 Mar 2010

Ok, that makes sense. I'll try compiling it tonight.

The 'message(charcount) = symbol;' line is what I was after. I just didn't understand the syntax of sending a character into an array from something, in this case from 'symbol'.

I'm guessing the message variable is implied as being defined already.

Thanks,

Simon

03 Mar 2010 . Edited: 03 Mar 2010

I think the line you want is

message[i] = symbol;

 

Your original line

message[i] = "%c", symbol;

means the same thing as

(message[i] = "%c"), symbol;

which means: first put a pointer to the string literal "%c" into the character at message[i]; then get the value of symbol (and do nothing with it).  Since the pointer (the "const char *" in the error message) isn't the right type (and there's no default way to convert it) the compiler gives an error.

 

Also, it's worth noting that nothing is going to stop the code from trying to put in the 22nd character message[21] and further. If that happens all bets are off and anything can happen ("undefined behavior"). This is often referred to as a buffer overrun and is the source of security problems.

03 Mar 2010

No probs Simon - probably worth doing some reading on arrays though still.

You define the message variable with char message[20] which is saying you want message to be a character array of size 21.

You can then assign characters to the array by doing message[0]='t', message[1]='e' etc. up until message[20] - in your case you want to progressively assign the symbol variable which is why I have used charcount as the index counter.

Scott's point above is correct but I re-wrote your code because I wasn't sure it would work even fixing this line.

 

03 Mar 2010

Sure, I understand. Thanks gents.

Now, for the time being, once that 'if' line has gone true and the array is full, I'd just like it to wait for a CRLF to send, and not try and put anything else into the array.

The only way I can think of doing this is putting another infinite loop inside that is exited once a CRLF is detected. Does that seem a good idea?

03 Mar 2010

Simon Pettitt wrote:

Sure, I understand. Thanks gents.

Now, for the time being, once that 'if' line has gone true and the array is full, I'd just like it to wait for a CRLF to send, and not try and put anything else into the array.

The only way I can think of doing this is putting another infinite loop inside that is exited once a CRLF is detected. Does that seem a good idea?

Something like this...

#include "mbed.h"
#include "PS2ASCII.h"
#include "TextLCD.h"

PS2ASCII myKeyboard(p9, p10);
TextLCD lcd(p24, p25, p26, p27, p28, p29, p30,20,4);
DigitalOut LED(LED1);
DigitalIn clearbutton(p5);
Serial mbed2(p13 ,p14);

char message[20];
int maxmessage =20;
int charcount  =0;

int main()
{
while (1) {
char symbol = myKeyboard.getChar();
//insert something here to watch for a carriage return line feed
//insert something here to initialise write loop for the message array
//to send over serial
message[charcount] = symbol;
charcount++;                               //increase charcount by one
if (charcount > maxmessage)                //message array full
{while(1)
{if (symbol == 0x00);   //example CRLF
{} //exit to write sequence here
else if (clearbutton = 1);
{} //clear array and clear LCD
}
}
if(myKeyboard.E0flag() == true) {          //is the make code one or two bytes long?
lcd.printf(" E0 ");
LED = 1;
}
lcd.printf("%c", symbol);
}
}

03 Mar 2010

Don't think you need the extra while - an if statement within the first should cover it.

 

eg:

 

if (charcount <= maxmessage) {

message[charcount]=symbol;

charcount++;

} else if { (charcount > maxmessage) {

if (symbol == 0x00) {   //example CRLF
//exit to write sequence here

}

} else if (clearbutton = 1) {
//clear array and clear LCD

}