<commit_before>/**
 * Name: RFID
 * Author: <NAME> (<EMAIL>)
 * Version: 1.0
 * Description: A library for interfacing RFID readers
                with either a UART or a Wiegand protocol
 * Attribution: The library was based on the work of Johann Richard
 *              https://github.com/johannrichard/SeeedRFIDLib
 * License: Copyright (c) 2013 <NAME> 
 *          This library is licensed under the MIT license
 *          http://www.opensource.org/licenses/mit-license.php
 *
 * Filename: RFID.cpp
 * File description: Implementation of methods for the RFID library
 */

#include "RFID.h"

byte RFID::_dataLen;  // Only for Wiegand Mode
volatile unsigned long long RFID::_databits;  // Stores the data bits
volatile unsigned char RFID::_bitCount;  // Counts the number of bits received so far
volatile boolean RFID::_wiegandIdAvailable;

// Forwards control to the right instance initializer
RFID::RFID(RFIDMode mode, byte arg)
{
    switch (mode)
    {
        case RFID_UART:
            initUART(arg);
            break;
        case RFID_WIEGAND:
            initWiegand(arg);
    }
}

// Initializes a UART interface
void RFID::initUART(byte rxPin)
{
    // Initializes RFIDTag fields
    _tag.mfr = 0;
    _tag.id = 0;
    _tag.chk = 0;
    _tag.valid = false;

    // Sets protocol parameters
    _uartIdAvailable = false;
    _bytesRead = 0;
    _libMode = RFID_UART;

    // Defines txPin as rxPin+1, but the library will not utilize that pin
    // So, txPin is available for use
    _rfidIO = new SoftwareSerial(rxPin, rxPin+1);
    _rfidIO->begin(9600);
}

// Initializes a Wiegand Interface
// This can only be used on PIN2 & PIN3, INT0 and INT1, respectively
// The dataLen attribute specifies the number of bits to read 
// The library currently only understands 26 bit or 35 bit formats
// Only one instance of the Wiegand interface can be supported
void RFID::initWiegand(byte dataLen)
{
    // Initializes RFIDTag fields
    _tag.mfr = 0;
    _tag.id = 0;
    _tag.chk = 0;
    _tag.valid = false;

    // Sets protocol parameters
    _dataLen = dataLen;
    _libMode = RFID_WIEGAND;
    
    resetWiegand();

    #if defined(__AVR_ATmega32U4__) // For Leonardo
    
    attachInterrupt(0, DATA1, FALLING);
    attachInterrupt(1, DATA0, FALLING);
    
    #else  // For the rest of boards
    
    attachInterrupt(0, DATA0, FALLING);
    attachInterrupt(1, DATA1, FALLING);
    
    #endif
}

// Reports whether an new RFID tag is available for reading
boolean RFID::available()
{ 
    switch (_libMode)
    {
        case RFID_UART:
            return prepareRFIDTagUART();
        case RFID_WIEGAND:
            return _wiegandIdAvailable;
    }
}

// Returns an RFIDTag construct with newly
// received data, and resets the interface
// variables to be ready for a new reading
RFIDTag RFID::getTag()
{
    switch (_libMode)
    {
        case RFID_UART:
            _uartIdAvailable = false;
            return _tag;
        case RFID_WIEGAND:
            refreshWiegand();
            return _tag;
    }
}

