Verwendung des eingebauten Mikrofons bei der T-WATCH V3
(EN google-translate)
(PL google-translate)
|
Verwendungsmöglichkeiten des Mikrofons
|
Bild 0-1: Darstellung von Stille bzw. Grundrauschen: Die Wellenform ist chaotisch und die Differenz zwischen Minimum (64032) und Maximum (64062) ist sehr klein.
Bild 0-2: Darstellung eines Pfeiftones (nahezu Sinuston). Die Differenz zwischen Minimum (63696) und Maximum (64403) ist deutlich größer. Grundsätzlich ließe sich die Grundfrequenz des Tones bestimmen.
Bestimmung der Frequenz des Pfeiftones
|
Initialisierung des Mikrofons in der setup-Funktion
|
I2S_CHANNEL_MONO, I2S_BITS_PER_SAMPLE_16BIT, ... sind vordefinierte Konstanten.
Verwendung des Mikrofons in der loop-Funktion
|
TWATCH_PROC023_Microphone.zip -- Testbeispiel (Projekt für die Arduino IDE)
#include "twatch.h"
#include "variables.h"
#include "functions.h"
//for microphone:
#include <driver/i2s.h>
#define BUFFER_SIZE (2*128)
// TWATCH 2020 V3 PDM microphone pin
#define MIC_DATA 2
#define MIC_CLOCK 0
uint8_t buffer[BUFFER_SIZE] = {0};
int x=0;
int micvalue=0;
int micvalue_old=0;
int minimum=0;
int maximum=0;
void setup()
{
setupTWATCH();
//begin setup microphone
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = 128,
};
i2s_pin_config_t i2s_cfg;
i2s_cfg.bck_io_num = I2S_PIN_NO_CHANGE;
i2s_cfg.ws_io_num = MIC_CLOCK;
i2s_cfg.data_out_num = I2S_PIN_NO_CHANGE;
i2s_cfg.data_in_num = MIC_DATA;
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &i2s_cfg);
i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
//end setup microphone
setFont(1,255,255,255, 0,255,0);
stroke(0,0,255);
backlight(true);
}
void loop()
{
size_t read_len = 0;
i2s_read(I2S_NUM_0, (char *) buffer, BUFFER_SIZE, &read_len, portMAX_DELAY);
minimum = buffer[0*2]+buffer[0*2+1]*256;
maximum = minimum;
for (int i = 0; i < BUFFER_SIZE/2 ; i++)
{
micvalue = buffer[i*2]+buffer[i*2+1]*256;
if(micvalue>maximum) maximum = micvalue;
if(micvalue<minimum) minimum = micvalue;
}
clear();
for (int i = 0; i < BUFFER_SIZE/2 ; i++)
{
micvalue = buffer[i*2]+buffer[i*2+1]*256;
micvalue-=minimum;
micvalue*=height;
micvalue/=(maximum-minimum);
line(i+110,micvalue_old,i+111,micvalue);
micvalue_old = micvalue;
}
//micvalue = buffer[0*2]+buffer[0*2+1]*256;
cursor(0,0);
editor(minimum);
editor("\n");
editor(maximum);
delay(100);
}
Code 0-1: Beispielquelltext -- setup- und loop-Funktion im Haupttab.
Tab twatch.h
void setupTWATCH()
{
//Serial.begin(115200); //weglassen, sonst ev. im Normalbetrieb Probleme
watch = TTGOClass::getWatch();
watch->begin();
// Turn on the backlight .. aber nicht nur das!!!!
watch->openBL();
tft = watch->tft;
sensor = watch->bma;
hintergrundlicht = watch->bl; //Hintergrundlicht als Objekt verfügbar machen!
// Accel parameter structure (siehe Kommentare in Originalbeispiel!)
Acfg cfg;
cfg.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
cfg.range = BMA4_ACCEL_RANGE_2G;
cfg.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
cfg.perf_mode = BMA4_CONTINUOUS_MODE;
// Configure the BMA423 accelerometer
sensor->accelConfig(cfg);
// Enable BMA423 accelerometer
sensor->enableAccel();
// You can also turn it off
// sensor->disableAccel();
// Some display settings
//tft->setTextColor(random(0xFFFF));
tft->setTextColor(TFT_GREEN, TFT_BLACK);
//tft->drawString("BMA423 accel", 25, 50, 4);
tft->setTextFont(4);
//tft->setTextColor(TFT_WHITE, TFT_BLACK);
//Check if the RTC clock matches, if not, use compile time
watch->rtc->check();
//Synchronize time to system time
watch->rtc->syncToSystem();
// Get the current data
tnow = watch->rtc->getDateTime();
//wav Tongenerator
/*
watch->enableAudio();
watch->enableLDO3();
file_wav = new AudioFileSourceFunction(1.);
file_wav->addAudioGenerators(sine_wave);
#if defined(STANDARD_BACKPLANE)
output_wav = new AudioOutputI2S(0, 1);
#elif defined(EXTERNAL_DAC_BACKPLANE)
output_wav = new AudioOutputI2S();
//External DAC decoding
output_wav->SetPinout(TWATCH_DAC_IIS_BCK, TWATCH_DAC_IIS_WS, TWATCH_DAC_IIS_DOUT);
#endif
wav_generator = new AudioGeneratorWAV();
wav_generator->begin(file_wav, output_wav);
*/
}
Code 0-2: Allgemeine Setup-Funktion für die T-WATCH, Tab twatch.h in Projekt TWATCH_PROC023. Der Bereich zur Initialisierung der Tonerzeugung musste auskommentiert werden!