From c058c006b7b7916c298e9ac756f943d0c3b734ca Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Thu, 1 Jan 2026 21:52:59 +0100 Subject: [PATCH] Base directory structure, lib module --- firmware/apps/leader/CMakeLists.txt | 10 ++ firmware/apps/leader/prj.conf | 16 +++ firmware/apps/leader/src/main.c | 19 +++ firmware/apps/weapon/CMakeLists.txt | 9 ++ firmware/apps/weapon/prj.conf | 1 + firmware/libs/CMakeLists.txt | 2 + firmware/libs/Kconfig | 4 + firmware/libs/lasertag_utils/CMakeLists.txt | 10 ++ firmware/libs/lasertag_utils/Kconfig | 19 +++ .../lasertag_utils/include/lasertag_utils.h | 34 +++++ .../libs/lasertag_utils/src/lasertag_utils.c | 124 ++++++++++++++++++ firmware/libs/zephyr/module.yml | 6 + 12 files changed, 254 insertions(+) create mode 100644 firmware/apps/leader/CMakeLists.txt create mode 100644 firmware/apps/leader/prj.conf create mode 100644 firmware/apps/leader/src/main.c create mode 100644 firmware/apps/weapon/CMakeLists.txt create mode 100644 firmware/apps/weapon/prj.conf create mode 100644 firmware/libs/CMakeLists.txt create mode 100644 firmware/libs/Kconfig create mode 100644 firmware/libs/lasertag_utils/CMakeLists.txt create mode 100644 firmware/libs/lasertag_utils/Kconfig create mode 100644 firmware/libs/lasertag_utils/include/lasertag_utils.h create mode 100644 firmware/libs/lasertag_utils/src/lasertag_utils.c create mode 100644 firmware/libs/zephyr/module.yml diff --git a/firmware/apps/leader/CMakeLists.txt b/firmware/apps/leader/CMakeLists.txt new file mode 100644 index 0000000..a2daf85 --- /dev/null +++ b/firmware/apps/leader/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.20) + +# Tell Zephyr to look into our libs folder for extra modules +list(APPEND ZEPHYR_EXTRA_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/../../libs) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(lasertag_leader) + +# Define application source files +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/firmware/apps/leader/prj.conf b/firmware/apps/leader/prj.conf new file mode 100644 index 0000000..4b84810 --- /dev/null +++ b/firmware/apps/leader/prj.conf @@ -0,0 +1,16 @@ +CONFIG_LOG=y + +# Shell and Built-in Commands +CONFIG_SHELL=y +CONFIG_KERNEL_SHELL=y +CONFIG_DEVICE_SHELL=y +CONFIG_REBOOT=y + +# Storage and Settings (NVS) +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_NVS=y +CONFIG_SETTINGS=y + +# Enable Lasertag Shared Modules +CONFIG_LASERTAG_UTILS=y \ No newline at end of file diff --git a/firmware/apps/leader/src/main.c b/firmware/apps/leader/src/main.c new file mode 100644 index 0000000..51b8235 --- /dev/null +++ b/firmware/apps/leader/src/main.c @@ -0,0 +1,19 @@ +#include +#include +#include + +LOG_MODULE_REGISTER(leader_app, CONFIG_LOG_DEFAULT_LEVEL); +int main(void) +{ + /* Initialize shared project logic */ + lasertag_utils_init(); + + LOG_INF("Leader Application successfully started.\n"); + + while (1) { + /* Main loop - keep process alive */ + k_sleep(K_MSEC(1000)); + } + + return 0; +} \ No newline at end of file diff --git a/firmware/apps/weapon/CMakeLists.txt b/firmware/apps/weapon/CMakeLists.txt new file mode 100644 index 0000000..f25e480 --- /dev/null +++ b/firmware/apps/weapon/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.20) + +# Zephyr mitteilen, dass unsere Libs Teil des Projekts sind +list(APPEND ZEPHYR_EXTRA_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/../../libs) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(lasertag_weapon) + +target_sources(app PRIVATE src/main.c) \ No newline at end of file diff --git a/firmware/apps/weapon/prj.conf b/firmware/apps/weapon/prj.conf new file mode 100644 index 0000000..4b3c3e3 --- /dev/null +++ b/firmware/apps/weapon/prj.conf @@ -0,0 +1 @@ +CONFIG_LASERTAG_UTILS=y diff --git a/firmware/libs/CMakeLists.txt b/firmware/libs/CMakeLists.txt new file mode 100644 index 0000000..1874118 --- /dev/null +++ b/firmware/libs/CMakeLists.txt @@ -0,0 +1,2 @@ +# Add library subdirectories +add_subdirectory(lasertag_utils) \ No newline at end of file diff --git a/firmware/libs/Kconfig b/firmware/libs/Kconfig new file mode 100644 index 0000000..6fac1d9 --- /dev/null +++ b/firmware/libs/Kconfig @@ -0,0 +1,4 @@ +# Main entry point for custom project Kconfigs +# This file is included by the application's Kconfig + +rsource "lasertag_utils/Kconfig" \ No newline at end of file diff --git a/firmware/libs/lasertag_utils/CMakeLists.txt b/firmware/libs/lasertag_utils/CMakeLists.txt new file mode 100644 index 0000000..1d43725 --- /dev/null +++ b/firmware/libs/lasertag_utils/CMakeLists.txt @@ -0,0 +1,10 @@ +if(CONFIG_LASERTAG_UTILS) + # Register this as a Zephyr library + zephyr_library() + + # Add source files + zephyr_library_sources(src/lasertag_utils.c) + + # Export the include directory to all applications + zephyr_include_directories(include) +endif() \ No newline at end of file diff --git a/firmware/libs/lasertag_utils/Kconfig b/firmware/libs/lasertag_utils/Kconfig new file mode 100644 index 0000000..36266bb --- /dev/null +++ b/firmware/libs/lasertag_utils/Kconfig @@ -0,0 +1,19 @@ +menuconfig LASERTAG_UTILS + bool "Lasertag Utilities" + help + Enable shared logic for all lasertag devices (Vest, Weapon, Leader). + +if LASERTAG_UTILS + config LASERTAG_UTILS_LOG_LEVEL + int "Utility Log Level" + default 3 + help + Set the verbosity of the lasertag utility library. + + config LASERTAG_SHELL + bool "Enable Lasertag Shell Commands" + default y + depends on SHELL + help + Provides commands like 'lasertag name' and 'lasertag reboot'. +endif \ No newline at end of file diff --git a/firmware/libs/lasertag_utils/include/lasertag_utils.h b/firmware/libs/lasertag_utils/include/lasertag_utils.h new file mode 100644 index 0000000..0c99898 --- /dev/null +++ b/firmware/libs/lasertag_utils/include/lasertag_utils.h @@ -0,0 +1,34 @@ +#ifndef LASERTAG_UTILS_H +#define LASERTAG_UTILS_H + +#include + +/** + * @file lasertag_utils.h + * @brief Common utility functions for the lasertag system. + */ + +/** + * @brief Initialize the common lasertag utilities (NVS, Shell, etc.). + */ +void lasertag_utils_init(void); + +/** + * @brief Get the current device name. + * @return Pointer to the device name string. + */ +const char* lasertag_get_device_name(void); + +/** + * @brief Get the configured Thread PAN ID. + * @return 16-bit PAN ID. + */ +uint16_t lasertag_get_thread_pan_id(void); + +/** + * @brief Get the configured Thread Network Name. + * @return Pointer to the network name string. + */ +const char* lasertag_get_thread_network_name(void); + +#endif /* LASERTAG_UTILS_H */ \ No newline at end of file diff --git a/firmware/libs/lasertag_utils/src/lasertag_utils.c b/firmware/libs/lasertag_utils/src/lasertag_utils.c new file mode 100644 index 0000000..66bcc12 --- /dev/null +++ b/firmware/libs/lasertag_utils/src/lasertag_utils.c @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(lasertag_utils, CONFIG_LASERTAG_UTILS_LOG_LEVEL); + +/* Default values */ +static char device_name[32] = "UnknownDevice"; +static uint16_t thread_pan_id = 0xabcd; +static char thread_network_name[17] = "OpenThread-nRF"; + +/* --- Settings Handling (Persistent Storage) --- */ + +static int lasertag_settings_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) +{ + const char *next; + + if (settings_name_steq(name, "name", &next) && !next) { + if (len > sizeof(device_name) - 1) return -EINVAL; + ssize_t rc = read_cb(cb_arg, device_name, len); + if (rc >= 0) { + device_name[rc] = '\0'; + return 0; + } + return (int)rc; + } + + if (settings_name_steq(name, "pan_id", &next) && !next) { + return read_cb(cb_arg, &thread_pan_id, sizeof(thread_pan_id)) >= 0 ? 0 : -EIO; + } + + if (settings_name_steq(name, "net_name", &next) && !next) { + if (len > sizeof(thread_network_name) - 1) return -EINVAL; + ssize_t rc = read_cb(cb_arg, thread_network_name, len); + if (rc >= 0) { + thread_network_name[rc] = '\0'; + return 0; + } + return (int)rc; + } + + return -ENOENT; +} + +struct settings_handler lasertag_conf = { + .name = "lasertag", + .h_set = lasertag_settings_set +}; + +void lasertag_utils_init(void) +{ + LOG_INF("=========================================="); + LOG_INF("Lasertag System - Common Lib v0.0.1"); + + int rc = settings_subsys_init(); + if (rc) { + LOG_ERR("Settings subsys init failed (err %d)", rc); + } + + settings_register(&lasertag_conf); + settings_load(); + + LOG_INF("Device Name : %s", device_name); + LOG_INF("Thread PAN : 0x%04x", thread_pan_id); + LOG_INF("Thread Name : %s", thread_network_name); + LOG_INF("=========================================="); +} + +const char* lasertag_get_device_name(void) { return device_name; } +uint16_t lasertag_get_thread_pan_id(void) { return thread_pan_id; } +const char* lasertag_get_thread_network_name(void) { return thread_network_name; } + +/* --- Shell Commands --- */ + +#if CONFIG_LASERTAG_SHELL + +static int cmd_name_set(const struct shell *sh, size_t argc, char **argv) +{ + strncpy(device_name, argv[1], sizeof(device_name) - 1); + device_name[sizeof(device_name) - 1] = '\0'; + settings_save_one("lasertag/name", device_name, strlen(device_name)); + shell_print(sh, "Device name saved: %s", device_name); + return 0; +} + +static int cmd_thread_set_panid(const struct shell *sh, size_t argc, char **argv) +{ + thread_pan_id = (uint16_t)strtoul(argv[1], NULL, 0); + settings_save_one("lasertag/pan_id", &thread_pan_id, sizeof(thread_pan_id)); + shell_print(sh, "Thread PAN ID saved: 0x%04x", thread_pan_id); + return 0; +} + +static int cmd_thread_set_name(const struct shell *sh, size_t argc, char **argv) +{ + strncpy(thread_network_name, argv[1], sizeof(thread_network_name) - 1); + thread_network_name[sizeof(thread_network_name) - 1] = '\0'; + settings_save_one("lasertag/net_name", thread_network_name, strlen(thread_network_name)); + shell_print(sh, "Thread Network Name saved: %s", thread_network_name); + return 0; +} + +/* Subcommands for 'lasertag thread' */ +SHELL_STATIC_SUBCMD_SET_CREATE(sub_thread, + SHELL_CMD_ARG(panid, NULL, "Set PAN ID ", cmd_thread_set_panid, 2, 0), + SHELL_CMD_ARG(name, NULL, "Set Network Name ", cmd_thread_set_name, 2, 0), + SHELL_SUBCMD_SET_END +); + +/* Main command 'lasertag' */ +SHELL_STATIC_SUBCMD_SET_CREATE(sub_lasertag, + SHELL_CMD_ARG(name, NULL, "Set device name ", cmd_name_set, 2, 0), + SHELL_CMD(thread, &sub_thread, "Thread network configuration", NULL), + SHELL_SUBCMD_SET_END +); + +SHELL_CMD_REGISTER(lasertag, &sub_lasertag, "Lasertag control commands", NULL); + +#endif /* CONFIG_LASERTAG_SHELL */ \ No newline at end of file diff --git a/firmware/libs/zephyr/module.yml b/firmware/libs/zephyr/module.yml new file mode 100644 index 0000000..91a0adf --- /dev/null +++ b/firmware/libs/zephyr/module.yml @@ -0,0 +1,6 @@ +# This file registers the libs directory as a formal Zephyr module. +# Adding a explicit name often helps Zephyr to recognize it correctly. +name: lasertag_libs +build: + cmake: . + kconfig: Kconfig \ No newline at end of file