trabajo de creacion de un despertador por i2c configurado por un encoder

Dependencies:   DebouncedIn QEI RTC-DS1307 mbed

Fork of I2C by German Camilo Diaz Mejia

Revision:
3:9e5e0b8d0e2a
Parent:
2:3be003301107
Child:
4:fa48e78e9bba
--- a/main.cpp	Wed Jun 26 21:07:13 2013 +0000
+++ b/main.cpp	Fri Nov 20 03:48:18 2015 +0000
@@ -1,19 +1,38 @@
 #include "mbed.h"
 #include "Rtc_Ds1307.h"
+#include "DebouncedIn.h" 
+#include "QEI.h"
 
 //RtcCls rtc(p28, p27, p29, true);
-Rtc_Ds1307 rtc(p28, p27);
+Rtc_Ds1307 rtc(PTE0,PTE1);
 
 Serial pc(USBTX, USBRX, "pc");
 
+//Configuracion encoder
+QEI wheel (PTD5, PTD0, NC, 100);
+
+//Botones
+DebouncedIn BotonENC(PTA13);  //Boton Encoder
+
+DebouncedIn Boton2(PTE20);  //Boton para confirmar
+
+//salida led
+DigitalOut myled(PTE21);
+int dia=0,mes=0,ano=2015,hh=0,mm=0,ss=0;
+
 char buffer[128];
 int readptr = 0;
+int start=0;
+int Config;
 
 int main() {
     char c;
     Rtc_Ds1307::Time_rtc tm = {};
     
     while(1) {
+        
+        set_reloj:
+       
         pc.printf("*************************************\n");
         pc.printf("* Menu for RTC Test :               *\n");
         pc.printf("* read  - reads the clock           *\n");
@@ -59,8 +78,12 @@
         }
         else if (strncmp(buffer, "start", 5) == 0) {
             //  start
-            pc.printf("Performing start operation\n");
+            readptr = 0;
+            pc.printf("Dispositivo Iniciado\n");
             rtc.startClock();
+            goto set_prev;
+            
+            
         }
         else if (strncmp(buffer, "stop", 4) == 0) {
             //  stop
@@ -82,6 +105,205 @@
             pc.printf("syntax error\n");
         }
         readptr = 0;
-        pc.printf("\n\n\n");
+        //pc.printf("\n\n\n");
+        }
+        ///////////////////////////////////////////////////////////////7
+        set_prev:
+        pc.printf("Dispositivo Configurado ? ---- 1 = No   2 = Si\n");
+        pc.scanf("%d", &Config);
+        
+        if (Config ==1){
+            readptr = 0;
+            goto set_reloj;
+            }
+        
+                  
+        else if (Config ==2){
+             
+             pc.printf("Dispositivo Configurado Correctamente  Espere ....\n");
+             wait(5);
+             goto set_alarma;
+        }
+        else {
+            pc.printf("syntax error\n");
+            goto set_prev;
+        }
+        /////////////////////////////////7
+        
+        set_alarma:
+        pc.printf("******* Configuracion Alarma *******\n");
+        
+        
+        set_dia:
+        while(1){
+         
+            dia=dia+wheel.getPulses();
+            wheel.reset();
+            ////////////////////////////////////////7 
+     
+            if(dia>=31){
+                dia=31;
+            }
+            else if (dia<=0){
+             dia=0;
+            }
+     pc.printf(">Dia=%d    Mes=%d    Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    
+    if(BotonENC.falling()){
+        goto set_mes;
+        }
+    if(Boton2.falling()){
+        goto set_comp;
+    }
+           
+            
+    }
+        
+        set_mes:
+        while(1){
+         
+            mes=mes+wheel.getPulses();
+            wheel.reset();
+            ////////////////////////////////////////7 
+     
+            if(mes>=12){
+                mes=12;
+            }
+            else if (mes<=0){
+             mes=0;
+            }
+     pc.printf(" Dia=%d   >Mes=%d    Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    
+    if(BotonENC.falling()){
+        goto set_ano;
+    }
+    if(Boton2.falling()){
+        goto set_comp;
+    }
+           
+            
+    }
+     set_ano:
+        while(1){
+         
+            ano=ano+wheel.getPulses();
+            wheel.reset();
+            ////////////////////////////////////////7 
+     
+            if(ano>=3000){
+                ano=3000;
+            }
+            else if (ano<=2015){
+             ano=2015;
+            }
+     pc.printf(" Dia=%d    Mes=%d   >Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    
+    if(BotonENC.falling()){
+        goto set_hh;
     }
-}
+    if(Boton2.falling()){
+        goto set_comp;
+    }       
+            
+    }
+    set_hh:
+        while(1){
+         
+            hh=hh+wheel.getPulses();
+            wheel.reset();
+            ////////////////////////////////////////7 
+     
+            if(hh>=23){
+                hh=23;
+            }
+            else if (hh<=0){
+             hh=0;
+            }
+     pc.printf(" Dia=%d    Mes=%d    Ano=%d   >Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    
+    if(BotonENC.falling()){
+        goto set_mm;
+    }
+    if(Boton2.falling()){
+        goto set_comp;
+    }
+           
+            
+    }
+     set_mm:
+        while(1){
+         
+            mm=mm+wheel.getPulses();
+            wheel.reset();
+            ////////////////////////////////////////7 
+     
+            if(mm>=59){
+                mm=59;
+            }
+            else if (hh<=0){
+             mm=0;
+            }
+     pc.printf(" Dia=%d    Mes=%d    Ano=%d    Hora=%d   >Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    
+    if(BotonENC.falling()){
+        goto set_ss;
+    }
+    if(Boton2.falling()){
+        goto set_comp;
+    }
+           
+            
+    }
+    
+    set_ss:
+        while(1){
+         
+            ss=ss+wheel.getPulses();
+            wheel.reset();
+            ////////////////////////////////////////7 
+     
+            if(ss>=59){
+                ss=59;
+            }
+            else if (ss<=0){
+             ss=0;
+            }
+     pc.printf(" Dia=%d    Mes=%d    Ano=%d    Hora=%d    Min=%d   >Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    
+    if(BotonENC.falling()){
+        goto set_dia;
+    }
+    if(Boton2.falling()){
+        goto set_comp;
+    }
+           
+            
+    }
+    
+    set_comp:
+    
+    pc.printf("***Datos ingresados para alarma****\n");
+    pc.printf(" Dia=%d    Mes=%d    Ano=%d    Hora=%d    Min=%d    Seg=%d\n ",dia,mes,ano,hh,mm,ss);
+    
+    while(1){
+       rtc.getTime(tm);
+       pc.printf("The current time is : %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
+       pc.printf("The current date is : %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year);
+    if(tm.hour>=hh && tm.min>=mm &&tm.sec>=ss && tm.mon>=mes && tm.date>=dia && tm.year>=ano){
+        myled=0;
+    }
+    else {
+        myled=1;
+        }
+        wait(1);
+    }
+     ////   
+     
+    }
+