|
| 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