0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5Paper: 日本語TTFファイルを SD から SPIFFS へコピー

0
Last updated at Posted at 2022-06-13

こちらのページを参考にしました。
M5Paperに日本語フォントを入れる話

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

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

wget https://moji.or.jp/wp-content/ipafont/IPAexfont/ipaexg00401.zip
unzip ipaexg00401.zip
cd ipaexg00401
mv ipaeg.ttf font.ttf

Arduino IDE の設定

ツールで

ボード "M5Stack-Paper"
Partition Scheme "Large SPIFFS(7MB)"

にして下さい。

プログラム

sd_copy.ino
// ---------------------------------------------------------------
//	sd_copy.ino
//
//					Jun/13/2022
// ---------------------------------------------------------------
#include	<M5EPD.h>
#include	<FS.h>

M5EPD_Canvas canvas(&M5.EPD);

// ---------------------------------------------------------------
// SDカードがセットされていれば、SPIFFSを初期化後ipaeg.ttfをコピーする
bool tryCopySDFile() {
	Serial.println("*** tryCopySDFile() ***\r\n");
 const char *path = "/font.ttf";
 if (!SD.begin(4) || !SD.exists(path)) {
	 return SPIFFS.begin(false) && SPIFFS.exists(path);
 }

 canvas.drawString("Copying font ...", 270, 664);
 canvas.pushCanvas(0,0,UPDATE_MODE_DU4);

 Serial.println("Formatting SPIFFS ...");
 if (!SPIFFS.format()) {
		 Serial.println("SPIFFS format Failed");
		 return false;
 }
 if (!SPIFFS.begin()) {
		 Serial.println("SPIFFS Mount Failed");
		 return false;
 }
 Serial.println("Copying font ...");
 File file = SD.open(path);
 SPIFFS.remove(path);
 File dest = SPIFFS.open(path, FILE_WRITE);
 if (!file || !dest) {
	 Serial.println("failed.");
	 return false;
 }
 uint8_t *buf = new uint8_t[4096];
 if (!buf) {
	 Serial.println("failed.");
	 return false;
 }
 size_t len, size, ret;
 size = len = file.size();
 while (len) {
	 size_t s = len;
	 if (s > 4096)
		 s = 4096;
	 file.read(buf, s);
	 if ((ret = dest.write(buf, s)) < s) {
		 Serial.print("write failed: ");
		 Serial.print(ret);
		 Serial.print(" - ");
		 Serial.println(s);
		 return false;
	 }
	 len -= s;
	 Serial.print(size - len);
	 Serial.print(" / ");
	 Serial.println(size);
 }
 delete[] buf;
 file.close();
 dest.close();

 if (!SPIFFS.exists(path)) {
	 Serial.println("no file");
	 return false;
 }
 dest = SPIFFS.open(path);
 len = dest.size();
 dest.close();
 if (len != size) {
	 Serial.print("size not match : ");
	 Serial.println(dest.size());
	 return false;
 }
 Serial.println("*** Done. ***\r\n");
 return true;
}

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

	Serial.print("*** setup ***\r\n");

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

	Serial.print("*** Initializing ***\r\n");
	canvas.drawString("... Initializing ...", 270, 640);
	canvas.pushCanvas(0,0,UPDATE_MODE_A2);

 if (!tryCopySDFile()) {
	 canvas.drawString("data copy failed !", 270, 254);
	 canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
	 for (;;);
 }

 // SPIFFSからフォントを読み込む
 canvas.drawString("Loading font ...", 270, 230);
 canvas.pushCanvas(0,0,UPDATE_MODE_DU4);
 Serial.println("*** Loading font from SPIFFS. ***\r\n");
 canvas.loadFont("/font.ttf", SPIFFS);
 Serial.println("*** Loading done. ***\r\n");
 canvas.createRender(96, 256);
 canvas.createRender(32, 256);

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

 M5.EPD.Clear(true);
}

// ---------------------------------------------------------------
void loop() {

 String buf;

 canvas.setTextDatum(TL_DATUM);
 canvas.fillCanvas(0);

 buf = "あいうえお かきくけこ";
 canvas.setTextSize(32);
 canvas.drawString(buf, 12, 16);

 buf = "漢字だよ!";
 canvas.setTextSize(96);
 canvas.drawString(buf, 12, 64);
 canvas.setTextSize(32);
 canvas.drawString("こんにちは 2", 12, 300);
 canvas.pushCanvas(0,0,UPDATE_MODE_A2);

 delay(3000);
canvas.drawString("こんにちは 3", 12, 350);
 canvas.drawString("こんにちは 4", 12, 400);
 canvas.pushCanvas(0,0,UPDATE_MODE_A2);
 delay(3000);
 Serial.print("*** loop ***\r\n");
}
// ---------------------------------------------------------------

Arduino IDE
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?