// Checks for UART Data and prepares an RFIDTag construct
boolean RFID::prepareRFIDTagUART()
{
    // Method outline
    // 1) Read until an STX byte is received
    // 2) Store any new data characters
    // 3) Upon ETX byte reception finalize the ID

    byte hex;

    if ( _rfidIO->available() )
    {
        // Upon STX character reception, it initializes parameters
        if ( ( hex = _rfidIO->read() ) == 0x02 )
        {
            _bytesRead = 0; 
    
            _tag.mfr = 0;
            _tag.id  = 0;
            _tag.chk = 0;
            _tag.valid = false;
            for (byte i = 0; i < 13; ++i) _tag.raw[i] = 0;
        
            _uartIdAvailable = false;
            
            #ifdef DEBUG
            Serial.println("\nSTART");
            #endif      
        }  // Upon ETX character reception, it finalizes ID
        else if ( hex == 0x03 )
        {<commit_msg>Added listen on serial before each IO<commit_after>/**
 * Name: RFID
 * Author: <NAME> (<EMAIL>)
 * Version: 1.0
 * Description: A library for interfacing RFID readers
                with either a UART or a Wiegand protocol
 * Attribution: The library was based on the work of <NAME>
 *              https://github.com/johannrichard/SeeedRFIDLib
 * License: Copyright (c) 2013 <NAME> 
 *          This library is licensed under the MIT license
 *          http://www.opensource.org/licenses/mit-license.php
 *
 * Filename: RFID.cpp
 * File description: Implementation of methods for the RFID library
 */

#include "RFID.h"

byte RFID::_dataLen;  // Only for Wiegand Mode
volatile unsigned long long RFID::_databits;  // Stores the data bits
volatile unsigned char RFID::_bitCount;  // Counts the number of bits received so far
volatile boolean RFID::_wiegandIdAvailable;

// Forwards control to the right instance initializer
RFID::RFID(RFIDMode mode, byte arg)
{
    switch (mode)
    {
        case RFID_UART:
            initUART(arg);
            break;
        case RFID_WIEGAND:
            initWiegand(arg);
    }
}

// Initializes a UART interface
void RFID::initUART(byte rxPin)
{
    // Initializes RFIDTag fields
    _tag.mfr = 0;
    _tag.id = 0;
    _tag.chk = 0;
    _tag.valid = false;

    // Sets protocol parameters
    _uartIdAvailable = false;
    _bytesRead = 0;
    _libMode = RFID_UART;

    // Defines txPin as rxPin+1, but the library will not utilize that pin
    // So, txPin is available for use
    _rfidIO = new SoftwareSerial(rxPin, rxPin+1);
    _rfidIO->begin(9600);
}

// Initializes a Wiegand Interface
// This can only be used on PIN2 & PIN3, INT0 and INT1, respectively
// The dataLen attribute specifies the number of bits to read 
// The library currently only understands 26 bit or 35 bit formats
// Only one instance of the Wiegand interface can be supported
void RFID::initWiegand(byte dataLen)
{
    // Initializes RFIDTag fields
    _tag.mfr = 0;
    _tag.id = 0;
    _tag.chk = 0;
    _tag.valid = false;

    // Sets protocol parameters
    _dataLen = dataLen;
    _libMode = RFID_WIEGAND;
    
    resetWiegand();

    #if defined(__AVR_ATmega32U4__) // For Leonardo
    
    attachInterrupt(0, DATA1, FALLING);
    attachInterrupt(1, DATA0, FALLING);
    
    #else  // For the rest of boards
    
    attachInterrupt(0, DATA0, FALLING);
    attachInterrupt(1, DATA1, FALLING);
    
    #endif
}

// Reports whether an new RFID tag is available for reading
boolean RFID::available()
{ 
    switch (_libMode)
    {
        case RFID_UART:
            return prepareRFIDTagUART();
        case RFID_WIEGAND:
            return _wiegandIdAvailable;
    }
}

// Returns an RFIDTag construct with newly
// received data, and resets the interface
// variables to be ready for a new reading
RFIDTag RFID::getTag()
{
    switch (_libMode)
    {
        case RFID_UART:
            _uartIdAvailable = false;
            return _tag;
        case RFID_WIEGAND:
            refreshWiegand();
            return _tag;
    }
}

// Checks for UART Data and prepares an RFIDTag construct
boolean RFID::prepareRFIDTagUART()
{
    // Method outline
    // 1) Read until an STX byte is received
    // 2) Store any new data characters
    // 3) Upon ETX byte reception finalize the ID

    byte hex;

    _rfidIO->listen();

    if ( _rfidIO->available() )
    {
        // Upon STX character reception, it initializes parameters
        if ( ( hex = _rfidIO->read() ) == 0x02 )
        {
            _bytesRead = 0; 
    
            _tag.mfr = 0;
            _tag.id  = 0;
            _tag.chk = 0;
            _tag.valid = false;
            for (byte i = 0; i < 13; ++i) _tag.raw[i] = 0;
        
            _uartIdAvailable = false;
            
            #ifdef DEBUG
            Serial.println("\nSTART");
            #endif      
        }  // Upon ETX character reception, it finalizes ID
        else if ( hex == 0x03 )
        {
{'fraction_non_alphanumeric': 0.08942575259296737, 'fraction_numerical': 0.013913483430306097, 'mean_word_length': 2.184454289166331, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6280142', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2442, 'n_words': 800}