m5stack / M5GFX

Graphics library for M5Stack series
MIT License
166 stars 47 forks source link

How to make this library work with M5Unified? #91

Closed linuxhuskarl closed 6 months ago

linuxhuskarl commented 6 months ago

Hi Lovyan!

I tried to make use of this library because I am particularly interested in using sprites, but I can't get it to work. I have searched for a similar issue both in this and in m5stack/M5Unified repo but without luck.

I followed examples listed on the official documentation site, but those don't use M5Unified.

My setup

I am using M5Stick C Plus 2 with Platform IO and Arduino framework. I have added two libraries for working with the device:

[env:m5stick-c]
platform = espressif32
board = m5stick-c
framework = arduino
lib_deps = 
    m5stack/M5GFX@^0.1.15
    m5stack/M5StickCPlus2@^1.0.2

With this setup, when I take some example code like BarGraph.ino it works out of the box, which is great!

The issue

It is when I try to extend the aforementioned example with M5Unified.h / M5StickCPlus2.h is when it goes wrong. The code compiles fine, and the display is initiated and backlit to ~50%, but the screen is completely black, with no output whatsoever.

The only changes I made are:

// on the top
#include <M5Unified.h>
// at beginning of setup()
  M5.begin();
// at beginning of loop()
  M5.update();

What is the proper way to integrate those two libraries? Am I missing something obvious, or can you suggest some troubleshooting steps?

linuxhuskarl commented 6 months ago

Of course, I suspect that the two libraries collide when it comes to initializing the display. I would like M5GFX to take priority - how to achieve that?

I've tried something like this to disable the display init within M5, but it didn't solve it:

  auto cfg = M5.config();
  cfg.external_display.module_display = false;
  M5.begin(cfg);

I've also tried different init function with M5GFX, with seemingly the same result:

  display.init_without_reset();

That is what I have tried so far.

What I'd love to see is a demo/example code of M5GFX sprites working with M5Unified, e.g. ball reacting to IMU changes or just any simple UI utilising M5.BtnA/B and M5GFX sprites.

linuxhuskarl commented 6 months ago

I've just figured it out, and now I feel kinda stupid for not figuring it out earlier! It took me a bit longer than I anticipated, but I found the correct way to initialize the display.

Apart from adding #include <M5Unified.h>, I had to modify the setup() function accordingly:

diff --git a/src/main.cpp b/src/main.cpp
index 9bae492..cead727 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,4 +1,4 @@
-
+#include <M5Unified.h>
 #include <M5GFX.h>
 M5GFX display;

@@ -24,7 +24,8 @@ static uint32_t colors[BAR_COUNT];

 void setup(void)
 {
-  display.init();
+  M5.begin();
+  display = M5.Lcd;
   display.startWrite();
   display.fillScreen(TFT_BLACK);

@@ -67,6 +68,7 @@ void setup(void)

 void loop(void)
 {
+  M5.update();
   int h = display.height();

   static int i;

In a nutshell, it was enough to replace display.init(); with M5.begin(); display = M5.Lcd;.