Base directory structure, lib module

This commit is contained in:
2026-01-01 21:52:59 +01:00
parent cb4a34ae2b
commit c058c006b7
12 changed files with 254 additions and 0 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -0,0 +1,19 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <lasertag_utils.h>
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;
}

View File

@@ -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)

View File

@@ -0,0 +1 @@
CONFIG_LASERTAG_UTILS=y

View File

@@ -0,0 +1,2 @@
# Add library subdirectories
add_subdirectory(lasertag_utils)

4
firmware/libs/Kconfig Normal file
View File

@@ -0,0 +1,4 @@
# Main entry point for custom project Kconfigs
# This file is included by the application's Kconfig
rsource "lasertag_utils/Kconfig"

View File

@@ -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()

View File

@@ -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

View File

@@ -0,0 +1,34 @@
#ifndef LASERTAG_UTILS_H
#define LASERTAG_UTILS_H
#include <stdint.h>
/**
* @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 */

View File

@@ -0,0 +1,124 @@
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/settings/settings.h>
#include <string.h>
#include <stdlib.h>
#include <lasertag_utils.h>
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 <id>", cmd_thread_set_panid, 2, 0),
SHELL_CMD_ARG(name, NULL, "Set Network Name <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 <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 */

View File

@@ -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