dio(5758).c 7.3 KB

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