Sunteți pe pagina 1din 5

//KBD stuff

#include "ps2dev.h" // to emulate a PS/2 device


#include <Keypad.h>
#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.77

PS2dev keyboard(2,3); // PS2dev object (2:data, 3:clock)


int enabled = 0; // pseudo variable for state of "keyboard"
unsigned char c; //char stores data recieved from computer for KBD

const byte ROWS = 3;


const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {
{'7', '8', '9'},
{'4', '5', '6'},
{'1', '2', '3'}

};

byte rowPins[ROWS] = {7, 6, 5};


byte colPins[COLS] = {4, 0, 1};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void Teclas(){

char customKey = customKeypad.getKey();

if (customKey == '1'){

keyboard.write(0x3d);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x3d);
delay(100);
}
if (customKey == '2'){
//Serial.println("Tecla 2 Apertada");
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '3'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '4'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '5'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '6'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '7'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '8'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
if (customKey == '9'){
keyboard.write(0x2a);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2a);
delay(100);
}
}

void ack()
{
//acknowledge commands
while(keyboard.write(0xFA));

int keyboardcommand(int command) {


unsigned char val;
switch (command) {
case 0xFF: //reset
ack();
//the while loop lets us wait for the host to be ready
while(keyboard.write(0xAA)!=0);
break;
case 0xFE: //resend
ack();
break;
case 0xF6: //set defaults
//enter stream mode
ack();
break;
case 0xF5: //disable data reporting
//FM
enabled = 0;
ack();
break;
case 0xF4: //enable data reporting
//FM
enabled = 1;
ack();
break;
case 0xF3: //set typematic rate
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
case 0xF2: //get device id
ack();
keyboard.write(0xAB);
keyboard.write(0x83);
break;
case 0xF0: //set scan code set
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
case 0xEE: //echo
//ack();
keyboard.write(0xEE);
break;
case 0xED: //set/reset LEDs
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
}
}

void noteiro()
{

// Look for new cards


if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Mostra UID na serial
String conteudo= "";
byte letra;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
// Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
// Serial.print(mfrc522.uid.uidByte[i], HEX);
conteudo.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
conteudo.concat(String(mfrc522.uid.uidByte[i], HEX));
}
// Serial.println();
conteudo.toUpperCase();

if (conteudo.substring(1) == "19 65 A9 89") //UID 1 - Chaveiro


{
keyboard.write(0x2e);
delay(100);
keyboard.write(0xF0);
keyboard.write(0x2e);
delay(1000);

if (conteudo.substring(1) == "A5 69 DD 89") //UID 2 - Cartao


{
//Serial.print("20 reais");

}
}

void setup()
{
// keyboard.write(0xAA);
SPI.begin(); // Inicia SPI bus
mfrc522.PCD_Init(); // Inicia MFRC522
}

void loop()
{

//if host device wants to send a command:


if( (digitalRead(3)==LOW) || (digitalRead(2) == LOW))

{
while(keyboard.read(&c)) ;
keyboardcommand(c);
while(keyboard.write(0xFA));
delay(100);
}
else
{
noteiro();
Teclas();

}
}