Go Down

Topic: Porting U8G to U8G2 Issues with printing strings (Read 109 times) previous topic - next topic

Oiitsjamesmate

U8G Code:

Variable is defined here:
Code: [Select]
String [b]percentC[/b], percentB, speedsetting ; //Strings for storing battery percent values

Variable is called upon here:
Code: [Select]
void voltCMeasure() { //Voltage Measuring Void
  unsigned long currentMillisC = millis();
  if (currentMillisC - previousMillisC >= intervalC) {
    previousMillisC = currentMillisC;
    int analogread = analogRead(batteryPin);
    float voltsC = (analogread / 1024.0) * 3.94;
    percentCdecimal = 83.333 * (voltsC - 3);
    percentC = (round(percentCdecimal));
    lengCdecimal = 15 * (voltsC - 3) + 1;
    lengC = ceil(lengCdecimal);
    if (voltsC < 3.00) {
      percentC = "UND";
      lengC = 0;
    }
    if (voltsC > 4.20) {
      percentC = "OVR";
      lengC = 0;
    }
  }
}


Variable is being printed here:
Code: [Select]
void draw_main(void) { //Main Screen Void
  u8g.setFont(u8g_font_unifont); //Drawing Lines and Headings
  u8g.setPrintPos(0, 12);
  u8g.print(speedsetting);
  u8g.setFont(u8g_font_unifont );
  u8g.drawStr(70, 12, "Battery");
  u8g.drawLine(66, 0, 66, 62);
  u8g.drawLine(0, 16, 128, 16);
  u8g.drawLine(66, 63, lengP, 63);
  u8g.setFont(u8g_font_unifont); //Drawing Battery 1 (Controller)
  u8g.drawFrame(104, 22, 22, 10);
  u8g.drawBox(126, 25, 2, 4);
  u8g.setPrintPos(68, 32);
  u8g.print("%");
  u8g.setPrintPos(76, 32);
  u8g.print(percentC);
  u8g.drawBox(106, 24, lengC, 6);
  u8g.setFont(u8g_font_unifont); //Drawing Battery 2 (Board)
  u8g.drawFrame(104, 47, 22, 10);
  u8g.drawBox(126, 50, 2, 4);
  u8g.setPrintPos(68, 55);
  u8g.print("%");
  u8g.setPrintPos(76, 55);
  u8g.print(percentB);
  u8g.drawBox(106, 49, lengB, 6);
  u8g.setFont(u8g_font_fub35n); //Print KMH Variable
  u8g.setPrintPos(4, 58);
  u8g.print(kmh);
}


This all works flawlessly, until I ported over to U8G2

U8G2 Code:

Variable is defined here:
Code: [Select]
String percentC, percentB, speedsetting ; //Strings for storing battery percent values

Variable is called upon here:
Code: [Select]
void voltCMeasure() { //Voltage Measuring Void
  unsigned long currentMillisC = millis();
  if (currentMillisC - previousMillisC >= intervalC) {
    previousMillisC = currentMillisC;
    int analogread = analogRead(batteryPin);
    float voltsC = (analogread / 1024.0) * 3.94;
    percentCdecimal = 83.333 * (voltsC - 3);
    percentC = (round(percentCdecimal));
    lengCdecimal = 15 * (voltsC - 3) + 1;
    lengC = ceil(lengCdecimal);
    if (voltsC < 3.00) {
      percentC = "UND";
      lengC = 0;
    }
    if (voltsC > 4.20) {
      percentC = "OVR";
      lengC = 0;
    }
  }
}


Variable is being printed here:
Code: [Select]
void draw_main(void) { //Main Screen Void
  u8g2.clearBuffer(); // clear the internal memory
  u8g2.setFont(u8g_font_unifont); //Drawing Lines and Headings
  u8g2.setCursor(0, 12);
  u8g2.print(speedsetting);
  u8g2.drawStr(70, 12, "Battery");
  u8g2.drawLine(66, 0, 66, 62);
  u8g2.drawLine(0, 16, 128, 16);
  u8g2.drawLine(66, 63, lengP, 63);
  u8g2.drawFrame(104, 22, 22, 10); //Drawing Battery 1 (Controller)
  u8g2.drawBox(126, 25, 2, 4);
  u8g2.setCursor(68, 32);
  u8g2.print("%");
  u8g2.setCursor(76, 32);
  u8g2.print(percentC);
  u8g2.drawBox(106, 24, lengC, 6);
  u8g2.drawFrame(104, 47, 22, 10);//Drawing Battery 2 (Board)
  u8g2.drawBox(126, 50, 2, 4);
  u8g2.setCursor(68, 55);
  u8g2.print("%");
  u8g2.setCursor(76, 55);
  u8g2.print(percentB);
  u8g2.drawBox(106, 49, lengB, 6);
  u8g2.setFont(u8g2_font_logisoso32_tf); //Print KMH Variable
  u8g2.setCursor(4, 58);
  u8g2.print(kmh);
  u8g2.sendBuffer(); // transfer internal memory to the display
}


This does not produce the variables like it did in U8G. The only code modified is the drawing part (changed the oled being defined, setprintpos to setcursor and the buffer stuff). Does anyone have an idea of what changed and what I have to do to get the variables being printed again. Everything else works fine, I just cannot print those strings.

david_prentice

Life is much easier if you post complete compilable code.   Then readers can replicate your problem and suggest a solution.

If you want to post only snippets,  you should strip everything down to the minimal statements that illustrate your question.    Readers will not build your snippet.  They might spot the answer by inspection.

Programs are easier to maintain if you use local variables.    Return values from functions.   Only use global variables when appropriate.

David.

Oiitsjamesmate

Here are zips of the code

Oiitsjamesmate

Programs are easier to maintain if you use local variables.    Return values from functions.   Only use global variables when appropriate.
Thank you David, I will go and re-do some of my code so it is easier to maintain.

Cheers

Go Up