
Arduino 上手教程
M5UnitLCD 已包含在 M5GFX 中,无需单独安装。

draw_function(LovyanGFX* gfx) 在 Unit LCD 上随机绘制圆形和方块。Unit LCD 采用 I2C 通信,默认地址为 0x3E,通信频率为 400kHz,设备连接到 CoreS3 的 Port A 后对应的引脚为 G2 (SDA)、G1 (SCL)。#include <M5Unified.h>
#include <M5UnitLCD.h>
M5UnitLCD unit_lcd;
M5Canvas lcd_canvas(&unit_lcd);
void draw_function(LovyanGFX* gfx)
{
int x = rand() % gfx->width();
int y = rand() % gfx->height();
int r = (gfx->width() >> 4) + 2;
uint16_t c = rand();
gfx->fillRect(x - r, y - r, r * 2, r * 2, c);
}
void setup()
{
auto cfg = M5.config();
M5.begin(cfg);
const int sda = M5.getPin(m5::pin_name_t::port_a_sda);
const int scl = M5.getPin(m5::pin_name_t::port_a_scl);
if (!unit_lcd.init(sda, scl, 400000)) {
M5.Display.fillScreen(TFT_RED);
M5.Display.setTextSize(2);
M5.Display.setCursor(12, 32);
M5.Display.println("Unit LCD init failed");
while (true) {
delay(1000);
}
}
unit_lcd.setRotation(3);
lcd_canvas.setColorDepth(16);
lcd_canvas.createSprite(unit_lcd.width(), unit_lcd.height());
lcd_canvas.fillScreen(TFT_BLACK);
int textsize = lcd_canvas.height() / 60;
if (textsize == 0) {
textsize = 1;
}
lcd_canvas.setTextSize(textsize);
}
void loop()
{
int x = rand() % lcd_canvas.width();
int y = rand() % lcd_canvas.height();
int r = (lcd_canvas.width() >> 4) + 2;
uint16_t c = rand();
lcd_canvas.fillCircle(x, y, r, c);
draw_function(&lcd_canvas);
lcd_canvas.pushSprite(0, 0);
unit_lcd.waitDisplay();
delay(50);
}
