fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / drivers / lcd-1bit-vert.c
blobfcf5dfcc4735c11590c997398ed0f1b57f324e54
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
24 #include "lcd.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "file.h"
30 #include "debug.h"
31 #include "system.h"
32 #include "font.h"
33 #include "rbunicode.h"
34 #include "bidi.h"
35 #include "scroll_engine.h"
37 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
38 #define LCDFN(fn) lcd_ ## fn
39 #define FBFN(fn) fb_ ## fn
40 #define LCDM(ma) LCD_ ## ma
41 #define LCDNAME "lcd_"
42 #define MAIN_LCD
43 #endif
45 /*** globals ***/
47 FBFN(data) LCDFN(framebuffer)[LCDM(FBHEIGHT)][LCDM(FBWIDTH)] IRAM_LCDFRAMEBUFFER;
49 static struct viewport default_vp =
51 .x = 0,
52 .y = 0,
53 .width = LCDM(WIDTH),
54 .height = LCDM(HEIGHT),
55 .font = FONT_SYSFIXED,
56 .drawmode = DRMODE_SOLID,
59 static struct viewport* current_vp = &default_vp;
61 /*** Viewports ***/
63 void LCDFN(set_viewport)(struct viewport* vp)
65 if (vp == NULL)
66 current_vp = &default_vp;
67 else
68 current_vp = vp;
70 #if defined(SIMULATOR)
71 /* Force the viewport to be within bounds. If this happens it should
72 * be considered an error - the viewport will not draw as it might be
73 * expected.
75 if((unsigned) current_vp->x > (unsigned) LCDM(WIDTH)
76 || (unsigned) current_vp->y > (unsigned) LCDM(HEIGHT)
77 || current_vp->x + current_vp->width > LCDM(WIDTH)
78 || current_vp->y + current_vp->height > LCDM(HEIGHT))
80 #if !defined(HAVE_VIEWPORT_CLIP)
81 DEBUGF("ERROR: "
82 #else
83 DEBUGF("NOTE: "
84 #endif
85 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
86 current_vp->x, current_vp->y,
87 current_vp->width, current_vp->height);
90 #endif
93 void LCDFN(update_viewport)(void)
95 LCDFN(update_rect)(current_vp->x, current_vp->y,
96 current_vp->width, current_vp->height);
99 void LCDFN(update_viewport_rect)(int x, int y, int width, int height)
101 LCDFN(update_rect)(current_vp->x + x, current_vp->y + y, width, height);
104 /* LCD init */
105 void LCDFN(init)(void)
107 LCDFN(clear_display)();
108 #ifndef SIMULATOR
109 LCDFN(init_device)();
110 #endif
111 #ifdef MAIN_LCD
112 scroll_init();
113 #endif
116 /*** parameter handling ***/
118 void LCDFN(set_drawmode)(int mode)
120 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
123 int LCDFN(get_drawmode)(void)
125 return current_vp->drawmode;
128 int LCDFN(getwidth)(void)
130 return current_vp->width;
133 int LCDFN(getheight)(void)
135 return current_vp->height;
138 void LCDFN(setfont)(int newfont)
140 current_vp->font = newfont;
143 int LCDFN(getfont)(void)
145 return current_vp->font;
148 int LCDFN(getstringsize)(const unsigned char *str, int *w, int *h)
150 return font_getstringsize(str, w, h, current_vp->font);
153 /*** low-level drawing functions ***/
155 static void setpixel(int x, int y)
157 LCDFN(framebuffer)[y>>3][x] |= BIT_N(y & 7);
160 static void clearpixel(int x, int y)
162 LCDFN(framebuffer)[y>>3][x] &= ~BIT_N(y & 7);
165 static void flippixel(int x, int y)
167 LCDFN(framebuffer)[y>>3][x] ^= BIT_N(y & 7);
170 static void nopixel(int x, int y)
172 (void)x;
173 (void)y;
176 LCDFN(pixelfunc_type)* const LCDFN(pixelfuncs)[8] = {
177 flippixel, nopixel, setpixel, setpixel,
178 nopixel, clearpixel, nopixel, clearpixel
181 static void ICODE_ATTR flipblock(FBFN(data) *address, unsigned mask,
182 unsigned bits)
184 *address ^= bits & mask;
187 static void ICODE_ATTR bgblock(FBFN(data) *address, unsigned mask,
188 unsigned bits)
190 *address &= bits | ~mask;
193 static void ICODE_ATTR fgblock(FBFN(data) *address, unsigned mask,
194 unsigned bits)
196 *address |= bits & mask;
199 static void ICODE_ATTR solidblock(FBFN(data) *address, unsigned mask,
200 unsigned bits)
202 unsigned data = *(char*)address;
204 bits ^= data;
205 *address = data ^ (bits & mask);
208 static void ICODE_ATTR flipinvblock(FBFN(data) *address, unsigned mask,
209 unsigned bits)
211 *address ^= ~bits & mask;
214 static void ICODE_ATTR bginvblock(FBFN(data) *address, unsigned mask,
215 unsigned bits)
217 *address &= ~(bits & mask);
220 static void ICODE_ATTR fginvblock(FBFN(data) *address, unsigned mask,
221 unsigned bits)
223 *address |= ~bits & mask;
226 static void ICODE_ATTR solidinvblock(FBFN(data) *address, unsigned mask,
227 unsigned bits)
229 unsigned data = *(char *)address;
231 bits = ~bits ^ data;
232 *address = data ^ (bits & mask);
235 LCDFN(blockfunc_type)* const LCDFN(blockfuncs)[8] = {
236 flipblock, bgblock, fgblock, solidblock,
237 flipinvblock, bginvblock, fginvblock, solidinvblock
240 /*** drawing functions ***/
242 /* Clear the whole display */
243 void LCDFN(clear_display)(void)
245 unsigned bits = (current_vp->drawmode & DRMODE_INVERSEVID) ? 0xFFu : 0;
247 memset(LCDFN(framebuffer), bits, sizeof LCDFN(framebuffer));
248 LCDFN(scroll_info).lines = 0;
251 /* Clear the current viewport */
252 void LCDFN(clear_viewport)(void)
254 int oldmode;
256 if (current_vp == &default_vp)
258 LCDFN(clear_display)();
260 else
262 oldmode = current_vp->drawmode;
264 /* Invert the INVERSEVID bit and set basic mode to SOLID */
265 current_vp->drawmode = (~current_vp->drawmode & DRMODE_INVERSEVID) |
266 DRMODE_SOLID;
268 LCDFN(fillrect)(0, 0, current_vp->width, current_vp->height);
270 current_vp->drawmode = oldmode;
272 LCDFN(scroll_stop)(current_vp);
276 /* Set a single pixel */
277 void LCDFN(drawpixel)(int x, int y)
279 if ( ((unsigned)x < (unsigned)current_vp->width)
280 && ((unsigned)y < (unsigned)current_vp->height)
281 #if defined(HAVE_VIEWPORT_CLIP)
282 && ((unsigned)x < (unsigned)LCDM(WIDTH))
283 && ((unsigned)y < (unsigned)LCDM(HEIGHT))
284 #endif
286 LCDFN(pixelfuncs)[current_vp->drawmode](current_vp->x + x, current_vp->y + y);
289 /* Draw a line */
290 void LCDFN(drawline)(int x1, int y1, int x2, int y2)
292 int numpixels;
293 int i;
294 int deltax, deltay;
295 int d, dinc1, dinc2;
296 int x, xinc1, xinc2;
297 int y, yinc1, yinc2;
298 LCDFN(pixelfunc_type) *pfunc = LCDFN(pixelfuncs)[current_vp->drawmode];
300 deltax = abs(x2 - x1);
301 if (deltax == 0)
303 /* DEBUGF(LCDNAME "drawline() called for vertical line - optimisation.\n"); */
304 LCDFN(vline)(x1, y1, y2);
305 return;
307 deltay = abs(y2 - y1);
308 if (deltay == 0)
310 /* DEBUGF(LCDNAME "drawline() called for horizontal line - optimisation.\n"); */
311 LCDFN(hline)(x1, x2, y1);
312 return;
314 xinc2 = 1;
315 yinc2 = 1;
317 if (deltax >= deltay)
319 numpixels = deltax;
320 d = 2 * deltay - deltax;
321 dinc1 = deltay * 2;
322 dinc2 = (deltay - deltax) * 2;
323 xinc1 = 1;
324 yinc1 = 0;
326 else
328 numpixels = deltay;
329 d = 2 * deltax - deltay;
330 dinc1 = deltax * 2;
331 dinc2 = (deltax - deltay) * 2;
332 xinc1 = 0;
333 yinc1 = 1;
335 numpixels++; /* include endpoints */
337 if (x1 > x2)
339 xinc1 = -xinc1;
340 xinc2 = -xinc2;
343 if (y1 > y2)
345 yinc1 = -yinc1;
346 yinc2 = -yinc2;
349 x = x1;
350 y = y1;
352 for (i = 0; i < numpixels; i++)
354 if ( ((unsigned)x < (unsigned)current_vp->width)
355 && ((unsigned)y < (unsigned)current_vp->height)
356 #if defined(HAVE_VIEWPORT_CLIP)
357 && ((unsigned)x < (unsigned)LCDM(WIDTH))
358 && ((unsigned)y < (unsigned)LCDM(HEIGHT))
359 #endif
361 pfunc(current_vp->x + x, current_vp->y + y);
363 if (d < 0)
365 d += dinc1;
366 x += xinc1;
367 y += yinc1;
369 else
371 d += dinc2;
372 x += xinc2;
373 y += yinc2;
378 /* Draw a horizontal line (optimised) */
379 void LCDFN(hline)(int x1, int x2, int y)
381 int x, width;
382 unsigned char *dst, *dst_end;
383 unsigned mask;
384 LCDFN(blockfunc_type) *bfunc;
386 /* direction flip */
387 if (x2 < x1)
389 x = x1;
390 x1 = x2;
391 x2 = x;
394 /******************** In viewport clipping **********************/
395 /* nothing to draw? */
396 if (((unsigned)y >= (unsigned)current_vp->height) || (x1 >= current_vp->width)
397 || (x2 < 0))
398 return;
400 if (x1 < 0)
401 x1 = 0;
402 if (x2 >= current_vp->width)
403 x2 = current_vp->width-1;
405 /* adjust to viewport */
406 x1 += current_vp->x;
407 x2 += current_vp->x;
408 y += current_vp->y;
410 #if defined(HAVE_VIEWPORT_CLIP)
411 /********************* Viewport on screen clipping ********************/
412 /* nothing to draw? */
413 if (((unsigned)y >= (unsigned) LCDM(HEIGHT)) || (x1 >= LCDM(WIDTH))
414 || (x2 < 0))
415 return;
417 /* clipping */
418 if (x1 < 0)
419 x1 = 0;
420 if (x2 >= LCDM(WIDTH))
421 x2 = LCDM(WIDTH)-1;
422 #endif
424 width = x2 - x1 + 1;
426 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
427 dst = &LCDFN(framebuffer)[y>>3][x1];
428 mask = BIT_N(y & 7);
430 dst_end = dst + width;
432 bfunc(dst++, mask, 0xFFu);
433 while (dst < dst_end);
436 /* Draw a vertical line (optimised) */
437 void LCDFN(vline)(int x, int y1, int y2)
439 int ny;
440 FBFN(data) *dst;
441 unsigned mask, mask_bottom;
442 LCDFN(blockfunc_type) *bfunc;
444 /* direction flip */
445 if (y2 < y1)
447 ny = y1;
448 y1 = y2;
449 y2 = ny;
452 /******************** In viewport clipping **********************/
453 /* nothing to draw? */
454 if (((unsigned)x >= (unsigned)current_vp->width) || (y1 >= current_vp->height)
455 || (y2 < 0))
456 return;
458 if (y1 < 0)
459 y1 = 0;
460 if (y2 >= current_vp->height)
461 y2 = current_vp->height-1;
463 /* adjust for viewport */
464 y1 += current_vp->y;
465 y2 += current_vp->y;
466 x += current_vp->x;
468 #if defined(HAVE_VIEWPORT_CLIP)
469 /********************* Viewport on screen clipping ********************/
470 /* nothing to draw? */
471 if (( (unsigned) x >= (unsigned)LCDM(WIDTH)) || (y1 >= LCDM(HEIGHT))
472 || (y2 < 0))
473 return;
475 /* clipping */
476 if (y1 < 0)
477 y1 = 0;
478 if (y2 >= LCDM(HEIGHT))
479 y2 = LCDM(HEIGHT)-1;
480 #endif
482 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
483 dst = &LCDFN(framebuffer)[y1>>3][x];
484 ny = y2 - (y1 & ~7);
485 mask = 0xFFu << (y1 & 7);
486 mask_bottom = 0xFFu >> (~ny & 7);
488 for (; ny >= 8; ny -= 8)
490 bfunc(dst, mask, 0xFFu);
491 dst += LCDM(WIDTH);
492 mask = 0xFFu;
494 mask &= mask_bottom;
495 bfunc(dst, mask, 0xFFu);
498 /* Draw a rectangular box */
499 void LCDFN(drawrect)(int x, int y, int width, int height)
501 if ((width <= 0) || (height <= 0))
502 return;
504 int x2 = x + width - 1;
505 int y2 = y + height - 1;
507 LCDFN(vline)(x, y, y2);
508 LCDFN(vline)(x2, y, y2);
509 LCDFN(hline)(x, x2, y);
510 LCDFN(hline)(x, x2, y2);
513 /* Fill a rectangular area */
514 void LCDFN(fillrect)(int x, int y, int width, int height)
516 int ny;
517 FBFN(data) *dst, *dst_end;
518 unsigned mask, mask_bottom;
519 unsigned bits = 0;
520 LCDFN(blockfunc_type) *bfunc;
521 bool fillopt = false;
523 /******************** In viewport clipping **********************/
524 /* nothing to draw? */
525 if ((width <= 0) || (height <= 0) || (x >= current_vp->width)
526 || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
527 return;
529 if (x < 0)
531 width += x;
532 x = 0;
534 if (y < 0)
536 height += y;
537 y = 0;
539 if (x + width > current_vp->width)
540 width = current_vp->width - x;
541 if (y + height > current_vp->height)
542 height = current_vp->height - y;
544 /* adjust for viewport */
545 x += current_vp->x;
546 y += current_vp->y;
548 #if defined(HAVE_VIEWPORT_CLIP)
549 /********************* Viewport on screen clipping ********************/
550 /* nothing to draw? */
551 if ((x >= LCDM(WIDTH)) || (y >= LCDM(HEIGHT))
552 || (x + width <= 0) || (y + height <= 0))
553 return;
555 /* clip image in viewport in screen */
556 if (x < 0)
558 width += x;
559 x = 0;
561 if (y < 0)
563 height += y;
564 y = 0;
566 if (x + width > LCDM(WIDTH))
567 width = LCDM(WIDTH) - x;
568 if (y + height > LCDM(HEIGHT))
569 height = LCDM(HEIGHT) - y;
570 #endif
572 if (current_vp->drawmode & DRMODE_INVERSEVID)
574 if (current_vp->drawmode & DRMODE_BG)
576 fillopt = true;
579 else
581 if (current_vp->drawmode & DRMODE_FG)
583 fillopt = true;
584 bits = 0xFFu;
587 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
588 dst = &LCDFN(framebuffer)[y>>3][x];
589 ny = height - 1 + (y & 7);
590 mask = 0xFFu << (y & 7);
591 mask_bottom = 0xFFu >> (~ny & 7);
593 for (; ny >= 8; ny -= 8)
595 if (fillopt && (mask == 0xFFu))
596 memset(dst, bits, width);
597 else
599 FBFN(data) *dst_row = dst;
601 dst_end = dst_row + width;
603 bfunc(dst_row++, mask, 0xFFu);
604 while (dst_row < dst_end);
607 dst += LCDM(WIDTH);
608 mask = 0xFFu;
610 mask &= mask_bottom;
612 if (fillopt && (mask == 0xFFu))
613 memset(dst, bits, width);
614 else
616 dst_end = dst + width;
618 bfunc(dst++, mask, 0xFFu);
619 while (dst < dst_end);
623 /* About Rockbox' internal bitmap format:
625 * A bitmap contains one bit for every pixel that defines if that pixel is
626 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
627 * at top.
628 * The bytes are stored in row-major order, with byte 0 being top left,
629 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
630 * 0..7, the second row defines pixel row 8..15 etc.
632 * This is the same as the internal lcd hw format. */
634 /* Draw a partial bitmap */
635 void ICODE_ATTR LCDFN(bitmap_part)(const unsigned char *src, int src_x,
636 int src_y, int stride, int x, int y,
637 int width, int height)
639 int shift, ny;
640 FBFN(data) *dst, *dst_end;
641 unsigned mask, mask_bottom;
642 LCDFN(blockfunc_type) *bfunc;
644 /******************** Image in viewport clipping **********************/
645 /* nothing to draw? */
646 if ((width <= 0) || (height <= 0) || (x >= current_vp->width)
647 || (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
648 return;
650 /* clip image in viewport */
651 if (x < 0)
653 width += x;
654 src_x -= x;
655 x = 0;
657 if (y < 0)
659 height += y;
660 src_y -= y;
661 y = 0;
663 if (x + width > current_vp->width)
664 width = current_vp->width - x;
665 if (y + height > current_vp->height)
666 height = current_vp->height - y;
668 /* adjust for viewport */
669 x += current_vp->x;
670 y += current_vp->y;
672 #if defined(HAVE_VIEWPORT_CLIP)
673 /********************* Viewport on screen clipping ********************/
674 /* nothing to draw? */
675 if ((x >= LCDM(WIDTH)) || (y >= LCDM(HEIGHT))
676 || (x + width <= 0) || (y + height <= 0))
677 return;
679 /* clip image in viewport in screen */
680 if (x < 0)
682 width += x;
683 src_x -= x;
684 x = 0;
686 if (y < 0)
688 height += y;
689 src_y -= y;
690 y = 0;
692 if (x + width > LCDM(WIDTH))
693 width = LCDM(WIDTH) - x;
694 if (y + height > LCDM(HEIGHT))
695 height = LCDM(HEIGHT) - y;
696 #endif
698 src += stride * (src_y >> 3) + src_x; /* move starting point */
699 src_y &= 7;
700 y -= src_y;
701 dst = &LCDFN(framebuffer)[y>>3][x];
702 shift = y & 7;
703 ny = height - 1 + shift + src_y;
705 bfunc = LCDFN(blockfuncs)[current_vp->drawmode];
706 mask = 0xFFu << (shift + src_y);
707 mask_bottom = 0xFFu >> (~ny & 7);
709 if (shift == 0)
711 bool copyopt = (current_vp->drawmode == DRMODE_SOLID);
713 for (; ny >= 8; ny -= 8)
715 if (copyopt && (mask == 0xFFu))
716 memcpy(dst, src, width);
717 else
719 const unsigned char *src_row = src;
720 FBFN(data) *dst_row = dst;
722 dst_end = dst_row + width;
724 bfunc(dst_row++, mask, *src_row++);
725 while (dst_row < dst_end);
728 src += stride;
729 dst += LCDM(WIDTH);
730 mask = 0xFFu;
732 mask &= mask_bottom;
734 if (copyopt && (mask == 0xFFu))
735 memcpy(dst, src, width);
736 else
738 dst_end = dst + width;
740 bfunc(dst++, mask, *src++);
741 while (dst < dst_end);
744 else
746 dst_end = dst + width;
749 const unsigned char *src_col = src++;
750 FBFN(data) *dst_col = dst++;
751 unsigned mask_col = mask;
752 unsigned data = 0;
754 for (y = ny; y >= 8; y -= 8)
756 data |= *src_col << shift;
758 if (mask_col & 0xFFu)
760 bfunc(dst_col, mask_col, data);
761 mask_col = 0xFFu;
763 else
764 mask_col >>= 8;
766 src_col += stride;
767 dst_col += LCDM(WIDTH);
768 data >>= 8;
770 data |= *src_col << shift;
771 bfunc(dst_col, mask_col & mask_bottom, data);
773 while (dst < dst_end);
777 /* Draw a full bitmap */
778 void LCDFN(bitmap)(const unsigned char *src, int x, int y, int width,
779 int height)
781 LCDFN(bitmap_part)(src, 0, 0, width, x, y, width, height);
784 #include "lcd-bitmap-common.c"