not running

Dependencies:   TextLCD MQTT

Committer:
hyan99
Date:
Wed Dec 11 20:20:12 2019 +0000
Revision:
2:16b3bd337db2
Parent:
0:3b4906b8a747
testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hyan99 0:3b4906b8a747 1 #ifndef _COMMUNICATION_H_
hyan99 0:3b4906b8a747 2 #define _COMMUNICATION_H_
hyan99 0:3b4906b8a747 3
hyan99 0:3b4906b8a747 4 #include "mbed.h"
hyan99 2:16b3bd337db2 5 #include "rtos.h"
hyan99 0:3b4906b8a747 6 #include "MQTTNetwork.h"
hyan99 0:3b4906b8a747 7 #include "MQTTClient.h"
hyan99 0:3b4906b8a747 8 #include "MQTTmbed.h"
hyan99 2:16b3bd337db2 9 #include <string.h>
hyan99 2:16b3bd337db2 10 #include <queue>
hyan99 2:16b3bd337db2 11 #include <mutex>
hyan99 0:3b4906b8a747 12
hyan99 0:3b4906b8a747 13
hyan99 0:3b4906b8a747 14 typedef struct {
hyan99 2:16b3bd337db2 15 char id;
hyan99 2:16b3bd337db2 16 char speed; // speed for AccCar, -1 for Road
hyan99 2:16b3bd337db2 17 char position; // position for AccCar, update number for Road
hyan99 0:3b4906b8a747 18 } message_t;
hyan99 0:3b4906b8a747 19
hyan99 0:3b4906b8a747 20 class Communication {
hyan99 0:3b4906b8a747 21 public:
hyan99 0:3b4906b8a747 22 static EventFlags control_flags; // For cars waiting on speed control
hyan99 0:3b4906b8a747 23 static EventFlags sync_flags;
hyan99 0:3b4906b8a747 24 static int speeds[5];
hyan99 0:3b4906b8a747 25 static int sync;
hyan99 0:3b4906b8a747 26 static int flags[5];
hyan99 0:3b4906b8a747 27
hyan99 0:3b4906b8a747 28 // Singleton
hyan99 2:16b3bd337db2 29 static Communication *getInstance(char* t1, char* t2, char* t3, char* t4);
hyan99 0:3b4906b8a747 30
hyan99 0:3b4906b8a747 31 // MQTT Setup
hyan99 0:3b4906b8a747 32 void setup_wifi();
hyan99 2:16b3bd337db2 33 void setup_mqtt(MQTTNetwork* network);
hyan99 0:3b4906b8a747 34 static void message_arrived(MQTT::MessageData& md);
hyan99 0:3b4906b8a747 35 static void control_arrived(MQTT::MessageData& md);
hyan99 0:3b4906b8a747 36 static void sync_arrived(MQTT::MessageData& md);
hyan99 0:3b4906b8a747 37 int send_message(char* topic);
hyan99 0:3b4906b8a747 38 int subscribe_to_topic_control(char* topic);
hyan99 0:3b4906b8a747 39 int subscribe_to_topic_sync(char* topic);
hyan99 2:16b3bd337db2 40 void publish_car(int id, int speed, int position);
hyan99 2:16b3bd337db2 41 void publish_road(int num);
hyan99 0:3b4906b8a747 42 void start(); // function for serving requests (single thread)
hyan99 0:3b4906b8a747 43 void reset(); // function to create and start the singleton thread
hyan99 0:3b4906b8a747 44 void stop();
hyan99 0:3b4906b8a747 45
hyan99 2:16b3bd337db2 46 //private:
hyan99 0:3b4906b8a747 47 static Communication *singleton;
hyan99 0:3b4906b8a747 48
hyan99 0:3b4906b8a747 49 WiFiInterface *wifi;
hyan99 2:16b3bd337db2 50 MQTTNetwork* network;
hyan99 0:3b4906b8a747 51 MQTT::Client<MQTTNetwork, Countdown> *client;
hyan99 2:16b3bd337db2 52 static Thread* thread;
hyan99 2:16b3bd337db2 53
hyan99 2:16b3bd337db2 54 Mutex* mutex;
hyan99 2:16b3bd337db2 55 std::queue<message_t>* q;
hyan99 0:3b4906b8a747 56
hyan99 2:16b3bd337db2 57 char* topic_pub_position;
hyan99 2:16b3bd337db2 58 char* topic_sub_control;
hyan99 2:16b3bd337db2 59 char* topic_pub_receive;
hyan99 2:16b3bd337db2 60 char* topic_sub_send;
hyan99 0:3b4906b8a747 61
hyan99 2:16b3bd337db2 62 Communication(char* t1, char* t2, char* t3, char* t4);
hyan99 0:3b4906b8a747 63
hyan99 0:3b4906b8a747 64 };
hyan99 0:3b4906b8a747 65
hyan99 0:3b4906b8a747 66 #endif