Skip to content

Commit fef78bf

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

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
26+
void fillBufferWithSinWave(uint16_t *buffer, uint32_t samples_per_period, uint16_t maxValue, uint16_t minValue)
27+
{
28+
auto resolution = 2.0 * M_PI / samples_per_period;
29+
30+
auto sin0_1 = [](double value) { return (sin(value) + 1.0) / 2.0; };
31+
auto normalization = [maxValue, minValue](double standard_value) {
32+
return standard_value * (maxValue - minValue) + minValue;
33+
};
34+
35+
for (uint32_t sample = 0; sample < samples_per_period; sample++) {
36+
auto standard_value = sin0_1(sample * resolution);
37+
auto normalized_value = normalization(standard_value);
38+
buffer[sample] = static_cast<uint16_t>(normalized_value);
39+
}
40+
}
41+
42+
auto main() -> int
43+
{
44+
logger::init();
45+
46+
log_info("Hello, World!\n\n");
47+
48+
hal_timer.initialize(500);
49+
coredac.initialize();
50+
51+
const uint32_t frequency = 440;
52+
const uint16_t maxVal = 0x100;
53+
const uint16_t minVal = 0x000;
54+
std::array<uint16_t, sample_rate_hz / frequency> buffer {};
55+
fillBufferWithSinWave(buffer.data(), buffer.size(), maxVal, minVal);
56+
57+
coredac.registerDataToPlay(buffer);
58+
59+
log_info("buffer size: %d", buffer.size());
60+
log_info("Start sound");
61+
coredac.start();
62+
63+
rtos::ThisThread::sleep_for(1s);
64+
65+
log_info("Stop sound");
66+
coredac.stop();
67+
68+
while (true) {
69+
rtos::ThisThread::sleep_for(1min);
70+
}
71+
}

0 commit comments

Comments
 (0)