Reformat mug.c and mug.h

This commit is contained in:
L-diy
2023-11-25 20:24:57 +01:00
parent 7cbee6afbb
commit ee1753b43e
2 changed files with 44 additions and 45 deletions

View File

@@ -7,7 +7,8 @@
#ifndef INC_MUG_H_ #ifndef INC_MUG_H_
#define INC_MUG_H_ #define INC_MUG_H_
/* @brief Initialise MQTT application /**
* @brief Initialise MQTT application
*/ */
void mug_init(void); void mug_init(void);

View File

@@ -4,21 +4,21 @@
* @author RobinVdB * @author RobinVdB
*/ */
#include "mdns.h"
#include "httpd.h"
#include "lwip/apps/fs.h"
#include <string.h> #include <string.h>
#include "httpd.h"
#include "lcd_api.h"
#include "lwip/apps/fs.h"
#include "lwip/ip_addr.h" #include "lwip/ip_addr.h"
#include "mdns.h"
#include "mqtt.h" #include "mqtt.h"
#include "mug.h" #include "mug.h"
#include "lcd_api.h"
#define LOGGER_LEVEL_INFO #define LOGGER_LEVEL_INFO
#include "log.h" #include "log.h"
//Function prototypes // Function prototypes
static void mqtt_pub_request_cb(void*, err_t); 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_publish_cb(void*, const char*, u32_t);
static void mqtt_incoming_data_cb(void*, const uint8_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_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 void mosquitto_connect(mqtt_client_t*);
static uint32_t color_picker(char*); 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 sFONT* font;
static uint16_t xpos; static uint16_t xpos;
static uint16_t ypos; static uint16_t ypos;
@@ -34,16 +34,16 @@ static uint16_t connection_attempt_counter;
static uint32_t color; static uint32_t color;
static uint32_t bgcolor; static uint32_t bgcolor;
static int inpub_id; static int inpub_id;
static const char *TAG = "mug"; static const char* TAG = "mug";
/** /**
* @brief callback function for publishing data * @brief callback function for publishing data
* *
* @param[in] arg * @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) { static void mqtt_pub_request_cb(void* arg, err_t result) {
if(result != ERR_OK) { if (result != ERR_OK) {
LOG_DEBUG(TAG, "Publish result: %d", result); 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 * @brief Publishes data
* Publishes the names of all the .bmp and .gif files on the topic getImageList * 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 * @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}; char pub_payload[200] = {0};
err_t err; err_t err;
size_t max_files = 20; size_t max_files = 20;
size_t num_files; size_t num_files;
llfs_file_t file_list[max_files]; llfs_file_t file_list[max_files];
u8_t qos = 2; u8_t qos = 2;
u8_t retain = 1 ; u8_t retain = 1;
LOG_DEBUG(TAG, "Entering publish"); 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 * Callback function for when data was published to a subscribed topic
* *
* @param[in] arg * @param[in] arg
* @param[in] topic The topic on which an incomming publish was received * @param[in] topic The topic on which an incoming publish was received
* @param[in] tot_len length of the incoming data * @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); 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) { if (strcmp(topic, "input/setText") == 0) {
inpub_id = 0; inpub_id = 0;
} else if (strcmp(topic, "input/setImage") == 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) { } else if (strcmp(topic, "input/setColor") == 0) {
inpub_id = 3; inpub_id = 3;
} else { } else {
//in case of wrong topic // In case of wrong topic
inpub_id = 4; 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 * Handles the recieved data from a publish to a subscribed topic
* *
* @param[in] arg * @param[in] arg
* @param[in] data the incoming data * @param[in] data The incoming data
* @param[in] len length of the data * @param[in] len Length of the data
* @param[in] flags Whether this is the last fragment of the incoming 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]; char data_buffer[len + 1];
LOG_INFO(TAG, "Incoming publish payload with length %d, flags %u", len, (unsigned int)flags); 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'; data_buffer[len] = '\0';
switch (inpub_id) { switch (inpub_id) {
case 0: case 0:
//places text on the lcd // Places text on the lcd
LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer); LOG_INFO(TAG, "incoming data on input/setText: %s.", data_buffer);
lcd_clear_text(); lcd_clear_text();
lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font); lcd_display_text((const char*)data_buffer, xpos, ypos, color, bgcolor, font);
break; break;
case 1: 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); LOG_INFO(TAG, "incoming data on input/setImage: %s.", data_buffer);
lcd_clear_images(); lcd_clear_images();
lcd_set_bg_color_layer0(bgcolor); 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); 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); lcd_draw_gif_from_fs((const char*)data_buffer, xpos, ypos);
} }
break; break;
case 2: 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); LOG_INFO(TAG, "incoming data on input/setTextColor: %s.", data_buffer);
color = color_picker(data_buffer); color = color_picker(data_buffer);
break; break;
case 3: 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); LOG_INFO(TAG, "incoming data on input/setColor: %s.", data_buffer);
bgcolor = color_picker(data_buffer); bgcolor = color_picker(data_buffer);
break; 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."); LOG_INFO(TAG, "Publish received on wrong topic, incoming data ignored.");
} }
} else { } 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] arg
* @param[in] result * @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); 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. * If a connection was made setup a callback function for incoming publishes.
* subscribes to the input topics and calls the publish_data function. * 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] arg
* @param[in] status Connect result code or disconnection notification * @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; err_t err;
if (status == MQTT_CONNECT_ACCEPTED) { if (status == MQTT_CONNECT_ACCEPTED) {
LOG_INFO(TAG, "Successfully connected"); LOG_INFO(TAG, "Successfully connected");
connection_attempt_counter = 0; 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); 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); err = mqtt_subscribe(client, "input/#", 1, mqtt_sub_request_cb, arg);
if (err != ERR_OK) { if (err != ERR_OK) {
LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err); LOG_DEBUG(TAG, "mqtt_subscribe return: %d", err);
} }
//publish list of images here // Publish list of images here
publish_data(client, NULL); publish_data(client, NULL);
} else { } else {
LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status); LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status);
while (connection_attempt_counter < 50) { while (connection_attempt_counter < 50) {
connection_attempt_counter++; connection_attempt_counter++;
//try to reconnect // Try to reconnect
mosquitto_connect(client); 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 * @brief Attempts to create a connection to the mosquitto broker
* Creates a mqtt client and sets up a connection callback function * 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; struct mqtt_connect_client_info_t ci;
err_t err; err_t err;
@@ -242,7 +240,7 @@ static void mosquitto_connect(mqtt_client_t *client) {
ci.client_id = "STM32"; ci.client_id = "STM32";
ip_addr_t server_ip; 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; uint16_t server_port = 1883;
err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci); err = mqtt_client_connect(client, &server_ip, server_port, mqtt_connection_cb, 0, &ci);
if (err != ERR_OK) { if (err != ERR_OK) {
@@ -265,7 +263,7 @@ void mug_init(void) {
ypos = 50; ypos = 50;
connection_attempt_counter = 0; connection_attempt_counter = 0;
mqtt_client_t *client = mqtt_client_new(); mqtt_client_t* client = mqtt_client_new();
if (client != NULL) { if (client != NULL) {
LOG_DEBUG(TAG, "Starting connection test"); LOG_DEBUG(TAG, "Starting connection test");
mosquitto_connect(client); 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[out] color Define to use with the LCD_API
* @param[in] color input string to select a color * @param[in] color Input string to select a color
*/ */
uint32_t color_picker(char* color) { uint32_t color_picker(char* color) {
uint32_t output = LCD_BLACK; uint32_t output = LCD_BLACK;