oboo_calendar.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @file oboo_calendar.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "lvgl/lvgl.h"
  9. /*********************
  10. * DEFINES
  11. *********************/
  12. /**********************
  13. * TYPEDEFS
  14. **********************/
  15. /**********************
  16. * STATIC PROTOTYPES
  17. **********************/
  18. /**********************
  19. * STATIC VARIABLES
  20. **********************/
  21. /**********************
  22. * MACROS
  23. **********************/
  24. /**********************
  25. * GLOBAL FUNCTIONS
  26. **********************/
  27. /**
  28. * Initializes the calendar styles and create a calendar
  29. * @param parent create the calendar on this parent
  30. * @aparam x position in x
  31. * @aparam y position in y
  32. * @aparam w width
  33. * @aparam h height
  34. * @return the created calendar object (lv_calendar)
  35. */
  36. lv_obj_t * oboo_calendar_create(lv_obj_t * parent, uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  37. {
  38. /*Background style*/
  39. static lv_style_t style_calendar_bg;
  40. lv_style_copy(&style_calendar_bg, &lv_style_plain);
  41. style_calendar_bg.body.main_color = LV_COLOR_HEX(0xeef3f9);
  42. style_calendar_bg.body.grad_color = LV_COLOR_HEX(0xf0f3f4);
  43. style_calendar_bg.body.padding.hor = LV_DPI / 12; /*Margin on the left and right of the rows of date numbers*/
  44. style_calendar_bg.body.padding.inner = LV_DPI / 6; /*Margin above the date numbers*/
  45. style_calendar_bg.body.padding.ver = LV_DPI / 10; /*Margin below the date numbers*/
  46. style_calendar_bg.body.border.color = LV_COLOR_HEX3(0x888);
  47. style_calendar_bg.body.border.width = 1;
  48. style_calendar_bg.body.border.part = LV_BORDER_LEFT | LV_BORDER_RIGHT | LV_BORDER_BOTTOM;
  49. style_calendar_bg.text.color = LV_COLOR_HEX3(0x666);
  50. /*Inactive day's style (prev/next month)*/
  51. static lv_style_t style_calendar_inactive_days;
  52. lv_style_copy(&style_calendar_inactive_days, &style_calendar_bg);
  53. style_calendar_inactive_days.text.color = LV_COLOR_HEX3(0xbbb);
  54. /*Header style*/
  55. static lv_style_t style_calendar_header;
  56. lv_style_copy(&style_calendar_header, &lv_style_plain);
  57. style_calendar_header.body.main_color = LV_COLOR_HEX(0xee8065);
  58. style_calendar_header.body.grad_color = style_calendar_header.body.main_color;
  59. style_calendar_header.body.padding.hor = LV_DPI / 10; /*Margin next to the arrows*/
  60. style_calendar_header.body.border.color = LV_COLOR_HEX(0x8e3823);
  61. style_calendar_header.body.radius = 0;
  62. style_calendar_header.body.border.width = 1;
  63. style_calendar_header.body.border.part = LV_BORDER_LEFT | LV_BORDER_RIGHT | LV_BORDER_TOP;
  64. style_calendar_header.text.font = LV_FONT_DEFAULT;
  65. style_calendar_header.text.color = LV_COLOR_WHITE;
  66. /*Arrow's pressed style (on the header)*/
  67. static lv_style_t style_calendar_header_pr;
  68. lv_style_copy(&style_calendar_header_pr, &style_calendar_header);
  69. style_calendar_header_pr.text.color = LV_COLOR_HEX(0x8e3823); /*Dark orange for pressed arrows*/
  70. /*Day's name style (Sun..Sat)*/
  71. static lv_style_t style_calendar_day_names;
  72. lv_style_copy(&style_calendar_day_names, &lv_style_plain);
  73. style_calendar_day_names.body.padding.ver = LV_DPI / 8; /*Space above the day names*/
  74. style_calendar_day_names.body.padding.hor = LV_DPI / 12; /*Space on the left and right*/
  75. style_calendar_day_names.text.color = LV_COLOR_HEX(0x62afd8);
  76. // style_calendar_day_names.text.font = &lv_font_dejavu_10;
  77. style_calendar_day_names.text.font = &lv_font_dejavu_20;
  78. /*Highlighted day's style*/
  79. static lv_style_t style_calendar_highlighted_days;
  80. lv_style_copy(&style_calendar_highlighted_days, &lv_style_plain);
  81. style_calendar_highlighted_days.text.color = LV_COLOR_HEX(0xee8065);
  82. /*"Week box" style*/
  83. static lv_style_t style_calendar_week_box;
  84. lv_style_copy(&style_calendar_week_box, &lv_style_pretty);
  85. style_calendar_week_box.body.main_color = LV_COLOR_HEX(0xb0dcec);
  86. style_calendar_week_box.body.grad_color = style_calendar_week_box.body.main_color;
  87. style_calendar_week_box.body.radius = LV_DPI / 30;
  88. style_calendar_week_box.body.border.width = 2;
  89. style_calendar_week_box.body.padding.ver = LV_DPI / 30;
  90. style_calendar_week_box.body.padding.hor = 0;
  91. style_calendar_week_box.text.color = LV_COLOR_HEX(0x6da0b5);
  92. /*"Today box" style*/
  93. static lv_style_t style_calendar_today_box;
  94. lv_style_copy(&style_calendar_today_box, &lv_style_pretty);
  95. style_calendar_today_box.body.main_color = LV_COLOR_HEX(0x46baeb);
  96. style_calendar_today_box.body.grad_color = style_calendar_today_box.body.main_color;
  97. style_calendar_today_box.body.radius = LV_DPI / 30;
  98. style_calendar_today_box.body.border.width = 2;
  99. style_calendar_today_box.body.padding.ver = LV_DPI / 16;
  100. style_calendar_today_box.body.padding.hor = 0;
  101. style_calendar_today_box.text.color = LV_COLOR_HEX(0xd4edf3);
  102. /*Create a calendar*/
  103. lv_obj_t * calendar = lv_calendar_create(parent, NULL);
  104. lv_obj_set_size(calendar, 230, 200);
  105. lv_obj_set_pos(calendar, 5, 5);
  106. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_BG, &style_calendar_bg);
  107. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_INACTIVE_DAYS, &style_calendar_inactive_days);
  108. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_HEADER, &style_calendar_header);
  109. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_DAY_NAMES, &style_calendar_day_names);
  110. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS, &style_calendar_highlighted_days);
  111. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_HEADER_PR, &style_calendar_header_pr);
  112. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_WEEK_BOX, &style_calendar_week_box);
  113. lv_calendar_set_style(calendar, LV_CALENDAR_STYLE_TODAY_BOX, &style_calendar_today_box);
  114. /*Set the day names*/
  115. static const char * day_names[7] = {"S", "M", "T", "W", "T", "F", "S"}; /*Must be static or global because only it's pointer will be saved*/
  116. lv_calendar_set_day_names(calendar, day_names);
  117. /* position and size */
  118. lv_obj_set_pos(calendar, x, y);
  119. lv_obj_set_size(calendar, w, h);
  120. return calendar;
  121. }
  122. /**
  123. * Set some day
  124. * @param calendar pointer to calendar (return value of `oboo_calendar_create`)
  125. * @param year the current year (e.g. 2018)
  126. * @param month the current month (e.g. 7 [1..12])
  127. * @param day the current day (e.g. 23 [1..31])
  128. */
  129. void oboo_calendar_set_day(lv_obj_t * calendar, uint32_t year, uint8_t month, uint8_t day)
  130. {
  131. lv_calendar_date_t date;
  132. date.year = year;
  133. date.month = month;
  134. date.day = day;
  135. lv_calendar_set_today_date(calendar, &date);
  136. lv_calendar_set_showed_date(calendar, &date);
  137. }
  138. /**
  139. * Set events for the calendar
  140. * @param calendar pointer to calendar (return value of `oboo_calendar_create`)
  141. * @param events an array with the events. Only the pointer is saved so the array can't be a local variable
  142. * @param event_cnt number of events in the array
  143. */
  144. void oboo_calendar_set_events(lv_obj_t * calendar, lv_calendar_date_t * events, uint32_t event_cnt)
  145. {
  146. lv_calendar_set_highlighted_dates(calendar, events, event_cnt);
  147. }
  148. /**
  149. * Clear the events
  150. * @param calendar pointer to calendar (return value of `oboo_calendar_create`)
  151. */
  152. void obbo_calendar_clear_events(lv_obj_t * calendar)
  153. {
  154. lv_calendar_set_highlighted_dates(calendar, NULL, 0);
  155. }
  156. /**********************
  157. * STATIC FUNCTIONS
  158. **********************/