aboutsummaryrefslogtreecommitdiffstats
path: root/src/display_text.c
blob: 880485e344cea253aaf9b9c81d43840836d59794 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * @file display_text.c
 * A text-rendering/managing library to work with the display
 *
 * Copyright (C) 2018 Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <clock.h>
#include <task.h>
#include <display_draw.h>
#include <heap.h>
#include <string.h>

#define WIDTH  40
#define HEIGHT 18

typedef struct {
	char *buf;
	uint8_t x;
	uint8_t y;
} tty_t;

static tty_t text_tty[TTY_COUNT];
static tty_t *text_current;
static uint8_t text_cursor;

void task_cursor(void)
{
	while (1) {
		delay(300);
		if (text_cursor == 0)
			continue;

		int x = text_current->x * C_WIDTH;
		int y = text_current->y * C_HEIGHT;
		dsp_rect(x, y + C_HEIGHT - 1, C_WIDTH, 1, 0xFFFF);
		delay(300);
		dsp_rect(x, y + C_HEIGHT - 1, C_WIDTH, 1, 0);
	}
}

void text_init(void)
{
	for (int i = 0; i < TTY_COUNT; i++) {
		text_tty[i].buf = calloc(WIDTH * HEIGHT, sizeof(char));
		text_tty[i].x = 0;
		text_tty[i].y = 0;
	}

	text_current = &text_tty[0];

	text_cursor = 1;
	task_start(task_cursor, 512);
}

void text_redraw(void)
{
	for (unsigned int j = 0; j < HEIGHT; j++) {
		unsigned int i = WIDTH;
		//for (; i > 0 && text_current->buf[j * WIDTH + i - 1] == '\0'; i--);
		for (; i > 0; i--) {
			int c = text_current->buf[j * WIDTH + i - 1];
			dsp_putchar(c, i - 1, j);
		}
	}
}

void text_switch(unsigned int i)
{
	if (i >= TTY_COUNT || text_current == text_tty + i)
		return;

	text_current = &text_tty[i];
	text_redraw();
}

void text_clear(void)
{

	for (unsigned int j = 0; j < HEIGHT; j++) {
		for (unsigned int i = 0; i < WIDTH; i++) {
			//int c = text_current->buf[j * WIDTH + 1];
			//if (c != 0) {
				text_current->buf[j * WIDTH + i] = 0;
				dsp_putchar(' ', i, j);
			//}
		}
	}
	text_current->x = 0;
	text_current->y = 0;
}

void text_putchar(int c)
{
	uint8_t x = text_current->x;
	uint8_t y = text_current->y;

	switch (c) {
	case '\n':
		y++;
		// fall_through
	case '\r':
		x = 0;
		break;
	case '\t':
		for (unsigned int i = 0; i < 4; i++)
			dsp_putchar(' ', x++, y);
		break;
	case '\b':
		x--;
		dsp_putchar(' ', x, y);
		break;
	default:
		dsp_putchar(c, x, y);
		text_current->buf[x + y * WIDTH] = c;
		x++;
		break;
	}

	if (x >= WIDTH) {
		x = 0;
		y++;
	}

	if (y >= HEIGHT) {
		// clear
		for (int i = 1; i < HEIGHT; i++) {
			memcpy(text_current->buf + (i - 1) * WIDTH,
				text_current->buf + i * WIDTH, WIDTH);
		}
		x = 0;
		y = HEIGHT - 1;
		for (int i = 0; i < WIDTH; i++)
			text_current->buf[i + y * WIDTH] = 0;

		for (int j = HEIGHT - 1; j >= 0; j--) {
			int i = WIDTH - 1;
			if (j > 0) {
				for (; i >= 0; i--) {
					int p = text_current->buf[(j - 1) * WIDTH + i];
					int c = text_current->buf[j * WIDTH + i];
					if (p != '\0' || c != '\0')
						break;
				}
			}
			for (; i >= 0; i--) {
				int c = text_current->buf[j * WIDTH + i];
				dsp_putchar(c, i, j);
			}
		}
	}

	text_current->x = x;
	text_current->y = y;
}

void text_puts(const char *s)
{
	unsigned int i = 0;
	while (s[i])
		text_putchar(s[i++]);
}

void text_setpos(uint8_t x, uint8_t y)
{
	if (x >= WIDTH)
		x = WIDTH - 1;
	if (y >= HEIGHT)
		y = HEIGHT - 1;
	text_current->x = x;
	text_current->y = y;
}

void text_relpos(int8_t x, int8_t y)
{
	if ((int)(text_current->x + x) < 0)
		text_current->x = 0;
	else
		text_current->x += x;
	if ((int)(text_current->y + y) < 0)
		text_current->y = 0;
	else
		text_current->y += y;
}