Error with char in U8glib

I trying to modify the "Hello World" example in the U8glib LCD library to print an analog variable. I have the hello world example working by substituting the right digital ports for the LCD. Code is:

]

/*

HelloWorld.pde

"Hello World!" example code.

Before compiling: Please remove comment from the constructor of the
connected graphics display (see below).

Universal 8bit Graphics Library, Google Code Archive - Long-term storage for Google Code Project Hosting.

Copyright (c) 2012, olikraus@gmail.com
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list
    of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "U8glib.h"

// setup u8g object, please remove comment from one of the following constructor calls

//U8GLIB_NHD27OLED_BW u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_NHD27OLED_2X_BW u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_NHD27OLED_GR u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_NHD27OLED_2X_GR u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGS102 u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGM132 u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGM128 u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_ST7920_128X64 u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, U8G_PIN_NONE, U8G_PIN_NONE, 17, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
//U8GLIB_ST7920_128X64 u8g(18, 16, 17, U8G_PIN_NONE); // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
//U8GLIB_ST7920_192X32 u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, U8G_PIN_NONE, U8G_PIN_NONE, 17, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16
//U8GLIB_ST7920_192X32 u8g(18, 16, 17, U8G_PIN_NONE); // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17
//U8GLIB_LM6059 u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_LM6063 u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGXL160_BW u8g(10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGXL160_GR u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGXL160_2X_BW u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_DOGXL160_2X_GR u8g(13, 11, 10, 9); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8
//U8GLIB_PCF8812 u8g(13, 11, 10, 9, 8); // SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, Reset = 8
//U8GLIB_KS0108_128 u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 14, 15, 17, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs1=14, cs2=15,di=17,rw=16
//U8GLIB_LC7981_160X80 u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 14, 15, 17, 16); // 8Bit Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, cs=14 ,di=15,rw=17, reset = 16
// RS=19, WR=18, CS=17, RST=16
U8GLIB_ST7920_128X64 u8g(4, 2, 3, U8G_PIN_NONE);
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr( 0, 22, "Hello World");
}

void setup(void) {

// flip screen, if required
// u8g.setRot180();

// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(3); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
}

void loop(void) {
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );

// rebuild the picture after some delay
delay(500);
}

My code:

draw();
} while( u8g.nextPage() );

my error:

HelloWorld.cpp: In function 'void draw()':
HelloWorld:12: error: invalid conversion from 'char' to 'const char*'
HelloWorld:12: error: initializing argument 3 of 'u8g_uint_t U8GLIB::drawStr(u8g_uint_t, u8g_uint_t, const char*)'

What am I doing wrong?

Jim

Hi

The drawStr procedure only supports strings. To print a value, use the print procedure, which inherits from the Arduino library. The "Hello World" example with print is called "PrintTest" (Google Code Archive - Long-term storage for Google Code Project Hosting.) and should be part of the examples. See also here: http://code.google.com/p/u8glib/wiki/userreference#print.

Oliver

I don't understand! That is exactly the sketch I modified the LCD address in, and it worked. I changed two other lines. I added " val = analogRead(analogPin); " and changed one line from " u8g.drawStr( 0, 22, "Hello World"); " to u8g.drawStr( 0, 22, char(val));
Your example uses " u8g.drawStr( 0, 22, "Hello World"); "

error with u8g.drawStr( 0, 22, char(val));
pressure.cpp: In function 'void draw()':
pressure:12: error: invalid conversion from 'char' to 'const char*'
pressure:12: error: initializing argument 3 of 'u8g_uint_t U8GLIB::drawStr(u8g_uint_t, u8g_uint_t, const char*)'

or with u8g.drawStr( 0, 22, val);

pressure.cpp: In function 'void draw()':
pressure:12: error: invalid conversion from 'int' to 'const char*'
pressure:12: error: initializing argument 3 of 'u8g_uint_t U8GLIB::drawStr(u8g_uint_t, u8g_uint_t, const char*)'

Jim

pressure:12: error: initializing argument 3 of 'u8g_uint_t U8GLIB::drawStr(u8g_uint_t, u8g_uint_t, const char*)'

So, read the argument descriptions here.

u8g.drawStr( 0, 22, char(val));

and here.

You are trying to pass a char where a char array is required. The compiler is telling you no, you can't do that.

PaulS posted

PaulS:

pressure:12: error: initializing argument 3 of 'u8g_uint_t U8GLIB::drawStr(u8g_uint_t, u8g_uint_t, const char*)'

So, read the argument descriptions here.

u8g.drawStr( 0, 22, char(val));

and here.

You are trying to pass a char where a char array is required. The compiler is telling you no, you can't do that.

Yes I would, IF I knew how! I have searched the web but I guess I have not found the right question to get an working answer. I have never worked in C/C++ before, how do I make an int into a char array? I am a newbie/dummy and need help!

Jim

Try looking at the itoa function. I think it's what you're looking for.

Please use u8g.print() instead of u8g.drawStr().
u8g.print() has a buildin itoa functionality.

Oliver

If I change u8g.drawStr(0,22,val) to u8g,print(0,22,val) the error is:

pressure.cpp: In function 'void draw()':
pressure:12: error: no matching function for call to 'U8GLIB_ST7920_128X64::print(int, int, int&)'
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:44: note: candidates are: void Print::print(const String&)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:45: note: void Print::print(const char*)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:46: note: void Print::print(char, int)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:47: note: void Print::print(unsigned char, int)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:48: note: void Print::print(int, int)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:49: note: void Print::print(unsigned int, int)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:50: note: void Print::print(long int, int)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:51: note: void Print::print(long unsigned int, int)
C:\arduino-0022\hardware\arduino\cores\arduino/Print.h:52: note: void Print::print(double, int)

or u8g,Print(0,22,val) the error is:

pressure.cpp: In function 'void draw()':
pressure:12: error: invalid use of 'class Print'

So I created variable char Sval[]; and put in line itoa(val, Sval, 10); and changed line to u8g.drawStr( 0, 22, Sval); it compiles and works.

Jim

The syntax of "print" is different to "drawStr" (see the u8glib reference here: http://code.google.com/p/u8glib/wiki/userreference#print )

Convert

u8g.drawStr( 0, 22, val);

to

u8g.setPrintPos(0, 22); 
u8g.print(val);

I am sorry for the confusion, but print() has been written by the Arduino group. U8glib just inherits from that class.

Also this might be a good starting point: Google Code Archive - Long-term storage for Google Code Project Hosting.

Oliver

The syntax of "print" is different to "drawStr"

many thanks for your detailed answer :slight_smile:

your lib is very convenient!