
not running
Diff: Communication.h
- Revision:
- 0:3b4906b8a747
- Child:
- 2:16b3bd337db2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Communication.h Tue Dec 10 22:29:09 2019 +0000 @@ -0,0 +1,57 @@ +#ifndef _COMMUNICATION_H_ +#define _COMMUNICATION_H_ + +#include "mbed.h" +#include "MQTTNetwork.h" +#include "MQTTClient.h" +#include "MQTTmbed.h" + + +typedef struct { + int speed; // speed for AccCar, -1 for Road + int position; // position for AccCar, update number for Road + char* topic; +} message_t; + +class Communication { +public: + static EventFlags control_flags; // For cars waiting on speed control + static EventFlags sync_flags; + static int speeds[5]; + static int sync; + static int flags[5]; + + // Singleton + static Communication *getInstance(); + + // MQTT Setup + void setup_wifi(); + void setup_mqtt(MQTTNetwork &network); + static void message_arrived(MQTT::MessageData& md); + static void control_arrived(MQTT::MessageData& md); + static void sync_arrived(MQTT::MessageData& md); + int send_message(char* topic); + int subscribe_to_topic_position(char* topic); + int subscribe_to_topic_control(char* topic); + int subscribe_to_topic_sync(char* topic); + void publish_car(char* topic, int speed, int position); + void publish_road(char* topic, int num); + void start(); // function for serving requests (single thread) + void reset(); // function to create and start the singleton thread + void stop(); + +private: + static Communication *singleton; + + WiFiInterface *wifi; + MQTT::Client<MQTTNetwork, Countdown> *client; + Thread* thread; + + MemoryPool<message_t, 10> mpool; + Queue<message_t, 10> q_car; + + Communication(); + +}; + +#endif \ No newline at end of file