APS SO

Dependencies:   WebSocketClient mbed

Fork of APS_SO by Ian Bolfarini

Committer:
ianwillianb
Date:
Sat Oct 28 22:45:10 2017 +0000
Revision:
1:3da12598c635
Parent:
0:f603fa86c646
Child:
2:27a7a42b2bae
Adi??o de fun??es de ajuste de data/hora, bem como inclus?o de teste de entrada inv?lida

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ianwillianb 0:f603fa86c646 1 #include "mbed.h"
ianwillianb 0:f603fa86c646 2 #include "Rtc_Ds1307.h"
ianwillianb 0:f603fa86c646 3
ianwillianb 0:f603fa86c646 4
ianwillianb 0:f603fa86c646 5 //Inicialização do Led RGB
ianwillianb 0:f603fa86c646 6 DigitalOut led(LED_BLUE);
ianwillianb 0:f603fa86c646 7 //Inicialização do RTC
ianwillianb 0:f603fa86c646 8 Rtc_Ds1307 rtc(D14,D15);
ianwillianb 0:f603fa86c646 9 //Estrutura responsável por armazenar os dados do RTC
ianwillianb 0:f603fa86c646 10 Rtc_Ds1307::Time_rtc alarm_time ;
ianwillianb 0:f603fa86c646 11 //Estabelece comunicação serial para DEBUG
ianwillianb 0:f603fa86c646 12 Serial gSerial(USBTX, USBRX);
ianwillianb 0:f603fa86c646 13
ianwillianb 0:f603fa86c646 14 //Ajuste da hora atraves da interface serial
ianwillianb 0:f603fa86c646 15 void set_time_serial()
ianwillianb 0:f603fa86c646 16 {
ianwillianb 0:f603fa86c646 17 int hora, min, seg, dia, mes, ano, opt;
ianwillianb 0:f603fa86c646 18
ianwillianb 1:3da12598c635 19 gSerial.printf("\n1-Ajuste de Hora, 2-Ajuste de Data, 3-Ajuste de Data/Hora: ");
ianwillianb 0:f603fa86c646 20 gSerial.scanf("%d",&opt);
ianwillianb 1:3da12598c635 21 /* if(opt >=1 && opt <=3){
ianwillianb 1:3da12598c635 22
ianwillianb 1:3da12598c635 23
ianwillianb 1:3da12598c635 24 } */
ianwillianb 0:f603fa86c646 25
ianwillianb 0:f603fa86c646 26 if(opt == 1 || opt ==3 ) {
ianwillianb 0:f603fa86c646 27
ianwillianb 1:3da12598c635 28 gSerial.printf("\nInsira a hora(HH MM SS): ");
ianwillianb 0:f603fa86c646 29 gSerial.scanf("%d %d %d", &hora, &min, &seg);
ianwillianb 0:f603fa86c646 30
ianwillianb 0:f603fa86c646 31 if( (hora < 24 && hora>=0) && (min>=0 && min <60) && (seg>=0 && seg <60))
ianwillianb 0:f603fa86c646 32 {
ianwillianb 0:f603fa86c646 33
ianwillianb 0:f603fa86c646 34 rtc.getTime(alarm_time);
ianwillianb 0:f603fa86c646 35 alarm_time.hour = hora;
ianwillianb 0:f603fa86c646 36 alarm_time.min = min;
ianwillianb 0:f603fa86c646 37 alarm_time.sec = seg;
ianwillianb 1:3da12598c635 38 //alarm_time.date = 28;
ianwillianb 1:3da12598c635 39 //alarm_time.mon = 10;
ianwillianb 1:3da12598c635 40 //alarm_time.year = 2017;
ianwillianb 0:f603fa86c646 41 rtc.setTime(alarm_time, true, false);
ianwillianb 0:f603fa86c646 42 rtc.getTime(alarm_time);
ianwillianb 0:f603fa86c646 43 gSerial.printf("\nHora Ajustada: %2d:%02d:%02d \n",alarm_time.hour,alarm_time.min,alarm_time.sec);
ianwillianb 0:f603fa86c646 44
ianwillianb 0:f603fa86c646 45
ianwillianb 0:f603fa86c646 46 }
ianwillianb 0:f603fa86c646 47
ianwillianb 0:f603fa86c646 48 else gSerial.printf("Entrada Incorreta.\n");
ianwillianb 0:f603fa86c646 49
ianwillianb 0:f603fa86c646 50
ianwillianb 0:f603fa86c646 51 }
ianwillianb 0:f603fa86c646 52
ianwillianb 0:f603fa86c646 53
ianwillianb 1:3da12598c635 54 if(opt==2 || opt ==3)
ianwillianb 0:f603fa86c646 55 {
ianwillianb 0:f603fa86c646 56
ianwillianb 0:f603fa86c646 57 gSerial.printf("Insira a data(DIA MES ANO): ");
ianwillianb 0:f603fa86c646 58 gSerial.scanf("%d %d %d", &dia, &mes, &ano);
ianwillianb 1:3da12598c635 59 if((dia < 32 && dia >0 && mes<13 && mes>0 && ano>2000) && !(mes == 2 && dia >=29 ) && !((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia >30))
ianwillianb 1:3da12598c635 60 {
ianwillianb 0:f603fa86c646 61
ianwillianb 1:3da12598c635 62
ianwillianb 1:3da12598c635 63 rtc.getTime(alarm_time);
ianwillianb 1:3da12598c635 64 alarm_time.date = dia;
ianwillianb 1:3da12598c635 65 alarm_time.mon = mes;
ianwillianb 1:3da12598c635 66 alarm_time.year = ano;
ianwillianb 1:3da12598c635 67 rtc.setTime(alarm_time, true, false);
ianwillianb 1:3da12598c635 68 rtc.getTime(alarm_time);
ianwillianb 1:3da12598c635 69 gSerial.printf("\nData Ajustada: %2d/%02d/%d \n",alarm_time.date,alarm_time.mon,alarm_time.year);
ianwillianb 0:f603fa86c646 70
ianwillianb 1:3da12598c635 71 }
ianwillianb 1:3da12598c635 72
ianwillianb 1:3da12598c635 73 else gSerial.printf("Entrada Incorreta.\n");
ianwillianb 0:f603fa86c646 74
ianwillianb 0:f603fa86c646 75
ianwillianb 0:f603fa86c646 76 }
ianwillianb 1:3da12598c635 77
ianwillianb 1:3da12598c635 78 else gSerial.printf("Opcao Invalida!.\n");
ianwillianb 0:f603fa86c646 79
ianwillianb 1:3da12598c635 80
ianwillianb 0:f603fa86c646 81
ianwillianb 0:f603fa86c646 82
ianwillianb 0:f603fa86c646 83
ianwillianb 0:f603fa86c646 84 }
ianwillianb 1:3da12598c635 85
ianwillianb 1:3da12598c635 86 void ajus_data_hora()
ianwillianb 1:3da12598c635 87
ianwillianb 1:3da12598c635 88 {
ianwillianb 1:3da12598c635 89
ianwillianb 1:3da12598c635 90 char hadj;
ianwillianb 1:3da12598c635 91 gSerial.printf("Deseja ajustar a hora? - Caso sim digite s: ");
ianwillianb 1:3da12598c635 92 gSerial.scanf("%c",&hadj);
ianwillianb 1:3da12598c635 93 if(hadj=='s')
ianwillianb 1:3da12598c635 94 {
ianwillianb 1:3da12598c635 95 set_time_serial();
ianwillianb 1:3da12598c635 96
ianwillianb 1:3da12598c635 97 }
ianwillianb 1:3da12598c635 98
ianwillianb 1:3da12598c635 99 }
ianwillianb 1:3da12598c635 100
ianwillianb 0:f603fa86c646 101
ianwillianb 0:f603fa86c646 102
ianwillianb 0:f603fa86c646 103
ianwillianb 0:f603fa86c646 104 int main()
ianwillianb 0:f603fa86c646 105 {
ianwillianb 1:3da12598c635 106
ianwillianb 1:3da12598c635 107 char hadj; //opcao do ajuste do rtc
ianwillianb 1:3da12598c635 108 gSerial.printf("Deseja ajustar a hora? - Caso sim digite s: ");
ianwillianb 1:3da12598c635 109 gSerial.scanf("%c",&hadj);
ianwillianb 1:3da12598c635 110 if(hadj=='s') //caso seja digitado s exibe as opcoes de ajuste
ianwillianb 1:3da12598c635 111 {
ianwillianb 1:3da12598c635 112 set_time_serial();
ianwillianb 1:3da12598c635 113
ianwillianb 1:3da12598c635 114 }
ianwillianb 1:3da12598c635 115
ianwillianb 1:3da12598c635 116
ianwillianb 0:f603fa86c646 117
ianwillianb 0:f603fa86c646 118 while (true) {
ianwillianb 0:f603fa86c646 119
ianwillianb 0:f603fa86c646 120 rtc.getTime(alarm_time);
ianwillianb 0:f603fa86c646 121 led = !led;
ianwillianb 0:f603fa86c646 122 gSerial.printf("\r");
ianwillianb 0:f603fa86c646 123 gSerial.printf("Hora: %2d:%02d:%02d \n",alarm_time.hour,alarm_time.min,alarm_time.sec);
ianwillianb 0:f603fa86c646 124 wait(1);
ianwillianb 0:f603fa86c646 125 }
ianwillianb 0:f603fa86c646 126
ianwillianb 0:f603fa86c646 127
ianwillianb 0:f603fa86c646 128
ianwillianb 0:f603fa86c646 129
ianwillianb 0:f603fa86c646 130
ianwillianb 0:f603fa86c646 131 }