Permalink
Browse files

Mover estado e controle do placar dos jogadores para uma classe

  • Loading branch information...
eduardoweiland committed Jan 13, 2018
1 parent 1c424fc commit 65a17c702fc61e1eac20e00efa50540a301fca26
Showing with 98 additions and 37 deletions.
  1. +25 −37 src/Placarduino.ino
  2. +42 −0 src/PlayerControl.cpp
  3. +31 −0 src/PlayerControl.h
@@ -4,6 +4,7 @@

#include "LcdBigNumbers.h"
#include "MusicPlayer.h"
#include "PlayerControl.h"

/*
* CONSTANTES
@@ -31,15 +32,8 @@ LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_COLS, LCD_ROWS);
LcdBigNumbers bigNumbers(&lcd);

// Configurações dos jogadores e placar
const char firstPlayerName[] = "Jogador 1";
int firstPlayerScore = 0;
int subPlayer1ButtonState = LOW;
int addPlayer1ButtonState = LOW;

const char secondPlayerName[] = "Jogador 2";
int secondPlayerScore = 0;
int subPlayer2ButtonState = LOW;
int addPlayer2ButtonState = LOW;
PlayerControl player1(PIN_BTN_ADD_PL1, PIN_BTN_SUB_PL1);
PlayerControl player2(PIN_BTN_ADD_PL2, PIN_BTN_SUB_PL2);

// Objeto para controle de um buzzer
Tone buzzer;
@@ -52,14 +46,17 @@ MusicPlayer musicPlayer(&buzzer);

void setup()
{
player1.setName("Jogador 1");
player2.setName("Jogador 2");

setupInputs();
setupLCD();
setupBuzzer();

printWelcome();
playWelcome();

delay(3000);
delay(1000);

printScore();
}
@@ -118,50 +115,41 @@ void playWelcome()
musicPlayer.cMajorScale();
}

void checkScoreInput(int inputPin, int *stateStore, int *scoreStore, int op, void(*feedbackFunction)())
{
int newButtonState = digitalRead(inputPin);

if (newButtonState != *stateStore && newButtonState == HIGH) {
// Placar não deve ficar negativo, então a operação só é feita se o placar for continuar >= 0
if (*scoreStore + op >= 0) {
*scoreStore += op;
printScore();
feedbackFunction();
}
}

*stateStore = newButtonState;
}

void checkButtons()
{
checkScoreInput(PIN_BTN_ADD_PL1, &addPlayer1ButtonState, &firstPlayerScore, +1, playFeedbackPositive);
checkScoreInput(PIN_BTN_SUB_PL1, &subPlayer1ButtonState, &firstPlayerScore, -1, playFeedbackNegative);
checkScoreInput(PIN_BTN_ADD_PL2, &addPlayer2ButtonState, &secondPlayerScore, +1, playFeedbackPositive);
checkScoreInput(PIN_BTN_SUB_PL2, &subPlayer2ButtonState, &secondPlayerScore, -1, playFeedbackNegative);
int player1Changed = player1.updateScore();
int player2Changed = player2.updateScore();

if (player1Changed > 0 || player2Changed > 0) {
printScore();
playFeedbackPositive();
}
else if (player1Changed < 0 || player2Changed < 0) {
printScore();
playFeedbackNegative();
}
}

void printScore()
{
lcd.clear();

lcd.setCursor(0, 0);
lcd.print(firstPlayerName);
lcd.print(player1.getName());

// Alinha o nome do jogador à direita
lcd.setCursor(LCD_COLS - strlen(secondPlayerName), 0);
lcd.print(secondPlayerName);
lcd.setCursor(LCD_COLS - strlen(player2.getName()), 0);
lcd.print(player1.getName());

bigNumbers.printNumber(firstPlayerScore, 0, 1);
bigNumbers.printNumber(player1.getScore(), 0, 1);

// Altera coluna inicial de acordo com valor da pontuação para
// manter alinhado à direita
if (secondPlayerScore >= 10) {
bigNumbers.printNumber(secondPlayerScore, LCD_COLS - 7, 1);
if (player2.getScore() >= 10) {
bigNumbers.printNumber(player2.getScore(), LCD_COLS - 7, 1);
}
else {
bigNumbers.printNumber(secondPlayerScore, LCD_COLS - 3, 1);
bigNumbers.printNumber(player2.getScore(), LCD_COLS - 3, 1);
}
}

@@ -0,0 +1,42 @@
#include <string.h>

#include "PlayerControl.h"

PlayerControl::PlayerControl(const int addScorePin, const int subScorePin)
{
this->addScoreButton.setPin(addScorePin);
this->subScoreButton.setPin(subScorePin);

memset(this->name, 0, sizeof(this->name));
}

void PlayerControl::setName(const char *name)
{
strncpy(this->name, name, PlayerControl::MAX_NAME_LENGTH);
}

const char* PlayerControl::getName() const
{
return this->name;
}

const int PlayerControl::getScore() const
{
return this->score;
}

const int PlayerControl::updateScore()
{
int change = 0;

if (this->addScoreButton.pressed()) {
change = +1;
}
else if (this->score > 0 && this->subScoreButton.pressed()) {
change = -1;
}

this->score += change;

return change;
}
@@ -0,0 +1,31 @@
#ifndef PLACARDUINO_PLAYER_CONTROL_H
#define PLACARDUINO_PLAYER_CONTROL_H

#include <Arduino.h>

#include "RisingEdgeButton.h"

class PlayerControl
{
public:
PlayerControl(const int addScorePin, const int subScorePin);

static const size_t MAX_NAME_LENGTH = 10;

void setName(const char *name);
const char* getName() const;

const int getScore() const;

const int updateScore();

private:
char name[PlayerControl::MAX_NAME_LENGTH + 1];
int score = 0;

// Buttons
RisingEdgeButton addScoreButton;
RisingEdgeButton subScoreButton;
};

#endif // PLACARDUINO_PLAYER_CONTROL_H

0 comments on commit 65a17c7

Please sign in to comment.