Speaker.ino 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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: Speaker example. 喇叭示例
  10. * Date: 2021/7/21
  11. *******************************************************************************
  12. */
  13. #include <M5Stack.h>
  14. // Set the frequency of the speaker
  15. //喇叭相关频率设置
  16. #define NOTE_D0 -1
  17. #define NOTE_D1 294
  18. #define NOTE_D2 330
  19. #define NOTE_D3 350
  20. #define NOTE_D4 393
  21. #define NOTE_D5 441
  22. #define NOTE_D6 495
  23. #define NOTE_D7 556
  24. #define NOTE_DL1 147
  25. #define NOTE_DL2 165
  26. #define NOTE_DL3 175
  27. #define NOTE_DL4 196
  28. #define NOTE_DL5 221
  29. #define NOTE_DL6 248
  30. #define NOTE_DL7 278
  31. #define NOTE_DH1 589
  32. #define NOTE_DH2 661
  33. #define NOTE_DH3 700
  34. #define NOTE_DH4 786
  35. #define NOTE_DH5 882
  36. #define NOTE_DH6 990
  37. #define NOTE_DH7 112
  38. /* After M5Core is started or reset
  39. the program in the setUp () function will be run, and this part will only be run
  40. once. 在 M5Core
  41. 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
  42. void setup() {
  43. M5.begin(); // Init M5Core. 初始化 M5Core
  44. M5.Power.begin(); // Init Power module. 初始化电源
  45. M5.Lcd.setTextSize(2); // Set the text size to 2. 设置文字大小为2
  46. M5.Lcd.println("M5Stack Speaker test"); // Screen printingformatted string.
  47. // 输出格式化字符串
  48. }
  49. /* After the program in setup() runs, it runs the program in loop()
  50. The loop() function is an infinite loop in which the program runs repeatedly
  51. 在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
  52. loop()函数是一个死循环,其中的程序会不断的重复运行 */
  53. void loop() {
  54. M5.update(); // Read the press state of the key. 读取按键 A, B, C 的状态
  55. if (M5.BtnA.wasPressed()) { // Check if the key is pressed. 如果按键A被按下
  56. M5.Lcd.println("A wasPressed");
  57. M5.Speaker.tone(NOTE_DH2, 200); // Set the speaker to ring at 661Hz for
  58. // 200ms. 设定喇叭以661Hz频率响200ms
  59. } else if (M5.BtnB.wasPressed()) {
  60. M5.Lcd.println("B wasPressed");
  61. M5.Speaker.tone(NOTE_DH7); // Set the horn to continuously sound at
  62. // 112Hz. 设定喇叭以112Hz频率持续响
  63. } else if (M5.BtnC.wasPressed()) {
  64. M5.Lcd.println("C wasPressed");
  65. M5.Speaker.end(); // Turn off the speaker. 关闭喇叭
  66. }
  67. }