I am reading over some code, this is set globally:
uint8_t line_buf[LINE_MAX] = "U8GLIB Console";
If I try to do the same thing in setup(){}
I get the error
invalid conversion from 'const char*' to 'uint8_t'
so how do I now put a string into that array? I dont get how usigned 8 bit integer can hold characters...
I dont get how usigned 8 bit integer can hold characters.
What is a character? It is a symbol you expect to see when the variable being printed contains some specific value. For instance, if the value is 68, you would expect to see 'D' printed. So, you can assign an integer variable a value based on either the symbol ('D") or the value (68).
A variable whose type is character is simply an integer-valued variable sized (one byte) to hold values in a specific range. That same variable type can be used to hold integer values in the same range.
Similarly, any type that can store the required range of values, in integer form, can be used to store characters, too.
Ok I get yah.
Sorry about '?' ...
So if the above bit of code is global, and in setup() I want to change it, I have clered it by setting thr first character to '\0' and then in setup I have the line:
line_buf[LINE_MAX] = (uint8_t ) "Hello World";
It doesnt like that though it says
cast from 'const char*' to 'uint8_t' loses precision
I also tried something like:
String text = "Hello World";
text.toCharArray(line_buf, LINE_MAX);
but couldnt think where I should put a cast in this case?
and then in setup I have the line...It doesnt like that though
You need to understand declaring variables vs. initializing variables. The rules for how to assign a value to an array are different when the value is being assigned as part of a declaration vs. when the value is assigned to an existing array/variable.
uint8_t line_buf[LINE_MAX] = "Hello World";
is valid, because the variable is being declared AND initialized all in one step.
Once the variable exists, though, a statement like:
line_buf[LINE_MAX] = (uint8_t ) "Hello World";
is invalid, because this is trying to assign a string to the LINE_MAX position in the array, not trying to assign multiple positions in the array. In this case, the element position is not even within the array. Valid indices range from 0 to LINE_MAX-1.
You could look at memcpy() to assign a new set of values to an array, or strcpy() if the set of values does not contain embedded NULLs.
I also tried something like...but couldnt think where I should put a cast in this case?
Think about what variable needs to be cast. Which variable is not of the "correct" type. In this case, it is the line_buf variable that needs to be cast to the correct type.
You are having enough issues with char vs. uint8_t. You should not be trying to add to your problems by dragging the inefficient String class into the picture, so I don't intend to show you exactly how to do the cast (aside from being too lazy to fire up the IDE to make sure I get it right).
Cool,
you got me thinking abit, I changed to this:
strcpy((char*)line_buf,"Hello World");
and that does compile, however it doesnt display on the lcd, which is the whole point of this exercise, so I am wondering whether that is technically corect although not actually doing what I think it does?
and that does compile, however it doesnt display on the lcd, which is the whole point of this exercise, so I am wondering whether that is technically corect although not actually doing what I think it does?
Clearly, then, the problem is not in that snippet. Hint, hint.
Really? Im being dumb then, because if everything works if I initialise the array with a contant string. Its only when I want to populate the uint8_t type array "on the fly" that I have problems. In this instance, I can compile succesfully, but it doesnt write to the lcd, therefore I must be filling the uint8_t array wrongly because that's the only thing changing, therefore that is the incorrect line.
#include "U8glib.h"
// setup input buffer
#define LINE_MAX 30
uint8_t line_buf[LINE_MAX];
uint8_t line_pos = 0;
#define ROW_MAX 12
uint8_t screen[ROW_MAX][LINE_MAX];
uint8_t rows, cols;
#define LINE_PIXEL_HEIGHT 7
// append a line to the screen, scroll up
void add_line_to_screen(void) {
uint8_t i, j;
for( j = 0; j < LINE_MAX; j++ )
for( i = 0; i < rows-1; i++ )
screen[i][j] = screen[i+1][j];
for( j = 0; j < LINE_MAX; j++ )
screen[rows-1][j] = line_buf[j];
}
// U8GLIB draw procedure: output the screen
void draw(void) {
uint8_t i, y;
// graphic commands to redraw the complete screen are placed here
y = 0; // reference is the top left -1 position of the string
y--; // correct the -1 position of the drawStr
for( i = 0; i < rows; i++ )
{
u8g.drawStr( 0, y, (char *)(screen[i]));
y += u8g.getFontLineSpacing();
}
}
void exec_line(void) {
// echo line to the serial monitor
Serial.println((const char *)line_buf);
// add the line to the screen
add_line_to_screen();
// U8GLIB picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
// clear current input buffer
void reset_line(void) {
line_pos = 0;
line_buf[line_pos] = '\0';
}
// Arduino master setup
void setup(void) {
// set font for the console window
u8g.setFont(u8g_font_5x7);
u8g.setFontPosTop();
// calculate the number of rows for the display
rows = u8g.getHeight() / u8g.getFontLineSpacing();
if ( rows > ROW_MAX )
rows = ROW_MAX;
// estimate the number of columns for the display
cols = u8g.getWidth() / u8g.getStrWidth("m");
if ( cols > LINE_MAX-1 )
cols = LINE_MAX-1;
//and here is where I want to populate the array again.
strcpy(line_buf,"Hello World?");
exec_line(); // place the input buffer into the screen
reset_line(); // clear input buffer
}
// Arduino main loop
void loop(void) {
}
Paul, your dry humour is almost too much for me to get, however what I think you mean is you want to know what is coming up on my serial terminal. Trouble is I have rewritten the firmware of the mega16U2 to be a midi device so Serial doesn't work, and that line is ust there because this is an example bit of code that I am trying to modify very slightly.
I understand I could add to the uint8_t array using a loop, one character at a time, but I don't think that's required.
I noticed howeer, that the function that is drawing to the lcd is:
void drawScreen(void) {
// set font for the console window
u8g.setFont(u8g_font_5x7);
u8g.setFontPosTop();
uint8_t i, y;
// graphic commands to redraw the complete screen are placed here
y = 0; // reference is the top left -1 position of the string
y--; // correct the -1 position of the drawStr
for( i = 0; i < rows; i++ )
{
u8g.drawStr( 0, y, (char *)(screen[i]));
y += u8g.getFontLineSpacing();
}
}
and it is casting the array screen backto a char*
Screen is populated by line_buf here: