Last active
April 6, 2024 11:08
-
-
Save mongonta0716/e5560f4495c5394797f690a59429009c to your computer and use it in GitHub Desktop.
M5StampS3でHUB75のLEDマトリクスを使うサンプル。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example sketch which shows how to display some patterns | |
// on a 64x32 LED matrix | |
// Copyright(c) 2024 Takao Akaki | |
#include <M5Unified.h> | |
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h> | |
// Change these to whatever suits | |
#define R1_PIN 1 | |
#define G1_PIN 2 | |
#define B1_PIN 3 | |
#define R2_PIN 4 | |
#define G2_PIN 5 | |
#define B2_PIN 6 | |
#define A_PIN 8 | |
#define B_PIN 9 | |
#define C_PIN 10 | |
#define D_PIN 11 | |
#define E_PIN 7 // required for 1/32 scan panels, like 64x64px. Any available pin would do, i.e. IO32 | |
#define LAT_PIN 14 | |
#define OE_PIN 46 | |
#define CLK_PIN 12 | |
#define LED_WIDTH 64 | |
#define LED_HEIGHT 32 | |
HUB75_I2S_CFG::i2s_pins _pins={R1_PIN, G1_PIN, B1_PIN, R2_PIN, G2_PIN, B2_PIN, A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, LAT_PIN, OE_PIN, CLK_PIN}; | |
HUB75_I2S_CFG mxconfig( | |
LED_WIDTH, // Module width | |
LED_HEIGHT, // Module height | |
1, // chain length | |
_pins // pin mapping | |
); | |
MatrixPanel_I2S_DMA *dma_display = new MatrixPanel_I2S_DMA(mxconfig); | |
//MatrixPanel_I2S_DMA dma_display; | |
//MatrixPanel_I2S_DMA *dma_display = nullptr; | |
M5Canvas canvas1 = new M5Canvas(); // LEDに転写するSprite | |
M5Canvas canvas2 = new M5Canvas(canvas1); // 回転用 | |
M5Canvas canvas3 = new M5Canvas(canvas1); // 文字スクロール用 | |
static uint16_t numOfLeds = 64 * 32; | |
int textpos[2] = { 0, 0 }; // 上下2行分のテキスト表示位置 | |
int scrollstep[2] = { 2, 2 }; // 上下2桁分のスクロールステップ | |
static constexpr uint32_t demo_interval = 5000; // デモを切り替える秒数 | |
uint32_t last_demo_change_msec = 0; // | |
void drawPanel() { | |
for (int i = 0; i<LED_WIDTH; i++) { | |
for (int j = 0; j<LED_HEIGHT; j++) { | |
auto color = canvas1.readPixelValue(i, j); | |
dma_display->drawPixel(i, j, color); | |
} | |
} | |
} | |
void scrollText(String str, uint8_t line_no) { | |
size_t textlen = sizeof(str) / sizeof(str[0]); | |
int32_t cursor_x = canvas3.getCursorX() - scrollstep[line_no]; | |
if (cursor_x <= 0) | |
{ | |
textpos[line_no] = 0; | |
cursor_x = canvas1.width(); | |
} | |
canvas3.setCursor(cursor_x, 0); | |
canvas3.scroll(-scrollstep[line_no], 0); | |
while (textpos[line_no] < textlen && cursor_x <= canvas1.width()) | |
{ | |
canvas3.print(str.substring(textpos[line_no])); | |
cursor_x = canvas3.getCursorX(); | |
} | |
canvas3.pushSprite(&canvas1, 0, (canvas1.height() - canvas3.height()) >> line_no); | |
drawPanel(); | |
vTaskDelay(50/portTICK_PERIOD_MS); | |
} | |
float angle = 0.0f; | |
void rotatetSprite(bool cc) { | |
canvas1.fillSprite(TFT_BLACK); | |
canvas2.pushRotated(&canvas1, angle); | |
if (cc) { | |
angle += 1.0f; | |
} else { | |
angle -= 1.0f; | |
} | |
drawPanel(); | |
} | |
void rotateText(String text, uint16_t pivot_x, uint16_t pivot_y, bool isCW, uint16_t color) { | |
canvas2.clear(); | |
canvas2.setCursor(0, 0); | |
canvas2.setTextColor(color); | |
canvas2.println(text); | |
canvas2.setPivot(pivot_x, pivot_y); | |
rotatetSprite(isCW); | |
} | |
void setup() { | |
auto cfg = M5.config(); | |
M5.begin(cfg); | |
canvas1.setColorDepth(16); | |
canvas1.setSwapBytes(false); | |
canvas1.createSprite(LED_WIDTH, LED_HEIGHT); | |
canvas2.setColorDepth(16); | |
canvas2.setSwapBytes(true); | |
canvas2.createSprite(LED_WIDTH, LED_HEIGHT); | |
canvas2.setTextSize(1); | |
canvas2.setFont(&m5gfx::fonts::efontCN_16); | |
canvas2.setCursor(0, 0); | |
canvas2.setTextColor(m5gfx::swap565(0, 255, 0)); | |
canvas2.println("Hello"); | |
canvas2.setTextColor(m5gfx::swap565(255, 0, 0)); | |
canvas2.println("今日は"); | |
// 文字スクロール用のcanvas | |
canvas3.setColorDepth(16); | |
canvas3.setSwapBytes(false); | |
canvas3.setTextSize(1); | |
canvas2.setFont(&m5gfx::fonts::efontCN_16); | |
canvas3.createSprite(LED_WIDTH, 16); | |
// Display Setup | |
dma_display->begin(); | |
dma_display->setBrightness8(90); //0-255 | |
dma_display->fillScreenRGB888(255, 0, 0); | |
delay(5000); | |
dma_display->clearScreen(); | |
last_demo_change_msec = millis(); | |
while((millis() - last_demo_change_msec) < demo_interval) rotateText("Start", 28, 8, false, m5gfx::swap565(255, 0, 0)); | |
last_demo_change_msec = millis(); | |
} | |
void loop() { | |
while((millis() - last_demo_change_msec) < demo_interval) rotateText("M5Stack", 28, 8, false, m5gfx::swap565(255, 0, 0)); | |
last_demo_change_msec = millis(); | |
while((millis() - last_demo_change_msec) < demo_interval) rotateText("スタック チャン", 32, 16, true, m5gfx::swap565(0, 255, 0)); | |
last_demo_change_msec = millis(); | |
while((millis() - last_demo_change_msec) < demo_interval) rotateText(" ・_・ ", 32, 8, false, m5gfx::swap565(0, 0, 255)); | |
last_demo_change_msec = millis(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:m5stack-stamps3] | |
platform = espressif32 @ 5.3.0 | |
board = esp32-s3-devkitc-1 | |
framework = arduino | |
upload_speed = 1500000 | |
monitor_speed = 115200 | |
board_build.f_flash = 80000000L | |
board_build.filesystem = spiffs | |
board_build.partitions = default_8MB.csv | |
build_flags = | |
-DCORE_DEBUG_LEVEL=4 | |
-DNO_GFX=1 | |
lib_extra_dirs=lib | |
lib_deps = | |
m5stack/M5Unified@0.1.14 | |
ESP32 HUB75 LED MATRIX PANEL DMA Display | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment