From e0a50ade82fe00e56d221f9fff170f703d54edaf Mon Sep 17 00:00:00 2001 From: Yevhen Boichuk Date: Tue, 10 Nov 2026 15:01:01 +0200 Subject: [PATCH] feat: add examples for Lilka v2 extension modules Lua scripts for the new Keira modules (gps, can, ir, lora, nrf24, cc1101, imu, mag) in LUA/modules, and a C++ LoRa beacon using the LilkaModules SDK library in C_C++/lilka_modules/lora_beacon. --- C_C++/lilka_modules/lora_beacon/README.md | 13 +++++++ .../lilka_modules/lora_beacon/platformio.ini | 10 +++++ C_C++/lilka_modules/lora_beacon/src/main.cpp | 38 +++++++++++++++++++ LUA/modules/README.md | 25 ++++++++++++ LUA/modules/can.lua | 19 ++++++++++ LUA/modules/cc1101.lua | 21 ++++++++++ LUA/modules/gps.lua | 27 +++++++++++++ LUA/modules/imu.lua | 22 +++++++++++ LUA/modules/ir.lua | 33 ++++++++++++++++ LUA/modules/lora.lua | 28 ++++++++++++++ LUA/modules/mag.lua | 27 +++++++++++++ LUA/modules/nrf24.lua | 23 +++++++++++ 12 files changed, 286 insertions(+) create mode 100644 C_C++/lilka_modules/lora_beacon/README.md create mode 100644 C_C++/lilka_modules/lora_beacon/platformio.ini create mode 100644 C_C++/lilka_modules/lora_beacon/src/main.cpp create mode 100644 LUA/modules/README.md create mode 100644 LUA/modules/can.lua create mode 100644 LUA/modules/cc1101.lua create mode 100644 LUA/modules/gps.lua create mode 100644 LUA/modules/imu.lua create mode 100644 LUA/modules/ir.lua create mode 100644 LUA/modules/lora.lua create mode 100644 LUA/modules/mag.lua create mode 100644 LUA/modules/nrf24.lua diff --git a/C_C++/lilka_modules/lora_beacon/README.md b/C_C++/lilka_modules/lora_beacon/README.md new file mode 100644 index 0000000..55e7423 --- /dev/null +++ b/C_C++/lilka_modules/lora_beacon/README.md @@ -0,0 +1,13 @@ +LoRa-маяк (модуль 003 LORA) +=========================== + +Кожні 10 секунд надсилає пакет `LILKA #N` на 868.1 МГц і виводить у послідовний порт усі прийняті пакети +(RSSI, SNR). Використовує драйвер `lilka::mod::SX126x` з бібліотеки `LilkaModules`. + +Контакти модуля беруться з `lilka::config` (секція `[lora]`); змінити їх можна файлом у `/sd/.lilka/config/`. + +```sh +pio run -t upload && pio device monitor +``` + +Перевірте, які частоти й потужності дозволені у вашій країні, і не вмикайте передачу без антени. diff --git a/C_C++/lilka_modules/lora_beacon/platformio.ini b/C_C++/lilka_modules/lora_beacon/platformio.ini new file mode 100644 index 0000000..955d047 --- /dev/null +++ b/C_C++/lilka_modules/lora_beacon/platformio.ini @@ -0,0 +1,10 @@ +; LoRa-маяк для модуля 003 LORA (E22-900M, SX1262). +; Докладніше про конфігурацію PlatformIO: https://docs.platformio.org/en/latest/projectconf/index.html + +[env:default] +platform = espressif32 @ ^6.6.0 +board = lilka_v2 +framework = arduino +lib_deps = + lilka/Lilka + lilka/LilkaModules diff --git a/C_C++/lilka_modules/lora_beacon/src/main.cpp b/C_C++/lilka_modules/lora_beacon/src/main.cpp new file mode 100644 index 0000000..8bbe14e --- /dev/null +++ b/C_C++/lilka_modules/lora_beacon/src/main.cpp @@ -0,0 +1,38 @@ +// Sends a LoRa beacon every 10 s and prints received packets (003 LORA module, E22-900M). +#include +#include + +using namespace lilka::mod; + +SX126x radio; + +void setup() { + lilka::begin(); + // Pins come from lilka::config: built-in v2 values, overridable in /sd/.lilka/config/*.ini ([lora] ...) + lilka::SPI2.begin(pins::get("lora", "sck"), pins::get("lora", "miso"), pins::get("lora", "mosi")); + SX126x::Config cfg; // nss, busy, nrst, rxen, txen are read from [lora] too + cfg.spi = &lilka::SPI2; + cfg.frequencyHz = 868100000; // check what is allowed where you are + cfg.powerDbm = 0; + if (radio.begin(cfg) != SX126x::Result::OK) { + Serial.println("No SX126x found"); + return; + } + radio.startReceive(); +} + +void loop() { + static uint32_t lastTx = 0; + static uint32_t counter = 0; + SX126x::Packet packet; + if (radio.receive(packet)) { + Serial.printf("RX %.*s rssi=%.1f snr=%.1f\n", packet.length, (const char*)packet.data, packet.rssi, packet.snr); + } + if (millis() - lastTx > 10000) { + char msg[32]; + int n = snprintf(msg, sizeof(msg), "LILKA #%lu", (unsigned long)counter++); + radio.send(reinterpret_cast(msg), n); + lastTx = millis(); + } + vTaskDelay(pdMS_TO_TICKS(5)); +} diff --git a/LUA/modules/README.md b/LUA/modules/README.md new file mode 100644 index 0000000..ac64255 --- /dev/null +++ b/LUA/modules/README.md @@ -0,0 +1,25 @@ +# Модулі розширення Лілки v2 + +Lua-приклади для [модулів розширення](https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/modules/) (потрібна Keira з модулями `gps`, `can`, `ir`, `lora`, `nrf24`, `cc1101`, `imu`, `mag`). + +| Файл | Модуль | Що робить | +|---|---|---| +| [`gps.lua`](gps.lua) | 005 GPS | координати, висота, кількість супутників | +| [`can.lua`](can.lua) | 008 CAN | сніфер шини (режим `listen`, нічого не передає) | +| [`ir.lua`](ir.lua) | 002 IR | приймає сигнал пульта і повторює його кнопкою A | +| [`lora.lua`](lora.lua) | 003 LORA | чат між двома Лілками | +| [`nrf24.lua`](nrf24.lua) | 004 RF (nRF24L01+) | сканер діапазону 2.4 ГГц | +| [`cc1101.lua`](cc1101.lua) | 004 RF (CC1101) | індикатор сигналу на 433.92 МГц | +| [`imu.lua`](imu.lua) | 006 GYRO MAGNET | акселерометр і гіроскоп | +| [`mag.lua`](mag.lua) | 006 GYRO MAGNET | компас з калібруванням | + +Контакти модулів беруться з налаштувань Лілки; щоб змінити їх, покладіть INI-файл у `/sd/.lilka/config/`, +наприклад: + +```ini +; /sd/.lilka/config/modules.ini +[lora] +nss = 21 +``` + +Радіомодулі передають у ефір: під'єднайте антену і перевірте, які частоти й потужності дозволені у вашій країні. diff --git a/LUA/modules/can.lua b/LUA/modules/can.lua new file mode 100644 index 0000000..934966f --- /dev/null +++ b/LUA/modules/can.lua @@ -0,0 +1,19 @@ +--[[ + CAN-сніфер (модуль 008 CAN) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/can/ + Керування: B - вихід +--]] + +local bus = assert(can.new({bitrate = 500000, mode = "listen"})) + +function lilka.update(delta) + local msg = bus:receive() + while msg do + local hex = msg.data:gsub(".", function(c) return string.format("%02X ", c:byte()) end) + console.print(string.format("%03X [%d] %s", msg.id, msg.dlc, hex)) + msg = bus:receive() + end + if controller.get_state().b.just_pressed then + util.exit() + end +end diff --git a/LUA/modules/cc1101.lua b/LUA/modules/cc1101.lua new file mode 100644 index 0000000..f455558 --- /dev/null +++ b/LUA/modules/cc1101.lua @@ -0,0 +1,21 @@ +--[[ + Індикатор сигналу 433.92 МГц (модуль 004 RF, CC1101) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/cc1101/ + Керування: B - вихід +--]] + +local radio = assert(cc1101.new({frequency = 433.92})) + +function lilka.update(delta) + if controller.get_state().b.just_pressed then + util.exit() + end +end + +function lilka.draw() + local rssi = radio:rssi() + display.fill_screen(display.color565(0, 0, 0)) + display.set_cursor(10, 40) + display.print(string.format("%.1f dBm", rssi)) + display.fill_rect(10, 80, math.max(0, (rssi + 110) * 3), 20, display.color565(255, 128, 0)) +end diff --git a/LUA/modules/gps.lua b/LUA/modules/gps.lua new file mode 100644 index 0000000..cde14aa --- /dev/null +++ b/LUA/modules/gps.lua @@ -0,0 +1,27 @@ +--[[ + GPS: координати і супутники (модуль 005 GPS) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/gps/ + Керування: B - вихід +--]] + +local receiver = assert(gps.new()) + +function lilka.update(delta) + receiver:update() + if controller.get_state().b.just_pressed then + receiver:close() + util.exit() + end +end + +function lilka.draw() + local fix = receiver:fix() + display.fill_screen(display.color565(0, 0, 0)) + display.set_cursor(10, 40) + if fix.valid then + display.print(string.format("%.5f %.5f\n%.0f m, %d sat", fix.latitude, fix.longitude, + fix.altitude, fix.satellites)) + else + display.print("Шукаю супутники: " .. #receiver:satellites()) + end +end diff --git a/LUA/modules/imu.lua b/LUA/modules/imu.lua new file mode 100644 index 0000000..9c47235 --- /dev/null +++ b/LUA/modules/imu.lua @@ -0,0 +1,22 @@ +--[[ + Акселерометр і гіроскоп (модуль 006 GYRO MAGNET, BMI260) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/imu/ + Керування: B - вихід +--]] + +local sensor = assert(imu.new()) + +function lilka.update(delta) + if controller.get_state().b.just_pressed then + util.exit() + end +end + +function lilka.draw() + local d = sensor:read() + display.fill_screen(display.color565(0, 0, 0)) + display.set_cursor(10, 40) + if d then + display.print(string.format("a: %.2f %.2f %.2f g\ng: %.1f %.1f %.1f", d.ax, d.ay, d.az, d.gx, d.gy, d.gz)) + end +end diff --git a/LUA/modules/ir.lua b/LUA/modules/ir.lua new file mode 100644 index 0000000..f8308b2 --- /dev/null +++ b/LUA/modules/ir.lua @@ -0,0 +1,33 @@ +--[[ + Навчити й повторити ІЧ-пульт (модуль 002 IR) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/ir/ + Керування: A - повторити вивчений сигнал, B - вихід +--]] + +local remote = assert(ir.new()) +local learned = nil + +function lilka.update(delta) + local code = remote:receive() + if code and not code["repeat"] then + learned = code + end + local state = controller.get_state() + if state.a.just_pressed and learned then + remote:send_raw(learned.raw) + end + if state.b.just_pressed then + util.exit() + end +end + +function lilka.draw() + display.fill_screen(display.color565(0, 0, 0)) + display.set_cursor(10, 40) + if learned then + display.print(string.format("%s 0x%X 0x%X\nA - повторити", learned.protocol, + learned.address or 0, learned.command or 0)) + else + display.print("Натисніть кнопку на пульті") + end +end diff --git a/LUA/modules/lora.lua b/LUA/modules/lora.lua new file mode 100644 index 0000000..77c64b5 --- /dev/null +++ b/LUA/modules/lora.lua @@ -0,0 +1,28 @@ +--[[ + LoRa-чат між двома Лілками (модуль 003 LORA) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/lora/ + Керування: A - надіслати, B - вихід +--]] + +local radio = assert(lora.new({chip = "sx1262", frequency = 868100000, sf = 7})) +local last = "-" + +function lilka.update(delta) + local packet = radio:receive() + if packet then + last = string.format("%s (%.0f dBm, SNR %.1f)", packet.data, packet.rssi, packet.snr) + end + local state = controller.get_state() + if state.a.just_pressed then + radio:send("Привіт від Лілки!") + end + if state.b.just_pressed then + util.exit() + end +end + +function lilka.draw() + display.fill_screen(display.color565(0, 0, 0)) + display.set_cursor(10, 40) + display.print("A - надіслати\n" .. last) +end diff --git a/LUA/modules/mag.lua b/LUA/modules/mag.lua new file mode 100644 index 0000000..c467db4 --- /dev/null +++ b/LUA/modules/mag.lua @@ -0,0 +1,27 @@ +--[[ + Компас з калібруванням (модуль 006 GYRO MAGNET, TMAG5273) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/mag/ + Керування: A - почати/завершити калібрування, B - вихід +--]] + +local sensor = assert(mag.new()) +local calibrating = false + +function lilka.update(delta) + local state = controller.get_state() + if state.a.just_pressed then + calibrating = not calibrating + if calibrating then sensor:start_calibration() else sensor:stop_calibration() end + end + if state.b.just_pressed then + util.exit() + end +end + +function lilka.draw() + local heading = sensor:heading() + display.fill_screen(display.color565(0, 0, 0)) + display.set_cursor(10, 40) + display.print(calibrating and "Обертайте Лілку...\nA - готово" or + string.format("%.0f°\nA - калібрування", heading or 0)) +end diff --git a/LUA/modules/nrf24.lua b/LUA/modules/nrf24.lua new file mode 100644 index 0000000..6318356 --- /dev/null +++ b/LUA/modules/nrf24.lua @@ -0,0 +1,23 @@ +--[[ + Сканер діапазону 2.4 ГГц (модуль 004 RF, nRF24L01+) + Документація: https://docs.lilka.dev/projects/keira/uk/latest/lua/reference/nrf24/ + Керування: B - вихід +--]] + +local radio = assert(nrf24.new()) +local hits = {} + +function lilka.update(delta) + hits = radio:scan(2) + if controller.get_state().b.just_pressed then + util.exit() + end +end + +function lilka.draw() + display.fill_screen(display.color565(0, 0, 0)) + for ch = 1, #hits do + local h = hits[ch] * 40 + display.fill_rect(10 + ch * 2, 200 - h, 2, h, display.color565(0, 255, 0)) + end +end