From 122230464794375476a1a5a10884c2c649c8cb6f Mon Sep 17 00:00:00 2001 From: ShallowGreen123 <2608653986@qq.com> Date: Wed, 11 Sep 2024 10:52:36 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9ANFC=20exception?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/factory_test/factory_test.ino | 27 +-- examples/factory_test/peripheral/peri_mic.cpp | 156 +----------------- examples/factory_test/peripheral/peripheral.h | 10 +- examples/factory_test/ui.cpp | 89 ++++------ examples/factory_test/ui_interface.cpp | 6 +- 5 files changed, 56 insertions(+), 232 deletions(-) diff --git a/examples/factory_test/factory_test.ino b/examples/factory_test/factory_test.ino index 893d22a..69dc477 100644 --- a/examples/factory_test/factory_test.ino +++ b/examples/factory_test/factory_test.ino @@ -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"; @@ -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 *********************************************************************************/ @@ -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) @@ -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); @@ -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); @@ -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: "); @@ -336,6 +338,8 @@ void setup(void) assert(radioLock); xSemaphoreGive(radioLock); + init_microphone(); + // iic scan byte error, address; int nDevices = 0; @@ -405,8 +409,6 @@ void setup(void) ws2812_init(); - mic_init(); - // infared_init(); multi_thread_create(); @@ -437,10 +439,6 @@ 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); @@ -448,5 +446,14 @@ void loop(void) 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); } diff --git a/examples/factory_test/peripheral/peri_mic.cpp b/examples/factory_test/peripheral/peri_mic.cpp index 8a18fc5..e085be7 100644 --- a/examples/factory_test/peripheral/peri_mic.cpp +++ b/examples/factory_test/peripheral/peri_mic.cpp @@ -1,160 +1,13 @@ -#include "peripheral.h" -#include + #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), @@ -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) ); -} - +} \ No newline at end of file diff --git a/examples/factory_test/peripheral/peripheral.h b/examples/factory_test/peripheral/peripheral.h index 2351fc5..ee62010 100644 --- a/examples/factory_test/peripheral/peripheral.h +++ b/examples/factory_test/peripheral/peripheral.h @@ -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 diff --git a/examples/factory_test/ui.cpp b/examples/factory_test/ui.cpp index 4010736..2f94ff9 100644 --- a/examples/factory_test/ui.cpp +++ b/examples/factory_test/ui.cpp @@ -1150,7 +1150,7 @@ void setting_scr_event(lv_event_t *e) case 4: {// "About System" char buf[128]; lv_snprintf(buf, 128, "LORA init --- %s\n" - "PMU init ---- %s\n" + "NFC init ---- %s\n" "TF card ----- %s", (ui_scr4_get_lora_st() ? "PASS" : "FAIL"), (ui_scr4_get_nfc_st() ? "PASS" : "FAIL"), @@ -1944,39 +1944,10 @@ scr_lifecycle_t screen7_1 = { // --------------------- screen 7.2 --------------------- MIC #if 1 lv_obj_t *scr7_2_cont; -lv_obj_t *mic_btn; - -extern int recode_cnt; void entry7_2_anim(lv_obj_t *obj) { entry1_anim(obj); } void exit7_2_anim(int user_data, lv_obj_t *obj) { exit1_anim(user_data, obj); } -void mic_recode_end_event(lv_timer_t *t) -{ - if(mic_recode_st() == false) { - lv_timer_del(t); - prompt_info("end recording! ", 2000); - } -} - -void mic_start_recode_event(lv_event_t *e) -{ - char buf[64]; - if(e->code == LV_EVENT_CLICKED) { - Serial.println("click btn"); - if(sd_is_valid()) { - Serial.println("open file for writing"); - lv_memset_00(buf, 64); - lv_snprintf(buf, 64, "Start 10s recording, save to 'record%02d.wav'.", recode_cnt); - prompt_info(buf, 1000); - mic_recode_start(10); - lv_timer_create(mic_recode_end_event, 100, NULL); - } else { - prompt_info("No fond SD card!", 1000); - } - } -} - static void scr7_2_btn_event_cb(lv_event_t * e) { if(e->code == LV_EVENT_CLICKED){ @@ -1999,36 +1970,6 @@ void create7_2(lv_obj_t *parent) lv_label_set_text(label, "Microphone"); lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 10); - lv_obj_t *mic_info = lv_label_create(scr7_2_cont); - lv_obj_set_style_text_color(mic_info, lv_color_hex(EMBED_COLOR_TEXT), LV_PART_MAIN); - lv_obj_set_style_text_font(mic_info, FONT_BOLD_14, LV_PART_MAIN); - lv_obj_set_width(mic_info, DISPALY_WIDTH); - lv_label_set_long_mode(mic_info, LV_LABEL_LONG_WRAP); - lv_obj_set_style_radius(mic_info, 5, LV_PART_MAIN); - // lv_obj_set_style_bg_opa(mic_info, LV_OPA_COVER, LV_PART_MAIN); - lv_obj_set_style_pad_hor(mic_info, 3, LV_PART_MAIN); - lv_obj_set_style_text_align(mic_info, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN); - lv_obj_align(mic_info, LV_ALIGN_TOP_MID, 0, 40); - lv_label_set_text(mic_info, "Press \'start\' to record the audio of 10s. No operation " - "can be performed during the recording, and the recording is saved to the SD card."); - - mic_btn = lv_btn_create(scr7_2_cont); - lv_obj_set_size(mic_btn, 100, 40); - lv_obj_set_style_border_width(mic_btn, 0, LV_PART_MAIN); - lv_obj_set_style_shadow_width(mic_btn, 0, LV_PART_MAIN); - lv_obj_remove_style(mic_btn, NULL, LV_STATE_FOCUS_KEY); - lv_obj_set_style_outline_pad(mic_btn, 2, LV_STATE_FOCUS_KEY); - lv_obj_set_style_outline_width(mic_btn, 2, LV_STATE_FOCUS_KEY); - lv_obj_set_style_outline_color(mic_btn, lv_color_hex(EMBED_COLOR_FOCUS_ON), LV_STATE_FOCUS_KEY); - lv_obj_align(mic_btn, LV_ALIGN_BOTTOM_MID, 0 , -15); - lv_obj_t *mic_lab = lv_label_create(mic_btn); - lv_obj_center(mic_lab); - lv_obj_set_style_text_color(mic_lab, lv_color_hex(EMBED_COLOR_TEXT), LV_PART_MAIN); - lv_obj_set_style_text_font(mic_lab, FONT_BOLD_14, LV_PART_MAIN); - lv_label_set_text(mic_lab, "start"); - // lv_obj_add_flag(mic_btn, LV_OBJ_FLAG_CHECKABLE); - lv_obj_add_event_cb(mic_btn, mic_start_recode_event, LV_EVENT_CLICKED, NULL); - // back bottom scr_back_btn_create(scr7_2_cont, scr7_2_btn_event_cb); } @@ -2174,7 +2115,9 @@ static lv_obj_t *music_lab; static lv_obj_t *pause_btn; static lv_obj_t *next_btn; static lv_obj_t *prev_btn; +static lv_obj_t *mic_led; static bool music_is_running = false; +static lv_timer_t *mic_chk_timer = NULL; int music_idx = 0; char *music_list[20] = {0}; @@ -2188,6 +2131,19 @@ void exit8_anim(int user_data, lv_obj_t *obj) exit1_anim(user_data, obj); } +extern int i2s_mic_cnt; +void mic_chk_timer_event(lv_timer_t *t) +{ + if(i2s_mic_cnt > 3) + { + i2s_mic_cnt = 0; + lv_led_on(mic_led); + } else + { + lv_led_off(mic_led); + } +} + void music_player_event(lv_event_t * e) { char buf[64]; @@ -2257,6 +2213,11 @@ static void create8(lv_obj_t *parent) { lv_obj_set_style_border_width(scr8_cont, 0, LV_PART_MAIN); lv_obj_set_style_pad_all(scr8_cont, 0, LV_PART_MAIN); + mic_led = lv_led_create(scr8_cont); + lv_obj_set_size(mic_led, 16, 16); + lv_obj_align(mic_led, LV_ALIGN_TOP_RIGHT, -10, 10); + lv_led_off(mic_led); + lv_obj_t *label = lv_label_create(scr8_cont); lv_obj_set_style_text_color(label, lv_color_hex(EMBED_COLOR_TEXT), LV_PART_MAIN); lv_obj_set_style_text_font(label, &Font_Mono_Bold_18, LV_PART_MAIN); @@ -2297,8 +2258,14 @@ static void create8(lv_obj_t *parent) { } static void entry8(void) { music_is_running = false; + mic_chk_timer = lv_timer_create(mic_chk_timer_event, 10, NULL); +} +static void exit8(void) { + if(mic_chk_timer) { + lv_timer_del(mic_chk_timer); + mic_chk_timer = NULL; + } } -static void exit8(void) { } static void destroy8(void) { } diff --git a/examples/factory_test/ui_interface.cpp b/examples/factory_test/ui_interface.cpp index c60fab5..3f5c7a4 100644 --- a/examples/factory_test/ui_interface.cpp +++ b/examples/factory_test/ui_interface.cpp @@ -46,9 +46,9 @@ void ui_scr1_set_mode(int mode) { bool ui_scr4_get_lora_st(void) { return lora_is_init(); } -// bool ui_scr4_get_pmu_st(void) { - -// } +bool ui_scr4_get_pmu_st(void) { + +} bool ui_scr4_get_nfc_st(void) { return nfc_is_init(); }