light_driver.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "light_driver.h"
  2. void LightDriver_Init(void)
  3. {
  4. GPIO_InitTypeDef GPIO_InitStruct;
  5. /* GPIO Ports Clock Enable */
  6. __HAL_RCC_GPIOA_CLK_ENABLE();
  7. __HAL_RCC_GPIOB_CLK_ENABLE();
  8. /*Configure GPIO pin Output Level */
  9. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
  10. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  11. /*Configure GPIO pins : PBPin PBPin */
  12. GPIO_InitStruct.Pin = LIGHT_F_Pin;
  13. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  14. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  15. HAL_GPIO_Init(LIGHT_F_GPIO_Port, &GPIO_InitStruct);
  16. /*Configure GPIO pins : PAPin PAPin */
  17. GPIO_InitStruct.Pin = LIGHT_B_Pin;
  18. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  19. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  20. HAL_GPIO_Init(LIGHT_B_GPIO_Port, &GPIO_InitStruct);
  21. }
  22. void LightDriver_Process(TrueOrFalse_Flag_Struct_t IsBreakFlag, MC_LightSwitch_Struct_t LightSwitchCode)
  23. {
  24. if(HAL_GetTick() < 3000)
  25. {
  26. return;
  27. }
  28. //前灯
  29. if(LightSwitchCode == MC_LightSwitch_ON)
  30. {
  31. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_SET);
  32. }
  33. else if(LightSwitchCode == MC_LightSwitch_OFF)
  34. {
  35. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
  36. }
  37. //尾灯
  38. static uint16_t FlashTimeCnt = 0;
  39. if(strncmp("FLASH", (char*)UserString2, 5) == 0)//利用UserString2作为尾灯刹车时显示状态
  40. {
  41. if(IsBreakFlag == TRUE)//刹车时,快闪
  42. {
  43. if(FlashTimeCnt < 600)
  44. {
  45. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  46. }
  47. else if(FlashTimeCnt < 1200)
  48. {
  49. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  50. }
  51. else
  52. {
  53. FlashTimeCnt = 0;
  54. }
  55. FlashTimeCnt++;
  56. }
  57. else//无刹车时,开灯亮
  58. {
  59. if(LightSwitchCode == MC_LightSwitch_ON)
  60. {
  61. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  62. }
  63. else if(LightSwitchCode == MC_LightSwitch_OFF)
  64. {
  65. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  66. }
  67. FlashTimeCnt = 0;
  68. }
  69. }
  70. else
  71. {
  72. if(IsBreakFlag == TRUE)//刹车时,高亮
  73. {
  74. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  75. FlashTimeCnt = 0;
  76. }
  77. else//无刹车时,开灯低亮
  78. {
  79. if(LightSwitchCode == MC_LightSwitch_ON)
  80. {
  81. if(FlashTimeCnt < 138)
  82. {
  83. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  84. }
  85. else if(FlashTimeCnt < 200)
  86. {
  87. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  88. }
  89. else
  90. {
  91. FlashTimeCnt = 0;
  92. }
  93. FlashTimeCnt++;
  94. }
  95. else if(LightSwitchCode == MC_LightSwitch_OFF)
  96. {
  97. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  98. FlashTimeCnt = 0;
  99. }
  100. }
  101. }
  102. }