-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContext.h
71 lines (61 loc) · 1.84 KB
/
Context.h
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
#ifndef __CONTEXT_H__
#define __CONTEXT_H__
#include <SoftwareSerial.h>
#include "DFMiniMp3.h"
class Mp3Notify;
typedef struct {
DFMiniMp3<SoftwareSerial, Mp3Notify> *mp3;
bool isPlaying;
bool isPaused;
int currentFolder; // increment + 1 for usage in mp3 api
int currentTrack; // increment + 1 for usage in mp3 api
int tracksInFolders[13]; // ignoring index 0
} Context;
void togglePlayPause(Context *context) {
DFMiniMp3<SoftwareSerial, Mp3Notify> *mp3 = context->mp3;
if (context->isPlaying) {
if (context->isPaused) {
Serial.print(F("Play"));
mp3->start();
context->isPaused = false;
} else {
Serial.print(F("Pause"));
mp3->pause();
context->isPaused = true;
}
}
}
void playFolderAndTrack(int folder, int track, Context *context) {
DFMiniMp3<SoftwareSerial, Mp3Notify> *mp3 = context->mp3;
if (track <= context->tracksInFolders[folder]) {
// folders and tracks are 1-indexed
//play specific mp3 in SD:/01/001.mp3; Folder Name(1~99); File Name(1~255)
Serial.print(F("Playing track "));
Serial.print(track);
Serial.print(F(" in folder "));
Serial.println(folder);
mp3->playFolderTrack(folder, track);
context->isPlaying = true;
context->currentFolder = folder;
context->currentTrack = track;
} else {
Serial.print(F("Could not play track "));
Serial.print(track);
Serial.print(F(" in folder "));
Serial.println(folder);
Serial.println(F("Stopping"));
mp3->stop();
context->isPlaying = false;
context->currentFolder = 0;
context->currentTrack = 0;
}
}
void playNextTrack(Context *context) {
int currentFolder = context->currentFolder;
int currentTrack = context->currentTrack;
playFolderAndTrack(currentFolder, currentTrack+1, context);
}
void handlePlayFinished(Context *context) {
playNextTrack(context);
}
#endif