Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 65a17c7

Browse files
Mover estado e controle do placar dos jogadores para uma classe
1 parent 1c424fc commit 65a17c7

3 files changed

Lines changed: 98 additions & 37 deletions

File tree

src/Placarduino.ino

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "LcdBigNumbers.h"
66
#include "MusicPlayer.h"
7+
#include "PlayerControl.h"
78

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

3334
// Configurações dos jogadores e placar
34-
const char firstPlayerName[] = "Jogador 1";
35-
int firstPlayerScore = 0;
36-
int subPlayer1ButtonState = LOW;
37-
int addPlayer1ButtonState = LOW;
38-
39-
const char secondPlayerName[] = "Jogador 2";
40-
int secondPlayerScore = 0;
41-
int subPlayer2ButtonState = LOW;
42-
int addPlayer2ButtonState = LOW;
35+
PlayerControl player1(PIN_BTN_ADD_PL1, PIN_BTN_SUB_PL1);
36+
PlayerControl player2(PIN_BTN_ADD_PL2, PIN_BTN_SUB_PL2);
4337

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

5347
void setup()
5448
{
49+
player1.setName("Jogador 1");
50+
player2.setName("Jogador 2");
51+
5552
setupInputs();
5653
setupLCD();
5754
setupBuzzer();
5855

5956
printWelcome();
6057
playWelcome();
6158

62-
delay(3000);
59+
delay(1000);
6360

6461
printScore();
6562
}
@@ -118,50 +115,41 @@ void playWelcome()
118115
musicPlayer.cMajorScale();
119116
}
120117

121-
void checkScoreInput(int inputPin, int *stateStore, int *scoreStore, int op, void(*feedbackFunction)())
122-
{
123-
int newButtonState = digitalRead(inputPin);
124-
125-
if (newButtonState != *stateStore && newButtonState == HIGH) {
126-
// Placar não deve ficar negativo, então a operação só é feita se o placar for continuar >= 0
127-
if (*scoreStore + op >= 0) {
128-
*scoreStore += op;
129-
printScore();
130-
feedbackFunction();
131-
}
132-
}
133-
134-
*stateStore = newButtonState;
135-
}
136-
137118
void checkButtons()
138119
{
139-
checkScoreInput(PIN_BTN_ADD_PL1, &addPlayer1ButtonState, &firstPlayerScore, +1, playFeedbackPositive);
140-
checkScoreInput(PIN_BTN_SUB_PL1, &subPlayer1ButtonState, &firstPlayerScore, -1, playFeedbackNegative);
141-
checkScoreInput(PIN_BTN_ADD_PL2, &addPlayer2ButtonState, &secondPlayerScore, +1, playFeedbackPositive);
142-
checkScoreInput(PIN_BTN_SUB_PL2, &subPlayer2ButtonState, &secondPlayerScore, -1, playFeedbackNegative);
120+
int player1Changed = player1.updateScore();
121+
int player2Changed = player2.updateScore();
122+
123+
if (player1Changed > 0 || player2Changed > 0) {
124+
printScore();
125+
playFeedbackPositive();
126+
}
127+
else if (player1Changed < 0 || player2Changed < 0) {
128+
printScore();
129+
playFeedbackNegative();
130+
}
143131
}
144132

145133
void printScore()
146134
{
147135
lcd.clear();
148136

149137
lcd.setCursor(0, 0);
150-
lcd.print(firstPlayerName);
138+
lcd.print(player1.getName());
151139

152140
// Alinha o nome do jogador à direita
153-
lcd.setCursor(LCD_COLS - strlen(secondPlayerName), 0);
154-
lcd.print(secondPlayerName);
141+
lcd.setCursor(LCD_COLS - strlen(player2.getName()), 0);
142+
lcd.print(player1.getName());
155143

156-
bigNumbers.printNumber(firstPlayerScore, 0, 1);
144+
bigNumbers.printNumber(player1.getScore(), 0, 1);
157145

158146
// Altera coluna inicial de acordo com valor da pontuação para
159147
// manter alinhado à direita
160-
if (secondPlayerScore >= 10) {
161-
bigNumbers.printNumber(secondPlayerScore, LCD_COLS - 7, 1);
148+
if (player2.getScore() >= 10) {
149+
bigNumbers.printNumber(player2.getScore(), LCD_COLS - 7, 1);
162150
}
163151
else {
164-
bigNumbers.printNumber(secondPlayerScore, LCD_COLS - 3, 1);
152+
bigNumbers.printNumber(player2.getScore(), LCD_COLS - 3, 1);
165153
}
166154
}
167155

src/PlayerControl.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <string.h>
2+
3+
#include "PlayerControl.h"
4+
5+
PlayerControl::PlayerControl(const int addScorePin, const int subScorePin)
6+
{
7+
this->addScoreButton.setPin(addScorePin);
8+
this->subScoreButton.setPin(subScorePin);
9+
10+
memset(this->name, 0, sizeof(this->name));
11+
}
12+
13+
void PlayerControl::setName(const char *name)
14+
{
15+
strncpy(this->name, name, PlayerControl::MAX_NAME_LENGTH);
16+
}
17+
18+
const char* PlayerControl::getName() const
19+
{
20+
return this->name;
21+
}
22+
23+
const int PlayerControl::getScore() const
24+
{
25+
return this->score;
26+
}
27+
28+
const int PlayerControl::updateScore()
29+
{
30+
int change = 0;
31+
32+
if (this->addScoreButton.pressed()) {
33+
change = +1;
34+
}
35+
else if (this->score > 0 && this->subScoreButton.pressed()) {
36+
change = -1;
37+
}
38+
39+
this->score += change;
40+
41+
return change;
42+
}

src/PlayerControl.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef PLACARDUINO_PLAYER_CONTROL_H
2+
#define PLACARDUINO_PLAYER_CONTROL_H
3+
4+
#include <Arduino.h>
5+
6+
#include "RisingEdgeButton.h"
7+
8+
class PlayerControl
9+
{
10+
public:
11+
PlayerControl(const int addScorePin, const int subScorePin);
12+
13+
static const size_t MAX_NAME_LENGTH = 10;
14+
15+
void setName(const char *name);
16+
const char* getName() const;
17+
18+
const int getScore() const;
19+
20+
const int updateScore();
21+
22+
private:
23+
char name[PlayerControl::MAX_NAME_LENGTH + 1];
24+
int score = 0;
25+
26+
// Buttons
27+
RisingEdgeButton addScoreButton;
28+
RisingEdgeButton subScoreButton;
29+
};
30+
31+
#endif // PLACARDUINO_PLAYER_CONTROL_H

0 commit comments

Comments
 (0)