-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.c
263 lines (235 loc) · 5.18 KB
/
text.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright 2014, 2015, 2019 Jacques Deschênes
* This file is part of BP_CHIPCON.
*
* BP_CHIPCON 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.
*
* BP_CHIPCON 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 BP_CHIPCON. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Created: 2014-09-30 22:31:27
* Author: Jacques Desch�nes
* 2019-05-12: Adapted to BP_CHIPCON project
*/
#include "include/timer.h"
#include "include/gen_macros.h"
#include "include/gen_fn.h"
#include "text.h"
#include "tvout.h"
#include "graphics.h"
#include "gamepad.h"
static uint8_t font=FONT_ASCII;
static uint8_t xpos=0, ypos=0;
static console_t console;
#define BG 0 // background
#define FG 1 // foreground
static uint8_t text_colors[2]={0,15};
void font_color(uint8_t color){
text_colors[FG]=color&0xf;
}
void bg_color(uint8_t color){
text_colors[BG]=color&0xf;
}
void select_font(uint8_t font_id){
font=font_id;
}
// get current font id
uint8_t get_font(){
return font;
}
void new_line(){
if (console==LOCAL){
xpos=0;
vmode_params_t *vparams=get_video_params();
switch (font){
case FONT_SHEX:
if ((ypos+SHEX_HEIGHT)<=(vparams->vres-SHEX_HEIGHT+1)){
ypos+=SHEX_HEIGHT;
}else{
gfx_scroll_up(SHEX_HEIGHT);
}
break;
case FONT_LHEX:
if ((ypos+LHEX_HEIGHT)<=(vparams->vres-LHEX_HEIGHT+1)){
ypos+=LHEX_HEIGHT;
}else{
gfx_scroll_up(LHEX_HEIGHT);
}
break;
case FONT_ASCII:
if ((ypos+CHAR_HEIGHT)<=(vparams->vres-CHAR_HEIGHT+1)){
ypos+=CHAR_HEIGHT;
}else{
gfx_scroll_up(CHAR_HEIGHT);
}
break;
}//swtich
}else{
usart_putc(CHN1,'\n');
}
}
static void draw_char(int x,int y,int w, int h, const char* glyph){
register int x0,y0;
uint8_t c,byte,bit,mask;
for (y0=y;y0<(y+h);y0++){
byte=*glyph++;
mask=128;
for (x0=x;x0<(x+w);x0++){
bit=byte&mask;
if (bit)c=text_colors[FG];else c=text_colors[BG];
gfx_blit(x0,y0,c,BIT_SET);
mask>>=1;
}
}
}
void put_char(uint8_t c){
if (console==LOCAL){
switch(font){
case FONT_SHEX:
if (c<16){
draw_char(xpos,ypos,SHEX_WIDTH,SHEX_HEIGHT,&font_hex_4x6[c*SHEX_HEIGHT]);
xpos+=SHEX_WIDTH;
if (xpos>(HRES-SHEX_WIDTH+1)){
new_line();
}
}
break;
case FONT_LHEX:
if (c<16){
draw_char(xpos,ypos,LHEX_WIDTH,LHEX_HEIGHT,&font_hex_8x10[c*LHEX_HEIGHT]);
xpos+=LHEX_WIDTH;
if (xpos>(HRES-LHEX_WIDTH+1)){
new_line();
}
}
break;
case FONT_ASCII:
if ((c>31) && (c<(FONT_SIZE+32))){
draw_char(xpos,ypos,CHAR_WIDTH,CHAR_HEIGHT,&font_6x8[(c-32)*CHAR_HEIGHT]);
xpos+=CHAR_WIDTH;
if (xpos>(HRES-CHAR_WIDTH+1)){
new_line();
}
}
break;
}
}else{
usart_putc(CHN1,c);
}
}
void set_cursor(uint8_t x, uint8_t y){
xpos=x;
ypos=y;
}
uint16_t get_cursor(){
return (xpos<<8)+ypos;
}
void print(const char* str){
char c;
while ((c=*str++)){
switch(c){
case LF:
case CR:
new_line();
break;
case BS:
cursor_left();
break;
default:
put_char(c);
}
}
}
// print text and do crlf()
void println(const char *text){
print(text);
new_line();
}
void print_int(int i,uint8_t base){
char fmt[16],sign=0;
int idx,d;
fmt[15]=0;
fmt[14]=' ';
idx=14;
if (i<0){
sign=1;
i=-i;
}
while (idx>1 && i){
d=i%base;
if (d>9) d+=7;
fmt[--idx]=d+='0';
i/=base;
}
if (idx==14)fmt[--idx]='0';
if (base==10 && sign){
fmt[--idx]='-';
}else if (base==16){
if((16-idx&1))fmt[--idx]='0';
fmt[--idx]='$';
}
print(&fmt[idx]);
}
void cursor_left(){
switch(font){
case FONT_SHEX:
if (xpos-SHEX_WIDTH>=0) xpos-=SHEX_WIDTH;
break;
case FONT_LHEX:
if (xpos-LHEX_WIDTH>=0) xpos-=LHEX_WIDTH;
break;
case FONT_ASCII:
if (xpos-CHAR_WIDTH>=0) xpos-=CHAR_WIDTH;
break;
}
}
void prompt_btn(){
print("press button");
}
void clear_screen(){
if (console==LOCAL){
gfx_cls();
xpos=0;
ypos=0;
}else{
usart_putc(CHN1,FF);
}
}
// affiche un curseur texte
void show_cursor(int show){
int x,y;
uint8_t color=show?15:0;
for (x=xpos;x<xpos+CHAR_WIDTH;x++){
gfx_blit(x,ypos+CHAR_HEIGHT-1,color,BIT_SET);
}
}
// efface la ligne où est le curseur texte
void clear_line(){
int y;
vmode_params_t *vparams=get_video_params();
// set_cursor(0,ypos);
for (y=ypos;y<ypos+CHAR_HEIGHT;y++)
fill(&video_buffer[y*vparams->bpr],vparams->bpr,0);
// while (xpos<(vparams->hres-CHAR_WIDTH))put_char(' ');
// set_cursor(0,ypos-CHAR_HEIGHT);
}
// select text output console
// serial console usefull for debugging
void select_console(console_t con){
console=con;
}
//read a character from serial port
char get_char(){
char c;
while (!(c=usart_getc(CHN1)));
return c;
}