Arduino + DOGM132 + u8glib text poitioning problem

Hi!

I don't know weather this is the right forum to post my question/problem, but I'll try my luck here. Basically, there is an issue with text positioning and I can't find out weather it's U8Glib related or it's just me. I'm writing a clock programm for Arduino, that displays a clock. It starts with an edit menu, where user can switch between 3 items: hours, minutes and OK button. Every selected item gets treated with a foreground color box and text is set to background color. Displaying a single text block with time and no boxes is not a problem, but once I separate every item (hours, colon sign in between, minutes, and a nice XBM icon) something goes worng when user select minutes for editing - there seems to be double rendering, where "unselected" minute view shows up slightli positioned to the left, which by the way it shouldn't. This is how it looks like: http://gusc.lv/stuff/fun/arduino_time.jpg the first number 5 is over the colon sign.

Here is my rendering code:

void viewMenuTime(){
  // x, y coords, widht and height of a character
  unsigned int x = 45, y = 5, w = 10, h = 17;
  // small itoa() implementation
  char hour[2] = {(timeH / 10) + 48, (timeH % 10) + 48};
  char minute[2] = {(timeM / 10) + 48, (timeM % 10) + 48};
  // Set Font
  u8g.setFont(u8g_font_10x20);
  // Set color to black
  u8g.setDefaultForegroundColor();
  // Draw icon
  u8g.drawXBM(25, 9, clock_width, clock_height, clock_bits);
  // Highlight hour spinner if selected
  if (menuTimeItem == TIME_ITEM_HOUR){
    u8g.drawBox(x, y, 20, 20);
    u8g.setDefaultBackgroundColor();
  }
  // Draw hour numbers
  u8g.drawStr(x, y + h, hour);
  x += (w * 2);
  // Reset color to black
  u8g.setDefaultForegroundColor();
  // Draw colon
  u8g.drawStr(x, y + h, ":");
  x += w;
  // Highlight minute spinner if selected
  if (menuTimeItem == TIME_ITEM_MINUTE){
    u8g.drawBox(x, y, 20, 20);
    u8g.setDefaultBackgroundColor();
  }
  // Draw minute numbers
  u8g.drawStr(x, y + h, minute);
}

Some global definitions that might clear thins up:

#define TIME_ITEM_HOUR 0
#define TIME_ITEM_MINUTE 1
#define TIME_ITEM_OK 2
...
char timeH = 0;
char timeM = 0;
...
unsigned int menuTimeItem = TIME_ITEM_HOUR;

Render loop:

void loop(){
  u8g.firstPage();
  do {
    viewMenuTime();
  } while( u8g.nextPage() );
}

Could this be some rendering optimization issue?

Hardware:
Arduino Uno R3
DOGM132 display connected on the breadboard using the schematics from GitHub - olikraus/dogm128: Automatically exported from code.google.com/p/dogm128
4 push buttons (left, right, up, down) connected like http://arduino.cc/en/Tutorial/Button

Software:
Arduino IDE 1.0.1
U8Glib for drawing through SPI on DOGM display.

  u8g.drawStr(x, y + h, hour);

This function expects a string as the third argument. What you are providing is NOT a string. It is a char array. A string is a char array THAT IS NULL TERMINATED.

Your char array is NOT NULL terminated. Do NOT pass char arrays to functions that expect strings, and expect reasonable actions.

Not quite right. drawStr asks for char pointer or array (which is a pointer anyway and it's also called a string) not the String object, if you meant that. The problem was that I forgot to add 0 at the end of char array (null terminate it).

Anyway, thank you for pointing it out. Sometimes an extra eye can help to catch a dumb mistake :slight_smile:

Here is the fixed code snippet if it helps anyone else:

// small itoa() implementation
  char hour[3] = {(timeH / 10) + 48, (timeH % 10) + 48, 0};
  char minute[3] = {(timeM / 10) + 48, (timeM % 10) + 48, 0};