blob: ade6d4e76e611d72903e3519e5a9f46c41ea4c09 [file] [log] [blame]
zongqiang.zhang0c6a0882019-08-07 14:48:21 +08001/**
2 \file text_tiny.c
3 \brief Functions relating to using tiny 5x7 text fonts.
4 \author Andy Gock
5 */
6
7/*
8 Copyright (c) 2012, Andy Gock
9
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
19 * Neither the name of Andy Gock nor the
20 names of its contributors may be used to endorse or promote products
21 derived from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#include "glcd.h"
36
37#if defined(GLCD_DEVICE_AVR8)
38void glcd_tiny_set_font(PGM_P font_table, uint8_t width, uint8_t height, char start_char, char end_char)
39#else
40void glcd_tiny_set_font(const char * font_table, uint8_t width, uint8_t height, char start_char, char end_char)
41#endif
42{
43 font_current.font_table = font_table;
44 font_current.width = width;
45 font_current.height = height;
46 font_current.start_char = start_char;
47 font_current.end_char = end_char;
48 font_current.table_type = STANG;
49}
50
51void glcd_tiny_draw_char(uint8_t x, uint8_t line, char c)
52{
53 uint8_t i;
54
55 /* Only works for fonts < 8 bits in height */
56 if (font_current.height >= 8) {
57 return;
58 }
59 if (c < font_current.start_char || c > font_current.end_char) {
60 c = '.';
61 }
62 if ( line >= GLCD_LCD_HEIGHT / (font_current.height + 1) ) {
63 return;
64 }
65 if ( (x+font_current.width) >= GLCD_LCD_WIDTH ) {
66 return;
67 }
68
69 glcd_update_bbox(x, line*(font_current.height + 1), x+font_current.width, line*(font_current.height + 1) + (font_current.height + 1));
70
71 for ( i = 0; i < font_current.width; i++ ) {
72#if defined(GLCD_DEVICE_AVR8)
73 glcd_buffer_selected[x + (line * GLCD_LCD_WIDTH)] = pgm_read_byte( font_current.font_table + ((c - font_current.start_char) * (font_current.width)) + i );
74#else
75 glcd_buffer_selected[x + (line * GLCD_LCD_WIDTH)] = *( font_current.font_table + ((c - font_current.start_char) * (font_current.width)) + i );
76#endif
77 x++;
78 }
79}
80
81void glcd_tiny_draw_string(uint8_t x, uint8_t line, char *str)
82{
83 if (font_current.height >= 8) {
84 return;
85 }
86 while (*str) {
87 glcd_tiny_draw_char(x, line, *str++);
88 x += (font_current.width + 1);
89 if ((x + font_current.width + 1) > GLCD_LCD_WIDTH) {
90 x = 0; /* Ran out of this line */
91 line++;
92 }
93 if (line >= (GLCD_LCD_HEIGHT/(font_current.height + 1)))
94 return; /* Ran out of space :( */
95 }
96}
97
98#if defined(GLCD_DEVICE_AVR8)
99void glcd_tiny_draw_string_P(uint8_t x, uint8_t line, PGM_P str)
100#else
101void glcd_tiny_draw_string_P(uint8_t x, uint8_t line, const char *str)
102#endif
103{
104 if (font_current.height >= 8) {
105 return;
106 }
107 while (1) {
108#if defined(GLCD_DEVICE_AVR8)
109 char c = pgm_read_byte(str++);
110#else
111 char c = *(str++);
112#endif
113 if (!c)
114 return;
115
116 glcd_tiny_draw_char(x, line, c);
117
118 x += (font_current.width + 1);
119 if ((x + font_current.width + 1) > GLCD_LCD_WIDTH) {
120 x = 0; /* Ran out of this line */
121 line++;
122 }
123 if (line >= (GLCD_LCD_HEIGHT/(font_current.height + 1)))
124 return; /* Ran out of space :( */
125 }
126}
127
128void glcd_tiny_draw_string_ammend(char *str) {
129 glcd_scroll_line();
130 glcd_tiny_draw_string(0, (GLCD_LCD_HEIGHT/8-1), str);
131 glcd_write();
132}
133
134void glcd_tiny_draw_string_ammend_P(const char *str) {
135 glcd_scroll_line();
136 glcd_tiny_draw_string_P(0, (GLCD_LCD_HEIGHT/8-1), str);
137 glcd_write();
138}
139
140void glcd_tiny_invert_line(uint8_t line)
141{
142 glcd_invert_area(0,line*8,GLCD_LCD_WIDTH-1,8);
143}
144
145void glcd_tiny_draw_char_xy(uint8_t x, uint8_t y, char c)
146{
147 uint8_t i;
148 uint8_t xvar, yvar;
149 uint8_t dat;
150
151 /* Only works for fonts < 8 bits in height */
152
153 /* Check all important bounds requirements are okay */
154 if ( (y >= GLCD_LCD_HEIGHT) || ((x+font_current.width) >= GLCD_LCD_WIDTH) || (font_current.height >= 8) || font_current.table_type != STANG) {
155 return;
156 }
157 if (c < font_current.start_char || c > font_current.end_char) {
158 c = '.';
159 }
160
161 xvar = x;
162
163 for ( i = 0; i < font_current.width; i++ ) {
164#if defined(GLCD_DEVICE_AVR8)
165 dat = pgm_read_byte( font_current.font_table + ((c - font_current.start_char) * (font_current.width)) + i );
166#else
167 dat = *( font_current.font_table + ((c - font_current.start_char) * (font_current.width)) + i );
168#endif
169 for (yvar = 0; yvar < font_current.height; yvar++) {
170 glcd_set_pixel(xvar,y+yvar, (dat & (1<<yvar) ? 1 : 0) );
171 }
172 xvar++;
173 }
174
175 glcd_update_bbox(x, y, x+font_current.width,y+font_current.height);
176
177}