chen
2024-09-20 292ed46c6066d47289f1330b1c2bcc6d74761f95
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*******************************************************************************
 * File Name         : APL.c
 * Description       :
 * Created on        : 2018Äê5ÔÂ8ÈÕ
 * Author            : ¶Å¼ü
 *******************************************************************************/
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "AppConfig.h"
#include "string.h"
#include "stm32f4xx_hal.h"
 
#include "Uart.h"
#include "Shell.h"
#include "DBG.h"
#include "Internet.h"
#include "HIDO_Input.h"
#include "HIDO_Timer.h"
#include "HIDO_ATLite.h"
#include "HIDO_Debug.h"
#include "HIDO_Json.h"
#include "Uart.h"
#include "Led.h"
#include "Reboot.h"
#include "Delay.h"
#include "GPS.h"
#include "qxwz_app.h"
#include "UDPClient.h"
#include "APL.h"
 
#include "mainex.h"
#include "lis3dh_driver.h"
#include "dw_driver.h"
#include "deca_device_api.h"
#include "global_param.h"
#include "TrackingDiff.h"
#include "dw_app.h"
#include "lis3dh_driver.h"
#include "Beep.h"
#include "mainex.h"
#include "Delay.h"
#include "Module.h"
#include "OTA.h"
#include "ADC.h"
#include "ADC_hal.h"
#include "OTA.h"
#include "Log.h"
#include "HTTPClient.h"
#include "SPIFlash.h"
#include "Power.h"
#include "TCPClient.h"
#include "RTKClient.h"
#include "NTRIPClient.h"
#include "NTRIPApp.h"
 
#include "TTS.h"
 
 
/*******************************************************************************
 *                                  Macro                                      *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Local Variable                                  *
 *******************************************************************************/
 
/*******************************************************************************
 *                        Local Function Declaration                           *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Local Function                                  *
 *******************************************************************************/
 
/*******************************************************************************
 *                             Global Function                                 *
 *******************************************************************************/
 
/*******************************************************************************
 * Function Name     : 
 * Description       : 
 * Input             : 
 * Output            : 
 * Return            : 
 * Author            : ¶Å¼ü
 * Modified Date:    : 2018Äê5ÔÂ8ÈÕ
 *******************************************************************************/
static HIDO_UINT32 l_u32TimerID = 0;
extern u8 bat_percent2,chargedbg_flag;
extern TIM_HandleTypeDef htim1;
extern uint16_t anchordata_id[TAG_NUM_IN_SYS],anchordata_dist[TAG_NUM_IN_SYS];
extern uint8_t anchordata_bat[TAG_NUM_IN_SYS];
extern uint8_t anchordata_num;
uint16_t beepontime,beep_start;
extern float nomove_count;
 u8 stationary_flag;
void message_construct_for_4G()
{
 
 
}
#ifdef __APP_CODE__
void IMUTask(void)
{   
if (nomove_count > g_com_map[STATIONARY_TIME])
    {
        stationary_flag = 1;
    }
    else
    {
        stationary_flag = 0;
    }
   // g_com_map[NOMOVESLEEP_TIME] = 5;
  if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&g_com_map[IMU_ENABLE]==1)
    {
      
     // CloseRTK4G();  
        mcu_sleep();
    }
}
u8 motor_flag;
void BepperTask(void)
{
    if(beepontime>0)
    {beep_start=1;}
    if(beep_start)
    {
        if(beepontime>0)
        {
            beepontime--;
            motor_flag = 1;
            HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
        }else{
            motor_flag = 0;
           HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4); 
        }
    }
}
uint16_t uwb_losttimer = 0;
HIDO_VOID OneSecond_TimerCallback(HIDO_VOID *_pArg)
{
    BepperTask();
    IMUTask();
    uwb_losttimer++;
        message_construct_for_4G();
}
#endif
HIDO_VOID APL_Init(void)
{
#ifdef __APP_CODE__
    SPIFlash_Init(SPI_FLASH_ID_0);
    parameter_init();
    Power_Init();
    DBG_Init();
    Shell_Init();
    ADC_Init();
    Accelerometer_Init();
    Internet_Init();
if(g_com_map[RTCMMODE_INDEX] == RTCMMODE_TCP)
    {
     TCPClient_Init();
    }else if(g_com_map[RTCMMODE_INDEX] == RTCMMODE_NTRIP)
    {
//    qxwz_app_init();
        NTRIPClient_Init();
        NTRIPApp_Init();
    }
#ifdef __USE_QXWZ__
    /* Ê¹ÓÃǧѰλÖà*/
    qxwz_app_init();
#endif
 
    GPS_Init();
    UDPClient_Init();
#ifdef __USE_TCP_RTCM__
    RTKClient_Init();
#endif
#ifdef __USE_UWB__
    uwb_app_init();
#endif
  HIDO_TimerCreate(&l_u32TimerID);
  HIDO_TimerStart(l_u32TimerID, HIDO_TIMER_TYPE_LOOP, HIDO_TIMER_TICK_MS(1000), OneSecond_TimerCallback, HIDO_NULL);
#endif
 
 
 
#ifdef __OTA_CODE__
    SPIFlash_Init(SPI_FLASH_ID_0);
    Power_Init();
    DBG_Init();
#ifdef __USE_QXWZ__
    HIDO_Debug("\r\nstarting ota application\r\n");
#endif
    Internet_Init();
    HTTPClient_Init();
    OTA_Init();
#endif
}
 
