From c6074b0f4de9f193e314b565bb9965181ab8bde8 Mon Sep 17 00:00:00 2001 From: Alessandro Toia Date: Mon, 12 Apr 2021 21:40:04 -0700 Subject: [PATCH] Check if there are batteries before checking if they are charging --- src/battery.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/battery.cpp b/src/battery.cpp index 010fd9f1..0b275fe7 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -151,27 +151,29 @@ float BatteryStats::getPower(int batt_num) { } bool BatteryStats::isCharging() { - for(int i =0; i < 2; i++) { - string syspath = battPath[i]; - string status = syspath + "/status"; - std::ifstream input(status); - std::string line; - - if(std::getline(input,line)) { - current_status= line; - state[i]=current_status; + if (numBattery() > 0) { + for(int i =0; i < 2; i++) { + string syspath = battPath[i]; + string status = syspath + "/status"; + std::ifstream input(status); + std::string line; + + if(std::getline(input,line)) { + current_status= line; + state[i]=current_status; + } } - } - for(int i =0; i < 2; i++) { - if (state[i] == "Charging") { + for(int i =0; i < 2; i++) { + if (state[i] == "Charging") { return true; + } } } return false; - } + bool BatteryStats::fullCharge(){ //check if both batteries are fully charged int charged =0;