]> git.karo-electronics.de Git - oswald.git/blob - ui/oswald_graphics.c
Starting to get rid of borrowed code (LcdDisplay, Fonts), integrate
[oswald.git] / ui / oswald_graphics.c
1 #include "oswald.h"
2 #include "oswald_strings.h"
3 #include "oswald_fonts.h"
4 #include "oswald_hal.h"
5
6 #include "oswald_graphics.h"
7
8 // dummies for MW replacement stuff
9
10 void SetFont(etFontType Type)
11 {
12 }
13
14 u8t WriteLcdString(const uint8_t x, const uint8_t y, const char *str)
15 {
16         return 1;
17 }
18
19 void WriteLcdNumber(const uint8_t x, const uint8_t y, const int16_t number)
20 {
21 }
22
23 u8t WriteLcdCharacter(const uint8_t x, const uint8_t y, const uint8_t Character)
24 {
25         return 1;
26 }
27
28
29 void oswald_draw_bitmap_offset(const unsigned int xstart, const unsigned int ystart, const unsigned int xoff, const unsigned int yoff, const unsigned int  width, const unsigned int  height, const void *bmp)
30 {
31         unsigned int x, y;
32         uint8_t *cb;
33
34         if (bmp == NULL)
35                 return;
36
37         // we only draw set pixel, unset pixel remain as they are
38         for (y=yoff; y<height; y++) {
39                 for (x=xoff; x<width; x++) {
40                         cb = (uint8_t *)(bmp + (y * ((width / 8) + ((width % 8) ? 1 : 0))) + (x / 8));
41                         if (*cb & (1 << (x % 8)))
42                                 hal_lcd_set_pixel((xstart + x) - xoff, (ystart + y) - yoff, TRUE);
43                 }
44         }
45 }
46
47 #if 0
48 /*inline*/ void oswald_draw_bitmap(const unsigned int xstart, const unsigned int ystart, const unsigned int width, const unsigned int height, const void *bmp)
49 {
50         unsigned int x, y;
51         uint8_t *cb;
52
53         // we only draw set pixel, unset pixel remain as they are
54         for (y=0; y<height; y++) {
55                 for (x=0; x<width; x++) {
56                         cb = (uint8_t *)(bmp + (y * ((width / 8) + ((width % 8) ? 1 : 0))) + (x / 8));
57                         if (*cb & (1 << (x % 8)))
58                                 hal_lcd_set_pixel((xstart + x), (ystart + y), TRUE);
59                 }
60         }
61 }
62 #else
63 void oswald_draw_bitmap(const unsigned int xstart, const unsigned int ystart, const unsigned int width, const unsigned int height, const void *bmp)
64 {
65         // seems we are triggering a MSPGCC compiler bug here...
66         // if we do not do this trick then bmp becomes 0x00 when passed a livel higher!
67         volatile unsigned int num;
68
69         num = (unsigned int) bmp;
70
71         oswald_draw_bitmap_offset(xstart, ystart, 0, 0, width, height, bmp);
72 }
73 #endif
74
75 void oswald_draw_line(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend)
76 {
77         int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
78  
79         dx = xend - xstart;
80         dy = yend - ystart;
81  
82         incx = (dx >= 0) ? 1 : -1;
83         incy = (dy >= 0) ? 1 : -1;
84
85         if (dx<0)
86                 dx = -dx;
87         if (dy<0)
88                 dy = -dy;
89  
90         if (dx>dy) {
91                 pdx = incx; pdy = 0;
92                 ddx=incx; ddy=incy;
93                 es =dy;   el =dx;
94         } else {
95                 pdx=0;    pdy=incy;
96                 ddx=incx; ddy=incy;
97                 es =dx;   el =dy;
98         }
99  
100         x = xstart;
101         y = ystart;
102         err = el/2;
103         hal_lcd_set_pixel(x, y, TRUE);
104  
105         for (t = 0; t < el; ++t) {
106                 err -= es; 
107                 if (err < 0) {
108                         err += el;
109                         x += ddx;
110                         y += ddy;
111                 } else {
112                         x += pdx;
113                         y += pdy;
114                 }
115                 hal_lcd_set_pixel(x, y, TRUE);
116         }
117         // hal_lcd_update_display();
118 }
119
120 void oswald_draw_line_ww(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend, const uint8_t thickness)
121 {
122         int i, x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
123  
124         dx = xend - xstart;
125         dy = yend - ystart;
126  
127         incx = (dx >= 0) ? 1 : -1;
128         incy = (dy >= 0) ? 1 : -1;
129
130         if (dx<0)
131                 dx = -dx;
132         if (dy<0)
133                 dy = -dy;
134  
135         if (dx>dy) {
136                 pdx = incx;
137                 pdy = 0;
138                 ddx=incx;
139                 ddy=incy;
140                 es =dy;
141                 el =dx;
142         } else {
143                 pdx=0;
144                 pdy=incy;
145                 ddx=incx;
146                 ddy=incy;
147                 es =dx;
148                 el =dy;
149         }
150  
151         x = xstart;
152         y = ystart;
153         err = el/2;
154         hal_lcd_set_pixel(x, y, TRUE);
155         for (i=1; i<thickness; i++) {
156                 hal_lcd_set_pixel(x-i, y, TRUE);
157                 hal_lcd_set_pixel(x+i, y, TRUE);
158                 hal_lcd_set_pixel(x, y-i, TRUE);
159                 hal_lcd_set_pixel(x, y+i, TRUE);
160         }
161  
162         for (t = 0; t < el; ++t) {
163                 err -= es; 
164                 if (err < 0) {
165                         err += el;
166                         x += ddx;
167                         y += ddy;
168                 } else {
169                         x += pdx;
170                         y += pdy;
171                 }
172                 hal_lcd_set_pixel(x, y, TRUE);
173                 for (i=1; i<thickness; i++) {
174                         hal_lcd_set_pixel(x-i, y, TRUE);
175                         hal_lcd_set_pixel(x+i, y, TRUE);
176                         hal_lcd_set_pixel(x, y-i, TRUE);
177                         hal_lcd_set_pixel(x, y+i, TRUE);
178                 }
179         }
180         // hal_lcd_update_display();
181 }
182
183 uint8_t oswald_write_character(const uint8_t x, const uint8_t y, const oswald_font_face face, const uint8_t Character)
184 {
185 #if 0
186         u8t CharacterHeight = GetCharacterHeight();
187         u8t CharacterWidth = GetCharacterWidth(Character);
188         u16t bitmap[MAX_FONT_ROWS];
189         register lx, ly;
190
191         GetCharacterBitmap(Character, bitmap);
192
193         // printf("cw=%d ch=%d\n", CharacterWidth, CharacterHeight);
194         for (ly=0; ly<CharacterHeight; ly++) {
195                 for (lx=0; lx<CharacterWidth; lx++) {
196                         if (bitmap[ly] & (1<<lx)) {
197                                 hal_lcd_set_pixel(lx+x, ly+y, TRUE);
198                                 // printf(".");
199                         } /*else {
200                                 hal_lcd_set_pixel(lx+x, ly+y, FALSE);
201                                 // printf(" ");
202                         }*/
203                 }
204                 // printf("\n");
205         }
206
207         return CharacterWidth + GetFontSpacing();
208 #else
209         uint8_t *cdata = oswald_fonts[face].data;
210         int csize;
211
212         if (Character == 32) // space / blank
213                 return oswald_fonts[face].width / 2;
214
215         csize = ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0)) * oswald_fonts[face].height;
216         //csize += (oswald_fonts[face].height / 8) + ((oswald_fonts[face].height % 8) ? 1 : 0);
217
218         // g_printerr("fp = 0x%08lx cdata = 0x%08lx\n", font_7x12, cdata);
219
220         cdata = (cdata + (csize * (int)Character));
221
222         //g_printerr("%02x\n", oswald_fonts[face].data[0][0]);
223         //g_printerr("char %02x face %d %dx%d csize %d\n", Character, face, oswald_fonts[face].width, oswald_fonts[face].height, csize);
224         //g_printerr("char %02x %02x %02x\n", (uint8_t)cdata[0], (uint8_t)cdata[1], (uint8_t)cdata[2]);
225
226         // oswald_draw_bitmap(x, y, oswald_fonts[face].height, oswald_fonts[face].height, cdata);
227         //oswald_draw_bitmap(x, y, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, cdata);
228         oswald_draw_bitmap_offset(x, y, (oswald_fonts[face].width % 8) > 0 ? (8-(oswald_fonts[face].width % 8)) : 0, 0, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, cdata);
229
230         return oswald_fonts[face].width;
231 #endif
232 }
233
234 void oswald_write_string(const uint8_t x, const uint8_t y, const oswald_font_face face, char *str)
235 {
236         uint8_t lx, i, strl;
237
238         strl = oswald_strlen(str);
239         if (strl == 0)
240                 return;
241
242         lx = x;
243         for (i=0; i<strl; i++) {
244                 lx += oswald_write_character(lx, y, face, str[i]);
245         }
246 }
247
248
249 void oswald_write_number(const uint8_t x, const uint8_t y, const oswald_font_face face, const int16_t number)
250 {
251         uint8_t lx, i, strl;
252         char str[8];
253
254         itoa(number, str, 10);
255         strl = oswald_strlen(str);
256         if (strl == 0)
257                 return;
258
259         lx = x;
260         for (i=0; i<strl; i++) {
261                 lx += oswald_write_character(lx, y, face, str[i]);
262         }
263 }
264
265