#ifdef __APP_CODE__
 
void LED4Gtask(void)
{
    
    
    if(Internet_IsIPReady() == HIDO_TRUE)
    {    
         led4g = GREEN;   
    }else{  
        if(rtk_state)
        {  
            led4g = RED;
        }else{
            led4g = LEDOFF;
        }
        
    }
 
}
#define BATTARY_DEBUG
u16 chongman_time;
u8 only_one_flag;
u8 power_state = 0,chargeon = 0;
u32 adctick = 0;
int LowPower_EN = 0;
extern HIDO_FLOAT fVoltage1;
void PowerLedTask(void)
     static u8 powerled_state=0;
    if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
    {
        chongman_time=0;
        only_one_flag=0;
        if(!power_state&&!chargedbg_flag)
        {
            CloseRTK4G();  
            DBG_SetMode(DBG_MODE_CHARGE);
            power_state = 1;
        }
       while(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin)&&DBG_GetMode() == DBG_MODE_CHARGE)
     {
        if(HIDO_TimerGetTick()-adctick>6000)  //6s²ÉÑùÒ»´Î µçÁ¿
        {   
            adctick = HIDO_TimerGetTick();
            chongman_time=chongman_time+6;
            #ifdef BATTARY_DEBUG
                //printf("µçѹ: %f V,µçÁ¿ %d,±ê־λ%d,ʱ¼ä %d\r\n",fVoltage1,bat_percent2,only_one_flag,chongman_time);
            #endif
            if( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10))
            {
                bat_percent2=Power_GetBatteryPercentage();
 
            }else{
                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET);
                bat_percent2=Power_GetBatteryPercentage();
                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
            }
        }
        if(fVoltage1>=4.15&&!only_one_flag)
        {
        only_one_flag=1;
        chongman_time=0;
        }
        if(fVoltage1>=4.15&&chongman_time>=1800)
            {
                powerled = GREEN;
            }else{
                powerled = RED;
            }
         delay_ms(100);
        DBG_Poll();
        Internet_Poll();
        Set4LEDColor(0,0,0,powerled);
     }
    }
 
            chargeon = 0;
           if(power_state)
            {              
                DBG_SetMode(DBG_MODE_SHELL);
                power_state = 0;
                OpenRTK4G();      
                if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
                {   
                    delay_ms(100);                    
                    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); 
                    delay_ms(100);
                }
            }
            if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
            {
             chargedbg_flag = 0;
            
            }
            if(HIDO_TimerGetTick()%1000<10 )
        {
            if(powerled_state==0)
            {
                if(bat_percent2>15)
                {
                    powerled = BLUE;
                }else{
                     powerled = RED;
                }
                powerled_state = 1;
              //  bat_percent = Battery_GetPercentage();
              Set4LEDColor(uwbled,rtkled,led4g,powerled);   
            }
        }else{
            if(powerled_state)
            {
                powerled_state = 0;
                powerled = LEDOFF;
                Set4LEDColor(uwbled,rtkled,led4g,powerled);
            }
 
        } 
         
        if(HIDO_TimerGetTick()-adctick>5000)  //10·ÖÖÓ²ÉÑùÒ»´Î µçÁ¿
        {
            adctick = HIDO_TimerGetTick();
            #ifdef BATTARY_DEBUG
                //printf("µçѹ: %f V,µçÁ¿ %d,±ê־λ%d,ʱ¼ä %d\r\n",fVoltage1,bat_percent2,only_one_flag,chongman_time);
            #endif
            if( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10))
            {
                bat_percent2=Power_GetBatteryPercentage();
 
            }else{
                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_SET);
                bat_percent2=Power_GetBatteryPercentage();
                HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10, GPIO_PIN_RESET);
            }
        }
}
//void PowerDownDetect(void)
//{  
// static HIDO_UINT32 l_u32QXpowerTick = 0;    
//static uint32_t powercount=0;
//   
//    if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12))
//      {
//           if((HIDO_TimerGetTick() - l_u32QXpowerTick) >=1000)
//          {
//              while(1)
//              {
//                  if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)==0)
//                  {     
//                      powercount = 0;
//                      l_u32QXpowerTick = HIDO_TimerGetTick();
//                      while(1)
//                      {
//                          
//                      if((HIDO_TimerGetTick() - l_u32QXpowerTick) >=100)
//                        {
//                            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
//                        }
//                    }
//                  }
//              }
//          }
//      }else{
//           l_u32QXpowerTick = HIDO_TimerGetTick();
//      }
//}
 
