Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- Version 2, December 2004
- Copyright (C) 2004 Sam Hocevar <[email protected]>
- Everyone is permitted to copy and distribute verbatim or modified
- copies of this license document, and changing it is allowed as long
- as the name is changed.
- DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- 0. You just DO WHAT THE FUCK YOU WANT TO.
- */
- #define _XTAL_FREQ 8000000
- #include <xc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "config.h"
- #include "GLCD.h"
- #define FALSE 0
- #define TRUE 1
- int estat_tecles[16] = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
- char mapa_tecles[16] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'};
- //char mapa_tecles[16] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};
- void config_teclat(); // Configureu aquí el PORTD per treballar amb les tecles
- void config_o_glcd(); // Configureu aquí el PORTD per treballar amb el GLCD
- void llegir_teclat(); // Escanejeu el teclat per saber quines tecles s'han premut
- void pintar_teclat(); // Feu aquí el processat de la taula de booleans i pinteu tecles
- void Delay(int s);
- #define SEQ_LEN 10
- #define DELAY_SEQ 1000
- char sequencia[SEQ_LEN];
- int n_correctes = 0;
- void generar_sequencia();
- void mostra_sequencia();
- char wait_key_press();
- void game_over();
- void guanyat();
- void splash();
- void credits();
- void main(void)
- {
- GLCDinit();
- splash();
- generar_sequencia();
- while (1) {
- if (n_correctes == SEQ_LEN) {
- guanyat();
- n_correctes = 0;
- generar_sequencia();
- } else {
- mostra_sequencia();
- int i = 0, correcte = 1;
- while ((i <= n_correctes) && correcte) {
- char c = wait_key_press();
- config_o_glcd();
- _putch(1, i*8, c);
- if (sequencia[i] != c) {
- correcte = 0;
- } else _putch(0, i*8, c);
- i++;
- }
- if (!correcte) {
- game_over();
- n_correctes = 0;
- generar_sequencia();
- } else n_correctes++;
- }
- }
- }
- void splash()
- {
- clearFullGLCD();
- char s[] = "Welcome!\nPress any key\nto start";
- writeTxt(0,0,s);
- wait_key_press();
- clearFullGLCD();
- }
- void Delay(int s)
- {
- int d;
- for (d = 0; d < (s*100); d++) {
- __delay_ms(10);
- }
- }
- void guanyat(){
- config_o_glcd();
- clearFullGLCD();
- char s[64];
- sprintf(s, "You Win!");
- writeTxt(0,0,s);
- wait_key_press();
- clearFullGLCD();
- }
- void game_over()
- {
- config_o_glcd();
- clearFullGLCD();
- char s[64];
- sprintf(s, "You Lose!\nCorrect keys: %d", n_correctes);
- writeTxt(0,0,s);
- wait_key_press();
- int i;
- for(i=0;i<1;i++) credits();
- clearFullGLCD();
- }
- char wait_key_press()
- {
- unsigned long long t = 0;
- config_teclat();
- while(1) {
- int i, j;
- for (i = 0; i < 4; i++) {
- PORTD = ((1<<4)<<i); // Activa la fila
- for (j = 0; j < 4; j++) {
- if ((PORTD & (1<<j))) {
- while ((PORTD & (1<<j))) t++;
- srand(t);
- return mapa_tecles[i*4+j];
- }
- }
- }
- t++;
- }
- return ' ';
- }
- void mostra_sequencia()
- {
- config_o_glcd();
- int i;
- for (i = 0; i <= n_correctes; i++) {
- _putch(0, i*8, sequencia[i]);
- }
- Delay(1);
- clearFullGLCD();
- }
- void generar_sequencia()
- {
- int i;
- for (i = 0; i < SEQ_LEN; i++) {
- sequencia[i] = mapa_tecles[rand()%16];
- }
- }
- void pintar_teclat()
- {
- int i, j;
- for (i = 0; i < 4; i++) {
- for (j = 0; j < 4; j++) {
- if (estat_tecles[i*4+j]) {
- _putch(i, j*8, mapa_tecles[i*4+j]);
- }
- }
- }
- }
- void llegir_teclat()
- {
- int i, j;
- for (i = 0; i < 4; i++) {
- PORTD = ((1<<4)<<i); // Activa la fila
- for (j = 0; j < 4; j++) {
- estat_tecles[i*4+j] = ((PORTD & (1<<j)) ? TRUE : FALSE);
- }
- }
- }
- void config_o_glcd()
- {
- TRISD = 0x00;
- }
- void config_teclat()
- {
- TRISD = 0x0F; //D0-D3 IN, D4-D7 OUT
- }
- void credits()
- {
- clearFullGLCD();
- static const char *str_credits = {
- "Fet per:\n"
- "Sergi Granell\n"
- "Alejandro Hidalgo\n"
- "Grup 20"
- };
- int i;
- for (i = 64; i >= 0; i--) {
- setStartLine(i);
- writeTxt(0, 0, str_credits);
- __delay_ms(98);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement