Correct beast manual install instructions in Windows.
[kugel-rb.git] / firmware / drivers / lcd-charcell.c
blobd02c5eeaad9309210354a6d866e7947c9f60a91a
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 "sprintf.h"
26 #include "lcd.h"
27 #include "kernel.h"
28 #include "thread.h"
29 #include <string.h>
30 #include <stdlib.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;
89 void lcd_update_viewport(void)
91 lcd_update();
94 /** parameter handling **/
96 int lcd_getwidth(void)
98 return current_vp->width;
101 int lcd_getheight(void)
103 return current_vp->height;
106 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
108 int width = utf8length(str);
110 if (w)
111 *w = width;
112 if (h)
113 *h = 1;
115 return width;
118 /** low-level functions **/
120 static int find_xchar(unsigned long ucs)
122 int low = 0;
123 int high = xchar_info_size - 1;
127 int probe = (low + high) >> 1;
129 if (xchar_info[probe].ucs < ucs)
130 low = probe + 1;
131 else if (xchar_info[probe].ucs > ucs)
132 high = probe - 1;
133 else
134 return probe;
136 while (low <= high);
138 /* Not found: return index of no-char symbol (last symbol, hardcoded). */
139 return xchar_info_size - 1;
142 static int glyph_to_pat(unsigned glyph)
144 int i;
146 for (i = 0; i < lcd_pattern_count; i++)
147 if (lcd_patterns[i].glyph == glyph)
148 return i;
150 return NO_PATTERN;
153 static void lcd_free_pat(int pat)
155 int x, y;
157 if (pat != NO_PATTERN)
159 for (x = 0; x < LCD_WIDTH; x++)
160 for (y = 0; y < LCD_HEIGHT; y++)
161 if (pat == lcd_charbuffer[y][x])
162 lcd_charbuffer[y][x] = lcd_substbuffer[y][x];
164 if (lcd_cursor.enabled && pat == lcd_cursor.hw_char)
165 lcd_cursor.hw_char = lcd_cursor.subst_char;
167 lcd_patterns[pat].count = 0;
171 static int lcd_get_free_pat(int xchar)
173 static int last_used_pat = 0;
175 int pat = last_used_pat; /* start from last used pattern */
176 int least_pat = pat; /* pattern with least priority */
177 int least_priority = lcd_patterns[pat].priority;
178 int i;
180 for (i = 0; i < lcd_pattern_count; i++)
182 if (++pat >= lcd_pattern_count) /* Keep 'pat' within limits */
183 pat = 0;
185 if (lcd_patterns[pat].count == 0)
187 last_used_pat = pat;
188 return pat;
190 if (lcd_patterns[pat].priority < least_priority)
192 least_priority = lcd_patterns[pat].priority;
193 least_pat = pat;
196 if (xchar_info[xchar].priority > least_priority) /* prioritized char */
198 lcd_free_pat(least_pat);
199 last_used_pat = least_pat;
200 return least_pat;
202 return NO_PATTERN;
205 static const unsigned char *glyph_to_pattern(unsigned glyph)
207 if (glyph & 0x8000)
208 return xfont_variable[glyph & 0x7fff];
209 else
210 return xfont_fixed[glyph];
213 static int map_xchar(int xchar, unsigned char *substitute)
215 int pat;
216 unsigned glyph;
218 if (xchar_info[xchar].priority > 0) /* soft char */
220 glyph = xchar_info[xchar].glyph;
221 pat = glyph_to_pat(glyph);
223 if (pat == NO_PATTERN) /* not yet mapped */
225 pat = lcd_get_free_pat(xchar); /* try to map */
227 if (pat == NO_PATTERN) /* failed: just use substitute */
228 return xchar_info[xchar].hw_char;
229 else
230 { /* define pattern */
231 lcd_patterns[pat].priority = xchar_info[xchar].priority;
232 lcd_patterns[pat].glyph = glyph;
233 memcpy(lcd_patterns[pat].pattern, glyph_to_pattern(glyph),
234 HW_PATTERN_SIZE);
237 lcd_patterns[pat].count++; /* increase reference count */
238 *substitute = xchar_info[xchar].hw_char;
239 return pat;
241 else /* hardware char */
242 return xchar_info[xchar].hw_char;
245 static void lcd_putxchar(int x, int y, int xchar)
247 int lcd_char;
249 /* Adjust for viewport */
250 x += current_vp->x;
251 y += current_vp->y;
253 lcd_char = lcd_charbuffer[y][x];
255 if (lcd_char < lcd_pattern_count) /* old char was soft */
256 lcd_patterns[lcd_char].count--; /* decrease old reference count */
258 lcd_charbuffer[y][x] = map_xchar(xchar, &lcd_substbuffer[y][x]);
261 /** user-definable pattern handling **/
263 unsigned long lcd_get_locked_pattern(void)
265 int i = 0;
267 for (i = 0; i < VARIABLE_XCHARS; i++)
269 if (!xfont_variable_locked[i])
271 xfont_variable_locked[i] = true;
272 return 0xe000 + i; /* hard-coded */
275 return 0;
278 void lcd_unlock_pattern(unsigned long ucs)
280 int xchar = find_xchar(ucs);
281 unsigned glyph = xchar_info[xchar].glyph;
283 if (glyph & 0x8000) /* variable extended char */
285 lcd_free_pat(glyph_to_pat(glyph));
286 xfont_variable_locked[glyph & 0x7fff] = false;
290 void lcd_define_pattern(unsigned long ucs, const char *pattern)
292 int xchar = find_xchar(ucs);
293 unsigned glyph = xchar_info[xchar].glyph;
294 int pat;
296 if (glyph & 0x8000) /* variable extended char */
298 memcpy(xfont_variable[glyph & 0x7fff], pattern, HW_PATTERN_SIZE);
299 pat = glyph_to_pat(glyph);
300 if (pat != NO_PATTERN)
301 memcpy(lcd_patterns[pat].pattern, pattern, HW_PATTERN_SIZE);
305 /** output functions **/
307 /* Clear the whole display */
308 void lcd_clear_display(void)
310 int x, y;
311 struct viewport* old_vp = current_vp;
313 lcd_scroll_info.lines = 0;
314 lcd_remove_cursor();
316 /* Set the default viewport - required for lcd_putxchar */
317 current_vp = &default_vp;
319 for (x = 0; x < LCD_WIDTH; x++)
320 for (y = 0; y < LCD_HEIGHT; y++)
321 lcd_putxchar(x, y, xspace);
323 current_vp = old_vp;
326 /* Clear the current viewport */
327 void lcd_clear_viewport(void)
329 int x, y;
331 if (current_vp == &default_vp)
333 lcd_clear_display();
335 else
337 /* Remove the cursor if it is within the current viewport */
338 if (lcd_cursor.enabled &&
339 (lcd_cursor.x >= current_vp->x) &&
340 (lcd_cursor.x <= current_vp->x + current_vp->width) &&
341 (lcd_cursor.y >= current_vp->y) &&
342 (lcd_cursor.y <= current_vp->y + current_vp->height))
344 lcd_remove_cursor();
347 for (x = 0; x < current_vp->width; x++)
348 for (y = 0; y < current_vp->height; y++)
349 lcd_putxchar(x, y, xspace);
351 lcd_scroll_stop(current_vp);
355 /* Put an unicode character at the given position */
356 void lcd_putc(int x, int y, unsigned long ucs)
358 if ((unsigned)x >= (unsigned)current_vp->width ||
359 (unsigned)y >= (unsigned)current_vp->height)
360 return;
362 lcd_putxchar(x, y, find_xchar(ucs));
365 /* Show cursor (alternating with existing character) at the given position */
366 void lcd_put_cursor(int x, int y, unsigned long cursor_ucs)
368 if ((unsigned)x >= (unsigned)current_vp->width ||
369 (unsigned)y >= (unsigned)current_vp->height ||
370 lcd_cursor.enabled)
371 return;
373 lcd_cursor.enabled = true;
374 lcd_cursor.visible = false;
375 lcd_cursor.hw_char = map_xchar(find_xchar(cursor_ucs), &lcd_cursor.subst_char);
376 lcd_cursor.x = current_vp->x + x;
377 lcd_cursor.y = current_vp->y + y;
378 lcd_cursor.downcount = 0;
379 lcd_cursor.divider = MAX((HZ/2) / lcd_scroll_info.ticks, 1);
382 /* Remove the cursor */
383 void lcd_remove_cursor(void)
385 if (lcd_cursor.enabled)
387 if (lcd_cursor.hw_char < lcd_pattern_count) /* soft char, unmap */
388 lcd_patterns[lcd_cursor.hw_char].count--;
390 lcd_cursor.enabled = lcd_cursor.visible = false;
394 /* Put a string at a given position, skipping first ofs chars */
395 static int lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
397 unsigned short ucs;
398 const unsigned char *utf8 = str;
400 while (*utf8 && x < current_vp->width)
402 utf8 = utf8decode(utf8, &ucs);
404 if (ofs > 0)
406 ofs--;
407 continue;
409 lcd_putxchar(x++, y, find_xchar(ucs));
411 return x;
414 /* Put a string at a given position */
415 void lcd_putsxy(int x, int y, const unsigned char *str)
417 if ((unsigned)y >= (unsigned)current_vp->height)
418 return;
420 lcd_putsxyofs(x, y, 0, str);
423 /*** Line oriented text output ***/
425 /* Put a string at a given char position */
426 void lcd_puts(int x, int y, const unsigned char *str)
428 lcd_puts_offset(x, y, str, 0);
431 /* Formatting version of lcd_puts */
432 void lcd_putsf(int x, int y, const unsigned char *fmt, ...)
434 va_list ap;
435 char buf[256];
436 va_start(ap, fmt);
437 vsnprintf(buf, sizeof (buf), fmt, ap);
438 va_end(ap);
439 lcd_puts(x, y, buf);
442 /* Put a string at a given char position, skipping first offset chars */
443 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
445 if ((unsigned)y >= (unsigned)current_vp->height)
446 return;
448 /* make sure scrolling is turned off on the line we are updating */
449 lcd_scroll_stop_line(current_vp, y);
451 x = lcd_putsxyofs(x, y, offset, str);
452 while (x < current_vp->width)
453 lcd_putxchar(x++, y, xspace);
456 /** scrolling **/
457 void lcd_puts_scroll(int x, int y, const unsigned char *string)
459 lcd_puts_scroll_offset(x, y, string, 0);
462 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string,
463 int offset)
465 struct scrollinfo* s;
466 int len;
468 if ((unsigned)y >= (unsigned)current_vp->height)
469 return;
471 /* remove any previously scrolling line at the same location */
472 lcd_scroll_stop_line(current_vp, y);
474 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
476 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
478 s->start_tick = current_tick + lcd_scroll_info.delay;
480 lcd_puts_offset(x, y, string, offset);
481 len = utf8length(string);
483 if (current_vp->width - x < len)
485 /* prepare scroll line */
486 char *end;
488 memset(s->line, 0, sizeof s->line);
489 strcpy(s->line, string);
491 /* get width */
492 s->len = utf8length(s->line);
494 /* scroll bidirectional or forward only depending on the string width */
495 if (lcd_scroll_info.bidir_limit)
497 s->bidir = s->len < (current_vp->width) *
498 (100 + lcd_scroll_info.bidir_limit) / 100;
500 else
501 s->bidir = false;
503 if (!s->bidir) /* add spaces if scrolling in the round */
505 strcat(s->line, " ");
506 /* get new width incl. spaces */
507 s->len += SCROLL_SPACING;
510 end = strchr(s->line, '\0');
511 strlcpy(end, string, utf8seek(s->line, current_vp->width));
513 s->vp = current_vp;
514 s->y = y;
515 s->offset = offset;
516 s->startx = x;
517 s->backward = false;
518 lcd_scroll_info.lines++;
522 void lcd_scroll_fn(void)
524 struct scrollinfo* s;
525 int index;
526 int xpos, ypos;
527 bool update;
528 struct viewport* old_vp = current_vp;
530 update = false;
531 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
532 s = &lcd_scroll_info.scroll[index];
534 /* check pause */
535 if (TIME_BEFORE(current_tick, s->start_tick))
536 continue;
538 lcd_set_viewport(s->vp);
540 if (s->backward)
541 s->offset--;
542 else
543 s->offset++;
545 xpos = s->startx;
546 ypos = s->y;
548 if (s->bidir) /* scroll bidirectional */
550 if (s->offset <= 0) {
551 /* at beginning of line */
552 s->offset = 0;
553 s->backward = false;
554 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
556 if (s->offset >= s->len - (current_vp->width - xpos)) {
557 /* at end of line */
558 s->offset = s->len - (current_vp->width - xpos);
559 s->backward = true;
560 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
563 else /* scroll forward the whole time */
565 if (s->offset >= s->len)
566 s->offset -= s->len;
569 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
570 update = true;
573 lcd_set_viewport(old_vp);
575 if (lcd_cursor.enabled)
577 if (--lcd_cursor.downcount <= 0)
579 lcd_cursor.downcount = lcd_cursor.divider;
580 lcd_cursor.visible = !lcd_cursor.visible;
581 update = true;
585 if (update)
586 lcd_update();