Simply Clock

Dependencies:   EthernetNetIf NTPClient_NetServices TextLCD mbed

Committer:
soramimi
Date:
Mon Mar 28 21:49:06 2011 +0000
Revision:
1:7974d199ed39
Parent:
0:7e901efc73e7

    

Who changed what in which revision?

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