This commit is contained in:
RobinVdB8
2023-11-25 20:36:01 +01:00

View File

@@ -5,18 +5,18 @@
*/
#include <string.h>
#include "mdns.h"
#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 mqtt_incoming_publish_cb(void*, const char*, u32_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,10 +52,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
*
* @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;
@@ -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
*
* @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
* @param[in] color Input string to select a color
*/
uint32_t color_picker(char* color) {
uint32_t output = LCD_BLACK;