diff options
Diffstat (limited to 'src/calendar.cpp')
| -rw-r--r-- | src/calendar.cpp | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/src/calendar.cpp b/src/calendar.cpp index d743718..04325b0 100644 --- a/src/calendar.cpp +++ b/src/calendar.cpp @@ -1,17 +1,18 @@ #include <GxEPD2_BW.h> -#include <Fonts/FreeSansBold12pt7b.h> -#include <Fonts/FreeSansBold9pt7b.h> -#include <Fonts/FreeSans9pt7b.h> +#include <U8g2_for_Adafruit_GFX.h> #include <ctime> -#define CELL_SPACING 10 +#define CELL_SPACING 8 + +extern GxEPD2_GFX_BASE_CLASS &get_display(); +extern U8G2_FOR_ADAFRUIT_GFX u8g2Fonts; static const char *week_days[] { "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So", }; static const char *months[] { - "Januar", "Februar", "Maerz", "April", "Mai", "Juni", "Juli", + "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember", }; @@ -55,7 +56,6 @@ static int days_in_month(int month, int year) { } /* - * @display where to draw on * @x0 absolute initial X coord * @y0 absolute initial Y coord * @w0 width of a cell @@ -65,24 +65,22 @@ static int days_in_month(int month, int year) { * @text text to draw * @highlight whether text should be highlighted */ -static void draw_cell_content(GxEPD2_GFX_BASE_CLASS &display, int16_t x0, int16_t y0, uint16_t w0, uint16_t h0, uint8_t xoff, uint8_t yoff, String &text, bool highlight = false) { - int16_t x, y; - uint16_t w, h; - - display.getTextBounds(text, 0, 0, &x, &y, &w, &h); +static void draw_cell_content(int16_t x0, int16_t y0, uint16_t w0, uint16_t h0, uint8_t xoff, uint8_t yoff, String &text, bool highlight = false) { + uint16_t w = u8g2Fonts.getUTF8Width(text.c_str()); + uint16_t h = 10; /* assume 10px high font */ int16_t w_diff = w0 - w; - int x2 = x0 + xoff * w0 + w_diff/2 - x; - int y2 = y0 + yoff * h0 - y; + int x2 = x0 + xoff * w0 + w_diff/2; + int y2 = y0 + yoff * h0; - display.setCursor(x2, y2); - display.print(text); + u8g2Fonts.setCursor(x2, y2); + u8g2Fonts.print(text); if (highlight) { - display.drawCircle(x2 + x + w/2, y2 - h/2, (w0 - CELL_SPACING) / 2, GxEPD_BLACK); + get_display().drawCircle(x2 + w/2, y2 - h/2 - 1, w0/2 - 3, GxEPD_BLACK); } } -void draw_calendar(GxEPD2_GFX_BASE_CLASS &display, int16_t x0, int16_t y0) { +void draw_calendar(int16_t x0, int16_t y0) { struct tm now; if (!getLocalTime(&now)) { return; @@ -97,32 +95,33 @@ void draw_calendar(GxEPD2_GFX_BASE_CLASS &display, int16_t x0, int16_t y0) { day_of_1st = (day_of_1st - 1 + 7) % 7; /* determine sizes */ - int16_t x, y; - uint16_t w, h, cell_width, cell_height; - display.getTextBounds(week_days[0], 0, 0, &x, &y, &w, &h); + u8g2Fonts.setFont(u8g2_font_helvB10_tf); + uint16_t cell_width, cell_height; + int w = u8g2Fonts.getUTF8Width(week_days[0]); + int h = 10; /* assume 10px high font */ cell_width = w + CELL_SPACING; - cell_height = h + CELL_SPACING; + cell_height = h + CELL_SPACING + 5; /* draw title with month and year */ - display.setFont(&FreeSansBold12pt7b); + u8g2Fonts.setFont(u8g2_font_helvB14_tf); String title_text = String(months[now.tm_mon]) + " " + String(1900 + now.tm_year); - draw_cell_content(display, x0, y0, cell_width * 7, h, 0, 0, title_text); + draw_cell_content(x0, y0, cell_width * 7, h, 0, 0, title_text); y0 += h + 2 * CELL_SPACING; /* draw table header with week days */ - display.setFont(&FreeSansBold9pt7b); + u8g2Fonts.setFont(u8g2_font_helvB10_tf); for (int i=0; i<7; i++) { String week_day = String(week_days[i]); - draw_cell_content(display, x0, y0, cell_width, cell_height, i, 0, week_day); + draw_cell_content(x0, y0, cell_width, cell_height, i, 0, week_day); } /* draw days */ - display.setFont(&FreeSans9pt7b); + u8g2Fonts.setFont(u8g2_font_helvR10_tf); int8_t wday = day_of_1st; uint8_t row = 1; for (uint8_t day = 1; day <= n_days; day++) { String day_str = String(day); - draw_cell_content(display, x0, y0, cell_width, cell_height, wday, row, day_str, day == now.tm_mday); + draw_cell_content(x0, y0, cell_width, cell_height, wday, row, day_str, day == now.tm_mday); wday++; if (wday > 6) { |
