Kestrel Interface
Loading...
Searching...
No Matches
kest_lv_log.c
Go to the documentation of this file.
1#include "kest_int.h"
2
3#ifndef PRINTLINES_ALLOWED
4#define PRINTLINES_ALLOWED 0
5#endif
6
7static const char *FNAME = "kest_lv_log.c";
8
9#ifdef KEST_USE_FREERTOS
10#ifdef KEST_ENABLE_LV_LOGGING
11SemaphoreHandle_t kest_lv_log_mutex;
12
13static char kest_lv_log_buf[KEST_INT_LV_LOG_BUF_LEN];
14static int kest_lv_log_wrapped = 0;
15static int kest_lv_log_pos = 0;
16
17char *waiting_buf = NULL;
18
19static const char *TAG = "kest_lv_log.c";
20
21void kest_lv_log_flush_task(void *param);
22
23int kest_log_init()
24{
25 for (int i = 0; i < KEST_INT_LV_LOG_BUF_LEN; i++)
26 kest_lv_log_buf[i] = 0;
27
28 kest_lv_log_mutex = xSemaphoreCreateMutex();
29 assert(kest_lv_log_mutex != NULL);
30
31 xTaskCreate(
32 kest_lv_log_flush_task,
33 "log_task",
34 8192,
35 NULL,
36 5,
37 NULL
38 );
39
40 return NO_ERROR;
41}
42
43void kest_lv_log_flush()
44{
45 char local_buf[KEST_INT_LV_LOG_BUF_LEN];
46 int local_buf_position = 0;
47
48 if (xSemaphoreTake(kest_lv_log_mutex, pdMS_TO_TICKS(0)) != pdTRUE)
49 {
50 KEST_PRINTF("Failed to obtain log mutex");
51 return;
52 }
53
54 if (!kest_lv_log_pos && !kest_lv_log_wrapped)
55 {
56 xSemaphoreGive(kest_lv_log_mutex);
57 return;
58 }
59
60 if (kest_lv_log_wrapped)
61 {
62 while (local_buf_position + kest_lv_log_pos + 2 < KEST_INT_LV_LOG_BUF_LEN)
63 {
64 local_buf[local_buf_position] = kest_lv_log_buf[local_buf_position + kest_lv_log_pos + 2];
65 local_buf_position++;
66 }
67 }
68
69 for (int i = 0; i < kest_lv_log_pos && local_buf_position + 1 < KEST_INT_LV_LOG_BUF_LEN; i++)
70 local_buf[local_buf_position++] = kest_lv_log_buf[i];
71
72 local_buf[local_buf_position++] = 0;
73
74 puts(local_buf);
75
76 for (int i = 0; i < KEST_INT_LV_LOG_BUF_LEN; i++)
77 kest_lv_log_buf[i] = 0;
78
79 kest_lv_log_pos = 0;
80 kest_lv_log_wrapped = 0;
81
82 xSemaphoreGive(kest_lv_log_mutex);
83}
84
85void kest_lv_log_cb(lv_log_level_t level, const char *buf)
86{
87 //kest_printf("kest_lv_log_cb\n");
88 if (!buf)
89 {
90 //kest_printf("buf is NULL! returning\n");
91 return;
92 }
93
94 //kest_printf("kest_lv_log_cb. buf = %p", buf);
95
96 //if (buf)
97 // KEST_PRINTF(" = %s", buf ? buf : "(NULL)");
98
99 //kest_printf("\n");
100 if (waiting_buf)
101 {
102 char *local_waiting_buf = kest_strndup(waiting_buf, KEST_INT_LV_LOG_BUF_LEN);
103
104 if (local_waiting_buf)
105 {
106 kest_free(waiting_buf);
107 waiting_buf = NULL;
108 kest_lv_log_cb(level, local_waiting_buf);
109 kest_free(local_waiting_buf);
110 }
111 }
112
113 if (xSemaphoreTake(kest_lv_log_mutex, pdMS_TO_TICKS(50)) != pdTRUE)
114 {
115 if (waiting_buf)
116 kest_free(waiting_buf);
117 waiting_buf = kest_strndup(buf, KEST_INT_LV_LOG_BUF_LEN);
118 return;
119 }
120
121 int len = strlen(buf);
122 int new_pos;
123
124 for (int i = 0; i < len; i++)
125 {
126 kest_lv_log_buf[kest_lv_log_pos] = buf[i];
127
128 new_pos = (kest_lv_log_pos + 1) % KEST_INT_LV_LOG_BUF_LEN;
129 if (new_pos < kest_lv_log_pos)
130 kest_lv_log_wrapped = 1;
131 kest_lv_log_pos = new_pos;
132 }
133
134 if (kest_lv_log_pos + 1 < KEST_INT_LV_LOG_BUF_LEN)
135 kest_lv_log_buf[kest_lv_log_pos + 1] = 0;
136
137 xSemaphoreGive(kest_lv_log_mutex);
138}
139
140void kest_lv_log_flush_task(void *param)
141{
142 TickType_t last_wake = xTaskGetTickCount();
143
144 while (true)
145 {
146 kest_lv_log_flush();
147 vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100));
148 }
149
150
151 vTaskDelete(NULL);
152}
153
154#endif
155#endif
void kest_free(void *ptr)
Definition kest_alloc.c:32
char * kest_strndup(const char *str, size_t n)
Definition kest_alloc.c:73
#define NO_ERROR
#define KEST_INT_LV_LOG_BUF_LEN
Definition kest_lv_log.h:4
void kest_lv_log_cb(lv_log_level_t level, const char *buf)
int kest_log_init()
#define KEST_PRINTF(...)
Definition kest_printf.h:10