tested working terminal and reordering of code
[cortex-from-scratch] / lib / stdio.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include <lib/stdio.h>
6 #include <lib/string.h>
7 #include <drivers/uart.h>
8
9 #define SERIAL 1
10 #define TFT 0
11 #define BUFSIZE 256
12
13 static char stdbuf[BUFSIZE];
14
15 /* Abstraction layer for I/O communication */
16
17 char getchar(void) {
18         char c;
19         if (SERIAL) {
20                  while ((c = uart_getc()) == 0);
21                 /* do nothing */
22         }
23         return c;
24 }
25
26 void cputchar(char c) {
27
28         if (SERIAL) {
29                 uart_putc(c);
30         }
31
32 }
33
34 void cputs(unsigned char *str) {
35      
36      int i;
37      for (i = 0; i < strlen(str); i++)     {
38          cputchar(str[i]);
39     }
40
41 }
42
43 char * readline(char *prompt)
44 {
45         int i, c, echoing;
46
47         if (prompt != NULL)
48                 cputs(prompt); 
49
50         i = 0;
51         echoing = 1;
52         while (1) {
53                 c = getchar();
54                 if (c < 0) {
55                         cputs("read error");
56                         return NULL;
57                 } else if ((c == '\b' || c == '\x7f') && i > 0) {
58                         if (echoing)
59                                 cputchar('\b');
60                         i--;
61                 } else if (c >= ' ' && i < BUFSIZE-1) {
62                         if (echoing)
63                                 cputchar(c);
64                         stdbuf[i++] = c;
65                 } else if (c == '\n' || c == '\r') {
66                         if (echoing)
67                                 cputchar('\n');
68                         stdbuf[i] = 0;
69                         return stdbuf;
70                 }
71         }
72 }