328 lines
10 KiB
C
328 lines
10 KiB
C
/**
|
|
* @file mug.c
|
|
* @mosquitto application for group assignment
|
|
* @author RobinVdB
|
|
*/
|
|
|
|
#include "mdns.h"
|
|
#include "httpd.h"
|
|
#include "lwip/apps/fs.h"
|
|
#include <string.h>
|
|
#include "lwip/ip_addr.h"
|
|
#include "mqtt.h"
|
|
#include "mug.h"
|
|
#include "lcd_api.h"
|
|
|
|
#define LOGGER_LEVEL_INFO
|
|
#include "log.h"
|
|
|
|
//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);
|
|
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 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
|
|
static sFONT* font;
|
|
static uint16_t xpos;
|
|
static uint16_t ypos;
|
|
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) {
|
|
LOG_DEBUG(TAG, "Publish result: %d", result);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Publishes data
|
|
* 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] = {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 ;
|
|
|
|
LOG_DEBUG(TAG, "Entering publish");
|
|
|
|
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 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) {
|
|
LOG_DEBUG(TAG, "Publish err: %d", err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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, 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) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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 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) {
|
|
memcpy(data_buffer, data, len);
|
|
data_buffer[len] = '\0';
|
|
switch (inpub_id) {
|
|
case 0:
|
|
//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
|
|
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') {
|
|
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
|
|
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
|
|
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 {
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Callback function for outgoing subscribe request
|
|
*/
|
|
static void mqtt_sub_request_cb(void *arg, err_t result) {
|
|
LOG_DEBUG(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;
|
|
|
|
if (status == MQTT_CONNECT_ACCEPTED) {
|
|
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);
|
|
|
|
//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 {
|
|
LOG_INFO(TAG, "mqtt_connection_cb: Disconnected, reason: %d", status);
|
|
|
|
//try to reconnect
|
|
mosquitto_connect(client);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
|
|
LOG_INFO(TAG, "Attempting MQTT Connection");
|
|
|
|
memset(&ci, 0, sizeof(ci));
|
|
|
|
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_DEBUG(TAG, "mqtt_connect return %d", err);
|
|
}
|
|
if (err == ERR_OK) {
|
|
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 mug_init(void) {
|
|
color = LCD_BLACK;
|
|
bgcolor = LCD_WHITE;
|
|
font = LCD_FONT16;
|
|
xpos = 50;
|
|
ypos = 50;
|
|
|
|
mqtt_client_t *client = mqtt_client_new();
|
|
if (client != NULL) {
|
|
LOG_DEBUG(TAG, "Starting connection test");
|
|
mosquitto_connect(client);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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) {
|
|
return LCD_BLUE;
|
|
}
|
|
if (strcmp((const char*)color, "green") == 0) {
|
|
return LCD_GREEN;
|
|
}
|
|
if (strcmp((const char*)color, "red") == 0) {
|
|
return LCD_RED;
|
|
}
|
|
if (strcmp((const char*)color, "cyan") == 0) {
|
|
return LCD_CYAN;
|
|
}
|
|
if (strcmp((const char*)color, "magenta") == 0) {
|
|
return LCD_MAGENTA;
|
|
}
|
|
if (strcmp((const char*)color, "yellow") == 0) {
|
|
return LCD_YELLOW;
|
|
}
|
|
if (strcmp((const char*)color, "light blue") == 0) {
|
|
return LCD_LIGHTBLUE;
|
|
}
|
|
if (strcmp((const char*)color, "light green") == 0) {
|
|
return LCD_LIGHTGREEN;
|
|
}
|
|
if (strcmp((const char*)color, "light red") == 0) {
|
|
return LCD_LIGHTRED;
|
|
}
|
|
if (strcmp((const char*)color, "light cyan") == 0) {
|
|
return LCD_LIGHTCYAN;
|
|
}
|
|
if (strcmp((const char*)color, "light magenta") == 0) {
|
|
return LCD_LIGHTMAGENTA;
|
|
}
|
|
if (strcmp((const char*)color, "light yellow") == 0) {
|
|
return LCD_LIGHTYELLOW;
|
|
}
|
|
if (strcmp((const char*)color, "dark blue") == 0) {
|
|
return LCD_DARKBLUE;
|
|
}
|
|
if (strcmp((const char*)color, "dark green") == 0) {
|
|
return LCD_DARKGREEN;
|
|
}
|
|
if (strcmp((const char*)color, "dark red") == 0) {
|
|
return LCD_DARKRED;
|
|
}
|
|
if (strcmp((const char*)color, "dark cyan") == 0) {
|
|
return LCD_DARKCYAN;
|
|
}
|
|
if (strcmp((const char*)color, "dark magenta") == 0) {
|
|
return LCD_DARKMAGENTA;
|
|
}
|
|
if (strcmp((const char*)color, "dark yellow") == 0) {
|
|
return LCD_DARKYELLOW;
|
|
}
|
|
if (strcmp((const char*)color, "white") == 0) {
|
|
return LCD_WHITE;
|
|
}
|
|
if (strcmp((const char*)color, "light gray") == 0) {
|
|
return LCD_LIGHTGRAY;
|
|
}
|
|
if (strcmp((const char*)color, "gray") == 0) {
|
|
return LCD_GRAY;
|
|
}
|
|
if (strcmp((const char*)color, "dark gray") == 0) {
|
|
return LCD_DARKGRAY;
|
|
}
|
|
if (strcmp((const char*)color, "black") == 0) {
|
|
return LCD_BLACK;
|
|
}
|
|
if (strcmp((const char*)color, "brown") == 0) {
|
|
return LCD_BROWN;
|
|
}
|
|
if (strcmp((const char*)color, "orange") == 0) {
|
|
return LCD_ORANGE;
|
|
}
|
|
if (strcmp((const char*)color, "transparent") == 0) {
|
|
return LCD_TRANSPARENT;
|
|
}
|
|
|
|
return output;
|
|
}
|