简体中文
English
简体中文
日本語

Arduino 上手教程

2. 设备开发 & 案例程序

5. 扩展模块

6. 应用案例

CoreMatrix Display 点阵显示

说明
CoreMatrix 使用 M5GFX 库作为 LED 点阵驱动,参考下方 API & 案例即可实现简单显示。获取更多 API 内容可以参考 M5GFX 文档。

案例程序

编译要求

  • M5Stack 板管理版本 >= 3.3.10-a
  • 开发板选项 = M5CoreMatrix
  • M5Unified 库版本 >= 0.2.29
  • M5GFX 库版本 >= 0.2.36

16 x 16 白色 LED 点阵电源由 M5IOE1 IO4 / PYB_LEDS_EN 控制,M5GFX 初始化时会自动打开点阵电源。

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <M5Unified.h>

static constexpr int kSquareSize = 2;
static constexpr int kMinX = 1;
static constexpr int kMaxX = 13;

static int squareX = kMinX;
static int direction = 1;
static bool reachedRight = false;

static void drawFrame()
{
  M5.Display.startWrite();
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.drawRect(0, 0, 16, 16, TFT_WHITE);
  M5.Display.fillRect(squareX, 7, kSquareSize, kSquareSize, TFT_WHITE);
  M5.Display.endWrite();
  M5.Display.display();
}

static void drawOk()
{
  M5.Display.startWrite();
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.drawRect(0, 0, 16, 16, TFT_WHITE);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.setTextDatum(textdatum_t::middle_center);
  M5.Display.drawString("OK", 8, 8);
  M5.Display.endWrite();
  M5.Display.display();
}

void setup()
{
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setColorDepth(8);
  M5.Display.setRotation(1);
  M5.Display.setBrightness(128);

  Serial.printf("CoreMatrix display: %d x %d\n",
                static_cast<int>(M5.Display.width()),
                static_cast<int>(M5.Display.height()));
  drawFrame();
}

void loop()
{
  squareX += direction;

  if (squareX >= kMaxX) {
    squareX = kMaxX;
    direction = -1;
    reachedRight = true;
  } else if (squareX <= kMinX) {
    squareX = kMinX;

    if (reachedRight) {
      drawFrame();
      delay(200);

      drawOk();
      delay(1000);

      reachedRight = false;
      direction = 1;
    }
  }

  drawFrame();
  delay(120);
}

上传后,点阵外圈显示 16 x 16 边框,一个 2 x 2 像素方块在边框内部从左向右再返回。完成一个来回后,点阵显示 OK,随后重新开始方块移动。

Page Tools
PDF
On This Page