M5Paper: 日本語の表示

SD カード に日本語TTFファイル (ipaeg.ttf)があるとします。

フォントの取得 (ipaexg.ttf)

```bash
wget https://moji.or.jp/wp-content/ipafont/IPAexfont/ipaexg00401.zip
unzip ipaexg00401.zip
```

Ubntu で SD カードを fat32 で初期化するには、

```bash
gparted
```

## プログラム ##

```text:ttf_jpn.ino
// ---------------------------------------------------------------
//	ttf_jpn.ino
//
//				Jun/9/2022
// ---------------------------------------------------------------
#include <M5EPD.h>
#include <FS.h>

M5EPD_Canvas canvas(&M5.EPD);

// ---------------------------------------------------------------
void setup() {
	M5.begin();
	M5.TP.SetRotation(90);
	M5.EPD.SetRotation(90);
	M5.EPD.Clear(true);
	M5.RTC.begin();

	canvas.createCanvas(540, 960);
	canvas.setTextDatum(TC_DATUM);
	canvas.setTextSize(3);

	canvas.drawString("... Initializing ...", 270, 640);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

	canvas.drawString("Loading font ...", 270, 230);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

// SD からフォントを読み込む
	canvas.loadFont("/ipaeg.ttf", SD);
	canvas.createRender(96, 256);
	canvas.createRender(32, 256);

	canvas.drawString("OK!", 270, 254);
	canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

	rtc_time_t RTCtime;
	RTCtime.hour = 0;
	RTCtime.min  = 0;
	RTCtime.sec  = 0;
	M5.RTC.setTime(&RTCtime);

	rtc_date_t RTCdate;
	RTCdate.year = 2022;
	RTCdate.mon = 6;
	RTCdate.day = 9;
	RTCdate.week = 4;
	M5.RTC.setDate(&RTCdate);

// delay(20000);
	M5.EPD.Clear(true);
	canvas.fillCanvas(0);
	display_contents();

}

// ---------------------------------------------------------------
void loop() {
	drawClock();
	delay(200);
}

// ---------------------------------------------------------------
// 毎分時計を再描画する. RTCが設定されている必要がある
void drawClock() {
	char buf[32];
	rtc_time_t RTCtime;
	rtc_date_t RTCdate;
	M5.RTC.getTime(&RTCtime);
	M5.RTC.getDate(&RTCdate);

	const char* weeks[] = {"日", "月", "火", "水", "木", "金", "土"};

	canvas.setTextDatum(TL_DATUM);
//	canvas.fillCanvas(0);
	canvas.drawLine(0, 179, 540, 179, 15);
	canvas.drawLine(0, 180, 540, 180, 15);

	snprintf(buf, 32, "%d/%d/%d (%s)", RTCdate.year, RTCdate.mon, RTCdate.day, weeks[RTCdate.week]);
	canvas.setTextSize(32);
	canvas.drawString(buf, 12, 16);

	snprintf(buf, 32, "% 2d:%02d:%02d", RTCtime.hour, RTCtime.min, RTCtime.sec);
	canvas.setTextSize(96);
	canvas.drawString(buf, 12, 64);


// canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
	
//	 canvas.pushCanvas(0,0,UPDATE_MODE_GLR16);
	 canvas.pushCanvas(0,0,UPDATE_MODE_A2);
}

// ---------------------------------------------------------------
void display_contents()
{
	int xpos = 200;
	canvas.setTextSize(32);
	canvas.drawString("ブレーメンの音楽隊", xpos, 300);	
	canvas.drawString("白雪姫", xpos, 350);	
	canvas.drawString("赤ずきん", xpos, 400);	
	canvas.drawString("ヘンデルとグレーテル", xpos, 450);
	canvas.drawString("おおかみと七匹のこやぎ", xpos, 500);
	canvas.drawString("灰かぶり", xpos, 550);
	canvas.drawString("親指こぞう", xpos, 600);
	canvas.drawString("いばらひめ", xpos, 650);
	canvas.drawString("ラプンツェル", xpos, 700);
	canvas.drawString("かえるの王さま", xpos, 750);
	canvas.drawString("ホレおばさん", xpos, 800);
	canvas.drawString("星の銀貨", xpos, 850);
	
	canvas.pushCanvas(0,0,UPDATE_MODE_A2);
}

// ---------------------------------------------------------------
```

Arduino IDE
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/4d8d657e-6990-4772-2eb5-12f184dd9081.png)

実行結果
![IMG_20220609_105649.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/a43615d8-534a-c1e8-31d8-f1d923ba5858.jpeg)