Newer
Older
m5stickcplus / src / pref01.ino
@Motoki Motoki 22 days ago 1 KB Plus2
#include <M5Unified.h>
#include <Preferences.h>

Preferences pref;
int count = 0;

void setup() {
  auto cfg = M5.config(); // M5Unified の設定
  M5.begin(cfg);

  M5.Display.setRotation(0); // 縦型
  M5.Display.fillScreen(CYAN);
  M5.Display.setTextSize(2);
  M5.Display.setTextScroll(true);
  loadCount(&count);
  M5.Speaker.tone(2000, 500);
  M5.Display.printf("count = %d\n", count);
}

void loop() {
  M5.update();

  if (M5.BtnA.wasReleaseFor(1000)) { // Aボタン長押し
    count = 0;
    saveCount(&count);
    M5.Speaker.tone(1200, 300);
    M5.Display.printf("count = %d\n", count);
  } else if (M5.BtnA.wasReleased()) { // Aボタン押し
    count++;
    saveCount(&count);
    M5.Speaker.tone(2000, 300);
    M5.Display.printf("count = %d\n", count);
  } else if (M5.BtnB.wasReleased()) { // Bボタン押し
    reboot();
  } else if (M5.Power.isCharging() == false && M5.BtnPWR.wasReleased()) { // 電源ボタン押し
    poweroff();
  }

  delay(50);
}

void loadCount(int *c) {
  pref.begin("mydata", false);
  *c = pref.getInt("count", 0); // 初回時に 0 をデフォルト値とする
  pref.end();
}

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

void poweroff() {
  M5.Power.powerOff();
}

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