20 #define MAX_TIME_ON (60*5) //shutdown after 5mn when no activity
21 u8 PWR_BatteryLevel = 100;
23 u16 PWR_CurrentVBat = 0;
24 counter_t PWR_ReferenceTime = -1;
25 enum ePowerState PWR_CurrentState = PWR_STATE_UNDEF;
40 void POWER_Handler(
void )
42 enum ePowerState new_state = PWR_STATE_UNDEF;
43 static enum ePowerState last_state = PWR_STATE_UNDEF;
44 static u32 last_time = 0;
45 static u16 MinBat = 0;
46 static s32 stacounter = 0;
47 static s32 stacounter2 = 0;
58 u32 time = RTC_GetCounter();
66 if ( time != last_time )
71 PWR_CurrentVBat = ( ( PWR_CurrentVBat * 7 ) + vbat ) / 8;
77 if ( ( time / 3 ) != ( last_time / 3 ) )
80 s32 flagsta = POWER_ReadStatus();
85 new_state = PWR_STATE_FULL;
86 PWR_BatteryLevel = 100;
89 new_state = PWR_STATE_CHARGING;
90 PWR_BatteryLevel = 50;
93 new_state = PWR_STATE_NORMAL;
94 PWR_BatteryLevel = 50;
97 if ( PWR_CurrentVBat < NO_BAT_VOLTAGE )
100 new_state = PWR_STATE_NOBAT;
101 PWR_BatteryLevel = 100;
106 new_state = PWR_STATE_NORMAL;
107 PWR_BatteryLevel = 0;
113 if ( new_state == last_state )
115 if ( new_state != PWR_CurrentState )
117 switch ( PWR_CurrentState )
119 case PWR_STATE_NORMAL:
121 case PWR_STATE_EMPTY:
122 if ( new_state == PWR_STATE_NORMAL )
125 PWR_CurrentState = new_state;
126 MinBat = PWR_CurrentVBat;
133 switch ( PWR_CurrentState )
135 case PWR_STATE_NORMAL:
136 if ( MinBat < BAT_LOW_VOLTAGE )
138 PWR_CurrentState = PWR_STATE_LOW;
141 if ( PWR_CurrentVBat < MinBat )
143 MinBat = ( PWR_CurrentVBat + MinBat ) / 2;
145 if ( MinBat < BAT_EMPTY_VOLTAGE )
147 PWR_CurrentState = PWR_STATE_EMPTY;
149 PWR_BatteryLevel = ( 100 * ( MinBat - BAT_EMPTY_VOLTAGE ) ) / ( BAT_FULL_VOLTAGE - BAT_EMPTY_VOLTAGE ) ;
150 if ( PWR_BatteryLevel > 100 )
152 PWR_BatteryLevel = 100 ;
154 else if ( PWR_BatteryLevel < 0 )
156 PWR_BatteryLevel = 0 ;
161 case PWR_STATE_EMPTY:
163 if ( stacounter2++ > 45 )
169 DRAW_RestoreCharMagniCoeff();
173 if ( stacounter++ > 4 )
185 last_state = new_state;
192 if ( PWR_ReferenceTime != -1 )
194 counter_t On_Time = time - PWR_ReferenceTime;
195 if ( On_Time > MAX_TIME_ON )
197 if ( ( PWR_CurrentState != PWR_STATE_CHARGING ) && ( PWR_CurrentState != PWR_STATE_NOBAT ) )
223 void POWER_Reset_Time(
void )
225 PWR_ReferenceTime = -1;
241 void POWER_Set_Time(
void )
243 if ( PWR_ReferenceTime == -1 )
246 PWR_ReferenceTime = RTC_GetCounter();