Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ai_hello/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
if(CONFIG_AI_HELLO)
nuttx_add_application(
MODULE ${CONFIG_AI_HELLO}
NAME ${CONFIG_AI_HELLO_PROGNAME}
SRCS ai_hello_main.c
INCLUDE_DIRECTORIES ${NUTTX_APPS_DIR}/packages/ai_agent/include
STACKSIZE ${CONFIG_AI_HELLO_STACKSIZE}
PRIORITY ${CONFIG_AI_HELLO_PRIORITY})
endif()
31 changes: 31 additions & 0 deletions ai_hello/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config AI_HELLO
tristate "AI hello example (LVGL + velaclaw)"
default n
depends on GRAPHICS_LVGL && EXAMPLES_AI_AGENT_VELA
---help---
Tap a preset question on the screen, ask ai_agent through the
velaclaw client SDK and show the reply.

if AI_HELLO

config AI_HELLO_PROGNAME
string "Program name"
default "ai_hello"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.

config AI_HELLO_PRIORITY
int "AI hello task priority"
default 100

config AI_HELLO_STACKSIZE
int "AI hello stack size"
default 40960

endif
3 changes: 3 additions & 0 deletions ai_hello/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ifneq ($(CONFIG_AI_HELLO),)
CONFIGURED_APPS += $(APPDIR)/packages/demos/ai_hello
endif
12 changes: 12 additions & 0 deletions ai_hello/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include $(APPDIR)/Make.defs

PROGNAME = $(CONFIG_AI_HELLO_PROGNAME)
PRIORITY = $(CONFIG_AI_HELLO_PRIORITY)
STACKSIZE = $(CONFIG_AI_HELLO_STACKSIZE)
MODULE = $(CONFIG_AI_HELLO)

CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/packages/ai_agent/include

MAINSRC = ai_hello_main.c

include $(APPDIR)/Application.mk
307 changes: 307 additions & 0 deletions ai_hello/ai_hello_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
/****************************************************************************
* Copyright (C) 2026 Xiaomi Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include <lvgl/lvgl.h>
#include <velaclaw/client.h>

#ifdef CONFIG_LV_USE_NUTTX_LIBUV
# include <uv.h>
#endif

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define FONT_BOARD "/resource/fonts/MiSans-Normal.ttf"
#define FONT_EMULATOR "/data/font/MiSans-Normal.ttf"
#define FONT_SIZE 22
#define PERSONA_FILE "/data/ai_hello_persona.txt"
#define PERSONA_MAX 256
#define REPLY_MAX 1024
#define ASK_TIMEOUT_MS 30000

/****************************************************************************
* Private Data
****************************************************************************/

static FAR const char *g_questions[] =
{
"Introduce yourself in one sentence",
"Tell me a short joke",
"Is today good for a walk? Any advice?",
};

static FAR const char *g_default_persona =
"You are a friendly assistant. Answer in Chinese, "
"within 60 characters.";

static FAR velaclaw_client_t *g_client;
static FAR lv_obj_t *g_status;
static FAR lv_obj_t *g_reply;
static bool g_busy;
static uint32_t g_ask_tick;

/* The reply arrives on an ai_agent thread, but lv_* may only be called
* from the LVGL thread, so the callback hands the text over here.
*/

static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
static char g_pending[REPLY_MAX];
static bool g_has_pending;

/****************************************************************************
* Private Functions
****************************************************************************/

static void ask_done_cb(int status, FAR const char *text, FAR void *arg)
{
syslog(LOG_INFO, "ai_hello: reply(%d): %s\n", status,
text ? text : "(null)");

pthread_mutex_lock(&g_lock);
snprintf(g_pending, sizeof(g_pending), "%s",
status == 0 && text ? text : "(no reply)");
g_has_pending = true;
pthread_mutex_unlock(&g_lock);
}

/* Re-read on every ask, so the persona can be changed with `echo` while
* the app keeps running (an LVGL app cannot be restarted in a flat build).
*/

static void load_persona(FAR char *buf, size_t size)
{
FAR FILE *fp;
size_t len = 0;

fp = fopen(PERSONA_FILE, "r");
if (fp != NULL)
{
len = fread(buf, 1, size - 1, fp);
fclose(fp);
}

while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
{
len--;
}

buf[len] = '\0';
if (len == 0)
{
snprintf(buf, size, "%s", g_default_persona);
}
}

