oboo_notification.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @file oboo_notification.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "lvgl/lvgl.h"
  9. /*********************
  10. * DEFINES
  11. *********************/
  12. #define TITLE_FONT_SIZE 20
  13. #define TEXT_WIDTH 140
  14. /**********************
  15. * TYPEDEFS
  16. **********************/
  17. /**********************
  18. * STATIC PROTOTYPES
  19. **********************/
  20. static lv_font_t * get_font_from_height(uint8_t h);
  21. /**********************
  22. * STATIC VARIABLES
  23. **********************/
  24. static lv_obj_t * notif_cont = NULL; /*Then main container for the notification*/
  25. static lv_obj_t * notif_title = NULL;
  26. static lv_obj_t * notif_text = NULL;
  27. static lv_style_t style_bg;
  28. static lv_style_t style_title;
  29. static lv_style_t style_text;
  30. /**********************
  31. * MACROS
  32. **********************/
  33. /**********************
  34. * GLOBAL FUNCTIONS
  35. **********************/
  36. void oboo_notification_init(void)
  37. {
  38. lv_style_copy(&style_bg, &lv_style_plain);
  39. style_bg.body.main_color = LV_COLOR_HEX(0x2082be);
  40. style_bg.body.grad_color = LV_COLOR_HEX(0x2082be);
  41. style_bg.body.radius = 7;
  42. style_bg.body.padding.hor = 10;
  43. style_bg.body.padding.ver = 5;
  44. lv_style_copy(&style_title, &lv_style_plain);
  45. style_title.text.color = LV_COLOR_WHITE;
  46. style_title.text.font = get_font_from_height(TITLE_FONT_SIZE);
  47. lv_style_copy(&style_text, &style_title);
  48. notif_cont = lv_cont_create(lv_layer_top(), NULL);
  49. lv_cont_set_fit(notif_cont, true, true);
  50. lv_cont_set_layout(notif_cont, LV_LAYOUT_CENTER);
  51. lv_cont_set_style(notif_cont, &style_bg);
  52. lv_obj_set_hidden(notif_cont, true);
  53. notif_title = lv_label_create(notif_cont, NULL);
  54. lv_label_set_style(notif_title, &style_title);
  55. lv_label_set_long_mode(notif_title, LV_LABEL_LONG_BREAK);
  56. lv_obj_set_width(notif_title, TEXT_WIDTH);
  57. notif_text = lv_label_create(notif_cont, notif_title);
  58. lv_label_set_style(notif_text, &style_text);
  59. lv_label_set_align(notif_text, LV_LABEL_ALIGN_CENTER);
  60. lv_obj_align(notif_cont, NULL, LV_ALIGN_CENTER, 0, 0);
  61. }
  62. /**
  63. * Show a notification with the given parameters. Set `title` to NULL to hide it.
  64. */
  65. void oboo_notification_show(const char * title, const char * msg, uint8_t font_size)
  66. {
  67. if(title != NULL) {
  68. lv_label_set_text(notif_title, title);
  69. lv_obj_set_hidden(notif_title, false);
  70. } else {
  71. lv_obj_set_hidden(notif_title, true);
  72. }
  73. style_text.text.font = get_font_from_height(font_size);
  74. lv_label_set_text(notif_text, msg);
  75. lv_obj_set_hidden(notif_cont, false);
  76. }
  77. /**
  78. * Hide the the notification
  79. */
  80. void oboo_notification_clear(void)
  81. {
  82. lv_obj_set_hidden(notif_cont, true);
  83. }
  84. /**********************
  85. * STATIC FUNCTIONS
  86. **********************/
  87. /**
  88. * Get a pointer to a built-in font when it's height is given
  89. */
  90. static lv_font_t * get_font_from_height(uint8_t h)
  91. {
  92. switch(h) {
  93. #if USE_LV_FONT_DEJAVU_10
  94. case 10: return &lv_font_dejavu_10;
  95. #endif
  96. #if USE_LV_FONT_DEJAVU_10
  97. case 20: return &lv_font_dejavu_20;
  98. #endif
  99. #if USE_LV_FONT_DEJAVU_10
  100. case 30: return &lv_font_dejavu_30;
  101. #endif
  102. #if USE_LV_FONT_DEJAVU_10
  103. case 40: return &lv_font_dejavu_40;
  104. #endif
  105. default: return NULL;
  106. }
  107. }