Newer
Older
M5StickCPlus_FactoryTest2022 / SampleSrc / pref01.ino
#include <M5StickCPlus.h>
#include <Preferences.h>

Preferences pref;

int count = 0;
void setup() {
  M5.begin(115200);
  M5.Lcd.setRotation(0); //縦型
  M5.Lcd.fillScreen(CYAN);
  M5.Lcd.setTextSize(2);
  loadCount(&count);
  M5.Beep.tone(2000, 500);
  M5.Lcd.printf("count = %d\n", count);
}

void loop() {
  M5.Beep.update();
  M5.update();
  if (M5.BtnA.wasReleasefor(1000) ) { // Aボタン長押し
    count = 0;
    saveCount(&count);
    M5.Beep.tone(1200, 300);
    M5.Lcd.printf("count = %d\n", count);
  } else if (M5.BtnA.wasReleased()) { // Aボタン押し
    count++ ;
    saveCount(&count);
    M5.Beep.tone(2000, 300);
    M5.Lcd.printf("count = %d\n", count);
  } else if (M5.BtnB.wasReleased()) { // Bボタン押し
    reboot();
  } else if (M5.Axp.GetBtnPress() == 2) { // 電源ボタン押し
    poweroff();
  }
  delay(50);
}

void loadCount(int *c) {
  pref.begin("mydata", false);
  *c = pref.getInt("count");
  pref.end();
}
void saveCount(int *c) {
  pref.begin("mydata", false);
  pref.putInt("count", *c);
  pref.end();
}

void poweroff() {
  M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80);//PowerOff
}

void reboot() {
  ESP.restart();
}