SikendeRTOS
RTOS for ARM Cortex M3+ SoCs designed and written from scratch
Interpreter.c
Go to the documentation of this file.
1
7#include "Interpreter.h"
8#include "UART0.h"
9#include "OS.h"
10#include "cpu.h"
11
12// Command Types
13// If more commands are added, add on to the list
14#define CMD_MENU 0
15#define CMD_HELP 1
16#define CMD_ECHO 2
17#define CMD_MEASURE 3
18#define CMD_DISPLAY 4
19#define CMD_LED 5
20#define CMD_OS 6
21
22// Type of modules to measure. Add more to the list
23#define MEASURE_ADC 1
24
25static char GraphicRTOS[] = " ___ ___ ___ ___\n\r /\\ \\ /\\ \\ /\\ \\ /\\ \\\n\r /::\\ \\ \\:\\ \\ /::\\ \\ /::\\ \\\n\r /:/\\:\\ \\ \\:\\ \\ /:/\\:\\ \\ /:/\\ \\ \\\n\r /::\\~\\:\\ \\ /::\\ \\ /:/ \\:\\ \\ _\\:\\~\\ \\ \\\n\r /:/\\:\\ \\:\\__\\ /:/\\:\\__\\/:/__/ \\:\\__\\/\\ \\:\\ \\ \\__\\\n\r \\/_|::\\/:/ //:/ \\/__/\\:\\ \\ /:/ /\\:\\ \\:\\ \\/__/\n\r |:|::/ //:/ / \\:\\ /:/ / \\:\\ \\:\\__\\\n\r |:|\\/__/ \\/__/ \\:\\/:/ / \\:\\/:/ /\n\r |:| | \\::/ / \\::/ /\n\r \\|__| \\/__/ \\/__/\n\r";
26
27char *readline(char *str);
28void parseString(char *str, char cmd[MAX_WORDS][MAX_CHARS_PER_WORD]);
30void executeCommand(int32_t cmdType, char cmd[MAX_WORDS][MAX_CHARS_PER_WORD]);
31void printWelcomeMenu(void);
32void printGenericMenu(void);
33void printHelpMenu(void);
38
39
43void newLine(void){
44 OutCRLF();
45}
46
47
51void Interpreter(void){
52 UART_Init();
53 newLine();
55
56 char *inString;
57 static char cmdString[MAX_WORDS][MAX_CHARS_PER_WORD];
58 int32_t cmdType = -1;
59 while((inString = readline(">>")) != NULL){
60 parseString(inString, cmdString);
61 cmdType = findCommandType(cmdString);
62 executeCommand(cmdType, cmdString);
63 }
64}
65
71char *readline(char *str){
72 // must be static since dynamic memory is not available
73 // length 80 was chosen because that's how many characters can fit in the default terminal
74 static char inString[80];
75
76 char letter = 0;
77 int32_t idx = 0;
78 printf("%s", str); // print start command notifier (usually >>)
79 while(letter != '\n' && letter != '\r'){
80 letter = UART_InChar();
81
82 // If backspace was used, decrease index
83 if(letter == 0x7F){
84 idx--;
85 }
86
87 // Check may seem redundant but this is to exclude the new line character in the string
88 if(letter != '\n' && letter != '\r' && letter != 0x7F && letter != 0x1B){
89 inString[idx++] = letter;
90 }
91
92 printf("%c", letter);
93 }
94 inString[idx] = 0;
95 newLine();
96 return inString;
97}
98
104void parseString(char *str, char cmd[MAX_WORDS][MAX_CHARS_PER_WORD]){
105 // strtok is not thread safe. Strtok_r must be used to make is thread safe.
106 // Both strtoks modifies the string that it tokenizes. Strtok_r reqruies the
107 // savePtr to know the location that it left off at.
108 char *savePtr, *quotePtr;
109
110 char *token, *quoteToken;
111 int32_t idx = 0;
112
113 // Check if there are any quotation marks. If there are, then everything encompassed in it will
114 // go into a row of cmd.
115 quoteToken = strtok_r(str, "\"", &quotePtr);
116
117 token = strtok_r(quoteToken, " ", &savePtr);
118 while(token != NULL && idx < MAX_WORDS){
119 if(strlen(token) >= MAX_CHARS_PER_WORD){
120 printf("OVERFLOW");
121 }else{
122 memset(cmd[idx], 0, MAX_CHARS_PER_WORD);
123 strncpy(cmd[idx], token, strlen(token));
124 cmd[idx++][strlen(token)] = '\0';
125 }
126 token = strtok_r(NULL, " ", &savePtr);
127 }
128
129 // Copy string in quotation marks
130 quoteToken = strtok_r(NULL, "\"", &quotePtr);
131 memset(cmd[idx], 0, MAX_CHARS_PER_WORD);
132 if(quoteToken != NULL){
133 strncpy(cmd[idx], quoteToken, strlen(quoteToken));
134 cmd[idx++][strlen(quoteToken)] = '\0';
135 }
136
137 cmd[idx][0] = 0; // End with end character
138}
139
145 int32_t type = -1; // Undefined type
146
147 // Determine type of command
148 // Menu
149 if(strcmp(cmd[0], "menu") == 0 || strcmp(cmd[0], "m") == 0){
150 type = CMD_MENU;
151
152 // help
153 }else if(strcmp(cmd[0], "help") == 0 || strcmp(cmd[0], "h") == 0){
154 type = CMD_HELP;
155
156 // echo
157 }else if(strcmp(cmd[0], "echo") == 0){
158 type = CMD_ECHO;
159
160 // measure
161 }else if(strcmp(cmd[0], "measure") == 0){
162 type = CMD_MEASURE;
163
164 // display
165 }else if(strcmp(cmd[0], "display") == 0){
166 type = CMD_DISPLAY;
167
168 // LED
169 }else if(strcmp(cmd[0], "led") == 0){
170 type = CMD_LED;
171
172 // OS
173 }else if(strcmp(cmd[0], "os") == 0){
174 type = CMD_OS;
175 }
176
177 return type;
178}
179
184void executeCommand(int32_t cmdType, char cmd[MAX_WORDS][MAX_CHARS_PER_WORD]){
185 switch(cmdType){
186 // generic menu
187 case CMD_MENU:
189 break;
190
191 // Help menu
192 case CMD_HELP:
194 break;
195
196 // Echoes string
197 case CMD_ECHO:
198 printf("%s\n\r", cmd[1]);
199 break;
200
201 // Get measurements
202 case CMD_MEASURE:
203 commandMeasure(cmd);
204 break;
205
206 // Display onto ST7735
207 case CMD_DISPLAY:
208 commandDisplay(cmd);
209 break;
210
211 // Change LED
212 case CMD_LED:
213 commandLED(cmd);
214 break;
215
216 // read OS time
217 case CMD_OS:
218 commandOS(cmd);
219 break;
220
221 // Undefined command
222 default:
223 printf("Undefined command.\n\r");
224 break;
225 }
226}
227
232 newLine(); newLine();;
233 for(int32_t i = 0; i < 55; i++){
234 UART_OutChar('=');
235 }
236 printf("\n\r\n\r%s\n\r", GraphicRTOS);
237 printf("\n\rWelcome to the Sikender Ashraf's and Sijin Woo's OS.\n\r");
238 for(int32_t i = 0; i < 55; i++){
239 UART_OutChar('=');
240 }
241 newLine(); newLine();;
242 printf("Type \"menu\" or \"m\" to see menu...\n\r");
243}
244
249 printf("Type \"help\" or \"h\" to see more details...\n\r");
250 printf("echo \"<string>\"\n\r");
251 printf("measure -module -channel -samples\n\r");
252 printf("display -device -line \"<string>\"\n\r");
253 printf("led -color -status\n\r");
254 printf("os -set\n\r");
255
256 newLine();
257}
258
262void printHelpMenu(void){
263 newLine(); newLine();;
264 for(int32_t i = 0; i < 80; i++){
265 UART_OutChar('=');
266 }
267 printf("\n\rWelcome to the manual.\n\r");
268 for(int32_t i = 0; i < 80; i++){
269 UART_OutChar('=');
270 }
271 newLine(); newLine();;
272
273 // Echo command
274 printf("Name:\n\r");
275 printf("\techo \"<string>\"\n\r");
276 printf("Description:\n\r");
277 printf("This is the same as the echo command in linux. This is mainly to test if the \n\r");
278 printf("microcontroller is receiving and sending correctly.\n\r\n\r");
279 printf(" \"<string>\"\tString to be echoed. Make sure the string is in quotations\n\r");
280 printf("\t\tor the whole message might not be reciprocated.\n\n\r\r");
281
282 // Measure command
283// printf("Name:\n\r");
284// printf("\tmeasure -module -channel -samples\n\r");
285// printf("Description:\n\r");
286// printf("Gets the measurement value from the specific module. This can be reused to get\n\r");
287// printf("measurments from any module in the future such as the lidar, ultrasonic, etc.\n\r\n\r");
288// printf(" module\tModule to get the measurement from.\n\r");
289// printf("\t\tCurrently available:\n\r");
290// printf("\t\t -ADC\n\r");
291// printf(" channel\tSpecifies which channel of the module.\n\r");
292// printf("\t\tCurrently available:\n\r");
293// printf("\t\t -ADC : channel 0 to 11 are available\n\r");
294// printf("\t\t\t 0 : PE3\n\r");
295// printf("\t\t\t 1 : PE2\n\r");
296// printf("\t\t\t 2 : PE1\n\r");
297// printf("\t\t\t 3 : PE0\n\r");
298// printf("\t\t\t 4 : PD3\n\r");
299// printf("\t\t\t 5 : PD2\n\r");
300// printf("\t\t\t 6 : PD1\n\r");
301// printf("\t\t\t 7 : PD0\n\r");
302// printf("\t\t\t 8 : PE5\n\r");
303// printf("\t\t\t 9 : PE4\n\r");
304// printf("\t\t\t10 : PB4\n\r");
305// printf("\t\t\t11 : PB5\n\r\n\r");
306// printf(" samples\tSpecifies how many samples to retrieve.\n\r\n\r");
307// printf(" rate\tSpecifies rate(Hz) to retrieve samples. 100 <= rate <= 10000\n\r");
308
309
310 // Display command
311 printf("Name:\n\r");
312 printf("\tdisplay -device -line \"<string>\"\n\r");
313 printf("Description:\n\r");
314 printf("Displays message onto the ST7735 depending on the device and row.\n\r\n\r");
315 printf(" device\tSpecifies which half of the screen to display the message.\n\r");
316 printf("\t\t\"0\" or \"top\" for top half, \"1\" or \"bottom\" for bottom half\n\r\n\r");
317 printf(" line\t\tSpecifies which row of device section to display the message\n\r");
318 printf("\t\tline must be <=7 or else nothing will be displayed\n\r\n\r");
319 printf(" \"<string\"\tString to be displayed. Make sure the string is in quotations.\n\r");
320 printf("\t\tor the whole message might not be reciprocated.\n\n\r\r");
321
322 // LED command
323 printf("Name:\n\r");
324 printf("\tled -color\n\r");
325 printf("Description:\n\r");
326 printf("The chosen colored LED will be toggled.\n\r\n\r");
327 printf(" color\t\tSpecifies which colored LED will be changed. The LED colors can\n\r");
328 printf("\t\the combined if two or more LEDs are on. If red and blue LEDs\n\r");
329 printf("\t\tare both on then a magenta color will appear.\n\r");
330 printf("\t\t -red\n\r");
331 printf("\t\t -green\n\r");
332 printf("\t\t -blue\n\r\n\r");
333 //printf(" status\tNew state of the LED. The options are \"on\", \"off\", and \"toggle\".\n\r\n\r");
334
335 // OS
336 printf("Name:\n\r");
337 printf("\tos -set\n\r");
338 printf("Description:\n\r");
339 printf("This interfaces with the OS. Currently not much support is available.\n\r");
340 printf("Read and clear are only available\n\r\n\r");
341 printf(" set\t\tReads/clears the periodic time counter of the OS.\n\r\n\r");
342}
343
350 int32_t type = -1;
351
352 int32_t channel = 0;
353 int32_t samples = 1;
354 int32_t rate = 100;
355
356 // Find which module to get data from
357 if(strcmp(cmd[1], "ADC") == 0 || strcmp(cmd[1], "-ADC") == 0 || strcmp(cmd[1], "adc") == 0 || strcmp(cmd[1], "-adc") == 0){
358 type = MEASURE_ADC;
359 }
360
361 // Find what channel
362 sscanf(cmd[2], "%d", &channel);
363 // In case user typed a dash in front of the number
364 if(channel < 0){
365 channel = -channel;
366 }
367
368 // Find how many samples
369 if(cmd[3][0] != 0){
370 sscanf(cmd[3], "%d", &samples);
371 // In case user typed a dash in front of the number
372 if(samples < 0){
373 samples = -samples;
374 }
375 }
376
377 // Find rate to gather samples
378 if(cmd[4][0] != 0){
379 sscanf(cmd[4], "%d", &rate);
380 // In case user typed a dash in front of the number
381 if(rate < 0){
382 rate = -rate;
383 }
384 }
385
386 switch(type){
387 case MEASURE_ADC:
388 printf("ADC data:\n\r");
389
390 break;
391
392 default:
393 break;
394 }
395
396 newLine();
397}
398
405
406}
407
414 if(strcmp(cmd[1], "read") == 0 || strcmp(cmd[1], "-read") == 0){
415 printf("Periodic Time: %d\n\r", OS_ReadMsTime());
416 }else if(strcmp(cmd[1], "clear") == 0 || strcmp(cmd[1], "-clear") == 0){
418 printf("Periodic Cleared.\n\r");
419 }
420}
421
428 if(strcmp(cmd[1], "red") == 0 || strcmp(cmd[1], "-red") == 0){
429 printf("Togglin RED LED");
431 }else if(strcmp(cmd[1], "blue") == 0 || strcmp(cmd[1], "-blue") == 0){
432 printf("Togglin BLUE LED");
434 }else if(strcmp(cmd[1], "clear") == 0 || strcmp(cmd[1], "-clear") == 0){
435 printf("Togglin GREEN LED");
437 }
438
439
440}
441
442
443
#define GREEN_LED
Definition: cpu.h:23
#define RED_LED
Definition: cpu.h:27
#define BLUE_BLINK
Definition: cpu.h:26
#define GREEN_BLINK
Definition: cpu.h:24
#define BLUE_LED
Definition: cpu.h:25
#define RED_BLINK
Definition: cpu.h:28
void commandOS(char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:413
void printGenericMenu(void)
Definition: Interpreter.c:248
void commandDisplay(char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:404
int32_t findCommandType(char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:144
void Interpreter(void)
Definition: Interpreter.c:51
void commandMeasure(char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:349
void printHelpMenu(void)
Definition: Interpreter.c:262
#define CMD_HELP
Definition: Interpreter.c:15
#define CMD_LED
Definition: Interpreter.c:19
void printWelcomeMenu(void)
Definition: Interpreter.c:231
char * readline(char *str)
Definition: Interpreter.c:71
#define CMD_OS
Definition: Interpreter.c:20
void executeCommand(int32_t cmdType, char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:184
#define CMD_MENU
Definition: Interpreter.c:14
#define CMD_ECHO
Definition: Interpreter.c:16
void commandLED(char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:427
void newLine(void)
Definition: Interpreter.c:43
#define CMD_DISPLAY
Definition: Interpreter.c:18
#define CMD_MEASURE
Definition: Interpreter.c:17
void parseString(char *str, char cmd[MAX_WORDS][MAX_CHARS_PER_WORD])
Definition: Interpreter.c:104
#define MEASURE_ADC
Definition: Interpreter.c:23
Runs on TM4C123 Command line interface.
#define MAX_WORDS
Definition: Interpreter.h:20
#define MAX_CHARS_PER_WORD
Definition: Interpreter.h:21
Functions for OS.
INT32U OS_ReadMsTime(void)
Definition: OS.c:767
void OS_ClearMsTime(void)
Definition: OS.c:760
char UART_InChar(void)
Definition: UART0.c:115
void UART_OutChar(char data)
Definition: UART0.c:122
void UART_Init(void)
void OutCRLF(void)
Definition: UART0.c:316