Is it possible to use these two libraries together? I want to display text using the u8x8 lib and do some graphics using the u8g2 library. I want to speed things up by using the u8x8 library for text, but would still like some of the graphics the u8g2 offers.
Attached is the code, you will see the text printed by the u8x8 library flickers pretty badly.
Any help would be much appreicated! Thank you
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, 3, 4, U8X8_PIN_NONE);
U8X8_SH1106_128X64_NONAME_4W_HW_SPI u8x8(3, 4, U8X8_PIN_NONE);
void setup(void) {
u8x8.begin();
u8g2.begin();
}
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawFrame(0,10,20,10); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.drawString(0,0,"Hello World!");
}
Attached is the code, you will see the text printed by the u8x8 library flickers pretty badly.
Your are in the Arduino main loop. The u8x8 will be cleared by your clear command.
Try this:
Code: [Select]
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, 3, 4, U8X8_PIN_NONE);
U8X8_SH1106_128X64_NONAME_4W_HW_SPI u8x8(3, 4, U8X8_PIN_NONE);
void setup(void) {
u8x8.begin();
u8g2.begin();
}
void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawFrame(0,10,20,10); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.drawString(0,0,"Hello World!");
for(;;) {
}
}
That took care of the flickering! Thank you. What exactly is that for( ;; ) command doing? IT seems to be blocking any other code from being executed which would be problematic for my purpose, as I need to do other things with the arduino.
What exactly is that for( ;; ) command doing? IT seems to be blocking any other code from being executed which would be problematic for my purpose, as I need to do other things with the arduino.
Yes, exactly, "for(;;)" is just a endless loop. It is there for demonstration only.
Oliver
is there a way to do this without freezing the main loop?
Ok, maybe I was not clear enough: The "for( ; ; ) ;" command has no meaning and can be removed.
Oliver
but when I remove that, the flickering comes back...
Coming back to the original question: Can we use u8g2 and u8x8 together?
Answer: Yes, this is possible,
Then your problem is: "too much of flicker"
Now the question is: Why does it flicker, what is the root cause for this and how to avoid this.
Answer: There are two reasons: First the example is bad for this because of the Ardunio-loop() thing which probably contributes by 80% for the flickering. The second reason (maybe 20%) is contributed by the use of u8g2 and u8x8 together.
The loop() thing: Usually you will only update your screen if there is something to update.
It is a good habit to introduce a variable ("isChanged") which will be set to true if something is changed. Then, you can update your screen whenever the isChanged variable is set to true:
if ( isChanged ) { update screen; isChanged=false; }
This will avoid the loop()-caused flicker. The flicker caused by using u8g2 and u8x8 co-usage can not be avoided in general (altough it can be minimized by clever programming techniques). So i suggeest to ask yourself:
Why do you want to use u8g2 and u8x8 together?
The performance probably is not a big thing. Did you measure the speed increase?
Oliver
I will see how I can incorporate your advice into my code. Your help is really appreciated!
the speed increase does seem to be an issue, I am generation a value that quickly cycles through a table at a set rate of 20ms to 100ms. When I have the u8g2 library displaying text, I cant get the value to move at the required speed. It works much better when I use the u8x8 library for text.
Just trying to allow other processes move at the required speed!
@timyoyoyo can you please guide me if I have to use the both the libraries at the same time. Your above code does not work for me. No text is shown on the OLED. I am using HELTEK 32 KIT with built in OLED.