Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / firmware / drivers / lcd-charcell.c
blob0186c2610628ffc54e0098f6ec1b992c62986ab4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Jens Arnold
11 * Based on the work of Alan Korr, Kjell Ericson and others
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "config.h"
23 #include "hwcompat.h"
24 #include "stdarg.h"
25 #include "lcd.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include <string.h>
29 #include <stdlib.h>
30 #include "debug.h"
31 #include "file.h"
32 #include "system.h"
33 #include "lcd-charcell.h"
34 #include "rbunicode.h"
35 #include "scroll_engine.h"
37 /** definitions **/
39 #define VARIABLE_XCHARS 16 /* number of software user-definable characters */
40 /* There must be mappings for this many characters in the 0xe000 unicode range
41 * in lcd-charset-<target>.c */
43 #define NO_PATTERN (-1)
45 static int find_xchar(unsigned long ucs);
47 /** globals **/
49 unsigned char lcd_charbuffer[LCD_HEIGHT][LCD_WIDTH]; /* The "frame"buffer */
50 static unsigned char lcd_substbuffer[LCD_HEIGHT][LCD_WIDTH];
51 struct pattern_info lcd_patterns[MAX_HW_PATTERNS];
52 struct cursor_info lcd_cursor;
54 static unsigned char xfont_variable[VARIABLE_XCHARS][HW_PATTERN_SIZE];
55 static bool xfont_variable_locked[VARIABLE_XCHARS];
56 static int xspace; /* stores xhcar id of ' ' - often needed */
58 static struct viewport default_vp =
60 .x = 0,
61 .y = 0,
62 .width = LCD_WIDTH,
63 .height = LCD_HEIGHT,
66 static struct viewport* current_vp = &default_vp;
68 /* LCD init */
69 void lcd_init (void)
71 lcd_init_device();
72 lcd_charset_init();
73 memset(lcd_patterns, 0, sizeof(lcd_patterns));
74 xspace = find_xchar(' ');
75 memset(lcd_charbuffer, xchar_info[xspace].hw_char, sizeof(lcd_charbuffer));
76 scroll_init();
79 /* Viewports */
81 void lcd_set_viewport(struct viewport* vp)
83 if (vp == NULL)
84 current_vp = &default_vp;
85 else
86 current_vp = vp;
88 #if defined(SIMULATOR)
89 /* Force the viewport to be within bounds. If this happens it should
90 * be considered an error - the viewport will not draw as it might be
91 * expected.
93 if((unsigned) current_vp->x > (unsigned) LCD_WIDTH
94 || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT
95 || current_vp->x + current_vp->width > LCD_WIDTH
96 || current_vp->y + current_vp->height > LCD_HEIGHT)
98 #if !defined(HAVE_VIEWPORT_CLIP)
99 DEBUGF("ERROR: "
100 #else
101 DEBUGF("NOTE: "
102 #endif
103 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
104 current_vp->x, current_vp->y,
105 current_vp->width, current_vp->height);
108 #endif
111 void lcd_update_viewport(void)
113 lcd_update();
116 /** parameter handling **/
118 int lcd_getwidth(void)
120 return current_vp->width;
123 int lcd_getheight(void)
125 return current_vp->height;
128 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
130 int width = utf8length(str);
132 if (w)
133 *w = width;
134 if (h)
135 *h = 1;
137 return width;
140 /** low-level functions **/
142 static int find_xchar(unsigned long ucs)
144 int low = 0;
145 int high = xchar_info_size - 1;
149 int probe = (low + high) >> 1;
151 if (xchar_info[probe].ucs < ucs)
152 low = probe + 1;
153 else if (xchar_info[probe].ucs > ucs)
154 high = probe - 1;
155 else
156 return probe;
158 while (low <= high);
160 /* Not found: return index of no-char symbol (last symbol, hardcoded). */
161 return xchar_info_size - 1;
164 static int glyph_to_pat(unsigned glyph)
166 int i;
168 for (i = 0; i < lcd_pattern_count; i++)
169 if (lcd_patterns[i].glyph == glyph)
170 return i;
172 return NO_PATTERN;
175 static void lcd_free_pat(int pat)
177 int x, y;
179 if (pat != NO_PATTERN)
181 for (x = 0; x < LCD_WIDTH; x++)
182 for (y = 0; y < LCD_HEIGHT; y++)
183 if (pat == lcd_charbuffer[y][x])
184 lcd_charbuffer[y][x] = lcd_substbuffer[y][x];
186 if (lcd_cursor.enabled && pat == lcd_cursor.hw_char)
187 lcd_cursor.hw_char = lcd_cursor.subst_char;
189 lcd_patterns[pat].count = 0;
193 static int lcd_get_free_pat(int xchar)
195 static int last_used_pat = 0;
197 int pat = last_used_pat; /* start from last used pattern */
198 int least_pat = pat; /* pattern with least priority */
199 int least_priority = lcd_patterns[pat].priority;
200 int i;
202 for (i = 0; i < lcd_pattern_count; i++)
204 if (++pat >= lcd_pattern_count) /* Keep 'pat' within limits */
205 pat = 0;
207 if (lcd_patterns[pat].count == 0)
209 last_used_pat = pat;
210 return pat;
212 if (lcd_patterns[pat].priority < least_priority)
214 least_priority = lcd_patterns[pat].priority;
215 least_pat = pat;
218 if (xchar_info[xchar].priority > least_priority) /* prioritized char */
220 lcd_free_pat(least_pat);
221 last_used_pat = least_pat;
222 return least_pat;
224 return NO_PATTERN;
227 static const unsigned char *glyph_to_pattern(unsigned glyph)
229 if (glyph & 0x8000)
230 return xfont_variable[glyph & 0x7fff];
231 else
232 return xfont_fixed[glyph];
235 static int map_xchar(int xchar, unsigned char *substitute)
237 int pat;
238 unsigned glyph;
240 if (xchar_info[xchar].priority > 0) /* soft char */
242 glyph = xchar_info[xchar].glyph;
243 pat = glyph_to_pat(glyph);
245 if (pat == NO_PATTERN) /* not yet mapped */
247 pat = lcd_get_free_pat(xchar); /* try to map */
249 if (pat == NO_PATTERN) /* failed: just use substitute */
250 return xchar_info[xchar].hw_char;
251 else
252 { /* define pattern */
253 lcd_patterns[pat].priority = xchar_info[xchar].priority;
254 lcd_patterns[pat].glyph = glyph;
255 memcpy(lcd_patterns[pat].pattern, glyph_to_pattern(glyph),
256 HW_PATTERN_SIZE);
259 lcd_patterns[pat].count++; /* increase reference count */
260 *substitute = xchar_info[xchar].hw_char;
261 return pat;
263 else /* hardware char */
264 return xchar_info[xchar].hw_char;
267 static void lcd_putxchar(int x, int y, int xchar)
269 int lcd_char;
271 /* Adjust for viewport */
272 x += current_vp->x;
273 y += current_vp->y;
275 #if defined(HAVE_VIEWPORT_CLIP)
276 if((unsigned)x > (unsigned)LCD_WIDTH || (unsigned)y > (unsigned)LCD_HEIGHT)
277 return;
278 #endif
280 lcd_char = lcd_charbuffer[y][x];
282 if (lcd_char < lcd_pattern_count) /* old char was soft */
283 lcd_patterns[lcd_char].count--; /* decrease old reference count */
285 lcd_charbuffer[y][x] = map_xchar(xchar, &lcd_substbuffer[y][x]);
288 /** user-definable pattern handling **/
290 unsigned long lcd_get_locked_pattern(void)
292 int i = 0;
294 for (i = 0; i < VARIABLE_XCHARS; i++)
296 if (!xfont_variable_locked[i])
298 xfont_variable_locked[i] = true;
299 return 0xe000 + i; /* hard-coded */
302 return 0;
305 void lcd_unlock_pattern(unsigned long ucs)
307 int xchar = find_xchar(ucs);
308 unsigned glyph = xchar_info[xchar].glyph;
310 if (glyph & 0x8000) /* variable extended char */
312 lcd_free_pat(glyph_to_pat(glyph));
313 xfont_variable_locked[glyph & 0x7fff] = false;
317 void lcd_define_pattern(unsigned long ucs, const char *pattern)
319 int xchar = find_xchar(ucs);
320 unsigned glyph = xchar_info[xchar].glyph;
321 int pat;
323 if (glyph & 0x8000) /* variable extended char */
325 memcpy(xfont_variable[glyph & 0x7fff], pattern, HW_PATTERN_SIZE);
326 pat = glyph_to_pat(glyph);
327 if (pat != NO_PATTERN)
328 memcpy(lcd_patterns[pat].pattern, pattern, HW_PATTERN_SIZE);
332 /** output functions **/
334 /* Clear the whole display */
335 void lcd_clear_display(void)
337 int x, y;
338 struct viewport* old_vp = current_vp;
340 lcd_scroll_info.lines = 0;
341 lcd_remove_cursor();
343 /* Set the default viewport - required for lcd_putxchar */
344 current_vp = &default_vp;
346 for (x = 0; x < LCD_WIDTH; x++)
347 for (y = 0; y < LCD_HEIGHT; y++)
348 lcd_putxchar(x, y, xspace);
350 current_vp = old_vp;
353 /* Clear the current viewport */
354 void lcd_clear_viewport(void)
356 int x, y;
358 if (current_vp == &default_vp)
360 lcd_clear_display();
362 else
364 /* Remove the cursor if it is within the current viewport */
365 if (lcd_cursor.enabled &&
366 (lcd_cursor.x >= current_vp->x) &&
367 (lcd_cursor.x <= current_vp->x + current_vp->width) &&
368 (lcd_cursor.y >= current_vp->y) &&
369 (lcd_cursor.y <= current_vp->y + current_vp->height))
371 lcd_remove_cursor();
374 for (x = 0; x < current_vp->width; x++)
375 for (y = 0; y < current_vp->height; y++)
376 lcd_putxchar(x, y, xspace);
378 lcd_scroll_stop(current_vp);
382 /* Put an unicode character at the given position */
383 void lcd_putc(int x, int y, unsigned long ucs)
385 if ((unsigned)x >= (unsigned)current_vp->width ||
386 (unsigned)y >= (unsigned)current_vp->height)
387 return;
389 lcd_putxchar(x, y, find_xchar(ucs));
392 /* Show cursor (alternating with existing character) at the given position */
393 void lcd_put_cursor(int x, int y, unsigned long cursor_ucs)
395 if ((unsigned)x >= (unsigned)current_vp->width ||
396 (unsigned)y >= (unsigned)current_vp->height ||
397 lcd_cursor.enabled)
398 return;
400 lcd_cursor.enabled = true;
401 lcd_cursor.visible = false;
402 lcd_cursor.hw_char = map_xchar(find_xchar(cursor_ucs), &lcd_cursor.subst_char);
403 lcd_cursor.x = current_vp->x + x;
404 lcd_cursor.y = current_vp->y + y;
405 lcd_cursor.downcount = 0;
406 lcd_cursor.divider = MAX((HZ/2) / lcd_scroll_info.ticks, 1);
409 /* Remove the cursor */
410 void lcd_remove_cursor(void)
412 if (lcd_cursor.enabled)
414 if (lcd_cursor.hw_char < lcd_pattern_count) /* soft char, unmap */
415 lcd_patterns[lcd_cursor.hw_char].count--;
417 lcd_cursor.enabled = lcd_cursor.visible = false;
421 /* Put a string at a given position, skipping first ofs chars */
422 static int lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
424 unsigned short ucs;
425 const unsigned char *utf8 = str;
427 while (*utf8 && x < current_vp->width)
429 utf8 = utf8decode(utf8, &ucs);
431 if (ofs > 0)
433 ofs--;
434 continue;
436 lcd_putxchar(x++, y, find_xchar(ucs));
438 return x;
441 /* Put a string at a given position */
442 void lcd_putsxy(int x, int y, const unsigned char *str)
444 if ((unsigned)y >= (unsigned)current_vp->height)
445 return;
447 lcd_putsxyofs(x, y, 0, str);
450 /*** Line oriented text output ***/
452 /* Put a string at a given char position */
453 void lcd_puts(int x, int y, const unsigned char *str)
455 lcd_puts_offset(x, y, str, 0);
458 /* Formatting version of lcd_puts */
459 void lcd_putsf(int x, int y, const unsigned char *fmt, ...)
461 va_list ap;
462 char buf[256];
463 va_start(ap, fmt);
464 vsnprintf(buf, sizeof (buf), fmt, ap);
465 va_end(ap);
466 lcd_puts(x, y, buf);
469 /* Put a string at a given char position, skipping first offset chars */
470 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
472 if ((unsigned)y >= (unsigned)current_vp->height)
473 return;
475 /* make sure scrolling is turned off on the line we are updating */
476 lcd_scroll_stop_line(current_vp, y);
478 x = lcd_putsxyofs(x, y, offset, str);
479 while (x < current_vp->width)
480 lcd_putxchar(x++, y, xspace);
483 /** scrolling **/
484 void lcd_puts_scroll(int x, int y, const unsigned char *string)
486 lcd_puts_scroll_offset(x, y, string, 0);
489 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string,
490 int offset)
492 struct scrollinfo* s;
493 int len;
495 if ((unsigned)y >= (unsigned)current_vp->height)
496 return;
498 /* remove any previously scrolling line at the same location */
499 lcd_scroll_stop_line(current_vp, y);
501 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
503 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
505 s->start_tick = current_tick + lcd_scroll_info.delay;
507 lcd_puts_offset(x, y, string, offset);
508 len = utf8length(string);
510 if (current_vp->width - x < len)
512 /* prepare scroll line */
513 char *end;
515 memset(s->line, 0, sizeof s->line);
516 strcpy(s->line, string);
518 /* get width */
519 s->len = utf8length(s->line);
521 /* scroll bidirectional or forward only depending on the string width */
522 if (lcd_scroll_info.bidir_limit)
524 s->bidir = s->len < (current_vp->width) *
525 (100 + lcd_scroll_info.bidir_limit) / 100;
527 else
528 s->bidir = false;
530 if (!s->bidir) /* add spaces if scrolling in the round */
532 strcat(s->line, " ");
533 /* get new width incl. spaces */
534 s->len += SCROLL_SPACING;
537 end = strchr(s->line, '\0');
538 strlcpy(end, string, utf8seek(s->line, current_vp->width));
540 s->vp = current_vp;
541 s->y = y;
542 s->offset = offset;
543 s->startx = x;
544 s->backward = false;
545 lcd_scroll_info.lines++;
549 void lcd_scroll_fn(void)
551 struct scrollinfo* s;
552 int index;
553 int xpos, ypos;
554 bool update;
555 struct viewport* old_vp = current_vp;
557 update = false;
558 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
559 s = &lcd_scroll_info.scroll[index];
561 /* check pause */
562 if (TIME_BEFORE(current_tick, s->start_tick))
563 continue;
565 lcd_set_viewport(s->vp);
567 if (s->backward)
568 s->offset--;
569 else
570 s->offset++;
572 xpos = s->startx;
573 ypos = s->y;
575 if (s->bidir) /* scroll bidirectional */
577 if (s->offset <= 0) {
578 /* at beginning of line */
579 s->offset = 0;
580 s->backward = false;
581 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
583 if (s->offset >= s->len - (current_vp->width - xpos)) {
584 /* at end of line */
585 s->offset = s->len - (current_vp->width - xpos);
586 s->backward = true;
587 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
590 else /* scroll forward the whole time */
592 if (s->offset >= s->len)
593 s->offset -= s->len;
596 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
597 update = true;
600 lcd_set_viewport(old_vp);
602 if (lcd_cursor.enabled)
604 if (--lcd_cursor.downcount <= 0)
606 lcd_cursor.downcount = lcd_cursor.divider;
607 lcd_cursor.visible = !lcd_cursor.visible;
608 update = true;
612 if (update)
613 lcd_update();