Created enum for inpub_id changed strcat to strncat

This commit is contained in:
RobinVdB8
2023-11-26 09:54:58 +01:00
parent dea0d26141
commit da5b02d5da

View File

@@ -22,6 +22,15 @@
#define MQTT_SERVER_PORT 1883
#define PRINT_XPOS 50
#define PRINT_YPOS 50
#define MAX_FILES 20
typedef enum input_topic {
set_text,
set_text_color,
set_color,
set_image,
other_topic
} input_topic_t;
// Function prototypes
static void mqtt_pub_request_cb(void*, err_t);
@@ -40,7 +49,7 @@ static uint16_t ypos;
static uint16_t connection_attempt_counter;
static uint32_t color;
static uint32_t bgcolor;
static int inpub_id;
static input_topic_t inpub_id;
static const char* TAG = "mug";
/**
@@ -65,7 +74,7 @@ static void mqtt_pub_request_cb(void* arg, err_t result) {
static void publish_data(mqtt_client_t* client, void* arg) {
char pub_payload[200] = {0};
err_t err;
size_t max_files = 20;
size_t max_files = MAX_FILES;
size_t num_files;
llfs_file_t file_list[max_files];
u8_t qos = PUBLISH_QOS;
@@ -76,29 +85,36 @@ static void publish_data(mqtt_client_t* client, void* arg) {
num_files = llfs_file_list(file_list, max_files, "*.bmp");
if (num_files == 0) {
strcpy(pub_payload, "No images found");
strncpy(pub_payload, "No images found", sizeof(pub_payload));
} else {
strcat(pub_payload, "Available images: ");
strncat(pub_payload, "Available images: ", sizeof(pub_payload) - strlen(pub_payload) - 1);
for (size_t i = 0; i < num_files; i++) {
// Concatenate file names into the payload string
strcat(pub_payload, file_list[i].name);
strcat(pub_payload, ", "); // Add a comma between file names
strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1);
strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names
}
strcat(pub_payload, "\0");
strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1);
}
err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
if (err != ERR_OK) {
LOG_DEBUG(TAG, "Publish err: %d", err);
}
pub_payload[0] = '\0';
num_files = llfs_file_list(file_list, max_files, "*.gif");
if (num_files == 0) {
strcpy(pub_payload, "No gifs found");
} else {
strcat(pub_payload, "Available gifs: ");
strcpy(pub_payload, "Available gifs: ");
for (size_t i = 0; i < num_files; i++) {
// Concatenate file names into the payload string
strcat(pub_payload, file_list[i].name);
strcat(pub_payload, ", "); // Add a comma between file names
strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1);
strncat(pub_payload, ", ", sizeof(pub_payload) - strlen(pub_payload) - 1); // Add a comma between file names
}
strcat(pub_payload, "\0");
strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1);
}
err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg);
if (err != ERR_OK) {
@@ -118,16 +134,16 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_
LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %lu", topic, tot_len);
// Check for which topic a publish was received
if (strcmp(topic, "input/setText") == 0) {
inpub_id = 0;
inpub_id = set_text;
} else if (strcmp(topic, "input/setImage") == 0) {
inpub_id = 1;
inpub_id = set_image;
} else if (strcmp(topic, "input/setTextColor") == 0) {
inpub_id = 2;
inpub_id = set_text_color;
} else if (strcmp(topic, "input/setColor") == 0) {
inpub_id = 3;
inpub_id = set_color;
} else {
// In case of wrong topic
inpub_id = 4;
inpub_id = other_topic;
}
}
@@ -149,13 +165,13 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len,
memcpy(data_buffer, data, len);
data_buffer[len] = '\0';
switch (inpub_id) {
case 0:
case set_text:
// Places text on the lcd
LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer);
lcd_clear_text();
lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font);
break;
case 1:
case set_image:
// Places an image on the lcd
LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer);
lcd_clear_images();
@@ -170,12 +186,12 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len,
}
}
break;
case 2:
case set_text_color:
// Changes the text color for the next time text is written
LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer);
color = color_picker(data_buffer);
break;
case 3:
case set_color:
// Changes the background color for the next time text is written
LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer);
bgcolor = color_picker(data_buffer);