For Hepta-Sat Lite
HeptaXbee.cpp@12:94abebd3d020, 2019-08-21 (annotated)
- Committer:
- HeptaSatTraining2019
- Date:
- Wed Aug 21 11:18:56 2019 +0000
- Revision:
- 12:94abebd3d020
- Parent:
- 11:a5a0ab9949f3
- Child:
- 13:59679840fb53
xbee library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hepta2ume | 0:1e02be589d1f | 1 | #include "HeptaXbee.h" |
hepta2ume | 0:1e02be589d1f | 2 | #include "mbed.h" |
hepta2ume | 0:1e02be589d1f | 3 | #include <stdarg.h> |
hepta2ume | 0:1e02be589d1f | 4 | |
hepta2ume | 0:1e02be589d1f | 5 | HeptaXbee::HeptaXbee(PinName tx, PinName rx) : xbee(tx,rx) |
hepta2ume | 0:1e02be589d1f | 6 | { |
hepta2ume | 0:1e02be589d1f | 7 | rcmd = 0; |
hepta2ume | 0:1e02be589d1f | 8 | cmdflag = 0; |
hepta2ume | 0:1e02be589d1f | 9 | } |
HEPTA | 7:d07155a73b24 | 10 | void HeptaXbee::baud(int rate) |
HEPTA | 7:d07155a73b24 | 11 | { |
HEPTA | 7:d07155a73b24 | 12 | xbee.baud(rate); |
HEPTA | 7:d07155a73b24 | 13 | } |
HEPTA | 9:96ed36bbc9dd | 14 | void HeptaXbee::xbee_receive(int *xrcmd, int *xcmdflag) |
umeume | 4:ede5c519e238 | 15 | { |
hepta2ume | 0:1e02be589d1f | 16 | xbee.attach(this,&HeptaXbee::commandget,Serial::RxIrq); |
hepta2ume | 0:1e02be589d1f | 17 | //xbee.printf("ssXbeedata= %d,%d",HeptaXbee::rcmd,HeptaXbee::cmdflag); |
hepta2ume | 0:1e02be589d1f | 18 | *xrcmd = HeptaXbee::rcmd; |
hepta2ume | 0:1e02be589d1f | 19 | *xcmdflag = HeptaXbee::cmdflag; |
hepta2ume | 0:1e02be589d1f | 20 | } |
hepta2ume | 0:1e02be589d1f | 21 | |
umeume | 4:ede5c519e238 | 22 | void HeptaXbee::initialize() |
umeume | 4:ede5c519e238 | 23 | { |
hepta2ume | 0:1e02be589d1f | 24 | HeptaXbee::rcmd = 0; |
hepta2ume | 0:1e02be589d1f | 25 | HeptaXbee::cmdflag = 0; |
hepta2ume | 0:1e02be589d1f | 26 | } |
hepta2ume | 0:1e02be589d1f | 27 | |
umeume | 4:ede5c519e238 | 28 | void HeptaXbee::commandget() |
umeume | 4:ede5c519e238 | 29 | { |
hepta2ume | 0:1e02be589d1f | 30 | HeptaXbee::rcmd=xbee.getc(); |
hepta2ume | 0:1e02be589d1f | 31 | HeptaXbee::cmdflag = 1; |
hepta2ume | 0:1e02be589d1f | 32 | //xbee.printf("Xbeedata= %d,%d",rcmd,cmdflag); |
hepta2ume | 0:1e02be589d1f | 33 | } |
hepta2ume | 0:1e02be589d1f | 34 | |
HeptaSatTraining2019 | 12:94abebd3d020 | 35 | //*gyro×3 = A,B,C *Accel×3 = D,E,F *magnetic×3 = G,H,I *latitude = J *longitude = K *altitude = L *battery = M *temperature = N |
HEPTA | 7:d07155a73b24 | 36 | void HeptaXbee::xbee_transmit(char dataA[],char dataB[],char dataC[],char dataD[],char dataE[], |
HEPTA | 7:d07155a73b24 | 37 | char dataF[],char dataG[],char dataH[],char dataI[],char dataJ[], |
HEPTA | 7:d07155a73b24 | 38 | char dataK[],char dataL[],char dataM[],char dataN[]) |
hepta2ume | 0:1e02be589d1f | 39 | { |
HEPTA | 7:d07155a73b24 | 40 | char OutputData[255]; |
HEPTA | 7:d07155a73b24 | 41 | int output_num,numA,numB,numC,numD,numE,numF,numG,numH,numI,numJ,numK,numL,numM,numN; |
HEPTA | 10:9c8433d32d11 | 42 | int sum = 0; |
HEPTA | 10:9c8433d32d11 | 43 | int check; |
HEPTA | 7:d07155a73b24 | 44 | numA = sizeof(dataA) / sizeof(dataA[0]); |
HEPTA | 7:d07155a73b24 | 45 | numB = sizeof(dataB) / sizeof(dataB[0]); |
HEPTA | 7:d07155a73b24 | 46 | numC = sizeof(dataC) / sizeof(dataC[0]); |
HEPTA | 7:d07155a73b24 | 47 | numD = sizeof(dataD) / sizeof(dataD[0]); |
HEPTA | 7:d07155a73b24 | 48 | numE = sizeof(dataE) / sizeof(dataE[0]); |
HEPTA | 7:d07155a73b24 | 49 | numF = sizeof(dataF) / sizeof(dataF[0]); |
HEPTA | 7:d07155a73b24 | 50 | numG = sizeof(dataG) / sizeof(dataG[0]); |
HEPTA | 7:d07155a73b24 | 51 | numH = sizeof(dataH) / sizeof(dataH[0]); |
HEPTA | 8:8e925f198e21 | 52 | numI = sizeof(dataI) / sizeof(dataI[0]); |
HEPTA | 7:d07155a73b24 | 53 | numJ = sizeof(dataJ) / sizeof(dataJ[0])*2; |
HEPTA | 8:8e925f198e21 | 54 | numK = sizeof(dataK) / sizeof(dataK[0])*2; |
HEPTA | 7:d07155a73b24 | 55 | numL = sizeof(dataL) / sizeof(dataL[0]); |
HEPTA | 7:d07155a73b24 | 56 | numM = sizeof(dataM) / sizeof(dataM[0]); |
HEPTA | 7:d07155a73b24 | 57 | numN = sizeof(dataN) / sizeof(dataN[0]); |
HEPTA | 7:d07155a73b24 | 58 | output_num=numA+numB+numC+numD+numE+numF+numG+numH+numI+numJ+numK+numL+numM+numN; |
umeume | 3:c3e667ee70f1 | 59 | int N=0,i=0,ii; |
HEPTA | 7:d07155a73b24 | 60 | for(i = 0; i <= numA-1; i++) { |
HEPTA | 7:d07155a73b24 | 61 | OutputData[N+i] = dataA[i]; |
hepta2ume | 0:1e02be589d1f | 62 | } |
HEPTA | 7:d07155a73b24 | 63 | N=i; |
HEPTA | 7:d07155a73b24 | 64 | for(i = 0; i <= numB-1; i++) { |
HEPTA | 7:d07155a73b24 | 65 | OutputData[N+i] = dataB[i]; |
umeume | 4:ede5c519e238 | 66 | } |
HEPTA | 7:d07155a73b24 | 67 | N=N+i; |
HEPTA | 7:d07155a73b24 | 68 | for(i = 0; i <= numC-1; i++) { |
HEPTA | 7:d07155a73b24 | 69 | OutputData[N+i] = dataC[i]; |
hepta2ume | 0:1e02be589d1f | 70 | } |
HEPTA | 7:d07155a73b24 | 71 | N=N+i; |
HEPTA | 7:d07155a73b24 | 72 | for(i = 0; i <= numD-1; i++) { |
HEPTA | 7:d07155a73b24 | 73 | OutputData[N+i] = dataD[i]; |
hepta2ume | 0:1e02be589d1f | 74 | } |
HEPTA | 7:d07155a73b24 | 75 | N=N+i; |
HEPTA | 7:d07155a73b24 | 76 | for(i = 0; i <= numE-1; i++) { |
HEPTA | 7:d07155a73b24 | 77 | OutputData[N+i] = dataE[i]; |
umeume | 4:ede5c519e238 | 78 | } |
HEPTA | 7:d07155a73b24 | 79 | N=N+i; |
HEPTA | 7:d07155a73b24 | 80 | for(i = 0; i <= numF-1; i++) { |
HEPTA | 7:d07155a73b24 | 81 | OutputData[N+i] = dataF[i]; |
hepta2ume | 0:1e02be589d1f | 82 | } |
HEPTA | 7:d07155a73b24 | 83 | N=N+i; |
HEPTA | 7:d07155a73b24 | 84 | for(i = 0; i <= numG-1; i++) { |
HEPTA | 7:d07155a73b24 | 85 | OutputData[N+i] = dataG[i]; |
umeume | 4:ede5c519e238 | 86 | } |
HEPTA | 7:d07155a73b24 | 87 | N=N+i; |
HEPTA | 7:d07155a73b24 | 88 | for(i = 0; i <= numH-1; i++) { |
HEPTA | 7:d07155a73b24 | 89 | OutputData[N+i] = dataH[i]; |
HEPTA | 7:d07155a73b24 | 90 | } |
HEPTA | 7:d07155a73b24 | 91 | N=N+i; |
HEPTA | 7:d07155a73b24 | 92 | for(i = 0; i <= numI-1; i++) { |
HEPTA | 7:d07155a73b24 | 93 | OutputData[N+i] = dataI[i]; |
hepta2ume | 0:1e02be589d1f | 94 | } |
HEPTA | 7:d07155a73b24 | 95 | N=N+i; |
HEPTA | 7:d07155a73b24 | 96 | for(i = 0; i <= numJ-1; i++) { |
HEPTA | 7:d07155a73b24 | 97 | OutputData[N+i] = dataJ[i]; |
hepta2ume | 0:1e02be589d1f | 98 | } |
HEPTA | 7:d07155a73b24 | 99 | N=N+i; |
HEPTA | 7:d07155a73b24 | 100 | for(i = 0; i <= numK-1; i++) { |
HEPTA | 7:d07155a73b24 | 101 | OutputData[N+i] = dataK[i]; |
HEPTA | 7:d07155a73b24 | 102 | } |
HEPTA | 7:d07155a73b24 | 103 | N=N+i; |
HEPTA | 7:d07155a73b24 | 104 | for(i = 0; i <= numL-1; i++) { |
HEPTA | 7:d07155a73b24 | 105 | OutputData[N+i] = dataL[i]; |
umeume | 4:ede5c519e238 | 106 | } |
HEPTA | 7:d07155a73b24 | 107 | N=N+i; |
HEPTA | 7:d07155a73b24 | 108 | for(i = 0; i <= numM-1; i++) { |
HEPTA | 7:d07155a73b24 | 109 | OutputData[N+i] = dataM[i]; |
hepta2ume | 0:1e02be589d1f | 110 | } |
HEPTA | 7:d07155a73b24 | 111 | N=N+i; |
HEPTA | 7:d07155a73b24 | 112 | for(i = 0; i <= numN-1; i++) { |
HEPTA | 7:d07155a73b24 | 113 | OutputData[N+i] = dataN[i]; |
umeume | 4:ede5c519e238 | 114 | } |
HEPTA | 10:9c8433d32d11 | 115 | //check sum |
HEPTA | 7:d07155a73b24 | 116 | for(ii=0; ii<output_num; ii++) { |
HEPTA | 10:9c8433d32d11 | 117 | sum = sum + OutputData[ii]; |
hepta2ume | 1:c31e4a5db17c | 118 | } |
HEPTA | 10:9c8433d32d11 | 119 | check = (~sum)&0xFF; |
HEPTA | 10:9c8433d32d11 | 120 | char check_sum[2],flame[2]; |
HEPTA | 10:9c8433d32d11 | 121 | sprintf( check_sum, "%02X", check); |
HEPTA | 10:9c8433d32d11 | 122 | sprintf( flame, "%02X", output_num); |
HEPTA | 10:9c8433d32d11 | 123 | // transmit data |
HEPTA | 11:a5a0ab9949f3 | 124 | //for(int i = 0;i<2;i++){ |
HeptaSatTraining2019 | 12:94abebd3d020 | 125 | // xbee.putc(flame[i]);//frame length |
HeptaSatTraining2019 | 12:94abebd3d020 | 126 | // } |
HEPTA | 10:9c8433d32d11 | 127 | for(ii=0; ii<output_num; ii++) { |
HEPTA | 10:9c8433d32d11 | 128 | xbee.putc(OutputData[ii]);//data |
HEPTA | 10:9c8433d32d11 | 129 | } |
HeptaSatTraining2019 | 12:94abebd3d020 | 130 | for(int i = 0; i<2; i++) { |
HEPTA | 10:9c8433d32d11 | 131 | xbee.putc(check_sum[i]); |
HeptaSatTraining2019 | 12:94abebd3d020 | 132 | } |
HEPTA | 10:9c8433d32d11 | 133 | xbee.putc(0x0a);//endcode |
umeume | 4:ede5c519e238 | 134 | } |
umeume | 4:ede5c519e238 | 135 | void HeptaXbee::puts( char *s ) |
umeume | 4:ede5c519e238 | 136 | { |
umeume | 4:ede5c519e238 | 137 | while ( char c = *s++ ) |
umeume | 4:ede5c519e238 | 138 | xbee.putc( c ); |
umeume | 4:ede5c519e238 | 139 | } |
HeptaSatTraining2019 | 12:94abebd3d020 | 140 | void HeptaXbee::scanf(char *format, ... ) |
HeptaSatTraining2019 | 12:94abebd3d020 | 141 | { |
HeptaSatTraining2019 | 12:94abebd3d020 | 142 | xbee.scanf(format); |
HeptaSatTraining2019 | 12:94abebd3d020 | 143 | } |
umeume | 4:ede5c519e238 | 144 | void HeptaXbee::printf( char *format, ... ) |
umeume | 4:ede5c519e238 | 145 | { |
umeume | 4:ede5c519e238 | 146 | char s[ 100 ]; |
umeume | 4:ede5c519e238 | 147 | va_list args; |
umeume | 4:ede5c519e238 | 148 | |
umeume | 4:ede5c519e238 | 149 | va_start( args, format ); |
umeume | 4:ede5c519e238 | 150 | vsnprintf( s, 100, format, args ); |
umeume | 4:ede5c519e238 | 151 | va_end( args ); |
umeume | 4:ede5c519e238 | 152 | |
umeume | 4:ede5c519e238 | 153 | xbee.puts( s ); |
umeume | 4:ede5c519e238 | 154 | } |
umeume | 4:ede5c519e238 | 155 | char HeptaXbee::getc() |
umeume | 4:ede5c519e238 | 156 | { |
umeume | 4:ede5c519e238 | 157 | char character; |
umeume | 4:ede5c519e238 | 158 | character = xbee.getc(); |
umeume | 4:ede5c519e238 | 159 | return character; |
umeume | 4:ede5c519e238 | 160 | } |
umeume | 4:ede5c519e238 | 161 | |
HEPTA | 7:d07155a73b24 | 162 | void HeptaXbee::putc(char data) |
umeume | 4:ede5c519e238 | 163 | { |
HEPTA | 7:d07155a73b24 | 164 | xbee.putc(data); |
umeume | 4:ede5c519e238 | 165 | } |