created Rfid class
This commit is contained in:
		
							parent
							
								
									69e6e69c77
								
							
						
					
					
						commit
						24783454e8
					
				| @ -1,9 +1,24 @@ | |||||||
| #include "Keyboard.h" | #include "Keyboard.h" | ||||||
|  | 
 | ||||||
| #define DEBUG | #define DEBUG | ||||||
| #define PIN_WIRE_SDA D3 | #define PIN_WIRE_SDA D3 | ||||||
| #define PIN_WIRE_SCL D4 | #define PIN_WIRE_SCL D4 | ||||||
| Keyboard::Keyboard(uint8_t _debounce) | 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; |     this->_debounce = _debounce; | ||||||
| } | } | ||||||
| void Keyboard::begin(TwoWire *databus) | void Keyboard::begin(TwoWire *databus) | ||||||
| @ -42,23 +57,7 @@ void Keyboard::scan() | |||||||
|     { |     { | ||||||
|         pcf8574->digitalWrite(i, LOW); |         pcf8574->digitalWrite(i, LOW); | ||||||
|         delay(15); |         delay(15); | ||||||
|         for (int j = 1; j < 5; j++) |         scanColumn(&key, 1, 4); | ||||||
|         { // 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; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|         pcf8574->digitalWrite(i, HIGH); |         pcf8574->digitalWrite(i, HIGH); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -76,7 +75,8 @@ void Keyboard::scanAsync() | |||||||
|         _current_scan_col = 0; |         _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++) |     for (int j = start; j <= stop; j++) | ||||||
|     { // Rows
 |     { // Rows
 | ||||||
|         (*key_ptr)++; |         (*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) | char Keyboard::mapChr(uint8_t key) | ||||||
| { | { | ||||||
| 
 |     return this->keybind[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'; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| char Keyboard::getLastChr() | char Keyboard::getLastChr() | ||||||
| { | { | ||||||
|  | |||||||
| @ -2,11 +2,13 @@ | |||||||
| #define KEYBOARD_CLASS | #define KEYBOARD_CLASS | ||||||
| #include "PCF8574.h" | #include "PCF8574.h" | ||||||
| #include <bits/stdc++.h>  | #include <bits/stdc++.h>  | ||||||
|  | #include <map> | ||||||
| class Keyboard | class Keyboard | ||||||
| { | { | ||||||
| private: | private: | ||||||
|     unsigned long _timeElapsed; |     unsigned long _timeElapsed; | ||||||
|     std::deque<char> _buffer; |     std::deque<char> _buffer; | ||||||
|  |     std::map<int,char> keybind; | ||||||
|     uint8_t _debounce; |     uint8_t _debounce; | ||||||
|     PCF8574* pcf8574; |     PCF8574* pcf8574; | ||||||
|     uint8_t _lastKey; |     uint8_t _lastKey; | ||||||
| @ -19,7 +21,7 @@ public: | |||||||
|     void scan(); |     void scan(); | ||||||
|     void scanAsync(); |     void scanAsync(); | ||||||
|     void begin(TwoWire *databus); |     void begin(TwoWire *databus); | ||||||
|     static char mapChr(uint8_t key); |     char mapChr(uint8_t key); | ||||||
|     char getLastChr(); |     char getLastChr(); | ||||||
|     bool available(); |     bool available(); | ||||||
|     void clear(); |     void clear(); | ||||||
|  | |||||||
							
								
								
									
										50
									
								
								src/Rfid.cpp
									
									
									
									
									
								
							
							
						
						
									
										50
									
								
								src/Rfid.cpp
									
									
									
									
									
								
							| @ -1,9 +1,55 @@ | |||||||
| #include "Rfid.h" | #include "Rfid.h" | ||||||
| 
 | #define SS_PIN D8 | ||||||
| Rfid::Rfid(/* args */) | #define RST_PIN D1 | ||||||
|  | #define RFID_TIMEOUT 3000 | ||||||
|  | Rfid::Rfid(/* args */) : mfrc522(SS_PIN) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Rfid::~Rfid() | 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; | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								src/Rfid.h
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/Rfid.h
									
									
									
									
									
								
							| @ -1,8 +1,20 @@ | |||||||
|  | #pragma once | ||||||
|  | #include <SPI.h> | ||||||
|  | #include <MFRC522.h> | ||||||
| class Rfid | class Rfid | ||||||
| { | { | ||||||
| private: | private: | ||||||
|     /* data */ |     MFRC522 mfrc522; // Create MFRC522 instance
 | ||||||
|  |     MFRC522::MIFARE_Key key; | ||||||
|  |     int status = 0; | ||||||
|  |     String rfid = ""; | ||||||
|  |     String lastRfid = ""; | ||||||
|  |     unsigned long lastRfidScan = 0; | ||||||
| public: | public: | ||||||
|     Rfid(/* args */); |     Rfid(/* args */); | ||||||
|     ~Rfid(); |     ~Rfid(); | ||||||
|  |     void begin(); | ||||||
|  |     void scan(); | ||||||
|  |     bool available(); | ||||||
|  |     String getID(); | ||||||
| }; | }; | ||||||
							
								
								
									
										43
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								src/main.cpp
									
									
									
									
									
								
							| @ -2,18 +2,10 @@ | |||||||
| #include <Arduino.h> | #include <Arduino.h> | ||||||
| #include <Wire.h> | #include <Wire.h> | ||||||
| #include "Keyboard.h" | #include "Keyboard.h" | ||||||
| #include <SPI.h> | #include "Rfid.h" | ||||||
| #include <MFRC522.h> |  | ||||||
| 
 | 
 | ||||||
| // RFID Reader
 | // Rfid
 | ||||||
| #define SS_PIN D8 | Rfid rfid; | ||||||
| #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; |  | ||||||
| // i2C Bus
 | // i2C Bus
 | ||||||
| #define PIN_WIRE_SDA D3 | #define PIN_WIRE_SDA D3 | ||||||
| #define PIN_WIRE_SCL D4 | #define PIN_WIRE_SCL D4 | ||||||
| @ -35,12 +27,9 @@ void setup() | |||||||
| 	// LCD
 | 	// LCD
 | ||||||
| 	lcd.init(); | 	lcd.init(); | ||||||
| 	lcd.backlight(); | 	lcd.backlight(); | ||||||
|  | 	rfid.begin(); | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| 	// RFID
 |  | ||||||
| 	SPI.begin(); |  | ||||||
| 	SPI.setClockDivider(SPI_CLOCK_DIV8); |  | ||||||
| 	mfrc522.PCD_Init(); |  | ||||||
| 	mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| namespace states | namespace states | ||||||
| @ -60,26 +49,10 @@ namespace states | |||||||
| void loop() | void loop() | ||||||
| { | { | ||||||
| 	keyboard.scanAsync(); | 	keyboard.scanAsync(); | ||||||
| 
 | 	rfid.scan(); | ||||||
| 	if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) | 	if(rfid.available()){ | ||||||
| 	{ |  | ||||||
| 		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) |  | ||||||
| 		{ |  | ||||||
| 
 |  | ||||||
| 		    display_state = states::READ_RFID; | 		    display_state = states::READ_RFID; | ||||||
|             displayUpdate = true; |             displayUpdate = true; | ||||||
| 			Serial.print(rfid); |  | ||||||
| 			lastRfid = rfid; |  | ||||||
| 			lastRfidScan = millis(); |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 	if (keyboard.available()) | 	if (keyboard.available()) | ||||||
| 	{ | 	{ | ||||||
| @ -166,7 +139,7 @@ void loop() | |||||||
| 			lcd.print("Card detected."); | 			lcd.print("Card detected."); | ||||||
| 			lcd.setCursor(0, 1); | 			lcd.setCursor(0, 1); | ||||||
| 
 | 
 | ||||||
| 			lcd.print(rfid); | 			lcd.print(rfid.getID()); | ||||||
| 			displayTimer1 = millis() + 3000; | 			displayTimer1 = millis() + 3000; | ||||||
| 			display_state = states::DELAY; | 			display_state = states::DELAY; | ||||||
| 			break; | 			break; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user