From 3e37fe8892d1079319ddd669ce78b55b90a70c1d Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:00:14 +0100 Subject: [PATCH 01/41] Added mug.h with mug_init() --- project/Core/Inc/mug.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 project/Core/Inc/mug.h diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h new file mode 100644 index 0000000..f2cbbe2 --- /dev/null +++ b/project/Core/Inc/mug.h @@ -0,0 +1,12 @@ +/** + * @file mug.h + * @header for mosquitto application of the groups assignment + * @author RobinVdB + */ + +#ifndef INC_MUG_H_ +#define INC_MUG_H_ + +void mug_init(); + +#endif /* INC_MUG_H_ */ From 9aabdac7aaa713a2cbd400ae8d41e8542f8aca01 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:06:45 +0100 Subject: [PATCH 02/41] Added mug.c with functions Functions are: mqtt_pub_request_cb example_publish mqtt_incoming_publish_cb mqtt_incoming_data_cb mqtt_sub_request_cb mqtt_connection_cb example_do_connect --- project/Core/Src/mug.c | 144 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 project/Core/Src/mug.c diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c new file mode 100644 index 0000000..b1eba9a --- /dev/null +++ b/project/Core/Src/mug.c @@ -0,0 +1,144 @@ +/** + * @file mug.c + * @mosquitto application for group assignment + * @author RobinVdB + */ + +#include "mdns.h" +#include "httpd.h" +#include "lwip/apps/fs.h" +#include +#include "lwip/ip_addr.h" +#include "mqtt.h" +#include "mug.h" + +static void mqtt_pub_request_cb(void*, err_t); +void example_publish(mqtt_client_client_t, void*); +static void mqtt_incoming_publish_cb(void*, const char*, u32_t); +static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); +static void mqtt_sub_request_cb(void*, err_t*); +static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); +void example_do_connect(mqtt_client_t*); + + +static void mqtt_pub_request_cb(void *arg, err_t result) +{ + if(result != ERR_OK) { + printf("Publish result: %d\n", result); + } +} + +int teller = 0; +void example_publish(mqtt_client_t *client, void *arg) +{ + printf("testing publish\n"); + const char *pub_payload[20]; + err_t err; + u8_t qos = 2; + u8_t retain = 0; + itoa(teller, &pub_payload, 10); + err = mqtt_publish(client, "pushbutton_event", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + if(err != ERR_OK) { + printf("Publish err: %d\n", err); + } +} + + +static int inpub_id; +static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) +{ + printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); + + //check for which topic a publish was received for + if(strcmp(topic, "/input/setText") == 0) { + inpub_id = 0; + } else if(strcmp(topic, "/input/setImage") == 0) { + inpub_id = 1; + } else if(strcmp(topic, "/input/setTextColor") == 0) { + inpub_id = 2; + } else if(strcmp(topic, "/input/setColor") == 0) { + inpub_id = 3; + } else { + //in case of wrong topic + inpub_id = 4; + } +} + +static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) +{ + printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + + if(flags & MQTT_DATA_FLAG_LAST) { + if (inpub_id == 0) { + //places text on the lcd + } else if (inpub_id == 1) { + //places an image on the lcd + } else if (inpub_id == 2) { + //changes the text color on the lcd + } else if (inpub_id == 3) { + //changes the background color + } else { + printf("mqtt_incoming_data_cb: Ignoring payload...\n"); + } + } else { + } +} + +static void mqtt_sub_request_cb(void *arg, err_t result) +{ + printf("Subscribe result: %d\n", result); +} + +static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) +{ + printf("entered connection cb function\n"); + err_t err; + if(status == MQTT_CONNECT_ACCEPTED) { + printf("mqtt_connection_cb: Successfully connected\n"); + + //set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + + //subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + + if(err != ERR_OK) { + printf("mqtt_subscribe return: %d\n", err); + } + //publish list of images here + } else { + printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); + + //try to reconnect + example_do_connect(client); + } +} + +void example_do_connect(mqtt_client_t *client) +{ + printf("testing the connection\n"); + struct mqtt_connect_client_info_t ci; + err_t err; + + memset(&ci, 0, sizeof(ci)); + + ci.client_id = "lwip_test"; + ip_addr_t server_ip; + IP4_ADDR(&server_ip, 192,168,69,11); + uint16_t server_port = 1883; + err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); + if(err != ERR_OK) { + printf("mqtt_connect return %d\n", err); + } + if(err == ERR_OK) { + printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); + } +} + +void mug_init() { + mqtt_client_t *client = mqtt_client_new(); + if(client != NULL) { + printf("Starting connection test\n"); + example_do_connect(client); + } +} From 15bd0f39691575a016d01d798e2b0e322fd712c5 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:14:00 +0100 Subject: [PATCH 03/41] Fixed position of '{' in mug.c --- project/Core/Src/mug.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index b1eba9a..c85a17f 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -21,16 +21,14 @@ static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); void example_do_connect(mqtt_client_t*); -static void mqtt_pub_request_cb(void *arg, err_t result) -{ +static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { printf("Publish result: %d\n", result); } } int teller = 0; -void example_publish(mqtt_client_t *client, void *arg) -{ +void example_publish(mqtt_client_t *client, void *arg) { printf("testing publish\n"); const char *pub_payload[20]; err_t err; @@ -45,8 +43,7 @@ void example_publish(mqtt_client_t *client, void *arg) static int inpub_id; -static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) -{ +static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); //check for which topic a publish was received for @@ -64,8 +61,7 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } } -static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) -{ +static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST) { @@ -84,13 +80,11 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } } -static void mqtt_sub_request_cb(void *arg, err_t result) -{ +static void mqtt_sub_request_cb(void *arg, err_t result) { printf("Subscribe result: %d\n", result); } -static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) -{ +static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { printf("entered connection cb function\n"); err_t err; if(status == MQTT_CONNECT_ACCEPTED) { @@ -114,8 +108,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } } -void example_do_connect(mqtt_client_t *client) -{ +void example_do_connect(mqtt_client_t *client) { printf("testing the connection\n"); struct mqtt_connect_client_info_t ci; err_t err; @@ -136,9 +129,9 @@ void example_do_connect(mqtt_client_t *client) } void mug_init() { - mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { - printf("Starting connection test\n"); - example_do_connect(client); - } + mqtt_client_t *client = mqtt_client_new(); + if(client != NULL) { + printf("Starting connection test\n"); + example_do_connect(client); + } } From d8e4471e7bd0780696039a20b2a1e94a7ff44be3 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 13:18:16 +0100 Subject: [PATCH 04/41] solved errors in mug.c --- project/Core/Src/mug.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index c85a17f..71917eb 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -13,10 +13,10 @@ #include "mug.h" static void mqtt_pub_request_cb(void*, err_t); -void example_publish(mqtt_client_client_t, void*); +void example_publish(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); -static void mqtt_sub_request_cb(void*, err_t*); +static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); void example_do_connect(mqtt_client_t*); From 0aed4429dab7ba9b07527bbff9dddeef431b8db3 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 14:49:03 +0100 Subject: [PATCH 05/41] Updated mug.h --- project/Core/Inc/mug.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index f2cbbe2..5285f89 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,6 +7,6 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ -void mug_init(); +void mug_init(void); #endif /* INC_MUG_H_ */ From 8e47a28ece4481847c5bc6286f7caea69ea1211a Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 13 Nov 2023 14:50:26 +0100 Subject: [PATCH 06/41] Fixed indentation and updated functions --- project/Core/Src/mug.c | 196 +++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 94 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 71917eb..40cc015 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -12,126 +12,134 @@ #include "mqtt.h" #include "mug.h" +#define LOGGER_LEVEL_INFO +#include "log.h" + +//Function prototypes static void mqtt_pub_request_cb(void*, err_t); -void example_publish(mqtt_client_t* , void*); +static void example_publish(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); -void example_do_connect(mqtt_client_t*); +static void example_do_connect(mqtt_client_t*); +//global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics +static int inpub_id; +static const char *TAG = "mug"; static void mqtt_pub_request_cb(void *arg, err_t result) { - if(result != ERR_OK) { - printf("Publish result: %d\n", result); - } + if(result != ERR_OK) { + LOG_INFO(TAG, "Publish result: %d\n", result); + } } -int teller = 0; -void example_publish(mqtt_client_t *client, void *arg) { - printf("testing publish\n"); - const char *pub_payload[20]; - err_t err; - u8_t qos = 2; - u8_t retain = 0; - itoa(teller, &pub_payload, 10); - err = mqtt_publish(client, "pushbutton_event", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); - if(err != ERR_OK) { - printf("Publish err: %d\n", err); - } +static void example_publish(mqtt_client_t *client, void *arg) { + printf("testing publish\n"); + const char *pub_payload[20]; + err_t err; + u8_t qos = 2; + u8_t retain = 0; + + err = mqtt_publish(client, "getImageList", pub_payload, sizeof(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + if(err != ERR_OK) { + printf("Publish err: %d\n", err); + } } - -static int inpub_id; static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); - - //check for which topic a publish was received for - if(strcmp(topic, "/input/setText") == 0) { - inpub_id = 0; - } else if(strcmp(topic, "/input/setImage") == 0) { - inpub_id = 1; - } else if(strcmp(topic, "/input/setTextColor") == 0) { - inpub_id = 2; - } else if(strcmp(topic, "/input/setColor") == 0) { - inpub_id = 3; - } else { - //in case of wrong topic - inpub_id = 4; - } + printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); + //check for which topic a publish was received + if(strcmp(topic, "/input/setText") == 0) { + inpub_id = 0; + } else if(strcmp(topic, "/input/setImage") == 0) { + inpub_id = 1; + } else if(strcmp(topic, "/input/setTextColor") == 0) { + inpub_id = 2; + } else if(strcmp(topic, "/input/setColor") == 0) { + inpub_id = 3; + } else { + //in case of wrong topic + inpub_id = 4; + } } static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + if(flags & MQTT_DATA_FLAG_LAST) { + switch(inpub_id) { + case 0: + //places text on the lcd + break; + case 1: + //places an image on the lcd + break; + case 2: + //changes the text color on the lcd + break; + case 3: + //changes the background color + break; + default: + LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); + } + } else { - if(flags & MQTT_DATA_FLAG_LAST) { - if (inpub_id == 0) { - //places text on the lcd - } else if (inpub_id == 1) { - //places an image on the lcd - } else if (inpub_id == 2) { - //changes the text color on the lcd - } else if (inpub_id == 3) { - //changes the background color - } else { - printf("mqtt_incoming_data_cb: Ignoring payload...\n"); - } - } else { - } + } } static void mqtt_sub_request_cb(void *arg, err_t result) { - printf("Subscribe result: %d\n", result); + LOG_INFO(TAG, "Subscribe result: %d\n", result); } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - printf("entered connection cb function\n"); + printf("entered connection cb function\n"); err_t err; - if(status == MQTT_CONNECT_ACCEPTED) { - printf("mqtt_connection_cb: Successfully connected\n"); + if(status == MQTT_CONNECT_ACCEPTED) { + printf("mqtt_connection_cb: Successfully connected\n"); - //set up callback function for input - mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + //set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - //subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + //subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + if(err != ERR_OK) { + printf("mqtt_subscribe return: %d\n", err); + } + //publish list of images here + example_publish(client, NULL); + } else { + printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); - if(err != ERR_OK) { - printf("mqtt_subscribe return: %d\n", err); - } - //publish list of images here - } else { - printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); - - //try to reconnect - example_do_connect(client); - } -} - -void example_do_connect(mqtt_client_t *client) { - printf("testing the connection\n"); - struct mqtt_connect_client_info_t ci; - err_t err; - - memset(&ci, 0, sizeof(ci)); - - ci.client_id = "lwip_test"; - ip_addr_t server_ip; - IP4_ADDR(&server_ip, 192,168,69,11); - uint16_t server_port = 1883; - err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); - if(err != ERR_OK) { - printf("mqtt_connect return %d\n", err); - } - if(err == ERR_OK) { - printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); - } -} - -void mug_init() { - mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { - printf("Starting connection test\n"); - example_do_connect(client); + //try to reconnect + example_do_connect(client); + } +} + +static void example_do_connect(mqtt_client_t *client) { + printf("testing the connection\n"); + struct mqtt_connect_client_info_t ci; + err_t err; + + memset(&ci, 0, sizeof(ci)); + + ci.client_id = "lwip_test"; + ip_addr_t server_ip; + IP4_ADDR(&server_ip, 192,168,69,11); + uint16_t server_port = 1883; + err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); + if(err != ERR_OK) { + printf("mqtt_connect return %d\n", err); + } + if(err == ERR_OK) { + printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); + } +} + +void mug_init(void) { + mqtt_client_t *client = mqtt_client_new(); + if(client != NULL) { + printf("Starting connection test\n"); + example_do_connect(client); } } From b79a42b767fc0c4c3fd3f99424296e135dc49541 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 19 Nov 2023 18:23:02 +0100 Subject: [PATCH 07/41] Implemented LCD and filesystem --- project/Core/Src/main.c | 2 + project/Core/Src/mug.c | 169 ++++++++++++++++++++++++++++++++++------ 2 files changed, 147 insertions(+), 24 deletions(-) diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 54e3083..7cfff86 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -28,6 +28,7 @@ #include "log.h" #include "llfs.h" #include "lcd_api.h" +#include "mug.h" /* USER CODE END Includes */ @@ -130,6 +131,7 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ + mug_init(); while (1) { /* USER CODE END WHILE */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 40cc015..f116a7a 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -11,6 +11,7 @@ #include "lwip/ip_addr.h" #include "mqtt.h" #include "mug.h" +#include "lcd_api.h" #define LOGGER_LEVEL_INFO #include "log.h" @@ -19,44 +20,64 @@ static void mqtt_pub_request_cb(void*, err_t); static void example_publish(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); -static void mqtt_incoming_data_cb(void*, const u8_t*, u16_t, u8_t); +static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void example_do_connect(mqtt_client_t*); +static uint32_t color_picker(uint8_t*); //global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static int inpub_id; static const char *TAG = "mug"; +static uint16_t xpos; +static uint16_t ypos; +static sFONT* font = LCD_FONT16; +static uint32_t color; +static uint32_t bgcolor; static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { - LOG_INFO(TAG, "Publish result: %d\n", result); + LOG_INFO(TAG, "Publish result: %d", result); } } static void example_publish(mqtt_client_t *client, void *arg) { - printf("testing publish\n"); - const char *pub_payload[20]; + LOG_INFO(TAG, "Entering publish"); + char pub_payload[200]; err_t err; u8_t qos = 2; - u8_t retain = 0; + u8_t retain = 1 ; + size_t max_files = 20; + llfs_file_t file_list[max_files]; + size_t num_files; - err = mqtt_publish(client, "getImageList", pub_payload, sizeof(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + num_files = llfs_file_list(file_list, max_files, "*.png"); + + if(num_files == 0) { + strcpy(pub_payload, "No images found"); + } else { + 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 space between file names + } + } + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK) { - printf("Publish err: %d\n", err); + LOG_INFO(TAG, "Publish err: %d", err); } } static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - printf("Incoming publish at topic %s with total length %u\n", topic, (unsigned int)tot_len); + LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); //check for which topic a publish was received - if(strcmp(topic, "/input/setText") == 0) { + if(strcmp(topic, "input/setText") == 0) { inpub_id = 0; - } else if(strcmp(topic, "/input/setImage") == 0) { + } else if(strcmp(topic, "input/setImage") == 0) { inpub_id = 1; - } else if(strcmp(topic, "/input/setTextColor") == 0) { + } else if(strcmp(topic, "input/setTextColor") == 0) { inpub_id = 2; - } else if(strcmp(topic, "/input/setColor") == 0) { + } else if(strcmp(topic, "input/setColor") == 0) { inpub_id = 3; } else { //in case of wrong topic @@ -65,20 +86,30 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - printf("Incoming publish payload with length %d, flags %u\n", len, (unsigned int)flags); + uint8_t data_buffer[len + 1]; + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST) { + memcpy(data_buffer, data, len); + data_buffer[len] = '\0'; switch(inpub_id) { case 0: - //places text on the lcd + //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); + LOG_INFO(TAG, "incoming data on input/setText:\n %s.", data); + lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: //places an image on the lcd + LOG_INFO(TAG, "incoming data on input/setImage:\n %s.", data); break; case 2: //changes the text color on the lcd + LOG_INFO(TAG, "incoming data on input/setTextColor:\n %s.", data); + color = color_picker(data_buffer); break; case 3: //changes the background color + LOG_INFO(TAG, "incoming data on input/setColor:\n %s.", data); + bgcolor = color_picker(data_buffer); break; default: LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); @@ -89,27 +120,28 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } static void mqtt_sub_request_cb(void *arg, err_t result) { - LOG_INFO(TAG, "Subscribe result: %d\n", result); + LOG_INFO(TAG, "Subscribe result: %d", result); } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - printf("entered connection cb function\n"); + LOG_INFO(TAG, "entered connection cb function"); err_t err; if(status == MQTT_CONNECT_ACCEPTED) { - printf("mqtt_connection_cb: Successfully connected\n"); + LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); //set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); //subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "/input/#", 1, mqtt_sub_request_cb, arg); + err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); if(err != ERR_OK) { - printf("mqtt_subscribe return: %d\n", err); + LOG_INFO(TAG, "mqtt_subscribe return: %d", err); } //publish list of images here + example_publish(client, NULL); } else { - printf("mqtt_connection_cb: Disconnected, reason: %d\n", status); + LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); //try to reconnect example_do_connect(client); @@ -117,7 +149,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } static void example_do_connect(mqtt_client_t *client) { - printf("testing the connection\n"); + LOG_INFO(TAG, "testing the connection"); struct mqtt_connect_client_info_t ci; err_t err; @@ -129,17 +161,106 @@ static void example_do_connect(mqtt_client_t *client) { uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if(err != ERR_OK) { - printf("mqtt_connect return %d\n", err); + LOG_INFO(TAG, "mqtt_connect return %d", err); } if(err == ERR_OK) { - printf("Went into mqtt_client_connect; mqtt_connect return %d\n", err); + LOG_INFO(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } } void mug_init(void) { + color = LCD_BLACK; + bgcolor = LCD_WHITE; + xpos = 50; + ypos = 50; + mqtt_client_t *client = mqtt_client_new(); if(client != NULL) { - printf("Starting connection test\n"); + LOG_INFO(TAG, "Starting connection test"); example_do_connect(client); } } + +uint32_t color_picker(uint8_t* color) { + uint32_t output = LCD_BLACK; + if(strcmp((const char*)color, "blue") == 0) { + output = LCD_BLUE; + } + if(strcmp((const char*)color, "green") == 0) { + output = LCD_GREEN; + } + if(strcmp((const char*)color, "red") == 0) { + output = LCD_RED; + } + if(strcmp((const char*)color, "cyan") == 0) { + output = LCD_CYAN; + } + if(strcmp((const char*)color, "magenta") == 0) { + output = LCD_MAGENTA; + } + if(strcmp((const char*)color, "yellow") == 0) { + output = LCD_YELLOW; + } + if(strcmp((const char*)color, "light blue") == 0) { + output = LCD_LIGHTBLUE; + } + if(strcmp((const char*)color, "light green") == 0) { + output = LCD_LIGHTGREEN; + } + if(strcmp((const char*)color, "light red") == 0) { + output = LCD_LIGHTRED; + } + if(strcmp((const char*)color, "light cyan") == 0) { + output = LCD_LIGHTCYAN; + } + if(strcmp((const char*)color, "light magenta") == 0) { + output = LCD_LIGHTMAGENTA; + } + if(strcmp((const char*)color, "light yellow") == 0) { + output = LCD_LIGHTYELLOW; + } + if(strcmp((const char*)color, "dark blue") == 0) { + output = LCD_DARKBLUE; + } + if(strcmp((const char*)color, "dark green") == 0) { + output = LCD_DARKGREEN; + } + if(strcmp((const char*)color, "dark red") == 0) { + output = LCD_DARKRED; + } + if(strcmp((const char*)color, "dark cyan") == 0) { + output = LCD_DARKCYAN; + } + if(strcmp((const char*)color, "dark magenta") == 0) { + output = LCD_DARKMAGENTA; + } + if(strcmp((const char*)color, "dark yellow") == 0) { + output = LCD_DARKYELLOW; + } + if(strcmp((const char*)color, "white") == 0) { + output = LCD_WHITE; + } + if(strcmp((const char*)color, "light gray") == 0) { + output = LCD_LIGHTGRAY; + } + if(strcmp((const char*)color, "gray") == 0) { + output = LCD_GRAY; + } + if(strcmp((const char*)color, "dark gray") == 0) { + output = LCD_DARKGRAY; + } + if(strcmp((const char*)color, "black") == 0) { + output = LCD_BLACK; + } + if(strcmp((const char*)color, "brown") == 0) { + output = LCD_BROWN; + } + if(strcmp((const char*)color, "orange") == 0) { + output = LCD_ORANGE; + } + if(strcmp((const char*)color, "transparent") == 0) { + output = LCD_TRANSPARENT; + } + + return output; +} From bbbf17b5ca4ff5311eb0d3b69d733b805d3d25c2 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 20 Nov 2023 13:20:46 +0100 Subject: [PATCH 08/41] Added styleguid.xml to editor --- project/Core/Src/mug.c | 318 +++++++++++++++++++++-------------------- 1 file changed, 160 insertions(+), 158 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index f116a7a..4e2bbf1 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -31,7 +31,7 @@ static int inpub_id; static const char *TAG = "mug"; static uint16_t xpos; static uint16_t ypos; -static sFONT* font = LCD_FONT16; +static sFONT* font; static uint32_t color; static uint32_t bgcolor; @@ -54,102 +54,103 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.png"); if(num_files == 0) { - strcpy(pub_payload, "No images found"); + strcpy(pub_payload, "No images found"); } else { - 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 space between file names - } + 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 space between file names + } } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK) { - LOG_INFO(TAG, "Publish err: %d", err); + LOG_INFO(TAG, "Publish err: %d", err); } } static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); - //check for which topic a publish was received - if(strcmp(topic, "input/setText") == 0) { - inpub_id = 0; - } else if(strcmp(topic, "input/setImage") == 0) { - inpub_id = 1; - } else if(strcmp(topic, "input/setTextColor") == 0) { - inpub_id = 2; - } else if(strcmp(topic, "input/setColor") == 0) { - inpub_id = 3; - } else { - //in case of wrong topic - inpub_id = 4; - } + LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); + //check for which topic a publish was received + if(strcmp(topic, "input/setText") == 0) { + inpub_id = 0; + } else if(strcmp(topic, "input/setImage") == 0) { + inpub_id = 1; + } else if(strcmp(topic, "input/setTextColor") == 0) { + inpub_id = 2; + } else if(strcmp(topic, "input/setColor") == 0) { + inpub_id = 3; + } else { + //in case of wrong topic + inpub_id = 4; + } } static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - uint8_t data_buffer[len + 1]; - LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); - if(flags & MQTT_DATA_FLAG_LAST) { - memcpy(data_buffer, data, len); - data_buffer[len] = '\0'; - switch(inpub_id) { - case 0: - //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); - LOG_INFO(TAG, "incoming data on input/setText:\n %s.", data); - lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); - break; - case 1: - //places an image on the lcd - LOG_INFO(TAG, "incoming data on input/setImage:\n %s.", data); - break; - case 2: - //changes the text color on the lcd - LOG_INFO(TAG, "incoming data on input/setTextColor:\n %s.", data); - color = color_picker(data_buffer); - break; - case 3: - //changes the background color - LOG_INFO(TAG, "incoming data on input/setColor:\n %s.", data); - bgcolor = color_picker(data_buffer); - break; - default: - LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); - } - } else { + uint8_t data_buffer[len + 1]; + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); + if(flags & MQTT_DATA_FLAG_LAST) { + memcpy(data_buffer, data, len); + data_buffer[len] = '\0'; + switch(inpub_id) { + case 0: + //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); + LOG_INFO(TAG, "incoming data on input/setText: %s.", data); + lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); + break; + case 1: + //places an image on the lcd + LOG_INFO(TAG, "incoming data on input/setImage: %s.", data); + //call function here + break; + case 2: + //changes the text color on the lcd + LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data); + color = color_picker(data_buffer); + break; + case 3: + //changes the background color + LOG_INFO(TAG, "incoming data on input/setColor: %s.", data); + bgcolor = color_picker(data_buffer); + break; + default: + LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); + } + } else { - } + } } static void mqtt_sub_request_cb(void *arg, err_t result) { - LOG_INFO(TAG, "Subscribe result: %d", result); + LOG_INFO(TAG, "Subscribe result: %d", result); } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - LOG_INFO(TAG, "entered connection cb function"); - err_t err; - if(status == MQTT_CONNECT_ACCEPTED) { - LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); + LOG_INFO(TAG, "entered connection cb function"); + err_t err; + if(status == MQTT_CONNECT_ACCEPTED) { + LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); - //set up callback function for input - mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + //set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - //subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); - if(err != ERR_OK) { - LOG_INFO(TAG, "mqtt_subscribe return: %d", err); - } - //publish list of images here + //subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); + if(err != ERR_OK) { + LOG_INFO(TAG, "mqtt_subscribe return: %d", err); + } + //publish list of images here - example_publish(client, NULL); - } else { - LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); + example_publish(client, NULL); + } else { + LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - //try to reconnect - example_do_connect(client); - } + //try to reconnect + example_do_connect(client); + } } static void example_do_connect(mqtt_client_t *client) { - LOG_INFO(TAG, "testing the connection"); + LOG_INFO(TAG, "testing the connection"); struct mqtt_connect_client_info_t ci; err_t err; @@ -169,98 +170,99 @@ static void example_do_connect(mqtt_client_t *client) { } void mug_init(void) { - color = LCD_BLACK; - bgcolor = LCD_WHITE; - xpos = 50; - ypos = 50; + color = LCD_BLACK; + bgcolor = LCD_WHITE; + font = LCD_FONT16; + xpos = 50; + ypos = 50; mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { + if(client != NULL) { LOG_INFO(TAG, "Starting connection test"); example_do_connect(client); - } + } } uint32_t color_picker(uint8_t* color) { - uint32_t output = LCD_BLACK; - if(strcmp((const char*)color, "blue") == 0) { - output = LCD_BLUE; - } - if(strcmp((const char*)color, "green") == 0) { - output = LCD_GREEN; - } - if(strcmp((const char*)color, "red") == 0) { - output = LCD_RED; - } - if(strcmp((const char*)color, "cyan") == 0) { - output = LCD_CYAN; - } - if(strcmp((const char*)color, "magenta") == 0) { - output = LCD_MAGENTA; - } - if(strcmp((const char*)color, "yellow") == 0) { - output = LCD_YELLOW; - } - if(strcmp((const char*)color, "light blue") == 0) { - output = LCD_LIGHTBLUE; - } - if(strcmp((const char*)color, "light green") == 0) { - output = LCD_LIGHTGREEN; - } - if(strcmp((const char*)color, "light red") == 0) { - output = LCD_LIGHTRED; - } - if(strcmp((const char*)color, "light cyan") == 0) { - output = LCD_LIGHTCYAN; - } - if(strcmp((const char*)color, "light magenta") == 0) { - output = LCD_LIGHTMAGENTA; - } - if(strcmp((const char*)color, "light yellow") == 0) { - output = LCD_LIGHTYELLOW; - } - if(strcmp((const char*)color, "dark blue") == 0) { - output = LCD_DARKBLUE; - } - if(strcmp((const char*)color, "dark green") == 0) { - output = LCD_DARKGREEN; - } - if(strcmp((const char*)color, "dark red") == 0) { - output = LCD_DARKRED; - } - if(strcmp((const char*)color, "dark cyan") == 0) { - output = LCD_DARKCYAN; - } - if(strcmp((const char*)color, "dark magenta") == 0) { - output = LCD_DARKMAGENTA; - } - if(strcmp((const char*)color, "dark yellow") == 0) { - output = LCD_DARKYELLOW; - } - if(strcmp((const char*)color, "white") == 0) { - output = LCD_WHITE; - } - if(strcmp((const char*)color, "light gray") == 0) { - output = LCD_LIGHTGRAY; - } - if(strcmp((const char*)color, "gray") == 0) { - output = LCD_GRAY; - } - if(strcmp((const char*)color, "dark gray") == 0) { - output = LCD_DARKGRAY; - } - if(strcmp((const char*)color, "black") == 0) { - output = LCD_BLACK; - } - if(strcmp((const char*)color, "brown") == 0) { - output = LCD_BROWN; - } - if(strcmp((const char*)color, "orange") == 0) { - output = LCD_ORANGE; - } - if(strcmp((const char*)color, "transparent") == 0) { - output = LCD_TRANSPARENT; - } + uint32_t output = LCD_BLACK; + if(strcmp((const char*)color, "blue") == 0) { + output = LCD_BLUE; + } + if(strcmp((const char*)color, "green") == 0) { + output = LCD_GREEN; + } + if(strcmp((const char*)color, "red") == 0) { + output = LCD_RED; + } + if(strcmp((const char*)color, "cyan") == 0) { + output = LCD_CYAN; + } + if(strcmp((const char*)color, "magenta") == 0) { + output = LCD_MAGENTA; + } + if(strcmp((const char*)color, "yellow") == 0) { + output = LCD_YELLOW; + } + if(strcmp((const char*)color, "light blue") == 0) { + output = LCD_LIGHTBLUE; + } + if(strcmp((const char*)color, "light green") == 0) { + output = LCD_LIGHTGREEN; + } + if(strcmp((const char*)color, "light red") == 0) { + output = LCD_LIGHTRED; + } + if(strcmp((const char*)color, "light cyan") == 0) { + output = LCD_LIGHTCYAN; + } + if(strcmp((const char*)color, "light magenta") == 0) { + output = LCD_LIGHTMAGENTA; + } + if(strcmp((const char*)color, "light yellow") == 0) { + output = LCD_LIGHTYELLOW; + } + if(strcmp((const char*)color, "dark blue") == 0) { + output = LCD_DARKBLUE; + } + if(strcmp((const char*)color, "dark green") == 0) { + output = LCD_DARKGREEN; + } + if(strcmp((const char*)color, "dark red") == 0) { + output = LCD_DARKRED; + } + if(strcmp((const char*)color, "dark cyan") == 0) { + output = LCD_DARKCYAN; + } + if(strcmp((const char*)color, "dark magenta") == 0) { + output = LCD_DARKMAGENTA; + } + if(strcmp((const char*)color, "dark yellow") == 0) { + output = LCD_DARKYELLOW; + } + if(strcmp((const char*)color, "white") == 0) { + output = LCD_WHITE; + } + if(strcmp((const char*)color, "light gray") == 0) { + output = LCD_LIGHTGRAY; + } + if(strcmp((const char*)color, "gray") == 0) { + output = LCD_GRAY; + } + if(strcmp((const char*)color, "dark gray") == 0) { + output = LCD_DARKGRAY; + } + if(strcmp((const char*)color, "black") == 0) { + output = LCD_BLACK; + } + if(strcmp((const char*)color, "brown") == 0) { + output = LCD_BROWN; + } + if(strcmp((const char*)color, "orange") == 0) { + output = LCD_ORANGE; + } + if(strcmp((const char*)color, "transparent") == 0) { + output = LCD_TRANSPARENT; + } - return output; + return output; } From ef4df1e5f569390eabb60ca6da936d26d7a11c84 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 20 Nov 2023 15:15:26 +0100 Subject: [PATCH 09/41] Added image application --- project/Core/Src/mug.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 4e2bbf1..d6fa9f5 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -51,16 +51,32 @@ static void example_publish(mqtt_client_t *client, void *arg) { llfs_file_t file_list[max_files]; size_t num_files; - num_files = llfs_file_list(file_list, max_files, "*.png"); + num_files = llfs_file_list(file_list, max_files, "*.bmp"); if(num_files == 0) { strcpy(pub_payload, "No images found"); } else { + strcat(pub_payload, "Available images: "); 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 space between file names + strcat(pub_payload, ", "); // Add a comma between file names } + strcat(pub_payload, "\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: "); + 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 + } + strcat(pub_payload, "\0"); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if(err != ERR_OK) { @@ -94,22 +110,22 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f switch(inpub_id) { case 0: //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); - LOG_INFO(TAG, "incoming data on input/setText: %s.", data); + LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: //places an image on the lcd - LOG_INFO(TAG, "incoming data on input/setImage: %s.", data); - //call function here + LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); break; case 2: //changes the text color on the lcd - LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data); + LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer); color = color_picker(data_buffer); break; case 3: //changes the background color - LOG_INFO(TAG, "incoming data on input/setColor: %s.", data); + LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer); bgcolor = color_picker(data_buffer); break; default: From b52e7b0bc8c4894072d6c0a53de9be8510953950 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Mon, 20 Nov 2023 15:42:43 +0100 Subject: [PATCH 10/41] Added support for gifs --- project/Core/Src/mug.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index d6fa9f5..c6d0843 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -53,6 +53,8 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); + strcpy(pub_payload, "\0"); + if(num_files == 0) { strcpy(pub_payload, "No images found"); } else { @@ -116,7 +118,12 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f case 1: //places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); - lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + if(data_buffer[len-3] == 'b') { + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + } + if(data_buffer[len-3] == 'g') { + lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + } break; case 2: //changes the text color on the lcd From 4da408dadf25cc41fe6ab8b16cbc9b80d92b12ce Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Thu, 23 Nov 2023 17:57:26 +0100 Subject: [PATCH 11/41] Added lcd_clear_text() and lcd_clear_images() --- project/Core/Src/mug.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index c6d0843..a8c887a 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -24,7 +24,7 @@ static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void example_do_connect(mqtt_client_t*); -static uint32_t color_picker(uint8_t*); +static uint32_t color_picker(char*); //global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static int inpub_id; @@ -41,6 +41,10 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { } } +/** + * @brief Publishes data + * Publishes the names of all the .bmp and .gif files on the topic getImageList + */ static void example_publish(mqtt_client_t *client, void *arg) { LOG_INFO(TAG, "Entering publish"); char pub_payload[200]; @@ -86,6 +90,10 @@ static void example_publish(mqtt_client_t *client, void *arg) { } } +/** + * @brief Handles incoming publish + * Callback function for when data was published to a subscribed topic + */ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); //check for which topic a publish was received @@ -103,21 +111,27 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len } } +/** + * @brief Handles incoming publish data + * Handles the recieved data from a publish to a subscribed topic + */ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { - uint8_t data_buffer[len + 1]; + char data_buffer[len + 1]; LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); if(flags & MQTT_DATA_FLAG_LAST) { memcpy(data_buffer, data, len); data_buffer[len] = '\0'; switch(inpub_id) { case 0: - //lcd_display_text("teststring", xpos, ypos, color, bgcolor, font); + //places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); - lcd_display_text(data_buffer, xpos, ypos, color, bgcolor, font); + lcd_clear_text(); + lcd_display_text((const char*)data_buffer, xpos, ypos, color, font); break; case 1: //places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); + lcd_clear_images(); if(data_buffer[len-3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } @@ -126,12 +140,12 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } break; case 2: - //changes the text color on the lcd + //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: - //changes the background 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); break; @@ -143,6 +157,9 @@ static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t f } } +/** + * @brief Callback function for outgoing subscribe request + */ static void mqtt_sub_request_cb(void *arg, err_t result) { LOG_INFO(TAG, "Subscribe result: %d", result); } @@ -161,8 +178,8 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection if(err != ERR_OK) { LOG_INFO(TAG, "mqtt_subscribe return: %d", err); } - //publish list of images here + //publish list of images here example_publish(client, NULL); } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); @@ -206,7 +223,7 @@ void mug_init(void) { } } -uint32_t color_picker(uint8_t* color) { +uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; if(strcmp((const char*)color, "blue") == 0) { output = LCD_BLUE; From 7f5ba2a6f76832bab428d5f8efa0b70e8246d3e1 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Thu, 23 Nov 2023 18:57:53 +0100 Subject: [PATCH 12/41] Started implementing style guide --- project/Core/Src/mug.c | 125 +++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 60 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index a8c887a..0eae03d 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -18,22 +18,22 @@ //Function prototypes static void mqtt_pub_request_cb(void*, err_t); -static void example_publish(mqtt_client_t* , void*); +static void publish_data(mqtt_client_t* , void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); -static void example_do_connect(mqtt_client_t*); +static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); -//global variable used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics -static int inpub_id; -static const char *TAG = "mug"; +//global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics +static sFONT* font; static uint16_t xpos; static uint16_t ypos; -static sFONT* font; static uint32_t color; static uint32_t bgcolor; +static int inpub_id; +static const char *TAG = "mug"; static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { @@ -45,21 +45,22 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { * @brief Publishes data * Publishes the names of all the .bmp and .gif files on the topic getImageList */ -static void example_publish(mqtt_client_t *client, void *arg) { - LOG_INFO(TAG, "Entering publish"); +static void publish_data(mqtt_client_t *client, void *arg) { char pub_payload[200]; err_t err; + size_t max_files = 20; + size_t num_files; + llfs_file_t file_list[max_files]; u8_t qos = 2; u8_t retain = 1 ; - size_t max_files = 20; - llfs_file_t file_list[max_files]; - size_t num_files; + + LOG_DEBUG(TAG, "Entering publish"); num_files = llfs_file_list(file_list, max_files, "*.bmp"); strcpy(pub_payload, "\0"); - if(num_files == 0) { + if (num_files == 0) { strcpy(pub_payload, "No images found"); } else { strcat(pub_payload, "Available images: "); @@ -73,7 +74,7 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.gif"); - if(num_files == 0) { + if (num_files == 0) { strcpy(pub_payload, "No gifs found"); } else { strcat(pub_payload, "Available gifs: "); @@ -85,7 +86,7 @@ static void example_publish(mqtt_client_t *client, void *arg) { strcat(pub_payload, "\0"); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); - if(err != ERR_OK) { + if (err != ERR_OK) { LOG_INFO(TAG, "Publish err: %d", err); } } @@ -94,16 +95,16 @@ static void example_publish(mqtt_client_t *client, void *arg) { * @brief Handles incoming publish * Callback function for when data was published to a subscribed topic */ -static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) { - LOG_INFO(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); +static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_len) { + LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); //check for which topic a publish was received - if(strcmp(topic, "input/setText") == 0) { + if (strcmp(topic, "input/setText") == 0) { inpub_id = 0; - } else if(strcmp(topic, "input/setImage") == 0) { + } else if (strcmp(topic, "input/setImage") == 0) { inpub_id = 1; - } else if(strcmp(topic, "input/setTextColor") == 0) { + } else if (strcmp(topic, "input/setTextColor") == 0) { inpub_id = 2; - } else if(strcmp(topic, "input/setColor") == 0) { + } else if (strcmp(topic, "input/setColor") == 0) { inpub_id = 3; } else { //in case of wrong topic @@ -115,13 +116,14 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len * @brief Handles incoming publish data * Handles the recieved data from a publish to a subscribed topic */ -static void mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) { +static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); - if(flags & MQTT_DATA_FLAG_LAST) { + if (flags & MQTT_DATA_FLAG_LAST) { memcpy(data_buffer, data, len); data_buffer[len] = '\0'; - switch(inpub_id) { + switch (inpub_id) { case 0: //places text on the lcd LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); @@ -165,9 +167,10 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { } static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { - LOG_INFO(TAG, "entered connection cb function"); err_t err; - if(status == MQTT_CONNECT_ACCEPTED) { + + LOG_INFO(TAG, "entered connection cb function"); + if (status == MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); //set up callback function for input @@ -175,25 +178,26 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection //subscribe to the topics setText, setImage, setColor and setTextcolor err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); - if(err != ERR_OK) { + if (err != ERR_OK) { LOG_INFO(TAG, "mqtt_subscribe return: %d", err); } //publish list of images here - example_publish(client, NULL); + publish_data(client, NULL); } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); //try to reconnect - example_do_connect(client); + mosquitto_connect(client); } } -static void example_do_connect(mqtt_client_t *client) { - LOG_INFO(TAG, "testing the connection"); +static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; err_t err; + LOG_INFO(TAG, "testing the connection"); + memset(&ci, 0, sizeof(ci)); ci.client_id = "lwip_test"; @@ -201,10 +205,10 @@ static void example_do_connect(mqtt_client_t *client) { IP4_ADDR(&server_ip, 192,168,69,11); uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); - if(err != ERR_OK) { + if (err != ERR_OK) { LOG_INFO(TAG, "mqtt_connect return %d", err); } - if(err == ERR_OK) { + if (err == ERR_OK) { LOG_INFO(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } } @@ -217,90 +221,91 @@ void mug_init(void) { ypos = 50; mqtt_client_t *client = mqtt_client_new(); - if(client != NULL) { + if (client != NULL) { LOG_INFO(TAG, "Starting connection test"); - example_do_connect(client); + mosquitto_connect(client); } } uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; - if(strcmp((const char*)color, "blue") == 0) { + + if (strcmp((const char*)color, "blue") == 0) { output = LCD_BLUE; } - if(strcmp((const char*)color, "green") == 0) { + if (strcmp((const char*)color, "green") == 0) { output = LCD_GREEN; } - if(strcmp((const char*)color, "red") == 0) { + if (strcmp((const char*)color, "red") == 0) { output = LCD_RED; } - if(strcmp((const char*)color, "cyan") == 0) { + if (strcmp((const char*)color, "cyan") == 0) { output = LCD_CYAN; } - if(strcmp((const char*)color, "magenta") == 0) { + if (strcmp((const char*)color, "magenta") == 0) { output = LCD_MAGENTA; } - if(strcmp((const char*)color, "yellow") == 0) { + if (strcmp((const char*)color, "yellow") == 0) { output = LCD_YELLOW; } - if(strcmp((const char*)color, "light blue") == 0) { + if (strcmp((const char*)color, "light blue") == 0) { output = LCD_LIGHTBLUE; } - if(strcmp((const char*)color, "light green") == 0) { + if (strcmp((const char*)color, "light green") == 0) { output = LCD_LIGHTGREEN; } - if(strcmp((const char*)color, "light red") == 0) { + if (strcmp((const char*)color, "light red") == 0) { output = LCD_LIGHTRED; } - if(strcmp((const char*)color, "light cyan") == 0) { + if (strcmp((const char*)color, "light cyan") == 0) { output = LCD_LIGHTCYAN; } - if(strcmp((const char*)color, "light magenta") == 0) { + if (strcmp((const char*)color, "light magenta") == 0) { output = LCD_LIGHTMAGENTA; } - if(strcmp((const char*)color, "light yellow") == 0) { + if (strcmp((const char*)color, "light yellow") == 0) { output = LCD_LIGHTYELLOW; } - if(strcmp((const char*)color, "dark blue") == 0) { + if (strcmp((const char*)color, "dark blue") == 0) { output = LCD_DARKBLUE; } - if(strcmp((const char*)color, "dark green") == 0) { + if (strcmp((const char*)color, "dark green") == 0) { output = LCD_DARKGREEN; } - if(strcmp((const char*)color, "dark red") == 0) { + if (strcmp((const char*)color, "dark red") == 0) { output = LCD_DARKRED; } - if(strcmp((const char*)color, "dark cyan") == 0) { + if (strcmp((const char*)color, "dark cyan") == 0) { output = LCD_DARKCYAN; } - if(strcmp((const char*)color, "dark magenta") == 0) { + if (strcmp((const char*)color, "dark magenta") == 0) { output = LCD_DARKMAGENTA; } - if(strcmp((const char*)color, "dark yellow") == 0) { + if (strcmp((const char*)color, "dark yellow") == 0) { output = LCD_DARKYELLOW; } - if(strcmp((const char*)color, "white") == 0) { + if (strcmp((const char*)color, "white") == 0) { output = LCD_WHITE; } - if(strcmp((const char*)color, "light gray") == 0) { + if (strcmp((const char*)color, "light gray") == 0) { output = LCD_LIGHTGRAY; } - if(strcmp((const char*)color, "gray") == 0) { + if (strcmp((const char*)color, "gray") == 0) { output = LCD_GRAY; } - if(strcmp((const char*)color, "dark gray") == 0) { + if (strcmp((const char*)color, "dark gray") == 0) { output = LCD_DARKGRAY; } - if(strcmp((const char*)color, "black") == 0) { + if (strcmp((const char*)color, "black") == 0) { output = LCD_BLACK; } - if(strcmp((const char*)color, "brown") == 0) { + if (strcmp((const char*)color, "brown") == 0) { output = LCD_BROWN; } - if(strcmp((const char*)color, "orange") == 0) { + if (strcmp((const char*)color, "orange") == 0) { output = LCD_ORANGE; } - if(strcmp((const char*)color, "transparent") == 0) { + if (strcmp((const char*)color, "transparent") == 0) { output = LCD_TRANSPARENT; } From fb8abbb7532e650ba48257eee754ba3e68726484 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 24 Nov 2023 02:27:31 +0100 Subject: [PATCH 13/41] MQTT Change color_picker by using if is blue return color blue --- project/Core/Src/mug.c | 55 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index a8c887a..3dc8522 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -224,85 +224,84 @@ void mug_init(void) { } uint32_t color_picker(char* color) { - uint32_t output = LCD_BLACK; if(strcmp((const char*)color, "blue") == 0) { - output = LCD_BLUE; + return LCD_BLUE; } if(strcmp((const char*)color, "green") == 0) { - output = LCD_GREEN; + return LCD_GREEN; } if(strcmp((const char*)color, "red") == 0) { - output = LCD_RED; + return LCD_RED; } if(strcmp((const char*)color, "cyan") == 0) { - output = LCD_CYAN; + return LCD_CYAN; } if(strcmp((const char*)color, "magenta") == 0) { - output = LCD_MAGENTA; + return LCD_MAGENTA; } if(strcmp((const char*)color, "yellow") == 0) { - output = LCD_YELLOW; + return LCD_YELLOW; } if(strcmp((const char*)color, "light blue") == 0) { - output = LCD_LIGHTBLUE; + return LCD_LIGHTBLUE; } if(strcmp((const char*)color, "light green") == 0) { - output = LCD_LIGHTGREEN; + return LCD_LIGHTGREEN; } if(strcmp((const char*)color, "light red") == 0) { - output = LCD_LIGHTRED; + return LCD_LIGHTRED; } if(strcmp((const char*)color, "light cyan") == 0) { - output = LCD_LIGHTCYAN; + return LCD_LIGHTCYAN; } if(strcmp((const char*)color, "light magenta") == 0) { - output = LCD_LIGHTMAGENTA; + return LCD_LIGHTMAGENTA; } if(strcmp((const char*)color, "light yellow") == 0) { - output = LCD_LIGHTYELLOW; + return LCD_LIGHTYELLOW; } if(strcmp((const char*)color, "dark blue") == 0) { - output = LCD_DARKBLUE; + return LCD_DARKBLUE; } if(strcmp((const char*)color, "dark green") == 0) { - output = LCD_DARKGREEN; + return LCD_DARKGREEN; } if(strcmp((const char*)color, "dark red") == 0) { - output = LCD_DARKRED; + return LCD_DARKRED; } if(strcmp((const char*)color, "dark cyan") == 0) { - output = LCD_DARKCYAN; + return LCD_DARKCYAN; } if(strcmp((const char*)color, "dark magenta") == 0) { - output = LCD_DARKMAGENTA; + return LCD_DARKMAGENTA; } if(strcmp((const char*)color, "dark yellow") == 0) { - output = LCD_DARKYELLOW; + return LCD_DARKYELLOW; } if(strcmp((const char*)color, "white") == 0) { - output = LCD_WHITE; + return LCD_WHITE; } if(strcmp((const char*)color, "light gray") == 0) { - output = LCD_LIGHTGRAY; + return LCD_LIGHTGRAY; } if(strcmp((const char*)color, "gray") == 0) { - output = LCD_GRAY; + return LCD_GRAY; } if(strcmp((const char*)color, "dark gray") == 0) { - output = LCD_DARKGRAY; + return LCD_DARKGRAY; } if(strcmp((const char*)color, "black") == 0) { - output = LCD_BLACK; + return LCD_BLACK; } if(strcmp((const char*)color, "brown") == 0) { - output = LCD_BROWN; + return LCD_BROWN; } if(strcmp((const char*)color, "orange") == 0) { - output = LCD_ORANGE; + return LCD_ORANGE; } if(strcmp((const char*)color, "transparent") == 0) { - output = LCD_TRANSPARENT; + return LCD_TRANSPARENT; } - return output; + return LCD_BLACK; } From d819c82e59ffa3ca819130c216779731870f4069 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Fri, 24 Nov 2023 02:28:31 +0100 Subject: [PATCH 14/41] MQTT Change payload clearing to initialisation instead of using strcpy --- project/Core/Src/mug.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 3dc8522..9553afe 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -47,7 +47,7 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { */ static void example_publish(mqtt_client_t *client, void *arg) { LOG_INFO(TAG, "Entering publish"); - char pub_payload[200]; + char pub_payload[200] = {0}; err_t err; u8_t qos = 2; u8_t retain = 1 ; @@ -57,8 +57,6 @@ static void example_publish(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); - strcpy(pub_payload, "\0"); - if(num_files == 0) { strcpy(pub_payload, "No images found"); } else { From 21f30bfd557ed089cd8e89a5a6ebb830155ac3c6 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Fri, 24 Nov 2023 17:34:24 +0100 Subject: [PATCH 15/41] Added documentation --- project/Core/Src/mug.c | 72 +++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 0eae03d..e50afbf 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -46,7 +46,7 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { * Publishes the names of all the .bmp and .gif files on the topic getImageList */ static void publish_data(mqtt_client_t *client, void *arg) { - char pub_payload[200]; + char pub_payload[200] = {0}; err_t err; size_t max_files = 20; size_t num_files; @@ -58,8 +58,6 @@ static void publish_data(mqtt_client_t *client, void *arg) { num_files = llfs_file_list(file_list, max_files, "*.bmp"); - strcpy(pub_payload, "\0"); - if (num_files == 0) { strcpy(pub_payload, "No images found"); } else { @@ -166,6 +164,11 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { LOG_INFO(TAG, "Subscribe result: %d", result); } +/** + * @brief Callback function for attempting a connection + * If a connection was made setup a callback function for incoming publishes. + * subscribes to the input topics and calls the publish_data function. + */ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { err_t err; @@ -192,6 +195,10 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } } +/** + * @brief Attempts to create a connection to the mosquitto broker + * Creates a mqtt client and sets up a connection callback function + */ static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; err_t err; @@ -213,6 +220,10 @@ static void mosquitto_connect(mqtt_client_t *client) { } } +/** + * @brief Init function for the mosquitto application of the assignment + * Gives the global variables a value and calls the mosquitto_connect function + */ void mug_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; @@ -227,86 +238,89 @@ void mug_init(void) { } } +/** + * @brief Reads the color input string and outputs it to a useable value for LCD_APi + */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; if (strcmp((const char*)color, "blue") == 0) { - output = LCD_BLUE; + return LCD_BLUE; } if (strcmp((const char*)color, "green") == 0) { - output = LCD_GREEN; + return LCD_GREEN; } if (strcmp((const char*)color, "red") == 0) { - output = LCD_RED; + return LCD_RED; } if (strcmp((const char*)color, "cyan") == 0) { - output = LCD_CYAN; + return LCD_CYAN; } if (strcmp((const char*)color, "magenta") == 0) { - output = LCD_MAGENTA; + return LCD_MAGENTA; } if (strcmp((const char*)color, "yellow") == 0) { - output = LCD_YELLOW; + return LCD_YELLOW; } if (strcmp((const char*)color, "light blue") == 0) { - output = LCD_LIGHTBLUE; + return LCD_LIGHTBLUE; } if (strcmp((const char*)color, "light green") == 0) { - output = LCD_LIGHTGREEN; + return LCD_LIGHTGREEN; } if (strcmp((const char*)color, "light red") == 0) { - output = LCD_LIGHTRED; + return LCD_LIGHTRED; } if (strcmp((const char*)color, "light cyan") == 0) { - output = LCD_LIGHTCYAN; + return LCD_LIGHTCYAN; } if (strcmp((const char*)color, "light magenta") == 0) { - output = LCD_LIGHTMAGENTA; + return LCD_LIGHTMAGENTA; } if (strcmp((const char*)color, "light yellow") == 0) { - output = LCD_LIGHTYELLOW; + return LCD_LIGHTYELLOW; } if (strcmp((const char*)color, "dark blue") == 0) { - output = LCD_DARKBLUE; + return LCD_DARKBLUE; } if (strcmp((const char*)color, "dark green") == 0) { - output = LCD_DARKGREEN; + return LCD_DARKGREEN; } if (strcmp((const char*)color, "dark red") == 0) { - output = LCD_DARKRED; + return LCD_DARKRED; } if (strcmp((const char*)color, "dark cyan") == 0) { - output = LCD_DARKCYAN; + return LCD_DARKCYAN; } if (strcmp((const char*)color, "dark magenta") == 0) { - output = LCD_DARKMAGENTA; + return LCD_DARKMAGENTA; } if (strcmp((const char*)color, "dark yellow") == 0) { - output = LCD_DARKYELLOW; + return LCD_DARKYELLOW; } if (strcmp((const char*)color, "white") == 0) { - output = LCD_WHITE; + return LCD_WHITE; } if (strcmp((const char*)color, "light gray") == 0) { - output = LCD_LIGHTGRAY; + return LCD_LIGHTGRAY; } if (strcmp((const char*)color, "gray") == 0) { - output = LCD_GRAY; + return LCD_GRAY; } if (strcmp((const char*)color, "dark gray") == 0) { - output = LCD_DARKGRAY; + return LCD_DARKGRAY; } if (strcmp((const char*)color, "black") == 0) { - output = LCD_BLACK; + return LCD_BLACK; } if (strcmp((const char*)color, "brown") == 0) { - output = LCD_BROWN; + return LCD_BROWN; } if (strcmp((const char*)color, "orange") == 0) { - output = LCD_ORANGE; + return LCD_ORANGE; } if (strcmp((const char*)color, "transparent") == 0) { - output = LCD_TRANSPARENT; + return LCD_TRANSPARENT; } return output; From 7fc1560bb730ba3ac4e9764ef002b83d58a94c69 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Fri, 24 Nov 2023 17:55:51 +0100 Subject: [PATCH 16/41] Changed some LOG_INFO to LOG_DEBUG --- project/Core/Src/mug.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index e50afbf..002cc7b 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -37,7 +37,7 @@ static const char *TAG = "mug"; static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { - LOG_INFO(TAG, "Publish result: %d", result); + LOG_DEBUG(TAG, "Publish result: %d", result); } } @@ -85,7 +85,7 @@ static void publish_data(mqtt_client_t *client, void *arg) { } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { - LOG_INFO(TAG, "Publish err: %d", err); + LOG_DEBUG(TAG, "Publish err: %d", err); } } @@ -126,12 +126,13 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, //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, font); + lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); break; case 1: //places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); + lcd_set_bg_color_layer0(bgcolor); if(data_buffer[len-3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } @@ -161,7 +162,7 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, * @brief Callback function for outgoing subscribe request */ static void mqtt_sub_request_cb(void *arg, err_t result) { - LOG_INFO(TAG, "Subscribe result: %d", result); + LOG_DEBUG(TAG, "Subscribe result: %d", result); } /** @@ -172,9 +173,8 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { err_t err; - LOG_INFO(TAG, "entered connection cb function"); if (status == MQTT_CONNECT_ACCEPTED) { - LOG_INFO(TAG, "mqtt_connection_cb: Successfully connected"); + LOG_INFO(TAG, "Successfully connected"); //set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); @@ -182,7 +182,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection //subscribe to the topics setText, setImage, setColor and setTextcolor err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); if (err != ERR_OK) { - LOG_INFO(TAG, "mqtt_subscribe return: %d", err); + LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); } //publish list of images here @@ -203,20 +203,20 @@ static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; err_t err; - LOG_INFO(TAG, "testing the connection"); + LOG_INFO(TAG, "Attempting MQTT Connection"); memset(&ci, 0, sizeof(ci)); - ci.client_id = "lwip_test"; + ci.client_id = "STM32"; ip_addr_t server_ip; IP4_ADDR(&server_ip, 192,168,69,11); uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { - LOG_INFO(TAG, "mqtt_connect return %d", err); + LOG_DEBUG(TAG, "mqtt_connect return %d", err); } if (err == ERR_OK) { - LOG_INFO(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); + LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } } @@ -233,7 +233,7 @@ void mug_init(void) { mqtt_client_t *client = mqtt_client_new(); if (client != NULL) { - LOG_INFO(TAG, "Starting connection test"); + LOG_DEBUG(TAG, "Starting connection test"); mosquitto_connect(client); } } From e6dcbebdc8ffb9958884a1675ec46fd52a4ef932 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Fri, 24 Nov 2023 18:00:02 +0100 Subject: [PATCH 17/41] Fixed infinit loop when MQTT broker closed --- project/Core/Src/mug.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 002cc7b..160560c 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -30,6 +30,7 @@ static uint32_t color_picker(char*); static sFONT* font; static uint16_t xpos; static uint16_t ypos; +static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; static int inpub_id; @@ -190,8 +191,12 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - //try to reconnect - mosquitto_connect(client); + while (connection_attempt_counter < 50) { + connection_attempt_counter++; + //try to reconnect + mosquitto_connect(client); + } + } } @@ -230,6 +235,7 @@ void mug_init(void) { font = LCD_FONT16; xpos = 50; ypos = 50; + connection_attempt_counter = 0; mqtt_client_t *client = mqtt_client_new(); if (client != NULL) { From 60b9b53b28b5b13e3d9572eb3ebbb8e639477867 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 17:24:45 +0100 Subject: [PATCH 18/41] Added reset for connection_attempt_counter --- project/Core/Src/mug.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 160560c..cfe8603 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -177,6 +177,7 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection if (status == MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "Successfully connected"); + connection_attempt_counter = 0; //set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); From 4147f53802f7beb415982aaf06ba34d5d81b6944 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 17:41:25 +0100 Subject: [PATCH 19/41] Added documentation to .h file --- project/Core/Inc/mug.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index 5285f89..788610e 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,6 +7,8 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ +/* @brief Initialise MQTT application + */ void mug_init(void); #endif /* INC_MUG_H_ */ From 7cbee6afbbcb3bf3fbf31c5e033d994d71828ba0 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 20:01:45 +0100 Subject: [PATCH 20/41] Added parameter info to doxygen --- project/Core/Src/mug.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index cfe8603..b830ca1 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -36,6 +36,12 @@ static uint32_t bgcolor; static int inpub_id; static const char *TAG = "mug"; +/** + * @brief callback function for publishing data + * + * @param[in] arg + * @param[in] result whether the publish was successfull or not + */ static void mqtt_pub_request_cb(void *arg, err_t result) { if(result != ERR_OK) { LOG_DEBUG(TAG, "Publish result: %d", result); @@ -45,6 +51,9 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { /** * @brief Publishes data * Publishes the names of all the .bmp and .gif files on the topic getImageList + * + * @param[in] client pointer to the MQTT client + * @param[in] arg */ static void publish_data(mqtt_client_t *client, void *arg) { char pub_payload[200] = {0}; @@ -93,6 +102,10 @@ static void publish_data(mqtt_client_t *client, void *arg) { /** * @brief Handles incoming publish * Callback function for when data was published to a subscribed topic + * + * @param[in] arg + * @param[in] topic The topic on which an incomming publish was received + * @param[in] tot_len length of the incoming data */ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_len) { LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); @@ -114,6 +127,11 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_ /** * @brief Handles incoming publish data * Handles the recieved data from a publish to a subscribed topic + * + * @param[in] arg + * @param[in] data the incoming data + * @param[in] len length of the data + * @param[in] flags Whether this is the last fragment of the incoming data */ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; @@ -161,6 +179,9 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, /** * @brief Callback function for outgoing subscribe request + * + * @param[in] arg + * @param[in] result */ static void mqtt_sub_request_cb(void *arg, err_t result) { LOG_DEBUG(TAG, "Subscribe result: %d", result); @@ -170,6 +191,10 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { * @brief Callback function for attempting a connection * If a connection was made setup a callback function for incoming publishes. * subscribes to the input topics and calls the publish_data function. + * + * @param[in] client pointer to the MQTT client + * @param[in] arg + * @param[in] status Connect result code or disconnection notification */ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { err_t err; @@ -204,6 +229,8 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection /** * @brief Attempts to create a connection to the mosquitto broker * Creates a mqtt client and sets up a connection callback function + * + * @param[in] client pointer to the MQTT client */ static void mosquitto_connect(mqtt_client_t *client) { struct mqtt_connect_client_info_t ci; @@ -247,6 +274,9 @@ void mug_init(void) { /** * @brief Reads the color input string and outputs it to a useable value for LCD_APi + * + * @param[out] color define to use with the LCD_API + * @param[in] color input string to select a color */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; From ee1753b43e77acb90ecce9c96d005fd6f626d14e Mon Sep 17 00:00:00 2001 From: L-diy Date: Sat, 25 Nov 2023 20:24:57 +0100 Subject: [PATCH 21/41] Reformat `mug.c` and `mug.h` --- project/Core/Inc/mug.h | 3 +- project/Core/Src/mug.c | 86 +++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index 788610e..ba1baa8 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,7 +7,8 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ -/* @brief Initialise MQTT application +/** + * @brief Initialise MQTT application */ void mug_init(void); diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index b830ca1..61bcf24 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -4,21 +4,21 @@ * @author RobinVdB */ -#include "mdns.h" -#include "httpd.h" -#include "lwip/apps/fs.h" #include +#include "httpd.h" +#include "lcd_api.h" +#include "lwip/apps/fs.h" #include "lwip/ip_addr.h" +#include "mdns.h" #include "mqtt.h" #include "mug.h" -#include "lcd_api.h" #define LOGGER_LEVEL_INFO #include "log.h" -//Function prototypes +// Function prototypes static void mqtt_pub_request_cb(void*, err_t); -static void publish_data(mqtt_client_t* , void*); +static void publish_data(mqtt_client_t*, void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); @@ -26,7 +26,7 @@ static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); -//global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics +// Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static sFONT* font; static uint16_t xpos; static uint16_t ypos; @@ -34,16 +34,16 @@ static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; static int inpub_id; -static const char *TAG = "mug"; +static const char* TAG = "mug"; /** * @brief callback function for publishing data * * @param[in] arg - * @param[in] result whether the publish was successfull or not + * @param[in] result Whether the publish was successful or not */ -static void mqtt_pub_request_cb(void *arg, err_t result) { - if(result != ERR_OK) { +static void mqtt_pub_request_cb(void* arg, err_t result) { + if (result != ERR_OK) { LOG_DEBUG(TAG, "Publish result: %d", result); } } @@ -52,17 +52,17 @@ static void mqtt_pub_request_cb(void *arg, err_t result) { * @brief Publishes data * Publishes the names of all the .bmp and .gif files on the topic getImageList * - * @param[in] client pointer to the MQTT client + * @param[in] client Pointer to the MQTT client * @param[in] arg */ -static void publish_data(mqtt_client_t *client, void *arg) { +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 num_files; llfs_file_t file_list[max_files]; u8_t qos = 2; - u8_t retain = 1 ; + u8_t retain = 1; LOG_DEBUG(TAG, "Entering publish"); @@ -104,12 +104,12 @@ static void publish_data(mqtt_client_t *client, void *arg) { * Callback function for when data was published to a subscribed topic * * @param[in] arg - * @param[in] topic The topic on which an incomming publish was received - * @param[in] tot_len length of the incoming data + * @param[in] topic The topic on which an incoming publish was received + * @param[in] tot_len Length of the incoming data */ -static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_len) { +static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_len) { LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); - //check for which topic a publish was received + // Check for which topic a publish was received if (strcmp(topic, "input/setText") == 0) { inpub_id = 0; } else if (strcmp(topic, "input/setImage") == 0) { @@ -119,7 +119,7 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_ } else if (strcmp(topic, "input/setColor") == 0) { inpub_id = 3; } else { - //in case of wrong topic + // In case of wrong topic inpub_id = 4; } } @@ -129,11 +129,11 @@ static void mqtt_incoming_publish_cb(void *arg, const char *topic, uint32_t tot_ * Handles the recieved data from a publish to a subscribed topic * * @param[in] arg - * @param[in] data the incoming data - * @param[in] len length of the data + * @param[in] data The incoming data + * @param[in] len Length of the data * @param[in] flags Whether this is the last fragment of the incoming data */ -static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, uint8_t flags) { +static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); @@ -142,30 +142,30 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, data_buffer[len] = '\0'; switch (inpub_id) { case 0: - //places text on the lcd + // 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: - //places an image on the lcd + // Places an image on the lcd LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); lcd_set_bg_color_layer0(bgcolor); - if(data_buffer[len-3] == 'b') { + if (data_buffer[len - 3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } - if(data_buffer[len-3] == 'g') { + if (data_buffer[len - 3] == 'g') { lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); } break; case 2: - //changes the text color for the next time text is written + // 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: - //changes the background color for the next time text is written + // 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); break; @@ -173,7 +173,6 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); } } else { - } } @@ -183,7 +182,7 @@ static void mqtt_incoming_data_cb(void *arg, const uint8_t *data, uint16_t len, * @param[in] arg * @param[in] result */ -static void mqtt_sub_request_cb(void *arg, err_t result) { +static void mqtt_sub_request_cb(void* arg, err_t result) { LOG_DEBUG(TAG, "Subscribe result: %d", result); } @@ -192,37 +191,36 @@ static void mqtt_sub_request_cb(void *arg, err_t result) { * If a connection was made setup a callback function for incoming publishes. * subscribes to the input topics and calls the publish_data function. * - * @param[in] client pointer to the MQTT client + * @param[in] client Pointer to the MQTT client * @param[in] arg * @param[in] status Connect result code or disconnection notification */ -static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) { +static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) { err_t err; if (status == MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "Successfully connected"); connection_attempt_counter = 0; - //set up callback function for input + // Set up callback function for input mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - //subscribe to the topics setText, setImage, setColor and setTextcolor + // Subscribe to the topics setText, setImage, setColor and setTextcolor err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); } - //publish list of images here + // Publish list of images here publish_data(client, NULL); } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); while (connection_attempt_counter < 50) { connection_attempt_counter++; - //try to reconnect + // Try to reconnect mosquitto_connect(client); } - } } @@ -230,9 +228,9 @@ static void mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection * @brief Attempts to create a connection to the mosquitto broker * Creates a mqtt client and sets up a connection callback function * - * @param[in] client pointer to the MQTT client + * @param[in] client Pointer to the MQTT client */ -static void mosquitto_connect(mqtt_client_t *client) { +static void mosquitto_connect(mqtt_client_t* client) { struct mqtt_connect_client_info_t ci; err_t err; @@ -242,7 +240,7 @@ static void mosquitto_connect(mqtt_client_t *client) { ci.client_id = "STM32"; ip_addr_t server_ip; - IP4_ADDR(&server_ip, 192,168,69,11); + IP4_ADDR(&server_ip, 192, 168, 69, 11); uint16_t server_port = 1883; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { @@ -265,7 +263,7 @@ void mug_init(void) { ypos = 50; connection_attempt_counter = 0; - mqtt_client_t *client = mqtt_client_new(); + mqtt_client_t* client = mqtt_client_new(); if (client != NULL) { LOG_DEBUG(TAG, "Starting connection test"); mosquitto_connect(client); @@ -273,10 +271,10 @@ void mug_init(void) { } /** - * @brief Reads the color input string and outputs it to a useable value for LCD_APi + * @brief Reads the color input string and outputs it to a usable value for LCD_APi * - * @param[out] color define to use with the LCD_API - * @param[in] color input string to select a color + * @param[out] color Define to use with the LCD_API + * @param[in] color Input string to select a color */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; From 875edb52b589f1b89482544c3c89b3a152467a5d Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 20:30:12 +0100 Subject: [PATCH 22/41] Solved reviews --- project/Core/Inc/mug.h | 3 ++- project/Core/Src/main.c | 4 +++- project/Core/Src/mug.c | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index 788610e..a1df453 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -7,7 +7,8 @@ #ifndef INC_MUG_H_ #define INC_MUG_H_ -/* @brief Initialise MQTT application +/** + * @brief Initialize MQTT application */ void mug_init(void); diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 6e0105e..9adc667 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -133,11 +133,13 @@ int main(void) /* Initialize the tftp server */ tftp_server_init(); + + /* Initialize the MQTT application */ + mug_init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ - mug_init(); while (1) { /* USER CODE END WHILE */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index b830ca1..193b030 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -4,10 +4,10 @@ * @author RobinVdB */ +#include #include "mdns.h" #include "httpd.h" #include "lwip/apps/fs.h" -#include #include "lwip/ip_addr.h" #include "mqtt.h" #include "mug.h" @@ -18,7 +18,7 @@ //Function prototypes static void mqtt_pub_request_cb(void*, err_t); -static void publish_data(mqtt_client_t* , void*); +static void publish_data(mqtt_client_t*, void*); static void mqtt_incoming_publish_cb(void*, const char*, u32_t); static void mqtt_incoming_data_cb(void*, const uint8_t*, u16_t, u8_t); static void mqtt_sub_request_cb(void*, err_t); @@ -62,7 +62,7 @@ static void publish_data(mqtt_client_t *client, void *arg) { size_t num_files; llfs_file_t file_list[max_files]; u8_t qos = 2; - u8_t retain = 1 ; + u8_t retain = 1; LOG_DEBUG(TAG, "Entering publish"); @@ -275,7 +275,7 @@ void mug_init(void) { /** * @brief Reads the color input string and outputs it to a useable value for LCD_APi * - * @param[out] color define to use with the LCD_API + * @return color define to use with the LCD_API * @param[in] color input string to select a color */ uint32_t color_picker(char* color) { From 14e01e84fa595f8de8a8c7796e9aea16462dc404 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 20:43:08 +0100 Subject: [PATCH 23/41] Changed doxyegen order for color_picker --- project/Core/Src/mug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index f87ebc7..1bfd330 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -273,8 +273,8 @@ void mug_init(void) { /** * @brief Reads the color input string and outputs it to a usable value for LCD_APi * - * @return color Define to use with the LCD_API * @param[in] color Input string to select a color + * @return color Define to use with the LCD_API */ uint32_t color_picker(char* color) { uint32_t output = LCD_BLACK; From dea0d26141499ce1dc326fc078209035df416b43 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sat, 25 Nov 2023 21:57:54 +0100 Subject: [PATCH 24/41] Solved some reviews --- project/Core/Inc/mug.h | 2 +- project/Core/Src/mug.c | 81 ++++++++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mug.h index a1df453..1e3776c 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mug.h @@ -1,6 +1,6 @@ /** * @file mug.h - * @header for mosquitto application of the groups assignment + * header for mosquitto application of the groups assignment * @author RobinVdB */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 1bfd330..26ab3b6 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -1,6 +1,6 @@ /** * @file mug.c - * @mosquitto application for group assignment + * mosquitto application for group assignment * @author RobinVdB */ @@ -16,6 +16,13 @@ #define LOGGER_LEVEL_INFO #include "log.h" +#define ATTEMPT_RECONNECT_AMOUNT 50 +#define PUBLISH_QOS 2 +#define PUBLISH_RETAIN 1 +#define MQTT_SERVER_PORT 1883 +#define PRINT_XPOS 50 +#define PRINT_YPOS 50 + // Function prototypes static void mqtt_pub_request_cb(void*, err_t); static void publish_data(mqtt_client_t*, void*); @@ -61,8 +68,8 @@ static void publish_data(mqtt_client_t* client, void* arg) { size_t max_files = 20; size_t num_files; llfs_file_t file_list[max_files]; - u8_t qos = 2; - u8_t retain = 1; + u8_t qos = PUBLISH_QOS; + u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); @@ -108,7 +115,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { * @param[in] tot_len Length of the incoming data */ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_len) { - LOG_DEBUG(TAG, "Incoming publish at topic %s with total length %u", topic, (unsigned int)tot_len); + 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; @@ -135,6 +142,7 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ */ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, uint8_t flags) { char data_buffer[len + 1]; + lcd_gif_t* gif; LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); if (flags & MQTT_DATA_FLAG_LAST) { @@ -156,7 +164,10 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } if (data_buffer[len - 3] == 'g') { - lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + if (gif == NULL) { + LOG_INFO(TAG, "GIF could not be drawn"); + } } break; case 2: @@ -216,7 +227,7 @@ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection } else { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - while (connection_attempt_counter < 50) { + while (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { connection_attempt_counter++; // Try to reconnect mosquitto_connect(client); @@ -259,8 +270,8 @@ void mug_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; font = LCD_FONT16; - xpos = 50; - ypos = 50; + xpos = PRINT_XPOS; + ypos = PRINT_YPOS; connection_attempt_counter = 0; mqtt_client_t* client = mqtt_client_new(); @@ -276,85 +287,85 @@ void mug_init(void) { * @param[in] color Input string to select a color * @return color Define to use with the LCD_API */ -uint32_t color_picker(char* color) { +uint32_t color_picker(char* color_in) { uint32_t output = LCD_BLACK; - if (strcmp((const char*)color, "blue") == 0) { + if (strcmp((const char*)color_in, "blue") == 0) { return LCD_BLUE; } - if (strcmp((const char*)color, "green") == 0) { + if (strcmp((const char*)color_in, "green") == 0) { return LCD_GREEN; } - if (strcmp((const char*)color, "red") == 0) { + if (strcmp((const char*)color_in, "red") == 0) { return LCD_RED; } - if (strcmp((const char*)color, "cyan") == 0) { + if (strcmp((const char*)color_in, "cyan") == 0) { return LCD_CYAN; } - if (strcmp((const char*)color, "magenta") == 0) { + if (strcmp((const char*)color_in, "magenta") == 0) { return LCD_MAGENTA; } - if (strcmp((const char*)color, "yellow") == 0) { + if (strcmp((const char*)color_in, "yellow") == 0) { return LCD_YELLOW; } - if (strcmp((const char*)color, "light blue") == 0) { + if (strcmp((const char*)color_in, "light blue") == 0) { return LCD_LIGHTBLUE; } - if (strcmp((const char*)color, "light green") == 0) { + if (strcmp((const char*)color_in, "light green") == 0) { return LCD_LIGHTGREEN; } - if (strcmp((const char*)color, "light red") == 0) { + if (strcmp((const char*)color_in, "light red") == 0) { return LCD_LIGHTRED; } - if (strcmp((const char*)color, "light cyan") == 0) { + if (strcmp((const char*)color_in, "light cyan") == 0) { return LCD_LIGHTCYAN; } - if (strcmp((const char*)color, "light magenta") == 0) { + if (strcmp((const char*)color_in, "light magenta") == 0) { return LCD_LIGHTMAGENTA; } - if (strcmp((const char*)color, "light yellow") == 0) { + if (strcmp((const char*)color_in, "light yellow") == 0) { return LCD_LIGHTYELLOW; } - if (strcmp((const char*)color, "dark blue") == 0) { + if (strcmp((const char*)color_in, "dark blue") == 0) { return LCD_DARKBLUE; } - if (strcmp((const char*)color, "dark green") == 0) { + if (strcmp((const char*)color_in, "dark green") == 0) { return LCD_DARKGREEN; } - if (strcmp((const char*)color, "dark red") == 0) { + if (strcmp((const char*)color_in, "dark red") == 0) { return LCD_DARKRED; } - if (strcmp((const char*)color, "dark cyan") == 0) { + if (strcmp((const char*)color_in, "dark cyan") == 0) { return LCD_DARKCYAN; } - if (strcmp((const char*)color, "dark magenta") == 0) { + if (strcmp((const char*)color_in, "dark magenta") == 0) { return LCD_DARKMAGENTA; } - if (strcmp((const char*)color, "dark yellow") == 0) { + if (strcmp((const char*)color_in, "dark yellow") == 0) { return LCD_DARKYELLOW; } - if (strcmp((const char*)color, "white") == 0) { + if (strcmp((const char*)color_in, "white") == 0) { return LCD_WHITE; } - if (strcmp((const char*)color, "light gray") == 0) { + if (strcmp((const char*)color_in, "light gray") == 0) { return LCD_LIGHTGRAY; } - if (strcmp((const char*)color, "gray") == 0) { + if (strcmp((const char*)color_in, "gray") == 0) { return LCD_GRAY; } - if (strcmp((const char*)color, "dark gray") == 0) { + if (strcmp((const char*)color_in, "dark gray") == 0) { return LCD_DARKGRAY; } - if (strcmp((const char*)color, "black") == 0) { + if (strcmp((const char*)color_in, "black") == 0) { return LCD_BLACK; } - if (strcmp((const char*)color, "brown") == 0) { + if (strcmp((const char*)color_in, "brown") == 0) { return LCD_BROWN; } - if (strcmp((const char*)color, "orange") == 0) { + if (strcmp((const char*)color_in, "orange") == 0) { return LCD_ORANGE; } - if (strcmp((const char*)color, "transparent") == 0) { + if (strcmp((const char*)color_in, "transparent") == 0) { return LCD_TRANSPARENT; } From da5b02d5da57aced42bdbba5fe773ac04210d78b Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 09:54:58 +0100 Subject: [PATCH 25/41] Created enum for inpub_id changed strcat to strncat --- project/Core/Src/mug.c | 56 +++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 26ab3b6..0d7f377 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -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); From 70b980dcfa37d50bfa72d4f27663094eab62342e Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 10:22:31 +0100 Subject: [PATCH 26/41] Turned server IP and Port into defines --- project/Core/Src/mug.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/project/Core/Src/mug.c b/project/Core/Src/mug.c index 0d7f377..b6d20c9 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mug.c @@ -23,6 +23,11 @@ #define PRINT_XPOS 50 #define PRINT_YPOS 50 #define MAX_FILES 20 +#define SERVER_IP4_A 192 +#define SERVER_IP4_B 168 +#define SERVER_IP4_C 69 +#define SERVER_IP4_D 11 +#define SERVER_PORT 1883 typedef enum input_topic { set_text, @@ -267,8 +272,8 @@ static void mosquitto_connect(mqtt_client_t* client) { ci.client_id = "STM32"; ip_addr_t server_ip; - IP4_ADDR(&server_ip, 192, 168, 69, 11); - uint16_t server_port = 1883; + IP4_ADDR(&server_ip, SERVER_IP4_A, SERVER_IP4_B, SERVER_IP4_C, SERVER_IP4_D); + uint16_t server_port = SERVER_PORT; err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { LOG_DEBUG(TAG, "mqtt_connect return %d", err); From d88d35c6cf6a8591df4583c744b1b1cc65142846 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 12:39:57 +0100 Subject: [PATCH 27/41] Renamed files from mug to mqtt_application --- project/Core/Inc/{mug.h => mqtt_application.h} | 8 ++++---- project/Core/Src/main.c | 2 +- project/Core/Src/{mug.c => mqtt_application.c} | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) rename project/Core/Inc/{mug.h => mqtt_application.h} (62%) rename project/Core/Src/{mug.c => mqtt_application.c} (99%) diff --git a/project/Core/Inc/mug.h b/project/Core/Inc/mqtt_application.h similarity index 62% rename from project/Core/Inc/mug.h rename to project/Core/Inc/mqtt_application.h index 1e3776c..9e8cca6 100644 --- a/project/Core/Inc/mug.h +++ b/project/Core/Inc/mqtt_application.h @@ -1,15 +1,15 @@ /** - * @file mug.h + * @file mqtt_application.h * header for mosquitto application of the groups assignment * @author RobinVdB */ -#ifndef INC_MUG_H_ -#define INC_MUG_H_ +#ifndef INC_MQTTA_H_ +#define INC_MQTTA_H_ /** * @brief Initialize MQTT application */ void mug_init(void); -#endif /* INC_MUG_H_ */ +#endif /* INC_MQTTA_H_ */ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 9adc667..5b47a18 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -28,7 +28,7 @@ #include "log.h" #include "llfs.h" #include "lcd_api.h" -#include "mug.h" +#include #include "tftp.h" /* USER CODE END Includes */ diff --git a/project/Core/Src/mug.c b/project/Core/Src/mqtt_application.c similarity index 99% rename from project/Core/Src/mug.c rename to project/Core/Src/mqtt_application.c index b6d20c9..5d283ec 100644 --- a/project/Core/Src/mug.c +++ b/project/Core/Src/mqtt_application.c @@ -1,9 +1,10 @@ /** - * @file mug.c + * @file mqtt_application.c * mosquitto application for group assignment * @author RobinVdB */ +#include #include #include "httpd.h" #include "lcd_api.h" @@ -11,7 +12,6 @@ #include "lwip/ip_addr.h" #include "mdns.h" #include "mqtt.h" -#include "mug.h" #define LOGGER_LEVEL_INFO #include "log.h" @@ -55,7 +55,7 @@ static uint16_t connection_attempt_counter; static uint32_t color; static uint32_t bgcolor; static input_topic_t inpub_id; -static const char* TAG = "mug"; +static const char* TAG = "MQTT"; /** * @brief callback function for publishing data From b50900e713b7b8ae43420b27de68b70910260204 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 12:43:38 +0100 Subject: [PATCH 28/41] Changed function name mug_init to mqtt_application_init --- project/Core/Inc/mqtt_application.h | 2 +- project/Core/Src/main.c | 2 +- project/Core/Src/mqtt_application.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 9e8cca6..087e8de 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -10,6 +10,6 @@ /** * @brief Initialize MQTT application */ -void mug_init(void); +void mqtt_application_init(void); #endif /* INC_MQTTA_H_ */ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 5b47a18..d3a955a 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -135,7 +135,7 @@ int main(void) tftp_server_init(); /* Initialize the MQTT application */ - mug_init(); + mqtt_application_init(); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 5d283ec..bb45846 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -287,7 +287,7 @@ static void mosquitto_connect(mqtt_client_t* client) { * @brief Init function for the mosquitto application of the assignment * Gives the global variables a value and calls the mosquitto_connect function */ -void mug_init(void) { +void mqtt_application_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; font = LCD_FONT16; From 1550ccd0ba977b9d2d0c5299d9faf8aa12006a1a Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 13:33:47 +0100 Subject: [PATCH 29/41] Made some requested changes from reviews --- project/Core/Inc/mqtt_application.h | 2 +- project/Core/Src/main.c | 2 +- project/Core/Src/mqtt_application.c | 173 ++++++++++++++-------------- 3 files changed, 91 insertions(+), 86 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 087e8de..117a893 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -10,6 +10,6 @@ /** * @brief Initialize MQTT application */ -void mqtt_application_init(void); +uint8_t mqtt_application_init(void); #endif /* INC_MQTTA_H_ */ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index d3a955a..336157c 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -28,7 +28,7 @@ #include "log.h" #include "llfs.h" #include "lcd_api.h" -#include +#include "mqtt_application.h" #include "tftp.h" /* USER CODE END Includes */ diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index bb45846..5437c9b 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -4,7 +4,6 @@ * @author RobinVdB */ -#include #include #include "httpd.h" #include "lcd_api.h" @@ -12,6 +11,7 @@ #include "lwip/ip_addr.h" #include "mdns.h" #include "mqtt.h" +#include "mqtt_application.h" #define LOGGER_LEVEL_INFO #include "log.h" @@ -64,9 +64,7 @@ static const char* TAG = "MQTT"; * @param[in] result Whether the publish was successful or not */ static void mqtt_pub_request_cb(void* arg, err_t result) { - if (result != ERR_OK) { - LOG_DEBUG(TAG, "Publish result: %d", result); - } + LOG_DEBUG(TAG, "Publish result: %d", result); } /** @@ -79,15 +77,14 @@ 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 = MAX_FILES; size_t num_files; - llfs_file_t file_list[max_files]; + llfs_file_t file_list[MAX_FILES]; u8_t qos = PUBLISH_QOS; u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); - num_files = llfs_file_list(file_list, max_files, "*.bmp"); + num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); if (num_files == 0) { strncpy(pub_payload, "No images found", sizeof(pub_payload)); @@ -108,12 +105,12 @@ static void publish_data(mqtt_client_t* client, void* arg) { pub_payload[0] = '\0'; - num_files = llfs_file_list(file_list, max_files, "*.gif"); + num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); if (num_files == 0) { - strcpy(pub_payload, "No gifs found"); + strncpy(pub_payload, "No gifs found", sizeof(pub_payload)); } else { - strcpy(pub_payload, "Available gifs: "); + strncpy(pub_payload, "Available gifs: ", sizeof(pub_payload)); for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string strncat(pub_payload, file_list[i].name, sizeof(pub_payload) - strlen(pub_payload) - 1); @@ -140,16 +137,22 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ // Check for which topic a publish was received if (strcmp(topic, "input/setText") == 0) { inpub_id = set_text; - } else if (strcmp(topic, "input/setImage") == 0) { - inpub_id = set_image; - } else if (strcmp(topic, "input/setTextColor") == 0) { - inpub_id = set_text_color; - } else if (strcmp(topic, "input/setColor") == 0) { - inpub_id = set_color; - } else { - // In case of wrong topic - inpub_id = other_topic; + return; } + if (strcmp(topic, "input/setImage") == 0) { + inpub_id = set_image; + return; + } + if (strcmp(topic, "input/setTextColor") == 0) { + inpub_id = set_text_color; + return; + } + if (strcmp(topic, "input/setColor") == 0) { + inpub_id = set_color; + return; + } + // In case of wrong topic + inpub_id = other_topic; } /** @@ -165,46 +168,47 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, char data_buffer[len + 1]; lcd_gif_t* gif; - LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); - if (flags & MQTT_DATA_FLAG_LAST) { - memcpy(data_buffer, data, len); - data_buffer[len] = '\0'; - switch (inpub_id) { - 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 set_image: - // Places an image on the lcd - LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); - lcd_clear_images(); - lcd_set_bg_color_layer0(bgcolor); - if (data_buffer[len - 3] == 'b') { - lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + LOG_INFO(TAG, "Incoming publish payload with length %d, flags %d", len, flags); + if (!(flags & MQTT_DATA_FLAG_LAST)) { + LOG_WARN(TAG, "incoming data too big to fit in buffer."); + return; + } + memcpy(data_buffer, data, len); + data_buffer[len] = '\0'; + switch (inpub_id) { + 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 set_image: + // Places an image on the lcd + LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); + lcd_clear_images(); + lcd_set_bg_color_layer0(bgcolor); + if (data_buffer[len - 3] == 'b') { + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + } + if (data_buffer[len - 3] == 'g') { + gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + if (gif == NULL) { + LOG_INFO(TAG, "GIF could not be drawn"); } - if (data_buffer[len - 3] == 'g') { - gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); - if (gif == NULL) { - LOG_INFO(TAG, "GIF could not be drawn"); - } - } - break; - 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 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); - break; - default: - LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); - } - } else { + } + break; + 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 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); + break; + default: + LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored."); } } @@ -230,22 +234,7 @@ static void mqtt_sub_request_cb(void* arg, err_t result) { static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) { err_t err; - if (status == MQTT_CONNECT_ACCEPTED) { - LOG_INFO(TAG, "Successfully connected"); - - connection_attempt_counter = 0; - // Set up callback function for input - mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); - - // Subscribe to the topics setText, setImage, setColor and setTextcolor - err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); - if (err != ERR_OK) { - LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); - } - - // Publish list of images here - publish_data(client, NULL); - } else { + if (status != MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); while (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { @@ -253,7 +242,22 @@ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection // Try to reconnect mosquitto_connect(client); } + return; } + LOG_INFO(TAG, "Successfully connected"); + + connection_attempt_counter = 0; + // Set up callback function for input + mqtt_set_inpub_callback(client, mqtt_incoming_publish_cb, mqtt_incoming_data_cb, arg); + + // Subscribe to the topics setText, setImage, setColor and setTextcolor + err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg); + if (err != ERR_OK) { + LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); + } + + // Publish list of images here + publish_data(client, NULL); } /** @@ -277,17 +281,16 @@ static void mosquitto_connect(mqtt_client_t* client) { err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); if (err != ERR_OK) { LOG_DEBUG(TAG, "mqtt_connect return %d", err); + return; } - if (err == ERR_OK) { - LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); - } + LOG_DEBUG(TAG, "Went into mqtt_client_connect; mqtt_connect return %d", err); } /** * @brief Init function for the mosquitto application of the assignment * Gives the global variables a value and calls the mosquitto_connect function */ -void mqtt_application_init(void) { +uint8_t mqtt_application_init(void) { color = LCD_BLACK; bgcolor = LCD_WHITE; font = LCD_FONT16; @@ -296,10 +299,14 @@ void mqtt_application_init(void) { connection_attempt_counter = 0; mqtt_client_t* client = mqtt_client_new(); - if (client != NULL) { - LOG_DEBUG(TAG, "Starting connection test"); - mosquitto_connect(client); + if (client == NULL) { + LOG_CRIT(TAG, "%s: client == NULL", __func__); + return 1; } + LOG_DEBUG(TAG, "Starting connection test"); + mosquitto_connect(client); + + return 0; } /** @@ -309,8 +316,6 @@ void mqtt_application_init(void) { * @return color Define to use with the LCD_API */ uint32_t color_picker(char* color_in) { - uint32_t output = LCD_BLACK; - if (strcmp((const char*)color_in, "blue") == 0) { return LCD_BLUE; } @@ -390,5 +395,5 @@ uint32_t color_picker(char* color_in) { return LCD_TRANSPARENT; } - return output; + return LCD_BLACK; } From 2d86900a4ac2d1610cfeeb0e7a18d3a3a9e9b18f Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 14:34:27 +0100 Subject: [PATCH 30/41] Created new function to fill payload string for publish --- project/Core/Src/mqtt_application.c | 44 ++++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 5437c9b..4580b1e 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -46,6 +46,7 @@ static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); +static void create_publish_string(char*, char*, llfs_file_t*, size_t, size_t); // Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static sFONT* font; @@ -87,17 +88,11 @@ static void publish_data(mqtt_client_t* client, void* arg) { num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); if (num_files == 0) { - strncpy(pub_payload, "No images found", sizeof(pub_payload)); + strncat(pub_payload, "No images found", sizeof(pub_payload) - strlen(pub_payload) - 1); + LOG_INFO(TAG, "%s: No images found", __func__); } else { - 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 - 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 - } - strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1); + create_publish_string("image", pub_payload, file_list, sizeof(pub_payload), num_files); } - 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); @@ -108,15 +103,10 @@ static void publish_data(mqtt_client_t* client, void* arg) { num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); if (num_files == 0) { - strncpy(pub_payload, "No gifs found", sizeof(pub_payload)); + strncat(pub_payload, "No gifs found", sizeof(pub_payload) - strlen(pub_payload) - 1); + LOG_INFO(TAG, "%s: No gifs found", __func__); } else { - strncpy(pub_payload, "Available gifs: ", sizeof(pub_payload)); - for (size_t i = 0; i < num_files; i++) { - // Concatenate file names into the payload string - 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 - } - strncat(pub_payload, "\0", sizeof(pub_payload) - strlen(pub_payload) - 1); + create_publish_string("gif", pub_payload, file_list, sizeof(pub_payload), num_files); } err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -397,3 +387,23 @@ uint32_t color_picker(char* color_in) { return LCD_BLACK; } + +static void create_publish_string(char* file_type, char* payload_buffer, llfs_file_t* file_list, size_t buffer_size, size_t files_amount) { + if (strcmp(file_type, "image") == 0) { + strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1); + } else if (strcmp(file_type, "gif") == 0) { + strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1); + } else { + LOG_WARN(TAG, "No application for given file type: %s", file_type); + return; + } + for (size_t i = 0; i < files_amount; i++) { + // Concatenate file names into the payload string + strncat(payload_buffer, file_list[i].name, buffer_size - strlen(payload_buffer) - 1); + strncat(payload_buffer, ", ", buffer_size - strlen(payload_buffer) - 1); // Add a comma between file names + } + strncat(payload_buffer, "\0", buffer_size - strlen(payload_buffer) - 1); + LOG_DEBUG(TAG, "String: %s", payload_buffer); + + return; +} From 907970ee5919e4cb2f1adaf90268508d2b313169 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 14:49:17 +0100 Subject: [PATCH 31/41] Added tolower() to color_picker() --- project/Core/Src/mqtt_application.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 4580b1e..2f0e546 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -306,6 +306,9 @@ uint8_t mqtt_application_init(void) { * @return color Define to use with the LCD_API */ uint32_t color_picker(char* color_in) { + for (int i = 0; i < strlen(color_in); i++) { + color_in[i] = tolower(color_in[i]); + } if (strcmp((const char*)color_in, "blue") == 0) { return LCD_BLUE; } From b302ab2e11af28281c364eeca1f7e2d3c14635fd Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:02:01 +0100 Subject: [PATCH 32/41] Updated doxygen --- project/Core/Inc/mqtt_application.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 117a893..59bbcbe 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -9,6 +9,8 @@ /** * @brief Initialize MQTT application + * + * @output returns 1 if the init failed to create a client and start an MQTT connection */ uint8_t mqtt_application_init(void); From b844d25f9552591ad6d6e5077ea79f1ff312331b Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 26 Nov 2023 15:12:00 +0100 Subject: [PATCH 33/41] TFTP Forgot to deinit tftp on failiure of init --- project/Core/Src/tftp.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index e1d8f53..87c2d1b 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -299,6 +299,7 @@ void tftp_server_init(void) { // Init the tftp server if (tftp_init(&tftpContext_s) != ERR_OK) { LOG_FATAL(TAG, "Could not initialize tftp server"); + tftp_server_deinit(); return; } LOG_INFO(TAG, "tftp server initialized successfully"); From be0339b2cf573d0d3f10452df649eecb2de670d2 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:15:56 +0100 Subject: [PATCH 34/41] Updated create_publish_string --- project/Core/Src/mqtt_application.c | 55 +++++++++++++++++------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 2f0e546..203830a 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -46,7 +46,7 @@ static void mqtt_sub_request_cb(void*, err_t); static void mqtt_connection_cb(mqtt_client_t*, void*, mqtt_connection_status_t); static void mosquitto_connect(mqtt_client_t*); static uint32_t color_picker(char*); -static void create_publish_string(char*, char*, llfs_file_t*, size_t, size_t); +static void create_publish_string(char*, char*, size_t); // Global variables used in mqtt_incoming_publish_cb and mqtt_incoming_data_cb to give an easy to use ID to the subscribed topics static sFONT* font; @@ -78,36 +78,21 @@ 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 num_files; - llfs_file_t file_list[MAX_FILES]; u8_t qos = PUBLISH_QOS; u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); - num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); + create_publish_string("image", pub_payload,sizeof(pub_payload)); - if (num_files == 0) { - strncat(pub_payload, "No images found", sizeof(pub_payload) - strlen(pub_payload) - 1); - LOG_INFO(TAG, "%s: No images found", __func__); - } else { - create_publish_string("image", pub_payload, file_list, sizeof(pub_payload), num_files); - } 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); - } + if (err != ERR_OK) { + LOG_DEBUG(TAG, "Publish err: %d", err); + } pub_payload[0] = '\0'; + create_publish_string("gif", pub_payload, sizeof(pub_payload)); - num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); - - if (num_files == 0) { - strncat(pub_payload, "No gifs found", sizeof(pub_payload) - strlen(pub_payload) - 1); - LOG_INFO(TAG, "%s: No gifs found", __func__); - } else { - create_publish_string("gif", pub_payload, file_list, sizeof(pub_payload), num_files); - } 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); @@ -391,16 +376,40 @@ uint32_t color_picker(char* color_in) { return LCD_BLACK; } -static void create_publish_string(char* file_type, char* payload_buffer, llfs_file_t* file_list, size_t buffer_size, size_t files_amount) { +/** + * @brief creates a string to publish on the getImageList topic + * + * @param[in] file_type The type of file file_list is made out of + * @param[in] payload_buffer The string to be published + * @param[in] buffer_size Size of payload_buffer + */ +static void create_publish_string(char* file_type, char* payload_buffer, size_t buffer_size) { + size_t num_files; + llfs_file_t file_list[MAX_FILES]; + if (strcmp(file_type, "image") == 0) { + num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); + + if (num_files == 0) { + strncat(payload_buffer, "No images found", buffer_size - strlen(payload_buffer) - 1); + LOG_INFO(TAG, "%s: No images found", __func__); + return; + } strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1); } else if (strcmp(file_type, "gif") == 0) { + num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); + + if (num_files == 0) { + strncat(payload_buffer, "No gifs found", buffer_size - strlen(payload_buffer) - 1); + LOG_INFO(TAG, "%s: No gifs found", __func__); + return; + } strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1); } else { LOG_WARN(TAG, "No application for given file type: %s", file_type); return; } - for (size_t i = 0; i < files_amount; i++) { + for (size_t i = 0; i < num_files; i++) { // Concatenate file names into the payload string strncat(payload_buffer, file_list[i].name, buffer_size - strlen(payload_buffer) - 1); strncat(payload_buffer, ", ", buffer_size - strlen(payload_buffer) - 1); // Add a comma between file names From 8263855ce7430f18f793c709c98db8a8b7a49c6e Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:39:16 +0100 Subject: [PATCH 35/41] Updated create_publish_string as requested --- project/Core/Src/mqtt_application.c | 31 ++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 203830a..f648725 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -83,7 +83,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { LOG_DEBUG(TAG, "Entering publish"); - create_publish_string("image", pub_payload,sizeof(pub_payload)); + create_publish_string("*.bmp", pub_payload,sizeof(pub_payload)); err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -91,7 +91,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { } pub_payload[0] = '\0'; - create_publish_string("gif", pub_payload, sizeof(pub_payload)); + create_publish_string("*.gif", pub_payload, sizeof(pub_payload)); err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); if (err != ERR_OK) { @@ -379,7 +379,7 @@ uint32_t color_picker(char* color_in) { /** * @brief creates a string to publish on the getImageList topic * - * @param[in] file_type The type of file file_list is made out of + * @param[in] file_type The file extension asked to be published * @param[in] payload_buffer The string to be published * @param[in] buffer_size Size of payload_buffer */ @@ -387,23 +387,18 @@ static void create_publish_string(char* file_type, char* payload_buffer, size_t size_t num_files; llfs_file_t file_list[MAX_FILES]; - if (strcmp(file_type, "image") == 0) { - num_files = llfs_file_list(file_list, MAX_FILES, "*.bmp"); + num_files = llfs_file_list(file_list, MAX_FILES, file_type); - if (num_files == 0) { - strncat(payload_buffer, "No images found", buffer_size - strlen(payload_buffer) - 1); - LOG_INFO(TAG, "%s: No images found", __func__); - return; - } + if (num_files == 0) { + strncat(payload_buffer, "No files found of type: ", buffer_size - strlen(payload_buffer) - 1); + strncat(payload_buffer, file_type, buffer_size - strlen(payload_buffer) - 1); + LOG_INFO(TAG, "%s: No files found of type %s", __func__, file_type); + return; + } + + if (strcmp(file_type, "*.bmp") == 0) { strncat(payload_buffer, "Available images: ", buffer_size - strlen(payload_buffer) - 1); - } else if (strcmp(file_type, "gif") == 0) { - num_files = llfs_file_list(file_list, MAX_FILES, "*.gif"); - - if (num_files == 0) { - strncat(payload_buffer, "No gifs found", buffer_size - strlen(payload_buffer) - 1); - LOG_INFO(TAG, "%s: No gifs found", __func__); - return; - } + } else if (strcmp(file_type, "*.gif") == 0) { strncat(payload_buffer, "Available gifs: ", buffer_size - strlen(payload_buffer) - 1); } else { LOG_WARN(TAG, "No application for given file type: %s", file_type); From fdc5eb865e0bffedc0268b531863e09d0093d540 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:45:36 +0100 Subject: [PATCH 36/41] Updated publish_data to use defines directly --- project/Core/Src/mqtt_application.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index f648725..96de1dc 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -78,14 +78,12 @@ 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; - u8_t qos = PUBLISH_QOS; - u8_t retain = PUBLISH_RETAIN; LOG_DEBUG(TAG, "Entering publish"); create_publish_string("*.bmp", pub_payload,sizeof(pub_payload)); - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); } @@ -93,7 +91,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { pub_payload[0] = '\0'; create_publish_string("*.gif", pub_payload, sizeof(pub_payload)); - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), qos, retain, mqtt_pub_request_cb, arg); + err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); } From def43a37a76a73d8677a7d97d8a6019b464e7ce2 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 15:48:47 +0100 Subject: [PATCH 37/41] Changed topic to public gifs so both messages are retained --- project/Core/Src/mqtt_application.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 96de1dc..b92219e 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -91,7 +91,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { pub_payload[0] = '\0'; create_publish_string("*.gif", pub_payload, sizeof(pub_payload)); - err = mqtt_publish(client, "getImageList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); + err = mqtt_publish(client, "getGifList", pub_payload, strlen(pub_payload), PUBLISH_QOS, PUBLISH_RETAIN, mqtt_pub_request_cb, arg); if (err != ERR_OK) { LOG_DEBUG(TAG, "Publish err: %d", err); } From 1e7638c63d7a1ba5e07b683c85c9266d9e3e1929 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 17:05:05 +0100 Subject: [PATCH 38/41] Solved reviews --- project/Core/Inc/mqtt_application.h | 2 ++ project/Core/Src/mqtt_application.c | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index 59bbcbe..ae9452d 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -7,6 +7,8 @@ #ifndef INC_MQTTA_H_ #define INC_MQTTA_H_ +#include + /** * @brief Initialize MQTT application * diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index b92219e..6692046 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -130,7 +130,7 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ /** * @brief Handles incoming publish data - * Handles the recieved data from a publish to a subscribed topic + * Handles the received data from a publish to a subscribed topic * * @param[in] arg * @param[in] data The incoming data @@ -160,13 +160,15 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); lcd_set_bg_color_layer0(bgcolor); - if (data_buffer[len - 3] == 'b') { - lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); - } - if (data_buffer[len - 3] == 'g') { - gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); - if (gif == NULL) { - LOG_INFO(TAG, "GIF could not be drawn"); + if (strlen(data_buffer) >= 3) { + if (data_buffer[len - 3] == 'b') { + lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); + } + if (data_buffer[len - 3] == 'g') { + gif = lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos); + if (gif == NULL) { + LOG_INFO(TAG, "GIF could not be drawn"); + } } } break; @@ -210,7 +212,7 @@ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection if (status != MQTT_CONNECT_ACCEPTED) { LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); - while (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { + if (connection_attempt_counter < ATTEMPT_RECONNECT_AMOUNT) { connection_attempt_counter++; // Try to reconnect mosquitto_connect(client); @@ -409,6 +411,4 @@ static void create_publish_string(char* file_type, char* payload_buffer, size_t } strncat(payload_buffer, "\0", buffer_size - strlen(payload_buffer) - 1); LOG_DEBUG(TAG, "String: %s", payload_buffer); - - return; } From 5600aa20f35df36ad8f26cef552e38d94c05ace3 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 17:13:15 +0100 Subject: [PATCH 39/41] Updated doxygen --- project/Core/Src/mqtt_application.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 6692046..4ecaea8 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -61,7 +61,7 @@ static const char* TAG = "MQTT"; /** * @brief callback function for publishing data * - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] result Whether the publish was successful or not */ static void mqtt_pub_request_cb(void* arg, err_t result) { @@ -73,7 +73,7 @@ static void mqtt_pub_request_cb(void* arg, err_t result) { * Publishes the names of all the .bmp and .gif files on the topic getImageList * * @param[in] client Pointer to the MQTT client - * @param[in] arg + * @param[in] arg Additional argument to pass to the callback function */ static void publish_data(mqtt_client_t* client, void* arg) { char pub_payload[200] = {0}; @@ -101,7 +101,7 @@ static void publish_data(mqtt_client_t* client, void* arg) { * @brief Handles incoming publish * Callback function for when data was published to a subscribed topic * - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] topic The topic on which an incoming publish was received * @param[in] tot_len Length of the incoming data */ @@ -132,7 +132,7 @@ static void mqtt_incoming_publish_cb(void* arg, const char* topic, uint32_t tot_ * @brief Handles incoming publish data * Handles the received data from a publish to a subscribed topic * - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] data The incoming data * @param[in] len Length of the data * @param[in] flags Whether this is the last fragment of the incoming data @@ -190,8 +190,8 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, /** * @brief Callback function for outgoing subscribe request * - * @param[in] arg - * @param[in] result + * @param[in] arg User supplied argument to connection callback + * @param[in] result Result code for the subscribe request */ static void mqtt_sub_request_cb(void* arg, err_t result) { LOG_DEBUG(TAG, "Subscribe result: %d", result); @@ -203,7 +203,7 @@ static void mqtt_sub_request_cb(void* arg, err_t result) { * subscribes to the input topics and calls the publish_data function. * * @param[in] client Pointer to the MQTT client - * @param[in] arg + * @param[in] arg User supplied argument to connection callback * @param[in] status Connect result code or disconnection notification */ static void mqtt_connection_cb(mqtt_client_t* client, void* arg, mqtt_connection_status_t status) { From 752ada6867ac4c08b8bcf7ed3b52171b81f6eaf5 Mon Sep 17 00:00:00 2001 From: RobinVdB8 Date: Sun, 26 Nov 2023 17:15:22 +0100 Subject: [PATCH 40/41] Changed strlen(data_buffer) to len --- project/Core/Src/mqtt_application.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 4ecaea8..21c0a3e 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -160,7 +160,7 @@ static void mqtt_incoming_data_cb(void* arg, const uint8_t* data, uint16_t len, LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer); lcd_clear_images(); lcd_set_bg_color_layer0(bgcolor); - if (strlen(data_buffer) >= 3) { + if (len >= 3) { if (data_buffer[len - 3] == 'b') { lcd_draw_img_from_fs((const char*)data_buffer, xpos, ypos); } From 5917bcf329f917cf6b8b76e1f269430bca1cfd5d Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sun, 26 Nov 2023 22:22:03 +0100 Subject: [PATCH 41/41] MQTT Add brief tag --- project/Core/Inc/mqtt_application.h | 2 +- project/Core/Src/mqtt_application.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project/Core/Inc/mqtt_application.h b/project/Core/Inc/mqtt_application.h index ae9452d..389f3b4 100644 --- a/project/Core/Inc/mqtt_application.h +++ b/project/Core/Inc/mqtt_application.h @@ -1,6 +1,6 @@ /** * @file mqtt_application.h - * header for mosquitto application of the groups assignment + * @brief header for mosquitto application of the groups assignment * @author RobinVdB */ diff --git a/project/Core/Src/mqtt_application.c b/project/Core/Src/mqtt_application.c index 21c0a3e..53f72e4 100644 --- a/project/Core/Src/mqtt_application.c +++ b/project/Core/Src/mqtt_application.c @@ -1,6 +1,6 @@ /** * @file mqtt_application.c - * mosquitto application for group assignment + * @brief mosquitto application for group assignment * @author RobinVdB */