Button.ino 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: Button example. 按键示例
  10. * Date: 2022/7/1
  11. *******************************************************************************
  12. Press button A/B/C to display the corresponding output on the screen
  13. 按下按键A/B/C,在屏幕上显示相应输出
  14. */
  15. #include <M5Stack.h>
  16. /* After M5Core is started or reset
  17. the program in the setUp () function will be run, and this part will only be
  18. run once. 在 M5Core
  19. 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
  20. void setup() {
  21. M5.begin(); // Init M5Core. 初始化 M5Core
  22. M5.Power.begin(); // Init Power module. 初始化电源模块
  23. M5.Lcd.setTextColor(
  24. YELLOW); // Set the font color to yellow. 设置字体颜色为黄色
  25. M5.Lcd.setTextSize(2); // Set the font size. 设置字体大小为2
  26. M5.Lcd.setCursor(65, 10); // Move the cursor position to (65, 10).
  27. // 移动光标位置到 (65, 10) 处
  28. M5.Lcd.println(
  29. "Button example"); // The screen prints the formatted string and wraps
  30. // the line. 输出格式化字符串并换行
  31. M5.Lcd.setCursor(3, 35);
  32. M5.Lcd.println("Press button B for 700ms");
  33. M5.Lcd.println("to clear screen.");
  34. M5.Lcd.setTextColor(RED);
  35. }
  36. /* After the program in setup() runs, it runs the program in loop()
  37. The loop() function is an infinite loop in which the program runs repeatedly
  38. 在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
  39. loop()函数是一个死循环,其中的程序会不断的重复运行 */
  40. void loop() {
  41. M5.update(); // Read the press state of the key. 读取按键 A, B, C 的状态
  42. if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
  43. M5.Lcd.print('A');
  44. } else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
  45. M5.Lcd.print('B');
  46. } else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
  47. M5.Lcd.print('C');
  48. } else if (M5.BtnB.wasReleasefor(700)) {
  49. M5.Lcd.clear(WHITE); // Clear the screen and set white to the
  50. // background color. 清空屏幕并将白色设置为底色
  51. M5.Lcd.setCursor(0, 0);
  52. }
  53. }