1
1
// Leka - LekaOS
2
- // Copyright 2022 APF France handicap
2
+ // Copyright 2024 APF France handicap
3
3
// SPDX-License-Identifier: Apache-2.0
4
4
5
- #include < array>
6
- #include < filesystem>
7
-
8
- #include " drivers/AnalogOut.h"
9
- #include " drivers/DigitalOut.h"
10
- #include " platform/mbed_wait_api.h"
11
5
#include " rtos/ThisThread.h"
12
- #include " rtos/Thread.h"
13
6
7
+ #include " BLEKit.h"
8
+ #include " BLEServiceConfig.h"
9
+
10
+ #include " AudioKit.h"
11
+ #include " CoreDAC.h"
12
+ #include " CoreSTM32Hal.h"
13
+ #include " CoreSTM32HalBasicTimer.h"
14
14
#include " FATFileSystem.h"
15
- #include " FileManagerKit.h"
16
15
#include " LogKit.h"
17
16
#include " SDBlockDevice.h"
18
17
19
18
using namespace leka ;
20
19
using namespace std ::chrono_literals;
21
20
22
- auto sd_bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK};
23
- auto fatfs = FATFileSystem {" fs" };
21
+ auto sd_bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK};
22
+ auto fatfs = FATFileSystem {" fs" };
23
+ auto filename = std::filesystem::path {};
24
+
25
+ auto hal = CoreSTM32Hal {};
26
+
27
+ namespace audio {
28
+
29
+ namespace internal {
30
+
31
+ extern " C" auto hal_timer = CoreSTM32HalBasicTimer {hal};
32
+ extern " C" auto coredac = CoreDAC {hal, hal_timer};
24
33
25
- const auto sound_file_path = std::filesystem::path {" /fs/home/wav/fur-elise.wav" };
26
- auto file = FileManagerKit::File {sound_file_path};
34
+ } // namespace internal
27
35
28
- auto thread_audio = rtos::Thread { };
36
+ auto kit = AudioKit {internal::hal_timer, internal::coredac };
29
37
30
- auto audio_enable = mbed::DigitalOut {SOUND_ENABLE, 1 };
31
- auto audio_output = mbed::AnalogOut {MCU_SOUND_OUT};
38
+ } // namespace audio
39
+
40
+ auto service_config = BLEServiceConfig {};
41
+ auto services = std::to_array<interface::BLEService *>({&service_config});
42
+ auto blekit = BLEKit {};
32
43
33
44
void initializeSD ()
34
45
{
@@ -40,48 +51,33 @@ void initializeSD()
40
51
fatfs.mount (&sd_bd);
41
52
}
42
53
43
- void playSound ( )
54
+ void play (std::string filename )
44
55
{
45
- static const auto _n_bytes_to_read = int {512 }; // arbitrary
46
- auto _buffer = std::array<uint8_t , _n_bytes_to_read> {0 };
47
-
48
- auto _ns_sample_rate = uint32_t {22676 }; // 1,000,000,000 / 44,100 (in ns)
49
- auto _ns_sample_rate_adapted = _ns_sample_rate * 1.7 ; // arbitrary, 1s in MCU is not exactly 1s in real life
50
- auto bytesread = uint32_t {_n_bytes_to_read};
51
-
52
- /* START READ WAV */
53
- while (bytesread == _n_bytes_to_read) {
54
- // Read "_n_bytes_to_read" from file at each iteration. Real bytes read is given by "bytesread"
55
- if (bytesread = file.read (_buffer.data (), _n_bytes_to_read); bytesread != 0 ) {
56
- // Play every 2-bytes (sound encoded in 16 bits)
57
- for (uint32_t j = 0 ; j < bytesread; j += 4 ) { // Play one channel, data for stereo are alternate
58
- audio_output.write_u16 ((_buffer.at (j + 1 ) + 0x8000 ) >>
59
- 1 ); // offset for int16 data (0x8000) and volume 50% (>>1)
60
-
61
- wait_ns (_ns_sample_rate_adapted); // adjust play speed
62
- }
63
- }
64
- }
65
- /* END READ WAV*/
56
+ log_info (" Play file: %s" , filename.c_str ());
57
+ audio::kit.play (filename);
66
58
}
67
59
68
60
auto main () -> int
69
61
{
70
62
logger::init ();
71
63
72
64
log_info (" Hello, World!\n\n " );
65
+ rtos::ThisThread::sleep_for (1s);
73
66
74
67
initializeSD ();
75
68
76
- if (FileManagerKit::file_is_missing (sound_file_path)) {
77
- return 1 ;
78
- }
69
+ audio::kit.initialize ();
79
70
80
- while (true ) {
81
- file.open (sound_file_path);
82
- playSound ();
83
- file.close ();
71
+ blekit.setServices (services);
72
+ blekit.init ();
73
+
74
+ service_config.onRobotNameUpdated ([](const std::array<uint8_t , BLEServiceConfig::kMaxRobotNameSize > &robot_name) {
75
+ const auto *end_index = std::find (robot_name.begin (), robot_name.end (), ' \0 ' );
76
+ filename = std::string {robot_name.begin (), end_index};
77
+ play (filename);
78
+ });
84
79
85
- rtos::ThisThread::sleep_for (1s);
80
+ while (true ) {
81
+ rtos::ThisThread::sleep_for (1min);
86
82
}
87
83
}
0 commit comments