Last active
March 26, 2025 22:11
-
-
Save maxbrito500/63a2340645fd37803d4298cefc9a3931 to your computer and use it in GitHub Desktop.
Chat application sample for Tactility using ESPnow. Attention: ESPnow initialization is failing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Tactility/app/AppManifest.h> | |
#include <Tactility/lvgl/Toolbar.h> | |
#include <lvgl.h> | |
#include <cstdio> | |
#include <cstring> | |
using namespace tt::app; | |
class ChatApp : public App { | |
lv_obj_t* msg_list = nullptr; | |
static void send_quick_msg(lv_event_t* e) { | |
ChatApp* self = static_cast<ChatApp*>(lv_event_get_user_data(e)); | |
lv_obj_t* btn = static_cast<lv_obj_t*>(lv_event_get_target(e)); | |
const char* msg = lv_label_get_text(lv_obj_get_child(btn, 0)); | |
if (self->msg_list && msg) { | |
lv_obj_t* msg_label = lv_label_create(self->msg_list); | |
lv_label_set_text(msg_label, msg); | |
lv_obj_set_width(msg_label, lv_pct(100)); | |
lv_label_set_long_mode(msg_label, LV_LABEL_LONG_WRAP); | |
lv_obj_set_style_text_align(msg_label, LV_TEXT_ALIGN_LEFT, 0); | |
lv_obj_set_style_pad_all(msg_label, 2, 0); | |
lv_obj_scroll_to_y(self->msg_list, lv_obj_get_scroll_y(self->msg_list) + 20, LV_ANIM_ON); | |
} | |
} | |
static void send_message(lv_event_t* e) { | |
ChatApp* self = static_cast<ChatApp*>(lv_event_get_user_data(e)); | |
lv_obj_t* ta = static_cast<lv_obj_t*>(lv_event_get_target(e)); | |
const char* msg = lv_textarea_get_text(ta); | |
if (self->msg_list && msg && strlen(msg) > 0) { | |
lv_obj_t* msg_label = lv_label_create(self->msg_list); | |
lv_label_set_text(msg_label, msg); | |
lv_obj_set_width(msg_label, lv_pct(100)); | |
lv_label_set_long_mode(msg_label, LV_LABEL_LONG_WRAP); | |
lv_obj_set_style_text_align(msg_label, LV_TEXT_ALIGN_LEFT, 0); | |
lv_obj_set_style_pad_all(msg_label, 2, 0); | |
lv_obj_scroll_to_y(self->msg_list, lv_obj_get_scroll_y(self->msg_list) + 20, LV_ANIM_ON); | |
lv_textarea_set_text(ta, ""); | |
} | |
} | |
public: | |
void onShow(AppContext& context, lv_obj_t* parent) override { | |
// Create toolbar | |
lv_obj_t* toolbar = tt::lvgl::toolbar_create(parent, context); | |
lv_obj_align(toolbar, LV_ALIGN_TOP_MID, 0, 0); | |
const int toolbar_height = lv_obj_get_height(toolbar); | |
// Message list | |
msg_list = lv_list_create(parent); | |
lv_obj_set_size(msg_list, lv_pct(75), lv_pct(43)); | |
lv_obj_align(msg_list, LV_ALIGN_TOP_LEFT, 5, toolbar_height + 45); | |
lv_obj_set_style_bg_color(msg_list, lv_color_hex(0xEEEEEE), 0); | |
lv_obj_set_style_border_width(msg_list, 1, 0); | |
lv_obj_set_style_pad_all(msg_list, 5, 0); | |
// Quick message panel | |
lv_obj_t* quick_panel = lv_obj_create(parent); | |
lv_obj_set_size(quick_panel, lv_pct(20), lv_pct(58)); | |
lv_obj_align(quick_panel, LV_ALIGN_TOP_RIGHT, -5, toolbar_height + 15); // Adjusted to match | |
lv_obj_set_flex_flow(quick_panel, LV_FLEX_FLOW_COLUMN); | |
lv_obj_set_flex_align(quick_panel, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); | |
lv_obj_set_style_pad_all(quick_panel, 5, 0); | |
// Quick message buttons | |
const char* quick_msgs[] = {":-)", "+1", ":-("}; | |
for (size_t i = 0; i < sizeof(quick_msgs)/sizeof(quick_msgs[0]); i++) { | |
lv_obj_t* btn = lv_btn_create(quick_panel); | |
lv_obj_set_size(btn, lv_pct(75), 25); | |
lv_obj_add_event_cb(btn, send_quick_msg, LV_EVENT_CLICKED, this); | |
lv_obj_t* label = lv_label_create(btn); | |
lv_label_set_text(label, quick_msgs[i]); | |
lv_obj_center(label); | |
} | |
// Input panel | |
lv_obj_t* input_panel = lv_obj_create(parent); | |
lv_obj_set_size(input_panel, lv_pct(95), 60); | |
lv_obj_align(input_panel, LV_ALIGN_BOTTOM_MID, 0, -5); | |
lv_obj_set_flex_flow(input_panel, LV_FLEX_FLOW_ROW); | |
lv_obj_set_flex_align(input_panel, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); | |
lv_obj_set_style_pad_all(input_panel, 5, 0); | |
// Input field | |
lv_obj_t* input_field = lv_textarea_create(input_panel); | |
lv_obj_set_flex_grow(input_field, 1); | |
lv_obj_set_height(input_field, LV_PCT(100)); | |
lv_textarea_set_placeholder_text(input_field, "Type a message..."); | |
lv_textarea_set_one_line(input_field, true); | |
// Send button | |
lv_obj_t* send_btn = lv_btn_create(input_panel); | |
lv_obj_set_size(send_btn, 80, LV_PCT(100)); | |
lv_obj_add_event_cb(send_btn, send_message, LV_EVENT_CLICKED, this); | |
lv_obj_t* btn_label = lv_label_create(send_btn); | |
lv_label_set_text(btn_label, "Send"); | |
lv_obj_center(btn_label); | |
} | |
~ChatApp() override = default; | |
}; | |
extern const AppManifest chat_app = { | |
.id = "Chat", | |
.name = "Chat", | |
.createApp = []() -> std::shared_ptr<App> { return std::make_shared<ChatApp>(); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment