Subversion Repositories freemyipod

Rev

Rev 714 | Rev 716 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 user890104 1
//
2
//
3
//    Copyright 2011 TheSeven, user890104
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "emcoreapp.h"
25
#include "beep.h"
26
 
27
void playsong(void *buf, size_t size);
28
 
29
static void main()
30
{
31
    if (0x47324e49 != get_platform_id()) // IN2G
32
    {
33
        cputs(3, "Your device is not supported!\n");
34
        return;
35
    }
36
 
37
    size_t size;
38
    int fd;
39
    void *buf;
40
    unsigned char play;
41
 
42
    play = 0;
43
    fd = file_open("/song.dat", O_RDONLY);
44
 
45
    if (fd <= 0)
46
    {
47
        cputs(3, "Unable to open /song.dat!\n");
48
        return;
49
    }
50
 
51
    cputs(3, "File opened\n");
52
    size = filesize(fd);
53
 
54
    if (size <= 0)
55
    {
56
        close(fd);
57
        cputs(3, "Unable to get file size or file is empty!\n");
58
        return;
59
    }
60
 
61
    cprintf(3, "File size: %d bytes\n", size);
62
 
63
    if (0 != size % 8)
64
    {
65
        close(fd);
66
        cputs(3, "Invalid file, unable to continue!\n");
67
        return;
68
    }
69
 
70
    buf = memalign(0x10, size);
71
 
72
    if (!buf)
73
    {
74
        close(fd);
75
        cputs(3, "Memory allocation failed!\n");
76
        return;
77
    }
78
 
79
    if (size != read(fd, buf, size))
80
    {
81
        free(buf);
82
        close(fd);
83
        cputs(3, "Read error!\n");
84
        return;
85
    }
86
 
87
    cputs(3, "Read successful\n");
88
    close(fd);
89
    cputs(3, "File closed\n");
90
    playsong(buf, size);
91
    free(buf);
92
}
93
 
94
void playsong(void *buf, size_t size) {
95
    int i;
715 user890104 96
    unsigned int *cycles, *lengths, count;
714 user890104 97
 
715 user890104 98
    count = size / 8;
99
    cycles = malloc(count * sizeof(unsigned int));
100
    lengths = malloc(count * sizeof(unsigned int));
101
 
102
    for (i = 0; i < count; ++i)
714 user890104 103
    {
104
        cycles[i] = *((unsigned int *)(buf) + i + i);
105
        lengths[i] = *((unsigned int *)(buf) + i + i + 1);
106
    }
107
 
715 user890104 108
    for (i = 0; i < count; ++i)
714 user890104 109
    {
110
        if (0 == cycles[i])
111
        {
112
            sleep(lengths[i]);
113
            continue;
114
        }
115
 
116
        singlebeep(cycles[i], lengths[i]);
117
    }
715 user890104 118
 
119
    free(lengths);
120
    free(cycles);
714 user890104 121
}
122
 
123
EMCORE_APP_HEADER("Beeper", main, 127)