void PowerDownDetect(void)
{  
 static HIDO_UINT32 l_u32QXpowerTick = 0;    
static uint32_t powercount=0;
   
    if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12))
      {
          delay_ms(1000);
          if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12))
          {     
              while(1)
              {
                if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)==0) 
                {
                    delay_ms(100);
                    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
                }
            }
          }
      }
}
extern u8 bat_percent2;
u16 basesystimer=0,g_Resttimer= 0;
 u8 g_start_sync_flag;
 void HAL_SYSTICK_Callback(void)
 {
 
 }
u32 keystarttime,keystarttime2;
extern u8 heartbeasend_flag,userkey_state4g;
 u8 userkey_state;
void UserKeyTask(void)
 {
     if(!GET_USERKEY)
     {
        if(HIDO_TimerGetTick() - keystarttime>3000)
        {
            beepontime = 2;
            userkey_state4g = 1;
            userkey_state = 1;
            heartbeasend_flag = 1;
            keystarttime =  HIDO_TimerGetTick();
        }
        if(HIDO_TimerGetTick() - keystarttime2>10000)
        {
          //  g_com_map[CNT_RESTART] = 1;
        }
     }else
     {
       keystarttime =  HIDO_TimerGetTick(); 
       keystarttime2 =  HIDO_TimerGetTick(); 
     }
 } 
#endif
void APLPollTask(void)
{
#ifdef __APP_CODE__
    g_Resttimer=0;
    DBG_Poll();
#ifdef _RTK_MODE_
    Internet_Poll();
    HIDO_TimerPoll();
    HIDO_ATLitePoll();
    LED4Gtask();
    GPS_Poll();
    UDPClient_Poll();
#ifdef __USE_TCP_RTCM__
    RTKClient_Poll();
#endif
#endif
#ifdef GAOJINGDU    
#ifdef TCP_RTCM
    if(g_com_map[RTCMMODE_INDEX] == RTCMMODE_TCP)
      TCPClient_Poll();
#endif
#endif
        PowerLedTask();
    PowerDownDetect();
    //uwbpolltask();
    UserKeyTask();
#endif
    
#ifdef __OTA_CODE__
    Internet_Poll();
    HIDO_TimerPoll();
    HIDO_ATLitePoll();
    HTTPClient_Poll();
    OTA_Poll();
#endif
    
 
   //     CloseRTK4G();
 
   
 
}
uint8_t udp_uwbenable = 1,uwb_state;
void APL_Poll(void)
{
    APLPollTask();
 
    if(uwb_state!=udp_uwbenable)
    {
        uwb_state = udp_uwbenable;
        if(uwb_state)
        {
            UWBSPIWakeup();
        }else{
            uwbled = LEDOFF;     
            dwt_entersleep();            
        }
    }
//    printf("UWB״̬:%d",uwb_state);
    if(uwb_state)
       uwb_app_poll();
     IdleTask();
}