From 99ea4e242b32a18ac1b6a12dbee9982fbe4426b2 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Fri, 14 Mar 2025 22:35:23 +0100 Subject: [PATCH] add save option --- src/ascii_cmd.c | 6 +++++- src/ascii_cmd.h | 1 + src/rds.c | 27 +++++++++++++++------------ src/rds.h | 4 ++++ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/ascii_cmd.c b/src/ascii_cmd.c index ba3b916..2542a57 100644 --- a/src/ascii_cmd.c +++ b/src/ascii_cmd.c @@ -366,7 +366,11 @@ void process_ascii_cmd(RDSModulator* enc, unsigned char *str) { unsigned char *cmd, *arg; uint16_t cmd_len = _strnlen((const char*)str, CTL_BUFFER_SIZE); - // Process commands with = delimiter at position 2 (format: X=y...) + if (str[0] == '*') { + saveToFile(enc->enc); + return; + } + if (cmd_len > 1 && str[1] == '=') { cmd = str; cmd[1] = 0; diff --git a/src/ascii_cmd.h b/src/ascii_cmd.h index 7584f7b..92adf58 100644 --- a/src/ascii_cmd.h +++ b/src/ascii_cmd.h @@ -1,4 +1,5 @@ #include "modulator.h" +#include "rds.h" #define CMD_BUFFER_SIZE 255 #define CTL_BUFFER_SIZE (CMD_BUFFER_SIZE * 2) #define READ_TIMEOUT_MS 100 diff --git a/src/rds.c b/src/rds.c index 35cd974..5f4fb2a 100644 --- a/src/rds.c +++ b/src/rds.c @@ -4,8 +4,10 @@ #include "lib.h" #include -void saveToFile(const char *filename, RDSEncoder *emp) { - FILE *file = fopen(filename, "wb"); +void saveToFile(RDSEncoder *emp) { + char encoderPath[256]; + snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME")); + FILE *file = fopen(encoderPath, "wb"); if (file == NULL) { perror("Error opening file"); return; @@ -14,8 +16,10 @@ void saveToFile(const char *filename, RDSEncoder *emp) { fclose(file); } -void loadFromFile(const char *filename, RDSEncoder *emp) { - FILE *file = fopen(filename, "rb"); +void loadFromFile(RDSEncoder *emp) { + char encoderPath[256]; + snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME")); + FILE *file = fopen(encoderPath, "rb"); if (file == NULL) { perror("Error opening file"); return; @@ -24,8 +28,10 @@ void loadFromFile(const char *filename, RDSEncoder *emp) { fclose(file); } -int fileExists(const char *filename) { - FILE *file = fopen(filename, "rb"); +int rdssaved() { + char encoderPath[256]; + snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME")); + FILE *file = fopen(encoderPath, "rb"); if (file) { fclose(file); return 1; @@ -404,13 +410,10 @@ void init_rds_encoder(RDSEncoder* enc) { init_rtplus(enc, GROUP_11A); - char encoderPath[256]; - snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME")); - - if (fileExists(encoderPath)) { - loadFromFile(encoderPath, enc); + if (rdssaved()) { + loadFromFile(enc); } else { - saveToFile(encoderPath, enc); + saveToFile(enc); } } diff --git a/src/rds.h b/src/rds.h index 0acddc7..b14c41d 100644 --- a/src/rds.h +++ b/src/rds.h @@ -279,6 +279,10 @@ typedef struct #define IS_TYPE_B(a) (a[1] & INT16_11) +void saveToFile(RDSEncoder *emp); +void loadFromFile(RDSEncoder *emp); +int rdssaved(); + void init_rds_encoder(RDSEncoder* enc); void get_rds_bits(RDSEncoder* enc, uint8_t *bits); void set_rds_rt1(RDSEncoder* enc, unsigned char *rt1);