rename esp32 project folder

This commit is contained in:
2021-10-01 09:52:08 +02:00
parent 87d5ab8e78
commit 2c0e996d6b
26 changed files with 0 additions and 27 deletions

View File

@@ -0,0 +1,94 @@
#include "audio.h"
AudioGeneratorMP3 *mp3;
AudioFileSourceID3 *id3;
AudioFileSourceLittleFS *file;
AudioOutputI2S *out;
uint8_t i = 0;
uint8_t n = 0;
const char *waveFile[] =
{"/ringoffire.mp3",
"/Let_it_be.mp3",
"/Billy-Jean.mp3"};
// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
(void)cbData;
Serial.printf("ID3 callback for: %s = '", type);
if (isUnicode)
{
string += 2;
}
while (*string)
{
char a = *(string++);
if (isUnicode)
{
string++;
}
Serial.printf("%c", a);
}
Serial.printf("'\n");
Serial.flush();
}
// Called when there's a warning or error (like a buffer underflow or decode hiccup)
void StatusCallback(void *cbData, int code, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
// Note that the string may be in PROGMEM, so copy it to RAM for printf
char s1[64];
strncpy_P(s1, string, sizeof(s1));
s1[sizeof(s1) - 1] = 0;
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
Serial.flush();
}
void playSong(uint8_t index)
{
if (index > AUDIONSONGS)
return;
Serial.printf("now playing %s\n", waveFile[index]);
file = new AudioFileSourceLittleFS(waveFile[index]);
id3 = new AudioFileSourceID3(file);
id3->RegisterMetadataCB(MDCallback, (void *)"ID3TAG");
mp3->begin(id3, out);
}
void initAudio()
{
Serial.println("init Audio");
audioLogger = &Serial;
out = new AudioOutputI2S();
out->SetPinout(I2S_BCLK, I2S_WCLK, I2S_DATA); //bclk, wclk, data
out->SetGain(AUDIOGAIN);
mp3 = new AudioGeneratorMP3();
mp3->RegisterStatusCB(StatusCallback, (void *)"mp3");
Serial.println("init Audio Done");
playSong(i);
}
void handleAudio()
{
if (mp3->isRunning())
{
if (!mp3->loop())
{
//mp3->stop();
if(n++ >= AUDIOREPEATS)
{
i++;
n= 0;
}
playSong(i);
;
}
}
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include "board.h"
#include "AudioFileSourcePROGMEM.h"
#include <AudioFileSourceLittleFS.h>
#include "AudioGeneratorMP3.h"
#include "AudioFileSourceID3.h"
#include "AudioOutputI2S.h"
#define AUDIOGAIN 1
#define AUDIONSONGS 3
#define AUDIOREPEATS 3
void initAudio(void);
void handleAudio(void);

View File

@@ -0,0 +1,6 @@
#pragma once
#define I2S_BCLK 19
#define I2S_WCLK 33
#define I2S_DATA 22

View File

@@ -0,0 +1,20 @@
#include <Arduino.h>
#include "storage.h"
#include "audio.h"
void setup()
{
Serial.begin(115200);
delay(1000);
initStorage();
initAudio();
}
void loop()
{
handleAudio();
}

View File

@@ -0,0 +1,154 @@
#include "rfid.h"
// void initRfid(void)
// {
// }
// void handleRfid(void)
// {
// }
/*
* Initial Author: ryand1011 (https://github.com/ryand1011)
*
* Reads data written by a program such as "rfid_write_personal_data.ino"
*
* See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data
*
* Uses MIFARE RFID card using RFID-RC522 reader
* Uses MFRC522 - Library
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//*****************************************************************************************//
void initRfid() {
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
}
//*****************************************************************************************//
void handleRfid() {
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
//-------------------------------------------
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("**Card Detected:**"));
//-------------------------------------------
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
Serial.print(F("Name: "));
byte buffer1[18];
block = 4;
len = 18;
//------------------------------------------- GET FIRST NAME
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer1, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
}
}
Serial.print(" ");
//---------------------------------------- GET LAST NAME
byte buffer2[18];
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] );
}
//----------------------------------------
Serial.println(F("\n**End Reading**\n"));
delay(1000); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//

View File

@@ -0,0 +1,6 @@
#pragma once
#include <MFRC522.h>
void initRfid(void);
void handleRfid(void);

View File

