I'm having trouble explaining my issue. What I'm doing is creating a library to work easily with the RFID reader/writer RC522 module. I need to create an object of the library MFRC522 in my .cpp file to be able to communticate with the module itself. I want my constructor end up like this:UltimateRFID rfid(53, 5);
, the parameters being the slave select and the reset pins, respectively.
The problem is that I don't know how to make my constructor so that it is within the scope of the functions that require the MFRC522 object to be called. Below's my code.
#include "Arduino.h"
#include "UltimateRFID.h"
#include <MFRC522.h>
MFRC522::MIFARE_Key key;
UltimateRFID::UltimateRFID(int ss, int rst)
{
MFRC522 mfrc522(ss, rst); //this would be ideal, but how can my functions access the instance?
}
void UltimateRFID::initialize() //sample of a function that uses the MFRC522 object
{
//digitalWrite(49, HIGH);
digitalWrite(2, LOW);
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
mfrc522.PCD_Init();
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
}
Now comes my header file, although I'm not sure it matters much.
#ifndef UltimateRFID_h
#define UltimateRFID_h
#include "Arduino.h"
#include <MFRC522.h>
class UltimateRFID
{
public:
UltimateRFID(int ss, int rst);
void initialize();
private:
MFRC522::MIFARE_Key key;
};
#endif
Please be patient, it's my first library.