implemented master-mod

This commit is contained in:
Marvin 2025-07-23 15:52:58 +02:00
parent 4989f763c7
commit de18d1a5c4
7 changed files with 195 additions and 3 deletions

View File

@ -14,6 +14,8 @@
"gpio.h": "c",
"array": "c",
"string": "c",
"string_view": "c"
"string_view": "c",
"buttons.h": "c",
"master.h": "c"
}
}

View File

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

14
Kconfig Normal file
View File

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

5
include/master/master.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef MASTER_H
#define MASTER_H
void master_init(void);
#endif

View File

@ -13,4 +13,4 @@ CONFIG_DISPLAY=y
CONFIG_SSD16XX=y
# --- Stack Size (unverändert) ---
CONFIG_MAIN_STACK_SIZE=2048
CONFIG_MAIN_STACK_SIZE=2048

View File

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

151
src/master/master.c Normal file
View File

@ -0,0 +1,151 @@
#include <zephyr/console/console.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/device.h>
#include <zephyr/drivers/lora.h>
#include <string.h>
#include <stdio.h>
#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);
}