这是我基于原版修改后的源码,不放心的自己编译
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void control_charge_speed(const char *file_path, int target_value) {
FILE *file = fopen(file_path, "r+");
if (file) {
int current_value;
fscanf(file, "%d", ¤t_value);
if (current_value != target_value) {
rewind(file);
fprintf(file, "%d", target_value);
}
fclose(file);
}
}
int main() {
char buffer[128], charge_type[128], capacity_str[16];
int capacity;
while (1) {
FILE *fp = fopen("/sys/class/power_supply/battery/status", "r");
if (fp && fgets(buffer, sizeof(buffer), fp)) {
buffer[strcspn(buffer, "\n")] = 0;
if (strstr(buffer, "Charging")) {
FILE *charge_type_fp = fopen("/sys/class/power_supply/battery/charge_type", "r");
FILE *capacity_fp = fopen("/sys/class/power_supply/battery/capacity", "r");
if (charge_type_fp && fgets(charge_type, sizeof(charge_type), charge_type_fp)) {
charge_type[strcspn(charge_type, "\n")] = 0;
}
if (capacity_fp && fgets(capacity_str, sizeof(capacity_str), capacity_fp)) {
capacity_str[strcspn(capacity_str, "\n")] = 0;
sscanf(capacity_str, "%d", &capacity);
}
if (strcmp(charge_type, "Standard") == 0) {
control_charge_speed("/sys/class/power_supply/battery/constant_charge_current", capacity < 80 ? 10000000 : 6000000);
} else if (strcmp(charge_type, "Fast") == 0) {
if (capacity < 50) {
control_charge_speed("/sys/class/power_supply/battery/constant_charge_current", 24000000);
} else if (capacity < 80) {
control_charge_speed("/sys/class/power_supply/battery/constant_charge_current", 12000000);
} else {
control_charge_speed("/sys/class/power_supply/battery/constant_charge_current", 6000000);
}
}
if (charge_type_fp) fclose(charge_type_fp);
if (capacity_fp) fclose(capacity_fp);
}
}
if (fp) fclose(fp);
sleep(1);
}
return 0;
}