FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / firmware / logf.c
blob8817d4df45f1c1595c30d985426ec46b56f36564
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Daniel Stenberg
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 ****************************************************************************/
23 * logf() logs entries in a circular buffer. Each logged string is null-terminated.
25 * When the length of log exceeds MAX_LOGF_SIZE bytes, the buffer wraps.
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <sprintf.h>
33 #include <system.h>
34 #include <font.h>
35 #include "config.h"
36 #include "lcd-remote.h"
37 #include "logf.h"
38 #include "serial.h"
40 #ifdef HAVE_USBSTACK
41 #include "usb_core.h"
42 #include "usbstack/usb_serial.h"
43 #endif
45 /* Only provide all this if asked to */
46 #ifdef ROCKBOX_HAS_LOGF
48 #ifndef __PCTOOL__
49 unsigned char logfbuffer[MAX_LOGF_SIZE];
50 int logfindex;
51 bool logfwrap;
52 #endif
54 #ifdef HAVE_REMOTE_LCD
55 static void displayremote(void)
57 /* TODO: we should have a debug option that enables/disables this! */
58 int w, h, i;
59 int fontnr;
60 int cur_x, cur_y, delta_y, delta_x;
61 struct font* font;
62 int nb_lines;
63 char buf[2];
64 /* Memorize the pointer to the beginning of the last ... lines
65 I assume delta_y >= 6 to avoid wasting memory and allocating memory dynamically
66 I hope there is no font with height < 6 ! */
67 const int NB_ENTRIES=LCD_REMOTE_HEIGHT / 6;
68 int line_start_ptr[NB_ENTRIES];
70 fontnr = lcd_getfont();
71 font = font_get(fontnr);
73 /* get the horizontal size of each line */
74 font_getstringsize("A", NULL, &delta_y, fontnr);
76 /* font too small ? */
77 if(delta_y < 6)
78 return;
79 /* nothing to print ? */
80 if(logfindex == 0 && !logfwrap)
81 return;
83 w = LCD_REMOTE_WIDTH;
84 h = LCD_REMOTE_HEIGHT;
85 nb_lines = 0;
87 if(logfwrap)
88 i = logfindex;
89 else
90 i = 0;
92 cur_x = 0;
94 line_start_ptr[0] = i;
98 if(logfbuffer[i] == '\0')
100 line_start_ptr[++nb_lines % NB_ENTRIES] = i+1;
101 cur_x = 0;
103 else
105 /* does character fit on this line ? */
106 delta_x = font_get_width(font, logfbuffer[i]);
108 if(cur_x + delta_x > w)
110 cur_x = 0;
111 line_start_ptr[++nb_lines % NB_ENTRIES] = i;
113 /* update pointer */
114 cur_x += delta_x;
116 i++;
117 if(i >= MAX_LOGF_SIZE)
118 i = 0;
119 } while(i != logfindex);
121 lcd_remote_clear_display();
123 i = line_start_ptr[ MAX(nb_lines - h / delta_y, 0) % NB_ENTRIES];
124 cur_x = 0;
125 cur_y = 0;
126 buf[1] = '\0';
128 do {
129 if(logfbuffer[i] == '\0')
131 cur_y += delta_y;
132 cur_x = 0;
134 else
136 /* does character fit on this line ? */
137 delta_x = font_get_width(font, logfbuffer[i]);
139 if(cur_x + delta_x > w)
141 cur_y += delta_y;
142 cur_x = 0;
145 buf[0] = logfbuffer[i];
146 lcd_remote_putsxy(cur_x, cur_y, buf);
147 cur_x += delta_x;
150 i++;
151 if(i >= MAX_LOGF_SIZE)
152 i = 0;
153 } while(i != logfindex);
155 lcd_remote_update();
157 #else
158 #define displayremote()
159 #endif
161 #ifdef __PCTOOL__
162 void _logf(const char *format, ...)
164 char buf[1024];
165 va_list ap;
166 va_start(ap, format);
168 vsnprintf(buf, sizeof buf, format, ap);
169 printf("DEBUG: %s\n", buf);
171 #else
172 static void check_logfindex(void)
174 if(logfindex >= MAX_LOGF_SIZE)
176 /* wrap */
177 logfwrap = true;
178 logfindex = 0;
182 static int logf_push(void *userp, unsigned char c)
184 (void)userp;
186 logfbuffer[logfindex++] = c;
187 check_logfindex();
189 #if defined(HAVE_SERIAL) && !defined(SIMULATOR)
190 if(c != '\0')
192 char buf[2];
193 buf[0] = c;
194 buf[1] = '\0';
195 serial_tx(buf);
197 #endif
199 return true;
202 void _logf(const char *fmt, ...)
204 #ifdef USB_ENABLE_SERIAL
205 int old_logfindex = logfindex;
206 #endif
207 va_list ap;
209 va_start(ap, fmt);
210 vfnprintf(logf_push, NULL, fmt, ap);
211 va_end(ap);
213 /* add trailing zero */
214 logf_push(NULL, '\0');
216 #if defined(HAVE_SERIAL) && !defined(SIMULATOR)
217 serial_tx("\r\n");
218 #endif
219 #ifdef USB_ENABLE_SERIAL
221 if(logfindex < old_logfindex)
223 usb_serial_send(logfbuffer + old_logfindex, MAX_LOGF_SIZE - old_logfindex);
224 usb_serial_send(logfbuffer, logfindex - 1);
226 else
227 usb_serial_send(logfbuffer + old_logfindex, logfindex - old_logfindex - 1);
228 usb_serial_send("\r\n", 2);
229 #endif
231 displayremote();
233 #endif
235 #endif