Gabriel Aviña
/
Laboratorio5
Laboratorio informática industrial 5
main.cpp@1:bc801083d70a, 2014-03-27 (annotated)
- Committer:
- elchef
- Date:
- Thu Mar 27 05:12:46 2014 +0000
- Revision:
- 1:bc801083d70a
- Parent:
- 0:ab62d30dacdc
Lol;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elchef | 1:bc801083d70a | 1 | /*Este es el código de un juego donde 2 jugadores tienen el objetivo de picar un botón mientras un led se encuentre encendido. |
elchef | 1:bc801083d70a | 2 | El led se encuentra encendido durante un tiempo generado aleatoriamente por eso el uso de una función rand(). |
elchef | 1:bc801083d70a | 3 | Se usan estructuras para el puntaje de ambos jugadores, un timer para leer si uno de los dos botones fue presionado,el led integrado de la freedom como indicador. |
elchef | 1:bc801083d70a | 4 | Al presionar un botón se realiza una interrupción del código y se apaga el led, eso genera que el puntaje del jugador aumente y se imprima en un display de 7 segmentos. |
elchef | 1:bc801083d70a | 5 | Si se desea reiniciar el juego se puede oprimir un tercer botón de reseteo o se reinicia cuando cualquier jugador llegue al puntaje de 5.*/ |
elchef | 1:bc801083d70a | 6 | |
elchef | 0:ab62d30dacdc | 7 | #include "mbed.h" |
elchef | 0:ab62d30dacdc | 8 | |
elchef | 0:ab62d30dacdc | 9 | struct players { |
elchef | 0:ab62d30dacdc | 10 | int score; |
elchef | 0:ab62d30dacdc | 11 | } player1, player2; |
elchef | 0:ab62d30dacdc | 12 | |
elchef | 0:ab62d30dacdc | 13 | DigitalOut myled(LED3); |
elchef | 1:bc801083d70a | 14 | BusOut Display1 (PTC8, PTA5, PTA12, PTD4, PTA2, PTA1); //G,f,E,... |
elchef | 1:bc801083d70a | 15 | BusOut Display2 (PTC9, PTA13, PTD5, PTD0, PTD2, PTD3, PTD1);//G,f,E,. |
elchef | 0:ab62d30dacdc | 16 | InterruptIn Bplayer1 (PTC2); |
elchef | 0:ab62d30dacdc | 17 | InterruptIn Bplayer2 (PTC3); |
elchef | 0:ab62d30dacdc | 18 | DigitalIn Boton3 (PTC4); |
elchef | 1:bc801083d70a | 19 | int numero_rand; |
elchef | 1:bc801083d70a | 20 | int juegos = 0; |
elchef | 0:ab62d30dacdc | 21 | Timer tiempo; |
elchef | 0:ab62d30dacdc | 22 | |
elchef | 0:ab62d30dacdc | 23 | void Player1Score() |
elchef | 0:ab62d30dacdc | 24 | { |
elchef | 0:ab62d30dacdc | 25 | player1.score = player1.score+1; |
elchef | 0:ab62d30dacdc | 26 | tiempo.reset(); |
elchef | 0:ab62d30dacdc | 27 | } |
elchef | 0:ab62d30dacdc | 28 | void Player2Score() |
elchef | 0:ab62d30dacdc | 29 | { |
elchef | 0:ab62d30dacdc | 30 | player2.score = player2.score+1; |
elchef | 0:ab62d30dacdc | 31 | tiempo.reset(); |
elchef | 0:ab62d30dacdc | 32 | } |
elchef | 0:ab62d30dacdc | 33 | int CeroAlNueve(int paso) |
elchef | 0:ab62d30dacdc | 34 | { |
elchef | 0:ab62d30dacdc | 35 | int Valor = 0; |
elchef | 0:ab62d30dacdc | 36 | switch (paso) { |
elchef | 0:ab62d30dacdc | 37 | case 0: |
elchef | 0:ab62d30dacdc | 38 | Valor = 63; |
elchef | 0:ab62d30dacdc | 39 | break; |
elchef | 0:ab62d30dacdc | 40 | case 1: |
elchef | 0:ab62d30dacdc | 41 | Valor = 6; |
elchef | 0:ab62d30dacdc | 42 | break; |
elchef | 0:ab62d30dacdc | 43 | case 2: |
elchef | 0:ab62d30dacdc | 44 | Valor = 91; |
elchef | 0:ab62d30dacdc | 45 | break; |
elchef | 0:ab62d30dacdc | 46 | case 3: |
elchef | 0:ab62d30dacdc | 47 | Valor = 79; |
elchef | 0:ab62d30dacdc | 48 | break; |
elchef | 0:ab62d30dacdc | 49 | case 4: |
elchef | 0:ab62d30dacdc | 50 | Valor = 102; |
elchef | 0:ab62d30dacdc | 51 | break; |
elchef | 0:ab62d30dacdc | 52 | case 5: |
elchef | 0:ab62d30dacdc | 53 | Valor = 109; |
elchef | 0:ab62d30dacdc | 54 | break; |
elchef | 0:ab62d30dacdc | 55 | case 6: |
elchef | 0:ab62d30dacdc | 56 | Valor = 125; |
elchef | 0:ab62d30dacdc | 57 | break; |
elchef | 0:ab62d30dacdc | 58 | case 7: |
elchef | 0:ab62d30dacdc | 59 | Valor = 7; |
elchef | 0:ab62d30dacdc | 60 | break; |
elchef | 0:ab62d30dacdc | 61 | case 8: |
elchef | 0:ab62d30dacdc | 62 | Valor = 127; |
elchef | 0:ab62d30dacdc | 63 | break; |
elchef | 0:ab62d30dacdc | 64 | case 9: |
elchef | 0:ab62d30dacdc | 65 | Valor = 103; |
elchef | 0:ab62d30dacdc | 66 | break; |
elchef | 0:ab62d30dacdc | 67 | } |
elchef | 0:ab62d30dacdc | 68 | return Valor; |
elchef | 0:ab62d30dacdc | 69 | } |
elchef | 0:ab62d30dacdc | 70 | |
elchef | 0:ab62d30dacdc | 71 | void PrintDisplay(int puntos1, int puntos2) |
elchef | 0:ab62d30dacdc | 72 | { |
elchef | 0:ab62d30dacdc | 73 | Display1 = CeroAlNueve(puntos1); |
elchef | 0:ab62d30dacdc | 74 | Display2 = CeroAlNueve(puntos2); |
elchef | 0:ab62d30dacdc | 75 | } |
elchef | 0:ab62d30dacdc | 76 | |
elchef | 0:ab62d30dacdc | 77 | int main () |
elchef | 0:ab62d30dacdc | 78 | { |
elchef | 0:ab62d30dacdc | 79 | tiempo.start(); |
elchef | 0:ab62d30dacdc | 80 | srand (time(NULL)); |
elchef | 1:bc801083d70a | 81 | numero_rand = rand() % 10 + 1; |
elchef | 0:ab62d30dacdc | 82 | myled=1; |
elchef | 1:bc801083d70a | 83 | Bplayer1.rise(&Player1Score); |
elchef | 1:bc801083d70a | 84 | Bplayer2.rise(&Player2Score); |
elchef | 1:bc801083d70a | 85 | while(1) { |
elchef | 0:ab62d30dacdc | 86 | |
elchef | 0:ab62d30dacdc | 87 | player1.score= 0; |
elchef | 0:ab62d30dacdc | 88 | player2.score = 0; |
elchef | 0:ab62d30dacdc | 89 | PrintDisplay(player1.score, player2.score); |
elchef | 1:bc801083d70a | 90 | while(tiempo.read() >= numero_rand) { |
elchef | 1:bc801083d70a | 91 | myled=0; |
elchef | 1:bc801083d70a | 92 | |
elchef | 0:ab62d30dacdc | 93 | } |
elchef | 0:ab62d30dacdc | 94 | if(Boton3 == 1) { |
elchef | 0:ab62d30dacdc | 95 | return 0; |
elchef | 0:ab62d30dacdc | 96 | } |
elchef | 1:bc801083d70a | 97 | if((player1.score || player2.score) == 5) { |
elchef | 1:bc801083d70a | 98 | player1.score= 0; |
elchef | 1:bc801083d70a | 99 | player2.score = 0; |
elchef | 1:bc801083d70a | 100 | } |
elchef | 0:ab62d30dacdc | 101 | } |
elchef | 0:ab62d30dacdc | 102 | } |
elchef | 1:bc801083d70a | 103 |