School project.
Dependencies: Timezone NTPClient BSP_DISCO_F746NG Grove_temperature
time_functions.h@17:01ebfd8ab87a, 2021-01-20 (annotated)
- Committer:
- tuxx0046
- Date:
- Wed Jan 20 14:22:46 2021 +0000
- Revision:
- 17:01ebfd8ab87a
- Child:
- 18:11db143c0502
Added clock functionality to view on display.
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 | 17:01ebfd8ab87a | 8 | /** |
tuxx0046 | 17:01ebfd8ab87a | 9 | Connects to an NTP server and updates the time to display. |
tuxx0046 | 17:01ebfd8ab87a | 10 | Currently only works with UTC time. |
tuxx0046 | 17:01ebfd8ab87a | 11 | 1/20/2021 |
tuxx0046 | 17:01ebfd8ab87a | 12 | */ |
tuxx0046 | 17:01ebfd8ab87a | 13 | void time_update_current_time() |
tuxx0046 | 17:01ebfd8ab87a | 14 | { |
tuxx0046 | 17:01ebfd8ab87a | 15 | char current_time[32]; |
tuxx0046 | 17:01ebfd8ab87a | 16 | /// Ethernet connection declared in main.cpp |
tuxx0046 | 17:01ebfd8ab87a | 17 | eth.connect(); |
tuxx0046 | 17:01ebfd8ab87a | 18 | |
tuxx0046 | 17:01ebfd8ab87a | 19 | if (ntp.setTime("0.pool.ntp.org") == 0) |
tuxx0046 | 17:01ebfd8ab87a | 20 | { |
tuxx0046 | 17:01ebfd8ab87a | 21 | printf("Time successfully retrieved\n"); |
tuxx0046 | 17:01ebfd8ab87a | 22 | } |
tuxx0046 | 17:01ebfd8ab87a | 23 | else |
tuxx0046 | 17:01ebfd8ab87a | 24 | { |
tuxx0046 | 17:01ebfd8ab87a | 25 | printf("Failed to retrieve time from NTP server.\n"); |
tuxx0046 | 17:01ebfd8ab87a | 26 | } |
tuxx0046 | 17:01ebfd8ab87a | 27 | |
tuxx0046 | 17:01ebfd8ab87a | 28 | eth.disconnect(); |
tuxx0046 | 17:01ebfd8ab87a | 29 | while(1) |
tuxx0046 | 17:01ebfd8ab87a | 30 | { |
tuxx0046 | 17:01ebfd8ab87a | 31 | time_t seconds = time(NULL); |
tuxx0046 | 17:01ebfd8ab87a | 32 | |
tuxx0046 | 17:01ebfd8ab87a | 33 | char buffer[32]; |
tuxx0046 | 17:01ebfd8ab87a | 34 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
tuxx0046 | 17:01ebfd8ab87a | 35 | //printf("Time as a custom formatted string = %s", buffer); |
tuxx0046 | 17:01ebfd8ab87a | 36 | sprintf(current_time, "%s", buffer); |
tuxx0046 | 17:01ebfd8ab87a | 37 | lcd_update_upper_left(current_time); |
tuxx0046 | 17:01ebfd8ab87a | 38 | |
tuxx0046 | 17:01ebfd8ab87a | 39 | ThisThread::sleep_for(20s); |
tuxx0046 | 17:01ebfd8ab87a | 40 | } |
tuxx0046 | 17:01ebfd8ab87a | 41 | } |