dio(3774).c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. Trigger_Emg();
  112. else{
  113. Trigger_DUF();
  114. }
  115. }
  116. if( DIn[DiIndex].pressTime % HSDIOTask.DISecCount == 0 ) // count up 0.5 sec
  117. {
  118. DIn[DiIndex].pressedSec++;
  119. if( DIn[0].onFlag == 1 && DIn[1].pressedSec == 2 )
  120. {
  121. ;
  122. }
  123. if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <= 5 ) //pressTime 25ms Tick
  124. {
  125. SystemTimer.SEND_TIMER = 0;
  126. }
  127. if( DIn[0].pressedSec == 5 )
  128. {
  129. Trigger_RegDev();
  130. }
  131. if( DIn[DiIndex].longPressTimeOne != 0 )
  132. {
  133. if( DIn[DiIndex].pressedSec >= DIn[DiIndex].longPressTimeOne )
  134. {
  135. DIn[DiIndex].longPressCount++;
  136. DIn[DiIndex].softRelease = 1;
  137. DIn[DiIndex].LongPressedOne(&DIn[DiIndex]);
  138. // NRF_LOG_INFO("==> Key_%d LongPressedOne", BT5ButtonList[DiIndex]);
  139. }
  140. }
  141. }
  142. }
  143. DIn[DiIndex].onFlag = 1;
  144. DIn[DiIndex].pressTime++;
  145. }else{
  146. if( DIn[DiIndex].onFlag == 1 )
  147. {
  148. if( DiIndex == 0 ) // TEST BUTTON
  149. {
  150. //if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <5 )
  151. if( DIn[0].pressTime >= 2 && DIn[0].pressTime < 100 ){
  152. if( SystemManager.FWUpdate == 0 )
  153. Trigger_LineTest();
  154. }else{
  155. // NRF_LOG_INFO("$$$ Key_%d Released", BT5ButtonList[DiIndex]);
  156. DIn[DiIndex].in = 0;
  157. DIn[DiIndex].pressTime = 0;
  158. DIn[DiIndex].onFlag = 0;
  159. DIn[DiIndex].pressedSec = 0;
  160. DIn[DiIndex].softRelease = 0;
  161. DIn[DiIndex].prevTime = 0;
  162. }
  163. }
  164. if( DiIndex == 1 ) // EMG BUTTON
  165. {
  166. //if( DIn[0].pressedSec >= 1 && DIn[0].pressedSec <5 )
  167. if( DIn[1].pressTime < 25 ){
  168. // NRF_LOG_INFO("$$$ Key_%d Released", BT5ButtonList[DiIndex]);
  169. DIn[DiIndex].in = 0;
  170. DIn[DiIndex].pressTime = 0;
  171. DIn[DiIndex].onFlag = 0;
  172. DIn[DiIndex].pressedSec = 0;
  173. DIn[DiIndex].softRelease = 0;
  174. DIn[DiIndex].prevTime = 0;
  175. }
  176. }
  177. DIn[DiIndex].Released(&DIn[DiIndex]);
  178. // NRF_LOG_INFO("==> Key_%d Released", BT5ButtonList[DiIndex]);
  179. }
  180. DIn[DiIndex].in = 0;
  181. DIn[DiIndex].pressTime = 0;
  182. DIn[DiIndex].onFlag = 0;
  183. DIn[DiIndex].pressedSec = 0;
  184. DIn[DiIndex].softRelease = 0;
  185. DIn[DiIndex].prevTime = 0;
  186. }
  187. }
  188. }
  189. void Trigger_Emg()
  190. {
  191. NRF_LOG_INFO("# EMG Pressed");
  192. SystemManager.EmgSendButton = BUTTON_EMG;
  193. SystemTimer.EMG_SEND_RUN = 1;
  194. SystemTimer.SEND_TIMER = 0;
  195. if(!advertising_flag)
  196. advertising_start();
  197. Buzzer_Select(BUZZER_EMG);
  198. }
  199. void Trigger_LineTest()
  200. {
  201. NRF_LOG_INFO("# Test Pressed");
  202. SystemManager.EmgSendButton = BUTTON_TEST;
  203. SystemTimer.EMG_SEND_RUN = 1;
  204. SystemTimer.SEND_TIMER = 0;
  205. if(!advertising_flag)
  206. advertising_start();
  207. Buzzer_Select(BUZZER_TEST);
  208. }
  209. void Trigger_RegDev()
  210. {
  211. NRF_LOG_INFO("# DevReg Pressed");
  212. SystemManager.EmgSendButton = BUTTON_REG;
  213. SystemTimer.EMG_SEND_RUN = 1;
  214. SystemTimer.SEND_TIMER = 0;
  215. if(!advertising_flag)
  216. advertising_start();
  217. Buzzer_Select(BUZZER_REG);
  218. }
  219. void Trigger_DUF()
  220. {
  221. NRF_LOG_INFO("# DevReg Pressed");
  222. SystemTimer.SEND_TIMER = 0;
  223. SystemManager.FWUpdate = 1;
  224. Buzzer_Select(BUZZER_BOOT_LOADER);
  225. }
  226. char Key1Flag = 0;
  227. void Key_0_Pressed(stHSDIn* pDin)
  228. {
  229. Key1Flag = 0;
  230. }
  231. void Key_0_Released(stHSDIn* pDin)
  232. {
  233. if( DIn[0].pressedSec < 1 )//jakuja
  234. return;
  235. if( Key1Flag == 1 )
  236. return;
  237. #if 0
  238. SystemManager.EmgSendButton = BUTTON_TEST;
  239. NRF_LOG_INFO("@@@@@@@ Test Pressed");
  240. SystemTimer.SEND_RUN = 1;
  241. SystemTimer.SEND_TIMER = 0;
  242. #endif
  243. }
  244. void DevRegLongPressedOne(stHSDIn* pDin)
  245. {
  246. Key1Flag = 1;
  247. #if 0
  248. SystemManager.EmgSendButton = BUTTON_REG;
  249. NRF_LOG_INFO("@@@@@@@ DevReg Pressed");
  250. SystemTimer.SEND_RUN = 1;
  251. SystemTimer.SEND_TIMER = 0;
  252. #endif
  253. }
  254. void Key_1_Pressed(stHSDIn* pDin)
  255. {
  256. if( DIn[0].onFlag == 1 )
  257. {
  258. SystemManager.FWUpdate = 1;
  259. Buzzer_Select(BUZZER_BOOT_LOADER);
  260. }
  261. }
  262. void Key_1_Released(stHSDIn* pDin)
  263. {
  264. if( DIn[1].pressedSec < 1 )//jakuja
  265. return;
  266. #if 0
  267. NRF_LOG_INFO("@@@@@@@ EMG Pressed");
  268. SystemManager.EmgSendButton = BUTTON_EMG;
  269. SystemTimer.SEND_RUN = 1;
  270. SystemTimer.SEND_TIMER = 0;
  271. #endif
  272. }
  273. #if 0
  274. void Key_2_Pressed(stHSDIn* pDin)
  275. {
  276. uint8_t sendKey[2];
  277. sendKey[0] = 0x51;
  278. keys_send(1, &sendKey[0]);
  279. }
  280. void Key_2_Released(stHSDIn* pDin)
  281. {
  282. //NRF_LOG_INFO("Key_2_Released");
  283. }
  284. void Key_3_Pressed(stHSDIn* pDin)
  285. {
  286. //NRF_LOG_INFO("Key_3_Pressed");
  287. }
  288. void Key_3_Released(stHSDIn* pDin)
  289. {
  290. //NRF_LOG_INFO("Key_3_Released");
  291. }
  292. void Key_4_Pressed(stHSDIn* pDin)
  293. {
  294. //NRF_LOG_INFO("Key_4_Pressed");
  295. }//
  296. void Key_4_Released(stHSDIn* pDin)
  297. {
  298. //NRF_LOG_INFO("Key_4_Released");
  299. }
  300. #endif