PWR_BTN_ATY.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @file PWR_BTN_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of power button for C platform
  20. *
  21. * @version
  22. * - 1_01_251118 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __PWR_BTN_ATY_C
  27. #define __PWR_BTN_ATY_C
  28. #include "PWR_BTN_ATY.h"
  29. #define PWR_BTN_ATY_TAG "\r\n[PWR_BTN_ATY] "
  30. /******************************* For user *************************************/
  31. /******************************************************************************/
  32. /**
  33. * @brief turn on power in soft
  34. *
  35. * @param dev device
  36. * @return uint8_t
  37. * @note ioSet pull down, output low when start
  38. * ioGet pull up
  39. */
  40. uint8_t PWR_BTN_On(struct PWR_BTN_ATY_Dev* dev){
  41. uint16_t i = 0;
  42. __ATY_LOCK(dev);
  43. // make little delay before ioSet, 0.5ms at least after main start
  44. while(i < 3000) i++;
  45. dev->ioSet(__ATY_HL_H);
  46. printf_ATY_D("%sPWR_BTN_On.", PWR_BTN_ATY_TAG);
  47. __ATY_UNLOCK(dev);
  48. return 0;
  49. }
  50. /**
  51. * @brief turn off power in soft
  52. *
  53. * @param dev device
  54. * @return uint8_t
  55. */
  56. uint8_t PWR_BTN_Off(struct PWR_BTN_ATY_Dev* dev){
  57. __ATY_LOCK(dev);
  58. dev->ioSet(__ATY_HL_L);
  59. printf_ATY_D("%sPWR_BTN_Off.", PWR_BTN_ATY_TAG);
  60. if(dev->powerOffCallback != NULL)
  61. dev->powerOffCallback();
  62. __ATY_UNLOCK(dev);
  63. return 0;
  64. }
  65. /**
  66. * @brief process power button status with IO IT type
  67. *
  68. * @param dev device
  69. * @return uint8_t
  70. * @note put this function at timer IT callback
  71. * button long press time count depend on cycle time
  72. */
  73. uint8_t PWR_BTN_Status_withIT(struct PWR_BTN_ATY_Dev* dev){
  74. __ATY_LOCK(dev);
  75. if(dev->PWR_BTN_LongPressCount != 0){
  76. dev->PWR_BTN_LongPressCount++;
  77. }
  78. if(dev->PWR_BTN_LongPressCount > dev->PWR_BTN_LongPressCountMax){
  79. dev->PWR_BTN_LongPressCount = 0;
  80. if(dev->ioGet() == __ATY_HL_L){
  81. __ATY_UNLOCK(dev);
  82. PWR_BTN_Off(dev);
  83. __ATY_LOCK(dev);
  84. }
  85. }
  86. __ATY_UNLOCK(dev);
  87. return 0;
  88. }
  89. uint8_t __PWR_BTN_INIT_Flag = 1;
  90. /**
  91. * @brief process power button status with IO cycle read type
  92. *
  93. * @param dev device
  94. * @return uint8_t
  95. * @note put this function at timer IT callback
  96. * button long press time count depend on cycle time
  97. */
  98. uint8_t PWR_BTN_Status_withCycle(struct PWR_BTN_ATY_Dev* dev){
  99. __ATY_LOCK(dev);
  100. if(dev->ioGet() == __ATY_HL_L){
  101. if(__PWR_BTN_INIT_Flag == 0){
  102. dev->PWR_BTN_LongPressCount++;
  103. if(dev->PWR_BTN_LongPressCount > dev->PWR_BTN_LongPressCountMax){
  104. dev->PWR_BTN_LongPressCount = 0;
  105. __ATY_UNLOCK(dev);
  106. PWR_BTN_Off(dev);
  107. __ATY_LOCK(dev);
  108. }
  109. }
  110. }
  111. else{
  112. dev->PWR_BTN_LongPressCount = 0;
  113. __PWR_BTN_INIT_Flag = 0;
  114. }
  115. __ATY_UNLOCK(dev);
  116. return 0;
  117. }
  118. #endif /* __PWR_BTN_ATY_C */
  119. /************************************ etc *************************************/
  120. /* init */
  121. // Power Button ----------------------------------------------------------------
  122. // #include "PWR_BTN_ATY.h"
  123. // void PWR_BTN_1_IO_SET(uint8_t level){
  124. // if(level)
  125. // HAL_GPIO_WritePin(SYS_PWR_GPIO_Port, SYS_PWR_Pin, GPIO_PIN_SET);
  126. // else
  127. // HAL_GPIO_WritePin(SYS_PWR_GPIO_Port, SYS_PWR_Pin, GPIO_PIN_RESET);
  128. // }
  129. // uint8_t PWR_BTN_1_IO_GET(void){
  130. // if(HAL_GPIO_ReadPin(SYS_OFF_GPIO_Port, SYS_OFF_Pin) == GPIO_PIN_SET)
  131. // return 1;
  132. // else
  133. // return 0;
  134. // }
  135. // void PWR_BTN_1_powerOffCallback(void){
  136. // // turn off board
  137. // LED_ATY_t_1.ledBlinkType = 0x00;
  138. // }
  139. // struct PWR_BTN_ATY_Dev PWR_BTN_ATY_t_1 = {
  140. // .PWR_BTN_LongPressCountMax = 20,
  141. // .PWR_BTN_LongPressCount = 0,
  142. // .ioSet = PWR_BTN_1_IO_SET,
  143. // .ioGet = PWR_BTN_1_IO_GET,
  144. // .powerOffCallback = PWR_BTN_1_powerOffCallback,
  145. // .lock = __ATY_UNLOCKED
  146. // };
  147. // // if use IT type
  148. // void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
  149. // if(GPIO_Pin == SYS_OFF_Pin){
  150. // // raise and fall
  151. // if(HAL_GPIO_ReadPin(SYS_OFF_GPIO_Port, SYS_OFF_Pin) == GPIO_PIN_SET)
  152. // PWR_BTN_ATY_t_1.PWR_BTN_LongPressCount = 0;
  153. // else
  154. // PWR_BTN_ATY_t_1.PWR_BTN_LongPressCount = 1;
  155. // }
  156. // }
  157. /* use */
  158. // PWR_BTN_Status_withIT(&PWR_BTN_ATY_t_1); // put at 1ms cycle
  159. // // or
  160. // PWR_BTN_Status_withCycle(&PWR_BTN_ATY_t_1); // put at 1ms cycle
  161. /******************************************************************************/
  162. /******************************** End Of File *********************************/