Files
pingpong-counter/src/main.cpp

153 lines
3.4 KiB
C++

#include <Arduino.h>
#include <game.h>
#include <MD_MAX72xx.h>
#include <MD_Parola.h>
#include "../include/fonts.h"
// Hardware SPI:
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define SS_PIN 5
#define DATA_PIN 23
#define CLK_PIN 18
#define B1_IN 26
#define B2_IN 25
using namespace std;
// Create a new instance of the MD_MAX72XX class:
MD_Parola matrix = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, SS_PIN, MAX_DEVICES);
int score[2]= {0, 0};
bool start = false;
int serving;
bool game = true;
char servingChar = '=';
char displayOut[5] = {'0','0',servingChar,'0','0'};
int convertASCII(int score);
char setServeChar(char servingChar, int* score, int serving);
void setDisplay(int* score, MD_Parola* matrix, char* displayOut);
void setup() {
// put your setup code here, to run once:
pinMode(B1_IN, INPUT_PULLUP);
pinMode(B2_IN, INPUT_PULLUP);
// Intialize the object:
matrix.begin();
// Set the intensity (brightness) of the display (0-15):
matrix.setIntensity(15);
// Clear the display:
matrix.displayClear();
matrix.setTextAlignment(PA_CENTER);
matrix.setFont(pingpong_font);
Serial.begin(9600);
while (digitalRead(B1_IN) == 1 && digitalRead(B2_IN) == 1) {
}
if (digitalRead(B1_IN) == 0) {
while (digitalRead(B1_IN) == 0) {}
serving = 0;
displayOut[2] = setServeChar(servingChar, score, serving);
setDisplay(score, &matrix, displayOut);
delay(500); // to avoid calling twice
}
if (digitalRead(B2_IN) == 0) {
while (digitalRead(B2_IN) == 0) {}
serving = 1;
displayOut[2] = setServeChar(servingChar, score, serving);
setDisplay(score, &matrix, displayOut);
delay(500); // to avoid calling twice
}
}
void loop() {
while (true) {
while (digitalRead(B1_IN) == 1 && digitalRead(B2_IN) == 1) {
}
if (digitalRead(B1_IN) == 0) {
while (digitalRead(B1_IN) == 0) {
} // Only execute add point on button release. Point removal if other button is pressed during while loop.
if (game == true) {
scored(score, 0);
if (checkWin(score, 0) == 0) {
displayOut[2] = servingChar;
game = false;
}
else {
displayOut[2] = setServeChar(servingChar, score, serving);
delay(500); // to avoid calling twice
}
setDisplay(score, &matrix, displayOut);
}
}
if (digitalRead(B2_IN) == 0) {
while (digitalRead(B2_IN) == 0) {
} // Only execute add point on button release. Point removal if other button is pressed during while loop.
if (game == true) {
scored(score, 1);
if (checkWin(score, 1) == 0) {
displayOut[2] = servingChar;
game = false;
}
else {
displayOut[2] = setServeChar(servingChar, score, serving);
delay(500); // to avoid calling twice
}
setDisplay(score, &matrix, displayOut);
}
}
}
setup();
}
// put function definitions here:
int convertASCII(int score) {
return(score+48);
}
char setServeChar(char servingChar, int* score, int serving) {
return (servingChar + 1 - 2*(servingPlayer(score, serving)));
}
void setDisplay(int* score, MD_Parola* matrix, char* displayOut) {
for (int i = 0; i < 2; i++) {
displayOut[i*3] = convertASCII((*(score+i) - (*(score+i)%10)) / 10);
displayOut[(i*3)+1] = convertASCII(*(score+i)%10);
}
matrix->print(displayOut);
}