ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
main.c File Reference
#include <dirent.h>
#include <math.h>
#include "bsp/esp-bsp.h"
#include "esp_log.h"
#include "esp_dsp.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "esp_err.h"
#include "esp_timer.h"
#include <malloc.h>
Include dependency graph for components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c:

Go to the source code of this file.

Macros

#define I2S_CHANNEL_NUM   (2)
#define SAMPLE_RATE   (10000)
#define BITS_PER_CHANNEL   16
#define BUFFER_PROCESS_SIZE   512
#define X_AXIS_SIZE   (320)
#define Y_AXIS_SIZE   (240)

Functions

static void microphone_read_task (void *arg)
static uint16_t convert_to_rgb (uint8_t minval, uint8_t maxval, int8_t val)
static void spectrum2d_picture_init ()
static void spectrum2d_picture ()
static void image_display_task (void *arg)
void app_main (void)

Variables

static const char * TAG = "main"
static float result_data [512]
static uint8_t screen_rgb_data [(320) *(240) *LV_IMG_PX_SIZE_ALPHA_BYTE]
static const lv_img_dsc_t img_screen_rgb
static int8_t colors [3][3] = { {0, 0, 31}, {0, 63, 0}, {31, 0, 0} }

Macro Definition Documentation

◆ BITS_PER_CHANNEL

#define BITS_PER_CHANNEL   16

◆ BUFFER_PROCESS_SIZE

◆ I2S_CHANNEL_NUM

#define I2S_CHANNEL_NUM   (2)

◆ SAMPLE_RATE

#define SAMPLE_RATE   (10000)

◆ X_AXIS_SIZE

#define X_AXIS_SIZE   (320)

◆ Y_AXIS_SIZE

#define Y_AXIS_SIZE   (240)

Function Documentation

◆ app_main()

void app_main ( void )

Definition at line 247 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

248{
249 /* Initialize I2C (for touch and audio) */
250 bsp_i2c_init();
251
252 /* Initialize display and LVGL */
253 bsp_display_start();
254
255 /* Set display brightness to 100% */
256 bsp_display_backlight_on();
257
258 int ret_val = xTaskCreatePinnedToCore(&microphone_read_task, "Microphone read Task", 8 * 1024, NULL, 3, NULL, 0);
259 if (ret_val != pdPASS) {
260 ESP_LOGE(TAG, "Not possible to allocate microphone task, ret_val = %i", ret_val);
261 return;
262 }
263
264 ret_val = xTaskCreatePinnedToCore(&image_display_task, "Draw task", 10 * 1024, NULL, 5, NULL, 1);
265 if (ret_val != pdPASS) {
266 ESP_LOGE(TAG, "Not possible to allocate microphone task, ret_val= %i", ret_val);
267 return;
268 }
269}
static const char * TAG
Definition main/main.c:31

References bsp_i2c_init(), image_display_task(), microphone_read_task(), and TAG.

Here is the call graph for this function:

◆ convert_to_rgb()

uint16_t convert_to_rgb ( uint8_t minval,
uint8_t maxval,
int8_t val )
static

Definition at line 134 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

135{
136 uint16_t result;
137
138 float i_f = (float)(val - minval) / (float)(maxval - minval) * 2;
139
140 int Ii = i_f;
141 float If = i_f - Ii;
142
143 int8_t *c1 = colors[Ii];
144 int8_t *c2 = colors[Ii + 1];
145 uint16_t res_colors[3];
146
147 res_colors[0] = c1[0] + If * (c2[0] - c1[0]);
148 res_colors[1] = c1[1] + If * (c2[1] - c1[1]);
149 res_colors[2] = c1[2] + If * (c2[2] - c1[2]);
150 result = res_colors[2] | (res_colors[1] << 5) | (res_colors[0] << 11);
151 return result;
152}

References colors.

Referenced by spectrum2d_picture().

Here is the caller graph for this function:

◆ image_display_task()

void image_display_task ( void * arg)
static

Definition at line 229 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

