dio(6619).c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. ******************************************************************************
  3. * @file dio.c
  4. * @author hskim
  5. * @version v0.7
  6. * @date 2019-06-07
  7. * @brief This file provides Digital Input and Output's fuction.
  8. ******************************************************************************
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include "pca10040.h"
  15. #include "dio.h"
  16. #include "nrf_log.h"
  17. #include "nrf_log_ctrl.h"
  18. #include "nrf_log_default_backends.h"
  19. #include "sysmgr.h"
  20. #include "main.h"
  21. uint8_t BT5ButtonList[2] = BUTTONS_LIST;
  22. stHSDOut HSDOut[3];
  23. stHSDIOTask HSDIOTask;
  24. uint32_t TimeSlice[10];
  25. static int TS = 0;
  26. stHSDIn DIn[2];
  27. void DioTask()
  28. {
  29. DITask();
  30. }
  31. int DITask()
  32. {
  33. int i;
  34. uint16_t readDI;
  35. if( HSDIOTask.DIRunFlag < HSDIOTask.DIRunPeriodTime )
  36. return 0;
  37. HSDIOTask.DIRunFlag = 0;
  38. for( i=0; i<1; i++)
  39. {
  40. //DInput[i].in = (uint8_t)HAL_GPIO_ReadPin(DIPinMap[i].Port, DIPinMap[i].Pin);
  41. //if( DIPinMap[i].HighActive == LowActive )
  42. // DInput[i].in = ~DInput[i].in&0x01;
  43. }
  44. return 0;
  45. }
  46. int DOTask()
  47. {
  48. #if 0
  49. int i;
  50. int DoIndex;
  51. if( !HSDIOTask.RunFlag )
  52. return 0;
  53. HSDIOTask.RunFlag = 0;
  54. HSDOutProcess(DOut, DOPinMap, 21 );
  55. #endif
  56. return 0;
  57. }
  58. void HSDInInit()
  59. {
  60. uint32_t i;
  61. for (i = 0; i < BUTTONS_NUMBER; ++i)
  62. {
  63. nrf_gpio_cfg_input(BT5ButtonList[i], BUTTON_PULL);
  64. }
  65. HSDIOTask.DIRunPeriodTime = 25;
  66. HSDIOTask.DISecCount =1000/HSDIOTask.DIRunPeriodTime;
  67. DIn[0].Pressed = Key_0_Pressed;
  68. DIn[0].Released = Key_0_Released;
  69. DIn[1].Pressed = Key_1_Pressed;
  70. DIn[1].Released = Key_1_Released;
  71. DIn[0].longPressTimeOne = 5; //jakuja
  72. DIn[0].LongPressedOne = DevRegLongPressedOne;
  73. #if 0
  74. DIn[2].Pressed = Key_2_Pressed;
  75. DIn[2].Released = Key_2_Released;
  76. DIn[3].Pressed = Key_3_Pressed;
  77. DIn[3].Released = Key_3_Released;
  78. DIn[4].Pressed = Key_4_Pressed;
  79. DIn[4].Released = Key_4_Released;
  80. #endif
  81. }
  82. void HSDInRun()
  83. {
  84. int Len;
  85. int DiIndex;
  86. for( DiIndex=0; DiIndex<BUTTONS_NUMBER; DiIndex++)
  87. {
  88. if( nrf_gpio_pin_read(BT5ButtonList[DiIndex]) == false) // BUTTON PUSH
  89. {
  90. DIn[DiIndex].in = 1;
  91. if( DIn[DiIndex].softRelease == 1 )
  92. {
  93. return;
  94. }
  95. if( DIn[DiIndex].onFlag == 0 )
  96. {
  97. DIn[DiIndex].pressCount++;
  98. DIn[DiIndex].Pressed(&DIn[DiIndex]);
  99. NRF_LOG_INFO("==> Key_%d Pressed", BT5ButtonList[DiIndex]);
  100. }else{
  101. if( DIn[DiIndex].longPressTimeKeep != 0 )
  102. {
  103. if( DIn[DiIndex].pressedSec >= DIn[DiIndex].longPressTimeKeep )
  104. {
  105. DIn[DiIndex].LongPressedKeep(&DIn[DiIndex]);
  106. }
  107. }
  108. if( DIn[1].pressTime == 20 ) //pressTime 25ms Tick
  109. {
  110. if( DIn[0].onFlag == 0 )
  111. TriggerEmg();
  112. else{
  113. SystemTimer.SEND_TIMER = 0;
  114. SystemManager.FWUpdate = 1;
  115. Buzzer_Select(BUZZER_BOOT_LOADER);
  116. }
  117. }
  118. if( DIn[DiIndex].pressTime % HSDIOTask.DISecCount == 0 ) // count up 0.5 sec
  119. {
  120. DIn[DiIndex].pressedSec++;
  121. if( DIn[0].onFlag == 1 && DIn[1].pressedSec == 2 )
  122. {
  123. //SystemTimer.SEND_TIMER = 0;
  124. //SystemManager.FWUpdate = 1;
  125. //Buzzer_Select(4);
  126. }
  127. if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <= 5 ) //pressTime 25ms Tick
  128. {
  129. SystemTimer.SEND_TIMER = 0;
  130. }
  131. if( DIn[0].pressedSec == 5 )
  132. {
  133. TriggerRegDev();
  134. }
  135. if( DIn[DiIndex].longPressTimeOne != 0 )
  136. {
  137. if( DIn[DiIndex].pressedSec >= DIn[DiIndex].longPressTimeOne )
  138. {
  139. DIn[DiIndex].longPressCount++;
  140. DIn[DiIndex].softRelease = 1;
  141. DIn[DiIndex].LongPressedOne(&DIn[DiIndex]);
  142. NRF_LOG_INFO("==> Key_%d LongPressedOne", BT5ButtonList[DiIndex]);
  143. }
  144. }
  145. }
  146. }
  147. DIn[DiIndex].onFlag = 1;
  148. DIn[DiIndex].pressTime++;
  149. }else{
  150. if( DIn[DiIndex].onFlag == 1 )
  151. {
  152. if( DiIndex == 0 ) // TEST BUTTON
  153. {
  154. //if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <5 )
  155. if( DIn[0].pressTime >= 2 && DIn[0].pressTime < 100 ){
  156. if( SystemManager.FWUpdate == 0 )
  157. TriggerLineTest();
  158. }else{
  159. NRF_LOG_INFO("$$$ Key_%d Released", BT5ButtonList[DiIndex]);
  160. DIn[DiIndex].in = 0;
  161. DIn[DiIndex].pressTime = 0;
  162. DIn[DiIndex].onFlag = 0;
  163. DIn[DiIndex].pressedSec = 0;
  164. DIn[DiIndex].softRelease = 0;
  165. DIn[DiIndex].prevTime = 0;
  166. }
  167. }
  168. if( DiIndex == 1 ) // EMG BUTTON
  169. {
  170. //if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <5 )
  171. if( DIn[1].pressTime < 25 ){
  172. NRF_LOG_INFO("$$$ Key_%d Released", BT5ButtonList[DiIndex]);
  173. DIn[DiIndex].in = 0;
  174. DIn[DiIndex].pressTime = 0;
  175. DIn[DiIndex].onFlag = 0;
  176. DIn[DiIndex].pressedSec = 0;
  177. DIn[DiIndex].softRelease = 0;
  178. DIn[DiIndex].prevTime = 0;
  179. }
  180. }
  181. DIn[DiIndex].Released(&DIn[DiIndex]);
  182. NRF_LOG_INFO("==> Key_%d Released", BT5ButtonList[DiIndex]);
  183. }
  184. DIn[DiIndex].in = 0;
  185. DIn[DiIndex].pressTime = 0;
  186. DIn[DiIndex].onFlag = 0;
  187. DIn[DiIndex].pressedSec = 0;
  188. DIn[DiIndex].softRelease = 0;
  189. DIn[DiIndex].prevTime = 0;
  190. }
  191. }
  192. }
  193. void TriggerEmg()
  194. {
  195. NRF_LOG_INFO("@@@@@@@ EMG Pressed");
  196. SystemManager.EmgSendButton = BUTTON_EMG;
  197. SystemTimer.EMG_SEND_RUN = 1;
  198. SystemTimer.SEND_TIMER = 0;
  199. if(!advertising_flag)
  200. advertising_start();
  201. Buzzer_Select(BUZZER_EMG);
  202. }
  203. void TriggerLineTest()
  204. {
  205. NRF_LOG_INFO("@@@@@@@ Test Pressed");
  206. SystemManager.EmgSendButton = BUTTON_TEST;
  207. SystemTimer.EMG_SEND_RUN = 1;
  208. SystemTimer.SEND_TIMER = 0;
  209. if(!advertising_flag)
  210. advertising_start();
  211. Buzzer_Select(BUZZER_TEST);
  212. }
  213. void TriggerRegDev()
  214. {
  215. NRF_LOG_INFO("@@@@@@@ DevReg Pressed");
  216. SystemManager.EmgSendButton = BUTTON_REG;
  217. SystemTimer.EMG_SEND_RUN = 1;
  218. SystemTimer.SEND_TIMER = 0;
  219. if(!advertising_flag)
  220. advertising_start();
  221. Buzzer_Select(BUZZER_REG);
  222. }
  223. char Key1Flag = 0;
  224. void Key_0_Pressed(stHSDIn* pDin)
  225. {
  226. Key1Flag = 0;
  227. }
  228. void Key_0_Released(stHSDIn* pDin)
  229. {
  230. if( DIn[0].pressedSec < 1 )//jakuja
  231. return;
  232. if( Key1Flag == 1 )
  233. return;
  234. #if 0
  235. SystemManager.EmgSendButton = BUTTON_TEST;
  236. NRF_LOG_INFO("@@@@@@@ Test Pressed");
  237. SystemTimer.SEND_RUN = 1;
  238. SystemTimer.SEND_TIMER = 0;
  239. #endif
  240. }
  241. void DevRegLongPressedOne(stHSDIn* pDin)
  242. {
  243. Key1Flag = 1;
  244. #if 0
  245. SystemManager.EmgSendButton = BUTTON_REG;
  246. NRF_LOG_INFO("@@@@@@@ DevReg Pressed");
  247. SystemTimer.SEND_RUN = 1;
  248. SystemTimer.SEND_TIMER = 0;
  249. #endif
  250. }
  251. void Key_1_Pressed(stHSDIn* pDin)
  252. {
  253. if( DIn[0].onFlag == 1 )
  254. {
  255. SystemManager.FWUpdate = 1;
  256. Buzzer_Select(BUZZER_BOOT_LOADER);
  257. }
  258. }
  259. void Key_1_Released(stHSDIn* pDin)
  260. {
  261. if( DIn[1].pressedSec < 1 )//jakuja
  262. return;
  263. #if 0
  264. NRF_LOG_INFO("@@@@@@@ EMG Pressed");
  265. SystemManager.EmgSendButton = BUTTON_EMG;
  266. SystemTimer.SEND_RUN = 1;
  267. SystemTimer.SEND_TIMER = 0;
  268. #endif
  269. }
  270. #if 0
  271. void Key_2_Pressed(stHSDIn* pDin)
  272. {
  273. uint8_t sendKey[2];
  274. sendKey[0] = 0x51;
  275. keys_send(1, &sendKey[0]);
  276. }
  277. void Key_2_Released(stHSDIn* pDin)
  278. {
  279. //NRF_LOG_INFO("Key_2_Released");
  280. }
  281. void Key_3_Pressed(stHSDIn* pDin)
  282. {
  283. //NRF_LOG_INFO("Key_3_Pressed");
  284. }
  285. void Key_3_Released(stHSDIn* pDin)
  286. {
  287. //NRF_LOG_INFO("Key_3_Released");
  288. }
  289. void Key_4_Pressed(stHSDIn* pDin)
  290. {
  291. //NRF_LOG_INFO("Key_4_Pressed");
  292. }//
  293. void Key_4_Released(stHSDIn* pDin)
  294. {
  295. //NRF_LOG_INFO("Key_4_Released");
  296. }
  297. #endif