static void ask(FAR const char *question)
{
velaclaw_ask_req_t req;
char persona[PERSONA_MAX];
char prompt[512];

if (g_busy)
{
return;
}

if (g_client == NULL)
{
g_client = velaclaw_client_open("ai_hello");
if (g_client == NULL)
{
lv_label_set_text(g_status, "Cannot connect to ai_agent");
return;
}
}

load_persona(persona, sizeof(persona));
snprintf(prompt, sizeof(prompt), "%s\nQuestion: %s", persona, question);
memset(&req, 0, sizeof(req));
req.text = prompt;

if (velaclaw_ask(g_client, &req, ask_done_cb, NULL) < 0)
{
lv_label_set_text(g_status, "Send failed. Is ai_agent running?");
return;
}

syslog(LOG_INFO, "ai_hello: ask: %s\n", question);
g_busy = true;
g_ask_tick = lv_tick_get();
lv_label_set_text(g_status, "Thinking...");
lv_label_set_text(g_reply, question);
}

static void question_clicked_cb(FAR lv_event_t *e)
{
ask(lv_event_get_user_data(e));
}

/* velaclaw ignores req.timeout_ms and never reports a timeout, so the
* app has to give up by itself (e.g. ai_agent not started).
*/

static void poll_reply_cb(FAR lv_timer_t *timer)
{
pthread_mutex_lock(&g_lock);
if (g_has_pending)
{
g_has_pending = false;
g_busy = false;
lv_label_set_text(g_reply, g_pending);
lv_label_set_text(g_status, "Tap a question");
}

pthread_mutex_unlock(&g_lock);

if (g_busy && lv_tick_elaps(g_ask_tick) > ASK_TIMEOUT_MS)
{
g_busy = false;
lv_label_set_text(g_status, "Timeout: check ai_agent, "
"network and API key");
}
}

static void create_ui(FAR lv_font_t *font)
{
FAR lv_obj_t *scr = lv_screen_active();
FAR lv_obj_t *btn;
FAR lv_obj_t *label;
int i;

lv_obj_set_flex_flow(scr, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_all(scr, 16, 0);
if (font != NULL)
{
lv_obj_set_style_text_font(scr, font, 0);
}

g_status = lv_label_create(scr);
lv_label_set_text(g_status, "Tap a question");

for (i = 0; i < sizeof(g_questions) / sizeof(g_questions[0]); i++)
{
btn = lv_button_create(scr);
lv_obj_set_width(btn, lv_pct(100));
lv_obj_add_event_cb(btn, question_clicked_cb, LV_EVENT_CLICKED,
(FAR void *)g_questions[i]);

label = lv_label_create(btn);
lv_label_set_text(label, g_questions[i]);
}

g_reply = lv_label_create(scr);
lv_obj_set_width(g_reply, lv_pct(100));
lv_obj_set_flex_grow(g_reply, 1);
lv_label_set_long_mode(g_reply, LV_LABEL_LONG_WRAP);
lv_label_set_text(g_reply, "");

lv_timer_create(poll_reply_cb, 100, NULL);
}

static FAR lv_font_t *load_font(void)
{
FAR const char *path;
FAR lv_font_t *font;

path = access(FONT_BOARD, F_OK) == 0 ? FONT_BOARD : FONT_EMULATOR;
font = lv_freetype_font_create(path, LV_FREETYPE_FONT_RENDER_MODE_BITMAP,
FONT_SIZE, LV_FREETYPE_FONT_STYLE_NORMAL);
if (font == NULL)
{
syslog(LOG_WARNING, "ai_hello: no CJK font, Chinese shows as "
"boxes\n");
}

return font;
}

/****************************************************************************
* Public Functions
****************************************************************************/

/* Usage: ai_hello [question]
* question - sent once at startup, handy for testing without touch
*/

int main(int argc, FAR char *argv[])
{
lv_nuttx_dsc_t info;
lv_nuttx_result_t result;
#ifdef CONFIG_LV_USE_NUTTX_LIBUV
uv_loop_t loop;
lv_nuttx_uv_t uv_info;
FAR void *data;
#endif

/* Flat build: LVGL is global, only one GUI app can own it. */

if (lv_is_initialized())
{
printf("LVGL is already in use (lvgldemo running?)\n");
return 1;
}

lv_init();
lv_nuttx_dsc_init(&info);
lv_nuttx_init(&info, &result);
if (result.disp == NULL)
{
printf("display init failed\n");
lv_deinit();
return 1;
}

create_ui(load_font());

if (argc > 1)
{
ask(argv[1]);
}

#ifdef CONFIG_LV_USE_NUTTX_LIBUV
memset(&loop, 0, sizeof(loop));
uv_loop_init(&loop);
memset(&uv_info, 0, sizeof(uv_info));
uv_info.loop = &loop;
uv_info.disp = result.disp;
uv_info.indev = result.indev;
data = lv_nuttx_uv_init(&uv_info);
uv_run(&loop, UV_RUN_DEFAULT);
lv_nuttx_uv_deinit(&data);
#else
while (1)
{
uint32_t idle = lv_timer_handler();
usleep((idle ? idle : 1) * 1000);
}
#endif

return 0;
}
Loading