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