From de18d1a5c4af83cd15cb974400afd25be7e02f82 Mon Sep 17 00:00:00 2001 From: Marvin Date: Wed, 23 Jul 2025 15:52:58 +0200 Subject: [PATCH] implemented master-mod --- .vscode/settings.json | 4 +- CMakeLists.txt | 1 + Kconfig | 14 ++++ include/master/master.h | 5 ++ prj.conf | 2 +- src/main.c | 21 +++++- src/master/master.c | 151 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 Kconfig create mode 100644 include/master/master.h create mode 100644 src/master/master.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 5bdeb15..a1a0769 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,6 +14,8 @@ "gpio.h": "c", "array": "c", "string": "c", - "string_view": "c" + "string_view": "c", + "buttons.h": "c", + "master.h": "c" } } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 074f2e1..1b24130 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ target_sources(app PRIVATE src/lora/encryption.c src/lora/lora.c #src/lora/synch.c + src/master/master.c src/epaper/epaper.c src/utils/constAndVars.c src/utils/buttons.c diff --git a/Kconfig b/Kconfig new file mode 100644 index 0000000..518a5d7 --- /dev/null +++ b/Kconfig @@ -0,0 +1,14 @@ +# Diese Zeile lädt alle Standard-Definitionen von Zephyr +source "Kconfig.zephyr" + +choice DEVICE_MODE + prompt "Device Mode" + default DEVICE_MODE_MASTER + +config DEVICE_MODE_MASTER + bool "Build as master" + +config DEVICE_MODE_SLAVE + bool "Build as slave" + +endchoice \ No newline at end of file diff --git a/include/master/master.h b/include/master/master.h new file mode 100644 index 0000000..df2780c --- /dev/null +++ b/include/master/master.h @@ -0,0 +1,5 @@ +#ifndef MASTER_H +#define MASTER_H +void master_init(void); + +#endif \ No newline at end of file diff --git a/prj.conf b/prj.conf index f8abd0c..50c5ba2 100644 --- a/prj.conf +++ b/prj.conf @@ -13,4 +13,4 @@ CONFIG_DISPLAY=y CONFIG_SSD16XX=y # --- Stack Size (unverändert) --- -CONFIG_MAIN_STACK_SIZE=2048 \ No newline at end of file +CONFIG_MAIN_STACK_SIZE=2048 diff --git a/src/main.c b/src/main.c index 442159a..9d00f13 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,8 @@ #include "lora/lora.h" +#include "master/master.h" + #include "utils/buttons.h" #include "utils/leds.h" #include "utils/constAndVars.h" @@ -34,8 +36,15 @@ uint8_t init_led_and_button(void) { return 0; } -int main(void) +void run_master(void) { + printk("Running as MASTER\n"); + master_init(); + // Master-spezifische Logik +} + +int run_slave(void) { + printk("Running as SLAVE\n"); if (init_led_and_button() != 0) { printk("❌ GPIO-Initialisierung fehlgeschlagen\n"); error = true; @@ -52,3 +61,13 @@ int main(void) return 0; } + +void main(void) { +#if IS_ENABLED(CONFIG_DEVICE_MODE_MASTER) + run_master(); +#elif IS_ENABLED(CONFIG_DEVICE_MODE_SLAVE) + run_slave(); +#else +#error "No device mode selected. Please enable one mode in prj.conf or via build flags." +#endif +} \ No newline at end of file diff --git a/src/master/master.c b/src/master/master.c new file mode 100644 index 0000000..106326a --- /dev/null +++ b/src/master/master.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lora/lorapacket.h" + +#define DEFAULT_RADIO_NODE DT_ALIAS(lora0) +#define SEND_INTERVAL_MS 1000 // Sendeintervall in ms + +#define SLEEP_INTERVAL_MS 120000 + +#define STACK_SIZE 1024 +#define INPUT_STACK_SIZE 1024 +#define INPUT_PRIORITY 5 + +struct k_thread send_thread_data; + +const struct device *lora_dev_master = DEVICE_DT_GET(DEFAULT_RADIO_NODE); + +K_THREAD_STACK_DEFINE(send_stack_area, STACK_SIZE); +K_THREAD_STACK_DEFINE(input_stack_area, INPUT_STACK_SIZE); +struct k_thread input_thread_data; + +char latest_word[32] = "Hello!"; +struct k_mutex word_mutex; + +static DataPacket packet; +struct lora_modem_config tx_config_master = { + .frequency = 868100000, + .bandwidth = BW_125_KHZ, + .datarate = SF_7, + .preamble_len = 8, + .coding_rate = CR_4_5, + .tx_power = 14, + .tx = true, +}; + +#define UART_DEVICE_NODE DT_CHOSEN(zephyr_console) +const struct device *uart_console = DEVICE_DT_GET(UART_DEVICE_NODE); + +void uart_input_get_line(char *buf, size_t max_len) +{ + size_t len = 0; + char c; + + while (len < max_len - 1) { + if (uart_poll_in(uart_console, &c) == 0) { + if (c == '\r' || c == '\n') { + break; + } + buf[len++] = c; + printk("%c", c); // Echo + } + k_msleep(1); + } + + buf[len] = '\0'; + printk("\n"); +} + + +void send_lora_message(const char* text) +{ + int ret = lora_config(lora_dev_master, &tx_config_master); + if (ret != 0) { + printk("❌ LoRa-Konfiguration fehlgeschlagen: %d\n", ret); + return; + } + + DataPacket pkt; + memset(&pkt, 0, sizeof(pkt)); + strncpy(pkt.message, text, sizeof(pkt.message)); + pkt.timestamp = k_uptime_get(); + + ret = lora_send(lora_dev_master, (uint8_t *)&pkt, sizeof(pkt)); + if (ret < 0) { + printk("❌ Senden fehlgeschlagen: %d\n", ret); + } else { + printk("✅ Gesendet: \"%s\"\n", text); + } +} + +void send_loop_thread(void *p1, void *p2, void *p3) +{ + const int active_time_ms = 10000; + const int interval_ms = 1000; + + char local_copy[32]; + + while (1) { + printk("🕒 Warte 120 Sekunden...\n"); + k_msleep(SLEEP_INTERVAL_MS); + + printk("📡 Sendephase gestartet\n"); + + // Wort einfrieren + k_mutex_lock(&word_mutex, K_FOREVER); + strncpy(local_copy, latest_word, sizeof(local_copy)); + local_copy[sizeof(local_copy) - 1] = '\0'; + k_mutex_unlock(&word_mutex); + + uint32_t start = k_uptime_get(); + while ((k_uptime_get() - start) < active_time_ms) { + send_lora_message(local_copy); + k_msleep(interval_ms); + } + + printk("😴 Sendephase beendet\n"); + } +} + + + +void input_thread(void *p1, void *p2, void *p3) +{ + char buffer[32]; + + while (1) { + printk("⌨️ Neues Wort eingeben:\n"); + uart_input_get_line(buffer, sizeof(buffer)); + + k_mutex_lock(&word_mutex, K_FOREVER); + strncpy(latest_word, buffer, sizeof(latest_word)); + latest_word[sizeof(latest_word) - 1] = '\0'; + k_mutex_unlock(&word_mutex); + + printk("✅ Neues Wort gespeichert: \"%s\"\n", latest_word); + } + + +} + +void master_init(void) +{ + if (!device_is_ready(lora_dev_master)) { + printk("❌ LoRa-Gerät nicht bereit\n"); + return; + } + + k_mutex_init(&word_mutex); + + k_thread_create(&input_thread_data, input_stack_area, INPUT_STACK_SIZE, + input_thread, NULL, NULL, NULL, INPUT_PRIORITY, 0, K_NO_WAIT); + k_thread_create(&send_thread_data, send_stack_area, STACK_SIZE, + send_loop_thread, NULL, NULL, NULL, INPUT_PRIORITY, 0, K_NO_WAIT); +}