230{
231 // LV_IMG_DECLARE(img_screen_rgb);
232 lv_obj_t *img1 = lv_img_create(lv_scr_act());
233 lv_img_set_src(img1, &img_screen_rgb);
235 lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
236
237 for (;;) {
238 // Update image with new spectrum values
240 // Update screen with new image
241 lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);
242 // Free CPU for a while
243 vTaskDelay(1);
244 }
245}

References img_screen_rgb, spectrum2d_picture(), and spectrum2d_picture_init().

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ microphone_read_task()

void microphone_read_task ( void * arg)
static

Definition at line 39 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

40{
41 esp_codec_dev_handle_t mic_codec_dev = NULL;
42 // Init board microphone
43 mic_codec_dev = bsp_audio_codec_microphone_init();
44 if (mic_codec_dev == NULL) {
45 ESP_LOGE(TAG, "Not possible to initialize microphone!");
46 return;
47 }
48
49 // Init esp-dsp library to use fft functionality
51 if (ret != ESP_OK) {
52 ESP_LOGE(TAG, "Not possible to initialize FFT esp-dsp from library!");
53 return;
54 }
55
56 esp_codec_dev_sample_info_t fs = {
57 .sample_rate = SAMPLE_RATE,
58 .channel = I2S_CHANNEL_NUM,
59 .channel_mask = 0,
60 .bits_per_sample = BITS_PER_CHANNEL,
61 };
62
63 int result = esp_codec_dev_open(mic_codec_dev, &fs);
64 if (result != ESP_OK) {
65 ESP_LOGE(TAG, "Not possible to open microphone!");
66 return;
67 }
68 // Set input microphone gain (from 1 to 100)
69 ESP_LOGI(TAG, "Adjust microphone input volume in the code here...");
70 result |= esp_codec_dev_set_in_gain(mic_codec_dev, 20.0);
71 if (result != ESP_OK) {
72 ESP_LOGE(TAG, "Not possible to set up microphone gain!");
73 return;
74 }
75
76 int audio_chunksize = BUFFER_PROCESS_SIZE;
77
78 // Allocate audio buffer and check for result
79 int16_t *audio_buffer = (int16_t *)memalign(16, (audio_chunksize + 16) * sizeof(int16_t) * I2S_CHANNEL_NUM);
80 // Allocate buffer for window
81 int16_t *wind_buffer = (int16_t *)memalign(16, (audio_chunksize + 16) * sizeof(int16_t) * I2S_CHANNEL_NUM);
82 // Generate window and convert it to int16_t
84 for (int i = 0 ; i < audio_chunksize; i++) {
85 wind_buffer[i * 2 + 0] = (int16_t)(result_data[i] * 32767);
86 wind_buffer[i * 2 + 1] = wind_buffer[i * 2 + 0];
87 }
88
89 while (true) {
90
91 // Read audio data from I2S bus
92 result = esp_codec_dev_read(mic_codec_dev, audio_buffer, audio_chunksize * sizeof(int16_t) * I2S_CHANNEL_NUM);
93 // Multiply input stream with window coefficients
94 dsps_mul_s16_ansi(audio_buffer, wind_buffer, audio_buffer, audio_chunksize * 2, 1, 1, 1, 15);
95
96 // Call FFT bit reverse
97 dsps_fft2r_sc16_ae32(audio_buffer, audio_chunksize);
98 dsps_bit_rev_sc16_ansi(audio_buffer, audio_chunksize);
99 // Convert spectrum from two input channels to two
100 // spectrums for two channels.
101 dsps_cplx2reC_sc16(audio_buffer, audio_chunksize);
102
103 // The output data array presented as moving average for input in dB
104 for (int i = 0 ; i < audio_chunksize ; i++) {
105 float spectrum_sqr = audio_buffer[i * 2 + 0] * audio_buffer[i * 2 + 0] + audio_buffer[i * 2 + 1] * audio_buffer[i * 2 + 1];
106 float spectrum_dB = 10 * log10f(0.1 + spectrum_sqr);
107 // Multiply with sime coefficient for better view data on screen
108 spectrum_dB = 4 * spectrum_dB;
109 // Apply moving average of spectrum
110 result_data[i] = 0.8 * result_data[i] + 0.2 * spectrum_dB;
111 }
112 vTaskDelay(10);
113 }
114}
#define memalign(align_, size_)
Definition dsp_tests.h:35
#define dsps_fft2r_sc16_ae32(data, N)
Definition dsps_fft2r.h:111
#define CONFIG_DSP_MAX_FFT_SIZE
Definition dsps_fft2r.h:24
esp_err_t dsps_fft2r_init_sc16(int16_t *fft_table_buff, int table_size)
esp_err_t dsps_bit_rev_sc16_ansi(int16_t *data, int N)
esp_err_t dsps_cplx2reC_sc16(int16_t *data, int N)
esp_err_t dsps_mul_s16_ansi(const int16_t *input1, const int16_t *input2, int16_t *output, int len, int step1, int step2, int step_out, int shift)
Multiply two arrays.
void dsps_wind_blackman_harris_f32(float *window, int len)
Blackman-Harris window.
int esp_err_t
Definition esp_err.h:21
#define ESP_OK
Definition esp_err.h:23

References BITS_PER_CHANNEL, BUFFER_PROCESS_SIZE, CONFIG_DSP_MAX_FFT_SIZE, dsps_bit_rev_sc16_ansi(), dsps_cplx2reC_sc16(), dsps_fft2r_init_sc16(), dsps_fft2r_sc16_ae32, dsps_mul_s16_ansi(), dsps_wind_blackman_harris_f32(), ESP_OK, I2S_CHANNEL_NUM, memalign, result_data, SAMPLE_RATE, and TAG.

Referenced by app_main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spectrum2d_picture()

void spectrum2d_picture ( )
static

Definition at line 167 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

168{
169 for (int y = 0 ; y < (img_screen_rgb.header.h - 1) ; y++) {
170 for (int x = 0 ; x < img_screen_rgb.header.w ; x++) {
171 for (int i = 0 ; i < LV_IMG_PX_SIZE_ALPHA_BYTE ; i++) {
172 screen_rgb_data[(y * img_screen_rgb.header.w + x)*LV_IMG_PX_SIZE_ALPHA_BYTE + i] = screen_rgb_data[((y + 1) * img_screen_rgb.header.w + x) * LV_IMG_PX_SIZE_ALPHA_BYTE + i];
173 }
174 }
175 }
176
177 // Add left channel to the screen
178 // The order of the values inverted
179 for (int x = 0 ; x < img_screen_rgb.header.w / 2 ; x++) {
180 // Get inverted index value
181 int in_index = img_screen_rgb.header.w / 2 - x - 1;
182 float data = result_data[in_index];
183
184 // Limit input data
185 if (data > 127) {
186 data = 127;
187 }
188 if (data < 0) {
189 data = 0;
190 }
191
192 // Convert input value in dB to the color
193 uint16_t color_val = convert_to_rgb(0, 128, data);
194 // Split 16 bit value to two bytes, to change the bytes order
195 uint8_t *ref_val = (uint8_t *)&color_val;
196 int out_index = x;
197 screen_rgb_data[((img_screen_rgb.header.h - 1)*img_screen_rgb.header.w + out_index)*LV_IMG_PX_SIZE_ALPHA_BYTE + 0] = ref_val[1];
198 screen_rgb_data[((img_screen_rgb.header.h - 1)*img_screen_rgb.header.w + out_index)*LV_IMG_PX_SIZE_ALPHA_BYTE + 1] = ref_val[0];
199 // Set alpha value
200 screen_rgb_data[((img_screen_rgb.header.h - 1)*img_screen_rgb.header.w + out_index)*LV_IMG_PX_SIZE_ALPHA_BYTE + 2] = 0xff;
201 }
202
203 // Add right channel to the screen
204 for (int x = 0 ; x < img_screen_rgb.header.w / 2 ; x++) {
205 // Get index of right channel
206 int in_index = BUFFER_PROCESS_SIZE / 2 + x;
207 float data = result_data[in_index];
208
209 // Limit input data
210 if (data > 127) {
211 data = 127;
212 }
213 if (data < 0) {
214 data = 0;
215 }
216
217 // Convert input value in dB to the color
218 uint16_t color_val = convert_to_rgb(0, 128, data);
219 // Split 16 bit value to two bytes, to change the bytes order
220 uint8_t *ref_val = (uint8_t *)&color_val;
221 int out_index = img_screen_rgb.header.w / 2 + x;
222 screen_rgb_data[((img_screen_rgb.header.h - 1)*img_screen_rgb.header.w + out_index)*LV_IMG_PX_SIZE_ALPHA_BYTE + 0] = ref_val[1];
223 screen_rgb_data[((img_screen_rgb.header.h - 1)*img_screen_rgb.header.w + out_index)*LV_IMG_PX_SIZE_ALPHA_BYTE + 1] = ref_val[0];
224 // Set alpha value
225 screen_rgb_data[((img_screen_rgb.header.h - 1)*img_screen_rgb.header.w + out_index)*LV_IMG_PX_SIZE_ALPHA_BYTE + 2] = 0xff;
226 }
227}
static uint8_t screen_rgb_data[(320) *(240) *LV_IMG_PX_SIZE_ALPHA_BYTE]
static uint16_t convert_to_rgb(uint8_t minval, uint8_t maxval, int8_t val)
static float data[128 *2]
Definition test_fft2r.c:34
float y[1024]
Definition test_fir.c:11
float x[1024]
Definition test_fir.c:10

