forked from sansumbrella/MP3Trigger-for-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MP3Trigger.cpp
197 lines (173 loc) · 2.66 KB
/
MP3Trigger.cpp
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
/*
MP3Trigger.cpp
@author David Wicks
@url sansumbrella.com
*/
#include "MP3Trigger.h"
MP3Trigger::MP3Trigger()
{
mDoLoop = false;
mPlaying = false;
}
MP3Trigger::~MP3Trigger()
{
s->flush();
s = NULL;
}
void MP3Trigger::setup(Stream *serial)
{
s = serial;
}
//
// Looping functions
//
void MP3Trigger::setLooping(bool doLoop, byte track)
{
mDoLoop = doLoop;
mLoopTrack = track;
if(!mPlaying && mDoLoop)
{
loop();
}
}
void MP3Trigger::setLoopingTrack(byte track)
{
mLoopTrack = track;
}
void MP3Trigger::update()
{
if( s->available() )
{
int data = s->read();
switch(char(data))
{
case 'X':
handleTrackEnd();
break;
case 'x':
if(!mPlaying) {
handleTrackEnd();
}
break;
case 'E':
mPlaying = false;
break;
case 'M':
handleTriggerInput();
break;
default:
break;
}
}
}
void MP3Trigger::handleTrackEnd()
{
if(mDoLoop)
{
loop();
} else
{
mPlaying = false;
}
}
void MP3Trigger::handleTriggerInput()
{
byte data;
byte reads = 0;
byte retries = 0;
byte i;
while(reads < 3 && retries < 1000) {
if(s->available()) {
data = (byte) s->read();
if(data) {
for(i = 0; i < 8; i++) {
if( (data >> i) & B00000001 ) {
quickModeCallback(i + 8*(2-reads) + 1);
}
}
}
reads++;
}
else {
retries++;
}
}
}
void MP3Trigger::loop()
{
trigger(mLoopTrack);
}
void MP3Trigger::stop()
{
mDoLoop = false;
if(mPlaying)
{
play();
}
}
//
// Single-byte built-in functions
//
void MP3Trigger::play()
{
s->write('O');
mPlaying = !mPlaying;
}
void MP3Trigger::forward()
{
s->write('F');
}
void MP3Trigger::reverse()
{
s->write('R');
}
//
// Built-in two-byte functions
//
void MP3Trigger::trigger(byte track)
{
s->write('t');
s->write(track);
mPlaying = true;
}
void MP3Trigger::play(byte track)
{
s->write('p');
s->write(track);
mPlaying = true;
}
void MP3Trigger::setVolume(byte level)
{
// level = level ^ B11111111; //flip it around, so the higher number > higher volume
s->write('v');
s->write(level);
}
// 1 for on, 0 for off, callback function
void MP3Trigger::quietMode(boolean onoff, void (*callback)(int))
{
s->write('Q');
s->write( '0' + onoff );
quickModeCallback = callback;
}
//
// Response functions
//
void MP3Trigger::statusRequest()
{
s->println("MP3Trigger::statusRequest is not yet implemented");
s->flush();
s->write('S');
s->write('1');
delay(5);
s->read();
//will need to work on this one to make it useful
// if (Serial.available() > 0)
// {
// // read the incoming byte:
// int incomingByte = Serial.read();
//
// // say what you got:
// Serial.print("I received: ");
// Serial.println(incomingByte, DEC);
// }
}