OLED clock using the builtin RTC of the STM32F446RE
Dependencies: mbed OLED_SSD1306_SH1106
main.cpp@0:696ab398b667, 2022-02-03 (annotated)
- Committer:
- cspista
- Date:
- Thu Feb 03 14:54:24 2022 +0000
- Revision:
- 0:696ab398b667
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cspista | 0:696ab398b667 | 1 | #include "mbed.h" |
cspista | 0:696ab398b667 | 2 | #include "Adafruit_SSD1306.h" |
cspista | 0:696ab398b667 | 3 | |
cspista | 0:696ab398b667 | 4 | I2C i2c(D14,D15); |
cspista | 0:696ab398b667 | 5 | Adafruit_SH1106_I2c oled(i2c, NC, 0x78, 64, 128); // SH1106 I2C 128x64, with no reset pin |
cspista | 0:696ab398b667 | 6 | Serial pc(USBTX,USBRX); //UART0 via OpenSDA |
cspista | 0:696ab398b667 | 7 | DigitalOut myled(LED1); |
cspista | 0:696ab398b667 | 8 | Ticker myticker; |
cspista | 0:696ab398b667 | 9 | static char msg[20]; // character buffer |
cspista | 0:696ab398b667 | 10 | volatile uint8_t myflag = 0; |
cspista | 0:696ab398b667 | 11 | #define DATE_20200222_222222 1582377742 // 2020/2/22 22:22:22 |
cspista | 0:696ab398b667 | 12 | |
cspista | 0:696ab398b667 | 13 | void processSerialCommand(); |
cspista | 0:696ab398b667 | 14 | |
cspista | 0:696ab398b667 | 15 | void setflag(void) |
cspista | 0:696ab398b667 | 16 | { |
cspista | 0:696ab398b667 | 17 | myflag = 1; |
cspista | 0:696ab398b667 | 18 | } |
cspista | 0:696ab398b667 | 19 | |
cspista | 0:696ab398b667 | 20 | int main() |
cspista | 0:696ab398b667 | 21 | { |
cspista | 0:696ab398b667 | 22 | pc.baud(115200); |
cspista | 0:696ab398b667 | 23 | time_t seconds; |
cspista | 0:696ab398b667 | 24 | seconds = time(NULL); |
cspista | 0:696ab398b667 | 25 | if (seconds < DATE_20200222_222222) { |
cspista | 0:696ab398b667 | 26 | set_time(DATE_20200222_222222); |
cspista | 0:696ab398b667 | 27 | } |
cspista | 0:696ab398b667 | 28 | i2c.frequency(400000); |
cspista | 0:696ab398b667 | 29 | oled.setRotation(0); |
cspista | 0:696ab398b667 | 30 | oled.clearDisplay(); |
cspista | 0:696ab398b667 | 31 | oled.setTextColor(1); |
cspista | 0:696ab398b667 | 32 | oled.setTextSize(1); |
cspista | 0:696ab398b667 | 33 | oled.setTextCursor(10,8); |
cspista | 0:696ab398b667 | 34 | oled.display(); |
cspista | 0:696ab398b667 | 35 | myticker.attach(&setflag,1); |
cspista | 0:696ab398b667 | 36 | while(1) { |
cspista | 0:696ab398b667 | 37 | if(pc.readable()) { |
cspista | 0:696ab398b667 | 38 | processSerialCommand(); |
cspista | 0:696ab398b667 | 39 | } |
cspista | 0:696ab398b667 | 40 | if(myflag) { |
cspista | 0:696ab398b667 | 41 | seconds = time(NULL); |
cspista | 0:696ab398b667 | 42 | oled.clearDisplay(); |
cspista | 0:696ab398b667 | 43 | oled.setTextSize(1); |
cspista | 0:696ab398b667 | 44 | oled.setTextCursor(14,1); |
cspista | 0:696ab398b667 | 45 | strftime (msg, 15, "%Y-%b-%d %a ", localtime(&seconds)); // Display date |
cspista | 0:696ab398b667 | 46 | oled.printf("%s",msg); |
cspista | 0:696ab398b667 | 47 | strftime (msg, 10, "%T ", localtime(&seconds)); // Display time |
cspista | 0:696ab398b667 | 48 | oled.setTextSize(2); |
cspista | 0:696ab398b667 | 49 | oled.setTextCursor(10,12); |
cspista | 0:696ab398b667 | 50 | oled.printf("%s",msg); |
cspista | 0:696ab398b667 | 51 | oled.display(); |
cspista | 0:696ab398b667 | 52 | pc.printf("RTC time: %s\r\n",ctime(&seconds)); |
cspista | 0:696ab398b667 | 53 | myflag = 0; |
cspista | 0:696ab398b667 | 54 | } |
cspista | 0:696ab398b667 | 55 | } |
cspista | 0:696ab398b667 | 56 | } |
cspista | 0:696ab398b667 | 57 | |
cspista | 0:696ab398b667 | 58 | void processSerialCommand() |
cspista | 0:696ab398b667 | 59 | { |
cspista | 0:696ab398b667 | 60 | char c = pc.getc(); |
cspista | 0:696ab398b667 | 61 | switch(c) { |
cspista | 0:696ab398b667 | 62 | case 'T': |
cspista | 0:696ab398b667 | 63 | // Command to set RTC time |
cspista | 0:696ab398b667 | 64 | // Command format: TYYMMDDHHMMSS |
cspista | 0:696ab398b667 | 65 | // Example: 2012 Oct 21 1:23pm is T121021132300 |
cspista | 0:696ab398b667 | 66 | struct tm tme; |
cspista | 0:696ab398b667 | 67 | time_t newTime; |
cspista | 0:696ab398b667 | 68 | |
cspista | 0:696ab398b667 | 69 | // Parse incomming 12 ASCII charaters into time_t |
cspista | 0:696ab398b667 | 70 | // no error checking for numeric values in YYMDDHHMMSS fields, so be careful! |
cspista | 0:696ab398b667 | 71 | c = pc.getc(); |
cspista | 0:696ab398b667 | 72 | tme.tm_year = c - '0'; |
cspista | 0:696ab398b667 | 73 | c = pc.getc(); |
cspista | 0:696ab398b667 | 74 | tme.tm_year = 10*tme.tm_year; |
cspista | 0:696ab398b667 | 75 | tme.tm_year += c-'0'; |
cspista | 0:696ab398b667 | 76 | tme.tm_year += 100; //Years are counted from 1900! |
cspista | 0:696ab398b667 | 77 | c = pc.getc(); |
cspista | 0:696ab398b667 | 78 | tme.tm_mon = c - '0'; |
cspista | 0:696ab398b667 | 79 | c = pc.getc(); |
cspista | 0:696ab398b667 | 80 | tme.tm_mon = 10*tme.tm_mon; |
cspista | 0:696ab398b667 | 81 | tme.tm_mon += c-'0'-1; //corrected by -1 due to a stupid error |
cspista | 0:696ab398b667 | 82 | c = pc.getc(); |
cspista | 0:696ab398b667 | 83 | tme.tm_mday = c - '0'; |
cspista | 0:696ab398b667 | 84 | c = pc.getc(); |
cspista | 0:696ab398b667 | 85 | tme.tm_mday = 10*tme.tm_mday; |
cspista | 0:696ab398b667 | 86 | tme.tm_mday += c-'0'; |
cspista | 0:696ab398b667 | 87 | c = pc.getc(); |
cspista | 0:696ab398b667 | 88 | tme.tm_hour = c - '0'; |
cspista | 0:696ab398b667 | 89 | c = pc.getc(); |
cspista | 0:696ab398b667 | 90 | tme.tm_hour = 10*tme.tm_hour; |
cspista | 0:696ab398b667 | 91 | tme.tm_hour += c-'0'; |
cspista | 0:696ab398b667 | 92 | c = pc.getc(); |
cspista | 0:696ab398b667 | 93 | tme.tm_min = c - '0'; |
cspista | 0:696ab398b667 | 94 | c = pc.getc(); |
cspista | 0:696ab398b667 | 95 | tme.tm_min = 10*tme.tm_min; |
cspista | 0:696ab398b667 | 96 | tme.tm_min += c-'0'; |
cspista | 0:696ab398b667 | 97 | c = pc.getc(); |
cspista | 0:696ab398b667 | 98 | tme.tm_sec = c - '0'; |
cspista | 0:696ab398b667 | 99 | c = pc.getc(); |
cspista | 0:696ab398b667 | 100 | tme.tm_sec = 10*tme.tm_sec; |
cspista | 0:696ab398b667 | 101 | tme.tm_sec += c-'0'; |
cspista | 0:696ab398b667 | 102 | newTime = mktime(&tme); |
cspista | 0:696ab398b667 | 103 | set_time(newTime); |
cspista | 0:696ab398b667 | 104 | pc.printf("RTC set to: %s\r\n",ctime(&newTime)); |
cspista | 0:696ab398b667 | 105 | } |
cspista | 0:696ab398b667 | 106 | while(pc.readable()) { |
cspista | 0:696ab398b667 | 107 | pc.getc(); // clear serial buffer |
cspista | 0:696ab398b667 | 108 | } |
cspista | 0:696ab398b667 | 109 | } |