#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
static uint8_t flag = 0;
static char *msg = NULL;
void capture_string (void *parameter) {
uint8_t count = 0;
char ch = 0;
char msg_tmp[30];
memset(msg_tmp, 0, 30);
Serial.println("set message: ");
while (1) {
if (Serial.available() > 0) {
ch = Serial.read();
if (ch != '\n') {
msg_tmp[count] = ch;
count++;
} else {
count += 1;
msg_tmp[count-1] = '\0';
msg = (char*) pvPortMalloc(count * sizeof(char));
if (msg == NULL) {
Serial.println("Not enough heap.");
} else {
memcpy(msg, msg_tmp, count);
flag = 1;
// Serial.println("Memory copied. (" + String(msg) + ")");
}
// Clear the fckin temp. msg not the real msg!
// memset(msg, 0, 30);
memset(msg_tmp, 0, 30);
count = 0;
}
}
}
}
void pass_message (void *parameter) {
while (1) {
if (flag == 1) {
Serial.println("[msg] " + String(msg));
msg = NULL;
flag = 0;
vPortFree(msg);
}
// // Serial.println("Tick..");
vTaskDelay(1 / portTICK_PERIOD_MS);
}
}
void setup () {
Serial.begin(115200);
vTaskDelay(1000 / portTICK_PERIOD_MS);
Serial.println("---FreeRTOS Demo Memory---");
xTaskCreatePinnedToCore(capture_string, "capt_str0", 1024, NULL, 1, NULL, app_cpu);
xTaskCreatePinnedToCore(pass_message, "pass_msg0", 1500, NULL, 1, NULL, app_cpu);
vTaskDelete(NULL);
}
void loop () {}