🛠 Graphismes avancés avec M5GFX : formes, couleurs et luminosité

M5Stack Core (ESP32) — Basic / Gray family (K001 / K002) · intermediaire · Schéma : IMG-m5stack-core-esp32-intermediaire-1-m5stack-core-esp32-graphismes-m5gfx.svg — écran de l'M5Stack Core divisé en zones annotées montrant rectangles, cercles, lignes et texte dessinés par M5GFX, curseur de luminosité en bas

Objectives

Hardware

Complete Code

#include <M5Unified.h>

uint8_t page = 0;
uint8_t luminosite = 128;

void pageFormes {
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.fillRect(10, 10, 90, 50, TFT_RED);
  M5.Display.fillRoundRect(120, 10, 90, 50, 12, TFT_GREEN);
  M5.Display.fillCircle(60, 120, 35, TFT_BLUE);
  M5.Display.drawCircle(170, 120, 35, TFT_YELLOW);
  M5.Display.drawLine(220, 10, 310, 60, TFT_CYAN);
  for (int x = 10; x < 310; x += 20)
    M5.Display.drawLine(x, 180, x + 10, 220, TFT_ORANGE);
}

void pageJauge {
  M5.Display.fillScreen(TFT_NAVY);
  for (uint8_t i = 0; i < 16; i++)
    M5.Display.fillRect(10 + i * 19, 40, 17, 60,
      M5.Display.color565(i * 16, 255 - i * 16, 128));
  M5.Display.fillRect(10, 140, 300 * luminosite / 255, 24, TFT_YELLOW);
  M5.Display.setCursor(10, 175);
  M5.Display.printf("Luminosite : %u", luminosite);
}

void setup {
  auto cfg = M5.config;
  M5.begin(cfg);
  M5.Display.setTextSize(2);
  pageFormes;
}

void loop {
  M5.update;
  if (M5.BtnA.wasPressed) { page = (page + 1) % 2; }
  if (M5.BtnB.wasPressed) { luminosite = (luminosite + 32) % 256; }
  if (M5.BtnC.wasPressed) { luminosite = (luminosite + 224) % 256; }
  if (page == 0) pageFormes; else pageJauge;
  M5.Display.setBrightness(luminosite);
  vTaskDelay(30 / portTICK_PERIOD_MS);
}

Expected Verification

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials