MicrophoneSignalTFT.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /************************************************************************
  2. M5StackFire simple oscilloscope example
  3. The oscilloscope has a auto trigger function to achive a stable
  4. visual impression when a repeating signal like a sine wave is applied.
  5. original version
  6. STM32F429 Discovery June 2018, ChrisMicro
  7. this version reworked for
  8. M5StackFire September 2018, ChrisMicro
  9. ************************************************************************/
  10. #include <M5Stack.h>
  11. #define M5STACKFIRE_MICROPHONE_PIN 34
  12. #define M5STACKFIRE_SPEAKER_PIN 25 // speaker DAC, only 8 Bit
  13. #define HORIZONTAL_RESOLUTION 320
  14. #define VERTICAL_RESOLUTION 240
  15. #define POSITION_OFFSET_Y 20
  16. #define SIGNAL_LENGTH HORIZONTAL_RESOLUTION
  17. uint16_t oldSignal[SIGNAL_LENGTH];
  18. uint16_t adcBuffer[SIGNAL_LENGTH];
  19. #define SAMPLING_TIME_US 25
  20. #define ANALOG_SIGNAL_INPUT M5STACKFIRE_MICROPHONE_PIN
  21. void setup() {
  22. dacWrite(M5STACKFIRE_SPEAKER_PIN,
  23. 0); // make sure that the speaker is quite
  24. M5.Lcd.begin();
  25. M5.Power.begin();
  26. M5.Lcd.fillScreen(BLACK);
  27. M5.Lcd.fillRect(10, 1, 150, 160, BLACK);
  28. M5.Lcd.setCursor(0, 0);
  29. M5.Lcd.setTextColor(GREEN); // M5.Lcd.setTextSize(3);
  30. M5.Lcd.setTextSize(1);
  31. M5.Lcd.println("analog input MIC");
  32. M5.Lcd.print("sampling frequency: ");
  33. M5.Lcd.print(1000000 / SAMPLING_TIME_US);
  34. M5.Lcd.println(" Hz");
  35. }
  36. void waitForAutoTrigger() {
  37. uint32_t triggerLevel = 512;
  38. uint32_t hysteresis = 10;
  39. uint32_t timeOut_ms = 100;
  40. uint32_t timeOutLimit = millis() + timeOut_ms;
  41. uint32_t timeOutFlag = false;
  42. uint32_t adcValue = 0;
  43. adcValue = analogRead(ANALOG_SIGNAL_INPUT);
  44. // wait for low level
  45. while ((adcValue > triggerLevel - hysteresis) && !timeOutFlag) {
  46. adcValue = analogRead(ANALOG_SIGNAL_INPUT);
  47. if (millis() > timeOutLimit) timeOutFlag = 1;
  48. }
  49. if (!timeOutFlag) {
  50. // wait for high level
  51. while ((adcValue < triggerLevel + hysteresis) &&
  52. (millis() < timeOutLimit)) {
  53. adcValue = analogRead(ANALOG_SIGNAL_INPUT);
  54. }
  55. }
  56. }
  57. void showSignal() {
  58. int n;
  59. int oldx;
  60. int oldy;
  61. int oldSig;
  62. int x, y;
  63. for (n = 0; n < SIGNAL_LENGTH; n++) {
  64. x = n;
  65. y = map(adcBuffer[n], 0, 4096, VERTICAL_RESOLUTION, POSITION_OFFSET_Y);
  66. if (n > 0) {
  67. // delete old line element
  68. M5.Lcd.drawLine(oldx, oldSig, x, oldSignal[n], BLACK);
  69. // draw new line element
  70. if (n < SIGNAL_LENGTH - 1) // don't draw last element because it
  71. // would generate artifacts
  72. {
  73. M5.Lcd.drawLine(oldx, oldy, x, y, GREEN);
  74. }
  75. }
  76. oldx = x;
  77. oldy = y;
  78. oldSig = oldSignal[n];
  79. oldSignal[n] = y;
  80. }
  81. }
  82. void loop(void) {
  83. int n;
  84. uint32_t nextTime = 0;
  85. waitForAutoTrigger();
  86. // record signal
  87. for (n = 0; n < SIGNAL_LENGTH; n++) {
  88. adcBuffer[n] = analogRead(ANALOG_SIGNAL_INPUT);
  89. // wait for next sample
  90. while (micros() < nextTime)
  91. ;
  92. nextTime = micros() + SAMPLING_TIME_US;
  93. }
  94. showSignal();
  95. }