pdf-icon

Arduino入門

2. デバイス&サンプル

6. アプリケーション

Fire microSD カード

Fire microSD カード利用に関連する API とサンプルプログラム。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャ バージョン >= 3.2.2
  • ボードオプション = M5Fire
  • M5Unified ライブラリ バージョン >= 0.2.8
  • M5GFX ライブラリ バージョン >= 0.2.11
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
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>

#define SD_SPI_CS_PIN    4
#define SD_SPI_SCK_PIN  18
#define SD_SPI_MISO_PIN 19
#define SD_SPI_MOSI_PIN 23

void setup() {
  M5.begin();

  M5.Display.setFont(&fonts::FreeMono12pt7b);
  M5.Display.clear();

  // SD Card Init
  SPI.begin(SD_SPI_SCK_PIN, SD_SPI_MISO_PIN, SD_SPI_MOSI_PIN, SD_SPI_CS_PIN);
  if (!SD.begin(SD_SPI_CS_PIN, SPI, 25000000)) {
    // Print a message if SD card init failed or if the SD card does not exist.
    M5.Display.println("SD card not detected");
    while (1)
      ;
  } else {
    M5.Display.println("SD card detected");
  }
  delay(1000);

  // Write TXT file
  M5.Display.println("SD card write test...");
  auto file = SD.open("/WriteTest.txt", FILE_WRITE, true);
  if (file) {
    file.print("Hello, world! \nSD card write success! \n");
    file.close();
    M5.Display.println("SD card write success");
  } else {
    M5.Display.println("Failed to create TXT file");
  }
  delay(1000);

  M5.Display.println("SD card read test...");
  if (SD.open("/TestPicture01.png", FILE_READ, false)) {
    M5.Display.println("PNG file 01 detected");
  } else {
    M5.Display.println("PNG file 01 not detected");
  }
  if (SD.open("/TestPicture02.png", FILE_READ, false)) {
    M5.Display.println("PNG file 02 detected");
  } else {
    M5.Display.println("PNG file 02 not detected");
  }
  delay(1000);
}

void loop() {
  // Read PNG file and draw picture
  M5.Display.drawPngFile(SD, "/TestPicture01.png");
  delay(500);
  M5.Display.drawPngFile(SD, "/TestPicture02.png");
  delay(500);
}

microSD カードを用意し、FAT32 形式でフォーマットした後、解像度が 320*240PNG 画像を 2 枚ルートディレクトリに配置します。名前は TestPicture01.pngTestPicture02.png にしてください。(サンプル画像 1サンプル画像 2 を直接ダウンロードしても構いません。画像の解像度が 320*240 でない場合、プログラムはプリセットに従って表示方法を決定し、表示が崩れる可能性があります。)

この SD カードを Fire に挿入し、SD カードの接点が Fire の画面と同じ向きになるように注意してください。上記のコードを Arduino IDE にコピーしてコンパイルし、Fire に書き込みます。

このプログラムは SD カードに WriteTest.txt というテキストファイルを作成して一部の文字列を書き込み、その後 SD カード内の 2 枚の PNG 画像をループ再生します。

API

Fire microSD カード機能は Arduino 標準の SD ライブラリと、M5GFX ライブラリの drawPngFile 関数を使用しています。関連する API の詳細については以下のドキュメントを参照してください:

On This Page