This repository was archived by the owner on Aug 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprintbuffer.cpp
More file actions
102 lines (83 loc) · 2.22 KB
/
printbuffer.cpp
File metadata and controls
102 lines (83 loc) · 2.22 KB
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
#include "printbuffer.h"
TaskHandle_t tPrinter;
struct sPrintMessage
{
int iSize;
char *cMessage;
};
QueueHandle_t qPrintBuffer = xQueueCreate(PRINTBUFFERSIZE, sizeof(sPrintMessage));
_eLogLevel eLogLevel = LL_NONE;
void PrintInitialize()
{
xTaskCreatePinnedToCore(tfPrint, "printer", configMINIMAL_STACK_SIZE , NULL, tskIDLE_PRIORITY, &tPrinter, 0);
}
void print(_eLogLevel ell, bool bTS, char *format, va_list args)
{
char cTimestamp[20];
char cLocalBuffer[PRINTMESSAGESIZE];
int iTSSize = 0;
if (bTS)
{
uint64_t tCurrentTick = esp_timer_get_time();
int iSeconds = tCurrentTick / 1000000;
int iUSeconds = tCurrentTick - (iSeconds * 1000000);
iTSSize = snprintf(cTimestamp, 20, "%4.4d.%6.6d:%.3s ", iSeconds, iUSeconds, _cLogLevel[ell]);
strncpy(cLocalBuffer, cTimestamp, 16);
}
int iMessageSize = vsnprintf(cLocalBuffer + iTSSize, PRINTMESSAGESIZE - iTSSize, format, args);
sPrintMessage sPM;
sPM.iSize = iMessageSize + iTSSize;
sPM.cMessage =(char *)malloc(sPM.iSize * sizeof(char));
memcpy(sPM.cMessage, cLocalBuffer, sPM.iSize);
if (xQueueSendToBackFromISR(qPrintBuffer, (const void *)&sPM, NULL) != pdPASS)
free(sPM.cMessage);
}
void fastprint(_eLogLevel ell, const char *c, int size)
{
sPrintMessage sPM;
sPM.iSize = size;
sPM.cMessage =(char *)malloc(size * sizeof(char));
memcpy(sPM.cMessage, c, size);
if (xQueueSendToBackFromISR(qPrintBuffer, (const void *)&sPM, NULL) != pdPASS)
free(sPM.cMessage);
}
void print(_eLogLevel ell, char *format, ...)
{
if (ell > eLogLevel)
return;
va_list args;
va_start(args, format);
print(ell, true, format, args);
va_end(args);
}
void printnts(_eLogLevel ell, char *format, ...)
{
if (ell > eLogLevel)
return;
va_list args;
va_start(args, format);
print(ell, false, format, args);
va_end(args);
}
void tfPrint(void *p)
{
sPrintMessage sPM;
for (;;)
{
if (xQueueReceive(qPrintBuffer, (void *)&sPM, portMAX_DELAY) == pdTRUE)
{
int i = 0;
for (; i < sPM.iSize - PRINTMAXSERIALWRITE; i += PRINTMAXSERIALWRITE)
{
Serial.write((char *)sPM.cMessage + i, PRINTMAXSERIALWRITE);
taskYIELD();
}
Serial.write((char *)sPM.cMessage + i, sPM.iSize - i);
free(sPM.cMessage);
}
}
}
void SetLogLevel(_eLogLevel ell)
{
eLogLevel = ell;
}