#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <TM1637.h>
#include "wires.h"
#include "variables.h"
#include "adc.h"
#include "timer.h"
#include "game.h"
// VARIABLES DE DEPURACIÓN
#define DEBUG_SKIP // Se saltea la presentación
// #define INPUT_PULL // Usa pull-up en los botones
// SETUP
void setup() {
// Preparación del LCD
lcd.init(); // Encendido LCD
lcd.backlight(); // Luz de fondo
lcd.clear();
// Preparación de TIMERS
tm0.init(); // Encendido TIMER_BLANCAS
tm0.set(BRIGHT_TYPICAL); // Luz de fondo
tm1.init(); // Encendido TIMER_NEGRAS
tm1.set(BRIGHT_TYPICAL); // Luz de fondo
#ifndef DEBUG_SKIP
// Presentación
lcd.setCursor(1, 1);
lcd.print("\xA1" "\xBF" "Ajedrez Pilaff?!"),
delay(500);
lcd.clear();
delay(250);
lcd.clear();
lcd.setCursor(1, 1);
lcd.print("\xA1" "\xBF" "Ajedrez Pilaff?!"),
delay(500);
lcd.clear();
delay(250);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("\xA1" "\xA1" " AJEDREZ PILAFF !!"),
lcd.setCursor(0, 2);
lcd.print("\xA1" "\xA1" " AJEDREZ PILAFF !!"),
delay(500);
lcd.clear();
delay(250);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("\xA1" "\xA1" " AJEDREZ PILAFF !!"),
lcd.setCursor(0, 2);
lcd.print("\xA1" "\xA1" " AJEDREZ PILAFF !!"),
delay(500);
lcd.clear();
delay(250);
#endif
// Menú de inicio
lcd.setCursor(3, 0);
lcd.print("Ajedrez Pilaff");
lcd.setCursor(0, 1);
lcd.print("Presione el bot" "\xF3" "n de");
lcd.setCursor(0, 2);
lcd.print("inicio para empezar!");
lcd.setCursor(9, 3);
lcd.print(":)");
// Declaración de pines
/*#ifdef INPUT_PULL
DDRD = 0x00 | (INPUT_PULLUP << POWER) |
(INPUT_PULLUP << WHITE) |
(INPUT_PULLUP << BLACK) ;
#endif
#ifndef INPUT_PULL
BUTTONS = 0x00 | (INPUT << POWER) |
(INPUT << WHITE) |
(INPUT << BLACK) ;
#endif
*/
pinMode(POWER, INPUT);
pinMode(WHITE, INPUT);
pinMode(BLACK, INPUT);
cli();
ADC_Set_Vref(VREF_AVCC);
ADC_Set_Prescaller(ADPS_2);
ADC_Init();
sei();
}
// LOOP GENERAL
/* La gracia es que este loop ocurra desde que aparece el menú
de inicio y vuelva a iniciar cuando se termine una partida.
El botón rojo debe ser un reset del loop, entonces se puede
usar para volver a elegir modo y reiniciar todos los timers.*/
// TODA UNA PARTIDA SE CONTIENE EN UNA VUELTA
void loop() {
millisCurrent = millis();
// Medir botón de encendido
if(millisCurrent > millisButtR && digitalRead(POWER)){
millisButtR = millisCurrent + 200;
power_st = !power_st;
}
// Si no se encendió, regresar al loop
if(power_st == 0) return;
// MENÚ DE MODO DE JUEGO
mode = changeMode();
// COMIENZO DE LA PARTIDA
gameStart(mode);
// PARTIDA EN SÍ
// TERMINA POR TIEMPO O PRESIONANDO EL BOTÓN POWER
while(time0 > 0 && time1 > 0){
millisCurrent = millis();
if(millisCurrent > millisButtR && digitalRead(POWER)){
millisButtR = millisCurrent + 200;
time1 = time0 = 0;
return;
}
if(millisCurrent > millisLCD) lcdtimers(time0, time1); // Muestra el tiempo restante
// TURNO NEGRAS
if(turn){
if(millisCurrent > millisButtB && digitalRead(WHITE)){ // Donación de 10 s
time1 = time1 + 10;
millisButtB = millisCurrent + 200;
}
if(millisCurrent > millisButtN && digitalRead(BLACK)){ // Fin de turno
turn = !turn;
millisButtN = millisCurrent + 200;
continue;
}
if(millisCurrent > millisTime1){ // Conteo
time1 = timer1(time1);
millisTime1 = millisCurrent + 1000;
}
}
// TURNO BLANCAS
else{
if(millisCurrent > millisButtN && digitalRead(BLACK)){ // Donación de 10 s
time0 = time0 + 10;
millisButtN = millisCurrent + 200;
}
if(millisCurrent > millisButtB && digitalRead(WHITE)){ // Fin de turno
turn = !turn;
millisButtB = millisCurrent + 200;
continue;
}
if(millisCurrent > millisTime0){ // Conteo
time0 = timer0(time0);
millisTime0 = millisCurrent + 1000;
}
}
}
if(time0 <= 0){ // BLANCAS SIN TIEMPO
lcd.setCursor(1,3);
lcd.print("L0S3R");
}
if(time1 <= 0){ // NEGRAS SIN TIEMPO
lcd.setCursor(14,3);
lcd.print("L0S3R");
}
if(time0 && time1){ // BOTÓN DE APAGADO
time0 = time1 = 0;
millisButtR = millisCurrent + 200;
}
power_st = !power_st;
delay(200);
lcd.clear();
lcd.setCursor(5, 1);
lcd.print("G4M3 0V3R");
delay(500);
}