/** * @file Bikelight.c * @author Wang, Zhiyu(wangzy49@midea.com) * @brief light of ebike * @version 0.1 * @date 2021-10-09 * * @copyright Copyright (c) 2021 * */ /************************************************************************ Beginning of File, do not put anything above here except notes Compiler Directives: *************************************************************************/ #include "syspar.h" #include "typedefine.h" #include "mathtool.h" #include "bikelight.h" #include "hwsetup.h" /****************************** * * Parameter * ******************************/ BIKELIGHT_OUT bikelight_stBikeLightOut = BIKELIGHT_OUT_DEFAULT; static BIKELIGHT_COEF bikelight_stBikeLightCoef = BIKELIGHT_COEF_DEFAULT; /*************************************************************** Function: bikelight_voSetBikeLightOn; Description: set bike light on Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ static void bikelight_voSetBikeLightOn(void) { if (bikelight_stBikeLightOut.LightState == LIGHT_Off) { IO_BIKELIGHT_FRONT_ON; IO_BIKELIGHT_REAR_ON; bikelight_stBikeLightOut.LightState = LIGHT_On; } } /*************************************************************** Function: bikelight_voSetBikeLightOff; Description: set bike off Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ static void bikelight_voSetBikeLightOff(void) { if (bikelight_stBikeLightOut.LightState == LIGHT_On) { IO_BIKELIGHT_FRONT_OFF; IO_BIKELIGHT_REAR_OFF; bikelight_stBikeLightOut.LightState = LIGHT_Off; } } /*************************************************************** Function: bikelight_voBikeLightInit; Description: bike light control initialization Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikelight_voGetBikeLightError(UWORD LightPowerVolPu) { if (bikelight_stBikeLightOut.LightState == LIGHT_On && (LightPowerVolPu < bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown || LightPowerVolPu > bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp)) { bikelight_stBikeLightOut.uwBikeLightErrorCnt++; if (bikelight_stBikeLightOut.uwBikeLightErrorCnt == bikelight_stBikeLightCoef.uwBikeLightErrorCnt) { IO_BIKELIGHT_FRONT_OFF; IO_BIKELIGHT_REAR_OFF; bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0; bikelight_stBikeLightOut.LightState = LIGHT_Error; bikelight_stBikeLightOut.blBikeLightErrorFlg = TRUE; } } else { bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0; } if (bikelight_stBikeLightOut.LightState == LIGHT_Error) { IO_BIKELIGHT_FRONT_ON; IO_BIKELIGHT_REAR_ON; if (LightPowerVolPu > bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown && LightPowerVolPu < bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp) { bikelight_stBikeLightOut.uwBikeLightRecoverCnt++; if (bikelight_stBikeLightOut.uwBikeLightRecoverCnt == bikelight_stBikeLightCoef.uwBikeLightRecoverCnt) { IO_BIKELIGHT_FRONT_OFF; IO_BIKELIGHT_REAR_OFF; bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0; bikelight_stBikeLightOut.LightState = LIGHT_Off; bikelight_stBikeLightOut.blBikeLightErrorFlg = FALSE; } } else { IO_BIKELIGHT_FRONT_OFF; IO_BIKELIGHT_REAR_OFF; bikelight_stBikeLightOut.uwBikeLightShutCnt++; bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0; if (bikelight_stBikeLightOut.uwBikeLightShutCnt == bikelight_stBikeLightCoef.uwBikeLightShutCnt) { bikelight_stBikeLightOut.LightState = LIGHT_Shut; bikelight_stBikeLightOut.uwBikeLightShutCnt = 0; } } } else { bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0; bikelight_stBikeLightOut.uwBikeLightShutCnt = 0; } } /*************************************************************** Function: bikelight_voBikeLightInit; Description: bike light control initialization Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikelight_voBikeLightInit(void) { bikelight_stBikeLightOut.uwBikeLightErrorCnt = 0; bikelight_stBikeLightOut.uwBikeLightRecoverCnt = 0; bikelight_stBikeLightOut.uwBikeLightShutCnt = 0; bikelight_stBikeLightOut.LightState = LIGHT_Off; bikelight_stBikeLightOut.blBikeLightErrorFlg = FALSE; } /*************************************************************** Function: bikelight_voBikeLightInit; Description: bike light control initialization Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikelight_voBikeLightCoef(UWORD volt) { bikelight_stBikeLightCoef.uwBikeLightErrorCnt = LIGHT_POWER_ERROR_DETECT / LIGHT_POWER_ERROR_TIMEUNIT; bikelight_stBikeLightCoef.uwBikeLightRecoverCnt = LIGHT_POWER_ERROR_RECOVER / LIGHT_POWER_ERROR_TIMEUNIT; bikelight_stBikeLightCoef.uwBikeLightShutCnt = LIGHT_POWER_ERROR_SHUT / LIGHT_POWER_ERROR_TIMEUNIT; if( volt == 12) { bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_DOWN << 15) / VBASE; bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_UP << 15) / VBASE; IO_BIKELIGHT_FRONT_12V_ENABLE; IO_BIKELIGHT_REAR_12V_ENABLE; } else { bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuDown = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_DOWN << 14) / VBASE; bikelight_stBikeLightCoef.uwBikeLightErrorVoltagePuUp = ((ULONG)LIGHT_POWER_ERROR_VOLTAGE_UP << 14) / VBASE; IO_BIKELIGHT_FRONT_12V_DISABLE; IO_BIKELIGHT_REAR_12V_DISABLE; } } /*************************************************************** Function: bikelight_voBikeLightInit; Description: bike light control initialization Call by: functions in main loop; Input Variables: N/A Output/Return Variables: N/A Subroutine Call: N/A; Reference: N/A ****************************************************************/ void bikelight_voBikeLightControl(UWORD switchAction) { if (switchAction == 1) { bikelight_voSetBikeLightOn(); } if (switchAction == 0) { bikelight_voSetBikeLightOff(); } } /************************************************************************* Local Functions (N/A) *************************************************************************/ /************************************************************************* End of this File (EOF)! Do not put anything after this part! *************************************************************************/