Merge branch 'master' of github.com:RfidResearchGroup/proxmark3
[RRG-proxmark3.git] / armsrc / vtsend.c
blobf07793cced400f6327bb5beddbbdf9c1c8b3f106
1 /**
2 * @file vtsend.c
3 * @author CuBeatSystems
4 * @author Shinichiro Nakamura
5 * @copyright
6 * ===============================================================
7 * Natural Tiny Shell (NT-Shell) Version 0.3.1
8 * ===============================================================
9 * Copyright (c) 2010-2016 Shinichiro Nakamura
11 * Permission is hereby granted, free of charge, to any person
12 * obtaining a copy of this software and associated documentation
13 * files (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use,
15 * copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following
18 * conditions:
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
33 #include "vtsend.h"
34 #include "pm3_cmd.h"
35 #include "dbprint.h"
37 #define ESC (0x1B)
38 //#define UART_WRITE(P, BUF, SIZ) (P)->uart_write(BUF, SIZ, (P)->extobj)
39 #define UART_WRITE(BUF) DbprintfEx(FLAG_RAWPRINT, "%s", BUF)
41 int vtsend_init(vtsend_t *p, VTSEND_SERIAL_WRITE uart_write, void *extobj) {
42 p->uart_write = uart_write;
43 p->extobj = extobj;
44 return 0;
47 int vtsend_cursor_position(vtsend_t *p, const int column, const int line) {
48 char buf[1 + 8];
49 buf[0] = ESC;
50 buf[1] = '[';
51 buf[2] = '0' + (line / 10);
52 buf[3] = '0' + (line % 10);
53 buf[4] = ';';
54 buf[5] = '0' + (column / 10);
55 buf[6] = '0' + (column % 10);
56 buf[7] = 'H';
57 buf[8] = '\0';
58 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
59 return 0;
62 int vtsend_cursor_up(vtsend_t *p, const int n) {
63 char buf[1 + 5];
64 buf[0] = ESC;
65 buf[1] = '[';
66 buf[2] = '0' + (n / 10);
67 buf[3] = '0' + (n % 10);
68 buf[4] = 'A';
69 buf[5] = '\0';
71 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
72 return 0;
75 int vtsend_cursor_down(vtsend_t *p, const int n) {
76 char buf[1 + 5];
77 buf[0] = ESC;
78 buf[1] = '[';
79 buf[2] = '0' + (n / 10);
80 buf[3] = '0' + (n % 10);
81 buf[4] = 'B';
82 buf[5] = '\0';
84 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
85 return 0;
88 int vtsend_cursor_forward(vtsend_t *p, const int n) {
89 char buf[1 + 5];
90 buf[0] = ESC;
91 buf[1] = '[';
92 buf[2] = '0' + (n / 10);
93 buf[3] = '0' + (n % 10);
94 buf[4] = 'C';
95 buf[5] = '\0';
97 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
98 return 0;
101 int vtsend_cursor_backward(vtsend_t *p, const int n) {
102 char buf[1 + 5];
103 buf[0] = ESC;
104 buf[1] = '[';
105 buf[2] = '0' + (n / 10);
106 buf[3] = '0' + (n % 10);
107 buf[4] = 'D';
108 buf[5] = '\0';
110 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
111 return 0;
114 int vtsend_cursor_position_save(vtsend_t *p) {
115 char buf[1 + 3];
116 buf[0] = ESC;
117 buf[1] = '[';
118 buf[2] = 's';
119 buf[3] = '\0';
121 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
122 return 0;
125 int vtsend_cursor_position_restore(vtsend_t *p) {
126 char buf[1 + 3];
127 buf[0] = ESC;
128 buf[1] = '[';
129 buf[2] = 'u';
130 buf[3] = '\0';
132 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
133 return 0;
136 int vtsend_erase_display(vtsend_t *p) {
137 char buf[1 + 4];
138 buf[0] = ESC;
139 buf[1] = '[';
140 buf[2] = '2';
141 buf[3] = 'J';
142 buf[4] = '\0';
144 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
145 return 0;
148 int vtsend_erase_line(vtsend_t *p) {
149 char buf[1 + 4];
150 buf[0] = ESC;
151 buf[1] = '[';
152 buf[2] = '2';
153 buf[3] = 'K';
154 buf[4] = '\0';
156 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
157 return 0;
160 int vtsend_set_color_foreground(vtsend_t *p, const int color) {
161 char buf[1 + 5];
162 buf[0] = ESC;
163 buf[1] = '[';
164 buf[2] = '0' + ((30 + color) / 10);
165 buf[3] = '0' + ((30 + color) % 10);
166 buf[4] = 'm';
167 buf[5] = '\0';
169 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
170 return 0;
173 int vtsend_set_color_background(vtsend_t *p, const int color) {
174 char buf[1 + 5];
175 buf[0] = ESC;
176 buf[1] = '[';
177 buf[2] = '0' + ((40 + color) / 10);
178 buf[3] = '0' + ((40 + color) % 10);
179 buf[4] = 'm';
180 buf[5] = '\0';
182 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
183 return 0;
186 int vtsend_set_attribute(vtsend_t *p, const int attr) {
187 char buf[1 + 5];
188 buf[0] = ESC;
189 buf[1] = '[';
190 buf[2] = '0' + ((attr) / 10);
191 buf[3] = '0' + ((attr) % 10);
192 buf[4] = 'm';
193 buf[5] = '\0';
195 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
196 return 0;
199 int vtsend_set_scroll_region(vtsend_t *p, const int top, const int bottom) {
200 char buf[1 + 8];
201 buf[0] = ESC;
202 buf[1] = '[';
203 buf[2] = '0' + (top / 10);
204 buf[3] = '0' + (top % 10);
205 buf[4] = ';';
206 buf[5] = '0' + (bottom / 10);
207 buf[6] = '0' + (bottom % 10);
208 buf[7] = 'r';
209 buf[8] = '\0';
211 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
212 return 0;
215 int vtsend_set_cursor(vtsend_t *p, const int visible) {
216 if (visible) {
217 char buf[1 + 6];
218 buf[0] = ESC;
219 buf[1] = '[';
220 buf[2] = '?';
221 buf[3] = '2';
222 buf[4] = '5';
223 buf[5] = 'h';
224 buf[6] = '\0';
226 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
227 } else {
228 char buf[1 + 6];
229 buf[0] = ESC;
230 buf[1] = '[';
231 buf[2] = '?';
232 buf[3] = '2';
233 buf[4] = '5';
234 buf[5] = 'l';
235 buf[6] = '\0';
237 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
239 return 0;
242 int vtsend_reset(vtsend_t *p) {
243 char buf[1 + 2];
244 buf[0] = ESC;
245 buf[1] = 'c';
246 buf[2] = '\0';
248 UART_WRITE(buf); // UART_WRITE(p, buf, sizeof(buf));
249 return 0;
252 int vtsend_draw_box(vtsend_t *p, const int x1, const int y1, const int x2, const int y2) {
253 int i;
255 vtsend_cursor_position(p, x1, y1);
256 for (i = x1; i <= x2; i++) {
257 UART_WRITE("-");
259 vtsend_cursor_position(p, x1, y2);
260 for (i = x1; i <= x2; i++) {
261 UART_WRITE("-");
263 for (i = y1; i <= y2; i++) {
264 vtsend_cursor_position(p, x1, i);
265 UART_WRITE("|");
266 vtsend_cursor_position(p, x2, i);
267 UART_WRITE("|");
269 return 0;
272 int vtsend_fill_box(vtsend_t *p, const int x1, const int y1, const int x2, const int y2) {
273 int i, j;
274 for (i = y1; i <= y2; i++) {
275 vtsend_cursor_position(p, x1, i);
276 for (j = x1; j <= x2; j++) {
277 UART_WRITE(" ");
280 return 0;