tm1637 signs of life, terminal builtin cmds
[cortex-from-scratch] / term.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include <sys/robsys.h>
6 #include <sys/mmap.h>
7
8 #include <lib/stdio.h>
9 #include <lib/string.h>
10 #include <lib/regfunc.h>
11
12 #include <drivers/led.h>
13
14 #define SERIAL 1
15 #define BUFSIZE 200
16 #define MAXARGS 5
17 #define WHITESPACE "\t\r\n "
18 #define BUILTINCMDS 3
19
20 int help(int, char**);
21
22 /* 
23  * Built in commands
24  *      info -- shows basic info of system
25  *      reset -- software reset
26  *      show [ADDRESS-ADDRESS] -- shows SRAM range
27  *      switchmode -- switch to unprivileged mode
28  * */
29
30 static char buf[BUFSIZE];
31
32
33 struct cmd {
34         char * name;
35         int (*function)(int argc, char ** argsv);
36 };
37
38 struct cmd builtincmds[4];
39
40 int help(int argc, char ** argsv) {
41         sysinfo();
42         return 0;
43 }
44
45 int led(int argc, char ** argsv) {
46
47
48         if (argsv[1] != NULL) {
49                 if (strcmp(argsv[1], "on")) {
50                         cputs("LED ON\n");
51                         led_on();
52                         }
53                 else if (strcmp(argsv[1], "off")) {
54                         cputs("LED OFF\n");
55                         led_off();
56                         }
57         }
58         return 0;
59 }
60
61 int show(int argc, char ** argsv) {
62
63         if ((argsv[1] != NULL) && (strlen(argsv[1]) == 8)) {
64         
65                 uint32_t * check = (uint32_t *) hextoreg(argsv[1]);
66                 cputs("REGISTER 0x");
67                 cputs(argsv[1]);
68                 cputs(" VALUE: ");
69                 cputs(regtohex(*check));
70                 cputchar('\n');
71                 return 1;
72         
73         }
74
75         return 0;
76 }
77
78 int exec_cmd(char * buf) {
79
80         int argc;
81         char *argv[MAXARGS];
82         int i;
83
84         // Parse the command buffer into whitespace-separated arguments
85         argc = 0;
86         argv[argc] = 0;
87         while (1) {
88                 // gobble whitespace
89                 while (*buf && strchr(WHITESPACE, *buf))
90                         *buf++ = 0;
91                 if (*buf == 0)
92                         break;
93
94                 // save and scan past next arg
95                 if (argc == MAXARGS-1) {
96                         cputs("Too many arguments");
97                         return 0;
98                 }
99                 argv[argc++] = buf;
100                 while (*buf && !strchr(WHITESPACE, *buf))
101                         buf++;
102         }
103         argv[argc] = 0;
104
105         // Lookup and invoke the command
106         if (argc == 0)
107                 return 0;
108         for (i = 0; i < BUILTINCMDS; i++) {
109                 if (strcmp(argv[0], builtincmds[i].name))
110                         return builtincmds[i].function(argc, argv);
111         }
112         cputs("Unknown command");
113         return 0;
114
115
116
117 void terminal() {
118
119         builtincmds[0].name = "help";
120         builtincmds[0].function = help;
121
122         builtincmds[1].name = "led";
123         builtincmds[1].function = led;
124
125         builtincmds[2].name = "show";
126         builtincmds[2].function = show;
127
128         char *buf;
129         cputs("WELCOME TO ROBSYS!\n");
130  
131          while (1) {
132                  buf = readline("root# ");
133                         if (buf != NULL)
134                          if (exec_cmd(buf) < 0)
135                                  break;
136          }
137 }
138