A class for using MU2.
Dependents: EM_Mission FM_integration_copy FM_integration_off FM_integration
MU2Class.cpp@2:2297b491e452, 2013-07-06 (annotated)
- Committer:
- ATKINZ117
- Date:
- Sat Jul 06 07:29:39 2013 +0000
- Revision:
- 2:2297b491e452
- Parent:
- 1:c60752b4acb9
- Child:
- 4:a7d413a2889d
changed how to communicate via serial;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ATKINZ117 | 0:6c7ba361f738 | 1 | #include "MU2Class.h" |
ATKINZ117 | 0:6c7ba361f738 | 2 | #include "mbed.h" |
ATKINZ117 | 2:2297b491e452 | 3 | //#include <stdlib.h> |
ATKINZ117 | 0:6c7ba361f738 | 4 | |
ATKINZ117 | 0:6c7ba361f738 | 5 | MU2Class::MU2Class(PinName tx,PinName rx){ |
ATKINZ117 | 0:6c7ba361f738 | 6 | _tx=tx; |
ATKINZ117 | 0:6c7ba361f738 | 7 | _rx=rx; |
ATKINZ117 | 0:6c7ba361f738 | 8 | } |
ATKINZ117 | 0:6c7ba361f738 | 9 | |
ATKINZ117 | 0:6c7ba361f738 | 10 | uint8_t MU2Class::Store_Str(char data_str[], int index, char Message[]){ |
ATKINZ117 | 0:6c7ba361f738 | 11 | uint8_t j=0; |
ATKINZ117 | 0:6c7ba361f738 | 12 | while(Message[j]!='\0'&&Message[j]!='^') data_str[index++]=Message[j++];//store char in message into data_str and returns the last index. |
ATKINZ117 | 0:6c7ba361f738 | 13 | return index; |
ATKINZ117 | 0:6c7ba361f738 | 14 | } |
ATKINZ117 | 0:6c7ba361f738 | 15 | |
ATKINZ117 | 2:2297b491e452 | 16 | /*uint8_t MU2Class::Store_Int(int data_int[], int index, int Message[]){ |
ATKINZ117 | 2:2297b491e452 | 17 | uint8_t j=0; |
ATKINZ117 | 2:2297b491e452 | 18 | while(Message[j]!='\0'&&Message[j]!='^') data_int[index++]=Message[j++];//store char in message into data_str and returns the last index. |
ATKINZ117 | 2:2297b491e452 | 19 | return index; |
ATKINZ117 | 2:2297b491e452 | 20 | }*/ |
ATKINZ117 | 2:2297b491e452 | 21 | |
ATKINZ117 | 1:c60752b4acb9 | 22 | void MU2Class::send(char Send_Message[]){ |
ATKINZ117 | 0:6c7ba361f738 | 23 | Serial MyMu2(_tx,_rx); |
ATKINZ117 | 0:6c7ba361f738 | 24 | Serial pc(USBTX,USBRX); |
ATKINZ117 | 0:6c7ba361f738 | 25 | |
ATKINZ117 | 0:6c7ba361f738 | 26 | int i=4, str_length=0,send_index=0; |
ATKINZ117 | 0:6c7ba361f738 | 27 | char send_data[255]; |
ATKINZ117 | 0:6c7ba361f738 | 28 | |
ATKINZ117 | 0:6c7ba361f738 | 29 | MyMu2.baud(19200); |
ATKINZ117 | 0:6c7ba361f738 | 30 | pc.baud(19200); |
ATKINZ117 | 0:6c7ba361f738 | 31 | |
ATKINZ117 | 0:6c7ba361f738 | 32 | send_index=0;//initializing the variant. |
ATKINZ117 | 0:6c7ba361f738 | 33 | send_index=Store_Str(send_data, send_index, "@DT00^");//this variant is the last index of the string.The reason why char "^" here is that for the case of the data size 10,20,A0 or something . Store_Str copies the string of its last variant into its first variant . |
ATKINZ117 | 0:6c7ba361f738 | 34 | |
ATKINZ117 | 0:6c7ba361f738 | 35 | while(Send_Message[str_length]!='\0'){ |
ATKINZ117 | 0:6c7ba361f738 | 36 | send_data[send_index++]=Send_Message[str_length++];//storing the string we wanna send into the send_data until null terminater appers. |
ATKINZ117 | 0:6c7ba361f738 | 37 | } |
ATKINZ117 | 0:6c7ba361f738 | 38 | send_data[send_index++]='\r';//insert carrige return at the last of send_data. |
ATKINZ117 | 0:6c7ba361f738 | 39 | send_data[send_index++]='\n'; |
ATKINZ117 | 0:6c7ba361f738 | 40 | |
ATKINZ117 | 0:6c7ba361f738 | 41 | //str_length-=2; //the code block below is for making data size proper. |
ATKINZ117 | 0:6c7ba361f738 | 42 | |
ATKINZ117 | 0:6c7ba361f738 | 43 | for(i=4; i>2; i--){ |
ATKINZ117 | 0:6c7ba361f738 | 44 | if(str_length%16<10){ |
ATKINZ117 | 0:6c7ba361f738 | 45 | send_data[i]='0'+str_length%16; |
ATKINZ117 | 0:6c7ba361f738 | 46 | }else{ |
ATKINZ117 | 0:6c7ba361f738 | 47 | switch(str_length%16){ |
ATKINZ117 | 0:6c7ba361f738 | 48 | case 10:send_data[i]='A'; break; |
ATKINZ117 | 0:6c7ba361f738 | 49 | case 11:send_data[i]='B'; break; |
ATKINZ117 | 0:6c7ba361f738 | 50 | case 12:send_data[i]='C'; break; |
ATKINZ117 | 0:6c7ba361f738 | 51 | case 13:send_data[i]='D'; break; |
ATKINZ117 | 0:6c7ba361f738 | 52 | case 14:send_data[i]='E'; break; |
ATKINZ117 | 0:6c7ba361f738 | 53 | case 15:send_data[i]='F'; break; |
ATKINZ117 | 0:6c7ba361f738 | 54 | } |
ATKINZ117 | 0:6c7ba361f738 | 55 | } |
ATKINZ117 | 0:6c7ba361f738 | 56 | str_length/=16; |
ATKINZ117 | 0:6c7ba361f738 | 57 | } |
ATKINZ117 | 2:2297b491e452 | 58 | MyMu2.printf(send_data); |
ATKINZ117 | 2:2297b491e452 | 59 | /*for(int j=0;j<=send_index;j++){ |
ATKINZ117 | 2:2297b491e452 | 60 | MyMu2.putc(send_data[j]); |
ATKINZ117 | 2:2297b491e452 | 61 | pc.putc(send_data[j]); //for debugging.Comment out when U use this in practice. |
ATKINZ117 | 2:2297b491e452 | 62 | }*/ |
ATKINZ117 | 2:2297b491e452 | 63 | } |
ATKINZ117 | 2:2297b491e452 | 64 | |
ATKINZ117 | 2:2297b491e452 | 65 | /*void MU2Class::send(int Send_Message[]){ |
ATKINZ117 | 2:2297b491e452 | 66 | Serial MyMu2(_tx,_rx); |
ATKINZ117 | 2:2297b491e452 | 67 | Serial pc(USBTX,USBRX); |
ATKINZ117 | 2:2297b491e452 | 68 | |
ATKINZ117 | 2:2297b491e452 | 69 | int i=4, int_length=0,send_index=0; |
ATKINZ117 | 2:2297b491e452 | 70 | char send_data[255]; |
ATKINZ117 | 2:2297b491e452 | 71 | |
ATKINZ117 | 2:2297b491e452 | 72 | MyMu2.baud(19200); |
ATKINZ117 | 2:2297b491e452 | 73 | pc.baud(19200); |
ATKINZ117 | 2:2297b491e452 | 74 | |
ATKINZ117 | 2:2297b491e452 | 75 | send_index=0;//initializing the variant. |
ATKINZ117 | 2:2297b491e452 | 76 | send_index=Store_Str(send_data, send_index, "@DT00^");//this variant is the last index of the string.The reason why char "^" here is that for the case of the data size 10,20,A0 or something . Store_Str copies the string of its last variant into its first variant . |
ATKINZ117 | 2:2297b491e452 | 77 | |
ATKINZ117 | 2:2297b491e452 | 78 | while(Send_Message[int_length]!='\0'){ |
ATKINZ117 | 2:2297b491e452 | 79 | //send_data[send_index++]=Send_Message[int_length++];//storing the string we wanna send into the send_data until null terminater appers. |
ATKINZ117 | 2:2297b491e452 | 80 | //itoa(Send_Message[int_length++],send_data[send_index++],10); |
ATKINZ117 | 2:2297b491e452 | 81 | |
ATKINZ117 | 2:2297b491e452 | 82 | } |
ATKINZ117 | 2:2297b491e452 | 83 | send_data[send_index++]='\r';//insert carrige return at the last of send_data. |
ATKINZ117 | 2:2297b491e452 | 84 | send_data[send_index++]='\n'; |
ATKINZ117 | 2:2297b491e452 | 85 | |
ATKINZ117 | 2:2297b491e452 | 86 | //str_length-=2; //the code block below is for making data size proper. |
ATKINZ117 | 2:2297b491e452 | 87 | |
ATKINZ117 | 2:2297b491e452 | 88 | for(i=4; i>2; i--){ |
ATKINZ117 | 2:2297b491e452 | 89 | if(int_length%16<10){ |
ATKINZ117 | 2:2297b491e452 | 90 | send_data[i]='0'+int_length%16; |
ATKINZ117 | 2:2297b491e452 | 91 | }else{ |
ATKINZ117 | 2:2297b491e452 | 92 | switch(int_length%16){ |
ATKINZ117 | 2:2297b491e452 | 93 | case 10:send_data[i]='A'; break; |
ATKINZ117 | 2:2297b491e452 | 94 | case 11:send_data[i]='B'; break; |
ATKINZ117 | 2:2297b491e452 | 95 | case 12:send_data[i]='C'; break; |
ATKINZ117 | 2:2297b491e452 | 96 | case 13:send_data[i]='D'; break; |
ATKINZ117 | 2:2297b491e452 | 97 | case 14:send_data[i]='E'; break; |
ATKINZ117 | 2:2297b491e452 | 98 | case 15:send_data[i]='F'; break; |
ATKINZ117 | 2:2297b491e452 | 99 | } |
ATKINZ117 | 2:2297b491e452 | 100 | } |
ATKINZ117 | 2:2297b491e452 | 101 | int_length/=16; |
ATKINZ117 | 2:2297b491e452 | 102 | } |
ATKINZ117 | 0:6c7ba361f738 | 103 | for(int j=0;j<=send_index;j++){ |
ATKINZ117 | 0:6c7ba361f738 | 104 | MyMu2.putc(send_data[j]); |
ATKINZ117 | 0:6c7ba361f738 | 105 | pc.putc(send_data[j]); //for debugging.Comment out when U use this in practice. |
ATKINZ117 | 0:6c7ba361f738 | 106 | } |
ATKINZ117 | 2:2297b491e452 | 107 | }*/ |