#include <driver/i2s.h>
#define outPin 35
#define adcPin 04
// I2S
#define I2S_SAMPLE_RATE (3000)
#define ADC_INPUT (ADC1_CHANNEL_4) //pin 32
#define I2S_DMA_BUF_LEN (8)
// The 4 high bits are the channel, and the data is inverted
size_t bytes_read;
uint16_t buffer[I2S_DMA_BUF_LEN] = {0};
unsigned long lastTimePrinted;
unsigned long loopTime = 0;
void i2sInit() {
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN),
.sample_rate = I2S_SAMPLE_RATE, // The format of the signal using ADC_BUILT_IN
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // is fixed at 12bit, stereo, MSB
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = I2S_DMA_BUF_LEN,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_adc_mode(ADC_UNIT_1, ADC_INPUT);
i2s_adc_enable(I2S_NUM_0);
adc1_config_channel_atten(ADC_INPUT, ADC_ATTEN_DB_11);
}
void taskCurve (void * parameter) {
/* Konstante Pi definieren */
const double Pi = 3.141592653;
/* Variablen definieren */
double winkel;
double rad;
double sinus;
int i;
static char msg[31] = "";
while (true) {
/* Schleife zur Berechnung der Sinuswerte */
for (i = 0; i <= 36; i++)
{
winkel = 10 * i; /* 10er Schritte berechnen */
rad = winkel * Pi / 180; /* Berechnen des Bogenmaßwinkels */
sinus = sin(rad);
int val = sinus * 255/2 + 128;
analogWrite(outPin, val);
sprintf(msg, "%6g %6.3f %d\n", winkel, sinus, val); /* tabellarische Ausgabe */
Serial.println( msg );
}
}
vTaskDelete( NULL );
}
void setup() {
Serial.begin(115200);
pinMode(outPin, OUTPUT);
//pinMode(adcPin, INPUT);
adcAttachPin(adcPin);
Serial.println("Starting Task 1...");
xTaskCreate(
taskCurve, /* Task function. */
"Sinuskurve", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL); /* Task handle to keep track of created task */
//i2sInit();
}
void loop() {
/*
unsigned long startMicros = ESP.getCycleCount();
i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, 0);
unsigned long stopMicros = ESP.getCycleCount();
loopTime = stopMicros - startMicros;
if (millis() - lastTimePrinted >= 100) {
Serial.println("------------------");
Serial.println(buffer[0] & 0x0FFF);
Serial.println(loopTime);
lastTimePrinted = millis();
}
*/
int val = analogRead(adcPin) *2 -2048;
Serial.println( val );
//delay(10);
}