PLUS.ino 1019 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Description: This exmpale can display the encoder gear reading of the PLUS Module and the state of the keys.
  3. */
  4. #include <Arduino.h>
  5. #include <M5Stack.h>
  6. #define IrPin 13
  7. #define PLUS_ADDR 0x62
  8. int32_t number = 0;
  9. uint8_t press = 0;
  10. void setup() {
  11. M5.begin(true, false, false);
  12. M5.Power.begin();
  13. M5.Lcd.setTextFont(6);
  14. M5.Lcd.clear(BLACK);
  15. M5.Lcd.setTextColor(ORANGE, BLACK);
  16. Wire.begin();
  17. ledcSetup(1, 38000, 10);
  18. ledcAttachPin(IrPin, 1);
  19. }
  20. void plus_encode() {
  21. Wire.requestFrom(PLUS_ADDR, 2);
  22. while(Wire.available()) {
  23. int8_t encode = Wire.read();
  24. uint8_t press_n = Wire.read();
  25. number += encode;
  26. if(press_n == 0xff) {
  27. press = 0;
  28. }
  29. else {
  30. press = 1;
  31. }
  32. }
  33. }
  34. void loop() {
  35. char data[20];
  36. plus_encode();
  37. ledcWrite(1, ledcRead(1) ? 0 : 512);
  38. sprintf(data, "%d %d ", number, press);
  39. M5.Lcd.setCursor(100, 100);
  40. M5.Lcd.print(data);
  41. vTaskDelay(200);
  42. }