// // semaforo na tela // // 28/09/07 // // JCBJ // #include "sem-tela.h" void PintaVermelho1 (void); void ApagaVermelho1 (void); void PintaAmarelo1 (void); void ApagaAmarelo1 (void); void PintaVerde1 (void); void ApagaVerde1 (void); void PintaVermelho2 (void); void ApagaVermelho2 (void); void PintaAmarelo2 (void); void ApagaAmarelo2 (void); void PintaVerde2 (void); void ApagaVerde2 (void); void PintaVermelhoPedestre (void); void ApagaVermelhoPedestre (void); void PintaVerdePedestre (void); void ApagaVerdePedestre (void); void JanelaVermelho1 (void); void JanelaAmarelo1 (void); void JanelaVerde1 (void); void JanelaVermelho2 (void); void JanelaAmarelo2 (void); void JanelaVerde2 (void); void JanelaVermelhoPedestre (void); void JanelaVerdePedestre (void); void AcoesIniciais (void); void main (void) { int mstate = 0; time_t tempo = 0; char ch = 0; int i; int pedestre = 0; int piscante = 0; AcoesIniciais (); // maquina de estado while (ch != 0x1b) { //////////////////////////// // acoes permanentes if (kbhit ()) { ch = getch (); // passagem de pedestre if ((ch == ' ') && (pedestre == 0)) pedestre = -1; // piscante if (ch == 'p') { if (piscante) piscante = 0; else piscante = 1; } } //////////////////////////// switch (mstate) { case 0: PintaVermelho1(); ApagaVermelho2(); PintaVerde2(); tempo = time (NULL); mstate = 1; break; case 1: // reduz o tempo de verde para o pedestre passar if ((difftime (time (NULL), tempo) > 5) && (pedestre)) tempo-=1; if (difftime (time (NULL), tempo) >= TEMPOVERDE2) mstate = 2; break; case 2: ApagaVerde2(); PintaAmarelo2(); tempo = time (NULL); mstate = 3; break; case 3: if (difftime (time (NULL), tempo) >= TEMPOAMARELO) mstate = 4; break; case 4: ApagaAmarelo2(); PintaVermelho2(); tempo = time (NULL); mstate = 5; // pedestre if (pedestre) { pedestre = mstate; mstate = 12; } // piscante if (piscante) mstate = 17; break; case 5: if (difftime (time (NULL), tempo) >= TEMPOVERMELHOVERMELHO) mstate = 6; break; case 6: ApagaVermelho1(); PintaVerde1(); tempo = time (NULL); mstate = 7; break; case 7: // reduz o tempo de verde para o pedestre passar if ((difftime (time (NULL), tempo) > 5) && (pedestre)) tempo-=1; if (difftime (time (NULL), tempo) >= TEMPOVERDE1) mstate = 8; break; case 8: ApagaVerde1(); PintaAmarelo1(); tempo = time (NULL); mstate = 9; break; case 9: if (difftime (time (NULL), tempo) > 1) mstate = 10; break; case 10: ApagaAmarelo1(); PintaVermelho1(); tempo = time (NULL); mstate = 11; // pedestre if (pedestre) { pedestre = mstate; mstate = 12; } // piscante if (piscante) mstate = 17; break; case 11: if (difftime (time (NULL), tempo) >= TEMPOVERMELHOVERMELHO) mstate = 0; break; // pedestre case 12: if (difftime (time (NULL), tempo) > 1) mstate = 13; break; case 13: ApagaVermelhoPedestre (); PintaVerdePedestre (); tempo = time (NULL); mstate = 14; break; case 14: if (difftime (time (NULL), tempo) >= TEMPOPEDESTRE) mstate = 15; break; case 15: ApagaVerdePedestre (); tempo = time (NULL); mstate = 16; break; case 16: if (difftime (time (NULL), tempo) >= TEMPOFIMPEDESTRE) { PintaVermelhoPedestre (); mstate = pedestre; pedestre = 0; tempo = time (NULL) + 1; } else { if (difftime (time (NULL), tempo) >= 1) { if ((long)difftime (time (NULL), tempo)%2) PintaVermelhoPedestre (); else ApagaVermelhoPedestre (); } } break; // piscante case 17: ApagaVermelho1 (); ApagaVermelho2 (); i = 0; mstate = 18; break; case 18: if ((time (NULL)%2) != i) { i = time (NULL)%2; if (i) { PintaAmarelo1 (); PintaAmarelo2 (); PintaVermelhoPedestre (); } else { ApagaAmarelo1 (); ApagaAmarelo2 (); ApagaVermelhoPedestre (); } } // sai do piscante if (!piscante) { ApagaAmarelo1 (); ApagaAmarelo2 (); PintaVermelhoPedestre (); pedestre = 0; mstate = 0; } break; default: window (1,1,80, 50); gotoxy (34, 48); printf (" DEU PAU %d ", mstate); mstate = 0; break; } } // fim do programa textmode (C80); _setcursortype (_NORMALCURSOR); } void AcoesIniciais (void) { int i, x; // acoes iniciais textmode (C4350); _setcursortype (_NOCURSOR); textbackground (BLUE); clrscr (); // desenha as bordas dos semaforos // linhas horizontais for (i = 1; i < 3; i++) { for (x = 0; x < 15; x++) { gotoxy (1+(20*i) + x, 5); putch ('Í'); gotoxy (1+(20*i) + x, 15); putch ('Í'); gotoxy (1+(20*i) + x, 25); putch ('Í'); gotoxy (1+(20*i) + x, 35); putch ('Í'); } } // linhas verticais for (i = 6; i < 35; i++) { gotoxy (20, i); putch ('º'); gotoxy (36, i); putch ('º'); gotoxy (40, i); putch ('º'); gotoxy (56, i); putch ('º'); } // cantos gotoxy (20,5); putch ('É'); gotoxy (36,5); putch ('»'); gotoxy (20,35); putch ('È'); gotoxy (36,35); putch ('¼'); gotoxy (40,5); putch ('É'); gotoxy (56,5); putch ('»'); gotoxy (40,35); putch ('È'); gotoxy (56,35); putch ('¼'); gotoxy (20,15); putch ('Ì'); gotoxy (20,25); putch ('Ì'); gotoxy (36,15); putch ('¹'); gotoxy (36,25); putch ('¹'); gotoxy (40,15); putch ('Ì'); gotoxy (40,25); putch ('Ì'); gotoxy (56,15); putch ('¹'); gotoxy (56,25); putch ('¹'); // pedestre // linhas horizontais for (i = 64; i < 74; i++) { gotoxy (i, 10); putch ('Í'); gotoxy (i, 15); putch ('Í'); gotoxy (i, 20); putch ('Í'); } // linhas verticais for (i = 11; i < 20; i++) { gotoxy (63, i); putch ('º'); gotoxy (74, i); putch ('º'); } // cantos gotoxy (63,10); putch ('É'); gotoxy (74,10); putch ('»'); gotoxy (63,20); putch ('È'); gotoxy (74,20); putch ('¼'); gotoxy (63,15); putch ('Ì'); gotoxy (74,15); putch ('¹'); // limpa os fundos do semaforo ApagaVermelho1(); ApagaAmarelo1(); ApagaVerde1(); ApagaVermelho2(); ApagaAmarelo2(); ApagaVerde2(); PintaVermelhoPedestre (); ApagaVerdePedestre (); } void PintaVermelho1 (void) { JanelaVermelho1 (); textbackground (RED); clrscr (); } void ApagaVermelho1 (void) { JanelaVermelho1 (); textbackground (BLACK); clrscr (); } void PintaAmarelo1 (void) { JanelaAmarelo1 (); textbackground (BROWN); clrscr (); } void ApagaAmarelo1 (void) { JanelaAmarelo1 (); textbackground (BLACK); clrscr (); } void PintaVerde1 (void) { JanelaVerde1 (); textbackground (GREEN); clrscr (); } void ApagaVerde1 (void) { JanelaVerde1 (); textbackground (BLACK); clrscr (); } void PintaVermelho2 (void) { JanelaVermelho2 (); textbackground (RED); clrscr (); } void ApagaVermelho2 (void) { JanelaVermelho2 (); textbackground (BLACK); clrscr (); } void PintaAmarelo2 (void) { JanelaAmarelo2 (); textbackground (BROWN); clrscr (); } void ApagaAmarelo2 (void) { JanelaAmarelo2 (); textbackground (BLACK); clrscr (); } void PintaVerde2 (void) { JanelaVerde2 (); textbackground (GREEN); clrscr (); } void ApagaVerde2 (void) { JanelaVerde2 (); textbackground (BLACK); clrscr (); } void PintaVermelhoPedestre (void) { JanelaVermelhoPedestre (); textbackground (RED); clrscr (); } void ApagaVermelhoPedestre (void) { JanelaVermelhoPedestre (); textbackground (BLACK); clrscr (); } void PintaVerdePedestre (void) { JanelaVerdePedestre (); textbackground (GREEN); clrscr (); } void ApagaVerdePedestre (void) { JanelaVerdePedestre (); textbackground (BLACK); clrscr (); } void JanelaVermelho1 (void) { window (21, 6, 35, 14); } void JanelaAmarelo1 (void) { window (21, 16, 35, 24); } void JanelaVerde1 (void) { window (21, 26, 35, 34); } void JanelaVermelho2 (void) { window (41, 6, 55, 14); } void JanelaAmarelo2 (void) { window (41, 16, 55, 24); } void JanelaVerde2 (void) { window (41, 26, 55, 34); } void JanelaVermelhoPedestre (void) { window (64, 11, 73, 14); } void JanelaVerdePedestre (void) { window (64, 16, 73, 19); }