OSDN Git Service

Added NT modules.
[bluetank/bluetank.git] / firm / bare_metal / common / ntshell / vtsend.c
1 /**
2  * @file vtsend.c
3  * @author Shinichiro Nakamura
4  * @brief VT100 Senderの実装。
5  */
6
7 /*
8  * ===============================================================
9  *  Natural Tiny Shell (NT-Shell)
10  *  Version 0.0.8
11  * ===============================================================
12  * Copyright (c) 2010-2011 Shinichiro Nakamura
13  *
14  * Permission is hereby granted, free of charge, to any person
15  * obtaining a copy of this software and associated documentation
16  * files (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use,
18  * copy, modify, merge, publish, distribute, sublicense, and/or
19  * sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * The above copyright notice and this permission notice shall be
24  * included in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  * ===============================================================
35  */
36
37 #include "vtsend.h"
38
39 #define ESC (0x1B)
40
41 int vtsend_init(
42         vtsend_t *p, int (*uart_write)(const char *buf, const int siz))
43 {
44     p->uart_write = uart_write;
45     return 0;
46 }
47
48 int vtsend_cursor_position(vtsend_t *p, const int column, const int line)
49 {
50     char buf[8];
51     buf[0] = ESC;
52     buf[1] = '[';
53     buf[2] = '0' + (line / 10);
54     buf[3] = '0' + (line % 10);
55     buf[4] = ';';
56     buf[5] = '0' + (column / 10);
57     buf[6] = '0' + (column % 10);
58     buf[7] = 'H';
59     p->uart_write(buf, 8);
60     return 0;
61 }
62
63 int vtsend_cursor_up(vtsend_t *p, const int n)
64 {
65     char buf[8];
66     buf[0] = ESC;
67     buf[1] = '[';
68     buf[2] = '0' + (n / 10);
69     buf[3] = '0' + (n % 10);
70     buf[4] = 'A';
71     p->uart_write(buf, 5);
72     return 0;
73 }
74
75 int vtsend_cursor_down(vtsend_t *p, const int n)
76 {
77     char buf[8];
78     buf[0] = ESC;
79     buf[1] = '[';
80     buf[2] = '0' + (n / 10);
81     buf[3] = '0' + (n % 10);
82     buf[4] = 'B';
83     p->uart_write(buf, 5);
84     return 0;
85 }
86
87 int vtsend_cursor_forward(vtsend_t *p, const int n)
88 {
89     char buf[8];
90     buf[0] = ESC;
91     buf[1] = '[';
92     buf[2] = '0' + (n / 10);
93     buf[3] = '0' + (n % 10);
94     buf[4] = 'C';
95     p->uart_write(buf, 5);
96     return 0;
97 }
98
99 int vtsend_cursor_backward(vtsend_t *p, const int n)
100 {
101     char buf[8];
102     buf[0] = ESC;
103     buf[1] = '[';
104     buf[2] = '0' + (n / 10);
105     buf[3] = '0' + (n % 10);
106     buf[4] = 'D';
107     p->uart_write(buf, 5);
108     return 0;
109 }
110
111 int vtsend_cursor_position_save(vtsend_t *p)
112 {
113     char buf[8];
114     buf[0] = ESC;
115     buf[1] = '[';
116     buf[2] = 's';
117     p->uart_write(buf, 3);
118     return 0;
119 }
120
121 int vtsend_cursor_position_restore(vtsend_t *p)
122 {
123     char buf[8];
124     buf[0] = ESC;
125     buf[1] = '[';
126     buf[2] = 'u';
127     p->uart_write(buf, 3);
128     return 0;
129 }
130
131 int vtsend_erase_display(vtsend_t *p)
132 {
133     char buf[8];
134     buf[0] = ESC;
135     buf[1] = '[';
136     buf[2] = '2';
137     buf[3] = 'J';
138     p->uart_write(buf, 4);
139     return 0;
140 }
141
142 int vtsend_erase_line(vtsend_t *p)
143 {
144     char buf[8];
145     buf[0] = ESC;
146     buf[1] = '[';
147     buf[2] = 'K';
148     p->uart_write(buf, 3);
149     return 0;
150 }
151
152 int vtsend_set_color_foreground(vtsend_t *p, const int color)
153 {
154     char buf[8];
155     buf[0] = ESC;
156     buf[1] = '[';
157     buf[2] = '0' + ((30 + color) / 10);
158     buf[3] = '0' + ((30 + color) % 10);
159     buf[4] = 'm';
160     p->uart_write(buf, 5);
161     return 0;
162 }
163
164 int vtsend_set_color_background(vtsend_t *p, const int color)
165 {
166     char buf[8];
167     buf[0] = ESC;
168     buf[1] = '[';
169     buf[2] = '0' + ((40 + color) / 10);
170     buf[3] = '0' + ((40 + color) % 10);
171     buf[4] = 'm';
172     p->uart_write(buf, 5);
173     return 0;
174 }
175
176 int vtsend_set_attribute(vtsend_t *p, const int attr)
177 {
178     char buf[8];
179     buf[0] = ESC;
180     buf[1] = '[';
181     buf[2] = '0' + ((attr) / 10);
182     buf[3] = '0' + ((attr) % 10);
183     buf[4] = 'm';
184     p->uart_write(buf, 5);
185     return 0;
186 }
187
188 int vtsend_set_scroll_region(vtsend_t *p, const int top, const int bottom)
189 {
190     char buf[8];
191     buf[0] = ESC;
192     buf[1] = '[';
193     buf[2] = '0' + (top / 10);
194     buf[3] = '0' + (top % 10);
195     buf[4] = ';';
196     buf[5] = '0' + (bottom / 10);
197     buf[6] = '0' + (bottom % 10);
198     buf[7] = 'r';
199     p->uart_write(buf, 8);
200     return 0;
201 }
202
203 int vtsend_set_cursor(vtsend_t *p, const int visible)
204 {
205     if (visible) {
206         char buf[8];
207         buf[0] = ESC;
208         buf[1] = '[';
209         buf[2] = '?';
210         buf[3] = '2';
211         buf[4] = '5';
212         buf[5] = 'h';
213         p->uart_write(buf, 6);
214     } else {
215         char buf[8];
216         buf[0] = ESC;
217         buf[1] = '[';
218         buf[2] = '?';
219         buf[3] = '2';
220         buf[4] = '5';
221         buf[5] = 'l';
222         p->uart_write(buf, 6);
223     }
224     return 0;
225 }
226
227 int vtsend_reset(vtsend_t *p)
228 {
229     char buf[2];
230     buf[0] = ESC;
231     buf[1] = 'c';
232     p->uart_write(buf, 2);
233     return 0;
234 }
235
236 int vtsend_draw_box(
237         vtsend_t *p,
238         const int x1, const int y1, const int x2, const int y2)
239 {
240     int i;
241
242     vtsend_cursor_position(p, x1, y1);
243     for (i = x1; i <= x2; i++) {
244         p->uart_write(" ", 1);
245     }
246     vtsend_cursor_position(p, x1, y2);
247     for (i = x1; i <= x2; i++) {
248         p->uart_write(" ", 1);
249     }
250     for (i = y1; i <= y2; i++) {
251         vtsend_cursor_position(p, x1, i);
252         p->uart_write(" ", 1);
253         vtsend_cursor_position(p, x2, i);
254         p->uart_write(" ", 1);
255     }
256     return 0;
257 }
258
259 int vtsend_fill_box(
260         vtsend_t *p,
261         const int x1, const int y1, const int x2, const int y2)
262 {
263     int i, j;
264     for (i = y1; i <= y2; i++) {
265         vtsend_cursor_position(p, x1, i);
266         for (j = x1; j <= x2; j++) {
267             p->uart_write(" ", 1);
268         }
269     }
270     return 0;
271 }
272