Simply Clock
Dependencies: EthernetNetIf NTPClient_NetServices TextLCD mbed
main.cpp@0:7e901efc73e7, 2011-03-25 (annotated)
- Committer:
- soramimi
- Date:
- Fri Mar 25 16:54:13 2011 +0000
- Revision:
- 0:7e901efc73e7
- Child:
- 1:7974d199ed39
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
soramimi | 0:7e901efc73e7 | 1 | #include "mbed.h" |
soramimi | 0:7e901efc73e7 | 2 | #include "time.h" |
soramimi | 0:7e901efc73e7 | 3 | #include "TextLCD.h" |
soramimi | 0:7e901efc73e7 | 4 | #include "EthernetNetIf.h" |
soramimi | 0:7e901efc73e7 | 5 | #include "NTPClient.h" |
soramimi | 0:7e901efc73e7 | 6 | |
soramimi | 0:7e901efc73e7 | 7 | #define TIMEZONE (9 * 60 * 60) |
soramimi | 0:7e901efc73e7 | 8 | |
soramimi | 0:7e901efc73e7 | 9 | TextLCD lcd(p21, p22, p23, p24, p25, p26); |
soramimi | 0:7e901efc73e7 | 10 | |
soramimi | 0:7e901efc73e7 | 11 | EthernetNetIf eth; |
soramimi | 0:7e901efc73e7 | 12 | NTPClient ntp; |
soramimi | 0:7e901efc73e7 | 13 | |
soramimi | 0:7e901efc73e7 | 14 | unsigned long convert_gregorian_to_julian(int year, int month, int day) |
soramimi | 0:7e901efc73e7 | 15 | { |
soramimi | 0:7e901efc73e7 | 16 | if (month < 3) { |
soramimi | 0:7e901efc73e7 | 17 | month += 9; |
soramimi | 0:7e901efc73e7 | 18 | year--; |
soramimi | 0:7e901efc73e7 | 19 | } else { |
soramimi | 0:7e901efc73e7 | 20 | month -= 3; |
soramimi | 0:7e901efc73e7 | 21 | } |
soramimi | 0:7e901efc73e7 | 22 | year += 4800; |
soramimi | 0:7e901efc73e7 | 23 | int c = year / 100; |
soramimi | 0:7e901efc73e7 | 24 | return c * 146097 / 4 + (year - c * 100) * 1461 / 4 + (153 * month + 2) / 5 + day - 32045; |
soramimi | 0:7e901efc73e7 | 25 | } |
soramimi | 0:7e901efc73e7 | 26 | |
soramimi | 0:7e901efc73e7 | 27 | void convert_julian_to_gregorian(unsigned long j, int *year, int *month, int *day) |
soramimi | 0:7e901efc73e7 | 28 | { |
soramimi | 0:7e901efc73e7 | 29 | int y, m, d; |
soramimi | 0:7e901efc73e7 | 30 | y = (j * 4 + 128179) / 146097; |
soramimi | 0:7e901efc73e7 | 31 | d = (j * 4 - y * 146097 + 128179) / 4 * 4 + 3; |
soramimi | 0:7e901efc73e7 | 32 | j = d / 1461; |
soramimi | 0:7e901efc73e7 | 33 | d = (d - j * 1461) / 4 * 5 + 2; |
soramimi | 0:7e901efc73e7 | 34 | m = d / 153; |
soramimi | 0:7e901efc73e7 | 35 | d = (d - m * 153) / 5 + 1; |
soramimi | 0:7e901efc73e7 | 36 | y = (y - 48) * 100 + j; |
soramimi | 0:7e901efc73e7 | 37 | if (m < 10) { |
soramimi | 0:7e901efc73e7 | 38 | m += 3; |
soramimi | 0:7e901efc73e7 | 39 | } else { |
soramimi | 0:7e901efc73e7 | 40 | m -= 9; |
soramimi | 0:7e901efc73e7 | 41 | y++; |
soramimi | 0:7e901efc73e7 | 42 | } |
soramimi | 0:7e901efc73e7 | 43 | *year = y; |
soramimi | 0:7e901efc73e7 | 44 | *month = m; |
soramimi | 0:7e901efc73e7 | 45 | *day = d; |
soramimi | 0:7e901efc73e7 | 46 | } |
soramimi | 0:7e901efc73e7 | 47 | |
soramimi | 0:7e901efc73e7 | 48 | void display(int year, int month, int day, int hour, int minute, int second) |
soramimi | 0:7e901efc73e7 | 49 | { |
soramimi | 0:7e901efc73e7 | 50 | lcd.cls(); |
soramimi | 0:7e901efc73e7 | 51 | lcd.printf("%04u-%02u-%02u\n", year, month, day); |
soramimi | 0:7e901efc73e7 | 52 | lcd.printf("%02u:%02u:%02u\n", hour, minute, second); |
soramimi | 0:7e901efc73e7 | 53 | } |
soramimi | 0:7e901efc73e7 | 54 | |
soramimi | 0:7e901efc73e7 | 55 | int main() |
soramimi | 0:7e901efc73e7 | 56 | { |
soramimi | 0:7e901efc73e7 | 57 | eth.setup(); |
soramimi | 0:7e901efc73e7 | 58 | |
soramimi | 0:7e901efc73e7 | 59 | Host server(IpAddr(), 123, "ntp.jst.mfeed.ad.jp"); |
soramimi | 0:7e901efc73e7 | 60 | |
soramimi | 0:7e901efc73e7 | 61 | bool adjust = true; |
soramimi | 0:7e901efc73e7 | 62 | |
soramimi | 0:7e901efc73e7 | 63 | double last = 0; |
soramimi | 0:7e901efc73e7 | 64 | |
soramimi | 0:7e901efc73e7 | 65 | while (1) { |
soramimi | 0:7e901efc73e7 | 66 | if (adjust) { |
soramimi | 0:7e901efc73e7 | 67 | ntp.setTime(server); |
soramimi | 0:7e901efc73e7 | 68 | adjust = false; |
soramimi | 0:7e901efc73e7 | 69 | } |
soramimi | 0:7e901efc73e7 | 70 | |
soramimi | 0:7e901efc73e7 | 71 | time_t t = time(0); |
soramimi | 0:7e901efc73e7 | 72 | double s = t + 2440588.0 * 24 * 60 * 60; // chronological julian second |
soramimi | 0:7e901efc73e7 | 73 | |
soramimi | 0:7e901efc73e7 | 74 | if (s > last) { |
soramimi | 0:7e901efc73e7 | 75 | last = s; |
soramimi | 0:7e901efc73e7 | 76 | |
soramimi | 0:7e901efc73e7 | 77 | s += TIMEZONE; |
soramimi | 0:7e901efc73e7 | 78 | |
soramimi | 0:7e901efc73e7 | 79 | unsigned long cjd = (unsigned long)(s / (24 * 60 * 60)); // chronological julian day |
soramimi | 0:7e901efc73e7 | 80 | int year, month, day, hour, minute, second; |
soramimi | 0:7e901efc73e7 | 81 | convert_julian_to_gregorian(cjd, &year, &month, &day); |
soramimi | 0:7e901efc73e7 | 82 | second = (int)fmod(s, 24 * 60 * 60); |
soramimi | 0:7e901efc73e7 | 83 | hour = second / (60 * 60); |
soramimi | 0:7e901efc73e7 | 84 | minute = second / 60 % 60; |
soramimi | 0:7e901efc73e7 | 85 | second %= 60; |
soramimi | 0:7e901efc73e7 | 86 | |
soramimi | 0:7e901efc73e7 | 87 | display(year, month, day, hour, minute, second); |
soramimi | 0:7e901efc73e7 | 88 | |
soramimi | 0:7e901efc73e7 | 89 | if (hour == 4 && minute == 0 && second == 0) { |
soramimi | 0:7e901efc73e7 | 90 | adjust = true; |
soramimi | 0:7e901efc73e7 | 91 | } |
soramimi | 0:7e901efc73e7 | 92 | } |
soramimi | 0:7e901efc73e7 | 93 | |
soramimi | 0:7e901efc73e7 | 94 | wait(0.1); |
soramimi | 0:7e901efc73e7 | 95 | } |
soramimi | 0:7e901efc73e7 | 96 | |
soramimi | 0:7e901efc73e7 | 97 | return 0; |
soramimi | 0:7e901efc73e7 | 98 | } |
soramimi | 0:7e901efc73e7 | 99 |