m5paper_gpio_button.ino

M5Paper IoT Development Kit with 4.7-inch Touchscreen e-Ink Display · File : m5paper_gpio_button.ino · Language : C++ · Use case : Gérer les boutons physiques du M5Paper (UP, DOWN, LEFT, RIGHT) pour changer l'affichage.

Uses the four physical buttons to increment, decrement, or reset a counter displayed on the screen.

Configures internal GPIOs for buttons. The pressedFor method handles software debouncing. The display is updated only if the state changes to save e-Ink energy.

m5paper_gpio_button.ino

#include <M5EPD.h>

M5EPD_Canvas canvas(&M5.EPD);
int counter = 0;

void setup() {
  M5.begin();
  M5.EPD.SetRotation(0);
  M5.EPD.Clear(true);
  canvas.createCanvas(800, 480);
  canvas.setTextSize(10);
  updateDisplay();
}

void loop() {
  M5.update();
  bool changed = false;
  
  if (M5.BtnUP.pressedFor(100)) { counter++; changed = true; }
  if (M5.BtnDOWN.pressedFor(100)) { counter--; changed = true; }
  if (M5.BtnLEFT.pressedFor(100)) { counter = 0; changed = true; }
  if (M5.BtnRIGHT.pressedFor(100)) { counter = 100; changed = true; }

  if (changed) {
    updateDisplay();
    delay(300); // Anti-rebond simple
  }
}

void updateDisplay() {
  canvas.fillCanvas(G16_WHITE);
  canvas.setTextColor(G16_BLACK);
  canvas.drawCentreString(String(counter), 400, 240, 4);
  canvas.pushCanvas(0, 0, UPDATE_MODE_DU4);
}

← Back to the board