aboutsummaryrefslogtreecommitdiff
path: root/src/tty.c
blob: f9413b462394505a808c52795881149740c71d19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <tft.h>
#include <zeta.h>
#include <tty.h>

struct color {
	uint8_t r, g, b;
};

static unsigned int row = 0;
static unsigned int col = 0;

// Index of first row inside buf
static volatile uint8_t head = 0;

// TTY's current contents
static volatile uint8_t buf[TTY_HEIGHT][TTY_WIDTH];

static volatile struct color bg_color = {0x00, 0x00, 0x00};
static volatile struct color fg_color = {0xFF, 0xFB, 0x00};

extern const uint8_t font[97][8];


static uint8_t cursor = 0;
static uint8_t timer = 0;

void
blink_cursor(void) __critical __interrupt(3)
{
	if (++timer >= 75) {
		uint8_t i;

		tft_set_area(8 * col, 8 * row, 8, 8);
		tft_ram_wr();

		if ((cursor ^= 1)) {
			for (i = 0; i < 64; ++i)
				tft_pixel(fg_color.r, fg_color.g, fg_color.b);
		} else {
			for (i = 0; i < 64; ++i)
				tft_pixel(bg_color.r, bg_color.g, bg_color.b);
		}
		timer = 0;
	}
}

static void
draw(u8 c)
{
	u8 i, j;
	const u8 *chr;

	if (c < ' ')
		return;

	chr = font[c - ' '];

	tft_ram_wr();

	for (i = 0; i < 8; ++i) {
		u8 row = chr[i];
		for (j = 0; j < 8; ++j) {
			if (row & 1)
				tft_pixel(fg_color.r, fg_color.g, fg_color.b);
			else
				tft_pixel(bg_color.r, bg_color.g, bg_color.b);
			row >>= 1;
		}
	}
}

static void
scroll(void)
{
	uint16_t i, j;
	head = (head + 1) % TTY_HEIGHT;

	for (i = head; i < TTY_HEIGHT; ++i) {
		for (j = 0; j < TTY_WIDTH; ++j) {
			tft_set_area(8 * j, 8 * (i - head), 8, 8);
			draw(buf[i][j]);
		}
	}

	for (i = 0; i < head - 1; ++i) {
		for (j = 0; j < TTY_WIDTH; ++j) {
			tft_set_area(8 * j, 8 * (TTY_HEIGHT - head + i), 8, 8);
			draw(buf[i][j]);
		}
	}

	for (j = 0; j < TTY_WIDTH; ++j) {
		tft_set_area(8 * j, 8 * (TTY_HEIGHT - 1), 8, 8);
		draw(' ');
	}
}

static void
newline(void)
{
	if (++row >= TTY_HEIGHT) {
		row = TTY_HEIGHT - 1;
		scroll();
	}
}

static void
advance(void)
{
	if (++col >= TTY_WIDTH) {
		newline();
		col = 0;
	}
}

void
addch(char c)
{
	switch (c) {
	case '\b':
		if (col != 0)
			--col;
		return;

	case '\r':
		col = 0;
		return;

	case '\n':
		newline();
		return;
	}

	tft_set_area(8 * col, 8 * row, 8, 8);

	draw(c);

	buf[(head + row) % TTY_HEIGHT][col] = c;
	advance();
}

void
setcur(unsigned int ncol, unsigned int nrow)
{
	col = ncol;
	row = nrow;
}

void
swap_colors(void)
{
	struct color tmp = {fg_color.r, fg_color.g, fg_color.b};
	fg_color = bg_color;
	bg_color = tmp;
}

void
addstr(const char *str)
{
	while (*str) {
		addch(*str);
		++str;
	}
}