Restructure
[liquid-crystal-terminal.git] / terminal / term.ino
1 #include "term.h"
2
3 void redraw(struct terminal *term)
4 {
5 lcd.noCursor();
6 lcd.clear();
7 lcd.home();
8 for (byte i = 0; i < COLS * ROWS; i++) {
9 if (i % COLS == 0)
10 lcd.setCursor(i % COLS, i / COLS);
11 lcd.write(term->contents[i]);
12 }
13 lcd.cursor();
14 }
15
16 void scroll_up(struct terminal *term)
17 {
18 for (byte i = 0; i < COLS * (ROWS - 1); i++)
19 term->contents[i] = term->contents[i + COLS];
20 for (byte i = COLS * (ROWS - 1); i < COLS * ROWS; i++)
21 term->contents[i] = ' ';
22 redraw(term);
23 }
24
25 /**
26 * Special character meanings are defined here.
27 */
28 void write(struct terminal *term, char ch)
29 {
30 switch (ch) {
31 case '\r':
32 term->x = 0;
33 lcd.setCursor(term->x, term->y);
34 break;
35 case '\n':
36 if (++term->y >= ROWS) {
37 term->y--;
38 scroll_up(term);
39 }
40 lcd.setCursor(term->x, term->y);
41 break;
42 case 0x7f:
43 if (term->x != 0) {
44 term->x--;
45 term->contents[term->x + term->y * COLS] = 0x00;
46 lcd.setCursor(term->x, term->y);
47 lcd.write(' ');
48 lcd.setCursor(term->x, term->y);
49 }
50 break;
51 default:
52 term->contents[term->x + term->y * COLS] = ch;
53 lcd.write(ch);
54 if (++term->x >= COLS) {
55 term->x = 0;
56 if (++term->y >= ROWS) {
57 term->y--;
58 scroll_up(term);
59 }
60 lcd.setCursor(term->x, term->y);
61 }
62 }
63 }