123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #include <M5Stack.h>
- #define M5STACKFIRE_MICROPHONE_PIN 34
- #define M5STACKFIRE_SPEAKER_PIN 25
- #define HORIZONTAL_RESOLUTION 320
- #define VERTICAL_RESOLUTION 240
- #define POSITION_OFFSET_Y 20
- #define SIGNAL_LENGTH HORIZONTAL_RESOLUTION
- uint16_t oldSignal[SIGNAL_LENGTH];
- uint16_t adcBuffer[SIGNAL_LENGTH];
- #define SAMPLING_TIME_US 25
- #define ANALOG_SIGNAL_INPUT M5STACKFIRE_MICROPHONE_PIN
- void setup()
- {
- dacWrite(M5STACKFIRE_SPEAKER_PIN, 0);
- M5.Lcd.begin();
- M5.Power.begin();
- M5.Lcd.fillScreen( BLACK );
- M5.Lcd.fillRect(10, 1, 150, 160, BLACK);
- M5.Lcd.setCursor(0, 0);
- M5.Lcd.setTextColor(GREEN);
- M5.Lcd.setTextSize(1);
- M5.Lcd.println("analog input MIC");
- M5.Lcd.print("sampling frequency: "); M5.Lcd.print(1000000 / SAMPLING_TIME_US); M5.Lcd.println(" Hz");
- }
- void waitForAutoTrigger()
- {
- uint32_t triggerLevel = 512;
- uint32_t hysteresis = 10;
- uint32_t timeOut_ms = 100;
- uint32_t timeOutLimit = millis() + timeOut_ms;
- uint32_t timeOutFlag = false;
- uint32_t adcValue = 0;
- adcValue = analogRead( ANALOG_SIGNAL_INPUT );
-
- while ( ( adcValue > triggerLevel - hysteresis ) && !timeOutFlag )
- {
- adcValue = analogRead( ANALOG_SIGNAL_INPUT );
- if ( millis() > timeOutLimit ) timeOutFlag = 1 ;
- }
- if ( !timeOutFlag )
- {
-
- while ( ( adcValue < triggerLevel + hysteresis ) && ( millis() < timeOutLimit ) )
- {
- adcValue = analogRead(ANALOG_SIGNAL_INPUT);
- }
- }
- }
- void showSignal()
- {
- int n;
- int oldx;
- int oldy;
- int oldSig;
- int x, y;
- for (n = 0; n < SIGNAL_LENGTH; n++)
- {
- x = n;
- y = map(adcBuffer[n], 0, 4096, VERTICAL_RESOLUTION, POSITION_OFFSET_Y);
- if (n > 0)
- {
-
- M5.Lcd.drawLine(oldx , oldSig, x, oldSignal[n], BLACK );
-
- if (n < SIGNAL_LENGTH - 1)
- {
- M5.Lcd.drawLine(oldx, oldy, x, y, GREEN );
- }
- }
- oldx = x;
- oldy = y;
- oldSig = oldSignal[n];
- oldSignal[n] = y;
- }
- }
- void loop(void)
- {
- int n;
- uint32_t nextTime = 0;
- waitForAutoTrigger();
-
- for (n = 0; n < SIGNAL_LENGTH; n++)
- {
- adcBuffer[n] = analogRead( ANALOG_SIGNAL_INPUT );
-
- while (micros() < nextTime);
- nextTime = micros() + SAMPLING_TIME_US;
- }
- showSignal();
- }
|