Alarm v1.0

Dependencies:   Debounced RTC-DS1307 mbed

Fork of Rtc_Ds1307_Sample by Henry Leinen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Programa tomado y adaptado de Henry Leinen*/
00002 /*Algunas modificaciones se realizan con propósitos educativos*/
00003 #include "mbed.h"
00004 #include "Rtc_Ds1307.h"
00005 #include "DebouncedIn.h"
00006 
00007 //RtcCls rtc(p28, p27, p29, true);
00008 Rtc_Ds1307 rtc(PTE0, PTE1);
00009 
00010 /****************Configurar Salidas********************/
00011 Serial pc(USBTX, USBRX, "pc");
00012 PwmOut led1(LED1);
00013 PwmOut led2(LED2);
00014 
00015 DebouncedIn Stop(PTE2);
00016 DebouncedIn CLK(PTE3);
00017 DebouncedIn DT(PTE4);
00018 DebouncedIn SW1(PTE5);
00019 DebouncedIn Snooze(PTB11);
00020 /*****************Definir variables********************/
00021 char buffer[128];
00022 int readptr = 0;
00023 int started=0;
00024 int wakeup=0;
00025 //int snooze=0;
00026 int howmanytimes = 0;
00027 //Alarm variables;
00028 int hour1;
00029 int min1;
00030 int sec1;
00031 
00032 /*******************Rutinas********************/
00033 
00034 void Encoder()
00035 {
00036     int NewClk;
00037     int OldClk;
00038     int NewDt;
00039     int OldDt;
00040     int NewPos;
00041     int OldPos;
00042     int counter2=0;
00043     pc.printf("Setting Alarm...\n\r") ;
00044     pc.printf("The current alarm settings : %02d:%02d\n", hour1,min1);
00045     pc.printf("Hour:\n\r") ;
00046        
00047     while(howmanytimes != 0)
00048     {           
00049         NewClk = CLK.read();
00050         NewDt=DT.read();
00051         NewPos = 2*NewClk + NewDt;
00052         if (NewClk != OldClk || NewDt != OldDt)
00053         {   
00054             counter2++;         
00055             if ((OldPos == 0 && NewPos == 2) ||(OldPos == 2 && NewPos == 3) || (OldPos == 3 && NewPos == 1) || (OldPos == 1 && NewPos == 0)) //Incremento;
00056             {
00057                 
00058                 if (howmanytimes ==1 && counter2 ==2){hour1++; if(hour1>23){hour1=0;}};
00059                 if (howmanytimes ==2 && counter2 ==2){min1++; if(min1>59){min1=0;}};
00060                 
00061                 printf("%d : %d\n", hour1, min1);
00062             }
00063             else if ((OldPos == 0 && NewPos == 1) ||(OldPos == 1 && NewPos == 3) || (OldPos == 3 && NewPos == 2) || (OldPos == 2 && NewPos == 0))
00064             {
00065                 if (howmanytimes ==1 && counter2 ==2 ){hour1--; if(hour1<0){hour1=23;}};
00066                 if (howmanytimes ==2 && counter2 ==2){min1--; if(min1<0){min1=59;}};
00067                 printf("%d : %d\n", hour1, min1);
00068             }         
00069              
00070             if (counter2==2){counter2=0;}
00071         }
00072         OldClk=NewClk;
00073         OldDt=NewDt;
00074         OldPos = NewPos;
00075         
00076         if(SW1.falling())
00077         {
00078             howmanytimes++;
00079             if(howmanytimes==2){pc.printf("Minutes...\n");}
00080             if(howmanytimes > 2) {howmanytimes=0;}
00081         }
00082     }
00083 }
00084 
00085 
00086 //Main
00087 int main() {
00088     int c;
00089     int counter1=0;
00090     
00091     led1.period(2.0);
00092     led1.write(1.0);
00093     led2.period(2.0);
00094     led2.write(1.0);
00095     
00096     Rtc_Ds1307::Time_rtc tm = {}; //Se da un nombre a la estructura usada para almacenar la información del reloj
00097     
00098     
00099     /*Inicializar el reloj*/
00100     pc.printf("************************\n Be Welcome! \n ************************\n");    
00101                 if (rtc.getTime(tm) )                     {
00102                         pc.printf("The current time is : %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
00103                         pc.printf("The current date is :  %02d/%02d/%04d\n",  tm.mon, tm.date, tm.year);
00104                         
00105                     }
00106     
00107    while(started==0)
00108   
00109    {  pc.printf("Is it right? yes or not?\n");
00110         while( (c = pc.getc()) != '\n') 
00111             {
00112                 buffer[readptr++] = c;
00113             }
00114                     
00115         buffer[readptr++] = 0; //Almaceno un cero en la posición después del cambio de línea;
00116         
00117         if (strncmp(buffer, "yes", 3) == 0) //Date and time are ok, lets start!
00118         {
00119             pc.printf("Lets start...\n");
00120             rtc.startClock();            
00121             started=1; 
00122         }
00123         
00124         else if (strncmp(buffer, "not", 3) == 0)
00125         {
00126                             //  perform write
00127                 pc.printf("Enter the date (date 0..31)");
00128                 pc.scanf("%d", &tm.date);
00129                 pc.printf("Enter the date (month 1..12)");
00130                 pc.scanf("%d", &tm.mon);
00131                 pc.printf("Enter the date (year)");
00132                 pc.scanf("%d", &tm.year);
00133                 pc.printf("Enter the time (hours 0..23)");
00134                 pc.scanf("%d", &tm.hour);
00135                 pc.printf("Enter the time (minutes 0..59)");
00136                 pc.scanf("%d", &tm.min);
00137                 pc.printf("Enter the time (seconds 0..59)");
00138                 pc.scanf("%d", &tm.sec);
00139                 pc.printf("Performing write operation\n");
00140             
00141                 while(pc.readable()) 
00142                     pc.getc();
00143                 rtc.setTime(tm, false, false);
00144                 rtc.startClock(); 
00145                 started=1;         
00146         }
00147         
00148         else 
00149         {
00150             pc.printf("Try again \n");
00151             readptr=0;
00152         }
00153     pc.printf("\n");
00154     }//while start
00155    
00156    //Esto es temporal
00157    if (tm.min==59){   hour1 = tm.hour+1;   min1=0;}
00158    else {hour1 = tm.hour;   min1=tm.min+1;}
00159    
00160     
00161     /*Ciclo infinito para correr el programa*/
00162     while(1)    
00163     {
00164         wait(0.01);
00165        counter1++; //Aumento el contador 
00166  
00167  /*******Get the time*********/
00168         if (counter1==90) //Cuento 900ms para mostrar algo en pantalla;
00169             {
00170                     counter1=0;        
00171                     if (rtc.getTime(tm) )
00172                     {
00173                             pc.printf("The current time is : %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
00174                             pc.printf("The current date is : %02d/%02d/%04d\n",  tm.mon, tm.date, tm.year);
00175                         if (hour1==tm.hour && min1==tm.min && sec1 == tm.sec)
00176                         {
00177                             wakeup=1;
00178                             pc.printf("Wakeup bitch!..\n");
00179                             led1.write(0.5);
00180                             led2.write(1.0);
00181                         }        
00182                         }  //if rtc
00183                     }    //if counter  
00184     
00185     
00186     if (SW1.falling() && wakeup != 1)
00187     {
00188         led2.write(0.5);   
00189         howmanytimes++;
00190         Encoder();
00191         led2.write(1.0);  
00192     }
00193     
00194     if (Stop.rising()&& wakeup==1)
00195     {
00196         pc.printf("Alarm Stopped ... \n");
00197         led1.write(1.0);
00198         wakeup=0;
00199     }
00200         if (Snooze.rising() && wakeup==1)//In case of pushing snooze;
00201     {
00202         pc.printf("Two more minutes please!!!!\n");
00203         led1.write(1.0);
00204         wakeup=0;
00205        
00206         if (tm.min==58){min1=0;hour1=hour1+1;}
00207         else if (tm.min==59){min1=1;hour1=hour1+1;}
00208         else { min1=tm.min+1;} //two more minutes please}
00209         pc.printf("Alarm will sound again at: %02d:%02d:%02d\n", hour1, min1,sec1);
00210         
00211     }
00212     }//while
00213    
00214 }//main
00215 
00216 
00217 
00218