]> git.karo-electronics.de Git - oswald.git/blob - ui/LcdDisplay.c
Lot's of stuff...
[oswald.git] / ui / LcdDisplay.c
1 #include "oswald.h"
2 #include "oswald_hal.h"
3 #include "oswald_strings.h"
4 #include "Fonts.h"
5
6 #include "LcdDisplay.h"
7
8
9 void oswald_draw_bitmap(const uint8_t xstart, const uint8_t ystart, const uint8_t width, const uint8_t height, const void *bmp)
10 {
11         uint8_t x, y;
12         uint8_t *cb;
13
14         // we only draw set pixel, unset pixel remain as they are
15         for (y=0; y<height; y++) {
16                 for (x=0; x<width; x++) {
17                         cb = (uint8_t *)(bmp + (y * ((width / 8)+((width % 8) ? 1:0))) + (x / 8));
18                         if (*cb & (1 << (x % 8)))
19                                 hal_lcd_set_pixel(xstart + x, ystart + y, TRUE);
20                 }
21         }
22 }
23
24 void DrawLcdLineBresenham(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend)
25 {
26         int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
27  
28         dx = xend - xstart;
29         dy = yend - ystart;
30  
31         incx = (dx >= 0) ? 1 : -1;
32         incy = (dy >= 0) ? 1 : -1;
33
34         if (dx<0)
35                 dx = -dx;
36         if (dy<0)
37                 dy = -dy;
38  
39         if (dx>dy) {
40                 pdx = incx; pdy = 0;
41                 ddx=incx; ddy=incy;
42                 es =dy;   el =dx;
43         } else {
44                 pdx=0;    pdy=incy;
45                 ddx=incx; ddy=incy;
46                 es =dx;   el =dy;
47         }
48  
49         x = xstart;
50         y = ystart;
51         err = el/2;
52         hal_lcd_set_pixel(x, y, TRUE);
53  
54         for (t = 0; t < el; ++t) {
55                 err -= es; 
56                 if (err < 0) {
57                         err += el;
58                         x += ddx;
59                         y += ddy;
60                 } else {
61                         x += pdx;
62                         y += pdy;
63                 }
64                 hal_lcd_set_pixel(x, y, TRUE);
65         }
66         // lcd_update_display();
67 }
68
69 void DrawLcdLineBresenhamWW(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend, const uint8_t thickness)
70 {
71         int i, x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
72  
73         dx = xend - xstart;
74         dy = yend - ystart;
75  
76         incx = (dx >= 0) ? 1 : -1;
77         incy = (dy >= 0) ? 1 : -1;
78
79         if (dx<0)
80                 dx = -dx;
81         if (dy<0)
82                 dy = -dy;
83  
84         if (dx>dy) {
85                 pdx = incx; pdy = 0;
86                 ddx=incx; ddy=incy;
87                 es =dy;   el =dx;
88         } else {
89                 pdx=0;    pdy=incy;
90                 ddx=incx; ddy=incy;
91                 es =dx;   el =dy;
92         }
93  
94         x = xstart;
95         y = ystart;
96         err = el/2;
97         hal_lcd_set_pixel(x, y, TRUE);
98         for (i=1; i<thickness; i++) {
99                 hal_lcd_set_pixel(x-i, y, TRUE);
100                 hal_lcd_set_pixel(x+i, y, TRUE);
101                 hal_lcd_set_pixel(x, y-i, TRUE);
102                 hal_lcd_set_pixel(x, y+i, TRUE);
103         }
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                 for (i=1; i<thickness; i++) {
117                         hal_lcd_set_pixel(x-i, y, TRUE);
118                         hal_lcd_set_pixel(x+i, y, TRUE);
119                         hal_lcd_set_pixel(x, y-i, TRUE);
120                         hal_lcd_set_pixel(x, y+i, TRUE);
121                 }
122         }
123         // lcd_update_display();
124 }
125
126 u8t WriteLcdCharacter(const uint8_t x, const uint8_t y, const uint8_t Character)
127 {
128         u8t CharacterHeight = GetCharacterHeight();
129         u8t CharacterWidth = GetCharacterWidth(Character);
130         u16t bitmap[MAX_FONT_ROWS];
131         int lx, ly;
132
133         GetCharacterBitmap(Character, bitmap);
134
135         // printf("cw=%d ch=%d\n", CharacterWidth, CharacterHeight);
136         for (ly=0; ly<CharacterHeight; ly++) {
137                 for (lx=0; lx<CharacterWidth; lx++) {
138                         if (bitmap[ly] & (1<<lx)) {
139                                 hal_lcd_set_pixel(lx+x, ly+y, TRUE);
140                                 // printf(".");
141                         } /*else {
142                                 hal_lcd_set_pixel(lx+x, ly+y, FALSE);
143                                 // printf(" ");
144                         }*/
145                 }
146                 // printf("\n");
147         }
148         // lcd_update_display();
149         return CharacterWidth + GetFontSpacing();
150 }
151
152 u8t WriteLcdString(const uint8_t x, const uint8_t y, const char *str)
153 {
154         int lx, i, strl;
155
156         strl = oswald_strlen((char *)str);
157         if (strl == 0)
158                 return 0;
159
160         lx = x;
161         for (i=0; i<strl; i++) {
162                 lx += WriteLcdCharacter(lx, y, str[i]);
163         }
164         return lx;
165 }
166
167
168 void WriteLcdNumber(const uint8_t x, const uint8_t y, const int16_t number)
169 {
170         int lx, i, strl;
171         char str[8];
172
173         itoa(number, str, 10);
174         strl = oswald_strlen(str);
175         if (strl == 0)
176                 return;
177
178         lx = x;
179         for (i=0; i<strl; i++) {
180                 lx += WriteLcdCharacter(lx, y, str[i]);
181         }
182 }
183