forked from bigtreetech/BIGTREETECH-TouchScreenFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotification.c
210 lines (176 loc) · 5.24 KB
/
Notification.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "Notification.h"
#include "includes.h"
#include "my_misc.h"
// area for toast notification
const GUI_RECT toastRect = {START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TOAST_Y_PAD, LCD_WIDTH - START_X, TITLE_END_Y - TOAST_Y_PAD};
const GUI_RECT toastIconRect = {START_X, TOAST_Y_PAD, START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TITLE_END_Y - TOAST_Y_PAD};
static struct
{
DIALOG_TYPE style;
uint8_t isNew;
char text[TOAST_MSG_LENGTH];
} toastlist[TOAST_MSG_COUNT];
static uint8_t nextToastIndex = 0; // next index to store new toast
static uint8_t curToastDisplay = 0; // current toast notification being displayed
static uint32_t nextToastTime = 0; // time to change to next notification
static NOTIFICATION msglist[MAX_MSG_COUNT];
static uint8_t nextMsgIndex = 0; // next index to store new message
static void (*notificationHandler)() = NULL;
bool _toastRunning = false;
// add new message to toast notification queue
void addToast(DIALOG_TYPE style, char * text)
{
LCD_WAKE();
strscpy(toastlist[nextToastIndex].text, text, TOAST_MSG_LENGTH);
toastlist[nextToastIndex].style = style;
toastlist[nextToastIndex].isNew = true;
nextToastIndex = (nextToastIndex + 1) % TOAST_MSG_COUNT;
}
// check if notification is currently displayed
bool toastRunning(void)
{
return _toastRunning;
}
// check if any new notification is available
bool toastAvailable(void)
{
for (uint8_t i = 0; i < TOAST_MSG_COUNT; i++)
{
if (toastlist[i].isNew == true)
return true;
}
return false;
}
// show next notification
void drawToast(bool redraw)
{
if (!redraw)
curToastDisplay = (curToastDisplay + 1) % TOAST_MSG_COUNT;
if (toastlist[curToastDisplay].isNew == true || redraw)
{
// set toast notification running status
_toastRunning = true;
// draw icon
uint8_t *icon;
SOUND cursound;
switch (toastlist[curToastDisplay].style)
{
case DIALOG_TYPE_ERROR:
GUI_SetColor(NOTIF_ICON_ERROR_BG_COLOR);
icon = IconCharSelect(CHARICON_ERROR);
cursound = SOUND_ERROR;
break;
case DIALOG_TYPE_SUCCESS:
GUI_SetColor(NOTIF_ICON_SUCCESS_BG_COLOR);
icon = IconCharSelect(CHARICON_OK_ROUND);
cursound = SOUND_SUCCESS;
break;
default:
GUI_SetColor(NOTIF_ICON_INFO_BG_COLOR);
icon = IconCharSelect(CHARICON_INFO);
cursound = SOUND_TOAST;
break;
}
if (!redraw) // if notification is new
{
BUZZER_PLAY(cursound); // play sound
nextToastTime = OS_GetTimeMs() + SEC_TO_MS(TOAST_DURATION); // set new timer
}
GUI_SetTextMode(GUI_TEXTMODE_TRANS);
GUI_FillPrect(&toastIconRect);
GUI_SetColor(NOTIF_ICON_FG_COLOR);
GUI_DispStringInPrect(&toastIconRect, icon);
// draw text
GUI_SetColor(NOTIF_TEXT_BG_COLOR);
GUI_FillPrect(&toastRect);
GUI_SetColor(NOTIF_TEXT_FONT_COLOR);
GUI_DispStringInPrect(&toastRect, (uint8_t *)toastlist[curToastDisplay].text);
// set current toast notification as old/completed
toastlist[curToastDisplay].isNew = false;
GUI_RestoreColorDefault();
}
}
// check and control toast notification display
void loopToast(void)
{
if (getMenuType() != MENU_TYPE_FULLSCREEN && OS_GetTimeMs() > nextToastTime)
{
if (toastAvailable())
{
drawToast(false);
}
else if (_toastRunning == true)
{
_toastRunning = false;
GUI_ClearPrect(&toastIconRect);
GUI_ClearPrect(&toastRect);
menuDrawTitle();
}
}
}
// add new message to notification queue
void addNotification(DIALOG_TYPE style, char *title, char *text, bool draw_dialog)
{
LCD_WAKE();
if (nextMsgIndex >= MAX_MSG_COUNT)
{
// remove oldest message and move all messages up one step
memmove(msglist, &msglist[1], (MAX_MSG_COUNT - 1) * (sizeof(NOTIFICATION)));
nextMsgIndex = MAX_MSG_COUNT - 1;
}
// store message
msglist[nextMsgIndex].style = style;
strscpy(msglist[nextMsgIndex].text, text, MAX_MSG_LENGTH);
strscpy(msglist[nextMsgIndex].title, title, MAX_MSG_TITLE_LENGTH);
nextMsgIndex++;
if (draw_dialog && MENU_IS_NOT(menuNotification))
popupReminder(style, (uint8_t *)title, (uint8_t *)text);
if (notificationHandler != NULL)
notificationHandler();
notificationDot();
statusScreenSetMsg((uint8_t *)title, (uint8_t *)text);
}
// replay a notification
void replayNotification(uint8_t index)
{
if (index < nextMsgIndex)
popupReminder(msglist[index].style, (uint8_t *)msglist[index].title, (uint8_t *)msglist[index].text);
}
// retrieve a stored notification
NOTIFICATION *getNotification(uint8_t index)
{
if (msglist[index].title[0] != '\0' && msglist[index].text[0] != '\0')
return &msglist[index];
else
return NULL;
}
bool hasNotification(void)
{
return (nextMsgIndex != 0);
}
void clearNotification(void)
{
nextMsgIndex = 0;
for (int i = 0; i < MAX_MSG_COUNT; i++)
{
msglist[i].text[0] = '\0';
msglist[i].title[0] = '\0';
}
notificationDot();
statusScreenSetReady();
}
// check if pressed on titlebar area
void titleBarPress(void)
{
if (getMenuType() == MENU_TYPE_ICON || getMenuType() == MENU_TYPE_LISTVIEW)
{
if (MENU_IS_NOT(menuNotification))
{
OPEN_MENU(menuNotification);
}
}
}
void setNotificationHandler(void (*handler)())
{
notificationHandler = handler;
}