Hello, I have two questions:
1)What is the difference between lcd.write() and lcd.print() ?
2)If I want to print to an LCD exactly what I hit to a PS2 keyboard connected to the arduino,
for example backspace to delete a letter, Enter, etc, do the above functions support it?
gewrou:
Hello, I have two questions:
1)What is the difference between lcd.write() and lcd.print() ?
2)If I want to print to an LCD exactly what I hit to a PS2 keyboard connected to the arduino,
for example backspace to delete a letter, Enter, etc, do the above functions support it?
write() and print() are both derived from Stream Class
gewrou:
Hello, I have two questions:
1)What is the difference between lcd.write() and lcd.print() ?
2)If I want to print to an LCD exactly what I hit to a PS2 keyboard connected to the arduino,
for example backspace to delete a letter, Enter, etc, do the above functions support it?
The Print class is what implements those functions, but the both will call the write() function in the h/w library like Serial, LiquidCrystal, etc....
It is not the job of write() or print() in the Print class to support special characters. That type of processing would be handled by the write() function the h/w library or the h/w itself.
For most cases, there is no difference between using write() and print() as print() calls write()
i.e: here are some of the print() functions:
size_t Print::print(const String &s)
{
return write(s.c_str(), s.length());
}
size_t Print::print(const char str[])
{
return write(str);
}
size_t Print::print(char c)
{
return write(c);
}
With respect to #2, if you are expecting the lcd to behave like a terminal, that will likely be an issue as very view lcds handle special character processing and almost none of lcd libraries support processing of the non printable characters like etc...
to emulate the standard behaviours.
I'm not sure which lcd or library you are using but the hd44780 lcd h/w does not do any processing of special characters and the included LiquidCrystal library does not not support any sort of special character processing, end of line processing, or line wrap;
so sending , , , etc.. characters will not behave like it does on a typical terminal.
--- bill
Like everyone else said: the convention in most C libraries is write() does one byte at a time raw output, and print does formatting.
So write(65) will output a 65 byte (uppercase 'A'), and print(65) will output 54 and 53 bytes ('6', '5').
Thnx a lot!
PaulMurrayCbr:
Like everyone else said: the convention in most C libraries is write() does one byte at a time raw output, and print does formatting.So write(65) will output a 65 byte (uppercase 'A'), and print(65) will output 54 and 53 bytes ('6', '5').
That is not a good example for this discussion.
The output is different because because 65 in this case is an int (constants are ints)
and the discussion so far has been focused on char vs unsigned char / byte variables.
Consider this example:
char x = 65;
libobject.write(x);
libobject.print(x);
both output uppercase 'A'.
Like I said it can depend on the type being passed into the function since they are overloaded.
When passing in a char they behave the same.
When passing in an 8 bit int they will not behave the same as print() outputs the characters for the integer's value rather than the value.
In fact this is great example where you can see the difference between the three 8 bit types I mentioned earlier:
- char
- signed char
- unsigned char / byte
And how they are treated differently.
signed char and unsigned char are both 8 bit integers, while char is not an integer.
Yes, you can do integer math operations on a char variable, but it is not really an integer.
And since print() treats integers differently than chars you will get different output when you pass in a type of char vs signed char and unsigned char.
So the thing to keep in mind is that for 8 bit values:
write() will pass the value of the argument straight through
print() will pass the value of the argument straight through when it is char
print() will output the ASCII characters for the value when it is integer (signed char, unsigned char)
And this is because print() potentially does formatting.
print() potentially formats the data into some other format before calling write() with the formatted data.
If you call write() directly, write() hands the data directly to the h/w library
as write() does not do any formatting.
--- bill