JOYSTICK.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <M5Stack.h>
  2. #include "Wire.h"
  3. #define FACE_JOY_ADDR 0x5e
  4. void Init(){
  5. Wire.begin();
  6. for (int i = 0; i < 256; i++)
  7. {
  8. Wire.beginTransmission(FACE_JOY_ADDR);
  9. Wire.write(i % 4);
  10. Wire.write(random(256) * (256 - i) / 256);
  11. Wire.write(random(256) * (256 - i) / 256);
  12. Wire.write(random(256) * (256 - i) / 256);
  13. Wire.endTransmission();
  14. delay(2);
  15. }
  16. Led(0, 0, 0, 0);
  17. Led(1, 0, 0, 0);
  18. Led(2, 0, 0, 0);
  19. Led(3, 0, 0, 0);
  20. }
  21. void Led(int indexOfLED, int r, int g, int b){
  22. Wire.beginTransmission(FACE_JOY_ADDR);
  23. Wire.write(indexOfLED);
  24. Wire.write(r);
  25. Wire.write(g);
  26. Wire.write(b);
  27. Wire.endTransmission();
  28. }
  29. void setup() {
  30. M5.begin();
  31. M5.Power.begin();
  32. M5.Lcd.clear();
  33. M5.Lcd.setCursor(60, 0, 4);
  34. M5.Lcd.printf("FACE JOYSTICK");
  35. Init();
  36. }
  37. uint8_t x_data_L;
  38. uint8_t x_data_H;
  39. uint16_t x_data;
  40. uint8_t y_data_L;
  41. uint8_t y_data_H;
  42. uint16_t y_data;
  43. uint8_t button_data;
  44. char data[100];
  45. void loop() {
  46. Wire.requestFrom(FACE_JOY_ADDR, 5);
  47. if (Wire.available()) {
  48. y_data_L = Wire.read();
  49. y_data_H = Wire.read();
  50. x_data_L = Wire.read();
  51. x_data_H = Wire.read();
  52. button_data = Wire.read();// Z(0: released 1: pressed)
  53. x_data = x_data_H << 8 |x_data_L;
  54. y_data = y_data_H << 8 |y_data_L;
  55. sprintf(data, "x:%d y:%d button:%d\n", x_data, y_data, button_data);
  56. Serial.print(data);
  57. M5.Lcd.setCursor(45, 120);
  58. M5.Lcd.println(data);
  59. if (x_data > 600){
  60. Led(2, 0, 0, 50);
  61. Led(0, 0, 0, 0);
  62. }
  63. else if (x_data < 400)
  64. {
  65. Led(0, 0, 0, 50);
  66. Led(2, 0, 0, 0);
  67. }
  68. else{
  69. Led(0, 0, 0,0);
  70. Led(2, 0, 0, 0);
  71. }
  72. if (y_data > 600)
  73. {
  74. Led(3, 0, 0, 50);
  75. Led(1, 0, 0, 0);
  76. }
  77. else if (y_data < 400)
  78. {
  79. Led(1, 0, 0, 50);
  80. Led(3, 0, 0, 0);
  81. }
  82. else{
  83. Led(1, 0, 0, 0);
  84. Led(3, 0, 0, 0);
  85. }
  86. }
  87. delay(200);
  88. }