chen
2024-11-01 631a90c1116fa33382a88a747c89bf761bc0fa9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <serial_at_cmd_app.h>
#include <global_param.h>
#include <string.h>
#include <stdio.h>
#include "Usart.h"
//#include "dw_app.h"
//#include "UsartII.h"
//#include "main.h"
#include "board.h"
//#include "Spi.h"
//#include "radio.h"
 
 
 
//#define EUART_RX_BUF_SIZE 100
#define Label_id_local    1     //±êÇ©id
#define data_buff_MAX     50    //»ùÕ¾ÊýÁ¿
#define Lora_TXD_bff_MAX  220   //·¢ËÍ»º´æÇø´óС  >4+8+4*»ùÕ¾ÊýÁ¿+2=14+4*»ùÕ¾ÊýÁ¿
 
uint8_t mUsartReceivePack[100] = {0};
uint8_t mUsart2ReceivePack[100] = {0};
 
typedef enum
{
    BLE_RECV_STATE_IDLE = 0,
    BLE_RECV_STATE_MAC,
    BLE_RECV_STATE_RSSI,
    //BLE_RECV_STATE_LF,
} E_BLERecvState;
typedef struct
{
    E_BLERecvState m_eState;
    char m_macHeader[100],m_rssiHeader2[100];
    uint32_t m_u32HeaderLen,m_u32Header2Len;
 
    char m_acRecvBuf[128];
    uint32_t m_u32RecvLen;
} ST_BLERecv;
 
 
uint8_t  mUsart2ReceivePack_before , mUsart2ReceivePack_now;
uint8_t j_ct=0,CT_satrt_temp=0,CT_satrt=0,numb_base=0    ;
 
uint8_t id_cmpare[12]={0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x30, 0x30, 0x30, 0x31};//123456780001//²âÊÔ//RC2202A£º
uint32_t CT_sum=0;
char char_broadcast_data[80];//¹ã²¥Êý¾Ý
 
uint8_t  Lora_TXD_bff[Lora_TXD_bff_MAX]; //lora·¢ËÍ»º´æÇø
uint16_t data_buff[data_buff_MAX][2];    //»ùÕ¾Êý¾Ý»º´æÇø
 
char  char_mac[14],char_ssi[6];
//int32_t distance_int;
double ssi_double;
uint16_t temp_16,temp_16_id,temp_16_distance,data_buff_start;
 
 
uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) 
{
    uint16_t sum = 0;
    uint32_t i;
    for(i=0; i<len; i++)
        sum += pdata[i];
    sum = ~sum;
    return sum;
}
 
void SendComMap(uint8_t data_length, uint8_t index)//±íʾ³É¹¦¶ÁÈ¡µÄ»ØÓ¦°ü
{
    static uint8_t send_frame[EUART_RX_BUF_SIZE];
    uint16_t checksum = 0;
    send_frame[0] = 0x55;
    send_frame[1] = 0xAA;
    send_frame[2] = 0x03;
    send_frame[3] = data_length+5;
    send_frame[4] = CMD_REPLY;
    send_frame[5] = index;
    send_frame[6] = data_length;
    memcpy(&send_frame[7], &g_com_map[index], data_length);
    for(int i = 0; i<(data_length+5); i++)
    {
        checksum += send_frame[2+i];
    }
    checksum = Checksum_u16(&send_frame[2],5+data_length);
    memcpy(&send_frame[7+data_length],&checksum,2);
    
     uart_send(UART_ID1, send_frame,data_length+9, NULL);    
}
 
