School project.
Dependencies: Timezone NTPClient BSP_DISCO_F746NG Grove_temperature
time_functions.h@20:9d4450357ce7, 2021-01-21 (annotated)
- Committer:
- tuxx0046
- Date:
- Thu Jan 21 12:43:03 2021 +0000
- Revision:
- 20:9d4450357ce7
- Parent:
- 19:a23c25da398e
Updated libraries used section in comments in main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tuxx0046 | 17:01ebfd8ab87a | 1 | /** |
tuxx0046 | 17:01ebfd8ab87a | 2 | @file time_functions.h |
tuxx0046 | 17:01ebfd8ab87a | 3 | @author Tu Tri Huynh |
tuxx0046 | 17:01ebfd8ab87a | 4 | @date January 20, 2021 |
tuxx0046 | 17:01ebfd8ab87a | 5 | @brief Functions related to getting and displaying the time |
tuxx0046 | 17:01ebfd8ab87a | 6 | */ |
tuxx0046 | 17:01ebfd8ab87a | 7 | |
tuxx0046 | 18:11db143c0502 | 8 | #include "Timezone.h" |
tuxx0046 | 18:11db143c0502 | 9 | |
tuxx0046 | 17:01ebfd8ab87a | 10 | /** |
tuxx0046 | 17:01ebfd8ab87a | 11 | Connects to an NTP server and updates the time to display. |
tuxx0046 | 17:01ebfd8ab87a | 12 | Currently only works with UTC time. |
tuxx0046 | 17:01ebfd8ab87a | 13 | 1/20/2021 |
tuxx0046 | 17:01ebfd8ab87a | 14 | */ |
tuxx0046 | 17:01ebfd8ab87a | 15 | void time_update_current_time() |
tuxx0046 | 17:01ebfd8ab87a | 16 | { |
tuxx0046 | 17:01ebfd8ab87a | 17 | char current_time[32]; |
tuxx0046 | 17:01ebfd8ab87a | 18 | /// Ethernet connection declared in main.cpp |
tuxx0046 | 17:01ebfd8ab87a | 19 | eth.connect(); |
tuxx0046 | 17:01ebfd8ab87a | 20 | |
tuxx0046 | 17:01ebfd8ab87a | 21 | if (ntp.setTime("0.pool.ntp.org") == 0) |
tuxx0046 | 17:01ebfd8ab87a | 22 | { |
tuxx0046 | 19:a23c25da398e | 23 | printf("\nTime successfully retrieved\n"); |
tuxx0046 | 17:01ebfd8ab87a | 24 | } |
tuxx0046 | 17:01ebfd8ab87a | 25 | else |
tuxx0046 | 17:01ebfd8ab87a | 26 | { |
tuxx0046 | 19:a23c25da398e | 27 | printf("\nFailed to retrieve time from NTP server.\n"); |
tuxx0046 | 17:01ebfd8ab87a | 28 | } |
tuxx0046 | 17:01ebfd8ab87a | 29 | |
tuxx0046 | 17:01ebfd8ab87a | 30 | eth.disconnect(); |
tuxx0046 | 18:11db143c0502 | 31 | |
tuxx0046 | 18:11db143c0502 | 32 | time_t dk_time; |
tuxx0046 | 18:11db143c0502 | 33 | TimeChangeRule dk_standardtime = {"DNT", Last, Sat, Oct, 3, 60}; |
tuxx0046 | 18:11db143c0502 | 34 | //TimeChangeRule dk_daylight = {"DST", Last, Sun, Mar, 2, 120}; |
tuxx0046 | 18:11db143c0502 | 35 | Timezone danish_time(dk_standardtime); |
tuxx0046 | 18:11db143c0502 | 36 | //Timezone danish_time(dk_daylight, dk_standardtime); |
tuxx0046 | 18:11db143c0502 | 37 | |
tuxx0046 | 18:11db143c0502 | 38 | //dk_tid = danish_timetoLocal(ctTime); |
tuxx0046 | 18:11db143c0502 | 39 | |
tuxx0046 | 17:01ebfd8ab87a | 40 | while(1) |
tuxx0046 | 17:01ebfd8ab87a | 41 | { |
tuxx0046 | 17:01ebfd8ab87a | 42 | time_t seconds = time(NULL); |
tuxx0046 | 18:11db143c0502 | 43 | dk_time = danish_time.toLocal(seconds); |
tuxx0046 | 17:01ebfd8ab87a | 44 | |
tuxx0046 | 17:01ebfd8ab87a | 45 | char buffer[32]; |
tuxx0046 | 18:11db143c0502 | 46 | strftime(buffer, 32, "%I:%M %p\n", localtime(&dk_time)); |
tuxx0046 | 17:01ebfd8ab87a | 47 | //printf("Time as a custom formatted string = %s", buffer); |
tuxx0046 | 17:01ebfd8ab87a | 48 | sprintf(current_time, "%s", buffer); |
tuxx0046 | 17:01ebfd8ab87a | 49 | lcd_update_upper_left(current_time); |
tuxx0046 | 17:01ebfd8ab87a | 50 | |
tuxx0046 | 17:01ebfd8ab87a | 51 | ThisThread::sleep_for(20s); |
tuxx0046 | 17:01ebfd8ab87a | 52 | } |
tuxx0046 | 17:01ebfd8ab87a | 53 | } |