@@ -190,28 +190,27 @@ static ssize_t battery_voltage_get(struct bt_conn *conn,
190190 const struct bt_gatt_attr * attr , void * buf ,
191191 uint16_t len , uint16_t offset )
192192{
193- uint8_t percent = 0 ;
194- uint32_t millivolts = 0 ;
195- uint8_t battery_attached = 0 ;
196- uint8_t charging = 0 ;
197- uint8_t vbus = 0 ;
198- uint8_t fault = 0 ;
199-
200- get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
201-
202- /* flush the fault status out by reading again */
203- if (fault ) {
204- get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
205- }
206- /* there can be 2 of these to flush */
207- if (fault ) {
208- get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
209- }
210-
211- if (battery_attached ) {
212- millivolts = read_battery_voltage ();
213- millivolts_to_percent (millivolts , & percent );
214- }
193+ uint8_t percent = 0 ;
194+ uint32_t millivolts = 0 ;
195+ uint8_t battery_attached = 0 ;
196+ uint8_t charging = 0 ;
197+ uint8_t vbus = 0 ;
198+ uint8_t fault = 0 ;
199+
200+ get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
201+
202+ /* flush the fault status out by reading again */
203+ if (fault ) {
204+ get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
205+ }
206+ /* there can be 2 of these to flush */
207+ if (fault ) {
208+ get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
209+ }
210+
211+ millivolts = read_battery_voltage ();
212+ millivolts_to_percent (millivolts , & percent );
213+
215214 return bt_gatt_attr_read (conn , attr , buf , len , offset , (uint8_t * ) & percent , 1 );
216215}
217216
@@ -223,7 +222,7 @@ static ssize_t battery_power_source_get(struct bt_conn *conn,
223222 uint8_t charging , vbus , battery_attached , fault ;
224223
225224 get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
226- if (vbus || ! battery_attached ) {
225+ if (vbus ) {
227226 power_source = ON_CHARGER_POWER ;
228227 } else {
229228 power_source = ON_BATTERY_POWER ;
@@ -827,14 +826,12 @@ void ble_notif_thread(void *a, void *b, void *c)
827826 uint8_t charging , vbus , battery_attached , fault , percent ;
828827
829828 get_battery_charging_status (& charging , & vbus , & battery_attached , & fault );
830- if (battery_attached ) {
831- uint32_t millivolts = read_battery_voltage ();
832- millivolts_to_percent (millivolts , & percent );
833- if (battery_last_percent != percent ) {
834- bt_gatt_notify (NULL , & bas .attrs [1 ], & percent , sizeof (percent ));
835- }
836- battery_last_percent = percent ;
829+ uint32_t millivolts = read_battery_voltage ();
830+ millivolts_to_percent (millivolts , & percent );
831+ if (battery_last_percent != percent ) {
832+ bt_gatt_notify (NULL , & bas .attrs [1 ], & percent , sizeof (percent ));
837833 }
834+ battery_last_percent = percent ;
838835 }
839836}
840837
0 commit comments