2019-05-06 17:00:22 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-05-06 17:00:22 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2020-11-08 13:16:50 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-05-06 17:00:22 +02:00
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::boot {
|
2019-05-06 17:00:22 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
class BatteryDriver {
|
2021-10-16 01:30:27 +02:00
|
|
|
NON_COPYABLE(BatteryDriver);
|
|
|
|
NON_MOVEABLE(BatteryDriver);
|
2019-06-22 09:10:21 +02:00
|
|
|
private:
|
2021-10-16 01:30:27 +02:00
|
|
|
static constinit inline powctl::Session s_battery_session{powctl::Session::ConstantInitializeTag{}};
|
|
|
|
static constinit inline int s_reference_count{0};
|
2019-06-22 09:10:21 +02:00
|
|
|
public:
|
2021-10-16 01:30:27 +02:00
|
|
|
BatteryDriver() {
|
|
|
|
if ((s_reference_count++) == 0) {
|
|
|
|
R_ABORT_UNLESS(powctl::OpenSession(std::addressof(s_battery_session), powctl::DeviceCode_Max17050, ddsf::AccessMode_ReadWrite));
|
|
|
|
}
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
2019-05-06 17:00:22 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
~BatteryDriver() {
|
2021-10-16 01:30:27 +02:00
|
|
|
if ((--s_reference_count) == 0) {
|
|
|
|
powctl::CloseSession(s_battery_session);
|
|
|
|
}
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
public:
|
2020-11-08 13:16:50 +01:00
|
|
|
Result IsBatteryRemoved(bool *out) {
|
|
|
|
bool present;
|
2021-10-16 01:30:27 +02:00
|
|
|
R_TRY(powctl::IsBatteryPresent(std::addressof(present), s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
|
2021-10-07 08:22:54 +02:00
|
|
|
*out = !present;
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
2021-10-16 01:30:27 +02:00
|
|
|
Result GetChargePercentage(float *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetBatteryChargePercentage(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetAverageVCell(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetBatteryAverageVCell(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
2021-10-16 04:03:11 +02:00
|
|
|
Result GetVoltageFuelGaugePercentage(float *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetBatteryVoltageFuelGaugePercentage(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetAverageCurrent(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetBatteryAverageCurrent(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetCurrent(int *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetBatteryCurrent(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetTemperature(float *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::GetBatteryTemperature(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result IsI2cShutdownEnabled(bool *out) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::IsBatteryI2cShutdownEnabled(out, s_battery_session));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SetI2cShutdownEnabled(bool en) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_RETURN(powctl::SetBatteryI2cShutdownEnabled(s_battery_session, en));
|
2020-11-08 13:16:50 +01:00
|
|
|
}
|
2019-06-22 09:10:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|