Now you can exit the clock plugin again
[kugel-rb.git] / apps / misc.c
blob535cd818140138342ae61dfc1bc87a651ec53875
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Daniel Stenberg
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <ctype.h>
20 #include "string.h"
21 #include "config.h"
22 #include "file.h"
23 #include "lcd.h"
24 #include "sprintf.h"
25 #include "errno.h"
26 #include "system.h"
27 #include "timefuncs.h"
29 #define ONE_KILOBYTE 1024
30 #define ONE_MEGABYTE (1024*1024)
32 /* The point of this function would be to return a string of the input data,
33 but never longer than 5 columns. Add suffix k and M when suitable...
34 Make sure to have space for 6 bytes in the buffer. 5 letters plus the
35 terminating zero byte. */
36 char *num2max5(unsigned int bytes, char *max5)
38 if(bytes < 100000) {
39 snprintf(max5, 6, "%5d", bytes);
40 return max5;
42 if(bytes < (9999*ONE_KILOBYTE)) {
43 snprintf(max5, 6, "%4dk", bytes/ONE_KILOBYTE);
44 return max5;
46 if(bytes < (100*ONE_MEGABYTE)) {
47 /* 'XX.XM' is good as long as we're less than 100 megs */
48 snprintf(max5, 6, "%2d.%0dM",
49 bytes/ONE_MEGABYTE,
50 (bytes%ONE_MEGABYTE)/(ONE_MEGABYTE/10) );
51 return max5;
53 snprintf(max5, 6, "%4dM", bytes/ONE_MEGABYTE);
54 return max5;
57 /* Read (up to) a line of text from fd into buffer and return number of bytes
58 * read (which may be larger than the number of bytes stored in buffer). If
59 * an error occurs, -1 is returned (and buffer contains whatever could be
60 * read). A line is terminated by a LF char. Neither LF nor CR chars are
61 * stored in buffer.
63 int read_line(int fd, char* buffer, int buffer_size)
65 int count = 0;
66 int num_read = 0;
68 errno = 0;
70 while (count < buffer_size)
72 unsigned char c;
74 if (1 != read(fd, &c, 1))
75 break;
77 num_read++;
79 if ( c == '\n' )
80 break;
82 if ( c == '\r' )
83 continue;
85 buffer[count++] = c;
88 buffer[MIN(count, buffer_size - 1)] = 0;
90 return errno ? -1 : num_read;
93 #ifdef TEST_MAX5
94 int main(int argc, char **argv)
96 char buffer[32];
97 if(argc>1) {
98 printf("%d => %s\n",
99 atoi(argv[1]),
100 num2max5(atoi(argv[1]), buffer));
102 return 0;
105 #endif
107 #ifdef HAVE_LCD_BITMAP
108 extern unsigned char lcd_framebuffer[LCD_HEIGHT/8][LCD_WIDTH];
109 static unsigned char bmpheader[] =
111 0x42, 0x4d, 0x3e, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
112 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x40, 0x00,
113 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
114 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0xee, 0x90, 0x00, 0x00, 0x00,
116 0x00, 0x00
119 static unsigned char buf[112*8];
120 static unsigned char buf2[112*8];
121 static char dummy[2] = {0, 0};
123 void screen_dump(void)
125 int f;
126 int i, shift;
127 int x, y;
128 char filename[MAX_PATH];
129 struct tm *tm = get_time();
131 i = 0;
132 for(y = 0;y < LCD_HEIGHT/8;y++)
134 for(x = 0;x < LCD_WIDTH;x++)
136 buf[i++] = lcd_framebuffer[y][x];
140 memset(buf2, 0, sizeof(buf2));
142 for(y = 0;y < 64;y++)
144 shift = y & 7;
146 for(x = 0;x < 112/8;x++)
148 for(i = 0;i < 8;i++)
150 buf2[y*112/8+x] |= ((buf[y/8*112+x*8+i] >> shift)
151 & 0x01) << (7-i);
156 snprintf(filename, MAX_PATH, "/dump %04d-%02d-%02d %02d-%02d-%02d.bmp",
157 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
158 tm->tm_hour, tm->tm_min, tm->tm_sec);
159 f = creat(filename, O_WRONLY);
160 if(f >= 0)
162 write(f, bmpheader, sizeof(bmpheader));
164 for(i = 63;i >= 0;i--)
166 write(f, &buf2[i*14], 14);
167 write(f, dummy, 2);
170 close(f);
173 #endif
175 /* parse a line from a configuration file. the line format is:
177 name: value
179 Any whitespace before setting name or value (after ':') is ignored.
180 A # as first non-whitespace character discards the whole line.
181 Function sets pointers to null-terminated setting name and value.
182 Returns false if no valid config entry was found.
185 bool settings_parseline(char* line, char** name, char** value)
187 char* ptr;
189 while ( isspace(*line) )
190 line++;
192 if ( *line == '#' )
193 return false;
195 ptr = strchr(line, ':');
196 if ( !ptr )
197 return false;
199 *name = line;
200 *ptr = 0;
201 ptr++;
202 while (isspace(*ptr))
203 ptr++;
204 *value = ptr;
205 return true;