|
| 1 | +#include "ECController.h" |
| 2 | +#include "libectool.h" |
| 3 | + |
| 4 | +void ECController::handle_error(int code, const std::string &msg) { |
| 5 | + if (code == 0) return; |
| 6 | + |
| 7 | + std::string reason; |
| 8 | + switch (code) { |
| 9 | + case EC_ERR_INIT: reason = "EC initialization failed"; break; |
| 10 | + case EC_ERR_READMEM: reason = "EC memory read failed"; break; |
| 11 | + case EC_ERR_EC_COMMAND: reason = "EC command failed"; break; |
| 12 | + case EC_ERR_INVALID_PARAM: reason = "Invalid parameter"; break; |
| 13 | + default: reason = "Unknown error"; break; |
| 14 | + } |
| 15 | + |
| 16 | + throw std::runtime_error(msg + " (" + reason + ", code " + std::to_string(code) + ")"); |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +bool ECController::is_on_ac() { |
| 21 | + int ac; |
| 22 | + int ret = ec_is_on_ac(&ac); |
| 23 | + handle_error(ret, "Failed to read AC status"); |
| 24 | + return ac; |
| 25 | +} |
| 26 | + |
| 27 | +void ECController::auto_fan_control() { |
| 28 | + int ret = ec_auto_fan_control(); |
| 29 | + handle_error(ret, "Failed to enable auto fan control"); |
| 30 | +} |
| 31 | + |
| 32 | +void ECController::set_fan_duty(int duty) { |
| 33 | + int ret = ec_set_fan_duty(duty); |
| 34 | + handle_error(ret, "Failed to set fan duty"); |
| 35 | +} |
| 36 | + |
| 37 | +float ECController::get_max_temperature() { |
| 38 | + float t; |
| 39 | + int ret = ec_get_max_temperature(&t); |
| 40 | + handle_error(ret, "Failed to get max temperature"); |
| 41 | + return t; |
| 42 | +} |
| 43 | + |
| 44 | +float ECController::get_max_non_battery_temperature() { |
| 45 | + float t; |
| 46 | + int ret = ec_get_max_non_battery_temperature(&t); |
| 47 | + handle_error(ret, "Failed to get non-battery temperature"); |
| 48 | + return t; |
| 49 | +} |
0 commit comments