Implement display, SD card, I2S WAV audio player, and button sync logic
This commit is contained in:
100
sound_picture_app/sound_picture_app.ino
Normal file
100
sound_picture_app/sound_picture_app.ino
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "Display_ST7789.h"
|
||||
#include "SD_Card.h"
|
||||
#include "Display_PNG.h"
|
||||
#include "Audio_Player.h"
|
||||
#include <algorithm>
|
||||
|
||||
#define BOOT_KEY_PIN 9
|
||||
|
||||
uint16_t currentIndex = 0;
|
||||
bool lastButtonState = HIGH;
|
||||
uint32_t lastDebounceTime = 0;
|
||||
const uint32_t debounceDelay = 50;
|
||||
bool buttonWasPressed = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
printf("\r\n==========================================\r\n");
|
||||
printf("Starting Sound and Picture Player...\r\n");
|
||||
printf("==========================================\r\n");
|
||||
|
||||
// Initialize peripherals
|
||||
Flash_test();
|
||||
SD_Init();
|
||||
LCD_Init();
|
||||
Set_Backlight(90);
|
||||
|
||||
// Setup button
|
||||
pinMode(BOOT_KEY_PIN, INPUT_PULLUP);
|
||||
|
||||
// Retrieve media files from SD Card
|
||||
Search_Images("/");
|
||||
Search_Wavs("/");
|
||||
|
||||
printf("Initialization complete!\r\n");
|
||||
printf("Found %d PNG images and %d WAV soundbites.\r\n", Image_Count, Wav_Count);
|
||||
|
||||
if (Image_Count > 0) {
|
||||
printf("Displaying initial image (index 0): %s\r\n", Image_Names[0]);
|
||||
Display_Image_By_Index("/", 0);
|
||||
} else {
|
||||
printf("WARNING: No PNG images found in SD Card root directory!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void playCurrentPair() {
|
||||
if (Image_Count == 0 && Wav_Count == 0) {
|
||||
printf("Cannot play: missing PNG images and WAV soundbites!\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Stop current playing audio if any
|
||||
stopWav();
|
||||
|
||||
printf("Playing item index %d:\r\n", currentIndex);
|
||||
|
||||
// Display the image
|
||||
if (Image_Count > 0) {
|
||||
uint16_t pngIndex = currentIndex % Image_Count;
|
||||
Display_Image_By_Index("/", pngIndex);
|
||||
}
|
||||
|
||||
// Play the soundbite
|
||||
if (Wav_Count > 0) {
|
||||
uint16_t wavIndex = currentIndex % Wav_Count;
|
||||
String soundPath = String("/") + Wav_Names[wavIndex];
|
||||
printf("Playing sound: %s\r\n", soundPath.c_str());
|
||||
playWav(soundPath.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
bool currentButtonReading = digitalRead(BOOT_KEY_PIN);
|
||||
|
||||
if (currentButtonReading != lastButtonState) {
|
||||
lastDebounceTime = millis();
|
||||
}
|
||||
|
||||
if ((millis() - lastDebounceTime) > debounceDelay) {
|
||||
if (currentButtonReading == LOW) {
|
||||
if (!buttonWasPressed) {
|
||||
buttonWasPressed = true;
|
||||
printf("Button pressed! Cycling to next picture and playing sound...\r\n");
|
||||
|
||||
if (Image_Count > 0 || Wav_Count > 0) {
|
||||
playCurrentPair();
|
||||
currentIndex = (currentIndex + 1) % std::max(Image_Count, Wav_Count);
|
||||
} else {
|
||||
printf("No media found on SD card to cycle!\r\n");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
buttonWasPressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
lastButtonState = currentButtonReading;
|
||||
delay(10); // Yield to other RTOS tasks
|
||||
}
|
||||
Reference in New Issue
Block a user