-
Notifications
You must be signed in to change notification settings - Fork 5
/
Playlist.h
154 lines (134 loc) · 3.94 KB
/
Playlist.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
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
/*
* The Closed Player - Kid-friendly MP3 player based on RFID tags
*
* See README.md for details and hardware setup.
*
* Copyright (c) 2019 Thomas Friedrichsmeier
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <SD.h>
#include <vector>
/** Holds a collection of files to play. The collection may contain sub-directories, in which case a sub-playlist will be created for the files in the directory, as and when needed.
* Files in a directory are sorted, but based on 8.3 naming. Still useful, as long as files are named with an numeric prefix for ordering. */
class Playlist {
public:
Playlist() {
current = -1;
sublist = 0;
wifi_enabled = false;
}
Playlist(std::vector<String> items) : Playlist(){
entries = items;
}
Playlist(File directory) : Playlist() {
Serial.print("Creating playlist for ");
Serial.print(directory.name());
directory.rewindDirectory();
File entry = directory.openNextFile();
while (entry) {
if (entry.isDirectory()) {
entries.push_back(entry.name());
} else {
String n = String (entry.name());
n.toLowerCase();
if (n.endsWith(".mp3")) {
entries.push_back(entry.name());
}
}
entry = directory.openNextFile();
}
std::sort(entries.begin(), entries.end());
Serial.print(entries.size());
Serial.println(" entries.");
}
~Playlist() {
delete sublist;
}
String next() {
if (sublist) {
String ret = sublist->next();
if (ret.length() > 0) return ret;
// If we get here, sublist was depleted
delete sublist;
sublist = 0;
}
if (++current >= entries.size()) return String();
String ret = entries[current];
File f = SD.open(ret);
if (f.isDirectory()) {
sublist = new Playlist(f);
return next(); // NOTE: Calling on self, as the sublist _might_ be empty
}
return ret;
}
String previous() {
if (sublist) {
String ret = sublist->previous();
if (ret.length() > 0) return ret;
// If we get here, sublist was depleted
delete sublist;
sublist = 0;
}
if (--current < 0) return String();
String ret = entries[current];
File f = SD.open(ret);
if (f.isDirectory()) {
sublist = new Playlist(f);
sublist->current = sublist->entries.size() - 1;
return previous(); // NOTE: Calling on self, as the sublist _might_ be empty
}
return ret;
}
String getCurrent() const {
if (sublist) return sublist->getCurrent();
if (current < 0 || isEmpty()) return String();
return entries[current];
}
bool isEmpty() const {
return entries.size() < 1;
}
void reset() {
if (sublist) {
delete sublist;
sublist = 0;
}
current = -1;
}
String serialize() const {
String ret = String(current);
if (sublist) {
ret += ",";
ret += sublist->serialize();
}
return ret;
}
void unserialize(std::vector<String> positions) {
if(positions.size() < 1) return;
int pos = positions[0].toInt();
current = pos - 1;
next();
if (sublist) {
positions.erase(positions.begin(), positions.begin() + 1);
sublist->unserialize(positions);
}
}
bool wifi_enabled;
protected:
std::vector<String> entries;
int current;
Playlist *sublist;
};
#endif