123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include "light_driver.h"
- void LightDriver_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pins : PBPin PBPin */
- GPIO_InitStruct.Pin = LIGHT_F_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LIGHT_F_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pins : PAPin PAPin */
- GPIO_InitStruct.Pin = LIGHT_B_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LIGHT_B_GPIO_Port, &GPIO_InitStruct);
- }
- void LightDriver_Process(MC_TailLight_Mode_Struct_t TailLight_Mode, uint8_t OnTime, uint8_t PeriodTime, TrueOrFalse_Flag_Struct_t IsBreakFlag, MC_LightSwitch_Struct_t LightSwitchCode)
- {
- //控制前灯
- if(LightSwitchCode == MC_LightSwitch_ON)
- {
- HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_SET);
- }
- else if(LightSwitchCode == MC_LightSwitch_OFF)
- {
- HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
- }
-
- //控制后灯
- uint8_t Light_CrlCode = 0;//高4位:控制方式,1-常亮,2-闪烁; 低4位:开关状态,1-开,2-关
- static uint32_t FlashTimeCnt = 0;
- switch(TailLight_Mode)
- {
- case MC_TAIL_LIGHT_LAMP_FlASH: //照明闪烁
- {
- Light_CrlCode = (LightSwitchCode == MC_LightSwitch_ON) ? 0x21 : 0x20;
- break;
- }
- case MC_TAIL_LIGHT_BREAK_FLASH: //刹车闪烁
- {
- Light_CrlCode = (IsBreakFlag == TRUE) ? 0x21 : 0x20;
- break;
- }
- case MC_TAIL_LIGHT_BREAK: //刹车常亮
- {
- Light_CrlCode = (IsBreakFlag == TRUE) ? 0x11 : 0x10;
- break;
- }
- case MC_TAIL_LIGHT_LAMP: //照明常亮
- default: //未配置时为照明常亮
- {
- Light_CrlCode = (LightSwitchCode == MC_LightSwitch_ON) ? 0x11 : 0x10;
- break;
- }
- }
- if((Light_CrlCode & 0xF0) == 0x10)//常亮
- {
- if((Light_CrlCode & 0x0F) == 0x01)//开
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else if((Light_CrlCode & 0x0F) == 0x00)//关
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- }
- else if((Light_CrlCode & 0xF0) == 0x20)//闪烁
- {
- if((Light_CrlCode & 0x0F) == 0x01)//开
- {
- if((HAL_GetTick() - FlashTimeCnt) < OnTime * 20)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else if((HAL_GetTick() - FlashTimeCnt) < PeriodTime * 20)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- else
- {
- FlashTimeCnt = HAL_GetTick();
- }
- }
- else if((Light_CrlCode & 0x0F) == 0x00)//关
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- FlashTimeCnt = HAL_GetTick();
- }
- }
-
- }
|