Skip to content

Commit

Permalink
Removing RC522 from task + bugfix for audio.isRunning()-recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
biologist79 committed Apr 1, 2021
1 parent ad1b5d4 commit 59b4efd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 67 deletions.
147 changes: 81 additions & 66 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ AsyncEventSource events("/events");
#endif

TaskHandle_t mp3Play;
TaskHandle_t rfid;
#ifdef RFID_READER_TYPE_PN5180
TaskHandle_t rfid;
#endif
TaskHandle_t fileStorageTaskHandle;

#ifdef NEOPIXEL_ENABLE
Expand Down Expand Up @@ -420,7 +422,11 @@ bool processJsonRequest(char *_serialJson);
void randomizePlaylist (char *str[], const uint32_t count);
char ** returnPlaylistFromWebstream(const char *_webUrl);
char ** returnPlaylistFromSD(File _fileOrDirectory);
void rfidScanner(void *parameter);
#ifdef RFID_READER_TYPE_PN5180
void rfidScanner(void *parameter);
#else
void rfidScanner(void);
#endif
void sleepHandler(void) ;
void sortPlaylist(const char** arr, int n);
bool startsWith(const char *str, const char *pre);
Expand Down Expand Up @@ -1433,6 +1439,7 @@ size_t nvsRfidWriteWrapper (const char *_rfidCardId, const char *_track, const u
// Function to play music as task
void playAudio(void *parameter) {
static Audio audio;
uint8_t settleCount = 0;
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(initVolume);
audio.forceMono(playProperties.currentPlayMono);
Expand Down Expand Up @@ -1848,91 +1855,93 @@ void playAudio(void *parameter) {
}
#endif

// If error occured: remove playlist from ESPuino
if ((playProperties.playMode != NO_PLAYLIST) && !audio.isRunning() && !playProperties.pausePlay) {
playProperties.trackFinished = true;
}

audio.loop();
if (playProperties.playlistFinished || playProperties.pausePlay) {
vTaskDelay(portTICK_PERIOD_MS*10); // Waste some time if playlist is not active
} else {
lastTimeActiveTimestamp = millis(); // Refresh if playlist is active so uC will not fall asleep due to reaching inactivity-time
}

if (audio.isRunning()) {
settleCount = 0;
}

// If error occured: remove playlist from ESPuino
if (playProperties.playMode != NO_PLAYLIST && playProperties.playMode != BUSY && !audio.isRunning() && !playProperties.pausePlay) {
if (settleCount++ == 50) { // Hack to give audio some time to settle down after playlist was generated
playProperties.playlistFinished = true;
playProperties.playMode = NO_PLAYLIST;
settleCount = 0;
}
}

esp_task_wdt_reset(); // Don't forget to feed the dog!
}
vTaskDelete(NULL);
}


