-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlcd_draw_font_example.c
144 lines (125 loc) · 3.76 KB
/
lcd_draw_font_example.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
// encoding method: rawbb, 2bpp
static void font_render_engine_rawbb(const font_t *fnt, const font_symbol_t *sym)
{
uint8_t font_width = sym->width;
uint8_t font_height = sym->height;
uint8_t top = sym->margin_top;
uint8_t bottom = font_height - sym->margin_bottom-1;
uint8_t left = sym->margin_left;
uint8_t right = font_width - sym->margin_right-1;
uint16_t bi = sym->bmp_index;
uint8_t h, w;
uint8_t i=0;
for(h=0; h<font_height; h++)
{
for(w=0; w<font_width; w++)
{
if(w < left || w > right || h < top || h > bottom)
{
// debug: lcdsim_write_gram(LCD_BLUE_COLOR);
lcdsim_write_gram(back_color);
}
else
{
uint8_t color_pixel = (fnt->bmp_base[bi] >> i) & 0x03;
if(color_pixel == 3)
{
lcdsim_write_gram(brush_color);
}
else if(color_pixel == 2)
{
lcdsim_write_gram(brush_color66);
}
else if(color_pixel == 1)
{
lcdsim_write_gram(brush_color33);
}
else
{
lcdsim_write_gram(back_color);
}
if(i==6)
{
i = 0;
++bi;
}
else
{
i+=2;
}
}
}
}
}
//=========================================================================
void lcdsim_draw_char(uint16_t x, uint16_t y, const font_t *fnt, utf8_t c)
{
font_symbol_t sym;
if(!fnt->lookup(c, &sym))
{
return;
}
uint8_t font_width = sym.width;
uint8_t font_height = sym.height;
//lcdsim_set_bound(x, y, x+font_width-1, y+font_height-1);
LCD_WR_REG(0X2A);
LCD_WR_DATA( x >> 8 );
LCD_WR_DATA( x & 0xff ); /* column start */
LCD_WR_DATA( (x+fs->width-1) >> 8 ); /* column end */
LCD_WR_DATA( (x+fs->width-1) & 0xff );
LCD_WR_REG(0X2B);
LCD_WR_DATA( y >> 8 ); /* page start */
LCD_WR_DATA( y & 0xff );
LCD_WR_DATA( (y+fs->height-1) >> 8); /* page end */
LCD_WR_DATA( (y+fs->height-1) & 0xff);
LCD_WR_REG(0x2C); /* Write GRAM */
font_render_engine_rawbb(fnt, &sym);
//lcdsim_set_bound(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
/* Release the chop region */
LCD_WR_REG(0X2A); /* column address control set */
LCD_WR_DATA(0);
LCD_WR_DATA(0);
LCD_WR_DATA((lcd_dev.width-1) >>8);
LCD_WR_DATA((lcd_dev.width-1) &0xff);
LCD_WR_REG(0X2B); /* page address control set */
LCD_WR_DATA(0);
LCD_WR_DATA(0);
LCD_WR_DATA((lcd_dev.height-1) >>8);
LCD_WR_DATA((lcd_dev.height-1) &0xff);
}
//=========================================================================
void lcdsim_draw_string(uint16_t x, uint16_t y, const font_t *fnt, const char *s)
{
uint16_t orgx = x;
utf8_t c;
while((c = utf8_getchar(s)) != '\0')
{
if(c == '\r')
{
// no operation
}
else if(c == '\n')
{
y += (fnt->default_height + ROW_CLERANCE_SIZE);
x = orgx;
}
else if(c == ' ')
{
x += fnt->default_width;
}
else
{
lcdsim_draw_char(x, y, fnt, c);
font_symbol_t sym;
if(!fnt->lookup(c, &sym))
{
x += fnt->default_width;
}
else
{
x += sym.width;
}
}
s += utf8_charlen(c);
}
}