-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathInterfaceMenu.cpp
More file actions
365 lines (317 loc) · 9.12 KB
/
InterfaceMenu.cpp
File metadata and controls
365 lines (317 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "InterfaceMenu.hpp"
#include "../IDataSource.hpp"
#include "../Audio/WaveSink.hpp"
#include "../ResourceManager.hpp"
#include "usart.h"
#include "baud_config.h"
void CLI_Init(TX_BYTE_POOL *byte_pool, UART_HandleTypeDef *huart)
{
static const char *name = "InterfaceMenu mutex";
tx_mutex_create(&CLI::InterfaceMenu::singleton_mutex, (char *)name, 0);
CLI::InterfaceMenu::InitSingleton(*byte_pool, new IO::Uart(*byte_pool, huart), ResourceManager::GetFileSystem(), ResourceManager::GetWaveSink(), ResourceManager::GetDeviceUnderTest());
}
void CLI_Run()
{
CLI::InterfaceMenu::GetSingleton().Run();
}
void Record_WW_Detection()
{
CLI::InterfaceMenu::GetSingleton().RecordOneDetection();
}
void Record_Dutycycle_Start()
{
if( CLI::InterfaceMenu::InstanceInitialized() )
{
// only call this if the CLI instance is initialized
CLI::InterfaceMenu::GetSingleton().DutycycleStart();
}
}
void Record_Dutycycle_Stop()
{
if( CLI::InterfaceMenu::InstanceInitialized() )
{
// only call this if the CLI instance is initialized
CLI::InterfaceMenu::GetSingleton().DutycycleStop();
}
}
namespace CLI
{
InterfaceMenu *InterfaceMenu::instance = (InterfaceMenu *)TX_NULL;
TX_MUTEX InterfaceMenu::singleton_mutex = { 0 };
/**
* Key-Value pairs that link the command string to functions to execute them
*/
const menu_command_t InterfaceMenu::menu_struct[] = { {"dut", PassthroughWrapper},
{"name", NameWrapper},
{"ls", ListWrapper},
{"play", PlayWrapper},
{"setbaud", SetBaudWrapper},
{"checkbaud", CheckBaudWrapper},
{"record_detections", RecDetsWrapper},
{"print_detections", PrintDetsWrapper},
{"print_dutycycle", PrintDutycycleWrapper},
{"", DefaultWrapper} };
bool InterfaceMenu::InstanceInitialized()
{
if( instance )
return true;
else
return false;
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::ListWrapper(const std::string &args)
{
instance->List(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::NameWrapper(const std::string &args)
{
instance->Name(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::PlayWrapper(const std::string &args)
{
instance->Play(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::SetBaudWrapper(const std::string &args)
{
instance->SetBaud(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::CheckBaudWrapper(const std::string &args)
{
instance->CheckBaud(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::RecDetsWrapper(const std::string &args)
{
instance->RecordDetections(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::PrintDetsWrapper(const std::string &args)
{
instance->PrintDetections(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::PrintDutycycleWrapper(const std::string &args)
{
instance->PrintDutycycle(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::DefaultWrapper(const std::string &args)
{
instance->Default(args);
}
/**
* Wrap the singleton function in a static function
*/
void InterfaceMenu::PassthroughWrapper(const std::string &args)
{
instance->Passthrough(args);
}
/**
* Create the singleton object. This is thread safe.
* @param byte_pool The data pool used to create objects
* @param uart The host UART
* @param file_system The file system to interact with
* @param player The media player
* @param dut The DUT object
*/
void InterfaceMenu::InitSingleton(TX_BYTE_POOL &byte_pool, IO::Uart *uart, IO::FileSystem &file_system, Audio::WaveSink &player, Test::DeviceUnderTest &dut)
{
tx_mutex_get(&singleton_mutex, TX_WAIT_FOREVER);
if(instance == TX_NULL)
{
instance = new InterfaceMenu(byte_pool, *uart, file_system, player, dut);
}
tx_mutex_put(&singleton_mutex);
}
InterfaceMenu &InterfaceMenu::GetSingleton()
{
return *instance;
}
/**
* Constructor for the CLI
* @param byte_pool The data pool used to create objects
* @param uart The host UART
* @param file_system The file system to interact with
* @param player The media player
* @param dut The DUT object
*/
InterfaceMenu::InterfaceMenu(TX_BYTE_POOL &byte_pool, IO::Uart &uart, IO::FileSystem &file_system, Audio::WaveSink &player, Test::DeviceUnderTest &dut):
Menu(byte_pool, uart, menu_struct),
file_system(file_system),
player(player),
dut(dut)
{
}
/**
* List the files in the given menu
* @param args The dir to read
*/
void InterfaceMenu::List(const std::string &args)
{
file_system.ListDirectory(args, &queue);
SendResponse();
SendEnd();
}
/**
* Return the name of this interface board
* @param args Ignored
*/
void InterfaceMenu::Name(const std::string &args)
{
SendString("tinyML Enhanced Interface Board");
SendEndLine();
SendEnd();
}
/**
* Sends the command to the dut
* Returns m-dut-passthrough(<<args>>)
* Then the response from the dut preceeded with "[dut]: "
*
* @param args command for the dut
*/
void InterfaceMenu::Passthrough(const std::string &args)
{
static const std::string prefix("[dut]: ");
SendString("m-dut-passthrough(");
SendString(args);
SendString(")");
SendEndLine();
SendEnd();
dut.SendCommand(args, &queue);
SendResponse(&prefix);
}
/**
* Play a file from the SD card
* @param args The name of the file to play
*/
void InterfaceMenu::Play(const std::string &args)
{
Audio::PlayerResult result;
IDataSource *source = file_system.OpenFile(args);
Audio::WaveSource wav(*source);
// if( !wav.valid_wave ) {
// std::string *msg = new std::string("Error. Not a valid wav file: ");
// msg->append(source->GetName());
// msg->append("\n");
// SendString(msg->c_str());
// }
std::string *msg = new std::string("Attempting to play ");
msg->append(source->GetName());
msg->append("\n");
SendString(msg->c_str());
result = player.Play(wav);
if( result == Audio::ERROR ) {
SendString("Error playing file. Check that the file exists and is a 2-channel wav file.\n");
}
else {
SendString("succeeded.\n");
}
SendEnd();
delete source;
}
/**
* Set the new baud rate for the board
*/
void InterfaceMenu::SetBaud(const std::string &args)
{
int baud = std::stoi(args);
SaveBaudRateToFlash(baud); // Save permanently to Flash
SendString("set baud to: " + args + "\n");
SendEnd();
NVIC_SystemReset(); //Reset the MCU
}
/**
* Check the current baud rate
*/
void InterfaceMenu::CheckBaud(const std::string &args)
{
int currentBaud = huart3.Init.BaudRate;
SendString("baud is: " + std::to_string(currentBaud) + "\n");
SendEnd(); // End response
}
void InterfaceMenu::RecordDetections(const std::string &args)
{
// Sets the interface board into mode to capture detections,
// but the detections are recorded in an interrupt handler,
// so we can return here and proceed to playing the wav file
__HAL_TIM_SET_COUNTER(&htim2, 0);
SendString("Now recording detections");
SendEndLine();
dut.StartRecordingDetections();
SendEnd();
}
void InterfaceMenu::PrintDetections(const std::string &args)
{
uint32_t *timestamps = dut.GetDetections();
uint32_t num_timestamps = dut.GetNumDetections();
SendString("Detection Timestamps (ms)");
SendEndLine();
for(uint32_t i=0; i<num_timestamps; i++)
{
SendString(std::to_string(timestamps[i]) + ",");
SendEndLine();
}
SendEnd();
}
void InterfaceMenu::PrintDutycycle(const std::string &args)
{
uint32_t *rising_edges = dut.GetDutycycleRisingEdges();
uint32_t num_rising_edges = dut.GetNumDutycycleRisingEdges();
uint32_t *falling_edges = dut.GetDutycycleFallingEdges();
uint32_t num_falling_edges = dut.GetNumDutycycleFallingEdges();
SendString("Duty cycle start times (s)");
SendEndLine();
for(uint32_t i=0; i<num_rising_edges; i++)
{
SendString(std::to_string(rising_edges[i]) + ", ");
if( (i+1) % 8 == 0){
SendEndLine();
}
}
SendEndLine();
SendString("Duty cycle stop times (s)");
SendEndLine();
for(uint32_t i=0; i<num_falling_edges; i++)
{
SendString(std::to_string(falling_edges[i]) + ", ");
if( (i+1) % 8 == 0){
SendEndLine();
}
}
SendEnd();
}
void InterfaceMenu::RecordOneDetection()
{
dut.RecordDetection();
}
void InterfaceMenu::DutycycleStart()
{
dut.RecordDutycycleStart();
}
void InterfaceMenu::DutycycleStop()
{
dut.RecordDutycycleStop();
}
}