Si-Labs EFM32 Back Up domain and BUrtc demo.
Dependencies: EFM32_BUrtc EFM32_CapSenseSlider EFM32_SegmentLCD mbed
main.cpp@1:1b7650724a0f, 2016-05-16 (annotated)
- Committer:
- star297
- Date:
- Mon May 16 23:15:16 2016 +0000
- Revision:
- 1:1b7650724a0f
- Parent:
- 0:dc30d5f06316
Rev 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:dc30d5f06316 | 1 | |
star297 | 0:dc30d5f06316 | 2 | #include "mbed.h" |
star297 | 0:dc30d5f06316 | 3 | #include "EFM32_SegmentLCD.h" |
star297 | 0:dc30d5f06316 | 4 | #include "EFM32_CapSenseSlider.h" |
star297 | 0:dc30d5f06316 | 5 | #include "BUrtc.h" |
star297 | 0:dc30d5f06316 | 6 | |
star297 | 0:dc30d5f06316 | 7 | silabs::EFM32_SegmentLCD segmentDisplay; |
star297 | 0:dc30d5f06316 | 8 | silabs::EFM32_CapSenseSlider capSlider; |
star297 | 0:dc30d5f06316 | 9 | |
star297 | 0:dc30d5f06316 | 10 | InterruptIn MinMonth(SW0); |
star297 | 0:dc30d5f06316 | 11 | InterruptIn HourDate(SW1); |
star297 | 0:dc30d5f06316 | 12 | |
star297 | 0:dc30d5f06316 | 13 | DigitalOut led1(LED1); |
star297 | 0:dc30d5f06316 | 14 | DigitalOut led2(LED2); |
star297 | 0:dc30d5f06316 | 15 | |
star297 | 0:dc30d5f06316 | 16 | char timebuff[20], lcdTime[8]; |
star297 | 0:dc30d5f06316 | 17 | int initialized = false; |
star297 | 0:dc30d5f06316 | 18 | int month,date,lcdDate,slider; |
star297 | 0:dc30d5f06316 | 19 | |
star297 | 0:dc30d5f06316 | 20 | struct tm t; |
star297 | 0:dc30d5f06316 | 21 | |
star297 | 0:dc30d5f06316 | 22 | void displayRefresh(),timeSet(),dateSet(); |
star297 | 0:dc30d5f06316 | 23 | |
star297 | 0:dc30d5f06316 | 24 | int main() |
star297 | 0:dc30d5f06316 | 25 | { |
star297 | 0:dc30d5f06316 | 26 | // set up BU domain and BUrtc if not started, else restore system RTC from BUrtc |
star297 | 0:dc30d5f06316 | 27 | BUrtcInit(); |
star297 | 0:dc30d5f06316 | 28 | |
star297 | 0:dc30d5f06316 | 29 | // set the reset time |
star297 | 0:dc30d5f06316 | 30 | t.tm_sec = 0; // 0-59 |
star297 | 0:dc30d5f06316 | 31 | t.tm_min = 0; // 0-59 |
star297 | 0:dc30d5f06316 | 32 | t.tm_hour = 0; // 0-23 |
star297 | 0:dc30d5f06316 | 33 | t.tm_mday = 17; // 1-31 |
star297 | 0:dc30d5f06316 | 34 | t.tm_mon = 4; // 0-11 |
star297 | 0:dc30d5f06316 | 35 | t.tm_year = 116; // years since 1900 |
star297 | 0:dc30d5f06316 | 36 | |
star297 | 0:dc30d5f06316 | 37 | // time setting button interrupts |
star297 | 0:dc30d5f06316 | 38 | HourDate.fall(&timeSet); |
star297 | 0:dc30d5f06316 | 39 | MinMonth.fall(&timeSet); |
star297 | 0:dc30d5f06316 | 40 | capSlider.start(); |
star297 | 0:dc30d5f06316 | 41 | capSlider.attach_slide(-1, dateSet); |
star297 | 0:dc30d5f06316 | 42 | |
star297 | 0:dc30d5f06316 | 43 | while(1) { |
star297 | 0:dc30d5f06316 | 44 | |
star297 | 0:dc30d5f06316 | 45 | // Sleep while waiting for interrupt |
star297 | 0:dc30d5f06316 | 46 | EMU_EnterEM2(true); |
star297 | 0:dc30d5f06316 | 47 | // Run display update code on wake up |
star297 | 0:dc30d5f06316 | 48 | displayRefresh(); |
star297 | 0:dc30d5f06316 | 49 | } |
star297 | 0:dc30d5f06316 | 50 | } |
star297 | 0:dc30d5f06316 | 51 | |
star297 | 0:dc30d5f06316 | 52 | void displayRefresh() |
star297 | 0:dc30d5f06316 | 53 | { |
star297 | 0:dc30d5f06316 | 54 | if (!initialized) { |
star297 | 0:dc30d5f06316 | 55 | segmentDisplay.Symbol(LCD_SYMBOL_COL3,true); |
star297 | 0:dc30d5f06316 | 56 | segmentDisplay.Symbol(LCD_SYMBOL_COL5,true); |
star297 | 0:dc30d5f06316 | 57 | SegmentLCD_Symbol(LCD_SYMBOL_DP10, true); |
star297 | 0:dc30d5f06316 | 58 | // set time if RTC is reset |
star297 | 0:dc30d5f06316 | 59 | if(time(NULL)<10) { |
star297 | 0:dc30d5f06316 | 60 | RTCset(mktime(&t)); |
star297 | 0:dc30d5f06316 | 61 | } |
star297 | 0:dc30d5f06316 | 62 | initialized = true; |
star297 | 0:dc30d5f06316 | 63 | } |
star297 | 0:dc30d5f06316 | 64 | time_t seconds = time(NULL); |
star297 | 0:dc30d5f06316 | 65 | strftime(timebuff, 2,"%d", localtime(&seconds)); |
star297 | 0:dc30d5f06316 | 66 | date = atoi(timebuff); |
star297 | 0:dc30d5f06316 | 67 | strftime(timebuff, 2,"%m", localtime(&seconds)); |
star297 | 0:dc30d5f06316 | 68 | month = atoi(timebuff); |
star297 | 0:dc30d5f06316 | 69 | lcdDate = ((date %31)* 100 + (month % 12)); |
star297 | 0:dc30d5f06316 | 70 | strftime(lcdTime,8,"%H%M%S", localtime(&seconds)); |
star297 | 0:dc30d5f06316 | 71 | |
star297 | 0:dc30d5f06316 | 72 | SegmentLCD_Write(lcdTime); |
star297 | 0:dc30d5f06316 | 73 | segmentDisplay.Number(lcdDate); |
star297 | 0:dc30d5f06316 | 74 | segmentDisplay.Symbol(LCD_SYMBOL_EFM32, seconds & 0x1); |
star297 | 0:dc30d5f06316 | 75 | led1=0;led2=0; |
star297 | 0:dc30d5f06316 | 76 | } |
star297 | 0:dc30d5f06316 | 77 | |
star297 | 0:dc30d5f06316 | 78 | void dateSet() { |
star297 | 0:dc30d5f06316 | 79 | |
star297 | 0:dc30d5f06316 | 80 | // set date by tapping slider, slow and fast settings |
star297 | 0:dc30d5f06316 | 81 | slider = capSlider.get_position(); |
star297 | 0:dc30d5f06316 | 82 | led2=!led2; |
star297 | 0:dc30d5f06316 | 83 | time_t seconds = time(NULL); |
star297 | 0:dc30d5f06316 | 84 | seconds=seconds + (86400 * (slider/10)); |
star297 | 0:dc30d5f06316 | 85 | RTCset(seconds); |
star297 | 0:dc30d5f06316 | 86 | } |
star297 | 0:dc30d5f06316 | 87 | |
star297 | 0:dc30d5f06316 | 88 | void timeSet() |
star297 | 0:dc30d5f06316 | 89 | { |
star297 | 0:dc30d5f06316 | 90 | // set hours and minutes with buttons, set date first, can't go backwards |
star297 | 0:dc30d5f06316 | 91 | led1=!led1; |
star297 | 0:dc30d5f06316 | 92 | time_t seconds = time(NULL); |
star297 | 0:dc30d5f06316 | 93 | if(MinMonth){seconds=seconds+60;} |
star297 | 0:dc30d5f06316 | 94 | if(HourDate){seconds=seconds+3600;} |
star297 | 0:dc30d5f06316 | 95 | RTCset(seconds); |
star297 | 0:dc30d5f06316 | 96 | } |
star297 | 0:dc30d5f06316 | 97 |