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 1 year has passed since last update.

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

Last updated at Posted at 2022-06-21

IMG_20220621_143625.jpg

日本語フォントの入れ方はこちら
M5Paper: 日本語TTFファイルを SD から SPIFFS へコピー

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

プログラム

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);

}

// ---------------------------------------------------------------
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

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?