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