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
|
#include <clock.h>
#include <task.h>
#include <display_draw.h>
#include <heap.h>
#include <string.h>
#define WIDTH 40
#define HEIGHT 18
#define TTY_COUNT 2
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 i = 0; i < WIDTH; i++) {
//unsigned int zc = 0;
for (unsigned int j = 0; j < HEIGHT; j++) {
int c = text_current->buf[i + j * WIDTH];
/*if (c == '\0') {
if (++zc == 3)
break;
} else if (zc > 0) {
zc = 0;
}*/
dsp_putchar(c, i, j);
}
}
}
void text_switch(unsigned int i)
{
if (i >= TTY_COUNT)
return;
text_current = &text_tty[i];
text_redraw();
}
void text_clear(void)
{
for (unsigned int i = 0; i < WIDTH; i++) {
for (unsigned int j = 0; j < HEIGHT; j++) {
text_current->buf[i + j * WIDTH] = 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;
text_redraw();
}
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;
}
|