summaryrefslogtreecommitdiff
path: root/firmware/drivers/lcd-recorder.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers/lcd-recorder.c')
-rw-r--r--firmware/drivers/lcd-recorder.c766
1 files changed, 766 insertions, 0 deletions
diff --git a/firmware/drivers/lcd-recorder.c b/firmware/drivers/lcd-recorder.c
new file mode 100644
index 0000000000..9f19bb89f0
--- /dev/null
+++ b/firmware/drivers/lcd-recorder.c
@@ -0,0 +1,766 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Alan Korr
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
21#ifdef HAVE_LCD_BITMAP
22
23#include "lcd.h"
24#include "kernel.h"
25#include "thread.h"
26#include <string.h>
27#include <stdlib.h>
28#include "file.h"
29#include "debug.h"
30#include "system.h"
31#include "font.h"
32
33/*** definitions ***/
34
35#define LCD_SET_LOWER_COLUMN_ADDRESS ((char)0x00)
36#define LCD_SET_HIGHER_COLUMN_ADDRESS ((char)0x10)
37#define LCD_SET_INTERNAL_REGULATOR_RESISTOR_RATIO ((char)0x20)
38#define LCD_SET_POWER_CONTROL_REGISTER ((char)0x28)
39#define LCD_SET_DISPLAY_START_LINE ((char)0x40)
40#define LCD_SET_CONTRAST_CONTROL_REGISTER ((char)0x81)
41#define LCD_SET_SEGMENT_REMAP ((char)0xA0)
42#define LCD_SET_LCD_BIAS ((char)0xA2)
43#define LCD_SET_ENTIRE_DISPLAY_OFF ((char)0xA4)
44#define LCD_SET_ENTIRE_DISPLAY_ON ((char)0xA5)
45#define LCD_SET_NORMAL_DISPLAY ((char)0xA6)
46#define LCD_SET_REVERSE_DISPLAY ((char)0xA7)
47#define LCD_SET_INDICATOR_OFF ((char)0xAC)
48#define LCD_SET_INDICATOR_ON ((char)0xAD)
49#define LCD_SET_DISPLAY_OFF ((char)0xAE)
50#define LCD_SET_DISPLAY_ON ((char)0xAF)
51#define LCD_SET_PAGE_ADDRESS ((char)0xB0)
52#define LCD_SET_COM_OUTPUT_SCAN_DIRECTION ((char)0xC0)
53#define LCD_SET_DISPLAY_OFFSET ((char)0xD3)
54#define LCD_SET_READ_MODIFY_WRITE_MODE ((char)0xE0)
55#define LCD_SOFTWARE_RESET ((char)0xE2)
56#define LCD_NOP ((char)0xE3)
57#define LCD_SET_END_OF_READ_MODIFY_WRITE_MODE ((char)0xEE)
58
59/* LCD command codes */
60#define LCD_CNTL_RESET 0xe2 // Software reset
61#define LCD_CNTL_POWER 0x2f // Power control
62#define LCD_CNTL_CONTRAST 0x81 // Contrast
63#define LCD_CNTL_OUTSCAN 0xc8 // Output scan direction
64#define LCD_CNTL_SEGREMAP 0xa1 // Segment remap
65#define LCD_CNTL_DISPON 0xaf // Display on
66
67#define LCD_CNTL_PAGE 0xb0 // Page address
68#define LCD_CNTL_HIGHCOL 0x10 // Upper column address
69#define LCD_CNTL_LOWCOL 0x00 // Lower column address
70
71struct scrollinfo {
72 char text[MAX_PATH];
73 char line[32];
74 int textlen;
75 int offset;
76 int startx;
77 int starty;
78 int space;
79};
80
81static void scroll_thread(void);
82static char scroll_stack[DEFAULT_STACK_SIZE];
83static char scroll_name[] = "scroll";
84static char scroll_speed = 8; /* updates per second */
85static char scroll_spacing = 3; /* spaces between end and start of text */
86static struct scrollinfo scroll; /* only one scroll line at the moment */
87static int scroll_count = 0;
88static int xmargin = 0;
89static int ymargin = 0;
90static int curfont = FONT_SYSFIXED;
91
92#ifdef SIMULATOR
93unsigned char lcd_framebuffer[LCD_WIDTH][LCD_HEIGHT/8];
94#else
95static unsigned char lcd_framebuffer[LCD_WIDTH][LCD_HEIGHT/8];
96#endif
97
98/* All zeros and ones bitmaps for area filling */
99static unsigned char zeros[8] = { 0 };
100static unsigned char ones[8] = { 0xff };
101
102#ifdef SIMULATOR
103
104void lcd_init(void)
105{
106 create_thread(scroll_thread, scroll_stack,
107 sizeof(scroll_stack), scroll_name);
108}
109
110#else
111
112/*
113 * Initialize LCD
114 */
115void lcd_init (void)
116{
117 /* Initialize PB0-3 as output pins */
118 PBCR2 &= 0xff00; /* MD = 00 */
119 PBIOR |= 0x000f; /* IOR = 1 */
120
121 lcd_clear_display();
122 lcd_update();
123 create_thread(scroll_thread, scroll_stack,
124 sizeof(scroll_stack), scroll_name);
125}
126
127/*
128 * Update the display.
129 * This must be called after all other LCD functions that change the display.
130 */
131void lcd_update (void) __attribute__ ((section (".icode")));
132void lcd_update (void)
133{
134 int x, y;
135
136 /* Copy display bitmap to hardware */
137 for (y = 0; y < LCD_HEIGHT/8; y++)
138 {
139 lcd_write (true, LCD_CNTL_PAGE | (y & 0xf));
140 lcd_write (true, LCD_CNTL_HIGHCOL);
141 lcd_write (true, LCD_CNTL_LOWCOL);
142
143 for (x = 0; x < LCD_WIDTH; x++)
144 lcd_write (false, lcd_framebuffer[x][y]);
145 }
146}
147
148/*
149 * Update a fraction of the display.
150 */
151void lcd_update_rect (int, int, int, int) __attribute__ ((section (".icode")));
152void lcd_update_rect (int x_start, int y,
153 int width, int height)
154{
155 int ymax;
156 int xmax;
157 int x;
158
159 /* The Y coordinates have to work on even 8 pixel rows */
160 ymax = (y + height)/8;
161 y /= 8;
162
163 xmax = x_start + width;
164
165 if(xmax > LCD_WIDTH)
166 xmax = LCD_WIDTH;
167 if(ymax >= LCD_HEIGHT/8)
168 ymax = LCD_HEIGHT/8-1;
169
170 /* Copy specified rectange bitmap to hardware */
171 for (; y <= ymax; y++)
172 {
173 lcd_write (true, LCD_CNTL_PAGE | (y & 0xf));
174 lcd_write (true, LCD_CNTL_HIGHCOL | ((x_start>>4) & 0xf));
175 lcd_write (true, LCD_CNTL_LOWCOL | (x_start & 0xf));
176
177 for (x = x_start; x < xmax; x++)
178 lcd_write (false, lcd_framebuffer[x][y]);
179 }
180}
181
182void lcd_set_contrast(int val)
183{
184 lcd_write(true, LCD_CNTL_CONTRAST);
185 lcd_write(true, val);
186}
187
188/**
189 * Rolls up the lcd display by the specified amount of lines.
190 * Lines that are rolled out over the top of the screen are
191 * rolled in from the bottom again. This is a hardware
192 * remapping only and all operations on the lcd are affected.
193 * ->
194 * @param int lines - The number of lines that are rolled.
195 * The value must be 0 <= pixels < LCD_HEIGHT.
196 */
197void lcd_roll(int lines)
198{
199 lcd_write(true, LCD_SET_DISPLAY_START_LINE | (lines & (LCD_HEIGHT-1)));
200}
201
202#endif /* SIMULATOR */
203
204void lcd_clear_display (void)
205{
206 memset (lcd_framebuffer, 0, sizeof lcd_framebuffer);
207}
208
209void lcd_setmargins(int x, int y)
210{
211 xmargin = x;
212 ymargin = y;
213}
214
215int lcd_getxmargin(void)
216{
217 return xmargin;
218}
219
220int lcd_getymargin(void)
221{
222 return ymargin;
223}
224
225void lcd_setfont(int newfont)
226{
227 curfont = newfont;
228}
229
230void lcd_getfontsize(int font, int *width, int *height)
231{
232 struct font* pf = font_get(font);
233
234 *width = pf->maxwidth;
235 *height = pf->height;
236}
237
238int lcd_getstringsize(unsigned char *str, int font, int *w, int *h)
239{
240 struct font* pf = font_get(font);
241 int ch;
242 int width = 0;
243
244 while((ch = *str++)) {
245 /* check input range*/
246 if (ch < pf->firstchar || ch >= pf->firstchar+pf->size)
247 ch = pf->defaultchar;
248 ch -= pf->firstchar;
249
250 /* get proportional width and glyph bits*/
251 width += pf->width? pf->width[ch]: pf->maxwidth;
252 }
253 *w = width;
254 *h = pf->height;
255
256 return width;
257}
258
259/* put a string at a given char position */
260void lcd_puts(int x, int y, unsigned char *str)
261{
262 int xpos,ypos,w,h;
263
264#if defined(SIMULATOR) && defined(HAVE_LCD_CHARCELLS)
265 /* We make the simulator truncate the string if it reaches the right edge,
266 as otherwise it'll wrap. The real target doesn't wrap. */
267
268 char buffer[12];
269 if(strlen(str)+x > 11 ) {
270 strncpy(buffer, str, sizeof buffer);
271 buffer[11-x]=0;
272 str = buffer;
273 }
274 xmargin = 0;
275 ymargin = 8;
276#endif
277
278 if(!str || !str[0])
279 return;
280
281 lcd_getstringsize(str, curfont, &w, &h);
282 xpos = xmargin + x*w / strlen(str);
283 ypos = ymargin + y*h;
284 lcd_putsxy( xpos, ypos, str, curfont);
285 lcd_clearrect(xpos + w, ypos, LCD_WIDTH - (xpos + w), h);
286
287#if defined(SIMULATOR) && defined(HAVE_LCD_CHARCELLS)
288 /* this function is being used when simulating a charcell LCD and
289 then we update immediately */
290 lcd_update();
291#endif
292}
293
294/* put a string at a given pixel position */
295void lcd_putsxy(int x, int y, unsigned char *str, int font)
296{
297 int ch;
298 struct font* pf = font_get(font);
299
300 while (((ch = *str++) != '\0')) {
301 bitmap_t *bits;
302 int width;
303
304 /* check input range*/
305 if (ch < pf->firstchar || ch >= pf->firstchar+pf->size)
306 ch = pf->defaultchar;
307 ch -= pf->firstchar;
308
309 /* get proportional width and glyph bits*/
310 width = pf->width ? pf->width[ch] : pf->maxwidth;
311 if (x + width > LCD_WIDTH)
312 break;
313
314 /* no partial-height drawing for now...*/
315 if (y + pf->height > LCD_HEIGHT)
316 break;
317 bits = pf->bits + (pf->offset ? pf->offset[ch] : (pf->height * ch));
318
319 lcd_bitmap((unsigned char *)bits, x, y, width, pf->height, true);
320 x += width;
321 }
322}
323
324/*
325 * All bitmaps have this format:
326 * Bits within a byte are arranged veritcally, LSB at top.
327 * Bytes are stored in column-major format, with byte 0 at top left,
328 * byte 1 is 2nd from top, etc. Bytes following left-most column
329 * starts 2nd left column, etc.
330 *
331 * Note: The HW takes bitmap bytes in row-major order.
332 *
333 * Memory copy of display bitmap
334 */
335
336/*
337 * Draw a bitmap at (x, y), size (nx, ny)
338 * if 'clear' is true, clear destination area first
339 */
340void lcd_bitmap (unsigned char *src, int x, int y, int nx, int ny,
341 bool clear) __attribute__ ((section (".icode")));
342void lcd_bitmap (unsigned char *src, int x, int y, int nx, int ny,
343 bool clear)
344{
345 unsigned char *dst;
346 unsigned char *dst2;
347 unsigned int data, mask, mask2, mask3, mask4;
348 int shift;
349
350 if (((unsigned)x >= LCD_WIDTH) || ((unsigned)y >= LCD_HEIGHT))
351 return;
352 if (((unsigned)(x + nx)) >= LCD_WIDTH)
353 nx = LCD_WIDTH - x;
354 if (((unsigned)(y + ny)) >= LCD_HEIGHT)
355 ny = LCD_HEIGHT - y;
356
357 shift = y & 7;
358 dst2 = &lcd_framebuffer[x][y/8];
359 ny += shift;
360
361 /* Calculate bit masks */
362 mask4 = ~(0xfe << ((ny-1) & 7));
363 if (clear)
364 {
365 mask = ~(0xff << shift);
366 mask2 = 0;
367 mask3 = ~mask4;
368 if (ny <= 8)
369 mask3 |= mask;
370 }
371 else
372 mask = mask2 = mask3 = 0xff;
373
374 /* Loop for each column */
375 for (x = 0; x < nx; x++)
376 {
377 dst = dst2;
378 dst2 += LCD_HEIGHT/8;
379 data = 0;
380 y = 0;
381
382 if (ny > 8)
383 {
384 /* First partial row */
385 data = *src++ << shift;
386 *dst = (*dst & mask) | data;
387 data >>= 8;
388 dst++;
389
390 /* Intermediate rows */
391 for (y = 8; y < ny-8; y += 8)
392 {
393 data |= *src++ << shift;
394 *dst = (*dst & mask2) | data;
395 data >>= 8;
396 dst++;
397 }
398 }
399
400 /* Last partial row */
401 if (y + shift < ny)
402 data |= *src++ << shift;
403 *dst = (*dst & mask3) | (data & mask4);
404 }
405}
406
407/*
408 * Draw a rectangle with upper left corner at (x, y)
409 * and size (nx, ny)
410 */
411void lcd_drawrect (int x, int y, int nx, int ny)
412{
413 int i;
414
415 if (x > LCD_WIDTH)
416 return;
417 if (y > LCD_HEIGHT)
418 return;
419
420 if (x + nx > LCD_WIDTH)
421 nx = LCD_WIDTH - x;
422 if (y + ny > LCD_HEIGHT)
423 ny = LCD_HEIGHT - y;
424
425 /* vertical lines */
426 for (i = 0; i < ny; i++) {
427 DRAW_PIXEL(x, (y + i));
428 DRAW_PIXEL((x + nx - 1), (y + i));
429 }
430
431 /* horizontal lines */
432 for (i = 0; i < nx; i++) {
433 DRAW_PIXEL((x + i),y);
434 DRAW_PIXEL((x + i),(y + ny - 1));
435 }
436}
437
438/*
439 * Clear a rectangular area at (x, y), size (nx, ny)
440 */
441void lcd_clearrect (int x, int y, int nx, int ny)
442{
443 int i;
444 for (i = 0; i < nx; i++)
445 lcd_bitmap (zeros, x+i, y, 1, ny, true);
446}
447
448/*
449 * Fill a rectangular area at (x, y), size (nx, ny)
450 */
451void lcd_fillrect (int x, int y, int nx, int ny)
452{
453 int i;
454 for (i = 0; i < nx; i++)
455 lcd_bitmap (ones, x+i, y, 1, ny, true);
456}
457
458/* Invert a rectangular area at (x, y), size (nx, ny) */
459void lcd_invertrect (int x, int y, int nx, int ny)
460{
461 int i, j;
462
463 if (x > LCD_WIDTH)
464 return;
465 if (y > LCD_HEIGHT)
466 return;
467
468 if (x + nx > LCD_WIDTH)
469 nx = LCD_WIDTH - x;
470 if (y + ny > LCD_HEIGHT)
471 ny = LCD_HEIGHT - y;
472
473 for (i = 0; i < nx; i++)
474 for (j = 0; j < ny; j++)
475 INVERT_PIXEL((x + i), (y + j));
476}
477
478void lcd_drawline( int x1, int y1, int x2, int y2 )
479{
480 int numpixels;
481 int i;
482 int deltax, deltay;
483 int d, dinc1, dinc2;
484 int x, xinc1, xinc2;
485 int y, yinc1, yinc2;
486
487 deltax = abs(x2 - x1);
488 deltay = abs(y2 - y1);
489
490 if(deltax >= deltay)
491 {
492 numpixels = deltax;
493 d = 2 * deltay - deltax;
494 dinc1 = deltay * 2;
495 dinc2 = (deltay - deltax) * 2;
496 xinc1 = 1;
497 xinc2 = 1;
498 yinc1 = 0;
499 yinc2 = 1;
500 }
501 else
502 {
503 numpixels = deltay;
504 d = 2 * deltax - deltay;
505 dinc1 = deltax * 2;
506 dinc2 = (deltax - deltay) * 2;
507 xinc1 = 0;
508 xinc2 = 1;
509 yinc1 = 1;
510 yinc2 = 1;
511 }
512 numpixels++; /* include endpoints */
513
514 if(x1 > x2)
515 {
516 xinc1 = -xinc1;
517 xinc2 = -xinc2;
518 }
519
520 if(y1 > y2)
521 {
522 yinc1 = -yinc1;
523 yinc2 = -yinc2;
524 }
525
526 x = x1;
527 y = y1;
528
529 for(i=0; i<numpixels; i++)
530 {
531 DRAW_PIXEL(x,y);
532
533 if(d < 0)
534 {
535 d += dinc1;
536 x += xinc1;
537 y += yinc1;
538 }
539 else
540 {
541 d += dinc2;
542 x += xinc2;
543 y += yinc2;
544 }
545 }
546}
547
548void lcd_clearline( int x1, int y1, int x2, int y2 )
549{
550 int numpixels;
551 int i;
552 int deltax, deltay;
553 int d, dinc1, dinc2;
554 int x, xinc1, xinc2;
555 int y, yinc1, yinc2;
556
557 deltax = abs(x2 - x1);
558 deltay = abs(y2 - y1);
559
560 if(deltax >= deltay)
561 {
562 numpixels = deltax;
563 d = 2 * deltay - deltax;
564 dinc1 = deltay * 2;
565 dinc2 = (deltay - deltax) * 2;
566 xinc1 = 1;
567 xinc2 = 1;
568 yinc1 = 0;
569 yinc2 = 1;
570 }
571 else
572 {
573 numpixels = deltay;
574 d = 2 * deltax - deltay;
575 dinc1 = deltax * 2;
576 dinc2 = (deltax - deltay) * 2;
577 xinc1 = 0;
578 xinc2 = 1;
579 yinc1 = 1;
580 yinc2 = 1;
581 }
582 numpixels++; /* include endpoints */
583
584 if(x1 > x2)
585 {
586 xinc1 = -xinc1;
587 xinc2 = -xinc2;
588 }
589
590 if(y1 > y2)
591 {
592 yinc1 = -yinc1;
593 yinc2 = -yinc2;
594 }
595
596 x = x1;
597 y = y1;
598
599 for(i=0; i<numpixels; i++)
600 {
601 CLEAR_PIXEL(x,y);
602
603 if(d < 0)
604 {
605 d += dinc1;
606 x += xinc1;
607 y += yinc1;
608 }
609 else
610 {
611 d += dinc2;
612 x += xinc2;
613 y += yinc2;
614 }
615 }
616}
617
618/*
619 * Set a single pixel
620 */
621void lcd_drawpixel(int x, int y)
622{
623 DRAW_PIXEL(x,y);
624}
625
626/*
627 * Clear a single pixel
628 */
629void lcd_clearpixel(int x, int y)
630{
631 CLEAR_PIXEL(x,y);
632}
633
634/*
635 * Invert a single pixel
636 */
637void lcd_invertpixel(int x, int y)
638{
639 INVERT_PIXEL(x,y);
640}
641
642void lcd_puts_scroll(int x, int y, unsigned char* string )
643{
644 struct scrollinfo* s = &scroll;
645 unsigned char ch[2];
646 int w, h;
647 int width, height;
648 lcd_getfontsize(curfont, &width, &height);
649
650 ch[1] = 0; /* zero terminate */
651 ch[0] = string[0];
652 width = 0;
653 s->space = 0;
654 while ( ch[0] &&
655 (width + lcd_getstringsize(ch, curfont, &w, &h) <
656 (LCD_WIDTH - x*8))) {
657 width += w;
658 s->space++;
659 ch[0]=string[s->space];
660 }
661
662 lcd_puts(x,y,string);
663 s->textlen = strlen(string);
664
665 s->space += 2;
666 lcd_getstringsize(string,curfont,&w,&h);
667 if ( w > LCD_WIDTH - xmargin ) {
668 s->offset=s->space;
669 s->startx=x;
670 s->starty=y;
671 strncpy(s->text,string,sizeof s->text);
672 s->text[sizeof s->text - 1] = 0;
673 memset(s->line, 0, sizeof s->line);
674 strncpy(s->line,string,
675 s->space > (int)sizeof s->line ?
676 (int)sizeof s->line : s->space );
677 s->line[sizeof s->line - 1] = 0;
678 scroll_count = 1;
679 }
680}
681
682
683void lcd_stop_scroll(void)
684{
685 if ( scroll_count ) {
686 int w,h;
687 struct scrollinfo* s = &scroll;
688 scroll_count = 0;
689
690 lcd_getstringsize( s->text, FONT_UI, &w, &h);
691 lcd_clearrect(xmargin + s->startx*w/s->textlen,
692 ymargin + s->starty*h,
693 LCD_WIDTH - xmargin,
694 h);
695
696 /* restore scrolled row */
697 lcd_puts(s->startx,s->starty,s->text);
698 lcd_update();
699 }
700}
701
702void lcd_scroll_pause(void)
703{
704 scroll_count = 0;
705}
706
707void lcd_scroll_resume(void)
708{
709 scroll_count = 1;
710}
711
712void lcd_scroll_speed(int speed)
713{
714 scroll_speed = speed;
715}
716
717static void scroll_thread(void)
718{
719 struct scrollinfo* s = &scroll;
720
721 while ( 1 ) {
722 if ( !scroll_count ) {
723 yield();
724 continue;
725 }
726 /* wait 0.5s before starting scroll */
727 if ( scroll_count < scroll_speed/2 )
728 scroll_count++;
729 else {
730 int i;
731 int w, h;
732 for ( i=0; i<s->space-1; i++ )
733 s->line[i] = s->line[i+1];
734
735 if ( s->offset < s->textlen ) {
736 s->line[(int)s->space - 1] = s->text[(int)s->offset];
737 s->offset++;
738 }
739 else {
740 s->line[s->space - 1] = ' ';
741 if ( s->offset < s->textlen + scroll_spacing - 1 )
742 s->offset++;
743 else
744 s->offset = 0;
745 }
746
747 lcd_getstringsize( s->text, FONT_UI, &w, &h);
748 lcd_clearrect(xmargin + s->startx*w/s->textlen,
749 ymargin + s->starty*h,
750 LCD_WIDTH - xmargin,
751 h);
752
753 lcd_puts(s->startx,s->starty,s->line);
754 lcd_update();
755 }
756 sleep(HZ/scroll_speed);
757 }
758}
759
760#endif
761
762/* -----------------------------------------------------------------
763 * local variables:
764 * eval: (load-file "../rockbox-mode.el")
765 * end:
766 */