Skip to content

Commit

Permalink
Merge pull request #1 from m5stack/add-example-serial-text-assistant
Browse files Browse the repository at this point in the history
add serial text assistant example
  • Loading branch information
Forairaaaaa authored Oct 28, 2024
2 parents 2dbcf66 + 82d4d77 commit 027aa79
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions examples/SerialTextAssistant/SerialTextAssistant.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include <Arduino.h>
#include <M5Unified.h>
#include <M5ModuleLLM.h>

#define CommSerialPort Serial

M5ModuleLLM module_llm;
String llm_work_id;
String received_question;
bool question_ok;

void setup()
{
M5.begin();
M5.Display.setTextSize(2);
M5.Display.setTextScroll(true);

/* Init usb serial */
CommSerialPort.begin(115200);

/* Init module serial port */
Serial2.begin(115200, SERIAL_8N1, 16, 17); // Basic
// Serial2.begin(115200, SERIAL_8N1, 13, 14); // Core2
// Serial2.begin(115200, SERIAL_8N1, 18, 17); // CoreS3

/* Init module */
module_llm.begin(&Serial2);

/* Make sure module is connected */
M5.Display.printf(">> Check ModuleLLM connection..\n");
while (1) {
if (module_llm.checkConnection()) {
break;
}
}

/* Reset ModuleLLM */
M5.Display.printf(">> Reset ModuleLLM..\n");
module_llm.sys.reset();

/* Setup LLM module and save returned work id */
M5.Display.printf(">> Setup llm..\n");
llm_work_id = module_llm.llm.setup();

M5.Display.printf(">> Setup finish\n");
M5.Display.printf(">> Try send your question via usb serial port\n");
M5.Display.setTextColor(TFT_GREEN);
M5.Display.printf("e.g. \nHi, What's your name?\n");
M5.Display.printf("(end with CRLF \\r\\n)\n\n");
}

void loop()
{
/* Check comm serial port and get received question */
question_ok = false;
if (CommSerialPort.available()) {
while (CommSerialPort.available()) {
char in_char = (char)CommSerialPort.read();
received_question += in_char;

/* Check if question finish */
if (received_question.endsWith("\r\n")) {
received_question.remove(received_question.length() - 2);
question_ok = true;
break;
}
}
}

/* If question is ready */
if (question_ok) {
M5.Display.setTextColor(TFT_GREEN);
M5.Display.printf("<< %s\n", received_question.c_str());
M5.Display.setTextColor(TFT_YELLOW);
M5.Display.printf(">> ");
CommSerialPort.printf("<< \"%s\"\n", received_question.c_str());
CommSerialPort.print(">> ");

/* Push question to LLM module and wait inference result */
module_llm.llm.inferenceAndWaitResult(llm_work_id, received_question.c_str(), [](String& result) {
/* Show result on screen and usb serial */
M5.Display.printf("%s", result.c_str());
CommSerialPort.print(result);
});

/* Clear for next question */
received_question.clear();

M5.Display.println();
CommSerialPort.println();
}

delay(20);
}

0 comments on commit 027aa79

Please sign in to comment.