pdf-icon

Arduino入門

2. デバイス&サンプル

Paper microSD カード

PaperのmicroSDカードに関連するAPIとサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャ バージョン >= 2.1.4
  • 開発ボードの選択 = M5Paper
  • M5Unified ライブラリ バージョン >= 0.2.5
  • M5GFX ライブラリ バージョン >= 0.2.7
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
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>
#include <M5GFX.h>

#define SD_SPI_SCK_PIN  14
#define SD_SPI_MISO_PIN 13
#define SD_SPI_MOSI_PIN 12
#define SD_SPI_CS_PIN   4

void setup() {
  M5.begin();
  M5.Display.setRotation(1);
  M5.Display.setFont(&fonts::FreeMonoBold18pt7b);
  M5.Display.setEpdMode(epd_quality);  // epd_quality, epd_text, epd_fast, epd_fastest

  // SD Card Initialization
  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 initialization failed or if the SD card does not exist.
    M5.Display.print("\n SD card not detected\n");
    while (1)
      ;
  } else {
    M5.Display.print("\n SD card detected\n");
  }
  delay(1000);

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

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

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

microSDカードを準備し、FAT32形式でフォーマットして、解像度が960*540PNG画像を2枚、TestPicture01.pngTestPicture02.pngという名前でルートディレクトリに保存してください。(サンプル画像1サンプル画像2 を直接ダウンロードすることもできます。画像の解像度が 960*540 でない場合は、プログラムが事前設定に基づいて表示方法を決定するため、表示異常が発生する可能性があります。)

このSDカードをPaperに挿入してください。SDカードの接点がPaperの画面と同じ方向を向いていることを確認してください。上記のコードをArduino IDEにコピーし、コンパイルしてPaperにアップロードします。

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

API

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

On This Page