example using DS3234 RTC connected to BBC micro:bit using SPI

Dependencies:   microbit

Files at this revision

API Documentation at this revision

Comitter:
jsa1969
Date:
Fri May 11 12:34:40 2018 +0000
Parent:
0:d5fb8cef7c31
Commit message:
set time functionality

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d5fb8cef7c31 -r c740bf7f5c59 main.cpp
--- a/main.cpp	Fri May 11 09:37:45 2018 +0000
+++ b/main.cpp	Fri May 11 12:34:40 2018 +0000
@@ -29,6 +29,11 @@
 MicroBit uBit;
 DS3234 rtcDS3234;
 
+int mode = 0;
+bool displaying = false;
+bool interruptLoop = false;
+uint8_t sec, minute, hour, day, date, month, year;
+
 void displayTime(){
     rtcDS3234.update();
     uBit.display.scroll(rtcDS3234.hour());
@@ -37,17 +42,117 @@
     uBit.display.scroll(":");
     uBit.display.scroll(rtcDS3234.second());
 }
+
+void displayLoop(){
+    if (!displaying) {
+        while (!interruptLoop) {
+            displaying = true;
+            displayTime();
+            uBit.sleep(60000);
+        }
+        interruptLoop = false;
+    }
+}
  
 void onButtonB(MicroBitEvent e)
 {
-    displayTime();
+    switch(mode) {
+        case 0:
+            displayTime();
+            break;
+        case 1:
+            ++year;
+            break;
+        case 2:
+            ++month;
+            break;
+        case 3:
+            ++date;
+            break;
+        case 4:
+            ++day;
+            break;
+        case 5:
+            ++hour;
+            break;
+        case 6:
+            ++minute;
+            break;
+    }
 }
 
 void onButtonA(MicroBitEvent e)
 {
-    while (true) {
-        displayTime();
-        uBit.sleep(60000);
+    switch(mode) {
+        case 0:
+            displayLoop();
+            break;
+        case 1:
+            --year;
+            break;
+        case 2:
+            --month;
+            break;
+        case 3:
+            --date;
+            break;
+        case 4:
+            --day;
+            break;
+        case 5:
+            --hour;
+            break;
+        case 6:
+            --minute;
+            break;
+    }
+}
+
+void onButtonAB(MicroBitEvent e)
+{
+    switch(++mode) {
+        case 1:
+            rtcDS3234.update();
+            uBit.display.scroll("y");
+            year = rtcDS3234.year();
+            uBit.display.scroll(year);
+            break;
+        case 2:
+            uBit.display.scroll("m");
+            month = rtcDS3234.month();
+            uBit.display.scroll(month);
+            break;
+        case 3:
+            uBit.display.scroll("d");
+            date = rtcDS3234.date();
+            uBit.display.scroll(date);
+            break;
+        case 4:
+            uBit.display.scroll("wd");
+            day = rtcDS3234.day();
+            uBit.display.scroll(day);
+            break;
+        case 5:
+            uBit.display.scroll("h");
+            hour = rtcDS3234.hour();
+            uBit.display.scroll(hour);
+            break;
+        case 6:
+            uBit.display.scroll("m");
+            minute = rtcDS3234.minute();
+            uBit.display.scroll(minute);
+            break;
+        default:
+            mode = 0;
+            sec = 0;
+            rtcDS3234.setTime(sec, minute, hour, day, date, month, year);
+            uBit.display.scroll(date);
+            uBit.display.scroll(".");
+            uBit.display.scroll(month);
+            uBit.display.scroll(".");
+            uBit.display.scroll(year);
+            uBit.display.scroll(" ");
+            displayTime();
     }
 }
 
@@ -62,11 +167,10 @@
     rtcDS3234.begin(&uBit.io.P16, &uBit);
     uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
     uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
-    uBit.display.scroll("rdy");
+    uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
+    
+    displayTime();
 
-    // If main exits, there may still be other fibers running or registered event handlers etc.
-    // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
-    // sit in the idle task forever, in a power efficient sleep.
     release_fiber();
 }