-
Notifications
You must be signed in to change notification settings - Fork 0
/
JC3248W535C_AviPlayer2.ino
301 lines (249 loc) · 7.08 KB
/
JC3248W535C_AviPlayer2.ino
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
292
293
294
295
296
297
298
299
300
301
/*******************************************************************************
* AVI Player example
*
* Dependent libraries:
* Arduino_GFX: https://github.com/moononournation/Arduino_GFX.git
* avilib: https://github.com/lanyou1900/avilib.git
* libhelix: https://github.com/pschatzmann/arduino-libhelix.git
* ESP32_JPEG: https://github.com/esp-arduino-libs/ESP32_JPEG.git
*
* Setup steps:
* 1. Change your LCD parameters in Arduino_GFX setting
* 2. Upload AVI file
* FFat/LittleFS:
* upload FFat (FatFS) data with ESP32 Sketch Data Upload:
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
* SD:
* Copy files to SD card
*
* Video Format:
* code "cvid": Cinepak
* cod "MJPG": MJPEG
*
* Audio Format:
* code 1: PCM
* code 85: MP3
******************************************************************************/
const char *root = "/root";
const char *avi_folder = "/avi320x480";
// #ifdef CANVAS_R1
// const char *avi_folder = "/avi480x320";
// #else
// const char *avi_folder = "/avi320x480";
// #endif
#include "JC3248W535.h"
// set up for random play
#define MAX_FILES 100
// Array to store file names
char fileNames[MAX_FILES][13]; // 8.3 filename format: 8 chars + dot + 3 chars + null terminator
int fileCount = 0;
#include <FFat.h>
#include <LittleFS.h>
#include <SPIFFS.h>
#include <SD.h>
#include <SD_MMC.h>
size_t output_buf_size;
uint16_t *output_buf;
#include "AviFunc.h"
#include "esp32_audio.h"
void setup()
{
Serial.begin(115200);
// Serial.setDebugOutput(true);
// while(!Serial);
Serial.println("AVI Player");
randomSeed(analogRead(0)); // Initialize random number generator
// If display and SD shared same interface, init SPI first
#ifdef SPI_SCK
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
#endif
#ifdef GFX_EXTRA_PRE_INIT
GFX_EXTRA_PRE_INIT();
#endif
// Init Display
// if (!gfx->begin())
if (!gfx->begin(GFX_SPEED))
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(BLACK);
#ifdef CANVAS
gfx->flush();
#endif
#ifdef GFX_BL
#if defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR < 3)
ledcSetup(0, 1000, 8);
ledcAttachPin(GFX_BL, 0);
ledcWrite(0, 204);
#else // ESP_ARDUINO_VERSION_MAJOR >= 3
ledcAttachChannel(GFX_BL, 1000, 8, 1);
ledcWrite(GFX_BL, 204);
#endif // ESP_ARDUINO_VERSION_MAJOR >= 3
#endif // GFX_BL
// gfx->setTextColor(WHITE, BLACK);
// gfx->setTextBound(60, 60, 240, 240);
#ifdef AUDIO_MUTE_PIN
pinMode(AUDIO_MUTE_PIN, OUTPUT);
digitalWrite(AUDIO_MUTE_PIN, HIGH);
#endif
i2s_init();
#if defined(SD_D1)
#define FILESYSTEM SD_MMC
SD_MMC.setPins(SD_SCK, SD_MOSI /* CMD */, SD_MISO /* D0 */, SD_D1, SD_D2, SD_CS /* D3 */);
if (!SD_MMC.begin(root, false /* mode1bit */, false /* format_if_mount_failed */, SDMMC_FREQ_HIGHSPEED))
#elif defined(SD_SCK)
#define FILESYSTEM SD_MMC
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
SD_MMC.setPins(SD_SCK, SD_MOSI /* CMD */, SD_MISO /* D0 */);
if (!SD_MMC.begin(root, true /* mode1bit */, false /* format_if_mount_failed */, SDMMC_FREQ_HIGHSPEED))
#elif defined(SD_CS)
#define FILESYSTEM SD
if (!SD.begin(SD_CS, SPI, 80000000, "/root"))
#else
#define FILESYSTEM FFat
if (!FFat.begin(false, root))
// if (!LittleFS.begin(false, root))
// if (!SPIFFS.begin(false, root))
#endif
{
Serial.println("ERROR: File system mount failed!");
}
else
{
output_buf_size = gfx->width() * gfx->height() * 2;
#ifdef RGB_PANEL
output_buf = gfx->getFramebuffer();
#else
output_buf = (uint16_t *)aligned_alloc(16, output_buf_size);
#endif
if (!output_buf)
{
Serial.println("output_buf aligned_alloc failed!");
}
avi_init();
}
Serial.println("Setup complete. Starting randomized playback...");
playRandomAVIFiles(avi_folder); // Specify your directory here
}
void loop() {
}
void playRandomAVIFiles(const char* dirPath) {
listAVIFiles(dirPath);
shuffleFiles();
playFiles(dirPath);
}
void listAVIFiles(const char* dirPath) {
File dir = FILESYSTEM.open(dirPath);
if (!dir) {
Serial.println("Failed to open directory");
return;
}
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// No more files
break;
}
if (!entry.isDirectory()) {
String fileName = entry.name();
if ((fileName.endsWith(".AVI") || fileName.endsWith(".avi")) && fileCount < MAX_FILES) {
strcpy(fileNames[fileCount], fileName.c_str());
fileCount++;
}
}
entry.close();
}
dir.close();
Serial.print("Found ");
Serial.print(fileCount);
Serial.println(" AVI files.");
}
void shuffleFiles() {
for (int i = fileCount - 1; i > 0; i--) {
int j = random(i + 1);
// Swap fileNames[i] and fileNames[j]
char temp[13];
strcpy(temp, fileNames[i]);
strcpy(fileNames[i], fileNames[j]);
strcpy(fileNames[j], temp);
}
Serial.println("Files shuffled.");
}
void playFiles(const char* dirPath) {
for (int i = 0; i < fileCount; i++) {
Serial.print("Playing (");
Serial.print(i + 1);
Serial.print("/");
Serial.print(fileCount);
Serial.print("): ");
Serial.println(fileNames[i]);
playAVIFile(dirPath, fileNames[i]);
}
}
void playAVIFile(const char* dirPath, const char* fileName) {
char fullPath[100];
sprintf(fullPath, "%s/%s", dirPath, fileName);
File aviFile = FILESYSTEM.open(fullPath);
if (!aviFile) {
Serial.println("Failed to open file");
return;
}
if (!aviFile.isDirectory())
{
std::string s = aviFile.name();
// if ((!s.starts_with(".")) && (s.ends_with(".avi")))
if ((s.rfind(".", 0) != 0) && ((int)s.find(".avi", 0) > 0))
{
s = root;
s += aviFile.path();
if (avi_open((char *)s.c_str()))
{
Serial.println("AVI start");
gfx->fillScreen(BLACK);
if (avi_aRate > 0)
{
i2s_set_sample_rate(avi_aRate);
}
avi_feed_audio();
if (avi_aFormat == PCM_CODEC_CODE)
{
Serial.println("Start play PCM audio task");
BaseType_t ret_val = pcm_player_task_start();
if (ret_val != pdPASS)
{
Serial.printf("pcm_player_task_start failed: %d\n", ret_val);
}
}
else if (avi_aFormat == MP3_CODEC_CODE)
{
Serial.println("Start play MP3 audio task");
BaseType_t ret_val = mp3_player_task_start();
if (ret_val != pdPASS)
{
Serial.printf("mp3_player_task_start failed: %d\n", ret_val);
}
}
else
{
Serial.println("No audio task");
}
avi_start_ms = millis();
Serial.println("Start play loop");
while (avi_curr_frame < avi_total_frames)
{
avi_feed_audio();
if (avi_decode())
{
avi_draw(0, 0);
}
}
avi_close();
Serial.println("AVI end");
// avi_show_stat();
// delay(5000); // 5 seconds
}
}
aviFile.close();
}
}