64 lines
4.1 KiB
Arduino
64 lines
4.1 KiB
Arduino
/*
|
|
This is free and unencumbered software released into the public domain.
|
|
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
|
|
|
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
|
|
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
For more information, please refer to <http://unlicense.org/>
|
|
*/
|
|
|
|
#include "WiFi.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFi/src
|
|
#include "Audio.h" // https://github.com/schreibfaul1/ESP32-audioI2S
|
|
#include "SPI.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/SPI/src
|
|
#include "SD.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/SD/src
|
|
#include "FS.h" // https://github.com/espressif/arduino-esp32/tree/master/libraries/FS/src
|
|
|
|
|
|
// define all constants
|
|
const int SD_CS = 5; // BOARD SPECIFIC
|
|
const int SPI_MISO = 19; // BOARD SPECIFIC
|
|
const int SPI_MOSI = 23; // BOARD SPECIFIC
|
|
const int SPI_SCK = 18; // BOARD SPECIFIC
|
|
const int I2S_DOUT = 25; // can be changed on need
|
|
const int I2S_BLCK = 27; // can be changed on need
|
|
const int I2S_LRC = 26; // can be changed on need
|
|
const int sdCardEjectPin = 13; // pin which is used to tell the program to eject the sd card (so that the file system doesn't brake)
|
|
const int waitOnSDCardEject = 5000; // defines how long to wait (in ms) after the button for SD card eject was pressed (on pin 'sdCardEjectPin'!)
|
|
const int retrySDMountTreshold = 1000; // defines how long to wait (in ms) to the next try of mounting the sd card
|
|
const int readyPin = 32; // for an LED that shows if everything started up correctly (SD card mounted, wifi connected, ...)
|
|
|
|
// create all needed variables
|
|
int currentVolume; // variable where current volume (0...21) is stored
|
|
Audio audio; // Audio object (for playing audio, decoding mp3, ...)
|
|
|
|
void setup() {
|
|
Serial.begin(115200); // setup Serial console (over USB) with baud 115200
|
|
|
|
// setup all pins (not SPI and other buses!)
|
|
pinMode(sdCardEjectPin, INPUT);
|
|
pinMode(readyPin, OUTPUT);
|
|
|
|
// connect to sd card reader
|
|
Serial.println("[DEBUG] Connecting to SD-Card reader");
|
|
while(!setupSD(SD_CS, SPI_MISO, SPI_MOSI, SPI_SCK)) { Serial.printf("[DEBUG] Can't connect to SD card reader! Waiting for %d milliseconds...\n", retrySDMountTreshold); delay(retrySDMountTreshold); }
|
|
Serial.println("[DEBUG] Connected to SD-Card reader");
|
|
|
|
digitalWrite(readyPin, HIGH); // show that startup is done and everything works fine
|
|
}
|
|
|
|
void loop() {
|
|
// for SD card eject
|
|
if(analogRead(sdCardEjectPin) > 4000) { // bigger than 4000: so that it's not affected by a little touch of fingers (4095 is maximum; 0 minimum)
|
|
SD.end(); // delete SD object and sync its cache to flash
|
|
digitalWrite(readyPin, LOW); // indicate that SD card can get removed (the ready-LED will go off)
|
|
Serial.printf("[DEBUG] Unmounting SD-Card, waiting for %dms and trying to remount (every %d milliseconds)...\n", waitOnSDCardEject, retrySDMountTreshold);
|
|
delay(waitOnSDCardEject); // wait some time (so that the sd card can be removed!)
|
|
while(!setupSD(SD_CS, SPI_MISO, SPI_MOSI, SPI_SCK)) delay(retrySDMountTreshold); // remount sd card
|
|
digitalWrite(readyPin, HIGH); // indicate that everything is ready again
|
|
}
|
|
}
|