diff --git a/README.md b/README.md index e6c312e..ee58690 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,9 @@ The register "DivIrqReg" corresponds to "DivIrqEn", and has the following bit de Bit 1: reserved - nothing to see here Bit 0: reserved - are you feeling lucky, punk? ``` +Interrupt method was not working because it seems reading a card does not trigger one. +See https://arduino.stackexchange.com/a/76285 and https://github.com/miguelbalboa/rfid/blob/master/examples/MinimalInterrupt/MinimalInterrupt.ino ## Buttons diff --git a/src/globals.h b/src/globals.h index 02741b1..c91a8c7 100644 --- a/src/globals.h +++ b/src/globals.h @@ -5,11 +5,31 @@ void stop(); void start(); +void loop2(void* parameter); + boolean buttonPressed(const uint8_t pin); +/** + * Helper routine to dump a byte array as hex values to Serial. + */ +void dump_byte_array(byte *buffer, byte bufferSize) +{ + for (byte i = 0; i < bufferSize; i++) + { + Serial.print(buffer[i] < 0x10 ? " 0" : " "); + Serial.print(buffer[i], HEX); + } +} + +String getRFIDString(byte uidByte[10]) +{ + String uidString = String(uidByte[0]) + " " + String(uidByte[1]) + " " + + String(uidByte[2]) + " " + String(uidByte[3]); + return uidString; +} + unsigned long lastStart = 0; const int startDelay = 250; - #endif \ No newline at end of file diff --git a/src/helper.h b/src/helper.h deleted file mode 100644 index 733776e..0000000 --- a/src/helper.h +++ /dev/null @@ -1,95 +0,0 @@ -#ifndef HELPER_H_ -#define HELPER_H_ - -#include -#include "globals.h" - - -struct DirectoryNode { - String name; - std::vector subdirectories; - std::vector mp3Files; - String* currentPlaying; -}; - - - - - -void buildDirectoryTree(DirectoryNode* currentNode, const char* currentPath) { - File root = SD.open(currentPath); - while (true) { - File entry = root.openNextFile(); - if (!entry) { - break; - } - if (entry.isDirectory()) { - DirectoryNode* newNode = new DirectoryNode; - newNode->name = String(currentPath) + entry.name(); - currentNode->subdirectories.push_back(newNode); - buildDirectoryTree(newNode, (String(currentPath) + entry.name()).c_str()); - } else if (String(entry.name()).endsWith(".mp3")) { - currentNode->mp3Files.push_back(entry.name()); - } - entry.close(); - } - root.close(); -} - -void printDirectoryTree(DirectoryNode* currentNode, int level = 0) { - for (int i = 0; i < level; i++) { - Serial.print(" "); - } - Serial.println(currentNode->name); - - for (const String& mp3File : currentNode->mp3Files) { - for (int i = 0; i <= level; i++) { - Serial.print(" "); - } - Serial.println(mp3File); - } - - for (DirectoryNode* childNode : currentNode->subdirectories) { - printDirectoryTree(childNode, level + 1); - } -} - -void advanceToNextMP3(DirectoryNode* currentNode) { - if (currentNode->currentPlaying != nullptr) { - for (size_t i = 0; i < currentNode->mp3Files.size(); i++) { - if (*currentNode->currentPlaying == currentNode->mp3Files[i]) { - // Found the current playing MP3 file - if (i < currentNode->mp3Files.size() - 1) { - // Advance to the next MP3 file in the same directory - currentNode->currentPlaying = ¤tNode->mp3Files[i + 1]; - return; - } - // Reached the end of the MP3 files in the directory - break; - } - } - } - // If not playing or reached the end, set the first MP3 file as the current playing - if (!currentNode->mp3Files.empty()) { - currentNode->currentPlaying = ¤tNode->mp3Files[0]; - } -} - -DirectoryNode* findFirstMP3Node(DirectoryNode* currentNode) { - if (!currentNode->mp3Files.empty()) { - return currentNode; - } - - for (DirectoryNode* subdirectory : currentNode->subdirectories) { - DirectoryNode* tempNode = findFirstMP3Node(subdirectory); - if (!tempNode->mp3Files.empty()) { - return tempNode; - } - } - - return NULL; -} - - - -#endif /* HELPER_H_ */ \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b4aca4f..6f9bd75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,15 +9,12 @@ #include "Audio.h" - - - #include #include -#include //RFID Reader +#include //RFID Reader // define pins for RFID -#define CS_RFID 32// SIC, tried 4 and 32 but only this worked! +#define CS_RFID 32 // SIC, tried 4 and 32 but only this worked! #define RST_RFID 33 #define IRQ_RFID 34 @@ -30,21 +27,21 @@ #define CS_SDCARD 22 +#define RFID_LOOP_INTERVAL 10 #include "globals.h" #include "WebContent.h" #include "DirectoryNode.h" - File root; File mp3File; - - Audio audio; uint volume = 12; +uint rfid_loop = RFID_LOOP_INTERVAL; + AsyncWebServer server(80); DNSServer dns; @@ -55,23 +52,27 @@ boolean blockState = false; volatile bool newRfidInt = false; +MFRC522 rfid(CS_RFID, RST_RFID); // instatiate a MFRC522 reader object. -MFRC522 rfid(CS_RFID, RST_RFID); // instatiate a MFRC522 reader object. +TaskHandle_t RfidTask; - -void activateSD() { +void activateSD() +{ digitalWrite(CS_SDCARD, LOW); } -void deactivateSD() { +void deactivateSD() +{ digitalWrite(CS_SDCARD, HIGH); } -void activateRFID() { +void activateRFID() +{ digitalWrite(CS_RFID, LOW); } -void deactivateRFID() { +void deactivateRFID() +{ digitalWrite(CS_RFID, HIGH); } @@ -107,15 +108,18 @@ void audio_info(const char *info) // Serial.print("info "); Serial.println(info); } -void mute() { - if (audio.getVolume()!=0) { +void mute() +{ + if (audio.getVolume() != 0) + { volume = audio.getVolume(); } - + audio.setVolume(0); } -void unmute() { +void unmute() +{ audio.setVolume(volume); } @@ -123,7 +127,8 @@ String getState() { String state = String(); - if (blockState) { + if (blockState) + { return state; } blockState = true; @@ -135,7 +140,6 @@ String getState() state += " / "; state += audio.getAudioFileDuration(); state += " "; - } else { @@ -174,40 +178,40 @@ void stop() void start() { - if (currentNode!=NULL) { + if (currentNode != NULL) + { currentNode->setCurrentPlaying(NULL); currentNode = NULL; } - + playNextMp3(); } void next() -{ +{ playNextMp3(); } -void audio_eof_mp3(const char *info) { +void audio_eof_mp3(const char *info) +{ Serial.println("audio file ended."); playNextMp3(); } -void readRFID() { +/* not working, FIXME remove me! */ +void IRAM_ATTR rfid_interrupt() +{ + newRfidInt = true; +} + +void readRFID() +{ rfid.PICC_ReadCardSerial(); Serial.print("Tag UID: "); - String uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + - String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]); + String uidString = getRFIDString(rfid.uid.uidByte); Serial.println(uidString); } -void clearInt(MFRC522 mfrc522) { - mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x60); -} - -void IRAM_ATTR rfid_interrupt() { - newRfidInt = true; -} - void setup() { // put your setup code here, to run once: @@ -215,24 +219,20 @@ void setup() pinMode(BTN_START_STOP, INPUT_PULLUP); -/* setup the IRQ pin*/ + /* setup the IRQ pin*/ pinMode(IRQ_RFID, INPUT_PULLUP); - pinMode(CS_RFID,OUTPUT); - pinMode(CS_SDCARD,OUTPUT); - + pinMode(CS_RFID, OUTPUT); + pinMode(CS_SDCARD, OUTPUT); deactivateRFID(); - - + activateSD(); // first parameter is name of access point, second is the password AsyncWiFiManager wifiManager(&server, &dns); wifiManager.autoConnect("HannaBox"); - - Serial.print("Initializing SD card..."); if (!SD.begin(CS_SDCARD)) @@ -246,42 +246,29 @@ void setup() rootNode.buildDirectoryTree("/"); rootNode.printDirectoryTree(); - deactivateSD(); activateRFID(); Serial.println("RFID"); - // Init MFRC522 - // Init SPI bus - SPI.begin(-1,-1,-1,CS_RFID); + // Init MFRC522 + // Init SPI bus + SPI.begin(-1, -1, -1, CS_RFID); rfid.PCD_Init(CS_RFID, RST_RFID); - if (rfid.PCD_PerformSelfTest()) { + + if (rfid.PCD_PerformSelfTest()) + { Serial.println("RFID OK"); - - /* - * Allow the ... irq to be propagated to the IRQ pin - * For test purposes propagate the IdleIrq and loAlert - */ - rfid.PCD_WriteRegister(rfid.ComIEnReg, 0x60); - attachInterrupt(IRQ_RFID, rfid_interrupt, RISING); - //clear a spourious interrupt at start - /* - do { - ; - } while (!newRfidInt); - newRfidInt = false; //interrupt flag - */ - } else { + } + else + { Serial.println("RFID Self Test failed!"); } audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); audio.setVolume(volume); // 0...21 - Serial.println("Audio initialized."); - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send_P(200, "text/html", index_html, processor); }); @@ -309,59 +296,75 @@ void setup() next(); }); server.begin(); + + xTaskCreatePinnedToCore( + loop2, /* Function to implement the task */ + "RFIDTask", /* Name of the task */ + 10000, /* Stack size in words */ + NULL, /* Task input parameter */ + 0, /* Priority of the task */ + &RfidTask, /* Task handle. */ + 0); /* Core where the task should run */ + Serial.println("initialization done."); } - - void loop() { - deactivateRFID(); - activateSD(); - audio.loop(); - deactivateSD(); - activateRFID(); - /* - if(rfid.PICC_IsNewCardPresent()) { - stop(); - readRFID(); - }*/ - - if (newRfidInt) { - - Serial.println("Interrupt."); - if(rfid.PICC_IsNewCardPresent()) { - stop(); - readRFID(); - } - //clearInt(rfid); - newRfidInt = false; + if (audio.isRunning()) + { + deactivateRFID(); + activateSD(); + audio.loop(); + deactivateSD(); + activateRFID(); } - - - if (buttonPressed(BTN_START_STOP)) { - if (audio.isRunning()) { + rfid_loop--; + if (rfid_loop == 0) + { + rfid_loop = RFID_LOOP_INTERVAL; + deactivateSD(); + activateRFID(); + if (rfid.PICC_IsNewCardPresent()) + { stop(); - } else { - start(); + readRFID(); } } - + if (buttonPressed(BTN_START_STOP)) + { + if (audio.isRunning()) + { + stop(); + } + else + { + start(); + } + } + delay(5); } -boolean buttonPressed(const uint8_t pin) { +void loop2(void *parameter) +{ + for (;;) + { + } +} + +boolean buttonPressed(const uint8_t pin) +{ if (digitalRead(pin) == LOW) { unsigned long now = millis(); if (now - lastStart > startDelay) { - + lastStart = now; return true; } } return false; } -