Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
date_time.cpp
Go to the documentation of this file.
1#include "date_time.h"
2
3// Time APIs
4int8_t PaperdinkUIDateClass::fetch_data(const char *time_zone, uint8_t week_start_offset)
5{
6 struct tm timeinfo;
7 time_t epoch;
8 const int MONTHS[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
9
10 for (uint8_t i = 0; i < CONFIG_TIME_RETRY_COUNT; i++) {
11 configTime(0, 0, NTP_SERVER);
12
13 Serial.printf("TZ: %s\r\n", time_zone);
14 setenv("TZ", time_zone, 1);
15
16 if (!getLocalTime(&timeinfo)) {
17 Serial.println("Failed to obtain time. Retrying...");
18 delay(1000);
20 return -1;
21 } else {
22 break;
23 }
24 }
25
26 epoch = mktime(&timeinfo);
27
28 sscanf(ctime(&epoch), "%s %s %hhd %hhd:%hhd:%hhd %d", wday, month, &mday, &mil_hour, &min, &sec, &year);
29
30 month_num = timeinfo.tm_mon + 1;
31 // Gives offset of first day of the month with respect to Monday
32 // https://www.tondering.dk/claus/cal/chrweek.php#calcdow
33 // 0=Sunday, 1=Monday ... 6=Saturday
34 uint16_t a = (14 - month_num) / 12;
35 uint16_t y = year - a;
36 uint16_t m = month_num + (12 * a) - 2;
37
38 day_offset = (1 + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7;
39 // Change the offset based on user preference
41
42 // Convert to 12 hour
43 if (mil_hour > 12)
44 hour = mil_hour - 12;
45 else
46 hour = mil_hour;
47
48 // Get number of days in a month
49 month_days = MONTHS[month_num];
50 // Handle leap years
51 if (month_num == 1 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
52 month_days = 29;
53
54 Serial.printf("Time is %d %d:%d:%d on %s on the %d/%d/%d . It is the month of %s. day_offset: %d\r\n", mil_hour, hour, min, sec, wday, mday, month_num, year, month, day_offset);
55 return 0;
56}
57
58void PaperdinkUIDateClass::display_day_date_style1_center(GxEPD2_GFX& display, uint16_t x, uint16_t y, uint16_t w)
59{
60 int16_t xt, yt;
61 uint16_t wt, ht, prev_height = 0, prev_width = 0;
62 char date_str[7];
63
64 display.setFont(font);
65 display.setTextColor(primary_color);
66 display.setTextSize(1);
67
68 // Display day
69 display.getTextBounds(wday, 0, 0, &xt, &yt, &wt, &ht);
70 display.setCursor(x + ((w - wt) / 2), y + ht);
71 display.println(wday);
72 prev_height += y + ht;
73
74 // Display date
75 sprintf(date_str, "%d %s", mday, month);
76 display.getTextBounds(date_str, 0, 0, &xt, &yt, &wt, &ht);
77 display.setCursor(x + ((w - wt) / 2), prev_height + ht + 20);
78 display.println(date_str);
79}
80
81void PaperdinkUIDateClass::display_day_date_style2_center(GxEPD2_GFX& display, uint16_t x, uint16_t y, uint16_t w)
82{
83 int16_t xt, yt;
84 uint16_t wt, ht, prev_height = 0, prev_width = 0;
85 char mday_str[3];
86
87 display.setFont(font);
88 display.setTextColor(secondary_color);
89 display.setTextSize(1);
90
91 // Display day
92 sprintf(mday_str, "%02d", Paperdink_Date.mday);
93 display.getTextBounds(mday_str, 0, 0, &xt, &yt, &wt, &ht);
94 display.setCursor(x + ((w - wt) / 2), y + ht);
95 display.println(mday_str);
96 prev_height += y + ht;
97
98 // Display month
99 display.getTextBounds(Paperdink_Date.month, 0, 0, &xt, &yt, &wt, &ht);
100 display.setCursor(x + ((w - wt) / 2), prev_height + ht + 5);
101 display.println(Paperdink_Date.month);
102}
103
104void PaperdinkUIDateClass::display_last_updated_time_style1_center(GxEPD2_GFX& display, uint16_t x, uint16_t y, uint16_t w)
105{
106 int16_t xt, yt;
107 uint16_t wt, ht, prev_height = 0, prev_width = 0;
108 char time_str[] = "Updated at: XX:XX";
109
110 display.setFont(font);
111 display.setTextColor(primary_color);
112 display.setTextSize(1);
113
114 sprintf(&time_str[12], "%02d:%02d", mil_hour, min);
115 display.getTextBounds(time_str, 0, 0, &xt, &yt, &wt, &ht);
116 display.setCursor(x + ((w - wt) / 2), y + ht);
117 display.print(time_str);
118}
119
120void PaperdinkUIDateClass::display_calendar(GxEPD2_GFX& display, uint16_t x, uint16_t y)
121{
122 int16_t xt, yt;
123 uint16_t wt, ht;
124
125 /* Space between text depends on the font
126 * hence font is hardcoded.
127 */
128 display.setFont(&Gobold_Thin9pt7b);
129 display.setTextSize(1);
130 display.setTextColor(primary_color);
131
132#define CAL_STRING "Mon Tue Wed Thu Fri Sat Sun"
133
134 display.setCursor(x, y);
135 display.print(CAL_STRING);
136 display.getTextBounds(CAL_STRING, 0, 0, &xt, &yt, &wt, &ht);
137 uint8_t num_offset, print_valid = 0;
138 uint8_t day = 1;
139
140 for (uint8_t j = 0; j <= 5; j++) {
141 for (uint8_t i = 1; i <= 7 && day <= month_days; i++) {
142 // you can hack around with this value to align your text properly based on what font, font size etc you are using
143 num_offset = 21; // 21 is what works for me for the first 2 columns
144 if (i >= 3 && i <= 7)
145 num_offset = 17; // then I need to reduce to 17
146 if (j == 0 && ((i - 1) == Paperdink_Date.day_offset || Paperdink_Date.day_offset == 0))
147 // start from the offset in the month, ie which day does 1st of the month lie on
148 print_valid = 1;
149 if (print_valid) {
150 display.setCursor(x + (i * (wt / 7)) - num_offset, y + ((j + 1) * ht) + ((j + 1) * 7));
151 if (day == Paperdink_Date.mday) {
152 char str[3];
153 sprintf(str, "%d", day);
154 int16_t x2, y2;
155 uint16_t w2, h2;
156 display.getTextBounds(str, x + (i * (wt / 7)) - num_offset, y + ((j + 1) * ht) + ((j + 1) * 7), &x2, &y2, &w2, &h2);
157 display.fillRect(x2 - 4, y2 - 4, w2 + 8, h2 + 8, primary_color);
158 display.setTextColor(secondary_color);
159 } else {
160 display.setTextColor(primary_color);
161 }
162 // once the offset is reached, start incrementing
163 display.println(day);
164 day += 1;
165 }
166 }
167 }
168}
169
const GFXfont * font
Definition ui_base.h:9
uint8_t week_start_offset
Definition date_time.h:24
void display_last_updated_time_style1_center(GxEPD2_GFX &display, uint16_t x, uint16_t y, uint16_t w)
void display_day_date_style2_center(GxEPD2_GFX &display, uint16_t x, uint16_t y, uint16_t w)
Definition date_time.cpp:81
void display_day_date_style1_center(GxEPD2_GFX &display, uint16_t x, uint16_t y, uint16_t w)
Definition date_time.cpp:58
void display_calendar(GxEPD2_GFX &display, uint16_t x, uint16_t y)
int8_t fetch_data(const char *time_zone, uint8_t week_start_offset=6)
Definition date_time.cpp:4
#define CAL_STRING
PaperdinkUIDateClass Paperdink_Date
#define CONFIG_TIME_RETRY_COUNT
Definition date_time.h:7
#define NTP_SERVER
Definition date_time.h:6
PaperdinkUIDateClass Paperdink_Date