Commander-API  V0.2A
Commander-API is a C API, that interprets character-based commands
interpreter.h
Go to the documentation of this file.
1 /*
2  * Created on June 18 2020
3  *
4  * Copyright (c) 2020 - Daniel Hajnal
5  * hajnal.daniel96@gmail.com
6  * This file is part of the Commander-API project.
7 */
8 
9 
10 #ifndef INTERPRETER_H_
11 #define INTERPRETER_H_
12 
13 
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <string.h>
17 
19 #define COMMANDER_API_VERSION "V0.2A"
20 
27 #define ARDUINO_PLATFORM
28 
29 #ifdef ARDUINO_PLATFORM
30 
32 #include "Arduino.h"
33 
35 int arduino_printf( const char *fmt, ... );
36 
37 #endif
38 
46 #define INTERPRETER_BUFFER_SIZE 30
47 
48 // +---- Set the correct values ----+
49 // | |
50 // | NUM_OF_API_FUNCS value has |
51 // | to be exact! |
52 // | |
53 // +----------------------------------+
54 
63 #define NUM_OF_API_FUNCS 4
64 
71 #define INTERPRETER_DBG printf
72 
77 #define ARDUINP_PRINTF_BUFF_SIZE 150
78 
79 #ifdef ARDUINO_PLATFORM
80 #undef INTERPRETER_DBG
81 #define INTERPRETER_DBG arduino_printf
82 #endif
83 
90 #define INTERPRETER_PRINTF printf
91 
92 #ifdef ARDUINO_PLATFORM
93 #undef INTERPRETER_PRINTF
94 #define INTERPRETER_PRINTF arduino_printf
95 #endif
96 
102 typedef struct API_t{
103 
104  uint16_t place; // This will store the alphabetical place of the command in the tree
105  struct API_t *left; // Left element on a binatry tree branch
106  struct API_t *right; // Right element on a binary tree branch
107  const char **name; // Name of the command
108  const char **desc; // Description of the command
109  void(*func)(char*,int(*resp_fn)(const char*, ...)); // Function pointer to the command function
110 
112 
121 void add_interpreter_command(const char **name, const char **desc, void(*func)(char*,int(*resp_fn)(const char*, ...)));
122 
130 void init_interpreter(void);
131 
137 void print_apis_in_order(API_t *head);
138 
146 void recursive_indexer(API_t *head);
147 
154 void index_apis_in_order(API_t *head);
155 
162 uint16_t find_api_index_by_place( uint16_t place );
163 
170 void optimise_api_tree(API_t *head);
171 
183 void swap_api_elements( uint16_t index, uint16_t place );
184 
192 void recursive_optimiser( int32_t start_index, int32_t stop_index );
193 
201 void execute( char *cmd, int(*resp_fn)(const char*, ...) );
202 
203 #endif
API_t
struct API_t API_t
Structure for command data.
API_t::name
const char ** name
Definition: interpreter.h:107
API_t
Structure for command data.
Definition: interpreter.h:102
optimise_api_tree
void optimise_api_tree(API_t *head)
Tree optimizer.
Definition: interpreter.c:369
find_api_index_by_place
uint16_t find_api_index_by_place(uint16_t place)
Finds an element in API tree by alphabetical order.
Definition: interpreter.c:324
API_t::func
void(* func)(char *, int(*resp_fn)(const char *,...))
Definition: interpreter.h:109
API_t::place
uint16_t place
Definition: interpreter.h:104
API_t::left
struct API_t * left
Definition: interpreter.h:105
recursive_optimiser
void recursive_optimiser(int32_t start_index, int32_t stop_index)
Recorsive Optinizer function.
Definition: interpreter.c:402
index_apis_in_order
void index_apis_in_order(API_t *head)
Indexes all elements in the tree.
Definition: interpreter.c:257
API_t::desc
const char ** desc
Definition: interpreter.h:108
print_apis_in_order
void print_apis_in_order(API_t *head)
Prints your commands.
Definition: interpreter.c:302
init_interpreter
void init_interpreter(void)
This is the initialization function.
Definition: interpreter.c:139
arduino_printf
int arduino_printf(const char *fmt,...)
If you use Arduino environment you have to include Arduino.h.
execute
void execute(char *cmd, int(*resp_fn)(const char *,...))
Execution function.
Definition: interpreter.c:427
recursive_indexer
void recursive_indexer(API_t *head)
Indexes all elements in the tree.
Definition: interpreter.c:275
API_t::right
struct API_t * right
Definition: interpreter.h:106
swap_api_elements
void swap_api_elements(uint16_t index, uint16_t place)
Swap two elements in API tree.
Definition: interpreter.c:346
add_interpreter_command
void add_interpreter_command(const char **name, const char **desc, void(*func)(char *, int(*resp_fn)(const char *,...)))
This command is used to add a new command to the interpreter.
Definition: interpreter.c:180