
not running
Communication.h@0:3b4906b8a747, 2019-12-10 (annotated)
- Committer:
- hyan99
- Date:
- Tue Dec 10 22:29:09 2019 +0000
- Revision:
- 0:3b4906b8a747
- Child:
- 2:16b3bd337db2
error
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:3b4906b8a747 | 5 | #include "MQTTNetwork.h" |
hyan99 | 0:3b4906b8a747 | 6 | #include "MQTTClient.h" |
hyan99 | 0:3b4906b8a747 | 7 | #include "MQTTmbed.h" |
hyan99 | 0:3b4906b8a747 | 8 | |
hyan99 | 0:3b4906b8a747 | 9 | |
hyan99 | 0:3b4906b8a747 | 10 | typedef struct { |
hyan99 | 0:3b4906b8a747 | 11 | int speed; // speed for AccCar, -1 for Road |
hyan99 | 0:3b4906b8a747 | 12 | int position; // position for AccCar, update number for Road |
hyan99 | 0:3b4906b8a747 | 13 | char* topic; |
hyan99 | 0:3b4906b8a747 | 14 | } message_t; |
hyan99 | 0:3b4906b8a747 | 15 | |
hyan99 | 0:3b4906b8a747 | 16 | class Communication { |
hyan99 | 0:3b4906b8a747 | 17 | public: |
hyan99 | 0:3b4906b8a747 | 18 | static EventFlags control_flags; // For cars waiting on speed control |
hyan99 | 0:3b4906b8a747 | 19 | static EventFlags sync_flags; |
hyan99 | 0:3b4906b8a747 | 20 | static int speeds[5]; |
hyan99 | 0:3b4906b8a747 | 21 | static int sync; |
hyan99 | 0:3b4906b8a747 | 22 | static int flags[5]; |
hyan99 | 0:3b4906b8a747 | 23 | |
hyan99 | 0:3b4906b8a747 | 24 | // Singleton |
hyan99 | 0:3b4906b8a747 | 25 | static Communication *getInstance(); |
hyan99 | 0:3b4906b8a747 | 26 | |
hyan99 | 0:3b4906b8a747 | 27 | // MQTT Setup |
hyan99 | 0:3b4906b8a747 | 28 | void setup_wifi(); |
hyan99 | 0:3b4906b8a747 | 29 | void setup_mqtt(MQTTNetwork &network); |
hyan99 | 0:3b4906b8a747 | 30 | static void message_arrived(MQTT::MessageData& md); |
hyan99 | 0:3b4906b8a747 | 31 | static void control_arrived(MQTT::MessageData& md); |
hyan99 | 0:3b4906b8a747 | 32 | static void sync_arrived(MQTT::MessageData& md); |
hyan99 | 0:3b4906b8a747 | 33 | int send_message(char* topic); |
hyan99 | 0:3b4906b8a747 | 34 | int subscribe_to_topic_position(char* topic); |
hyan99 | 0:3b4906b8a747 | 35 | int subscribe_to_topic_control(char* topic); |
hyan99 | 0:3b4906b8a747 | 36 | int subscribe_to_topic_sync(char* topic); |
hyan99 | 0:3b4906b8a747 | 37 | void publish_car(char* topic, int speed, int position); |
hyan99 | 0:3b4906b8a747 | 38 | void publish_road(char* topic, int num); |
hyan99 | 0:3b4906b8a747 | 39 | void start(); // function for serving requests (single thread) |
hyan99 | 0:3b4906b8a747 | 40 | void reset(); // function to create and start the singleton thread |
hyan99 | 0:3b4906b8a747 | 41 | void stop(); |
hyan99 | 0:3b4906b8a747 | 42 | |
hyan99 | 0:3b4906b8a747 | 43 | private: |
hyan99 | 0:3b4906b8a747 | 44 | static Communication *singleton; |
hyan99 | 0:3b4906b8a747 | 45 | |
hyan99 | 0:3b4906b8a747 | 46 | WiFiInterface *wifi; |
hyan99 | 0:3b4906b8a747 | 47 | MQTT::Client<MQTTNetwork, Countdown> *client; |
hyan99 | 0:3b4906b8a747 | 48 | Thread* thread; |
hyan99 | 0:3b4906b8a747 | 49 | |
hyan99 | 0:3b4906b8a747 | 50 | MemoryPool<message_t, 10> mpool; |
hyan99 | 0:3b4906b8a747 | 51 | Queue<message_t, 10> q_car; |
hyan99 | 0:3b4906b8a747 | 52 | |
hyan99 | 0:3b4906b8a747 | 53 | Communication(); |
hyan99 | 0:3b4906b8a747 | 54 | |
hyan99 | 0:3b4906b8a747 | 55 | }; |
hyan99 | 0:3b4906b8a747 | 56 | |
hyan99 | 0:3b4906b8a747 | 57 | #endif |