app_safe.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*********************************************************************
  2. * INCLUDES
  3. */
  4. #include "ble_comm.h"
  5. #include "app_flash.h"
  6. #include "app_safe.h"
  7. #include "hal_ble_client.h"
  8. #include "hal_ble_host.h"
  9. #include "hal_led.h"
  10. #include "drv_trigger.h"
  11. static volatile uint8_t restflag =0;
  12. void app_SetRestsettting(void){
  13. restflag =1;
  14. }
  15. static void app_safe_Process(void)
  16. {
  17. static uint8_t cnt =0;
  18. if(mBackup.ErrStartCnt >0){
  19. cnt++;
  20. if(cnt >=60){//一分钟后清空标志位
  21. mBackup.ErrStartCnt =0;
  22. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save ErrStartCnt fail");
  23. DEBUG_LOG("clear mBackup.ErrorStartFlag\n");
  24. }
  25. }
  26. if(mBackup.RestartCnt > 1){
  27. char buff[40]={0};
  28. memset(buff,0,sizeof(buff));
  29. if(mFlash.isHost){
  30. sprintf(buff,"left shoes restart:%d",mBackup.RestartCnt);
  31. }else{
  32. sprintf(buff,"right shoes restart:%d",mBackup.RestartCnt);
  33. }
  34. if(0 == Except_TxError(EXCEPT_Power,(const char*)buff)){
  35. mBackup.RestartCnt =0;
  36. }
  37. }
  38. if(restflag){restflag =0;
  39. Flash_DeleteAllInfor();
  40. Flash_DeleteAllStep();
  41. Flash_DeleteAllBackup();
  42. nrf_delay_ms(500);
  43. NVIC_SystemReset();
  44. }
  45. }
  46. //恢复出厂设置
  47. static void cb_BLE_RESTSETTING(void* handle)
  48. {
  49. Flash_DeleteAllInfor();
  50. Flash_DeleteAllStep();
  51. Flash_DeleteAllBackup();
  52. BLE_Client_Rx_t* target = handle;
  53. uint8_t cmd = target->pDat[0];
  54. BLE_Host_Tx_Send(0,BLE_RESTSETTING,&cmd,1);
  55. nrf_delay_ms(500);
  56. NVIC_SystemReset();
  57. }
  58. static void app_CONNTED_LED_Process(void){
  59. static uint8_t lastconeted =0;
  60. static uint8_t BlinkTime =0;
  61. static uint8_t BlinkfLAG =0;
  62. uint8_t nowconeted =0;
  63. if(mFlash.isHost)
  64. nowconeted = host_isconnect();
  65. else
  66. nowconeted = slave_isconnect();
  67. // DEBUG_LOG("mFlash.isHost %d,nowconeted:%d,\n",mFlash.isHost,nowconeted);
  68. if(nowconeted != lastconeted){
  69. lastconeted = nowconeted;
  70. if(nowconeted){
  71. BlinkfLAG =1;
  72. BlinkTime =0;
  73. DEBUG_LOG("COLOR_PURPLE\r\n");
  74. // LED_Start(LED_LRCHECK,COLOR_CYAN);
  75. LED_Start(LED_LRCHECK,COLOR_CYAN);
  76. Process_SetHoldOn(app_CONNTED_LED_Process,1);
  77. }
  78. else{
  79. BlinkfLAG =0;
  80. BlinkTime =0;
  81. Process_SetHoldOn(app_CONNTED_LED_Process,0);
  82. LED_Stop(LED_LRCHECK);
  83. }
  84. }
  85. if(BlinkfLAG){BlinkTime++;
  86. if(BlinkTime <=30){
  87. BlinkfLAG =1;
  88. }
  89. else{
  90. BlinkfLAG =0;
  91. BlinkTime =0;
  92. LED_Stop(LED_LRCHECK);
  93. }
  94. }
  95. }
  96. //static uint8_t update_led =0;
  97. //static void cb_BLE_LEDON_PURPLE(void* handle)
  98. //{
  99. // update_led = 1;
  100. //}
  101. void app_BLE_LEDON_PURPLE_process(void){
  102. static uint32_t Lastch =0;
  103. uint32_t ch = drv_GetChargeTrig();
  104. if(!ch && Lastch != ch){
  105. LED_Stop(LED_ONPURPLE);
  106. LED_Stop(LED_LR_LED);
  107. Process_SetHoldOn(app_BLE_LEDON_PURPLE_process,0);
  108. }
  109. Lastch = ch;
  110. }
  111. void app_safe_Init(void)
  112. {
  113. mBackup.RestartCnt++;
  114. Process_Start(1000,"app_safe",app_safe_Process);
  115. // Process_Start(100,"app_CONNTED_LED",app_CONNTED_LED_Process);
  116. BLE_Client_Rx_Regist(BLE_RESTSETTING,cb_BLE_RESTSETTING);
  117. //常亮紫色灯
  118. // BLE_Client_Rx_Regist(BLE_LEDON_PURPLE,cb_BLE_LEDON_PURPLE);
  119. // SEGGER_RTT_printf(0,"LastsotfVersion:%d,updateFlag:%d,nowsoftv:%d\r\n",mFlash.mClient.LastsotfVersion,mFlash.mClient.updateFlag,SOFTWARE_VERSION);
  120. Process_Start(100,"app_BLE_LEDON_PURPLE",app_BLE_LEDON_PURPLE_process);
  121. if(0 == mFlash.mClient.LastsotfVersion ||
  122. (SOFTWARE_VERSION != mFlash.mClient.LastsotfVersion && 0xA5 == mFlash.mClient.updateFlag)){
  123. // if(0xA5 == mFlash.mClient.updateFlag){
  124. LED_Start(LED_ONPURPLE,COLOR_PURPLE);
  125. Process_SetHoldOn(app_BLE_LEDON_PURPLE_process,1);
  126. mFlash.mClient.LastsotfVersion = SOFTWARE_VERSION;
  127. mFlash.mClient.updateFlag = 0;
  128. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save information fail");
  129. }
  130. }