our_service.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include "nrf_gpio.h"
  43. #include "our_service.h"
  44. #include "ble_srv_common.h"
  45. #include "app_error.h"
  46. #include "SEGGER_RTT.h"
  47. #include "board_gpi.h"
  48. #include "nrf_log.h"
  49. char SvrRxBuffer[100];
  50. int SvrRxBufferPos;
  51. char SvrEvent;
  52. stSystemTimer SystemTimer;
  53. stSystemManager SystemManager;
  54. void USART_DataCheck()
  55. {
  56. if( SystemTimer.UART_LAST_RECV_TIMER > 10 && SystemManager.RxValild )
  57. {
  58. SvrRxBuffer[SvrRxBufferPos] = 0;
  59. //printf("[%d]%s\r\n", SvrRxBufferPos, SvrRxBuffer);
  60. SystemManager.RxValild = 0;
  61. if( SvrRxBufferPos == 20 )
  62. {
  63. SystemManager.RxEvent = 1;
  64. }
  65. SvrRxBufferPos = 0;
  66. }
  67. if( SystemTimer.UART_LAST_RECV_TIMER > 10 && SvrRxBufferPos )
  68. {
  69. SystemManager.RxValild = 0;
  70. SvrRxBufferPos = 0;
  71. }
  72. }
  73. void USART_Handler(void)
  74. {
  75. char ch;
  76. uint8_t cr;
  77. //SystemTimer.UART1_LAST_RECV_TIMER = 0;
  78. if( app_uart_get(&cr) == NRF_SUCCESS )
  79. {
  80. SystemManager.RxValild = 1;
  81. SvrRxBuffer[SvrRxBufferPos++] = cr;
  82. SvrRxBufferPos %= 100;
  83. SystemTimer.UART_LAST_RECV_TIMER = 0;
  84. }
  85. }
  86. #define COM_TYPE_SVR_NORMAL 0x02
  87. #define COM_TYPE_SVR_TALKING 0x03
  88. #define COM_TYPE_SVR_DISCON 0x04
  89. #define COM_TYPE_SVR_BUSY 0x05
  90. #define COM_TYPE_SVR_ALIVE 0x06
  91. #define COM_TYPE_SVR_MIC 0x07
  92. void ParseEventServer()
  93. {
  94. int i;
  95. if( SystemManager.RxEvent == 0 )
  96. return;
  97. for( i=0; i<20; i++)
  98. {
  99. //printf("0x%02x ", SvrRxBuffer[i]);
  100. }
  101. SystemManager.RxEvent = 0;
  102. if(SvrRxBuffer[0] != 0x02)
  103. return;
  104. if( SvrRxBuffer[1] == COM_TYPE_SVR )
  105. {
  106. if( SvrRxBuffer[3] == COM_TYPE_SVR_NORMAL )
  107. {
  108. NRF_LOG_INFO("Recv : Normal");
  109. PKLedOff();
  110. PKBuzzerOff();
  111. PKMicOff();
  112. SystemManager.SwitchOn = 0;
  113. }else if( SvrRxBuffer[3] == COM_TYPE_SVR_TALKING )
  114. {
  115. NRF_LOG_INFO("Recv : Talking");
  116. PKLedOn();
  117. PKBuzzerOff();
  118. SystemManager.SwitchOn = 0;
  119. }else if( SvrRxBuffer[3] == COM_TYPE_SVR_DISCON )
  120. {
  121. NRF_LOG_INFO("Recv : Disconnect");
  122. SystemManager.SwitchOn = 0;
  123. //bsp_board_leds_off();
  124. }else if( SvrRxBuffer[3] == COM_TYPE_SVR_BUSY )
  125. {
  126. NRF_LOG_INFO("Recv : Busy");
  127. }else if( SvrRxBuffer[3] == COM_TYPE_SVR_ALIVE )
  128. {
  129. NRF_LOG_INFO("Recv : Keep Alive");
  130. }else if( SvrRxBuffer[3] == COM_TYPE_SVR_MIC )
  131. {
  132. NRF_LOG_INFO("Recv : mic on/off");
  133. if( SvrRxBuffer[11] == 0 )
  134. {
  135. NRF_LOG_INFO("mic off");
  136. NRF_LOG_INFO("speaker off");
  137. PKSpeakerOff();
  138. PKMicOn();
  139. PKBuzzerOff();
  140. PKLedOn();
  141. SystemManager.SwitchOn = 0;
  142. }else{
  143. NRF_LOG_INFO("mic on");
  144. NRF_LOG_INFO("speaker on");
  145. PKMicOff();
  146. PKSpeakerOn();
  147. PKLedOn();
  148. }
  149. }
  150. }else if( SvrRxBuffer[1] == COM_TYPE_BAND )
  151. {
  152. }
  153. }
  154. void SendToEventServer(char* data, int len)
  155. {
  156. int i;
  157. for( i=0; i<len; i++)
  158. {
  159. while (app_uart_put(data[i]) != NRF_SUCCESS);
  160. }
  161. }
  162. char SvrSendBuff[50];
  163. void SendEmergency()
  164. {
  165. SvrSendBuff[0] = COM_STX;
  166. SvrSendBuff[1] = COM_TYPE_SVR;
  167. SvrSendBuff[2] = 0;
  168. SvrSendBuff[3] = COM_TYPE_BELL_EMG;
  169. SvrSendBuff[4] = 0xaa;
  170. SvrSendBuff[5] = 0xaa;
  171. SvrSendBuff[6] = 0xaa;
  172. SvrSendBuff[7] = 0xbb;
  173. SvrSendBuff[8] = 0xbb;
  174. SvrSendBuff[9] = 0xbb;
  175. SvrSendBuff[10] = 8;
  176. SvrSendBuff[11] = 0;
  177. SvrSendBuff[12] = 0;
  178. SvrSendBuff[13] = 0;
  179. SvrSendBuff[14] = 0;
  180. SvrSendBuff[15] = 0;
  181. SvrSendBuff[16] = 0;
  182. SvrSendBuff[17] = 0;
  183. SvrSendBuff[18] = 0;
  184. SvrSendBuff[19] = COM_ETX;
  185. NRF_LOG_INFO("SendEmergency");
  186. SendToEventServer(&SvrSendBuff[1], 19);
  187. SystemManager.SwitchOn = 1;
  188. PKBuzzerOn();
  189. NRF_LOG_INFO("SendEmergency");
  190. }
  191. void SendEmergencyLocal()
  192. {
  193. SvrSendBuff[0] = COM_STX;
  194. SvrSendBuff[1] = COM_TYPE_SVR;
  195. SvrSendBuff[2] = 0;
  196. SvrSendBuff[3] = COM_TYPE_BELL_EMG;
  197. SvrSendBuff[4] = 0xaa;
  198. SvrSendBuff[5] = 0xaa;
  199. SvrSendBuff[6] = 0xaa;
  200. SvrSendBuff[7] = 0xbb;
  201. SvrSendBuff[8] = 0xbb;
  202. SvrSendBuff[9] = 0xbb;
  203. SvrSendBuff[10] = 8;
  204. SvrSendBuff[11] = 0;
  205. SvrSendBuff[12] = 0;
  206. SvrSendBuff[13] = 0;
  207. SvrSendBuff[14] = 0;
  208. SvrSendBuff[15] = 0;
  209. SvrSendBuff[16] = 0;
  210. SvrSendBuff[17] = 0;
  211. SvrSendBuff[18] = 0;
  212. SvrSendBuff[19] = COM_ETX;
  213. SendToEventServer(&SvrSendBuff[0], 20);
  214. SystemManager.SwitchOn = 1;
  215. PKBuzzerOn();
  216. }
  217. void LedToggle()
  218. {
  219. static char toggle = 0;
  220. if( SystemManager.SwitchOn == 0 )
  221. return;
  222. if( SystemTimer.LED_TOGGLE_TIMER < 500 )
  223. return;
  224. SystemTimer.LED_TOGGLE_TIMER = 0;
  225. if( toggle++&0x01)
  226. {
  227. PKLedOn();
  228. }else{
  229. PKLedOff();
  230. }
  231. }