Skip to content

Commit

Permalink
✨ (dac): Add CoreDAC spike
Browse files Browse the repository at this point in the history
Co-Authored-By: Maxime Blanc <[email protected]>
Co-Authored-By: SamHadjes <[email protected]>
  • Loading branch information
3 people committed Mar 4, 2024
1 parent 1bf26bd commit fef78bf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spikes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_subdirectory(${SPIKES_DIR}/lk_command_kit)
add_subdirectory(${SPIKES_DIR}/lk_config_kit)
add_subdirectory(${SPIKES_DIR}/lk_coreled)
add_subdirectory(${SPIKES_DIR}/lk_core_touch_sensor)
add_subdirectory(${SPIKES_DIR}/lk_dac)
add_subdirectory(${SPIKES_DIR}/lk_event_queue)
add_subdirectory(${SPIKES_DIR}/lk_file_reception)
add_subdirectory(${SPIKES_DIR}/lk_file_manager_kit)
Expand Down Expand Up @@ -57,6 +58,7 @@ add_dependencies(spikes_leka
spike_lk_command_kit
spike_lk_coreled
spike_lk_core_touch_sensor
spike_lk_dac
spike_lk_event_queue
spike_lk_file_reception
spike_lk_file_manager_kit
Expand Down
22 changes: 22 additions & 0 deletions spikes/lk_dac/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Leka - LekaOS
# Copyright 2024 APF France handicap
# SPDX-License-Identifier: Apache-2.0

add_mbed_executable(spike_lk_dac)

target_include_directories(spike_lk_dac
PRIVATE
.
)

target_sources(spike_lk_dac
PRIVATE
main.cpp
)

target_link_libraries(spike_lk_dac
CoreSTM32Hal
CoreDAC
)

target_link_custom_leka_targets(spike_lk_dac)
71 changes: 71 additions & 0 deletions spikes/lk_dac/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Leka - LekaOS
// Copyright 2024 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include <cmath>

#include "rtos/ThisThread.h"

#include "CoreDAC.h"
#include "CoreSTM32Hal.h"
#include "CoreSTM32HalBasicTimer.h"
#include "DigitalOut.h"
#include "LogKit.h"

using namespace leka;
using namespace std::chrono_literals;

auto hal = CoreSTM32Hal {};
auto hal_timer = CoreSTM32HalBasicTimer {hal};
auto coredac = CoreDAC {hal, hal_timer};

auto audio_enable = mbed::DigitalOut {SOUND_ENABLE, 1};

constexpr uint32_t sample_rate_hz = 44'100;

void fillBufferWithSinWave(uint16_t *buffer, uint32_t samples_per_period, uint16_t maxValue, uint16_t minValue)
{
auto resolution = 2.0 * M_PI / samples_per_period;

auto sin0_1 = [](double value) { return (sin(value) + 1.0) / 2.0; };
auto normalization = [maxValue, minValue](double standard_value) {
return standard_value * (maxValue - minValue) + minValue;
};

for (uint32_t sample = 0; sample < samples_per_period; sample++) {
auto standard_value = sin0_1(sample * resolution);
auto normalized_value = normalization(standard_value);
buffer[sample] = static_cast<uint16_t>(normalized_value);
}
}

auto main() -> int
{
logger::init();

log_info("Hello, World!\n\n");

hal_timer.initialize(500);
coredac.initialize();

const uint32_t frequency = 440;
const uint16_t maxVal = 0x100;
const uint16_t minVal = 0x000;
std::array<uint16_t, sample_rate_hz / frequency> buffer {};
fillBufferWithSinWave(buffer.data(), buffer.size(), maxVal, minVal);

coredac.registerDataToPlay(buffer);

log_info("buffer size: %d", buffer.size());
log_info("Start sound");
coredac.start();

rtos::ThisThread::sleep_for(1s);

log_info("Stop sound");
coredac.stop();

while (true) {
rtos::ThisThread::sleep_for(1min);
}
}

0 comments on commit fef78bf

Please sign in to comment.