Skip to content

Commit

Permalink
fix:NFC exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ShallowGreen123 committed Sep 11, 2024
1 parent b6dfba4 commit 1222304
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 232 deletions.
27 changes: 17 additions & 10 deletions examples/factory_test/factory_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ TaskHandle_t lora_handle;
TaskHandle_t ws2812_handle;
TaskHandle_t battery_handle;
// TaskHandle_t infared_handle;
TaskHandle_t mic_handle;

// wifi
// char wifi_ssid[WIFI_SSID_MAX_LEN] = "xinyuandianzi";
Expand All @@ -66,6 +65,11 @@ static uint32_t last_tick;
uint8_t eeprom_ssid[WIFI_SSID_MAX_LEN];
uint8_t eeprom_pswd[WIFI_PSWD_MAX_LEN];

// mic
int16_t i2s_readraw_buff[SAMPLE_SIZE] = {0};
size_t bytes_read = 0;
int i2s_mic_cnt = 0;

/*********************************************************************************
* FUNCTION
*********************************************************************************/
Expand Down Expand Up @@ -179,7 +183,6 @@ void multi_thread_create(void)
xTaskCreate(battery_task, "battery_task", 1024 * 2, NULL, BATTERY_PRIORITY, &battery_handle);
// xTaskCreate(infared_task, "infared_task", 1024 * 2, NULL, INFARED_PRIORITY, &infared_handle);

// xTaskCreate(mic_task, "mic_task", 1024 * 4, NULL, tskIDLE_PRIORITY + 2, &mic_handle);
}

void wifi_init(void)
Expand Down Expand Up @@ -226,7 +229,6 @@ static void msg_send_event(lv_timer_t *t)
}
}


static void msg_subsribe_event(void * s, lv_msg_t * msg)
{
LV_UNUSED(s);
Expand All @@ -245,7 +247,6 @@ static void msg_subsribe_event(void * s, lv_msg_t * msg)
}
}


void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing spiffs directory: %s\n", dirname);

Expand Down Expand Up @@ -324,6 +325,7 @@ void setup(void)
pinMode(ENCODER_KEY, INPUT);
pinMode(BOARD_USER_KEY, INPUT);

pinMode(BOARD_PN532_IRQ, OPEN_DRAIN);

Serial.begin(115200);
Serial.print("setup() running core ID: ");
Expand All @@ -336,6 +338,8 @@ void setup(void)
assert(radioLock);
xSemaphoreGive(radioLock);

init_microphone();

// iic scan
byte error, address;
int nDevices = 0;
Expand Down Expand Up @@ -405,8 +409,6 @@ void setup(void)

ws2812_init();

mic_init();

// infared_init();

multi_thread_create();
Expand Down Expand Up @@ -437,16 +439,21 @@ void loop(void)

audio.loop();

if(mic_recode_st()) {
record_wav(10);
}

if (irrecv.decode(&results)) {
// print() & println() can't handle printing long longs. (uint64_t)
serialPrintUint64(results.value, HEX);
IR_recv_value = results.value;
Serial.println("");
irrecv.resume(); // Receive the next value
}

i2s_read((i2s_port_t)EXAMPLE_I2S_CH, (char *)i2s_readraw_buff, SAMPLE_SIZE, &bytes_read, 100);
for(int i = 0; i < 10; i++) {
// Serial.printf("%d ", i2s_readraw_buff[i]);
// if(i == 9) {
// Serial.println(" ");
// }
if(i2s_readraw_buff[i] > 0) i2s_mic_cnt++;
}
delay(1);
}
156 changes: 4 additions & 152 deletions examples/factory_test/peripheral/peri_mic.cpp
Original file line number Diff line number Diff line change
@@ -1,160 +1,13 @@
#include "peripheral.h"
#include <sys/stat.h>

#include "driver/i2s.h"
#include "utilities.h"
#include "esp_err.h"
#include "peripheral.h"

#define EXAMPLE_REC_TIME 15 // Recording time
#define EXAMPLE_I2S_CH 0 // I2S Channel Number
#define EXAMPLE_SAMPLE_RATE 44100 // Audio Sample Rate 44.1KHz
#define EXAMPLE_BIT_SAMPLE 16 // Audio Bit Sample

#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
#define NUM_CHANNELS (1) // For mono recording only!
#define SAMPLE_SIZE (EXAMPLE_BIT_SAMPLE * 1024)
#define BYTE_RATE (EXAMPLE_SAMPLE_RATE * (EXAMPLE_BIT_SAMPLE / 8)) * NUM_CHANNELS

static int16_t i2s_readraw_buff[SAMPLE_SIZE];
size_t bytes_read;
const int WAVE_HEADER_SIZE = 44;

bool recode_start_flag = false;
int recode_cnt = 0;

static portMUX_TYPE mic_spinlock = portMUX_INITIALIZER_UNLOCKED;

void generate_wav_header(char* wav_header, uint32_t wav_size, uint32_t sample_rate)
{
// See this for reference: http://soundfile.sapp.org/doc/WaveFormat/
uint32_t file_size = wav_size + WAVE_HEADER_SIZE - 8;
uint32_t byte_rate = BYTE_RATE;

const char set_wav_header[] = {
'R','I','F','F', // ChunkID
(char)file_size, (char)(file_size >> 8), (char)(file_size >> 16), (char)(file_size >> 24), // ChunkSize
'W','A','V','E', // Format
'f','m','t',' ', // Subchunk1ID
0x10, 0x00, 0x00, 0x00, // Subchunk1Size (16 for PCM)
0x01, 0x00, // AudioFormat (1 for PCM)
0x01, 0x00, // NumChannels (1 channel)
(char)sample_rate, (char)(sample_rate >> 8), (char)(sample_rate >> 16), (char)(sample_rate >> 24), // SampleRate
(char)byte_rate, (char)(byte_rate >> 8), (char)(byte_rate >> 16), (char)(byte_rate >> 24), // ByteRate
0x02, 0x00, // BlockAlign
0x10, 0x00, // BitsPerSample (16 bits)
'd','a','t','a', // Subchunk2ID
(char)wav_size, (char)(wav_size >> 8), (char)(wav_size >> 16), (char)(wav_size >> 24), // Subchunk2Size
};

memcpy(wav_header, set_wav_header, sizeof(set_wav_header));
}

void record_wav(uint32_t rec_time)
{
// Use POSIX and C standard library functions to work with files.
int flash_wr_size = 0;
Serial.println("Opening File");

char wav_header_fmt[WAVE_HEADER_SIZE];

uint32_t flash_rec_time = BYTE_RATE * rec_time;
generate_wav_header(wav_header_fmt, flash_rec_time, EXAMPLE_SAMPLE_RATE);

// Create new WAV file
char buf[32];
snprintf(buf, 32, "/record%02d.wav", recode_cnt);
recode_cnt++;

File file = SD.open(buf, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
file.close();
mic_recode_stop();
return;
}

file.write((uint8_t *)wav_header_fmt, WAVE_HEADER_SIZE);

// Start recording
while(flash_wr_size < flash_rec_time) {
// Read the RAW samples from the microphone
i2s_read((i2s_port_t)EXAMPLE_I2S_CH, (char *)i2s_readraw_buff, SAMPLE_SIZE, &bytes_read, 100);
// Write the samples to the WAV file
file.write((uint8_t *)i2s_readraw_buff, bytes_read);
flash_wr_size += bytes_read;
}
Serial.println("Recording done!");
file.close();
Serial.println("File written on SDCard");

mic_recode_stop();
}



uint32_t flash_rec_time;
File recode_file;
void mic_recode_start(uint32_t rec_time)
{
recode_start_flag = true;
// char wav_header_fmt[WAVE_HEADER_SIZE];
// flash_rec_time = BYTE_RATE * rec_time;
// generate_wav_header(wav_header_fmt, flash_rec_time, EXAMPLE_SAMPLE_RATE);

// // Create new WAV file
// char buf[32]={0};
// snprintf(buf, 32, "/record%02d.wav", recode_cnt);
// recode_cnt++;
// Serial.printf("file: %s\n", buf);

// File file = SD.open(buf, FILE_WRITE);
// if(!file){
// Serial.println("Failed to open file for writing");
// return;
// } else {
// Serial.printf("Open file: %s\n", buf);
// }

// file.write((uint8_t *)wav_header_fmt, WAVE_HEADER_SIZE);

}

void mic_task(void *param)
{
int flash_wr_size = 0;

while(1) {
taskENTER_CRITICAL(&mic_spinlock);
// record
if(recode_start_flag) {
if(flash_wr_size < flash_rec_time) {
// Read the RAW samples from the microphone
i2s_read((i2s_port_t)EXAMPLE_I2S_CH, (char *)i2s_readraw_buff, SAMPLE_SIZE, &bytes_read, 100);
// Write the samples to the WAV file
recode_file.write((uint8_t *)i2s_readraw_buff, bytes_read);
flash_wr_size += bytes_read;
} else {
mic_recode_stop();
}
}
taskEXIT_CRITICAL(&mic_spinlock);
delay(1);
}
}

void mic_recode_stop(void)
{
recode_start_flag = false;
// Serial.println("Recording done!");
// recode_file.close();
// Serial.println("File written on SDCard");
}

bool mic_recode_st(void)
{
return recode_start_flag;
}

void mic_init(void)
void init_microphone(void)
{
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
Expand All @@ -181,5 +34,4 @@ void mic_init(void)
ESP_ERROR_CHECK( i2s_driver_install((i2s_port_t )EXAMPLE_I2S_CH, &i2s_config, 0, NULL) );
ESP_ERROR_CHECK( i2s_set_pin((i2s_port_t)EXAMPLE_I2S_CH, &pin_config) );
ESP_ERROR_CHECK( i2s_set_clk((i2s_port_t )EXAMPLE_I2S_CH, EXAMPLE_SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO) );
}

}
10 changes: 4 additions & 6 deletions examples/factory_test/peripheral/peripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ void battery_task(void *param);
extern BQ27220 bq27220;

/**------------------------------ MIC ------------------------------------**/
void mic_init(void);
void mic_task(void *param);
void mic_recode_start(uint32_t rec_time);
void mic_recode_stop(void);
bool mic_recode_st(void);
void record_wav(uint32_t rec_time);
#define SAMPLE_SIZE (20)
#define EXAMPLE_I2S_CH 0 // I2S Channel Number

void init_microphone(void);

/**------------------------------- IR ------------------------------------**/
#define IR_MODE_SEND 1
Expand Down
Loading

0 comments on commit 1222304

Please sign in to comment.