M5Paper: バッテリーの残量を表示

![IMG_20220621_143625.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/a2cbbc27-bb76-c8ba-8edf-dbf0bac1bc52.jpeg)

日本語フォントの入れ方はこちら
[M5Paper: 日本語TTFファイルを SD から SPIFFS へコピー](https://qiita.com/ekzemplaro/items/1ef653a24b8ca22998ed)

4.35 V で 100%
3.2 V で 0% になります。

## プログラム ##

```text:battery.ino
// ---------------------------------------------------------------
//	battery.ino
//
//					Jun/21/2022
// ---------------------------------------------------------------
#include <M5EPD.h>

void read_font_proc();

M5EPD_Canvas canvas_a(&M5.EPD);

// ---------------------------------------------------------------
void setup()
{
	Serial.println("*** setup *** start ***");
	M5.begin();
	M5.EPD.SetRotation(90);
	M5.TP.SetRotation(90);
	M5.EPD.Clear(true);

	read_font_proc();
	
	Serial.println("*** setup *** end ***");
}

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

	int ivolt = M5.getBatteryVoltage();

	float volt = ivolt / 1000.0;

	Serial.printf("ivolt = %d\r\n",ivolt);
	Serial.printf("volt = %f V\r\n",volt);

	float dmax = 4.35 - 3.2;
	float delt = volt - 3.2;
	float percentage = (delt / dmax) * 100.0;
	Serial.printf("percentage = %f \%\r\n",percentage);

	char voltStr[16];
	char perStr[16];
	dtostrf(volt, 2, 2 , voltStr);
	dtostrf(percentage, 2, 2 , perStr);

	
	int xpos = 250;
	canvas_a.fillCanvas(0x0);
	canvas_a.drawString("Voltage: " + String(voltStr) + " V", xpos, 200);

	canvas_a.drawString("Percentage: " + String(perStr) + " %", xpos, 300);

	canvas_a.drawString("ボルト: " + String(voltStr) + " V", xpos, 400);

	canvas_a.drawString("パーセント: " + String(perStr) + " %", xpos, 500);

	canvas_a.pushCanvas(0,0,UPDATE_MODE_A2);
	Serial.println("*** loop *** ccc ***");

	delay(2000);

}

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

```cpp:read_font.cpp
// ---------------------------------------------------------------
//	read_font.cpp
//
//						Jun/21/2022
// ---------------------------------------------------------------
#include <M5EPD.h>
#include <FS.h>

extern M5EPD_Canvas canvas_a;
// ---------------------------------------------------------------
// [2]:
void read_font_proc()
{
	Serial.print("*** read_font_proc start ***\r\n");
	canvas_a.createCanvas(540, 960);
	canvas_a.setTextDatum(TC_DATUM);
	canvas_a.setTextSize(3);

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

	SPIFFS.begin(false);
	Serial.print("*** read_font_proc bbb ***\r\n");

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

	canvas_a.createRender(32, 256);

	Serial.print("*** read_font_proc ddd ***\r\n");
  canvas_a.setTextSize(32);
  
	canvas_a.drawString("OK!", 270, 254);
	canvas_a.pushCanvas(0,0,UPDATE_MODE_A2);
	Serial.print("*** read_font_proc eee ***\r\n");

	M5.EPD.Clear(true);
	Serial.print("*** read_font_proc fff ***\r\n");


	Serial.print("*** read_font_proc end ***\r\n");
}

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

## Arduino IDE ##
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/a090d106-68ff-e68c-a346-8740db19ca9b.png)