app_safe.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. static void app_safe_Process(void)
  11. {
  12. static uint8_t cnt =0;
  13. if(mBackup.ErrStartCnt >0){
  14. cnt++;
  15. if(cnt >=60){//一分钟后清空标志位
  16. mBackup.ErrStartCnt =0;
  17. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save ErrStartCnt fail");
  18. DEBUG_LOG("clear mBackup.ErrorStartFlag\n");
  19. }
  20. }
  21. // //测试使用
  22. // static uint8_t temp =0;
  23. // static uint8_t flag =0;
  24. // if(mBackup.RestartCnt > 1 && flag ==0){flag =1;}
  25. //
  26. // if(1 == flag){
  27. // if(temp ==0){temp++;
  28. // Process_SetHoldOn(app_safe_Process,1);
  29. // LED_Start(LED_RESTART,COLOR_PURPLE);
  30. // }
  31. // else if(temp <200){
  32. // temp++;
  33. // if(temp >20){temp =220;
  34. // Process_SetHoldOn(app_safe_Process,0);
  35. // LED_Stop(LED_RESTART);
  36. // flag =2;
  37. // }
  38. // }
  39. // }
  40. static uint8_t lastconeted =0;
  41. static uint8_t BlinkTime =0;
  42. static uint8_t BlinkLed =0;
  43. uint8_t nowconeted =0;
  44. if(mFlash.isHost)
  45. nowconeted = host_isconnect();
  46. else
  47. nowconeted = slave_isconnect();
  48. if(nowconeted != lastconeted){
  49. lastconeted = nowconeted;
  50. if(lastconeted){
  51. BlinkLed =0;
  52. BlinkTime =0;
  53. }
  54. else{
  55. BlinkLed =0;
  56. LED_Stop(LED_LRCHECK);
  57. }
  58. }
  59. if(lastconeted){
  60. if(BlinkTime <=10){
  61. BlinkTime++;
  62. if(BlinkLed){BlinkLed=0;
  63. LED_Start(LED_LRCHECK,COLOR_BLACK);
  64. }
  65. else {BlinkLed=1;
  66. LED_Start(LED_LRCHECK,COLOR_CYAN);
  67. }
  68. }
  69. else LED_Stop(LED_LRCHECK);
  70. }
  71. if(mBackup.RestartCnt > 1){
  72. char buff[40]={0};
  73. memset(buff,0,sizeof(buff));
  74. if(mFlash.isHost){
  75. sprintf(buff,"left shoes restart:%d",mBackup.RestartCnt);
  76. }else{
  77. sprintf(buff,"right shoes restart:%d",mBackup.RestartCnt);
  78. }
  79. if(0 == Except_TxError(EXCEPT_Power,(const char*)buff)){
  80. mBackup.RestartCnt =0;
  81. }
  82. }
  83. }
  84. //恢复出厂设置
  85. static void cb_BLE_RESTSETTING(void* handle)
  86. {
  87. Flash_DeleteAllInfor();
  88. Flash_DeleteAllStep();
  89. Flash_DeleteAllBackup();
  90. BLE_Client_Rx_t* target = handle;
  91. uint8_t cmd = target->pDat[0];
  92. BLE_Host_Tx_Send(0,BLE_RESTSETTING,&cmd,1);
  93. nrf_delay_ms(500);
  94. NVIC_SystemReset();
  95. }
  96. void app_safe_Init(void)
  97. {
  98. mBackup.RestartCnt++;
  99. Process_Start(1000,"app_safe",app_safe_Process);
  100. BLE_Client_Rx_Regist(BLE_RESTSETTING,cb_BLE_RESTSETTING);
  101. }