MAKEY.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: MAKEY.
  10. * Date: 2021/8/19
  11. *******************************************************************************
  12. Please connect to Port A(22、21),Connect port G on the MAKEY Unit to other
  13. ports (except 5V) to control the tone it emits 请连接端口A(22、21),将MAKEY
  14. Unit上的G口与其他口连接(除5V外)控制其发出音调
  15. */
  16. #include <M5Stack.h>
  17. #define NOTE_D1 294
  18. #define NOTE_DL1 261
  19. #define NOTE_DL2 293
  20. #define NOTE_DL3 329
  21. #define NOTE_DL4 349
  22. #define NOTE_DL5 392
  23. #define NOTE_DL6 440
  24. #define NOTE_DL7 494
  25. void setup() {
  26. M5.begin(); // Init M5Stack. 初始化M5Stack
  27. M5.Power.begin(); // Init power 初始化电源模块
  28. M5.lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2
  29. M5.Lcd.println("M5Stack Makey Piano.");
  30. Wire.begin(); // Wire init, adding the I2C bus. Wire初始化, 加入i2c总线
  31. }
  32. int Key1 = 0, Key2 = 0, Index = 0;
  33. void showKey() {
  34. M5.Lcd.setCursor(20, 200);
  35. M5.Lcd.setTextColor(BLUE, BLACK);
  36. M5.Lcd.print(Index);
  37. M5.Lcd.print(" ");
  38. M5.Lcd.setCursor(5 + 0 * 16, 110);
  39. if ((Key1 & (0x01 << 0)) == 0x00)
  40. M5.Lcd.setTextColor(WHITE, BLACK);
  41. else {
  42. M5.Lcd.setTextColor(RED, BLACK);
  43. M5.Speaker.tone(NOTE_DL1, 20);
  44. }
  45. M5.Lcd.print("1");
  46. M5.Lcd.setCursor(5 + 1 * 16, 110);
  47. if ((Key1 & (0x01 << 1)) == 0x00)
  48. M5.Lcd.setTextColor(WHITE, BLACK);
  49. else {
  50. M5.Lcd.setTextColor(RED, BLACK);
  51. M5.Speaker.tone(NOTE_DL2, 20);
  52. }
  53. M5.Lcd.print("2");
  54. M5.Lcd.setCursor(5 + 2 * 16, 110);
  55. if ((Key1 & (0x01 << 2)) == 0x00)
  56. M5.Lcd.setTextColor(WHITE, BLACK);
  57. else {
  58. M5.Lcd.setTextColor(RED, BLACK);
  59. M5.Speaker.tone(NOTE_DL3, 20);
  60. }
  61. M5.Lcd.print("3");
  62. M5.Lcd.setCursor(5 + 3 * 16, 110);
  63. if ((Key1 & (0x01 << 3)) == 0x00)
  64. M5.Lcd.setTextColor(WHITE, BLACK);
  65. else {
  66. M5.Lcd.setTextColor(RED, BLACK);
  67. M5.Speaker.tone(NOTE_DL4, 20);
  68. }
  69. M5.Lcd.print("4");
  70. M5.Lcd.setCursor(5 + 4 * 16, 110);
  71. if ((Key1 & (0x01 << 4)) == 0x00)
  72. M5.Lcd.setTextColor(WHITE, BLACK);
  73. else {
  74. M5.Lcd.setTextColor(RED, BLACK);
  75. M5.Speaker.tone(NOTE_DL5, 20);
  76. }
  77. M5.Lcd.print("5");
  78. M5.Lcd.setCursor(5 + 5 * 16, 110);
  79. if ((Key1 & (0x01 << 5)) == 0x00)
  80. M5.Lcd.setTextColor(WHITE, BLACK);
  81. else {
  82. M5.Lcd.setTextColor(RED, BLACK);
  83. M5.Speaker.tone(NOTE_DL6, 20);
  84. }
  85. M5.Lcd.print("6");
  86. M5.Lcd.setCursor(5 + 6 * 16, 110);
  87. if ((Key1 & (0x01 << 6)) == 0x00)
  88. M5.Lcd.setTextColor(WHITE, BLACK);
  89. else {
  90. M5.Lcd.setTextColor(RED, BLACK);
  91. M5.Speaker.tone(NOTE_DL7, 20);
  92. }
  93. M5.Lcd.print("7");
  94. M5.Lcd.setCursor(10 + 7 * 16, 110);
  95. if ((Key1 & (0x01 << 7)) == 0x00)
  96. M5.Lcd.setTextColor(WHITE, BLACK);
  97. else {
  98. M5.Lcd.setTextColor(RED, BLACK);
  99. M5.Speaker.tone(NOTE_D1, 20);
  100. }
  101. }
  102. int CommandStatus = 0;
  103. void loop() {
  104. Wire.requestFrom(0x51, 2);
  105. while (Wire.available()) {
  106. Key1 = Wire.read();
  107. Key2 = Wire.read();
  108. Index++;
  109. }
  110. showKey();
  111. // delay(10);
  112. M5.update();
  113. }