![](/media/cache/profiles/altb_JGUSAOG.jpg.50x50_q85.jpg)
Mirror actuator for RT2 lab
uart_comm_thread.h@7:942fd77d5e19, 2021-04-15 (annotated)
- Committer:
- altb2
- Date:
- Thu Apr 15 05:36:55 2021 +0000
- Revision:
- 7:942fd77d5e19
- Child:
- 8:49ac75c42da0
intermediate
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altb2 | 7:942fd77d5e19 | 1 | #ifndef UART_COMM_THREAD_H_ |
altb2 | 7:942fd77d5e19 | 2 | #define UART_COMM_THREAD_H_ |
altb2 | 7:942fd77d5e19 | 3 | |
altb2 | 7:942fd77d5e19 | 4 | #include "mbed.h" |
altb2 | 7:942fd77d5e19 | 5 | #include "ThreadFlag.h" |
altb2 | 7:942fd77d5e19 | 6 | #include "data_structs.h" |
altb2 | 7:942fd77d5e19 | 7 | #include "data_structs.h" |
altb2 | 7:942fd77d5e19 | 8 | #include "Mirror_Kinematic.h" |
altb2 | 7:942fd77d5e19 | 9 | |
altb2 | 7:942fd77d5e19 | 10 | using namespace std; |
altb2 | 7:942fd77d5e19 | 11 | |
altb2 | 7:942fd77d5e19 | 12 | extern DATA_Xchange data; |
altb2 | 7:942fd77d5e19 | 13 | extern Mirror_Kinematic mk; |
altb2 | 7:942fd77d5e19 | 14 | |
altb2 | 7:942fd77d5e19 | 15 | // "protocol" specifics |
altb2 | 7:942fd77d5e19 | 16 | |
altb2 | 7:942fd77d5e19 | 17 | #define BUF_LEN 20 // max 256 |
altb2 | 7:942fd77d5e19 | 18 | #define DATA_LEN 20 // max 256 |
altb2 | 7:942fd77d5e19 | 19 | |
altb2 | 7:942fd77d5e19 | 20 | // states |
altb2 | 7:942fd77d5e19 | 21 | #define IDLE 0 |
altb2 | 7:942fd77d5e19 | 22 | #define WAIT 1 |
altb2 | 7:942fd77d5e19 | 23 | #define RECEIVE 2 |
altb2 | 7:942fd77d5e19 | 24 | #define DONE 3 |
altb2 | 7:942fd77d5e19 | 25 | |
altb2 | 7:942fd77d5e19 | 26 | #define LEN_OF_EXP_TYPE2 1 // length in bytes of expected Type |
altb2 | 7:942fd77d5e19 | 27 | #define NUM_OF_VALUE 7 // number of expected values |
altb2 | 7:942fd77d5e19 | 28 | #define EXPECTED2 LEN_OF_EXP_TYPE2 * NUM_OF_VALUE // byte per Value * expected values = total expected bytes |
altb2 | 7:942fd77d5e19 | 29 | |
altb2 | 7:942fd77d5e19 | 30 | extern Mirror_Kinematic mc; |
altb2 | 7:942fd77d5e19 | 31 | |
altb2 | 7:942fd77d5e19 | 32 | // predefiniton for callback (couldn't implement as member) |
altb2 | 7:942fd77d5e19 | 33 | |
altb2 | 7:942fd77d5e19 | 34 | class uart_comm_thread{ |
altb2 | 7:942fd77d5e19 | 35 | public: |
altb2 | 7:942fd77d5e19 | 36 | // public members |
altb2 | 7:942fd77d5e19 | 37 | uart_comm_thread(BufferedSerial*,float); |
altb2 | 7:942fd77d5e19 | 38 | virtual ~uart_comm_thread(); |
altb2 | 7:942fd77d5e19 | 39 | void run(void); // runs the statemachine, call this function periodicly, returns true when new data ready (only true for 1 cycle) |
altb2 | 7:942fd77d5e19 | 40 | // void request(); // request new set of data |
altb2 | 7:942fd77d5e19 | 41 | void start_uart(void); |
altb2 | 7:942fd77d5e19 | 42 | |
altb2 | 7:942fd77d5e19 | 43 | // public vars |
altb2 | 7:942fd77d5e19 | 44 | // public vars |
altb2 | 7:942fd77d5e19 | 45 | const uint8_t N = DATA_LEN; |
altb2 | 7:942fd77d5e19 | 46 | |
altb2 | 7:942fd77d5e19 | 47 | uint16_t head[6]; |
altb2 | 7:942fd77d5e19 | 48 | float f_values[20]; |
altb2 | 7:942fd77d5e19 | 49 | uint8_t checksum; |
altb2 | 7:942fd77d5e19 | 50 | uint8_t buffer[80]; // RX buffer |
altb2 | 7:942fd77d5e19 | 51 | uint8_t buffCnt; // max 255 |
altb2 | 7:942fd77d5e19 | 52 | uint8_t expected; |
altb2 | 7:942fd77d5e19 | 53 | uint8_t num_floats; |
altb2 | 7:942fd77d5e19 | 54 | uint8_t k_write; |
altb2 | 7:942fd77d5e19 | 55 | |
altb2 | 7:942fd77d5e19 | 56 | private: |
altb2 | 7:942fd77d5e19 | 57 | |
altb2 | 7:942fd77d5e19 | 58 | // private members |
altb2 | 7:942fd77d5e19 | 59 | void sendCmd(char); // sends comand to device |
altb2 | 7:942fd77d5e19 | 60 | void callBack(); // ISR for storing serial bytes |
altb2 | 7:942fd77d5e19 | 61 | void callBack_2(); // ISR for storing serial bytes |
altb2 | 7:942fd77d5e19 | 62 | void init(); // re initializes the buffers and the statemachine |
altb2 | 7:942fd77d5e19 | 63 | float Ts; |
altb2 | 7:942fd77d5e19 | 64 | EventQueue printfQueue; |
altb2 | 7:942fd77d5e19 | 65 | bool analyse_received_data(void); |
altb2 | 7:942fd77d5e19 | 66 | |
altb2 | 7:942fd77d5e19 | 67 | // ------------------- |
altb2 | 7:942fd77d5e19 | 68 | // uint8_t buffer[BUF_LEN]; // RX buffer |
altb2 | 7:942fd77d5e19 | 69 | // uint8_t buffCnt; // max 255 |
altb2 | 7:942fd77d5e19 | 70 | uint8_t state; // statemachine state variable |
altb2 | 7:942fd77d5e19 | 71 | BufferedSerial* uart; // pointer to uart for communication with device |
altb2 | 7:942fd77d5e19 | 72 | ThreadFlag threadFlag; |
altb2 | 7:942fd77d5e19 | 73 | Thread thread; |
altb2 | 7:942fd77d5e19 | 74 | Ticker ticker; |
altb2 | 7:942fd77d5e19 | 75 | Mutex mutex; |
altb2 | 7:942fd77d5e19 | 76 | void sendThreadFlag(); |
altb2 | 7:942fd77d5e19 | 77 | void send_data(char,char,uint16_t,float*); |
altb2 | 7:942fd77d5e19 | 78 | void send_data(char,char,uint16_t,int16_t*); |
altb2 | 7:942fd77d5e19 | 79 | void send_data(char,char,int16_t); |
altb2 | 7:942fd77d5e19 | 80 | void send_data(char,char,uint8_t); |
altb2 | 7:942fd77d5e19 | 81 | }; |
altb2 | 7:942fd77d5e19 | 82 | |
altb2 | 7:942fd77d5e19 | 83 | #endif |
altb2 | 7:942fd77d5e19 | 84 | |
altb2 | 7:942fd77d5e19 | 85 |