#if defined RFID_READER_TYPE_MFRC522_SPI || defined RFID_READER_TYPE_MFRC522_I2C
// Instructs RFID-scanner to scan for new RFID-tags
void rfidScanner(void *parameter) {
// Instructs RFID-scanner to scan for new RFID-tags using RC522 (running as function)
void rfidScanner(void) {
byte cardId[cardIdSize];
char *cardIdString;

for (;;) {
esp_task_wdt_reset();
vTaskDelay(10);
if ((millis() - lastRfidCheckTimestamp) >= RFID_SCAN_INTERVAL) {
lastRfidCheckTimestamp = millis();
// Reset the loop if no new card is present on the sensor/reader. This saves the entire process when idle.
if ((millis() - lastRfidCheckTimestamp) >= RFID_SCAN_INTERVAL) {
lastRfidCheckTimestamp = millis();
// Reset the loop if no new card is present on the sensor/reader. This saves the entire process when idle.

if (!mfrc522.PICC_IsNewCardPresent()) {
continue;
}
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}

// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
continue;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}

//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();

if (psramInit()) {
cardIdString = (char *) ps_malloc(cardIdSize*3 +1);
} else {
cardIdString = (char *) malloc(cardIdSize*3 +1);
}
if (psramInit()) {
cardIdString = (char *) ps_malloc(cardIdSize*3 +1);
} else {
cardIdString = (char *) malloc(cardIdSize*3 +1);
}

if (cardIdString == NULL) {
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE
showLedError = true;
#endif
continue;
}
if (cardIdString == NULL) {
logger(serialDebug, (char *) FPSTR(unableToAllocateMem), LOGLEVEL_ERROR);
#ifdef NEOPIXEL_ENABLE
showLedError = true;
#endif
return;
}

uint8_t n = 0;
logger(serialDebug, (char *) FPSTR(rfidTagDetected), LOGLEVEL_NOTICE);
for (uint8_t i=0; i<cardIdSize; i++) {
cardId[i] = mfrc522.uid.uidByte[i];
uint8_t n = 0;
logger(serialDebug, (char *) FPSTR(rfidTagDetected), LOGLEVEL_NOTICE);
for (uint8_t i=0; i<cardIdSize; i++) {
cardId[i] = mfrc522.uid.uidByte[i];

snprintf(logBuf, serialLoglength, "%02x", cardId[i]);
logger(serialDebug, logBuf, LOGLEVEL_NOTICE);
snprintf(logBuf, serialLoglength, "%02x", cardId[i]);
logger(serialDebug, logBuf, LOGLEVEL_NOTICE);

n += snprintf (&cardIdString[n], sizeof(cardIdString) / sizeof(cardIdString[0]), "%03d", cardId[i]);
if (i<(cardIdSize-1)) {
logger(serialDebug, "-", LOGLEVEL_NOTICE);
} else {
logger(serialDebug, "\n", LOGLEVEL_NOTICE);
}
n += snprintf (&cardIdString[n], sizeof(cardIdString) / sizeof(cardIdString[0]), "%03d", cardId[i]);
if (i<(cardIdSize-1)) {
logger(serialDebug, "-", LOGLEVEL_NOTICE);
} else {
logger(serialDebug, "\n", LOGLEVEL_NOTICE);
}
xQueueSend(rfidCardQueue, &cardIdString, 0);
// free(cardIdString);
}
xQueueSend(rfidCardQueue, &cardIdString, 0);
}
vTaskDelete(NULL);
}
#endif



#ifdef RFID_READER_TYPE_PN5180
// Instructs RFID-scanner to scan for new RFID-tags using PN5180
// Instructs RFID-scanner to scan for new RFID-tags using PN5180 (running as task)
// Runs as task (instead of function) because it takes about 230 ms (IDLE) and 500 ms (non-IDLE)
void rfidScanner(void *parameter) {
static PN5180ISO14443 nfc14443(RFID_CS, RFID_BUSY, RFID_RST);
static PN5180ISO15693 nfc15693(RFID_CS, RFID_BUSY, RFID_RST);
Expand Down Expand Up @@ -4798,6 +4807,7 @@ void setup() {
Serial.println(F(" | |___ ___) | | __/ | |_| | | | | | | | | (_) |"));
Serial.println(F(" |_____| |____/ |_| \\__,_| |_| |_| |_| \\___/ "));
Serial.println(F(" Rfid-controlled musicplayer\n"));
Serial.println(F(" Rev 20210402-1\n"));
// print wake-up reason
printWakeUpReason();
#ifdef PN5180_ENABLE_LPCD
Expand Down Expand Up @@ -4856,8 +4866,8 @@ void setup() {
// Get initial LED-brightness from NVS
uint8_t nvsILedBrightness = prefsSettings.getUChar("iLedBrightness", 0);
if (nvsILedBrightness) {
initialLedBrightness = nvsILedBrightness;
ledBrightness = nvsILedBrightness;
initialLedBrightness = nvsILedBrightness;
ledBrightness = nvsILedBrightness;
snprintf(logBuf, serialLoglength, "%s: %d", (char *) FPSTR(initialBrightnessfromNvs), nvsILedBrightness);
loggerNl(serialDebug, logBuf, LOGLEVEL_INFO);
} else {
Expand Down Expand Up @@ -5068,16 +5078,18 @@ void setup() {
timerAlarmWrite(timer, 10000, true); // 100 Hz
timerAlarmEnable(timer);

// Create tasks
xTaskCreatePinnedToCore(
rfidScanner, /* Function to implement the task */
"rfidhandling", /* Name of the task */
2000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
&rfid, /* Task handle. */
0 /* Core where the task should run */
);
#ifdef RFID_READER_TYPE_PN5180
// Create task for rfid
xTaskCreatePinnedToCore(
rfidScanner, /* Function to implement the task */
"rfidhandling", /* Name of the task */
1500, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
&rfid, /* Task handle. */
0 /* Core where the task should run */
);
#endif

// Activate internal pullups for all enabled buttons
#ifdef BUTTON_0_ENABLE
Expand Down Expand Up @@ -5171,6 +5183,9 @@ void bluetoothHandler(void) {
#endif

void loop() {
#ifndef RFID_READER_TYPE_PN5180
rfidScanner(); // PN5180 runs as task; RC522 as function
#endif

#ifdef BLUETOOTH_ENABLE
if(operationMode == OPMODE_BLUETOOTH) {
Expand Down
2 changes: 1 addition & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
uint16_t intervalToLongPress = 700; // Interval in ms to distinguish between short and long press of previous/next-button

// RFID
#define RFID_SCAN_INTERVAL 300 // Interval-time in ms (how often is RFID read?)
#define RFID_SCAN_INTERVAL 100 // Interval-time in ms (how often is RFID read?)

// Automatic restart
#ifdef SHUTDOWN_IF_SD_BOOT_FAILS
Expand Down

0 comments on commit 59b4efd

Please sign in to comment.