PLUS.ino 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/module/plus
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/module/plus
  8. *
  9. * Describe: PLUS.
  10. * Date: 2021/9/2
  11. *******************************************************************************
  12. This exmpale can display the encoder gear reading of the PLUS Module and the
  13. state of the keys. 本例可以显示PLUS模块的编码器齿轮读数和按键状态。
  14. */
  15. #include <Arduino.h>
  16. #include <M5Stack.h>
  17. #define IrPin 13
  18. #define PLUS_ADDR 0x62
  19. int32_t number = 0;
  20. uint8_t press = 0;
  21. void setup() {
  22. M5.begin(true, false, false);
  23. M5.Power.begin();
  24. M5.Lcd.setTextFont(6);
  25. M5.Lcd.clear(BLACK);
  26. M5.Lcd.setTextColor(ORANGE, BLACK);
  27. Wire.begin();
  28. ledcSetup(1, 38000, 10);
  29. ledcAttachPin(IrPin, 1);
  30. }
  31. void plus_encode() {
  32. Wire.requestFrom(PLUS_ADDR, 2);
  33. while (Wire.available()) {
  34. int8_t encode = Wire.read();
  35. uint8_t press_n = Wire.read();
  36. number += encode;
  37. if (press_n == 0xff) {
  38. press = 0;
  39. } else {
  40. press = 1;
  41. }
  42. }
  43. }
  44. void loop() {
  45. char data[20];
  46. plus_encode();
  47. ledcWrite(1, ledcRead(1) ? 0 : 512);
  48. sprintf(data, "%d %d ", number, press);
  49. M5.Lcd.setCursor(100, 100);
  50. M5.Lcd.print(data);
  51. vTaskDelay(200);
  52. }