MicrophoneSignalTFT.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. {
  23. dacWrite(M5STACKFIRE_SPEAKER_PIN, 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: "); M5.Lcd.print(1000000 / SAMPLING_TIME_US); M5.Lcd.println(" Hz");
  33. }
  34. void waitForAutoTrigger()
  35. {
  36. uint32_t triggerLevel = 512;
  37. uint32_t hysteresis = 10;
  38. uint32_t timeOut_ms = 100;
  39. uint32_t timeOutLimit = millis() + timeOut_ms;
  40. uint32_t timeOutFlag = false;
  41. uint32_t adcValue = 0;
  42. adcValue = analogRead( ANALOG_SIGNAL_INPUT );
  43. // wait for low level
  44. while ( ( adcValue > triggerLevel - hysteresis ) && !timeOutFlag )
  45. {
  46. adcValue = analogRead( ANALOG_SIGNAL_INPUT );
  47. if ( millis() > timeOutLimit ) timeOutFlag = 1 ;
  48. }
  49. if ( !timeOutFlag )
  50. {
  51. // wait for high level
  52. while ( ( adcValue < triggerLevel + hysteresis ) && ( millis() < timeOutLimit ) )
  53. {
  54. adcValue = analogRead(ANALOG_SIGNAL_INPUT);
  55. }
  56. }
  57. }
  58. void showSignal()
  59. {
  60. int n;
  61. int oldx;
  62. int oldy;
  63. int oldSig;
  64. int x, y;
  65. for (n = 0; n < SIGNAL_LENGTH; n++)
  66. {
  67. x = n;
  68. y = map(adcBuffer[n], 0, 4096, VERTICAL_RESOLUTION, POSITION_OFFSET_Y);
  69. if (n > 0)
  70. {
  71. // delete old line element
  72. M5.Lcd.drawLine(oldx , oldSig, x, oldSignal[n], BLACK );
  73. // draw new line element
  74. if (n < SIGNAL_LENGTH - 1) // don't draw last element because it would generate artifacts
  75. {
  76. M5.Lcd.drawLine(oldx, oldy, x, y, GREEN );
  77. }
  78. }
  79. oldx = x;
  80. oldy = y;
  81. oldSig = oldSignal[n];
  82. oldSignal[n] = y;
  83. }
  84. }
  85. void loop(void)
  86. {
  87. int n;
  88. uint32_t nextTime = 0;
  89. waitForAutoTrigger();
  90. // record signal
  91. for (n = 0; n < SIGNAL_LENGTH; n++)
  92. {
  93. adcBuffer[n] = analogRead( ANALOG_SIGNAL_INPUT );
  94. // wait for next sample
  95. while (micros() < nextTime);
  96. nextTime = micros() + SAMPLING_TIME_US;
  97. }
  98. showSignal();
  99. }