imme-dongle.tar.gz
[imme-dongle] / linux / console.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include "console.h"
4 #include "tty_posix.h"
5
6 #define CMDBUF_SIZ 255
7
8 #if CMDBUF_SIZ > 255
9 #error Commands over 255 bytes not supported, change type of cmdbuf_len
10 #endif
11
12 /**************************************************************/
13
14 // CONSOLE_MODE_LINE
15 static uint8_t cmdbuf_len; // number of bytes in command buf
16 static uint8_t cmdbuf[CMDBUF_SIZ+1];    // null terminator space
17 static bool got_line = false;
18
19 static void prompt(void)
20 {
21     console_puts("> ");
22 }
23
24 void console_init(void)
25 {
26     prompt();
27 }
28
29 bool console_rx_ready_callback(void)
30 {
31     return !got_line;
32 }
33
34 void console_rx_callback(uint8_t c)
35 {
36     if (got_line)   // throw away chars until the line is handled
37         return;
38
39     switch(c)
40     {
41         case 0x0D:
42         //case '\r':
43             got_line = true;
44             console_newline();
45             break;
46         case '\b':  // backspace
47         case 0x7F:  // del
48             if (cmdbuf_len > 0)
49             {
50                 cmdbuf_len--;
51                 console_putc('\b');
52                 console_putc(' ');
53                 console_putc('\b');
54             }
55             break;
56         default:
57             if (cmdbuf_len < sizeof(cmdbuf)-1)
58             {
59                 console_putc(c);
60                 cmdbuf[cmdbuf_len++] = c;
61             }
62             else
63                 console_putc('\a');  // bell
64             break;
65     }
66 }
67
68 void console_tick(void)
69 {
70     if (got_line)
71     {
72         if (cmdbuf_len > 0)
73         {
74             cmdbuf[cmdbuf_len] = 0; // terminate it
75             shell_eval((char *)cmdbuf);
76         }
77         cmdbuf_len = 0;
78         prompt();
79         got_line = false;
80     }
81 }
82
83 void console_putc(uint8_t c)
84 {
85     tty_putc(c);
86 }
87
88 void console_newline(void)
89 {
90     console_putc('\r');
91     console_putc('\n');
92 }
93
94 void console_puts(char *str)
95 {
96     while(*str!=0)
97         console_putc(*str++);
98 }
99
100 static char nibble_to_char(uint8_t nibble)
101 {
102     if (nibble < 0xA)
103         return nibble + '0';
104     return nibble - 0xA + 'A';
105 }
106
107 void console_puthex8(uint8_t h)
108 {
109     console_putc(nibble_to_char((h & 0xF0)>>4));
110     console_putc(nibble_to_char(h & 0x0F));
111 }
112
113 void console_puthex16(uint16_t h)
114 {
115     console_puthex8((h & 0xFF00) >> 8);
116     console_puthex8(h & 0xFF);
117 }
118
119 void console_put0x8(uint8_t h)
120 {
121     console_putc('0');
122     console_putc('x');
123     console_puthex8(h);
124 }
125
126 void console_putsmem(const uint8_t *a, const uint8_t *b)
127 {
128     while(a != b)
129     {
130         console_putc(*a);
131         a++;
132     }
133 }
134
135 void console_putdec(uint32_t n)
136 {
137     uint32_t m;
138     bool in_leading_zeroes = true;
139
140     for (m = 1000000000; m != 1; m/=10)
141     {
142         if ((n / m) != 0)
143             in_leading_zeroes = false;
144         if (!in_leading_zeroes)
145             console_putc(nibble_to_char(n / m));
146         n = n % m;
147     }
148     console_putc(nibble_to_char(n));
149 }
150
151 void console_putbin(uint8_t b)
152 {
153     console_putc('b');
154     (b & 0x80) ? console_putc('1') : console_putc('0');
155     (b & 0x40) ? console_putc('1') : console_putc('0');
156     (b & 0x20) ? console_putc('1') : console_putc('0');
157     (b & 0x10) ? console_putc('1') : console_putc('0');
158     (b & 0x08) ? console_putc('1') : console_putc('0');
159     (b & 0x04) ? console_putc('1') : console_putc('0');
160     (b & 0x02) ? console_putc('1') : console_putc('0');
161     (b & 0x01) ? console_putc('1') : console_putc('0');
162 }
163