Skip to content

Instantly share code, notes, and snippets.

@tatmos
Created February 24, 2024 20:03
Show Gist options
  • Save tatmos/765add4633588ce90a71dabc0fdb11c5 to your computer and use it in GitHub Desktop.
Save tatmos/765add4633588ce90a71dabc0fdb11c5 to your computer and use it in GitHub Desktop.
32x8NeoPixelに文字表示(M5StickPlus2)
#include <Adafruit_NeoPixel.h>
#include <misakiUTF16.h>
#include <M5StickCPlus2.h>
// Neopixelの設定
#define NUMPIXELS 256 // Neopixel ピクセル数(LED数) 32x8に変更
#define PIN 26 // Neopixel 制御用ピン番号
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
M5.begin(); // M5StickC Plusの初期化
M5.Lcd.fillScreen(BLACK); // 画面を黒色でクリア
Serial.begin(9600);
strip.begin(); // Neopixel ストリップを初期化
strip.show(); // 全ての LED をオフにする(初期化時)
strip.setBrightness(255); // 明るさを設定 (適宜調整してください)
// 他の必要な初期化処理があればここに追加
}
void loop() {
NeoMsg("_■__■_ _■_ _■_ ブレイゼンブレイズ Brazen Blaze ", 8, 4, 4, 25);
//delay(1000); // 次のメッセージ表示までの間隔
}
void NeoCLS(bool flgUpdate = true) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, 0); // 全てのピクセルをオフにする
}
if (flgUpdate) strip.show();
}
void NeoSetRGB(uint8_t no, uint8_t R, uint8_t G, uint8_t B, bool flgUpdate = false) {
if (no < NUMPIXELS) {
strip.setPixelColor(no, strip.Color(R, G, B)); // RGB順に設定
}
if (flgUpdate) strip.show();
}
void NeoScroll(bool flgUpdate = false) {
for (int i = 0; i < 8; i++) { // 8列すべてに対して実行
for (int j = 0; j < 31; j++) { // 各列について31ピクセル分をシフト
uint32_t color = strip.getPixelColor(XYtoNo(i, j+1));
strip.setPixelColor(XYtoNo(i, j), color);
}
strip.setPixelColor(XYtoNo(i, 31), 0); // 列の最後をクリア
}
if (flgUpdate) strip.show();
}
inline uint8_t XYtoNo(uint8_t x, uint8_t y) {
// ヘビ型マトリックスの各行でピクセルの進行方向が異なるための処理
if (y % 2 == 0) {
// 偶数行: ピクセルは左から右へ進む
return y * 8 + x;
} else {
// 奇数行: ピクセルは右から左へ進む
return y * 8 + (7 - x);
}
}
void NeoScrollInChar(uint8_t *fnt, uint8_t R, uint8_t G, uint8_t B, uint16_t tm) {
for (int i = 0; i < 8; i++) {
NeoScroll(false); // 左に1ドットスクロール
for (int j = 0; j < 8; j++) {
if (fnt[j] & (0x80 >> i)) {
NeoSetRGB(XYtoNo(j, 31), R, G, B, false); // 32列目に文字を挿入
} else {
NeoSetRGB(XYtoNo(j, 31), 0, 0, 0, false);
}
}
strip.show();
delay(tm); // 指定された時間だけ待つ
}
}
void NeoMsg(char* msg, uint8_t R, uint8_t G, uint8_t B, uint16_t tm) {
uint8_t fnt[8]; // フォントデータを格納するバッファ
NeoCLS(false); // ディスプレイをクリア
char* str = msg;
while (*str) {
if (!(str = getFontData(fnt, str))) {
Serial.println("Error");
break;
}
NeoScrollInChar(fnt, random(24), random(24), random(24), tm);
}
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment