GLCD Library
A C Library for Embedded Applications
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
text.c
Go to the documentation of this file.
1 
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 extern uint8_t *glcd_buffer_selected;
39 
41 
42 #if defined(GLCD_DEVICE_AVR8)
43 void glcd_set_font(PGM_P font_table, uint8_t width, uint8_t height, char start_char, char end_char)
44 #else
45 void glcd_set_font(const char * font_table, uint8_t width, uint8_t height, char start_char, char end_char)
46 #endif
47 {
48  // supports variable width fonts
49  font_current.font_table = font_table;
50  font_current.width = width;
51  font_current.height = height;
52  font_current.start_char = start_char;
53  font_current.end_char = end_char;
54  font_current.table_type = MIKRO; // only supports MikroElektronika generated format
55 }
56 
57 uint8_t glcd_draw_char_xy(uint8_t x, uint8_t y, char c)
58 {
59  if (c < font_current.start_char || c > font_current.end_char) {
60  c = '.';
61  }
62 
63  if (font_current.table_type == STANG) {
64  // font table in Pascal Stang format (single byte height with with no width specifier
65 
66  uint8_t i;
67  for ( i = 0; i < font_current.width; i++ ) {
68 #if defined(GLCD_DEVICE_AVR8)
69  uint8_t dat = pgm_read_byte( font_current.font_table + ((c - font_current.start_char) * (font_current.width)) + i );
70 #else
71  uint8_t dat = *( font_current.font_table + ((c - font_current.start_char) * (font_current.width)) + i );
72 #endif
73  uint8_t j;
74  for (j = 0; j < 8; j++) {
75  if (x+i >= GLCD_LCD_WIDTH || y+j >= GLCD_LCD_HEIGHT) {
76  return 0;
77  }
78  if (dat & (1<<j)) {
79  glcd_set_pixel(x+i,y+j,BLACK);
80  } else {
81  glcd_set_pixel(x+i,y+j,WHITE);
82  }
83  }
84  }
85  return font_current.width;
86 
87  } else if (font_current.table_type == MIKRO) {
88  // font table in MikroElecktronica format
89 
90  uint8_t i;
91  uint8_t var_width;
92 
93  uint8_t bytes_high = font_current.height / 8 + 1;
94  uint8_t bytes_per_char = font_current.width * bytes_high + 1;
95 
96  const char *p;
97  p = font_current.font_table + (c - font_current.start_char) * bytes_per_char;
98 
99 #if defined(GLCD_DEVICE_AVR8)
100  var_width = pgm_read_byte(p);
101 #else
102  var_width = *p;
103 #endif
104  p++; // step over the variable width field
105 
106  /*
107  if (x+var_width >= GLCD_LCD_WIDTH || y+font_current.height >= GLCD_LCD_HEIGHT) {
108  return;
109  }
110  */
111 
112 
113  for ( i = 0; i < var_width; i++ ) {
114  uint8_t j;
115  for ( j = 0; j < bytes_high; j++ ) {
116 #if defined(GLCD_DEVICE_AVR8)
117  uint8_t dat = pgm_read_byte( p + i*bytes_high + j );
118 #else
119  uint8_t dat = *( p + i*bytes_high + j );
120 #endif
121  uint8_t bit;
122  for (bit = 0; bit < 8; bit++) {
123 
124  if (x+i >= GLCD_LCD_WIDTH || y+j*8+bit >= GLCD_LCD_HEIGHT) {
125  return 0;
126  }
127 
128  if (dat & (1<<bit)) {
129  glcd_set_pixel(x+i,y+j*8+bit,BLACK);
130  } else {
131  glcd_set_pixel(x+i,y+j*8+bit,WHITE);
132  }
133  }
134  }
135  }
136  return var_width;
137 
138  } else {
139  // don't recognise the font table
140  return 0;
141  }
142 
143 }
144 
145 void glcd_draw_string_xy(uint8_t x, uint8_t y, char *c)
146 {
147  uint8_t width;
148 
149  if (y > (GLCD_LCD_HEIGHT - font_current.height - 1)) {
150  // character won't fit
151  return;
152  }
153 
154  while (*c) {
155  width = glcd_draw_char_xy(x,y,*c);
156  x += (width + 1);
157  // ignore section - up to user to make sure text does not overflow display width
158  //if (x+width >= GLCD_LCD_WIDTH) {
159  // return;
160  //}
161  c++;
162  }
163 }
164 
165 void glcd_draw_string_xy_P(uint8_t x, uint8_t y, const char *str)
166 {
167  uint8_t width;
168 
169  if (y > (GLCD_LCD_HEIGHT - font_current.height - 1)) {
170  // character won't fit
171  return;
172  }
173 
174  while (1) {
175 #if defined(GLCD_DEVICE_AVR8)
176  char c = pgm_read_byte(str++);
177 #else
178  char c = *(str++);
179 #endif
180  if (!c)
181  return;
182 
183  width = glcd_draw_char_xy(x,y,c);
184  x += (width + 1);
185  // ignore section - up to user to make sure text does not overflow display width
186  //if (x+width >= GLCD_LCD_WIDTH) {
187  // return;
188  //}
189  c++;
190  }
191 }
192 
void glcd_draw_string_xy(uint8_t x, uint8_t y, char *c)
Draw a string at specified location.
Definition: text.c:145
#define GLCD_LCD_WIDTH
User specified GLCD width in pixels Set to 0 for automatic assignment based on controller.
Definition: glcd.h:140
char end_char
Definition: glcd.h:258
uint8_t width
Definition: glcd.h:255
glcd_BoundingBox_t * glcd_bbox_selected
Pointer to bounding box currently in use.
Definition: glcd.c:65
glcd_FontConfig_t font_current
Definition: text.c:40
Bounding box for pixels that need to be updated.
Definition: glcd.h:162
uint8_t glcd_draw_char_xy(uint8_t x, uint8_t y, char c)
Draw a char at specified location.
Definition: text.c:57
Definition: glcd.h:251
const char * font_table
Definition: glcd.h:254
#define BLACK
Definition: glcd.h:105
#define GLCD_LCD_HEIGHT
User specified GLCD height in pixels Set to 0 for automatic assignment based on controller.
Definition: glcd.h:141
uint8_t * glcd_buffer_selected
Pointer to screen buffer currently in use.
Definition: glcd.c:60
enum font_table_type table_type
Definition: glcd.h:259
Definition: glcd.h:251
void glcd_set_font(const char *font_table, uint8_t width, uint8_t height, char start_char, char end_char)
Set GLCD font to predefined font table.
Definition: text.c:45
void glcd_set_pixel(uint8_t x, uint8_t y, uint8_t color)
Set pixel to specified colour.
Definition: graphics.c:46
#define WHITE
Definition: glcd.h:106
char start_char
Definition: glcd.h:257
uint8_t height
Definition: glcd.h:256
GLCD Library main header file.
void glcd_draw_string_xy_P(uint8_t x, uint8_t y, const char *str)
Draw a string from program memory at specified location.
Definition: text.c:165