References BUFFER_PROCESS_SIZE, convert_to_rgb(), data, img_screen_rgb, result_data, screen_rgb_data, x, and y.

Referenced by image_display_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spectrum2d_picture_init()

void spectrum2d_picture_init ( )
static

Definition at line 155 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

156{
157 for (int y = 0 ; y < img_screen_rgb.header.h ; y++) {
158 for (int x = 0 ; x < img_screen_rgb.header.w ; x++) {
159 screen_rgb_data[(y * img_screen_rgb.header.w + x)*LV_IMG_PX_SIZE_ALPHA_BYTE + 0] = 0x0;
160 screen_rgb_data[(y * img_screen_rgb.header.w + x)*LV_IMG_PX_SIZE_ALPHA_BYTE + 1] = 0x1f;
161 screen_rgb_data[(y * img_screen_rgb.header.w + x)*LV_IMG_PX_SIZE_ALPHA_BYTE + 2] = 0xff;
162 }
163 }
164}

References img_screen_rgb, screen_rgb_data, x, and y.

Referenced by image_display_task().

Here is the caller graph for this function:

Variable Documentation

◆ colors

int8_t colors[3][3] = { {0, 0, 31}, {0, 63, 0}, {31, 0, 0} }
static

Definition at line 133 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

133{ {0, 0, 31}, {0, 63, 0}, {31, 0, 0} };

Referenced by convert_to_rgb(), and convert_to_rgb().

◆ img_screen_rgb

const lv_img_dsc_t img_screen_rgb
static
Initial value:
= {
.header.always_zero = 0,
.header.w = (320) ,
.header.h = (240) ,
.data_size = (320) * (240) * LV_IMG_PX_SIZE_ALPHA_BYTE,
.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA,
.data = screen_rgb_data,
}

Definition at line 123 of file components/espressif__esp-dsp/applications/spectrum_box_lite/main/main.c.

123 {
124 .header.always_zero = 0,
125 .header.w = X_AXIS_SIZE,
126 .header.h = Y_AXIS_SIZE,
127 .data_size = X_AXIS_SIZE * Y_AXIS_SIZE * LV_IMG_PX_SIZE_ALPHA_BYTE,
128 .header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA,
129 .data = screen_rgb_data,
130};

Referenced by image_display_task(), image_display_task(), spectrum2d_picture(), spectrum2d_picture(), spectrum2d_picture_init(), and spectrum2d_picture_init().

◆ result_data

◆ screen_rgb_data

uint8_t screen_rgb_data[(320) *(240) *LV_IMG_PX_SIZE_ALPHA_BYTE]
static

◆ TAG

const char* TAG = "main"
static