created Rfid class

This commit is contained in:
Jean Jacques Avril 2021-09-29 23:08:37 +02:00
parent 69e6e69c77
commit 24783454e8
5 changed files with 111 additions and 108 deletions

View File

@ -1,9 +1,24 @@
#include "Keyboard.h"
#define DEBUG
#define PIN_WIRE_SDA D3
#define PIN_WIRE_SCL D4
Keyboard::Keyboard(uint8_t _debounce)
{
this->keybind.insert({
{1, '1'},
{2, '4'},
{3, '7'},
{4, 'O'},
{5, '2'},
{6, '5'},
{7, '8'},
{8, '0'},
{9, '3'},
{10, '6'},
{11, '9'},
{12, 'X'},
});
this->_debounce = _debounce;
}
void Keyboard::begin(TwoWire *databus)
@ -42,23 +57,7 @@ void Keyboard::scan()
{
pcf8574->digitalWrite(i, LOW);
delay(15);
for (int j = 1; j < 5; j++)
{ // Rows
key++;
uint8_t val = pcf8574->digitalRead(j);
#ifdef DEBUG
Serial.println("i=" + String(i) + " j=" + String(j) + " v=" + String(val));
delay(500);
#endif
if (val == 0)
{
_timeElapsed = millis();
this->_lastKey = key;
this->_buffer.push_back(mapChr(key));
pcf8574->digitalWrite(i, HIGH);
return;
}
}
scanColumn(&key, 1, 4);
pcf8574->digitalWrite(i, HIGH);
}
}
@ -76,7 +75,8 @@ void Keyboard::scanAsync()
_current_scan_col = 0;
}
void Keyboard::scanColumn(uint8_t* key_ptr, uint8_t start, uint8_t stop){
void Keyboard::scanColumn(uint8_t *key_ptr, uint8_t start, uint8_t stop)
{
for (int j = start; j <= stop; j++)
{ // Rows
(*key_ptr)++;
@ -93,37 +93,7 @@ void Keyboard::scanColumn(uint8_t* key_ptr, uint8_t start, uint8_t stop){
}
char Keyboard::mapChr(uint8_t key)
{
switch (key)
{
case 1:
return '1';
case 2:
return '4';
case 3:
return '7';
case 4:
return 'O';
case 5:
return '2';
case 6:
return '5';
case 7:
return '8';
case 8:
return '0';
case 9:
return '3';
case 10:
return '6';
case 11:
return '9';
case 12:
return 'X';
default:
return 'F';
}
return this->keybind[key];
}
char Keyboard::getLastChr()
{

View File

@ -2,11 +2,13 @@
#define KEYBOARD_CLASS
#include "PCF8574.h"
#include <bits/stdc++.h>
#include <map>
class Keyboard
{
private:
unsigned long _timeElapsed;
std::deque<char> _buffer;
std::map<int,char> keybind;
uint8_t _debounce;
PCF8574* pcf8574;
uint8_t _lastKey;
@ -19,7 +21,7 @@ public:
void scan();
void scanAsync();
void begin(TwoWire *databus);
static char mapChr(uint8_t key);
char mapChr(uint8_t key);
char getLastChr();
bool available();
void clear();

View File

@ -1,9 +1,55 @@
#include "Rfid.h"
Rfid::Rfid(/* args */)
#define SS_PIN D8
#define RST_PIN D1
#define RFID_TIMEOUT 3000
Rfid::Rfid(/* args */) : mfrc522(SS_PIN)
{
}
Rfid::~Rfid()
{
}
void Rfid::begin()
{
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
this->mfrc522.PCD_Init();
this->mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
}
void Rfid::scan()
{
if (this->mfrc522.PICC_IsNewCardPresent() && this->mfrc522.PICC_ReadCardSerial())
{
this->rfid = "";
for (byte i = 0; i < this->mfrc522.uid.size; i++)
{
this->rfid += this->mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ";
this->rfid += String(this->mfrc522.uid.uidByte[i], HEX);
}
this->rfid.trim();
this->rfid.toUpperCase();
if (this->rfid != this->lastRfid || millis() > this->lastRfidScan + RFID_TIMEOUT)
{
status = 1;
Serial.print(this->rfid);
this->lastRfid = this->rfid;
this->lastRfidScan = millis();
}
}
}
bool Rfid::available()
{
if (this->status==1)
{
this->status = 0;
return true;
}
else
return false;
}
String Rfid::getID()
{
return this->rfid;
}

View File

@ -1,8 +1,20 @@
#pragma once
#include <SPI.h>
#include <MFRC522.h>
class Rfid
{
private:
/* data */
MFRC522 mfrc522; // Create MFRC522 instance
MFRC522::MIFARE_Key key;
int status = 0;
String rfid = "";
String lastRfid = "";
unsigned long lastRfidScan = 0;
public:
Rfid(/* args */);
~Rfid();
void begin();
void scan();
bool available();
String getID();
};

View File

@ -2,18 +2,10 @@
#include <Arduino.h>
#include <Wire.h>
#include "Keyboard.h"
#include <SPI.h>
#include <MFRC522.h>
#include "Rfid.h"
// RFID Reader
#define SS_PIN D8
#define RST_PIN D1
#define RFID_TIMEOUT 3000
MFRC522 mfrc522(SS_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key;
String rfid = "";
String lastRfid = "";
unsigned long lastRfidScan = 0;
// Rfid
Rfid rfid;
// i2C Bus
#define PIN_WIRE_SDA D3
#define PIN_WIRE_SCL D4
@ -35,12 +27,9 @@ void setup()
// LCD
lcd.init();
lcd.backlight();
rfid.begin();
// RFID
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
mfrc522.PCD_Init();
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
}
namespace states
@ -60,26 +49,10 @@ namespace states
void loop()
{
keyboard.scanAsync();
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
rfid = "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
rfid += mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ";
rfid += String(mfrc522.uid.uidByte[i], HEX);
}
rfid.trim();
rfid.toUpperCase();
if (rfid != lastRfid || millis() > lastRfidScan + RFID_TIMEOUT)
{
rfid.scan();
if(rfid.available()){
display_state = states::READ_RFID;
displayUpdate = true;
Serial.print(rfid);
lastRfid = rfid;
lastRfidScan = millis();
}
}
if (keyboard.available())
{
@ -166,7 +139,7 @@ void loop()
lcd.print("Card detected.");
lcd.setCursor(0, 1);
lcd.print(rfid);
lcd.print(rfid.getID());
displayTimer1 = millis() + 3000;
display_state = states::DELAY;
break;