Skip to content

Commit 54509ec

Browse files
YannLocatelliAermanioSamHadjes
committed
✨ (dac): Add CoreDAC spike
Co-Authored-By: Maxime Blanc <[email protected]> Co-Authored-By: SamHadjes <[email protected]>
1 parent 7733657 commit 54509ec

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

spikes/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_subdirectory(${SPIKES_DIR}/lk_command_kit)
1313
add_subdirectory(${SPIKES_DIR}/lk_config_kit)
1414
add_subdirectory(${SPIKES_DIR}/lk_coreled)
1515
add_subdirectory(${SPIKES_DIR}/lk_core_touch_sensor)
16+
add_subdirectory(${SPIKES_DIR}/lk_dac)
1617
add_subdirectory(${SPIKES_DIR}/lk_event_queue)
1718
add_subdirectory(${SPIKES_DIR}/lk_file_reception)
1819
add_subdirectory(${SPIKES_DIR}/lk_file_manager_kit)
@@ -57,6 +58,7 @@ add_dependencies(spikes_leka
5758
spike_lk_command_kit
5859
spike_lk_coreled
5960
spike_lk_core_touch_sensor
61+
spike_lk_dac
6062
spike_lk_event_queue
6163
spike_lk_file_reception
6264
spike_lk_file_manager_kit

spikes/lk_dac/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Leka - LekaOS
2+
# Copyright 2024 APF France handicap
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
add_mbed_executable(spike_lk_dac)
6+
7+
target_include_directories(spike_lk_dac
8+
PRIVATE
9+
.
10+
)
11+
12+
target_sources(spike_lk_dac
13+
PRIVATE
14+
main.cpp
15+
)
16+
17+
target_link_libraries(spike_lk_dac
18+
CoreSTM32Hal
19+
CoreDAC
20+
)
21+
22+
target_link_custom_leka_targets(spike_lk_dac)

spikes/lk_dac/main.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Leka - LekaOS
2+
// Copyright 2024 APF France handicap
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include <cmath>
6+
7+
#include "rtos/ThisThread.h"
8+
9+
#include "CoreDAC.h"
10+
#include "CoreSTM32Hal.h"
11+
#include "CoreSTM32HalBasicTimer.h"
12+
#include "DigitalOut.h"
13+
#include "LogKit.h"
14+
15+
using namespace leka;
16+
using namespace std::chrono_literals;
17+
18+
auto hal = CoreSTM32Hal {};
19+
auto hal_timer = CoreSTM32HalBasicTimer {hal};
20+
auto coredac = CoreDAC {hal, hal_timer};
21+
22+
auto audio_enable = mbed::DigitalOut {SOUND_ENABLE, 1};
23+
24+
constexpr uint32_t sample_rate_hz = 44'100;
25+
constexpr auto coefficient = 10;
26+
27+
void fillBufferWithSinWave(uint16_t *buffer, uint32_t samples_per_period, uint16_t maxValue, uint16_t minValue)
28+
{
29+
auto resolution = 2.0 * M_PI / samples_per_period;
30+
31+
auto sin0_1 = [](double value) { return (sin(value) + 1.0) / 2.0; };
32+
auto normalization = [maxValue, minValue](double standard_value) {
33+
return standard_value * (maxValue - minValue) + minValue;
34+
};
35+
36+
for (uint32_t sample = 0; sample < samples_per_period; sample += coefficient) {
37+
auto standard_value = sin0_1(sample * resolution);
38+
auto normalized_value = normalization(standard_value);
39+
std::fill_n(buffer + sample, coefficient, static_cast<uint16_t>(normalized_value));
40+
}
41+
}
42+
43+
auto main() -> int
44+
{
45+
logger::init();
46+
47+
log_info("Hello, World!\n\n");
48+
49+
hal_timer.initialize(sample_rate_hz * coefficient);
50+
coredac.initialize();
51+
52+
const uint32_t frequency = 440;
53+
const uint16_t maxVal = 0xFFF;
54+
const uint16_t minVal = 0x000;
55+
std::array<uint16_t, sample_rate_hz * coefficient / frequency> buffer {};
56+
fillBufferWithSinWave(buffer.data(), buffer.size(), maxVal, minVal);
57+
58+
coredac.registerDataToPlay(buffer);
59+
60+
log_info("buffer size: %d", buffer.size());
61+
log_info("Start sound");
62+
coredac.start();
63+
64+
rtos::ThisThread::sleep_for(1s);
65+
66+
log_info("Stop sound");
67+
coredac.stop();
68+
69+
while (true) {
70+
rtos::ThisThread::sleep_for(1min);
71+
}
72+
}

0 commit comments

Comments
 (0)