void UsartParseDataHandler(uint8_t data)
{
    static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
    uint16_t checksum = 0;
    static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ;
    static uint8_t index = 0;
 
 
 
    
    if(usart_receive_state == UsartReceiveWaitChecksum) {            //ÈôÊÕµ½Ð£ÑéºÍ°ü
        checksum = 0;
        for(int i = 0; i<pack_length-5; i++) { 
            checksum += mUsartReceivePack[i];
        }
        checksum += pack_cmd;
        checksum += pack_length;
        checksum += pack_index;
        checksum += pack_datalen;
        checksum += pack_msgtype;
        if(((data + checksum)&0xff) == 0xFF)                //УÑéͨ¹ý
        {
            switch(pack_cmd)
            {
                case CMD_WRITE:
                    //´ÓmUsartReceivePackÖжÁÈ¡pack_length³¤¶ÈµÄ×Ö½Ú£¬·Åµ½È«¾Ö±äÁ¿ÖÐ,¸³Öµ±£´æµÄ²ÎÊý²¢ÇÒ´æÈëflash
                    memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
                    //·µ»ØÒ»¸öerror״̬
                    //SendComMap(pack_datalen,pack_index);
                    save_com_map_to_flash();
                    //delay_ms(100);
                    NVIC_SystemReset();
                    break;
                case CMD_READ:
                    //read°üÖÐdata×Ö½Ú£¬¼´mUsartReceivePack[0]±íʾÊý¾Ý³¤¶È£»
                //´Óg_com_data½á¹¹ÌåÖеĵÚindexλÖöÁÈ¡³¤¶ÈΪmUsartReceivePack[0]µÄ×Ö½Ú£¬·¢ËͳöÀ´
                    SendComMap(pack_datalen,pack_index>>1);        
                    break;
                default:
                    break;
            }
        }
        usart_receive_state = UsartReceiveWaitHead0;
        pack_index = 0;
        pack_length = 0;
        index=0;
    } else if((usart_receive_state == UsartReceiveWaitData) ) {    //Èô¹ûÊÕµ½µÄÊÇÕý³£Í¨Ñ¶°ü
        mUsartReceivePack[index] = data;
        index++;
        if(index == pack_length-5) {        //Èç¹ûÊÕµ½µÄindexÓ볤¶ÈÏàµÈ
            usart_receive_state = UsartReceiveWaitChecksum;
        }
    } else if(usart_receive_state == UsartReceiveWaitDataLen) {                        //ÊÕµ½Ö¸ÁîÀàÐÍ×Ö½Ú
        pack_datalen = data;
        usart_receive_state = UsartReceiveWaitData;
    }else if(usart_receive_state == UsartReceiveWaitIndex) {                        //ÊÕµ½Ö¸ÁîÀàÐÍ×Ö½Ú
        pack_index = data;
        usart_receive_state = UsartReceiveWaitDataLen;
    } else if(usart_receive_state == UsartReceiveWaitCMD) {                            //ÊÕµ½Ö¸ÁîÀàÐÍ×Ö½Ú
        pack_cmd = data;
        usart_receive_state = UsartReceiveWaitIndex;
    } else if(usart_receive_state == UsartReceiveWaitLength) {                        //ÊÕµ½³¤¶È×Ö½Ú
        
            pack_length = data;
            pack_index = 0;            
            usart_receive_state = UsartReceiveWaitCMD;
        
    } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) {    //ÊÕµ½µÚÒ»¸ö°üÍ·
        usart_receive_state = UsartReceiveWaitHead1;
    } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) {    //ÊÕµ½µÚ¶þ¸ö°üÍ·
        usart_receive_state = UsartReceiveWaitMsgType;
    }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) {
        usart_receive_state = UsartReceiveWaitLength;
        pack_msgtype = data;
    }
    else {
        usart_receive_state = UsartReceiveWaitHead0;
        pack_index = 0;
        pack_length = 0;
    }
 
}
 
