Created
March 18, 2018 23:52
-
-
Save TareObjects/b31dee88b1f65ba9c56ad374497a9e1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Sound | |
// | |
#include "driver/i2s.h" | |
#include "driver/gpio.h" | |
#include "ching.h" | |
#include "clap.h" | |
#include "cymbal.h" | |
#include "voice.h" | |
const i2s_port_t I2S_NUM = (i2s_port_t)0; | |
const i2s_bits_per_sample_t BITS_PER_SAMPLE = (i2s_bits_per_sample_t)16; | |
#define ADC_MODE 1 | |
// I2S | |
void set_16k_16() { | |
i2s_config_t i2s_config; | |
i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX); // Only TX | |
i2s_config.sample_rate = 16000; | |
i2s_config.bits_per_sample = BITS_PER_SAMPLE; | |
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT; //right channels | |
i2s_config.communication_format = I2S_COMM_FORMAT_PCM; | |
i2s_config.dma_buf_count = 4; | |
i2s_config.dma_buf_len = 1024; | |
i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; //Interrupt level 1 | |
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL); | |
} | |
// | |
// 加速度 | |
// | |
#include <Wire.h> // Must include Wire library for I2C | |
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library | |
MMA8452Q accel(0x1C); | |
volatile int soundOn = false; | |
volatile int iSound = 0; | |
volatile int prevSound = -1; | |
void sound_loop(void *param) { | |
char *buf = NULL; | |
int length; | |
while (1) { | |
if (soundOn == true) { | |
switch (iSound) { | |
case 0: buf = (char*)ching; length = sizeof(ching); break; | |
case 1: buf = (char*)clap; length = sizeof(clap); break; | |
case 2: buf = (char*)cymbal; length = sizeof(cymbal); break; | |
case 3: buf = (char*)voice; length = sizeof(voice); break; | |
default: buf = NULL; iSound = 0; break; | |
} | |
soundOn = false; | |
prevSound = iSound; | |
int i = 0; | |
while (i < length) { | |
int ret = i2s_push_sample(I2S_NUM, &buf[i], portMAX_DELAY); | |
if (ret <= 0) { | |
Serial.print("return = "); | |
Serial.print(ret); | |
delay(1); | |
iSound = -1; | |
break; | |
} else { | |
i += ret; | |
} | |
if (prevSound != iSound || soundOn == true) { | |
break; | |
} | |
} | |
i2s_zero_dma_buffer(I2S_NUM); | |
} else { | |
delay(1); | |
} | |
} | |
} | |
TaskHandle_t task; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("MMA8452Q Test Code!"); | |
pinMode(13, OUTPUT); | |
accel.init(); | |
static i2s_pin_config_t pin_config; | |
pin_config.bck_io_num = 26; | |
pin_config.ws_io_num = 25; | |
pin_config.data_out_num = 27; | |
pin_config.data_in_num = -1; //Not used | |
set_16k_16(); | |
i2s_set_pin(I2S_NUM, &pin_config); | |
xTaskCreatePinnedToCore( | |
sound_loop, // Handler | |
"sound_loop", // Task name | |
4096, // Stack size | |
NULL, // Parameter | |
1, // Priority : 25 is the most high | |
&task, // task handler | |
1); // Application CPU | |
Serial.println("setup end"); | |
} | |
float ave = -1; | |
void loop() | |
{ | |
if (accel.available()) | |
{ | |
// First, use accel.read() to read the new variables: | |
accel.read(); | |
float gravity = sqrt(accel.x * accel.x + accel.y * accel.y + accel.z * accel.z); | |
if (ave == -1) { | |
ave = gravity; | |
} else { | |
if (abs(gravity - ave) > 1000) { | |
digitalWrite(13, HIGH); | |
soundOn = true; | |
delay(25); | |
digitalWrite(13, LOW); | |
} else { | |
digitalWrite(13, LOW); | |
} | |
ave = (ave * 9.0 + gravity) / 10.0; | |
} | |
Serial.print("gravity="); | |
Serial.print(gravity, 3); | |
Serial.print(", ave="); | |
Serial.print(ave, 3); | |
Serial.print(", abs="); | |
Serial.print(abs(gravity - ave)); | |
Serial.print(" "); | |
printCalculatedAccels(); | |
Serial.println(); // Print new line every time. | |
delay(1); | |
} else { | |
Serial.println("not available"); | |
delay(1000); | |
} | |
} | |
void printCalculatedAccels() | |
{ | |
Serial.print(accel.cx, 3); | |
Serial.print("\t"); | |
Serial.print(accel.cy, 3); | |
Serial.print("\t"); | |
Serial.print(accel.cz, 3); | |
Serial.print("\t"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment