Created
September 12, 2019 17:28
-
-
Save hughesjs/f37475ecd85786126c9c1af5687f80ab to your computer and use it in GitHub Desktop.
BT Speaker Control - Main Loop
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
int main(void) | |
{ | |
HAL_Init(); | |
SystemClock_Config(); | |
MX_GPIO_Init(); | |
MX_TIM10_Init(); | |
MX_TIM11_Init(); | |
MX_USART6_UART_Init(); | |
MX_ADC1_Init(); | |
//HAL_TIM_Base_Start_IT(&htim10); | |
init_goertzel(); | |
unsigned char display [256][3]; | |
disable_interrupts(); | |
init_neopixel_gpio(display); | |
enable_interrupts(); | |
adc_finish_collect = 0; | |
adc_sample_count = 0; | |
float val = 0; | |
double test_var = 0; | |
while (1) | |
{ | |
HAL_UART_Transmit(&huart6, "\0", sizeof(unsigned char), 10); | |
//Read ADC values for set number of samples | |
HAL_TIM_Base_Start_IT(&htim11); | |
while (!adc_finish_collect){}; | |
HAL_TIM_Base_Stop_IT(&htim11); | |
adc_finish_collect = 0; | |
HAL_UART_Transmit(&huart6, "\0", sizeof(unsigned char), 10); | |
//Do scaling... | |
for (int i = 0; i < NUMBER_OF_SAMPLES; i++){ | |
val = (float) adc_vals[i]; | |
val = (val-115)/115; | |
scaled_vals[i] = val; | |
} | |
//Get volume scalar | |
float accumulator = 0; | |
for (int i = 0; i < NUMBER_OF_SAMPLES; i++) | |
{ | |
float currentVal = scaled_vals[i]; | |
if (currentVal < 0) | |
{ | |
accumulator += -currentVal; | |
} | |
else | |
{ | |
accumulator += currentVal; | |
} | |
} | |
float scalar = 25 * accumulator / NUMBER_OF_SAMPLES; | |
//Get goertzel values | |
perform_dft_process(mag_list, scaled_vals); | |
//Do scaling for neopixels | |
float smallest = mag_list[0]; | |
float largest = mag_list[0]; | |
float current_magnitude = 0; | |
for (int i = 1; i < NO_OF_FREQUENCY_BINS; i++){ | |
current_magnitude = mag_list[i]; | |
if (current_magnitude > largest){ | |
largest = current_magnitude; | |
} else if (current_magnitude < smallest){ | |
smallest = current_magnitude; | |
} | |
} | |
float height_ratio = 0; | |
height_ratio = (largest - smallest) / 16; | |
for (int i = 0; i < 16; i++){ | |
current_magnitude = round(mag_list[i] / height_ratio); | |
if (current_magnitude > 16){ | |
current_magnitude = 16; | |
} | |
scaled_magnitudes[i] = current_magnitude; | |
//column_heights[i] = (unsigned char) (current_magnitude * scalar); | |
if (scalar < 0.45) | |
{ | |
column_heights[i] = 0; | |
} | |
else | |
{ | |
column_heights[i] = (unsigned char) (current_magnitude * scalar); | |
} | |
} | |
calculate_column_array(display, column_heights); | |
disable_interrupts(); | |
send_array(display); | |
enable_interrupts(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment