JOYSTICK.ino 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit the website for more
  7. information: https://docs.m5stack.com/en/unit/joystick
  8. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/joystick
  9. *
  10. * Describe: JOYSTICK.
  11. * Date: 2021/8/30
  12. *******************************************************************************
  13. Please connect to Port A,Read JOYSTICK Unit X, Y axis offset data and button
  14. status 请连接端口 A,读取操纵杆单位X, Y轴偏移数据和按钮状态
  15. */
  16. #include <M5Stack.h>
  17. #define JOY_ADDR 0x52 // define Joystick I2C address. 定义摇杆的I2C地址
  18. void setup() {
  19. M5.begin();
  20. M5.Power.begin();
  21. M5.Lcd.setCursor(70, 0, 4);
  22. M5.Lcd.println(("Joystick Test"));
  23. dacWrite(25, 0); // disable the speak noise. 禁用语音噪音
  24. Wire.begin(21, 22, 400000UL);
  25. }
  26. char data[100];
  27. void loop() {
  28. static uint8_t x_data, y_data, button_data;
  29. Wire.requestFrom(
  30. JOY_ADDR,
  31. 3); // Request 3 bytes from the slave device. 向从设备请求3个字节
  32. if (Wire.available()) { // If data is received. 如果接收到数据
  33. x_data = Wire.read();
  34. y_data = Wire.read();
  35. button_data = Wire.read();
  36. sprintf(data, "x:%d y:%d button:%d\n", x_data, y_data, button_data);
  37. Serial.print(data);
  38. M5.Lcd.setCursor(100, 50, 4);
  39. M5.Lcd.printf("X:%d ", x_data);
  40. M5.Lcd.setCursor(100, 80, 4);
  41. M5.Lcd.printf("Y:%d ", y_data);
  42. M5.Lcd.setCursor(100, 110, 4);
  43. M5.Lcd.printf("B:%d ", button_data);
  44. }
  45. delay(200);
  46. }