Some test about how work with Queue. The target is send repeated messages via CAN-BUS for simulate a basic car communication for testing secondary ECUs
main.cpp@0:d3a56afeb685, 2019-06-08 (annotated)
- Committer:
- JohnnyK
- Date:
- Sat Jun 08 12:36:35 2019 +0000
- Revision:
- 0:d3a56afeb685
First working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 0:d3a56afeb685 | 1 | #include "mbed.h" |
JohnnyK | 0:d3a56afeb685 | 2 | #define MAX 3//number of dummy messages |
JohnnyK | 0:d3a56afeb685 | 3 | |
JohnnyK | 0:d3a56afeb685 | 4 | struct RepMsg { //Repeating message |
JohnnyK | 0:d3a56afeb685 | 5 | CANMessage canmsg; |
JohnnyK | 0:d3a56afeb685 | 6 | int repeatTime; //ms |
JohnnyK | 0:d3a56afeb685 | 7 | }; |
JohnnyK | 0:d3a56afeb685 | 8 | RepMsg *tempRepMsg[MAX]; |
JohnnyK | 0:d3a56afeb685 | 9 | |
JohnnyK | 0:d3a56afeb685 | 10 | Thread thread; |
JohnnyK | 0:d3a56afeb685 | 11 | EventQueue e_queue; |
JohnnyK | 0:d3a56afeb685 | 12 | Queue<RepMsg, MAX * 5> queue; |
JohnnyK | 0:d3a56afeb685 | 13 | DigitalOut myled1(LED1); |
JohnnyK | 0:d3a56afeb685 | 14 | DigitalOut myled2(LED2); |
JohnnyK | 0:d3a56afeb685 | 15 | Serial pc(USBTX, USBRX); |
JohnnyK | 0:d3a56afeb685 | 16 | CAN can2(PB_8, PB_9); |
JohnnyK | 0:d3a56afeb685 | 17 | CAN can1(PB_12, PB_13); |
JohnnyK | 0:d3a56afeb685 | 18 | |
JohnnyK | 0:d3a56afeb685 | 19 | void dumy_msg (void) { //make some dummy messages |
JohnnyK | 0:d3a56afeb685 | 20 | for(int i = 0; i < MAX; i++){ |
JohnnyK | 0:d3a56afeb685 | 21 | tempRepMsg[i] = new RepMsg; |
JohnnyK | 0:d3a56afeb685 | 22 | char id_temp = 0x00 + i; |
JohnnyK | 0:d3a56afeb685 | 23 | pc.printf("Msg ID 0x%02x data: ",id_temp); |
JohnnyK | 0:d3a56afeb685 | 24 | tempRepMsg[i]->canmsg.id = id_temp; |
JohnnyK | 0:d3a56afeb685 | 25 | tempRepMsg[i]->canmsg.len = 8; |
JohnnyK | 0:d3a56afeb685 | 26 | tempRepMsg[i]->repeatTime = (i + 1) * 250; //ms |
JohnnyK | 0:d3a56afeb685 | 27 | |
JohnnyK | 0:d3a56afeb685 | 28 | for(int y = 0; y < 8; y++){ |
JohnnyK | 0:d3a56afeb685 | 29 | char temp = (0x08 + i) * (y + 1) - y; |
JohnnyK | 0:d3a56afeb685 | 30 | tempRepMsg[i]->canmsg.data[y] = temp; |
JohnnyK | 0:d3a56afeb685 | 31 | pc.printf(" 0x%02x",temp); |
JohnnyK | 0:d3a56afeb685 | 32 | } |
JohnnyK | 0:d3a56afeb685 | 33 | pc.printf("\n"); |
JohnnyK | 0:d3a56afeb685 | 34 | } |
JohnnyK | 0:d3a56afeb685 | 35 | } |
JohnnyK | 0:d3a56afeb685 | 36 | |
JohnnyK | 0:d3a56afeb685 | 37 | void put_into_ (RepMsg *msg) { |
JohnnyK | 0:d3a56afeb685 | 38 | queue.put(msg); |
JohnnyK | 0:d3a56afeb685 | 39 | } |
JohnnyK | 0:d3a56afeb685 | 40 | |
JohnnyK | 0:d3a56afeb685 | 41 | int main (void) { |
JohnnyK | 0:d3a56afeb685 | 42 | pc.baud(115200); |
JohnnyK | 0:d3a56afeb685 | 43 | pc.printf("Starts example\n"); |
JohnnyK | 0:d3a56afeb685 | 44 | dumy_msg(); |
JohnnyK | 0:d3a56afeb685 | 45 | wait(1); |
JohnnyK | 0:d3a56afeb685 | 46 | thread.start(callback(&e_queue, &EventQueue::dispatch_forever)); |
JohnnyK | 0:d3a56afeb685 | 47 | for(int i = 0; i < MAX; i++){ //it will make queue.call_every for all dummy messages |
JohnnyK | 0:d3a56afeb685 | 48 | e_queue.call_every(tempRepMsg[i]->repeatTime,put_into_, tempRepMsg[i]); |
JohnnyK | 0:d3a56afeb685 | 49 | } |
JohnnyK | 0:d3a56afeb685 | 50 | CANMessage msg; |
JohnnyK | 0:d3a56afeb685 | 51 | while(1) { |
JohnnyK | 0:d3a56afeb685 | 52 | if (queue.empty() == false) { |
JohnnyK | 0:d3a56afeb685 | 53 | osEvent evt = queue.get(); |
JohnnyK | 0:d3a56afeb685 | 54 | if (evt.status == osEventMessage) { |
JohnnyK | 0:d3a56afeb685 | 55 | RepMsg *message = (RepMsg*)evt.value.p; |
JohnnyK | 0:d3a56afeb685 | 56 | CANMessage w_msg = message->canmsg; |
JohnnyK | 0:d3a56afeb685 | 57 | can1.write(w_msg); |
JohnnyK | 0:d3a56afeb685 | 58 | myled1 = !myled1; |
JohnnyK | 0:d3a56afeb685 | 59 | } |
JohnnyK | 0:d3a56afeb685 | 60 | } |
JohnnyK | 0:d3a56afeb685 | 61 | |
JohnnyK | 0:d3a56afeb685 | 62 | if(can2.read(msg)) { |
JohnnyK | 0:d3a56afeb685 | 63 | pc.printf("ID: %9x Len: %d Data: %2x %2x %2x %2x %2x %2x %2x %2x\n", msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4], msg.data[5], msg.data[6], msg.data[07]); |
JohnnyK | 0:d3a56afeb685 | 64 | myled2 = !myled2; |
JohnnyK | 0:d3a56afeb685 | 65 | } |
JohnnyK | 0:d3a56afeb685 | 66 | } |
JohnnyK | 0:d3a56afeb685 | 67 | } |