You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Commander API is a C API to interpret character based commands and map them to their functions easily
2
+
3
+
**Version 1.0.0**
4
+
5
+
Commander API is a C API to interpret character based commands and map them to their functions easily. It is designed mainly to work with low memory capacity devices for example small ARM devices or some embedded stuff.
6
+
7
+
8
+
9
+
## Installation
10
+
11
+
Installation is easy. Jus add __interpreter.h__, and __interpreter.c__ files to your project.
12
+
13
+
## Usage
14
+
15
+
First you have to add the two files to your project. Than you have to create the instruction data in __interpreter.c__ file under the following comment:
16
+
17
+
'''c
18
+
// +---- Create instruction data for the API ----+
Use the __create_instruction_data()__ macro to add your commands to the interpreter like above. You have to do this for every instruction that you want to add. The first argument is the exact command name( this will be the exact command name! ), and the second one is a short description. Note that the first argument __must not have any quotes before and after!__
29
+
30
+
The next step is to match the functions to your commands. You have to do it in interpreter.c file, in the init_interpreter() function, under the following comment:
Use the __add_instruction()__ macro to match the name and the asociated function like above. You have to do this for every instruction that you have added previously. The first argument is the instruction name __exactly like the previous step__, the second argument is the function that you want to use when this command gets executed. This function has to be a __void__ return type with two arguments. The first argument is a __char*__ type and the second one is a __int(*resp_fn)(const char*, ...)__ type. I know that it looks scary but it can be very handy and you have to just copy the example function to create a new one.
44
+
45
+
The next thing you have to do is to specify the number of commands you have been added. It can be done in the __interpreter.h__ file under the following comment:
46
+
47
+
'''c
48
+
// +---- Set the correct values ----+
49
+
// | |
50
+
// | NUM_OF_API_FUNCS value has |
51
+
// | to be exact! |
52
+
// | |
53
+
// +----------------------------------+
54
+
55
+
#define NUM_OF_API_FUNCS 4
56
+
'''
57
+
58
+
Modify the __NUM_OF_API_FUNCS__ value to the right number unless __the program will crash!__
59
+
60
+
The last thing you have to do is to actually create your functions. A simple example function can be like this:
The example function above shows the function of the response function. The interpreter can send messages back to the sender channel. If not used just pass a __null__ pointer to it.
80
+
81
+
## How it works, why to use?
82
+
The library does not use any malloc or dynamic memory. The module creates a balanced binary tree by alphabetical order in initialization time. The memory requirement of this tree can be calculated at compile time if we know how many instructions we have. This is why you must configure __NUM_OF_API_FUNCS__ symbol correctly! To create the balanced tree it is a bit more compute hungry than the old strcmp method, but the tree method will boost significantly the speed in search time, so I think it's worth it. In the worst case it finds a command in O*log2(N). That means it can find a command(if it exists) in a __1024__ element long command list under __10__ search steps.
83
+
84
+
## Contributing
85
+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
0 commit comments