I want to make a class to connect the ESP32 to a PN532 module, i made a header file:
Cardreader.h:
#ifndef Cardreader_h
#define Cardreader_h
#include <Arduino.h>
#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <IPSDisplay.h>
class Cardreader
{
public:
Cardreader(IPSDisplay ipsDisplay);
void begin();
static void scanCard(void * parameter);
private:
TaskHandle_t task_ScanCard;
IPSDisplay _ipsDisplay;
PN532_SPI pn532spi(SPI, 5);
PN532 nfc(pn532spi);
};
#endif
With a Cardreader.cpp file:
#include <Arduino.h>
#include <Cardreader.h>
#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532.h>
#include <IPSDisplay.h>
Cardreader::Cardreader(IPSDisplay ipsDisplay)
{
_ipsDisplay = ipsDisplay;
}
void Cardreader::begin() {
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
_ipsDisplay.show("/i003");
delay(5000);
begin();
}
else {
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A Card ...");
xTaskCreatePinnedToCore(this->scanCard, "scanCard", 5000, this, 1, &task_ScanCard, 0);
}
}
void Cardreader::scanCard(void * parameter) {
for(;;) {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHex(uid, uidLength);
Serial.println("");
if (uidLength == 7)
{
// We probably have a Mifare Ultralight card ...
Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
// Try to read the first general-purpose user page (#4)
Serial.println("Reading page 4");
uint8_t data[32];
success = nfc.mifareultralight_ReadPage (4, data);
if (success)
{
// Data seems to have been read ... spit it out
nfc.PrintHexChar(data, 4);
Serial.println("");
// Wait a bit before reading the card again
delay(1000);
}
else
{
Serial.println("Ooops ... unable to read the requested page!?");
}
}
}
}
}
I am getting some errors i don’t know how to fix:
In file included from src\main.cpp:7:0:
lib\Cardreader/Cardreader.h:19:24: error: 'SPI' is not a type
PN532_SPI pn532spi(SPI, 5);
^
lib\Cardreader/Cardreader.h:19:29: error: expected identifier before numeric constant
PN532_SPI pn532spi(SPI, 5);
^
lib\Cardreader/Cardreader.h:19:29: error: expected ',' or '...' before numeric constant
lib\Cardreader/Cardreader.h:20:15: error: 'pn532spi' is not a type
PN532 nfc(pn532spi);
^
Archiving .pio\build\esp32dev\lib90f\libAdafruit BusIO.a
Archiving .pio\build\esp32dev\libf36\libAdafruit PN532.a
Archiving .pio\build\esp32dev\libd7b\libIPSDisplay.a
Archiving .pio\build\esp32dev\lib449\libInternet.a
Compiling .pio\build\esp32dev\lib428\PN532\llcp.cpp.o
Compiling .pio\build\esp32dev\lib428\PN532\mac_link.cpp.o
Compiling .pio\build\esp32dev\lib428\PN532\snep.cpp.o
*** [.pio\build\esp32dev\src\main.cpp.o] Error 1
====================================================
Thanks for the help already