#include "RoomCardWidget.hpp" #include "../Fonts.hpp" #include "../WidgetManager.hpp" #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif RoomCardWidget::RoomCardWidget(const WidgetConfig& config) : Widget(config) { roomName_[0] = '\0'; tempFormat_[0] = '\0'; for (size_t i = 0; i < MAX_SUBBUTTONS; ++i) { subButtonStates_[i] = false; } } int RoomCardWidget::encodeUtf8(uint32_t codepoint, char* buf) { if (codepoint < 0x80) { buf[0] = static_cast(codepoint); buf[1] = '\0'; return 1; } else if (codepoint < 0x800) { buf[0] = static_cast(0xC0 | (codepoint >> 6)); buf[1] = static_cast(0x80 | (codepoint & 0x3F)); buf[2] = '\0'; return 2; } else if (codepoint < 0x10000) { buf[0] = static_cast(0xE0 | (codepoint >> 12)); buf[1] = static_cast(0x80 | ((codepoint >> 6) & 0x3F)); buf[2] = static_cast(0x80 | (codepoint & 0x3F)); buf[3] = '\0'; return 3; } else if (codepoint < 0x110000) { buf[0] = static_cast(0xF0 | (codepoint >> 18)); buf[1] = static_cast(0x80 | ((codepoint >> 12) & 0x3F)); buf[2] = static_cast(0x80 | ((codepoint >> 6) & 0x3F)); buf[3] = static_cast(0x80 | (codepoint & 0x3F)); buf[4] = '\0'; return 4; } buf[0] = '\0'; return 0; } void RoomCardWidget::parseText() { roomName_[0] = '\0'; tempFormat_[0] = '\0'; const char* text = config_.text; if (text && text[0] != '\0') { const char* newline = strchr(text, '\n'); if (newline) { size_t nameLen = static_cast(newline - text); if (nameLen >= MAX_TEXT_LEN) nameLen = MAX_TEXT_LEN - 1; memcpy(roomName_, text, nameLen); roomName_[nameLen] = '\0'; strncpy(tempFormat_, newline + 1, MAX_TEXT_LEN - 1); tempFormat_[MAX_TEXT_LEN - 1] = '\0'; } else { strncpy(roomName_, text, MAX_TEXT_LEN - 1); roomName_[MAX_TEXT_LEN - 1] = '\0'; } } } void RoomCardWidget::calculateSubButtonPosition(SubButtonPosition pos, int16_t& x, int16_t& y) { int16_t centerX = config_.width / 2; int16_t centerY = config_.height / 2; // Sub-button size (configurable, default 40) int16_t subBtnSize = config_.subButtonSize > 0 ? config_.subButtonSize : 40; // Sub-buttons orbit radius in pixels (default 80) int16_t orbitRadius = config_.subButtonDistance > 0 ? config_.subButtonDistance : 80; // Calculate angle: 0=TOP, going clockwise, 8 positions = 45 degrees each float angle = static_cast(static_cast(pos)) * (M_PI / 4.0f) - (M_PI / 2.0f); // Calculate position (center of sub-button) x = centerX + static_cast(orbitRadius * cosf(angle)) - subBtnSize / 2; y = centerY + static_cast(orbitRadius * sinf(angle)) - subBtnSize / 2; } lv_obj_t* RoomCardWidget::create(lv_obj_t* parent) { parseText(); // Create main container obj_ = lv_obj_create(parent); lv_obj_remove_style_all(obj_); lv_obj_set_pos(obj_, config_.x, config_.y); lv_obj_set_size(obj_, config_.width, config_.height); lv_obj_clear_flag(obj_, LV_OBJ_FLAG_SCROLLABLE); // Create central bubble first createCentralBubble(); // Create sub-buttons createSubButtons(); return obj_; } void RoomCardWidget::createCentralBubble() { // Calculate bubble size (80% of widget size, circular) int16_t minSide = config_.width < config_.height ? config_.width : config_.height; int16_t bubbleSize = (minSide * 80) / 100; // Create bubble container (centered) bubble_ = lv_obj_create(obj_); lv_obj_remove_style_all(bubble_); lv_obj_set_size(bubble_, bubbleSize, bubbleSize); lv_obj_center(bubble_); lv_obj_clear_flag(bubble_, LV_OBJ_FLAG_SCROLLABLE); lv_obj_add_flag(bubble_, LV_OBJ_FLAG_CLICKABLE); // Circular shape lv_obj_set_style_radius(bubble_, LV_RADIUS_CIRCLE, 0); lv_obj_set_style_bg_opa(bubble_, config_.bgOpacity, 0); lv_obj_set_style_bg_color(bubble_, lv_color_hex(config_.bgColor.toLvColor()), 0); // Set up flex layout for bubble content lv_obj_set_flex_flow(bubble_, LV_FLEX_FLOW_COLUMN); lv_obj_set_flex_align(bubble_, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_set_style_pad_all(bubble_, 8, 0); lv_obj_set_style_pad_gap(bubble_, 2, 0); // Room icon if (config_.iconCodepoint > 0 && Fonts::hasIconFont()) { roomIcon_ = lv_label_create(bubble_); lv_obj_clear_flag(roomIcon_, LV_OBJ_FLAG_CLICKABLE); char iconText[5]; encodeUtf8(config_.iconCodepoint, iconText); lv_label_set_text(roomIcon_, iconText); } // Temperature label tempLabel_ = lv_label_create(bubble_); lv_obj_clear_flag(tempLabel_, LV_OBJ_FLAG_CLICKABLE); lv_label_set_text(tempLabel_, tempFormat_[0] != '\0' ? "--" : ""); // Room name label if (roomName_[0] != '\0') { roomLabel_ = lv_label_create(bubble_); lv_obj_clear_flag(roomLabel_, LV_OBJ_FLAG_CLICKABLE); lv_label_set_text(roomLabel_, roomName_); } // Click handler for navigation lv_obj_add_event_cb(bubble_, bubbleClickCallback, LV_EVENT_CLICKED, this); } void RoomCardWidget::createSubButtons() { // Sub-button size (configurable, default 40) int16_t subBtnSize = config_.subButtonSize > 0 ? config_.subButtonSize : 40; for (uint8_t i = 0; i < config_.subButtonCount && i < MAX_SUBBUTTONS; ++i) { const SubButtonConfig& cfg = config_.subButtons[i]; if (!cfg.enabled) continue; // Create sub-button object lv_obj_t* btn = lv_obj_create(obj_); lv_obj_remove_style_all(btn); lv_obj_set_size(btn, subBtnSize, subBtnSize); lv_obj_clear_flag(btn, LV_OBJ_FLAG_SCROLLABLE); lv_obj_add_flag(btn, LV_OBJ_FLAG_CLICKABLE); // Circular shape lv_obj_set_style_radius(btn, LV_RADIUS_CIRCLE, 0); lv_obj_set_style_bg_opa(btn, 255, 0); // Position using circle geometry int16_t x, y; calculateSubButtonPosition(cfg.position, x, y); lv_obj_set_pos(btn, x, y); // Create icon if (cfg.iconCodepoint > 0 && Fonts::hasIconFont()) { lv_obj_t* icon = lv_label_create(btn); lv_obj_clear_flag(icon, LV_OBJ_FLAG_CLICKABLE); char iconText[5]; encodeUtf8(cfg.iconCodepoint, iconText); lv_label_set_text(icon, iconText); lv_obj_center(icon); subButtonIcons_[i] = icon; } // Store index in user_data for click handler lv_obj_set_user_data(btn, reinterpret_cast(static_cast(i))); lv_obj_add_event_cb(btn, subButtonClickCallback, LV_EVENT_CLICKED, this); subButtonObjs_[i] = btn; subButtonStates_[i] = false; // Apply initial color (OFF state) updateSubButtonColor(i); } } void RoomCardWidget::applyStyle() { if (!obj_) return; // Apply shadow to bubble if enabled if (bubble_ && config_.shadow.enabled) { lv_obj_set_style_shadow_width(bubble_, config_.shadow.blur, 0); lv_obj_set_style_shadow_ofs_x(bubble_, config_.shadow.offsetX, 0); lv_obj_set_style_shadow_ofs_y(bubble_, config_.shadow.offsetY, 0); lv_obj_set_style_shadow_color(bubble_, lv_color_hex(config_.shadow.color.toLvColor()), 0); lv_obj_set_style_shadow_opa(bubble_, 255, 0); } // Font sizes const lv_font_t* iconFont = Fonts::iconFont(config_.iconSize); const lv_font_t* textFont = Fonts::bySizeIndex(config_.fontSize); const lv_font_t* labelFont = Fonts::bySizeIndex(config_.fontSize > 0 ? config_.fontSize - 1 : 0); lv_color_t textColor = lv_color_hex(config_.textColor.toLvColor()); // Style room icon if (roomIcon_ && iconFont) { lv_obj_set_style_text_font(roomIcon_, iconFont, 0); lv_obj_set_style_text_color(roomIcon_, textColor, 0); } // Style temperature if (tempLabel_ && textFont) { lv_obj_set_style_text_font(tempLabel_, textFont, 0); lv_obj_set_style_text_color(tempLabel_, textColor, 0); } // Style room label if (roomLabel_ && labelFont) { lv_obj_set_style_text_font(roomLabel_, labelFont, 0); lv_obj_set_style_text_color(roomLabel_, lv_color_hex(config_.textColor.toLvColor()), 0); lv_obj_set_style_text_opa(roomLabel_, 180, 0); // Slightly dimmed } // Style sub-buttons - adjust icon size based on button size // subButtonSize: 30-40 -> font 1, 41-55 -> font 2, 56+ -> font 3 uint8_t subBtnFontIdx = 1; // Default small if (config_.subButtonSize > 55) { subBtnFontIdx = 3; } else if (config_.subButtonSize > 40) { subBtnFontIdx = 2; } const lv_font_t* subBtnIconFont = Fonts::iconFont(subBtnFontIdx); for (uint8_t i = 0; i < config_.subButtonCount && i < MAX_SUBBUTTONS; ++i) { if (subButtonIcons_[i] && subBtnIconFont) { lv_obj_set_style_text_font(subButtonIcons_[i], subBtnIconFont, 0); lv_obj_set_style_text_color(subButtonIcons_[i], lv_color_white(), 0); } } } void RoomCardWidget::updateSubButtonColor(uint8_t index) { if (index >= MAX_SUBBUTTONS || !subButtonObjs_[index]) return; const SubButtonConfig& cfg = config_.subButtons[index]; const Color& color = subButtonStates_[index] ? cfg.colorOn : cfg.colorOff; lv_obj_set_style_bg_color(subButtonObjs_[index], lv_color_hex(color.toLvColor()), 0); } void RoomCardWidget::updateTemperature(float value) { if (!tempLabel_ || tempFormat_[0] == '\0') return; char buf[32]; snprintf(buf, sizeof(buf), tempFormat_, value); lv_label_set_text(tempLabel_, buf); } void RoomCardWidget::onKnxValue(float value) { cachedPrimaryValue_ = value; hasCachedPrimary_ = true; updateTemperature(value); } void RoomCardWidget::onKnxSwitch(bool value) { // Not used directly - sub-button status is handled via onSubButtonStatus (void)value; } void RoomCardWidget::onSubButtonStatus(uint8_t index, bool value) { if (index >= MAX_SUBBUTTONS) return; subButtonStates_[index] = value; updateSubButtonColor(index); } void RoomCardWidget::sendSubButtonToggle(uint8_t index) { if (index >= config_.subButtonCount || index >= MAX_SUBBUTTONS) return; const SubButtonConfig& cfg = config_.subButtons[index]; if (cfg.action == SubButtonAction::TOGGLE_KNX && cfg.knxAddrWrite > 0) { bool newState = !subButtonStates_[index]; // Send KNX toggle via WidgetManager WidgetManager::instance().sendKnxSwitch(cfg.knxAddrWrite, newState); // Optimistically update local state subButtonStates_[index] = newState; updateSubButtonColor(index); } } void RoomCardWidget::bubbleClickCallback(lv_event_t* e) { RoomCardWidget* widget = static_cast(lv_event_get_user_data(e)); if (!widget) return; // Handle navigation based on action if (widget->config_.action == ButtonAction::JUMP) { WidgetManager::instance().navigateToScreen(widget->config_.targetScreen); } else if (widget->config_.action == ButtonAction::BACK) { WidgetManager::instance().navigateBack(); } } void RoomCardWidget::subButtonClickCallback(lv_event_t* e) { RoomCardWidget* widget = static_cast(lv_event_get_user_data(e)); lv_obj_t* target = static_cast(lv_event_get_target(e)); if (!widget || !target) return; uint8_t index = static_cast(reinterpret_cast(lv_obj_get_user_data(target))); if (index < widget->config_.subButtonCount && index < MAX_SUBBUTTONS) { const SubButtonConfig& cfg = widget->config_.subButtons[index]; if (cfg.action == SubButtonAction::TOGGLE_KNX) { widget->sendSubButtonToggle(index); } else if (cfg.action == SubButtonAction::NAVIGATE) { WidgetManager::instance().navigateToScreen(cfg.targetScreen); } } }