Projekt-Alarmclock

Dependencies:   LCD RTC8563 Wecker mbed

Revision:
2:d3708e92660f
Parent:
1:b71a6c225c2b
--- a/main.cpp	Fri May 29 12:01:00 2015 +0000
+++ b/main.cpp	Thu Jun 25 07:39:17 2015 +0000
@@ -6,11 +6,12 @@
 
 Wecker wecker;
 
-void btnStdr();
-void btnMinr();
+void btnStdf();
+void btnMinf();
 void btnSlpr();
 void btnSlpf();
 void tickControll();
+void blink();
 
 typedef enum {Normal, setAlarm, AlarmOn, Alarm, Sleep} state;
 state Mode=Normal;
@@ -19,17 +20,31 @@
 Ticker tick1;
 Timer tim1,tim2,tim3;
 
-InterruptIn swStd(P0_15);
-InterruptIn swMin(P0_23);
-InterruptIn swSlp(P0_10);
+InterruptIn swStd(p22);
+InterruptIn swMin(p23);
+InterruptIn swSlp(p21);
+DigitalOut alarmOut(p24,0);
 DigitalIn alarm(P1_28);
 DigitalOut myled(LED1);
 
+
+const int SlpAlTime = (60)*5;
+
 int main()
 {
+    swMin.mode(PullUp);
+    swStd.mode(PullUp);
+    swSlp.mode(PullUp);
+    alarmOut=0;
+    while(swSlp==1);
+    alarmOut=0;
+    wait(1);
     wecker.rtc_init();
-    swStd.rise(&btnStdr);
-    swMin.rise(&btnMinr);
+    swMin.mode(PullUp);
+    swStd.mode(PullUp);
+    swSlp.mode(PullUp);
+    swStd.fall(&btnStdf);
+    swMin.fall(&btnMinf);
     swSlp.rise(&btnSlpr);
     swSlp.fall(&btnSlpf);
     tick1.attach(&tickControll,0.01);
@@ -42,15 +57,14 @@
 }
 
 
-
-void btnSlpr()
+void btnSlpf()
 {
     wait_ms(20);
     switch (Mode) {
         case Normal:
             if(tim1.read()==0)
                 tim1.start();
-            if (swStd==1 || swMin==1)
+            if (swStd==0 || swMin==0)
                 Mode = setAlarm;
             break;
         case AlarmOn:
@@ -63,17 +77,19 @@
             break;
         case Alarm:
             Mode=Sleep;
+            tim3.stop();
+            tim3.reset();
             break;
     }
 }
 
-void btnStdr()
+void btnStdf()
 {
     wait_ms(20);
     switch(Mode) {
         case Normal:
             int nextStd = wecker.unbcd(wecker.rtc_read(HOURS));
-            if (swSlp != 1) {
+            if (swSlp != 0) {
                 if (nextStd>=23)
                     nextStd=0;
                 else
@@ -99,13 +115,13 @@
     }
 }
 
-void btnMinr()
+void btnMinf()
 {
     wait_ms(20);
     switch(Mode) {
         case Normal:
             int nextMin = wecker.unbcd(wecker.rtc_read(MINUTES));
-            if (swSlp != 1) {
+            if (swSlp != 0) {
                 if (nextMin>=59)
                     nextMin=0;
                 else
@@ -136,10 +152,11 @@
     switch(Mode) {
         case Normal:
             strcpy(sMode,"No");
+            
             break;
         case setAlarm:
             strcpy(sMode,"sA");
-            if(swStd==0 && swMin==0)
+            if(swStd==1 && swMin==1)
                 tim2.start();
             if(tim2.read()>=5.0) {
                 Mode=Normal;
@@ -151,62 +168,67 @@
             strcpy(sMode,"AO");
             if(alarm==0)
                 Mode=Alarm;
-            break;
-        case Sleep:
-            if(tim3.read()==0)
-                tim3.start();
-            if(tim3.read()>=300) {
-                Mode=Alarm;
-                tim3.stop();
-                tim3.reset();
-            }
+            //else if((wecker.rtc_read(MINUTE_ALARM) == wecker.rtc_read(MINUTES)) && (wecker.rtc_read(HOUR_ALARM)==wecker.rtc_read(HOURS)))
+                   // Mode=Alarm;
+
+                    break;
+                case Sleep:
+                        if(tim3.read()==0)
+                            tim3.start();
+                    if(tim3.read()>=SlpAlTime) {
+                        Mode=Alarm;
+                        tim3.stop();
+                            tim3.reset();
+                        }
             strcpy(sMode,"Sl");
-            myled=0;
+            alarmOut=0;
             break;
         case Alarm:
-            if(tim3.read()==0)
+                if(tim3.read()==0)
                 tim3.start();
-            if(tim3.read()>=300) {
-                Mode=Sleep;
-                tim3.stop();
-                tim3.reset();
-            }
+                if(tim3.read()>=SlpAlTime) {
+                    Mode=Sleep;
+                    tim3.stop();
+                        tim3.reset();
+                    }
             strcpy(sMode,"Al");
-            myled=1;
+            alarmOut=1;
             break;
     }
 }
 
-void btnSlpf()
+void btnSlpr()
 {
     switch(Mode) {
         case Normal:
-            if (swStd==1 || swMin==1)
+            if (swStd==0 || swMin==0)
                 Mode = setAlarm;
             if(tim1.read()!=0) {
                 tim1.stop();
-                if (tim1.read()>=3.0)
+                if (tim1.read()>=3.0) {
                     Mode = AlarmOn;
-                wecker.rtc_write(CONTROL2, 0x02);
-                tim1.reset();
-                break;
-            case AlarmOn:
-                tim1.stop();
-                if (tim1.read()>=3.0)
-                    Mode = Normal;
-                wecker.rtc_write(CONTROL2, 0);
+                    wecker.rtc_write(CONTROL2, 0x02);
+                }
                 tim1.reset();
-                break;
-            case Sleep:
-                tim1.stop();
-                if (tim1.read()>=3.0) {
-                    Mode = Normal;
-                    tim3.stop();
-                    tim3.reset();
-                }
+            }
+            break;
+        case AlarmOn:
+            tim1.stop();
+            if (tim1.read()>=3.0) {
+                Mode = Normal;
                 wecker.rtc_write(CONTROL2, 0);
-                tim1.reset();
-                break;
             }
+            tim1.reset();
+            break;
+        case Sleep:
+            tim1.stop();
+            if (tim1.read()>=3.0) {
+                Mode = Normal;
+                tim3.stop();
+                tim3.reset();
+            }
+            wecker.rtc_write(CONTROL2, 0);
+            tim1.reset();
+            break;
     }
 }
\ No newline at end of file