본문으로 건너뛰기
cccskills
무료GitHub에 공개

lua-integration

Lua scripting context for the Megahub project. Use when working on Lua script execution, Lua bindings, libluahub, Lua error handling, Lua threads, or the Blockly-to-Lua code generation pipeline.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

설치 방법 보기

포함된 파일(1)

  • SKILL.md2.9 KB

SKILL.md(원문)

설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.

Lua Integration — Megahub ESP32

Overview

  • Lua 5.4 embedded interpreter
  • All custom Lua bindings: lib/megahub/src/libluahub.cpp
  • Lua state created in lib/megahub/src/megahub.cpp → newLuaState()
  • Scripts stored on LittleFS/SD, loaded on demand
  • Execution triggered via BLE command (app request type 6 = execute-lua)

Execution Flow

BLE request (type 6, Lua code payload)
  → megahub.cpp: executeLUACode()
      → newLuaState()       (~20ms, opens standard libs + registers custom bindings)
      → lua_pcall()         (executes the script)
      → cleanup Lua state

From real device logs: execution of a motor control script completes in ~62ms.


Lua Bindings (libluahub.cpp)

Key functions available in Lua scripts:

Lua functionPurpose
hub.init(fn)Register initialization block (runs first)
hub.startthread(name, fn, stackSize)Start a FreeRTOS task running a Lua coroutine

Threads: hub.startthread() creates a FreeRTOS task. Stack size is specified in bytes (e.g., 4096). The thread runs as a Lua coroutine and should yield periodically.


Critical Rules

  • Always wrap Lua execution in lua_pcall() — never use lua_call() in production code
  • Always install an error handler before pcall to get useful error messages
  • Never let Lua errors propagate unhandled — they will crash the firmware
  • Lua threads via hub.startthread() consume both FreeRTOS stack and Lua VM heap
  • The Lua VM runs in the main heap; large scripts reduce heap available for BLE, LEGO, etc.
  • Long-running scripts must yield periodically — a tight Lua loop will starve other tasks

Error Handling Pattern

// Safe Lua execution (in megahub.cpp)
lua_State* L = newLuaState();
if (!L) { ERROR("Failed to create Lua state"); return; }

int result = lua_pcall(L, 0, 0, 0);
if (result != LUA_OK) {
    ERROR("Lua error: %s", lua_tostring(L, -1));
    lua_pop(L, 1);
}
// Always close state when done
lua_close(L);

Blockly → Lua Pipeline

  1. User builds program in Blockly editor (browser)
  2. Blockly generates Lua code (custom generators in frontend/src/components/blockly/)
  3. Lua code sent to firmware via BLE (streaming upload or direct execute)
  4. Firmware executes via executeLUACode()

The generated Lua code always follows a structure with hub.init() and optionally hub.startthread() for background monitoring loops.


Memory Impact

From device logs:

  • Creating a new Lua state: ~30KB heap consumed (~170KB → ~140KB)
  • A script with hub.startthread("Device monitor", fn, 4096) adds another ~4KB stack + Lua coroutine overhead

리뷰

아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.

댓글

로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.

아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.

보내면 바로 공개됩니다. 이메일 주소나 전화번호 같은 개인 정보는 쓰지 마세요. URL은 2개까지입니다.

0 / 1,000자

이 양식은 reCAPTCHA로 보호되며 Google 개인정보처리방침 및 서비스 약관이 적용됩니다.

같은 저장소의 스킬

ble

무료

Bluetooth context for the Megahub project. Use when working on BLE GATT server, Classic Bluetooth HID host, Web Bluetooth API integration, btremote library, connection handling, or any Bluetooth-related firmware code.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

mirkosertic/Megahub72026년 10월 18일 업데이트

blockly

무료

Blockly visual programming context for the Megahub project. Use when adding, modifying, or debugging custom Blockly blocks, the Lua code generator, the toolbox, block colors, or the BLOCKS.md documentation generation script.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

mirkosertic/Megahub72026년 10월 18일 업데이트

Documentation structure and maintenance rules for the Megahub project. Use whenever making user-facing changes — adding features, changing behavior, fixing hardware compatibility, or modifying configuration. Describes which files to update, the README structure, and writing style.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

mirkosertic/Megahub72026년 10월 18일 업데이트

Core firmware context for the Megahub ESP32 project. Use when working on PlatformIO configuration, build scripts, library structure, main.cpp, or general ESP32 firmware development. Covers build system, project structure, common pitfalls, and debugging.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

mirkosertic/Megahub72026년 10월 18일 업데이트

frontend

무료

Frontend development context for the Megahub project. Use when working on the web-based IDE, Vite build system, Blockly visual programming, Web Bluetooth API integration, JavaScript frontend code, CSS/styles, UI components, or anything in the /frontend/ directory.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

mirkosertic/Megahub72026년 10월 18일 업데이트

LEGO Powered Up protocol context for the Megahub project. Use when working on LEGO device communication, lpfuart library, UART parsing, motor control, sensor modes, device initialization, or debugging UART noise/corruption issues.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

mirkosertic/Megahub72026년 10월 18일 업데이트

mirkosertic의 스킬 모두 보기

이 스킬의 문제 신고