summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/fscanf.c
blob: 9f5f129d3cc46d5dcc879b73b069f7c851384503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copied from firmware/common/sscanf.c
 * Original author: Tomasz Malesinski
 *
 * Copyright (C) 2010 Maurus Cuelenaere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "rocklibc.h"

static int parse_dec(int (*peek)(void *userp),
                     void (*pop)(void *userp),
                     void *userp,
                     long *vp)
{
    long v = 0;
    int n = 0;
    int minus = 0;
    char ch;

    if ((*peek)(userp) == '-')
    {
        (*pop)(userp);
        n++;
        minus = 1;
    }

    ch = (*peek)(userp);
    if (!isdigit(ch))
        return -1;

    do
    {
        v = v * 10 + ch - '0';
        (*pop)(userp);
        n++;
        ch = (*peek)(userp);
    } while (isdigit(ch));

    *vp = minus ? -v : v;
    return n;
}

static int parse_chars(int (*peek)(void *userp),
                       void (*pop)(void *userp),
                       void *userp,
                       char *vp,
                       bool fake)
{
    int n = 0;

    char *pt=vp;

    while (!isspace((*peek)(userp)))
    {
        if(fake==false)
            *(pt++) = (*peek)(userp);

        n++;
        (*pop)(userp);
    }

    if(fake==false)
        (*pt)='\0';

    return n;
}

static int parse_hex(int (*peek)(void *userp),
                     void (*pop)(void *userp),
                     void *userp,
                     unsigned long *vp)
{
    unsigned long v = 0;
    int n = 0;
    char ch;

    ch = (*peek)(userp);
    if (!isxdigit(ch))
        return -1;

    do
    {
        if (ch >= 'a')
            ch = ch - 'a' + 10;
        else if (ch >= 'A')
            ch = ch - 'A' + 10;
        else
            ch = ch - '0';
        v = v * 16 + ch;
        (*pop)(userp);
        n++;
        ch = (*peek)(userp);
    } while (isxdigit(ch));

    *vp = v;
    return n;
}

static int skip_spaces(int (*peek)(void *userp),
                       void (*pop)(void *userp),
                       void *userp)
{
    int n = 0;
    while (isspace((*peek)(userp))) {
        n++;
        (*pop)(userp);
    }
    return n;
}

static int scan(int (*peek)(void *userp),
                void (*pop)(void *userp),
                void *userp,
                const char *fmt,
                va_list ap)
{
    char ch;
    int n = 0;
    int n_chars = 0;
    int r;
    long lval;
    bool skip=false;
    unsigned long ulval;

    while ((ch = *fmt++) != '\0')
    {
        bool literal = false;

        if (ch == '%')
        {
            ch = *fmt++;

            if(ch== '*')  /* We should process this, but not store it in an argument */
            {
                ch=*fmt++;
                skip=true;
            }
            else
            {
                skip=false;
            }

            switch (ch)
            {
                case 'x':
                    n_chars += skip_spaces(peek, pop, userp);
                    if ((r = parse_hex(peek, pop, userp, &ulval)) >= 0)
                    {
                        if(skip==false)
                        {
                            *(va_arg(ap, unsigned int *)) = ulval;
                            n++;
                        }
                        n_chars += r;
                    }
                    else
                        return n;
                    break;
                case 'd':
                    n_chars += skip_spaces(peek, pop, userp);
                    if ((r = parse_dec(peek, pop, userp, &lval)) >= 0)
                    {
                        if(skip==false)
                        {
                            *(va_arg(ap, int *)) = lval;
                            n++;
                        }
                        n_chars += r;
                    }
                    else
                        return n;
                    break;
                case 'n':
                    if(skip==false)
                    {
                        *(va_arg(ap, int *)) = n_chars;
                        n++;
                    }
                    break;
                case 'l':
                    n_chars += skip_spaces(peek, pop, userp);
                    ch = *fmt++;
                    switch (ch)
                    {
                        case 'x':
                            if ((r = parse_hex(peek, pop, userp, &ulval)) >= 0)
                            {
                                if(skip==false)
                                {
                                    *(va_arg(ap, unsigned long *)) = ulval;
                                    n++;
                                }
                                n_chars += r;
                            }
                            else
                                return n;
                            break;
                        case 'd':
                            if ((r = parse_dec(peek, pop, userp, &lval)) >= 0)
                            {
                                if(skip==false)
                                {
                                    *(va_arg(ap, long *)) = lval;
                                    n++;
                                }
                                n_chars += r;
                            }
                            else
                                return n;
                            break;
                        case '\0':
                            return n;
                        default:
                            literal = true;
                            break;
                    }
                    break;
                case 's':
                    n_chars += skip_spaces(peek, pop, userp);
                    n_chars += parse_chars(peek,pop, userp,skip?0:va_arg(ap, char *), skip );
                    if(skip==false)
                    {
                        n++;
                    }
                    break;
                case '\0':
                    return n;
                default:
                    literal = true;
                    break;
            }
        } else
            literal = true;

        if (literal)
        {
            n_chars += skip_spaces(peek, pop, userp);
            if ((*peek)(userp) != ch)
                continue;
            else
            {
                (*pop)(userp);
                n_chars++;
            }
        }
    }
    return n;
}

