app_safe.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if(mBackup.RestartCnt > 1){
  22. char buff[40]={0};
  23. memset(buff,0,sizeof(buff));
  24. if(mFlash.isHost){
  25. sprintf(buff,"left shoes restart:%d",mBackup.RestartCnt);
  26. }else{
  27. sprintf(buff,"right shoes restart:%d",mBackup.RestartCnt);
  28. }
  29. if(0 == Except_TxError(EXCEPT_Power,(const char*)buff)){
  30. mBackup.RestartCnt =0;
  31. }
  32. }
  33. }
  34. //恢复出厂设置
  35. static void cb_BLE_RESTSETTING(void* handle)
  36. {
  37. Flash_DeleteAllInfor();
  38. Flash_DeleteAllStep();
  39. Flash_DeleteAllBackup();
  40. BLE_Client_Rx_t* target = handle;
  41. uint8_t cmd = target->pDat[0];
  42. BLE_Host_Tx_Send(0,BLE_RESTSETTING,&cmd,1);
  43. nrf_delay_ms(500);
  44. NVIC_SystemReset();
  45. }
  46. static void app_CONNTED_LED_Process(void){
  47. static uint8_t lastconeted =0;
  48. static uint8_t BlinkTime =0;
  49. static uint8_t BlinkfLAG =0;
  50. uint8_t nowconeted =0;
  51. if(mFlash.isHost)
  52. nowconeted = host_isconnect();
  53. else
  54. nowconeted = slave_isconnect();
  55. // DEBUG_LOG("mFlash.isHost %d,nowconeted:%d,\n",mFlash.isHost,nowconeted);
  56. if(nowconeted != lastconeted){
  57. lastconeted = nowconeted;
  58. if(nowconeted){
  59. BlinkfLAG =1;
  60. BlinkTime =0;
  61. DEBUG_LOG("COLOR_PURPLE\r\n");
  62. // LED_Start(LED_LRCHECK,COLOR_CYAN);
  63. LED_Start(LED_LRCHECK,COLOR_PURPLE);
  64. Process_SetHoldOn(app_CONNTED_LED_Process,1);
  65. }
  66. else{
  67. BlinkfLAG =0;
  68. BlinkTime =0;
  69. Process_SetHoldOn(app_CONNTED_LED_Process,0);
  70. LED_Stop(LED_LRCHECK);
  71. }
  72. }
  73. if(BlinkfLAG){BlinkTime++;
  74. if(BlinkTime <=30){
  75. BlinkfLAG =1;
  76. }
  77. else{
  78. BlinkfLAG =0;
  79. BlinkTime =0;
  80. LED_Stop(LED_LRCHECK);
  81. }
  82. }
  83. }
  84. static uint8_t update_led =0;
  85. static void cb_BLE_LEDON_PURPLE(void* handle)
  86. {
  87. update_led = 1;
  88. }
  89. static void app_BLE_LEDON_PURPLE_process(void){
  90. if(update_led){update_led=0;
  91. LED_Start(LED_ONPURPLE,COLOR_PURPLE);
  92. Process_SetHoldOn(app_BLE_LEDON_PURPLE_process,1);
  93. }
  94. static uint32_t Lastch =0;
  95. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
  96. if(!ch && Lastch != ch){
  97. LED_Stop(LED_ONPURPLE);
  98. Process_SetHoldOn(app_BLE_LEDON_PURPLE_process,0);
  99. }
  100. Lastch = ch;
  101. }
  102. void app_safe_Init(void)
  103. {
  104. mBackup.RestartCnt++;
  105. Process_Start(1000,"app_safe",app_safe_Process);
  106. // Process_Start(100,"app_CONNTED_LED",app_CONNTED_LED_Process);
  107. BLE_Client_Rx_Regist(BLE_RESTSETTING,cb_BLE_RESTSETTING);
  108. //常亮紫色灯
  109. BLE_Client_Rx_Regist(BLE_LEDON_PURPLE,cb_BLE_LEDON_PURPLE);
  110. Process_Start(100,"app_BLE_LEDON_PURPLE",app_BLE_LEDON_PURPLE_process);
  111. }