#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu=0;
#else 
static const BaseType_t app_cpu=1;
#endif

// Task handle
TaskHandle_t inputTaskHandle = NULL;

// Maximum input length
#define MAX_INPUT_LENGTH 128

void inputTask(void *parameter) {
  (void)parameter;

  char *inputBuffer = (char *)pvPortMalloc(MAX_INPUT_LENGTH); // Allocate memory on the heap
  if (inputBuffer == NULL) {
    Serial.println("Failed to allocate memory");
    vTaskDelete(NULL); // Delete the task if memory allocation fails
  }

  int index = 0;
  char receivedChar;

  while (1) {
    if (Serial.available() > 0) {
      receivedChar = Serial.read();

      if (receivedChar == '\n') {
        // Null-terminate the string
        inputBuffer[index] = '\0';

        // Print the received input
        Serial.print("Received input: ");
        Serial.println(inputBuffer);

        // Reset index and clear buffer
        index = 0;
        memset(inputBuffer, 0, MAX_INPUT_LENGTH);
      } else {
        // Store the character in the buffer
        inputBuffer[index] = receivedChar;
        index++;

        // Check for buffer overflow
        if (index >= MAX_INPUT_LENGTH) {
          Serial.println("Input buffer overflow");
          index = 0;
          memset(inputBuffer, 0, MAX_INPUT_LENGTH);
        }
      }
    }

    // Delay to avoid high CPU usage
    vTaskDelay(10 / portTICK_PERIOD_MS);
  }

  // Free the allocated memory (this should never be reached)
  free(inputBuffer);
  vTaskDelete(NULL);
}

void setup() {
  Serial.begin(9600);
  xTaskCreatePinnedToCore(inputTask, "InputTask", 10000, NULL, 1, &inputTaskHandle,app_cpu);
}

void loop() {
  // Empty, all work is done in tasks
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK