RFID
This commit is contained in:
parent
cb3b817295
commit
0f47f29c74
|
|
@ -80,7 +80,9 @@ The register "DivIrqReg" corresponds to "DivIrqEn", and has the following bit de
|
||||||
Bit 1: reserved - nothing to see here
|
Bit 1: reserved - nothing to see here
|
||||||
Bit 0: reserved - are you feeling lucky, punk?
|
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
|
## Buttons
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,31 @@ void stop();
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
|
void loop2(void* parameter);
|
||||||
|
|
||||||
boolean buttonPressed(const uint8_t pin);
|
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;
|
unsigned long lastStart = 0;
|
||||||
|
|
||||||
const int startDelay = 250;
|
const int startDelay = 250;
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
95
src/helper.h
95
src/helper.h
|
|
@ -1,95 +0,0 @@
|
||||||
#ifndef HELPER_H_
|
|
||||||
#define HELPER_H_
|
|
||||||
|
|
||||||
#include <SD.h>
|
|
||||||
#include "globals.h"
|
|
||||||
|
|
||||||
|
|
||||||
struct DirectoryNode {
|
|
||||||
String name;
|
|
||||||
std::vector<DirectoryNode*> subdirectories;
|
|
||||||
std::vector<String> 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_ */
|
|
||||||
153
src/main.cpp
153
src/main.cpp
|
|
@ -9,9 +9,6 @@
|
||||||
|
|
||||||
#include "Audio.h"
|
#include "Audio.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
#include <MFRC522.h> //RFID Reader
|
#include <MFRC522.h> //RFID Reader
|
||||||
|
|
@ -30,21 +27,21 @@
|
||||||
|
|
||||||
#define CS_SDCARD 22
|
#define CS_SDCARD 22
|
||||||
|
|
||||||
|
#define RFID_LOOP_INTERVAL 10
|
||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "WebContent.h"
|
#include "WebContent.h"
|
||||||
#include "DirectoryNode.h"
|
#include "DirectoryNode.h"
|
||||||
|
|
||||||
|
|
||||||
File root;
|
File root;
|
||||||
File mp3File;
|
File mp3File;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Audio audio;
|
Audio audio;
|
||||||
|
|
||||||
uint volume = 12;
|
uint volume = 12;
|
||||||
|
|
||||||
|
uint rfid_loop = RFID_LOOP_INTERVAL;
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
DNSServer dns;
|
DNSServer dns;
|
||||||
|
|
||||||
|
|
@ -55,23 +52,27 @@ boolean blockState = false;
|
||||||
|
|
||||||
volatile bool newRfidInt = 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);
|
digitalWrite(CS_SDCARD, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deactivateSD() {
|
void deactivateSD()
|
||||||
|
{
|
||||||
digitalWrite(CS_SDCARD, HIGH);
|
digitalWrite(CS_SDCARD, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void activateRFID() {
|
void activateRFID()
|
||||||
|
{
|
||||||
digitalWrite(CS_RFID, LOW);
|
digitalWrite(CS_RFID, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deactivateRFID() {
|
void deactivateRFID()
|
||||||
|
{
|
||||||
digitalWrite(CS_RFID, HIGH);
|
digitalWrite(CS_RFID, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,15 +108,18 @@ void audio_info(const char *info)
|
||||||
// Serial.print("info "); Serial.println(info);
|
// Serial.print("info "); Serial.println(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mute() {
|
void mute()
|
||||||
if (audio.getVolume()!=0) {
|
{
|
||||||
|
if (audio.getVolume() != 0)
|
||||||
|
{
|
||||||
volume = audio.getVolume();
|
volume = audio.getVolume();
|
||||||
}
|
}
|
||||||
|
|
||||||
audio.setVolume(0);
|
audio.setVolume(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unmute() {
|
void unmute()
|
||||||
|
{
|
||||||
audio.setVolume(volume);
|
audio.setVolume(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,7 +127,8 @@ String getState()
|
||||||
{
|
{
|
||||||
String state = String();
|
String state = String();
|
||||||
|
|
||||||
if (blockState) {
|
if (blockState)
|
||||||
|
{
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
blockState = true;
|
blockState = true;
|
||||||
|
|
@ -135,7 +140,6 @@ String getState()
|
||||||
state += " / ";
|
state += " / ";
|
||||||
state += audio.getAudioFileDuration();
|
state += audio.getAudioFileDuration();
|
||||||
state += " ";
|
state += " ";
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -174,7 +178,8 @@ void stop()
|
||||||
|
|
||||||
void start()
|
void start()
|
||||||
{
|
{
|
||||||
if (currentNode!=NULL) {
|
if (currentNode != NULL)
|
||||||
|
{
|
||||||
currentNode->setCurrentPlaying(NULL);
|
currentNode->setCurrentPlaying(NULL);
|
||||||
currentNode = NULL;
|
currentNode = NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -187,27 +192,26 @@ void next()
|
||||||
playNextMp3();
|
playNextMp3();
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_eof_mp3(const char *info) {
|
void audio_eof_mp3(const char *info)
|
||||||
|
{
|
||||||
Serial.println("audio file ended.");
|
Serial.println("audio file ended.");
|
||||||
playNextMp3();
|
playNextMp3();
|
||||||
}
|
}
|
||||||
|
|
||||||
void readRFID() {
|
/* not working, FIXME remove me! */
|
||||||
|
void IRAM_ATTR rfid_interrupt()
|
||||||
|
{
|
||||||
|
newRfidInt = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void readRFID()
|
||||||
|
{
|
||||||
rfid.PICC_ReadCardSerial();
|
rfid.PICC_ReadCardSerial();
|
||||||
Serial.print("Tag UID: ");
|
Serial.print("Tag UID: ");
|
||||||
String uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " +
|
String uidString = getRFIDString(rfid.uid.uidByte);
|
||||||
String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
|
|
||||||
Serial.println(uidString);
|
Serial.println(uidString);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearInt(MFRC522 mfrc522) {
|
|
||||||
mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x60);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRAM_ATTR rfid_interrupt() {
|
|
||||||
newRfidInt = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
|
|
@ -221,18 +225,14 @@ void setup()
|
||||||
pinMode(CS_RFID, OUTPUT);
|
pinMode(CS_RFID, OUTPUT);
|
||||||
pinMode(CS_SDCARD, OUTPUT);
|
pinMode(CS_SDCARD, OUTPUT);
|
||||||
|
|
||||||
|
|
||||||
deactivateRFID();
|
deactivateRFID();
|
||||||
|
activateSD();
|
||||||
|
|
||||||
|
|
||||||
// first parameter is name of access point, second is the password
|
// first parameter is name of access point, second is the password
|
||||||
AsyncWiFiManager wifiManager(&server, &dns);
|
AsyncWiFiManager wifiManager(&server, &dns);
|
||||||
|
|
||||||
wifiManager.autoConnect("HannaBox");
|
wifiManager.autoConnect("HannaBox");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Serial.print("Initializing SD card...");
|
Serial.print("Initializing SD card...");
|
||||||
|
|
||||||
if (!SD.begin(CS_SDCARD))
|
if (!SD.begin(CS_SDCARD))
|
||||||
|
|
@ -246,7 +246,6 @@ void setup()
|
||||||
rootNode.buildDirectoryTree("/");
|
rootNode.buildDirectoryTree("/");
|
||||||
rootNode.printDirectoryTree();
|
rootNode.printDirectoryTree();
|
||||||
|
|
||||||
|
|
||||||
deactivateSD();
|
deactivateSD();
|
||||||
activateRFID();
|
activateRFID();
|
||||||
Serial.println("RFID");
|
Serial.println("RFID");
|
||||||
|
|
@ -255,33 +254,21 @@ void setup()
|
||||||
// Init SPI bus
|
// Init SPI bus
|
||||||
SPI.begin(-1, -1, -1, CS_RFID);
|
SPI.begin(-1, -1, -1, CS_RFID);
|
||||||
rfid.PCD_Init(CS_RFID, RST_RFID);
|
rfid.PCD_Init(CS_RFID, RST_RFID);
|
||||||
if (rfid.PCD_PerformSelfTest()) {
|
|
||||||
Serial.println("RFID OK");
|
|
||||||
|
|
||||||
/*
|
if (rfid.PCD_PerformSelfTest())
|
||||||
* Allow the ... irq to be propagated to the IRQ pin
|
{
|
||||||
* For test purposes propagate the IdleIrq and loAlert
|
Serial.println("RFID OK");
|
||||||
*/
|
}
|
||||||
rfid.PCD_WriteRegister(rfid.ComIEnReg, 0x60);
|
else
|
||||||
attachInterrupt(IRQ_RFID, rfid_interrupt, RISING);
|
{
|
||||||
//clear a spourious interrupt at start
|
|
||||||
/*
|
|
||||||
do {
|
|
||||||
;
|
|
||||||
} while (!newRfidInt);
|
|
||||||
newRfidInt = false; //interrupt flag
|
|
||||||
*/
|
|
||||||
} else {
|
|
||||||
Serial.println("RFID Self Test failed!");
|
Serial.println("RFID Self Test failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
|
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
|
||||||
audio.setVolume(volume); // 0...21
|
audio.setVolume(volume); // 0...21
|
||||||
|
|
||||||
|
|
||||||
Serial.println("Audio initialized.");
|
Serial.println("Audio initialized.");
|
||||||
|
|
||||||
|
|
||||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||||
{ request->send_P(200, "text/html", index_html, processor); });
|
{ request->send_P(200, "text/html", index_html, processor); });
|
||||||
|
|
||||||
|
|
@ -309,49 +296,66 @@ void setup()
|
||||||
next(); });
|
next(); });
|
||||||
|
|
||||||
server.begin();
|
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.");
|
Serial.println("initialization done.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
{
|
||||||
|
if (audio.isRunning())
|
||||||
{
|
{
|
||||||
deactivateRFID();
|
deactivateRFID();
|
||||||
activateSD();
|
activateSD();
|
||||||
audio.loop();
|
audio.loop();
|
||||||
deactivateSD();
|
deactivateSD();
|
||||||
activateRFID();
|
activateRFID();
|
||||||
/*
|
}
|
||||||
if(rfid.PICC_IsNewCardPresent()) {
|
|
||||||
stop();
|
|
||||||
readRFID();
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if (newRfidInt) {
|
rfid_loop--;
|
||||||
|
if (rfid_loop == 0)
|
||||||
Serial.println("Interrupt.");
|
{
|
||||||
if(rfid.PICC_IsNewCardPresent()) {
|
rfid_loop = RFID_LOOP_INTERVAL;
|
||||||
|
deactivateSD();
|
||||||
|
activateRFID();
|
||||||
|
if (rfid.PICC_IsNewCardPresent())
|
||||||
|
{
|
||||||
stop();
|
stop();
|
||||||
readRFID();
|
readRFID();
|
||||||
}
|
}
|
||||||
//clearInt(rfid);
|
|
||||||
newRfidInt = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (buttonPressed(BTN_START_STOP))
|
||||||
|
{
|
||||||
if (buttonPressed(BTN_START_STOP)) {
|
if (audio.isRunning())
|
||||||
if (audio.isRunning()) {
|
{
|
||||||
stop();
|
stop();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
delay(5);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean buttonPressed(const uint8_t pin) {
|
void loop2(void *parameter)
|
||||||
|
{
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean buttonPressed(const uint8_t pin)
|
||||||
|
{
|
||||||
if (digitalRead(pin) == LOW)
|
if (digitalRead(pin) == LOW)
|
||||||
{
|
{
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
|
|
@ -364,4 +368,3 @@ boolean buttonPressed(const uint8_t pin) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue