diff --git a/src/cppmain.cc b/src/cppmain.cc index 2d46368..8d2edd3 100644 --- a/src/cppmain.cc +++ b/src/cppmain.cc @@ -12,6 +12,7 @@ #include "limits" const double pi{ std::acos(-1) }; +#define BUFFER_SIZE 512 namespace Heck { @@ -25,21 +26,9 @@ namespace Heck { } -// DAC -// --- -#define BUFFER_SIZE 128 + // DAC + // --- u32 audio_buffer[BUFFER_SIZE]; - // u32 audio_buffer[BUFFER_SIZE] = { - // 2048, 2149, 2250, 2350, 2450, 2549, 2646, 2742, 2837, 2929, 3020, 3108, 3193, 3275, 3355, - // 3431, 3504, 3574, 3639, 3701, 3759, 3812, 3861, 3906, 3946, 3982, 4013, 4039, 4060, 4076, - // 4087, 4094, 4095, 4091, 4082, 4069, 4050, 4026, 3998, 3965, 3927, 3884, 3837, 3786, 3730, - // 3671, 3607, 3539, 3468, 3394, 3316, 3235, 3151, 3064, 2975, 2883, 2790, 2695, 2598, 2500, - // 2400, 2300, 2199, 2098, 1997, 1896, 1795, 1695, 1595, 1497, 1400, 1305, 1212, 1120, 1031, - // 944, 860, 779, 701, 627, 556, 488, 424, 365, 309, 258, 211, 168, 130, 97, - // 69, 45, 26, 13, 4, 0, 1, 8, 19, 35, 56, 82, 113, 149, 189, - // 234, 283, 336, 394, 456, 521, 591, 664, 740, 820, 902, 987, 1075, 1166, 1258, - // 1353, 1449, 1546, 1645, 1745, 1845, 1946, 2047 - // }; void dac1_set(u32 val) { @@ -72,17 +61,17 @@ namespace Heck { void led_green_toggle() { - HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin); + HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12); } void led_green_on() { - HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET); } void led_green_off() { - HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); } @@ -124,9 +113,9 @@ namespace Heck { return ret; } - void init_buffer_noise() + void buffer_init_noise() { - log("init_buffer_noise()"); + log("buffer_init_noise()"); for (int i = 0; i < BUFFER_SIZE; i++) { u32 val = random(4096); audio_buffer[i] = val; @@ -134,9 +123,9 @@ namespace Heck { } } - void init_buffer_sin() + void buffer_init_sin() { - log("init_buffer_sin()"); + log("buffer_init_sin()"); for (int i = 0; i < BUFFER_SIZE; i++) { float p = float(i) / (float)BUFFER_SIZE - 0.5; u32 val = sin(p) * 4096; @@ -145,19 +134,30 @@ namespace Heck { } } - void noisify_buffer() + void buffer_div(int div) { + log("init_scale()"); + for (int i = 0; i < BUFFER_SIZE; i++) { + u32 val = audio_buffer[i] / div; + audio_buffer[i] = val; + log(std::to_string(val)); + } + } + + void buffer_randomize(int max) + { + log("buffer_randomize()"); u32 buf_index = random(BUFFER_SIZE); - u32 buf_val = random(4096); + u32 buf_val = random(max); audio_buffer[buf_index] = buf_val; log("i:" + std::to_string(buf_index) + " v:" + std::to_string(buf_val)); } void main() { - // while (!is_running) { - // HAL_Delay(1); - // } + while (!is_running) { + HAL_Delay(1); + } log("Starting..."); dac_start_dma(); @@ -167,10 +167,11 @@ namespace Heck { HAL_TIM_Base_Start(&htim6); // init_buffer_noise(); - init_buffer_sin(); + buffer_init_sin(); + buffer_div(10); log("Entering MainLoop..."); while (true) { - noisify_buffer(); + // buffer_randomize(400); HAL_Delay(10); } } @@ -184,7 +185,7 @@ namespace Heck { void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - if (GPIO_Pin == BUT_BLUE_Pin) { + if (GPIO_Pin == GPIO_PIN_0) { Heck::irq1_cb(); } else { __NOP(); // why NOP? i dont know! was from an example diff --git a/src/main.c b/src/main.c index a466e8e..c092d2d 100644 --- a/src/main.c +++ b/src/main.c @@ -137,7 +137,7 @@ void SysTick_Handler(void) void EXTI0_IRQHandler(void) { - HAL_GPIO_EXTI_IRQHandler(BUT_BLUE_GPIO_PIN); + HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); } void DMA1_Stream5_IRQHandler(void) @@ -167,27 +167,27 @@ void OTG_FS_IRQHandler(void) void MX_GPIO_Init(void) { - HAL_GPIO_WritePin(LED_GREEN_GPIO_PORT, LED_GREEN_GPIO_PIN, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); { GPIO_InitTypeDef GPIO_InitStruct; memset(&GPIO_InitStruct, 0, sizeof GPIO_InitStruct); - GPIO_InitStruct.Pin = BUT_BLUE_GPIO_PIN; + GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(BUT_BLUE_GPIO_PORT, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } { GPIO_InitTypeDef GPIO_InitStruct; memset(&GPIO_InitStruct, 0, sizeof GPIO_InitStruct); - GPIO_InitStruct.Pin = LED_GREEN_GPIO_PIN; + GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(LED_GREEN_GPIO_PORT, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); } /* EXTI interrupt init*/ diff --git a/src/main.h b/src/main.h index 5afe9d5..a93e294 100644 --- a/src/main.h +++ b/src/main.h @@ -1,14 +1,9 @@ #ifndef __MAIN_H #define __MAIN_H +#include "stm32f4xx.h" #include "stm32f4xx_hal.h" -#define BUT_BLUE_Pin GPIO_PIN_0 -#define BUT_BLUE_GPIO_Port GPIOA -#define BUT_BLUE_EXTI_IRQn EXTI0_IRQn -#define LED_GREEN_Pin GPIO_PIN_12 -#define LED_GREEN_GPIO_Port GPIOD - #ifdef __cplusplus extern "C" { #endif