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 hal = CoreSTM32Hal {};
22
+ auto hal_timer = CoreSTM32HalBasicTimer {hal};
23
+ auto coredac = CoreDAC {hal, hal_timer};
24
24
25
- const auto sound_file_path = std::filesystem::path {" /fs/home/wav/fur-elise.wav" };
26
- auto file = FileManagerKit::File {sound_file_path};
25
+ auto sd_bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK};
26
+ auto fatfs = FATFileSystem {" fs" };
27
+ auto filename = std::filesystem::path {};
27
28
28
- auto thread_audio = rtos::Thread { };
29
+ auto audiokit = AudioKit {hal_timer, coredac };
29
30
30
- auto audio_enable = mbed::DigitalOut {SOUND_ENABLE, 1 };
31
- auto audio_output = mbed::AnalogOut {MCU_SOUND_OUT};
31
+ auto service_config = BLEServiceConfig {};
32
+ auto services = std::to_array<interface::BLEService *>({&service_config});
33
+ auto blekit = BLEKit {};
32
34
33
35
void initializeSD ()
34
36
{
@@ -40,48 +42,33 @@ void initializeSD()
40
42
fatfs.mount (&sd_bd);
41
43
}
42
44
43
- void playSound ( )
45
+ void play (std::string filename )
44
46
{
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*/
47
+ log_info (" Play file: %s" , filename.c_str ());
48
+ audiokit.play (filename);
66
49
}
67
50
68
51
auto main () -> int
69
52
{
70
53
logger::init ();
71
54
72
55
log_info (" Hello, World!\n\n " );
56
+ rtos::ThisThread::sleep_for (1s);
73
57
74
58
initializeSD ();
75
59
76
- if (FileManagerKit::file_is_missing (sound_file_path)) {
77
- return 1 ;
78
- }
60
+ audiokit.initialize ();
79
61
80
- while (true ) {
81
- file.open (sound_file_path);
82
- playSound ();
83
- file.close ();
62
+ blekit.setServices (services);
63
+ blekit.init ();
64
+
65
+ service_config.onRobotNameUpdated ([](const std::array<uint8_t , BLEServiceConfig::kMaxRobotNameSize > &robot_name) {
66
+ const auto *end_index = std::find (robot_name.begin (), robot_name.end (), ' \0 ' );
67
+ filename = std::string {robot_name.begin (), end_index};
68
+ play (filename);
69
+ });
84
70
85
- rtos::ThisThread::sleep_for (1s);
71
+ while (true ) {
72
+ rtos::ThisThread::sleep_for (1min);
86
73
}
87
74
}
0 commit comments