Permalink
Browse files

Adicionar a leitura de smart cards com nomes dos jogadores

  • Loading branch information...
eduardoweiland committed Jan 25, 2018
1 parent 6ce647f commit a3a134dbea6cd2b06375bd2a23715088431d1fbc
Showing with 99 additions and 2 deletions.
  1. +33 −2 src/Placarduino.ino
  2. +43 −0 src/PlayerCard.cpp
  3. +23 −0 src/PlayerCard.h
@@ -1,10 +1,14 @@
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <Tone.h>
#include <string.h>

#include "LcdBigNumbers.h"
#include "MusicPlayer.h"
#include "SmartCard.h"

#include "PlayerControl.h"
#include "PlayerCard.h"

/*
* CONSTANTES
@@ -16,6 +20,9 @@
#define LCD_ROWS 4

// Configurações de pinos
#define PIN_RFID_SS 10
#define PIN_RFID_RESET 9

#define PIN_BTN_SUB_PL1 7
#define PIN_BTN_ADD_PL1 6
#define PIN_BTN_SUB_PL2 5
@@ -35,6 +42,12 @@ LcdBigNumbers bigNumbers(&lcd);
PlayerControl player1(PIN_BTN_ADD_PL1, PIN_BTN_SUB_PL1);
PlayerControl player2(PIN_BTN_ADD_PL2, PIN_BTN_SUB_PL2);

// Leitor RFID
byte rfidKey[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
SmartCard smartCard(rfidKey, PIN_RFID_SS, PIN_RFID_RESET);
PlayerCard playerCard(&smartCard, 1);
PlayerControl *playerToConfigure = &player1;

// Objeto para controle de um buzzer
Tone buzzer;
MusicPlayer musicPlayer(&buzzer);
@@ -59,6 +72,10 @@ void setup()
delay(1000);

printScore();

smartCard.begin();

Serial.begin(9600);
}


@@ -68,10 +85,10 @@ void setup()

void loop()
{
readPlayerCard();
checkButtons();
}


/*
* FUNÇÕES
*/
@@ -112,7 +129,7 @@ void printWelcome()

void playWelcome()
{
musicPlayer.cMajorScale();
musicPlayer.superMarioTheme();
}

void checkButtons()
@@ -153,6 +170,20 @@ void printScore()
}
}

void readPlayerCard()
{
if (playerCard.readPlayerNameFromCard(playerToConfigure)) {
if (playerToConfigure == &player1) {
playerToConfigure = &player2;
}
else {
playerToConfigure = &player1;
}

printScore();
}
}

void playFeedbackPositive()
{
musicPlayer.playNote(NOTE_C6, 140);
@@ -0,0 +1,43 @@
#include <string.h>

#include "PlayerCard.h"

PlayerCard::PlayerCard(SmartCard *smartCard, const byte blockNumber)
{
this->smartCard = smartCard;
this->blockNumber = blockNumber;
}

bool PlayerCard::readPlayerNameFromCard(PlayerControl *player)
{
byte buffer[18];
byte length = sizeof(buffer);

if (!this->smartCard->read(this->blockNumber, buffer, &length)) {
return false;
}

Serial.print(F("Dados lidos do cartão: "));
Serial.println((char*)buffer);

if (!this->isValidPlayerName(buffer, strnlen((const char*)buffer, length))) {
Serial.println(F("Nome contém caracteres inválidos, ignorando"));
return false;
}

player->setName((const char*)buffer);
return true;
}

bool PlayerCard::isValidPlayerName(const byte *name, const size_t length) const
{
size_t i;

for (i = 0; i < length; ++i) {
if (!isAlphaNumeric(name[i])) {
return false;
}
}

return true;
}
@@ -0,0 +1,23 @@
#ifndef PLACARDUINO_PLAYER_CARD_H
#define PLACARDUINO_PLAYER_CARD_H

#include <Arduino.h>

#include "SmartCard.h"
#include "PlayerControl.h"

class PlayerCard
{
public:
PlayerCard(SmartCard *smartCard, const byte blockNumber);

bool readPlayerNameFromCard(PlayerControl *player);

private:
SmartCard *smartCard;
byte blockNumber;

bool isValidPlayerName(const byte *name, const size_t length) const;
};

#endif // PLACARDUINO_PLAYER_CARD_H

0 comments on commit a3a134d

Please sign in to comment.