I can't print the text on the screen with PSTR

I can't print the text "Hello, Arduino!" sent as PSTR on the screen

I also have to associate it with a String (text_program_oled) to be able to print it on the screen with: u8g2.print(text_program_oled);
and it prints only the character "D"

// pantalla oled 1106
#include <U8g2lib.h>
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

String texto_programa;
String texto_programa_oled;

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//                     Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
LiquidCrystal_I2C lcd( 0x27, 2,   1,  0,  4,  5,  6,  7,           3, POSITIVE );   //

void lcd_print_P(const char *pstr) { 
  while (pgm_read_byte(pstr) != 0x00)
    lcd.print((const char)pgm_read_byte(pstr++));
}

void setup() {
    u8g2.begin();
  u8g2.clearBuffer();          // clear the internal memory
}

void loop() {
  secuencia_programa_estandar (PSTR("Hello, Arduino!"));
}

void secuencia_programa_estandar (const char* texto_programa) {
  texto_programa_oled = texto_programa;
  u8g2.setFont(u8g2_font_10x20_me);
  u8g2.setCursor(0, 20);
  u8g2.print(texto_programa); //print oled
  u8g2.setCursor(0, 40);
  u8g2.print(texto_programa_oled); //print oled
  u8g2.sendBuffer();
}

Thanks for the help

The following is incorrect for the I2C version fix that first.

[quote="dedaloblanco, post:1, topic:1357777"]
LiquidCrystal_I2C lcd( 0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE );
[/quote]

Thanks for the reply.
I have done what you indicated and it prints ""DDoAoAA..."
The problem is how to pass the content of (const char* texto_programa) to the variable: String texto_programa_oled

do you miss a Wire.begin() and the lcd.begin()?

Try this

That function is never called. What is it's purpose?

You're over complicating things. U8g2 and LiquidCrystal_I2C both inherit from the Print class. So, they both know how to print text using the F() macro.

It is to save memory, the program consists of a lot of texts.

What does that mean?
Your code never calls this function:

And, even if it did, you're making it over-complicated (as I said previously). Will try one more time:

The new program is adapted from LCD 16x2
I will look at: macro.F()
Thanks for your help.

Works perfectly: u8g2.print((const __FlashStringHelper*)texto_programa);
Thank you very much david_2018

As for the original problem...
In the line I've marked with /* *** (or actually as part of the function call - flash memory is NOT the same as "const" on an AVR), you're "losing" the PSTR-ness of the string you pass to it. When you do the u8g2.print()m the pointer is treated as a pointer to RAM instead of a pointer to Flash, and RAM at that location has random contents.

If you look in WString.h in the AVR boards package, this is the define for the F() macro

 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

The string literal is put into flash memory with PSTR(). That creates a const char*, but the compiler cannot distinguish the pointer from a const char* into ram, so it is cast to const __FlashStringHelper*. __FlashStringHelper* is then used to facilitate overloading functions such as print() to handle references to flash memory.

Your code has basically taken a lengthy route to achieve the same objective, by placing the string literal in flash memory with PSTR(), then passing the const char* to a special function which subsequently casts the pointer to const __FlashStringHelper* so that the proper overload of print() will be used.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.