bikelight.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @file Bikelight.c
  3. * @author Wang, Zhiyu(wangzy49@midea.com)
  4. * @brief light of ebike
  5. * @version 0.1
  6. * @date 2021-10-09
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. /************************************************************************
  12. Beginning of File, do not put anything above here except notes
  13. Compiler Directives:
  14. *************************************************************************/
  15. #include "syspar.h"
  16. #include "typedefine.h"
  17. #include "mathtool.h"
  18. #include "bikelight.h"
  19. #include "hwsetup.h"
  20. /******************************
  21. *
  22. * Parameter
  23. *
  24. ******************************/
  25. BIKELIGHT_OUT bikelight_stBikeLightOut = BIKELIGHT_OUT_DEFAULT;
  26. static BIKELIGHT_COEF bikelight_stBikeLightCoef = BIKELIGHT_COEF_DEFAULT;
  27. /***************************************************************
  28. Function: bikelight_voSetBikeLightOn;
  29. Description: set bike light on
  30. Call by: functions in main loop;
  31. Input Variables: N/A
  32. Output/Return Variables: N/A
  33. Subroutine Call: N/A;
  34. Reference: N/A
  35. ****************************************************************/
  36. static void bikelight_voSetBikeLightOn(void)
  37. {
  38. if (bikelight_stBikeLightOut.LightState == LIGHT_Off)
  39. {
  40. IO_BIKELIGHT_FRONT_ON;
  41. IO_BIKELIGHT_REAR_ON;
  42. bikelight_stBikeLightOut.LightState = LIGHT_On;
  43. }
  44. }
  45. /***************************************************************
  46. Function: bikelight_voSetBikeLightOff;
  47. Description: set bike off
  48. Call by: functions in main loop;
  49. Input Variables: N/A
  50. Output/Return Variables: N/A
  51. Subroutine Call: N/A;
  52. Reference: N/A
  53. ****************************************************************/
  54. static void bikelight_voSetBikeLightOff(void)
  55. {
  56. if (bikelight_stBikeLightOut.LightState == LIGHT_On)
  57. {
  58. IO_BIKELIGHT_FRONT_OFF;
  59. IO_BIKELIGHT_REAR_OFF;
  60. bikelight_stBikeLightOut.LightState = LIGHT_Off;
  61. }
  62. }
  63. /***************************************************************
  64. Function: bikelight_voBikeLightInit;
  65. Description: bike light control initialization
  66. Call by: functions in main loop;
  67. Input Variables: N/A
  68. Output/Return Variables: N/A
  69. Subroutine Call: N/A;
  70. Reference: N/A
  71. ****************************************************************/
  72. void bikelight_voGetBikeLightError(UWORD LightPowerVolPu)
  73. {
  74. if (bikelight_stBikeLightOut.LightState == LIGHT_On && (LightPowerVolPu < bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown ||
  75. LightPowerVolPu > bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp))
  76. {
  77. bikelight_stBikeLightOut.uwBikeLightErrorCnt++;
  78. if (bikelight_stBikeLightOut.uwBikeLightErrorCnt == bikelight_stBikeLightCoef.uwBikeLightErrorCnt)
  79. {
  80. IO_BIKELIGHT_FRONT_OFF;
  81. IO_BIKELIGHT_REAR_OFF;
  82. bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0;
  83. bikelight_stBikeLightOut.LightState = LIGHT_Error;
  84. bikelight_stBikeLightOut.blBikeLightErrorFlg = TRUE;
  85. }
  86. }
  87. else
  88. {
  89. bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0;
  90. }
  91. if (bikelight_stBikeLightOut.LightState == LIGHT_Error)
  92. {
  93. IO_BIKELIGHT_FRONT_ON;
  94. IO_BIKELIGHT_REAR_ON;
  95. if (LightPowerVolPu > bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown &&
  96. LightPowerVolPu < bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp)
  97. {
  98. bikelight_stBikeLightOut.uwBikeLightRecoverCnt++;
  99. if (bikelight_stBikeLightOut.uwBikeLightRecoverCnt == bikelight_stBikeLightCoef.uwBikeLightRecoverCnt)
  100. {
  101. IO_BIKELIGHT_FRONT_OFF;
  102. IO_BIKELIGHT_REAR_OFF;
  103. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  104. bikelight_stBikeLightOut.LightState = LIGHT_Off;
  105. bikelight_stBikeLightOut.blBikeLightErrorFlg = FALSE;
  106. }
  107. }
  108. else
  109. {
  110. IO_BIKELIGHT_FRONT_OFF;
  111. IO_BIKELIGHT_REAR_OFF;
  112. bikelight_stBikeLightOut.uwBikeLightShutCnt++;
  113. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  114. if (bikelight_stBikeLightOut.uwBikeLightShutCnt == bikelight_stBikeLightCoef.uwBikeLightShutCnt)
  115. {
  116. bikelight_stBikeLightOut.LightState = LIGHT_Shut;
  117. bikelight_stBikeLightOut.uwBikeLightShutCnt = 0;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  124. bikelight_stBikeLightOut.uwBikeLightShutCnt = 0;
  125. }
  126. }
  127. /***************************************************************
  128. Function: bikelight_voBikeLightInit;
  129. Description: bike light control initialization
  130. Call by: functions in main loop;
  131. Input Variables: N/A
  132. Output/Return Variables: N/A
  133. Subroutine Call: N/A;
  134. Reference: N/A
  135. ****************************************************************/
  136. void bikelight_voBikeLightInit(void)
  137. {
  138. bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0;
  139. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  140. bikelight_stBikeLightOut.uwBikeLightShutCnt = 0;
  141. bikelight_stBikeLightOut.LightState = LIGHT_Off;
  142. bikelight_stBikeLightOut.blBikeLightErrorFlg = FALSE;
  143. }
  144. /***************************************************************
  145. Function: bikelight_voBikeLightInit;
  146. Description: bike light control initialization
  147. Call by: functions in main loop;
  148. Input Variables: N/A
  149. Output/Return Variables: N/A
  150. Subroutine Call: N/A;
  151. Reference: N/A
  152. ****************************************************************/
  153. void bikelight_voBikeLightCoef(UWORD volt)
  154. {
  155. bikelight_stBikeLightCoef.uwBikeLightErrorCnt = LIGHT_POWER_ERROR_DETECT / LIGHT_POWER_ERROR_TIMEUNIT;
  156. bikelight_stBikeLightCoef.uwBikeLightRecoverCnt = LIGHT_POWER_ERROR_RECOVER / LIGHT_POWER_ERROR_TIMEUNIT;
  157. bikelight_stBikeLightCoef.uwBikeLightShutCnt = LIGHT_POWER_ERROR_SHUT / LIGHT_POWER_ERROR_TIMEUNIT;
  158. if( volt == 12)
  159. {
  160. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_DOWN << 15) / VBASE;
  161. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_UP << 15) / VBASE;
  162. IO_BIKELIGHT_FRONT_12V_ENABLE;
  163. IO_BIKELIGHT_REAR_12V_ENABLE;
  164. }
  165. else
  166. {
  167. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_DOWN << 14) / VBASE;
  168. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_UP << 14) / VBASE;
  169. IO_BIKELIGHT_FRONT_12V_DISABLE;
  170. IO_BIKELIGHT_REAR_12V_DISABLE;
  171. }
  172. }
  173. /***************************************************************
  174. Function: bikelight_voBikeLightInit;
  175. Description: bike light control initialization
  176. Call by: functions in main loop;
  177. Input Variables: N/A
  178. Output/Return Variables: N/A
  179. Subroutine Call: N/A;
  180. Reference: N/A
  181. ****************************************************************/
  182. void bikelight_voBikeLightControl(UWORD switchAction)
  183. {
  184. if (switchAction == 1)
  185. {
  186. bikelight_voSetBikeLightOn();
  187. }
  188. if (switchAction == 0)
  189. {
  190. bikelight_voSetBikeLightOff();
  191. }
  192. }
  193. /*************************************************************************
  194. Local Functions (N/A)
  195. *************************************************************************/
  196. /*************************************************************************
  197. End of this File (EOF)!
  198. Do not put anything after this part!
  199. *************************************************************************/