static int fspeek(void *userp)
{
    int fd = *((int*) userp);
    char buf = 0;
    if(rb->read(fd, &buf, 1) == 1)
        rb->lseek(fd, -1, SEEK_CUR);
    return buf;
}

static void fspop(void *userp)
{
    int fd = *((int*) userp);
    rb->lseek(fd, 1, SEEK_CUR);
}

int PREFIX(fscanf)(int fd, const char *fmt, ...)
{
    int r;
    va_list ap;

    va_start(ap, fmt);
    r = scan(fspeek, fspop, &fd, fmt, ap);
    va_end(ap);
    return r;
}
pt">, "bytes_read/*4 err: %ld",(long int)bytes_read); rb->close (file); return -1; } sampleshort = (int16_t*)samples; sampleval = letoh16(*sampleshort); peak->lmin = sampleval; peak->lmax = sampleval; sampleval = letoh16(*(sampleshort+1)); peak->rmin = sampleval; peak->rmax = sampleval; while(bytes_read) { sampleval = letoh16(*sampleshort++); if(sampleval < peak->lmin) peak->lmin = sampleval; else if (sampleval > peak->lmax) peak->lmax = sampleval; sampleval = letoh16(*sampleshort++); if(sampleval < peak->rmin) peak->rmin = sampleval; else if (sampleval > peak->rmax) peak->rmax = sampleval; bytes_read -= 4; peakcount++; fppmp_count--; if(!fppmp_count) { peak++; mempeakcount++; fppmp_count = fppmp; sampleval = letoh16(*sampleshort); peak->lmin = sampleval; peak->lmax = sampleval; sampleval = letoh16(*(sampleshort+1)); peak->rmin = sampleval; peak->rmax = sampleval; } } /* update progress */ rb->snprintf(tstr,127, "Searching for peaks... %d%%",(int) (total_bytes_read / ((header.datachunksize + sizeof(struct wav_header)) / 100))); rb->lcd_puts(0, 6, tstr); rb->lcd_update(); /* allow user to abort */ if(ACTION_KBD_ABORT == rb->get_action(CONTEXT_KEYBOARD,TIMEOUT_NOBLOCK)) { rb->splash(HZ*2, "ABORTED"); rb->close (file); return -1; } } rb->lcd_puts(0, 6, "Searching for peaks... done"); rb->lcd_update(); rb->close (file); return 0; } int displaypeaks(void) { register int x = 0; register int lymin = INT_MAX; register int lymax = INT_MIN; register int rymin = INT_MAX; register int rymax = INT_MIN; register unsigned int peakcount = 0; struct peakstruct* peak = (struct peakstruct*)audiobuf + leftmargin; #if LCD_DEPTH > 1 unsigned org_forecolor = rb->lcd_get_foreground(); rb->lcd_set_foreground(LCD_LIGHTGRAY); #endif if(!zoomlevel) zoomlevel = 1; ppp = (mempeakcount / LCD_WIDTH) / zoomlevel; /* peaks per pixel */ rb->lcd_clear_display(); rb->lcd_drawline(0, LEFTZERO - (0x8000 / YSCALE), LCD_WIDTH-1, LEFTZERO - (0x8000 / YSCALE)); rb->lcd_drawline(0, LEFTZERO, LCD_WIDTH-1, LEFTZERO); rb->lcd_drawline(0, LEFTZERO + (0x8000 / YSCALE), LCD_WIDTH-1, LEFTZERO + (0x8000 / YSCALE)); rb->lcd_drawline(0, RIGHTZERO - (0x8000 / YSCALE), LCD_WIDTH-1, RIGHTZERO - (0x8000 / YSCALE)); rb->lcd_drawline(0, RIGHTZERO, LCD_WIDTH-1, RIGHTZERO); rb->lcd_drawline(0, RIGHTZERO + (0x8000 / YSCALE), LCD_WIDTH-1, RIGHTZERO + (0x8000 / YSCALE)); #if LCD_DEPTH > 1 rb->lcd_set_foreground(LCD_BLACK); #endif /* draw zoombar */ rb->lcd_drawline(leftmargin / (mempeakcount / LCD_WIDTH), LCD_HEIGHT / 2, (leftmargin / (mempeakcount / LCD_WIDTH)) + (LCD_WIDTH / zoomlevel), LCD_HEIGHT / 2); while((x < LCD_WIDTH) && (peakcount < mempeakcount)) { if(peak->lmin < lymin) lymin = peak->lmin; if(peak->lmax > lymax) lymax = peak->lmax; if(peak->rmin < rymin) rymin = peak->rmin; if(peak->rmax > rymax) rymax = peak->rmax; peak++; if(0 == (peakcount % ppp)) { /* drawing time */ rb->lcd_drawline(x, LEFTZERO - (lymax / YSCALE), x, LEFTZERO - (lymin / YSCALE)); rb->lcd_drawline(x, RIGHTZERO - (rymax / YSCALE), x, RIGHTZERO - (rymin / YSCALE)); lymin = INT_MAX; lymax = INT_MIN; rymin = INT_MAX; rymax = INT_MIN; x++; rb->lcd_update(); } peakcount++; } #if LCD_DEPTH > 1 rb->lcd_set_foreground(org_forecolor); #endif return 0; } void show_help(void) { rb->lcd_clear_display(); rb->lcd_puts(0, 0, "WAVVIEW USAGE:"); rb->lcd_puts(0, 2, "up/down: zoom out/in"); rb->lcd_puts(0, 3, "left/right: pan left/right"); rb->lcd_puts(0, 4, "select: refresh/continue"); rb->lcd_puts(0, 5, "stop/off: quit"); rb->lcd_update(); } enum plugin_status plugin_start(struct plugin_api* api, void *parameter) { unsigned int quit = 0; unsigned int action = 0; unsigned int dodisplay = 1; rb = api; int retval; if (!parameter) return PLUGIN_ERROR; audiobuf = rb->plugin_get_audio_buffer((size_t *)&audiobuflen); if (!audiobuf) { rb->splash(HZ*2, "unable to get audio buffer!"); return PLUGIN_ERROR; } #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(true); #endif retval = readwavpeaks(parameter); /* read WAV file and create peaks array */ #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(false); #endif if(retval) return 0; /* press any key to continue */ while(1) { retval = rb->get_action(CONTEXT_KEYBOARD,TIMEOUT_BLOCK); if(ACTION_KBD_ABORT == retval) return 0; else if(ACTION_KBD_SELECT == retval) break; } /* start with the overview */ zoomlevel = 1; leftmargin = 0; while(!quit) { if(!center) center = mempeakcount / 2; if(zoomlevel <= 1) { zoomlevel = 1; leftmargin = 0; } else { if(center < (mempeakcount / (zoomlevel * 2))) center = mempeakcount / (zoomlevel * 2); if(center > (((zoomlevel * 2) - 1) * (mempeakcount / (zoomlevel * 2)))) center = ((zoomlevel * 2) - 1) * (mempeakcount / (zoomlevel * 2)); leftmargin = center - (mempeakcount / (zoomlevel * 2)); } #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(true); #endif if(dodisplay) displaypeaks(); dodisplay = 1; #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(false); #endif action = rb->get_action(CONTEXT_KEYBOARD, TIMEOUT_BLOCK); switch(action) { case ACTION_KBD_UP: /* zoom out */ if(zoomlevel > 1) zoomlevel /= 2; rb->splash(HZ/2, "ZOOM: %dx",(int)zoomlevel); break; case ACTION_KBD_DOWN: if(zoomlevel < (mempeakcount / LCD_WIDTH / 2)) zoomlevel *= 2; rb->splash(HZ/2, "ZOOM: %dx",(int)zoomlevel); break; case ACTION_KBD_LEFT: center -= 10 * (mempeakcount / LCD_WIDTH) / zoomlevel; break; case ACTION_KBD_RIGHT: center += 10 * (mempeakcount / LCD_WIDTH) / zoomlevel; break; case ACTION_KBD_ABORT: quit = 1; break; case ACTION_KBD_SELECT: /* refresh */ break; case ACTION_KBD_PAGE_FLIP: /* menu key shows help */ show_help(); while(1) { retval = rb->get_action(CONTEXT_KEYBOARD,TIMEOUT_BLOCK); if((ACTION_KBD_SELECT == retval) || (ACTION_KBD_ABORT == retval)) break; } break; default: /* eat it */ dodisplay = 0; break; } } return 0; }