Skip to content

Instantly share code, notes, and snippets.

@shikarunochi
Last active February 6, 2025 09:55
Show Gist options
  • Save shikarunochi/a871ccf8ebf8f4c8601c8f6250640451 to your computer and use it in GitHub Desktop.
Save shikarunochi/a871ccf8ebf8f4c8601c8f6250640451 to your computer and use it in GitHub Desktop.
M5Stack LLM Module に M5Facesキーボードから入力
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
* SPDX-License-Identifier: MIT
* M5Stack LLM Module で日本語対話。Serial MonitorでBoth BL&CRを設定するとよいです。
*
* 変更元プログラム:https://gist.github.com/ksasao/37425d3463013221e7fd0f9ae5ab1c62
* Faces keyboard対応 2025/01/25 @shikarunochi
*/
#include <Arduino.h>
#include <M5Unified.h>
#include <M5ModuleLLM.h>
#include <Wire.h>
#define KEYBOARD_I2C_ADDR 0X08
#define KEYBOARD_INT 5
M5ModuleLLM module_llm;
String llm_work_id;
void setup()
{
M5.begin();
Wire.begin();
pinMode(KEYBOARD_INT, INPUT_PULLUP);
M5.Display.setTextSize(1);
M5.Display.setTextScroll(true);
M5.Lcd.setTextFont(&fonts::efontJA_16);
/* Init module serial port */
Serial.begin(115200);
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();
Serial.print("Initialized...");
}
String inputString = "";
void loop()
{
if (Serial.available() > 0) {
String input = Serial.readString();
std::string question = input.c_str();
M5.Display.setTextColor(TFT_GREEN);
M5.Display.printf("<< %s\n", question.c_str());
Serial.printf("<< %s\n", question.c_str());
M5.Display.setTextColor(TFT_YELLOW);
M5.Display.printf(">> ");
Serial.printf(">> ");
/* Push question to LLM module and wait inference result */
module_llm.llm.inferenceAndWaitResult(llm_work_id, question.c_str(), [](String& result) {
/* Show result on screen */
M5.Display.printf("%s", result.c_str());
Serial.printf("%s", result.c_str());
});
M5.Display.println();
}
//https://github.com/m5stack/M5Stack/blob/master/examples/Face/KEYBOARD/KEYBOARD.ino
if (digitalRead(KEYBOARD_INT) == LOW) {
Wire.requestFrom(KEYBOARD_I2C_ADDR, 1); // request 1 byte from keyboard
M5.Display.setTextColor(TFT_GREEN);
while (Wire.available()) {
uint8_t key_val = Wire.read(); // receive a byte as character
if (key_val != 0) {
if(key_val != 13){//改行
if(inputString == ""){
M5.Display.print("<< ");
}
inputString = inputString + String((char)key_val);
M5.Display.printf("%c",key_val);
}else{
M5.Display.println();
M5.Display.setTextColor(TFT_YELLOW);
M5.Display.printf(">> ");
std::string question = inputString.c_str();
Serial.printf("%s\n",inputString.c_str());
module_llm.llm.inferenceAndWaitResult(llm_work_id, question.c_str(), [](String& result) {
/* Show result on screen */
M5.Display.printf("%s", result.c_str());
Serial.printf("%s", result.c_str());
});
M5.Display.println();
inputString = "";
}
}
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment