Advertisement
xerpi

simon PIC

Oct 29th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. /*
  2. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  3.                     Version 2, December 2004
  4.  
  5.  Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  6.  
  7.  Everyone is permitted to copy and distribute verbatim or modified
  8.  copies of this license document, and changing it is allowed as long
  9.  as the name is changed.
  10.  
  11.             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  12.    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  13.  
  14.   0. You just DO WHAT THE FUCK YOU WANT TO.
  15. */
  16.  
  17.  
  18.  
  19. #define _XTAL_FREQ 8000000
  20. #include <xc.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include "config.h"
  25. #include "GLCD.h"
  26.  
  27. #define FALSE 0
  28. #define TRUE  1
  29.  
  30. int estat_tecles[16] = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE};
  31. char mapa_tecles[16] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'};
  32. //char mapa_tecles[16] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};
  33. void config_teclat(); // Configureu aquí el PORTD per treballar amb les tecles
  34. void config_o_glcd(); // Configureu aquí el PORTD per treballar amb el GLCD
  35. void llegir_teclat(); // Escanejeu el teclat per saber quines tecles s'han premut
  36. void pintar_teclat(); // Feu aquí el processat de la taula de booleans i pinteu tecles
  37. void Delay(int s);
  38.  
  39. #define SEQ_LEN 10
  40. #define DELAY_SEQ 1000
  41. char sequencia[SEQ_LEN];
  42. int n_correctes = 0;
  43. void generar_sequencia();
  44. void mostra_sequencia();
  45. char wait_key_press();
  46. void game_over();
  47. void guanyat();
  48. void splash();
  49. void credits();
  50.  
  51. void main(void)
  52. {
  53.     GLCDinit();
  54.     splash();
  55.     generar_sequencia();
  56.    
  57.     while (1) {
  58.        
  59.         if (n_correctes == SEQ_LEN) {
  60.             guanyat();
  61.             n_correctes = 0;
  62.             generar_sequencia();
  63.         } else {
  64.             mostra_sequencia();
  65.             int i = 0, correcte = 1;
  66.             while ((i <= n_correctes) && correcte) {
  67.                 char c = wait_key_press();
  68.                 config_o_glcd();
  69.                 _putch(1, i*8, c);
  70.                 if (sequencia[i] != c) {
  71.                     correcte = 0;
  72.                 } else _putch(0, i*8, c);
  73.                 i++;
  74.             }
  75.             if (!correcte) {
  76.                 game_over();
  77.                 n_correctes = 0;
  78.                 generar_sequencia();   
  79.             } else n_correctes++;
  80.         }
  81.     }
  82. }
  83.  
  84. void splash()
  85. {
  86.     clearFullGLCD();
  87.     char s[] = "Welcome!\nPress any key\nto start";
  88.     writeTxt(0,0,s);
  89.     wait_key_press();
  90.     clearFullGLCD();
  91. }
  92.  
  93. void Delay(int s)
  94. {
  95.     int d;
  96.     for (d = 0; d < (s*100); d++) {
  97.         __delay_ms(10);
  98.     }
  99. }
  100.  
  101. void guanyat(){
  102.     config_o_glcd();
  103.     clearFullGLCD();
  104.     char s[64];
  105.     sprintf(s, "You Win!");
  106.     writeTxt(0,0,s);
  107.     wait_key_press();
  108.     clearFullGLCD();
  109. }
  110.  
  111. void game_over()
  112. {
  113.     config_o_glcd();
  114.     clearFullGLCD();
  115.     char s[64];
  116.     sprintf(s, "You Lose!\nCorrect keys: %d", n_correctes);
  117.     writeTxt(0,0,s);
  118.     wait_key_press();
  119.     int i;
  120.     for(i=0;i<1;i++) credits();
  121.    
  122.     clearFullGLCD();
  123. }
  124.  
  125. char wait_key_press()
  126. {
  127.     unsigned long long t = 0;
  128.     config_teclat();
  129.     while(1) {
  130.         int i, j;
  131.         for (i = 0; i < 4; i++) {
  132.             PORTD = ((1<<4)<<i); // Activa la fila
  133.             for (j = 0; j < 4; j++) {
  134.                 if ((PORTD & (1<<j))) {
  135.                     while ((PORTD & (1<<j))) t++;
  136.                     srand(t);
  137.                     return mapa_tecles[i*4+j];
  138.                 }
  139.             }
  140.         }
  141.         t++;
  142.     }
  143.     return ' ';
  144. }
  145.  
  146. void mostra_sequencia()
  147. {
  148.     config_o_glcd();
  149.     int i;
  150.     for (i = 0; i <= n_correctes; i++) {
  151.          _putch(0, i*8, sequencia[i]);
  152.     }
  153.     Delay(1);
  154.     clearFullGLCD();
  155. }
  156.  
  157. void generar_sequencia()
  158. {
  159.     int i;
  160.     for (i = 0; i < SEQ_LEN; i++) {
  161.         sequencia[i] = mapa_tecles[rand()%16];
  162.     }
  163. }
  164.  
  165. void pintar_teclat()
  166. {
  167.     int i, j;
  168.     for (i = 0; i < 4; i++) {
  169.         for (j = 0; j < 4; j++) {
  170.             if (estat_tecles[i*4+j]) {
  171.                 _putch(i, j*8, mapa_tecles[i*4+j]);
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177. void llegir_teclat()
  178. {
  179.     int i, j;
  180.     for (i = 0; i < 4; i++) {
  181.         PORTD = ((1<<4)<<i); // Activa la fila
  182.         for (j = 0; j < 4; j++) {
  183.             estat_tecles[i*4+j] = ((PORTD & (1<<j)) ? TRUE : FALSE);
  184.         }
  185.     }
  186. }
  187.  
  188. void config_o_glcd()
  189. {
  190.     TRISD = 0x00;
  191. }
  192.  
  193. void config_teclat()
  194. {
  195.     TRISD = 0x0F; //D0-D3 IN, D4-D7 OUT
  196. }
  197.  
  198. void credits()
  199. {
  200.     clearFullGLCD();
  201.     static const char *str_credits = {
  202.         "Fet per:\n"
  203.         "Sergi Granell\n"
  204.         "Alejandro Hidalgo\n"
  205.         "Grup 20"
  206.     };
  207.     int i;
  208.     for (i = 64; i >= 0; i--) {
  209.         setStartLine(i);
  210.         writeTxt(0, 0, str_credits);   
  211.         __delay_ms(98);
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement