bikelight.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_ON;
  41. bikelight_stBikeLightOut.LightState = LIGHT_On;
  42. }
  43. }
  44. /***************************************************************
  45. Function: bikelight_voSetBikeLightOff;
  46. Description: set bike off
  47. Call by: functions in main loop;
  48. Input Variables: N/A
  49. Output/Return Variables: N/A
  50. Subroutine Call: N/A;
  51. Reference: N/A
  52. ****************************************************************/
  53. static void bikelight_voSetBikeLightOff(void)
  54. {
  55. if (bikelight_stBikeLightOut.LightState == LIGHT_On)
  56. {
  57. IO_BIKELIGHT_OFF;
  58. bikelight_stBikeLightOut.LightState = LIGHT_Off;
  59. }
  60. }
  61. /***************************************************************
  62. Function: bikelight_voBikeLightInit;
  63. Description: bike light control initialization
  64. Call by: functions in main loop;
  65. Input Variables: N/A
  66. Output/Return Variables: N/A
  67. Subroutine Call: N/A;
  68. Reference: N/A
  69. ****************************************************************/
  70. void bikelight_voGetBikeLightError(UWORD LightPowerVolPu)
  71. {
  72. if (bikelight_stBikeLightOut.LightState == LIGHT_On && (LightPowerVolPu < bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown ||
  73. LightPowerVolPu > bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp))
  74. {
  75. bikelight_stBikeLightOut.uwBikeLightErrorCnt++;
  76. if (bikelight_stBikeLightOut.uwBikeLightErrorCnt == bikelight_stBikeLightCoef.uwBikeLightErrorCnt)
  77. {
  78. IO_BIKELIGHT_OFF;
  79. bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0;
  80. bikelight_stBikeLightOut.LightState = LIGHT_Error;
  81. bikelight_stBikeLightOut.blBikeLightErrorFlg = TRUE;
  82. }
  83. }
  84. else
  85. {
  86. bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0;
  87. }
  88. if (bikelight_stBikeLightOut.LightState == LIGHT_Error)
  89. {
  90. IO_BIKELIGHT_ON;
  91. if (LightPowerVolPu > bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown &&
  92. LightPowerVolPu < bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp)
  93. {
  94. bikelight_stBikeLightOut.uwBikeLightRecoverCnt++;
  95. if (bikelight_stBikeLightOut.uwBikeLightRecoverCnt == bikelight_stBikeLightCoef.uwBikeLightRecoverCnt)
  96. {
  97. IO_BIKELIGHT_OFF;
  98. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  99. bikelight_stBikeLightOut.LightState = LIGHT_Off;
  100. bikelight_stBikeLightOut.blBikeLightErrorFlg = FALSE;
  101. }
  102. }
  103. else
  104. {
  105. IO_BIKELIGHT_OFF;
  106. bikelight_stBikeLightOut.uwBikeLightShutCnt++;
  107. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  108. if (bikelight_stBikeLightOut.uwBikeLightShutCnt == bikelight_stBikeLightCoef.uwBikeLightShutCnt)
  109. {
  110. bikelight_stBikeLightOut.LightState = LIGHT_Shut;
  111. bikelight_stBikeLightOut.uwBikeLightShutCnt = 0;
  112. }
  113. }
  114. }
  115. else
  116. {
  117. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  118. bikelight_stBikeLightOut.uwBikeLightShutCnt = 0;
  119. }
  120. }
  121. /***************************************************************
  122. Function: bikelight_voBikeLightInit;
  123. Description: bike light control initialization
  124. Call by: functions in main loop;
  125. Input Variables: N/A
  126. Output/Return Variables: N/A
  127. Subroutine Call: N/A;
  128. Reference: N/A
  129. ****************************************************************/
  130. void bikelight_voBikeLightInit(void)
  131. {
  132. bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0;
  133. bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0;
  134. bikelight_stBikeLightOut.uwBikeLightShutCnt = 0;
  135. bikelight_stBikeLightOut.LightState = LIGHT_Off;
  136. bikelight_stBikeLightOut.blBikeLightErrorFlg = FALSE;
  137. }
  138. /***************************************************************
  139. Function: bikelight_voBikeLightInit;
  140. Description: bike light control initialization
  141. Call by: functions in main loop;
  142. Input Variables: N/A
  143. Output/Return Variables: N/A
  144. Subroutine Call: N/A;
  145. Reference: N/A
  146. ****************************************************************/
  147. void bikelight_voBikeLightCoef(UWORD volt)
  148. {
  149. bikelight_stBikeLightCoef.uwBikeLightErrorCnt = LIGHT_POWER_ERROR_DETECT / LIGHT_POWER_ERROR_TIMEUNIT;
  150. bikelight_stBikeLightCoef.uwBikeLightRecoverCnt = LIGHT_POWER_ERROR_RECOVER / LIGHT_POWER_ERROR_TIMEUNIT;
  151. bikelight_stBikeLightCoef.uwBikeLightShutCnt = LIGHT_POWER_ERROR_SHUT / LIGHT_POWER_ERROR_TIMEUNIT;
  152. if( volt == 12)
  153. {
  154. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_DOWN << 15) / VBASE;
  155. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_UP << 15) / VBASE;
  156. IO_BIKELIGHT_12V_ENABLE;
  157. }
  158. else
  159. {
  160. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_DOWN << 14) / VBASE;
  161. bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_UP << 14) / VBASE;
  162. IO_BIKELIGHT_12V_DISABLE;
  163. }
  164. }
  165. /***************************************************************
  166. Function: bikelight_voBikeLightInit;
  167. Description: bike light control initialization
  168. Call by: functions in main loop;
  169. Input Variables: N/A
  170. Output/Return Variables: N/A
  171. Subroutine Call: N/A;
  172. Reference: N/A
  173. ****************************************************************/
  174. void bikelight_voBikeLightControl(UWORD switchAction)
  175. {
  176. if (switchAction == 1)
  177. {
  178. bikelight_voSetBikeLightOn();
  179. }
  180. if (switchAction == 0)
  181. {
  182. bikelight_voSetBikeLightOff();
  183. }
  184. }
  185. /*************************************************************************
  186. Local Functions (N/A)
  187. *************************************************************************/
  188. /*************************************************************************
  189. End of this File (EOF)!
  190. Do not put anything after this part!
  191. *************************************************************************/