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: 再起動でクリアされないカウンター

0
Last updated at Posted at 2022-07-09

変数を SPIFFS に保存して再起動でもクリアされないカウンターです。
Preferences というライブラリーを使います。

IMG_20220710_063112.jpg

フォルダー構造

$ tree counter_preference/
counter_preference/
├── counter_preference.ino
└── touch.cpp
counter_preference.ino
// ---------------------------------------------------------------
//	counter_preference.ino
//
//					Jul/10/2022
// ---------------------------------------------------------------
#include <M5EPD.h>
#include <Preferences.h>

Preferences preferences;

M5EPD_Canvas canvas_home(&M5.EPD);

int minutes_sleep;
int point[2];

extern void touch_proc();
extern void show_sleep_time_proc();
// ---------------------------------------------------------------
void setup() {
	M5.begin();
	M5.TP.SetRotation(90);
	M5.EPD.SetRotation(90);

	M5.EPD.Clear(true);

	Serial.println("*** setup *** aaa\r\n");

	canvas_home.createCanvas(540,960);
	canvas_home.setTextSize(5);

	int ypos[]= {30,110,190,270,350,430,510};
	int xx = 50;
	
	canvas_home.drawString("Counter", xx, ypos[0]);
	canvas_home.drawString("Preference", xx, ypos[1]); 
	canvas_home.pushCanvas(0,0,UPDATE_MODE_A2);
	delay(2000);

	canvas_home.drawString("Good Afternoon", xx, ypos[2]);
	canvas_home.drawString("Good Evening", xx, ypos[3]);
	canvas_home.drawString("Good Night", xx, ypos[4]);
	canvas_home.drawString("Jul/10/2022", xx, ypos[5]);
	canvas_home.drawString("AM 06:12", xx, ypos[6]);
	canvas_home.pushCanvas(0,0,UPDATE_MODE_A2);

	delay(2000);

	preferences.begin("my-app", false);

	minutes_sleep = preferences.getUInt("sleep", 5);

	Serial.printf("minutes_sleep = %d\r\n",minutes_sleep);

	preferences.end();

	delay(1000);

	show_sleep_time_proc();
}

// ---------------------------------------------------------------
void loop()
{
	touch_proc();
	delay(30);
}

// ---------------------------------------------------------------
touch.cpp
// ---------------------------------------------------------------
//	touch.cpp
//
//					Jul/10/2022
// ---------------------------------------------------------------
#include <M5EPD.h>
#include <Preferences.h>

extern Preferences preferences;

extern M5EPD_Canvas canvas_home;
extern int minutes_sleep;
extern int point[];

boolean screen_touch_monitor_proc();
void value_change_proc();
bool finger_get_proc();
void draw_minutes_sleep_proc();
// ---------------------------------------------------------------
// [4-6]:
void touch_proc()
{
	boolean flag_touched = screen_touch_monitor_proc();

	if (flag_touched)
		{
		value_change_proc();
		}
}

// ---------------------------------------------------------------
boolean screen_touch_monitor_proc()
{
	boolean flag_touched = false;

	if(M5.TP.avaliable())
		{
		if(!M5.TP.isFingerUp())
			{
			if (finger_get_proc())
				{
				flag_touched = true;

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

	return flag_touched;
}

// ---------------------------------------------------------------
bool finger_get_proc()
{
	bool is_update = false;

	M5.TP.update();

	tp_finger_t FingerItem = M5.TP.readFinger(0);

	if ((point[0] != FingerItem.x) || (point[1] != FingerItem.y))
		{
		is_update = true;
		point[0] = FingerItem.x;
		point[1] = FingerItem.y;

		Serial.printf("Finger ID:%d-->X: %d 	Y: %d	Size: %d\r\n", FingerItem.id, FingerItem.x, FingerItem.y , FingerItem.size);

		}

	return is_update;
}

// ---------------------------------------------------------------
void value_change_proc()
{
	Serial.println("*** value_change_proc start ***");

	if ((600 < point[1]) && (point[1] < 700))
		{
		Serial.println("*** 600 < point[1] ***");
		Serial.printf("minutes_sleep = %d\r\n",minutes_sleep);

		if (point[0] < 200)
			{
			Serial.println("*** point[0] < 200 ***");

			minutes_sleep--;

			draw_minutes_sleep_proc();

			canvas_home.pushCanvas(0,0,UPDATE_MODE_A2);
			}
		else if (340 < point[0])
			{
			Serial.println("*** 340 < point[0] ***");

			minutes_sleep++;

			draw_minutes_sleep_proc();

			canvas_home.pushCanvas(0,0,UPDATE_MODE_A2);
			}

		Serial.printf("minutes_sleep = %d\r\n",minutes_sleep);
		}
}

// ---------------------------------------------------------------
void show_sleep_time_proc()
{
	Serial.println("*** show_sleep_time_proc ***");
//	Serial.printf("minutes_sleep = %d\r\n", minutes_sleep);

	const int xpos = 270;
	const int ypos = 600;
	canvas_home.drawRoundRect(xpos-200,ypos,100,100,30,WHITE);
	canvas_home.drawRoundRect(xpos+100,ypos,100,100,30,WHITE);

	canvas_home.drawRect(xpos-75,ypos,150,100,WHITE);

	canvas_home.setTextDatum(TC_DATUM);
	canvas_home.setTextSize(64);
	canvas_home.drawString("-",xpos-150,ypos+30);
	canvas_home.drawString("+",xpos+150,ypos+30);

	draw_minutes_sleep_proc();

	canvas_home.pushCanvas(0,0,UPDATE_MODE_A2);
}

// ---------------------------------------------------------------
void draw_minutes_sleep_proc()
{
	Serial.println("*** draw_minutes_sleep_proc ***");
	Serial.printf("minutes_sleep = %d\r\n", minutes_sleep);

	const int xpos = 270;
	const int ypos = 630;

	char buf[30];
	sprintf(buf, " %d ",minutes_sleep);
	canvas_home.drawString(buf, xpos, ypos);

	preferences.begin("my-app", false);
	preferences.putUInt("sleep", minutes_sleep);
	preferences.end();



}

// ---------------------------------------------------------------

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?