Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for power_now #2099

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 39 additions & 26 deletions src/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2360,35 +2360,48 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat) {
}

void get_battery_power_draw(char *buffer, unsigned int n, const char *bat) {
static int reported = 0;
char current_now_path[256], voltage_now_path[256], current_now_val[256],
voltage_now_val[256];
char *ptr;
long current_now, voltage_now;
FILE *current_now_file;
FILE *voltage_now_file;
double result;

snprintf(current_now_path, 255, SYSFS_BATTERY_BASE_PATH "/%s/current_now",
bat);
snprintf(voltage_now_path, 255, SYSFS_BATTERY_BASE_PATH "/%s/voltage_now",
bat);

current_now_file = open_file(current_now_path, &reported);
voltage_now_file = open_file(voltage_now_path, &reported);

if (current_now_file != nullptr && voltage_now_file != nullptr) {
if (fgets(current_now_val, 256, current_now_file) &&
fgets(voltage_now_val, 256, voltage_now_file)) {
current_now = strtol(current_now_val, &ptr, 10);
voltage_now = strtol(voltage_now_val, &ptr, 10);

result = (double)(current_now * voltage_now) / (double)1000000000000;
static int reported_power = 0;
static int reported_other = 0;
char path[256];
char value[256];
FILE *fp;
char *ret;

snprintf(path, 255, SYSFS_BATTERY_BASE_PATH "/%s/power_now", bat);
fp = open_file(path, &reported_power);
if (fp != nullptr) {
ret = fgets(value, 256, fp);
fclose(fp);

if (ret != nullptr) {
double result = strtol(value, NULL, 10) * 1e-6;
snprintf(buffer, n, "%.1f", result);
return;
}
fclose(current_now_file);
fclose(voltage_now_file);
}

snprintf(path, 255, SYSFS_BATTERY_BASE_PATH "/%s/current_now", bat);
fp = open_file(path, &reported_other);
if (fp == nullptr)
return;
ret = fgets(value, 256, fp);
fclose(fp);

if (ret == nullptr)
return;
double result = strtol(value, NULL, 10) * 1e-6;

snprintf(path, 255, SYSFS_BATTERY_BASE_PATH "/%s/voltage_now", bat);
fp = open_file(path, &reported_other);
if (fp == nullptr)
return;
ret = fgets(value, 256, fp);
fclose(fp);

if (fp == nullptr)
return;
result *= strtol(value, NULL, 10) * 1e-6;
snprintf(buffer, n, "%.1f", result);
}

int _get_battery_perct(const char *bat) {
Expand Down
Loading