@@ -0,0 +1,337 @@
#include "storage.h"
#include <Arduino.h>
#include "FS.h"
#include <LITTLEFS.h>
#ifndef CONFIG_LITTLEFS_FOR_IDF_3_2
#include <time.h>
#endif
/* You only need to format LITTLEFS the first time you run a
test or else use the LITTLEFS plugin to create a partition
https://github.com/lorol/arduino-esp32littlefs-plugin */
#define FORMAT_LITTLEFS_IF_FAILED false
void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
Serial.printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if (!root)
{
Serial.println("- failed to open directory");
return;
}
if (!root.isDirectory())
{
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while (file)
{
if (file.isDirectory())
{
Serial.print(" DIR : ");
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
Serial.println(file.name());
#else
Serial.print(file.name());
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
#endif
if (levels)
{
listDir(fs, file.name(), levels - 1);
}
}
else
{
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
#ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
Serial.println(file.size());
#else
Serial.print(file.size());
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
#endif
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char *path)
{
Serial.printf("Creating Dir: %s\n", path);
if (fs.mkdir(path))
{
Serial.println("Dir created");
}
else
{
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char *path)
{
Serial.printf("Removing Dir: %s\n", path);
if (fs.rmdir(path))
{
Serial.println("Dir removed");
}
else
{
Serial.println("rmdir failed");
}
}
void readFile(fs::FS &fs, const char *path)
{
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory())
{
Serial.println("- failed to open file for reading");
return;
}
Serial.println("- read from file:");
while (file.available())
{
Serial.write(file.read());
}
file.close();
}
void writeFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
Serial.println("- failed to open file for writing");
return;
}
if (file.print(message))
{
Serial.println("- file written");
}
else
{
Serial.println("- write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char *path, const char *message)
{
Serial.printf("Appending to file: %s\r\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file)
{
Serial.println("- failed to open file for appending");
return;
}
if (file.print(message))
{
Serial.println("- message appended");
}
else
{
Serial.println("- append failed");
}
file.close();
}
void renameFile(fs::FS &fs, const char *path1, const char *path2)
{
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
if (fs.rename(path1, path2))
{
Serial.println("- file renamed");
}
else
{
Serial.println("- rename failed");
}
}
void deleteFile(fs::FS &fs, const char *path)
{
Serial.printf("Deleting file: %s\r\n", path);
if (fs.remove(path))
{
Serial.println("- file deleted");
}
else
{
Serial.println("- delete failed");
}
}
// SPIFFS-like write and delete file, better use #define CONFIG_LITTLEFS_SPIFFS_COMPAT 1
void writeFile2(fs::FS &fs, const char *path, const char *message)
{
if (!fs.exists(path))
{
if (strchr(path, '/'))
{
Serial.printf("Create missing folders of: %s\r\n", path);
char *pathStr = strdup(path);
if (pathStr)
{
char *ptr = strchr(pathStr, '/');
while (ptr)
{
*ptr = 0;
fs.mkdir(pathStr);
*ptr = '/';
ptr = strchr(ptr + 1, '/');
}
}
free(pathStr);
}
}
Serial.printf("Writing file to: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
Serial.println("- failed to open file for writing");
return;
}
if (file.print(message))
{
Serial.println("- file written");
}
else
{
Serial.println("- write failed");
}
file.close();
}
void deleteFile2(fs::FS &fs, const char *path)
{
Serial.printf("Deleting file and empty folders on path: %s\r\n", path);
if (fs.remove(path))
{
Serial.println("- file deleted");
}
else
{
Serial.println("- delete failed");
}
char *pathStr = strdup(path);
if (pathStr)
{
char *ptr = strrchr(pathStr, '/');
if (ptr)
{
Serial.printf("Removing all empty folders on path: %s\r\n", path);
}
while (ptr)
{
*ptr = 0;
fs.rmdir(pathStr);
ptr = strrchr(pathStr, '/');
}
free(pathStr);
}
}
void testFileIO(fs::FS &fs, const char *path)
{
Serial.printf("Testing file I/O with %s\r\n", path);
static uint8_t buf[512];
size_t len = 0;
File file = fs.open(path, FILE_WRITE);
if (!file)
{
Serial.println("- failed to open file for writing");
return;
}
size_t i;
Serial.print("- writing");
uint32_t start = millis();
for (i = 0; i < 2048; i++)
{
if ((i & 0x001F) == 0x001F)
{
Serial.print(".");
}
file.write(buf, 512);
}
Serial.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
file.close();
file = fs.open(path);
start = millis();
end = start;
i = 0;
if (file && !file.isDirectory())
{
len = file.size();
size_t flen = len;
start = millis();
Serial.print("- reading");
while (len)
{
size_t toRead = len;
if (toRead > 512)
{
toRead = 512;
}
file.read(buf, toRead);
if ((i++ & 0x001F) == 0x001F)
{
Serial.print(".");
}
len -= toRead;
}
Serial.println("");
end = millis() - start;
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
file.close();
}
else
{
Serial.println("- failed to open file for reading");
}
}
void initStorage()
{
if (!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED))
{
Serial.println("LITTLEFS Mount Failed");
return;
}
listDir(LITTLEFS, "/", 0);
}
void handleStorage()
{
}

View File

@@ -0,0 +1,5 @@
#pragma once
void initStorage();