void Usart2ParseDataHandler(uint8_t data)//UARTÀ¶ÑÀÊý¾Ý·ÖÎö´¦Àí
{
    //´òÓ¡½ÓÊÕµ½µÄÀ¶ÑÀÄ£¿éÐÅÏ¢
    //printf("Õâ¸öÊÇ´«ÈëµÄÊý¾Ý%c",data);
//    static UsartRecvPackState usart2_receive_state = UsartReceiveWaitHead0;
    //static UsartRecvPackState usart2_receive_state = UsartReceiveWaitData;
    
    static ST_BLERecv BLE_recvive;//´´½¨À¶ÑÀ״̬½á¹¹Ìå
    
    
    uint16_t checksum = 0;
 
    static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ;
    static uint8_t index = 0;
    
//    uint8_t dev_id[14],dev_ssi[5],i,k=0;
//    //char  char_mac[14],char_ssi[6];
//    int j=0;
//    //uint16_t temp_16,temp_16_id,temp_16_distance;
//    uint8_t temp8_H,temp8_L;
//    //float temp_float;
//    //double ssi_double;//,distance_double;
//    //int32_t distance_int;
//    uint16_t Label_id=0;  //±êÇ©id
    
 
    //½ÓÊÕÊý¾Ý¿ªÊ¼·ÖÎö
  mUsart2ReceivePack[index]  =  data; //charÊý×é´«½øÀ´²ÎÊýdata
    mUsart2ReceivePack_before  =  mUsart2ReceivePack_now;
    mUsart2ReceivePack_now=data;
    index++;
    if( mUsart2ReceivePack_before == 0x0D && mUsart2ReceivePack_now==0x0A )//½ÓÊÕÊý¾Ýµ½¡°0x0D 0x0A¡±½áÊø
    {
        mUsart2ReceivePack[index]=0;
        //printf("0D0A½øÈëºóµÄÊý¾ÝΪ%sµ½´Ë½áÊø",mUsart2ReceivePack);
//        if(strncmp("RF-CRAZY",(char*)mUsart2ReceivePack,8)==0)
//        {
//        BLE_recvive.m_eState=BLE_RECV_STATE_MAC;//Èç¹ûΪÃû×ÖºóÅжϲ¢¸ÄΪMAC½ÓÊÕ״̬
//        }
//        
        switch(BLE_recvive.m_eState)
            {    case BLE_RECV_STATE_IDLE:
                if(strncmp("RF-CRAZY",(char*)mUsart2ReceivePack,8)==0)
                BLE_recvive.m_eState=BLE_RECV_STATE_MAC;//Èç¹ûΪÃû×ÖºóÅжϲ¢¸ÄΪMAC½ÓÊÕ״̬
                break;
            case BLE_RECV_STATE_MAC://MAC½ÓÊÜ´¦Àí״̬
                strcpy(BLE_recvive.m_macHeader,(char*)mUsart2ReceivePack+5);//¸´ÖƵ½À¶ÑÀ½á¹¹ÌåÖÐ,+5ÊÇΪÁËÌø¹ýMAC×Ö·ûºÍðºÅδ´¦Àí0D0A
                //´¦ÀíMAC×Ö·û²»Èû»ÐÐ
              char*p=BLE_recvive.m_macHeader+strlen(BLE_recvive.m_macHeader)-2;
                *p='\0';
                //printf("´¦Àí¹ýºóµÄMACΪ%s",BLE_recvive.m_macHeader);
                BLE_recvive.m_eState=BLE_RECV_STATE_RSSI;
                break;
            case BLE_RECV_STATE_RSSI://RSSI½ÓÊÕ´¦Àí״̬
                strcpy(BLE_recvive.m_rssiHeader2,(char*)mUsart2ReceivePack+6);//¸´ÖƵ½À¶ÑÀ½á¹¹ÌåÖÐ,+5ÊÇΪÁËÌø¹ýMAC×Ö·ûºÍðºÅδ´¦Àí0D0A
                //printf("  ´¦Àí¹ýºóµÄRSSIΪ%s",BLE_recvive.m_rssiHeader2);
                BLE_recvive.m_eState=BLE_RECV_STATE_IDLE;
                break;
        }
        
//        if(mUsart2ReceivePack[0] ==0x2B )             //¡°+¡±É¨Ãèµ½¿ªÊ¼»ò½áÊø×Ö·û´®µÄÊ׸ö×Ö·û
//        {
//            if (mUsart2ReceivePack[5] ==0x20)    
//            {                
//                printf("UARTɨÃè½áÊø   :+SCAN END\r\n");    //±¾´ÎɨÃè½áÊø                
//                CT_sum++;                                 //°üÐòÖµ
//                if(CT_sum>255) CT_sum=0;
//                    
//          //·¢ËÍÊý¾Ý»º´æ        
//                Lora_TXD_bff[0] = 0x55;
//                Lora_TXD_bff[1] = 0xAA;
//                Lora_TXD_bff[2] = 0x12;            
//                Lora_TXD_bff[3] = 8+4*numb_base;          //Êý¾Ý³¤¶È 8+4*»ùÕ¾ÊýÁ¿                        
//                
//                Label_id=Label_id_local;                  //±êÇ©id
//                memcpy(&Lora_TXD_bff[4],&Label_id ,2);                         
//                
//                Lora_TXD_bff[6] = CT_sum;                 //±êÇ©°üÐò
//                Lora_TXD_bff[7] = 0x32;                   //°´¼ü/µçÁ¿,²âÊÔÊý¾Ý50                    
//                Lora_TXD_bff[8] = 0;                         //LORAµÈ´ýÓ¦´ð
 
//                Lora_TXD_bff[9]= numb_base;               //»ùÕ¾ÊýÁ¿
 
//                for(i=10;i<(10+numb_base*2);i=i+2)
//                {             
//          memcpy(&Lora_TXD_bff[i+0]          ,&data_buff[(i-10)/2][0] ,2);            //»ùÕ¾ID    
//          memcpy(&Lora_TXD_bff[i+numb_base*2],&data_buff[(i-10)/2][1] ,2);            //»ùÕ¾¾àÀë                                    
//                }                    
//               for( i = 2;i<(4+8+4*numb_base); i++)                  //È¥°üÍ·ºó  Ð£Ñé    ÀÛ¼ÓÈ¡·´
//          {
//                    checksum += Lora_TXD_bff[i];
//                }
//                checksum = ~checksum;
//        memcpy(&Lora_TXD_bff[2+8+4*numb_base],&checksum ,2); //ºÍÈ¡·´ Ð£Ñé                
//                
//                Radio_init();                                        //»½ÐѠΪ·¢ËÍ״̬
//                Radio.Send( Lora_TXD_bff, (4+8+4*numb_base));        //LORAÄ£¿éÉÏ´«Êý¾Ý
//                
 
//                //»ùÕ¾¾àÀë  ÇåÁã    
//                for(i=0;i<numb_base;i++)
//                {
//                    //for(k=1;k<2;k++)                                 //Çå¾àÀëÊý¾Ý
//                    for(k=0;k<2;k++)                                   //ÇåIDºÅºÍ¾àÀëÊý¾Ý
//                  {                
//                     data_buff[i][k]=0;
//                    }
//                } 
//        numb_base =0;
//                data_buff_start =0;                
//                        
//                printf("\r\n");        
//          //delay_ms(5);
//          Uart2_SendString(  Uart2_str);                          //·¢ÏÂÒ»´ÎɨÃèÖ¸Áî                                                                                
//            }
//            
//            else if(mUsart2ReceivePack[5] ==0x5F)                   //¡°_"Ï»®Ïß
//            {                
//        printf("UARTɨÃ迪ʼ   :+SCAN_MANU:\r\n");                
//            }                                                                                      
//        }        
//    else
//        {                
//            j=memcmp(id_cmpare,mUsart2ReceivePack,8);               //±È½ÏÄ¿±êÀ¶ÑÀµØÖ· Ç°4¸ö×Ö½Ú        
//            if(j==0)//¸úÄ¿±êÀ¶ÑÀmacµØÖ·Ïàͬ
//            {
//                
//                //¿½±´macµØÖ·
//                for(i=0;i<12;i++)
//              {
//                char_mac[i] = mUsart2ReceivePack[i];                         
//              }
//                char_mac[12]='\0';    
//                
//                
//                //¿½±´ÐźÅ
//                for(i=15;i<18;i++)
//              {
//                char_ssi[i-15] = mUsart2ReceivePack[i];            
//              }
//              if(mUsart2ReceivePack[18]==0x0D || mUsart2ReceivePack[18]==0x20)          //ÐźÅÊÇ-Á½Î»Êý
//              {                                                 
//                    char_ssi[3]= '\0'    ;
//                    char_broadcast_data[0]= '\0';
//                    if (mUsart2ReceivePack[18]==0x0D)                                      //ÐźÅÊÇ-Á½Î»Êý£¬Î޹㲥ÄÚÈÝ
//                    {
//                        CT_satrt=0;
//                    }
//                    else if(mUsart2ReceivePack[18]==0x20)                                  //ÐźÅÊÇ-Á½Î»Êý£¬Óй㲥ÄÚÈÝ,¿Õ¸ñºóһλ¿ªÊ¼£¬µ½0x0D ½áÊø¡£
//                    {
//                        CT_satrt=19;                                                                                                
//                    }
//                    ssi_double = -((char_ssi[1]-0x30)*10 + (char_ssi[2]-0x30));            //2λÐźÅÖµint    
//              }
//              else if(mUsart2ReceivePack[18]>=0x30  &&  mUsart2ReceivePack[18]<=0x39 ) //ÐźÅÊÇ-ÈýλÊý
//                {                                                  
//                  char_ssi[3]=mUsart2ReceivePack[18];                                                             
//                    char_ssi[4]= '\0'    ;    
//          char_broadcast_data[0]= '\0';    
//                    if (mUsart2ReceivePack[19]==0x0D)                                      //ÐźÅÊÇ-ÈýλÊý£¬Î޹㲥ÄÚÈÝ
//                    {
//                        CT_satrt=0;
//                    }
//                    else if(mUsart2ReceivePack[19]==0x20)                                  //ÐźÅÊÇ-ÈýλÊý£¬Óй㲥ÄÚÈÝ,¿Õ¸ñºóһλ¿ªÊ¼£¬µ½0x0D ½áÊø¡£
//                    {
//                        CT_satrt=20;                                                                                                
//                    }     
//          ssi_double = -((char_ssi[1]-0x30)*100 + (char_ssi[2]-0x30)*10 + (char_ssi[3]-0x30));//3λÐźÅÖµint            
//            }                                                                                                
//            
//                printf("mac= %s  ",char_mac);    
//        printf("ssi= %s  ",char_ssi);      
 
//                //»ùÕ¾ID´¦Àí
//        temp_16_id = (char_mac[8]-0x30)*1000 + (char_mac[9]-0x30)*100    + (char_mac[10]-0x30)*10 + (char_mac[11]-0x30);    
//                printf("ID = %d  ",temp_16_id );        
//                //»ùÕ¾¾àÀë´¦Àí
//                temp_16_distance = 0.003083 * exp(-0.09909*ssi_double)*100;//µ¥Î»cm
//                printf("¾àÀë = %d cm  \r\n",temp_16_distance );        
 
//        //±È½ÏIDºÅ²¢¿½±´Êý¾Ýµ½»º´æÇø                          
//                for(i=0;i<data_buff_MAX;i++)
//                {
//                  if(data_buff[i][0] == temp_16_id)     //¼ì²âµ½ÒÑ´æID
//                    {
//                        data_buff[i][1] = temp_16_distance;    
//                        data_buff_start=0;
//                        break;                
//                    }
//          else if    (data_buff[i][0]==0)
//                    {
//                        data_buff_start=i+1;
//                      numb_base=data_buff_start;            //¿½±´É¨Ãèµ½»ùÕ¾µÄÊýÁ¿
//                      break;
//                    }    
//                }    
//                if(data_buff_start!=0)
//                {
//                    data_buff[data_buff_start-1][0] = temp_16_id;            
//          data_buff[data_buff_start-1][1] = temp_16_distance;    
//                      
//                }
//            }    
//        }     
        index=0;
        mUsart2ReceivePack_before=0;
        mUsart2ReceivePack_now=0;        
    }
//printf("´¦Àí¹ýºóµÄÊý×éΪ%s\n",mUsart2ReceivePack);
}