-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamshell.h
60 lines (45 loc) · 1.97 KB
/
yamshell.h
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
/*
YamShell (Yet Another Mbed Shell) is a serial based CLI for Mbed OS 6.
It is designed to be easily used in quick and small projects.
If you need a proper shell for serious purposes look at NaturalTinyShell.
*/
#ifndef YAMSHELL_H
#define YAMSHELL_H
#include "mbed.h"
#include <array>
#include <tuple>
#include <string>
constexpr uint32_t MAX_COMMAND_COUNT = 32;
constexpr uint32_t LINE_BUFFER_SIZE = 256;
constexpr uint32_t ARG_MAX = 8; //max number of arguments allowed
class YamShell
{
public:
typedef Callback<void(int argc, char** argv)> _CommandCallback;
YamShell(PinName serialTX, PinName serialRX, uint32_t baud, bool preserveLine = true);
void write(const void *buf, std::size_t len);
void print(const char* s);
void println(const char* s);
void printf(const char fmt[], ...);
void registerCommand(std::string command_name, _CommandCallback command_function);
void setBaud(int baud){_bf.set_baud(baud);};
void setPreserveLine(bool pl){_preserve_line = pl;};
bool getPreserveLine(){return _preserve_line;};
BufferedSerial* getBufferedSerial(){return &_bf;};
private:
BufferedSerial _bf;
Thread _input_thread;
//Array of callbacks to call for a given command string
//TODO dynamic registration isn't great. There is probably some way to use C++ features so that callback handlers can all be registered at compile time.
// The std::string for command name could probably easily be char*, is anything gained by being a string?
// The C way would probably be a preprocessor directive that would feed into a big assembled if-else block or static array to loop through
typedef std::tuple<std::string, _CommandCallback> _CallbackTuple;
std::array<_CallbackTuple, MAX_COMMAND_COUNT> _commands{};
uint32_t _command_callback_count = 0;
bool _preserve_line;
char _linebuf[LINE_BUFFER_SIZE]{0};
int _lineind = 0;
void _input_loop();
void _input_line_handler(const char* il);
};
#endif