Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 2 additions & 20 deletions pyectool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
from __future__ import annotations

from .libectool_py import (
__doc__,
__version__,
ascii_mode,
auto_fan_control,
get_max_non_battery_temperature,
get_max_temperature,
init,
is_on_ac,
release,
set_fan_duty,
)
from .libectool_py import __doc__, __version__, ECController

__all__ = [
"__doc__",
"__version__",
"ascii_mode",
"auto_fan_control",
"get_max_non_battery_temperature",
"get_max_temperature",
"init",
"is_on_ac",
"release",
"set_fan_duty",
"ECController",
]
16 changes: 7 additions & 9 deletions pyectool/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ from __future__ import annotations
__doc__: str
__version__: str

def init() -> None: ...
def release() -> None: ...
def is_on_ac() -> bool: ...
def auto_fan_control() -> None: ...
def set_fan_duty(duty: int) -> None: ...
def get_max_temperature() -> float: ...
def get_max_non_battery_temperature() -> float: ...

ascii_mode: bool
class ECController:
def __init__(self) -> None: ...
def is_on_ac(self) -> bool: ...
def auto_fan_control(self) -> None: ...
def set_fan_duty(self, duty: int) -> None: ...
def get_max_temperature(self) -> float: ...
def get_max_non_battery_temperature(self) -> float: ...
5 changes: 3 additions & 2 deletions src/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Create the Python module
python_add_library(libectool_py MODULE libectool_py.cc WITH_SOABI)
python_add_library(libectool_py MODULE PyECController.cc ECController.cc
WITH_SOABI)

# Link against required libraries
target_link_libraries(libectool_py PRIVATE pybind11::headers libectool)
target_include_directories(libectool_py PUBLIC ../include)
target_include_directories(libectool_py PRIVATE . ../include)
target_compile_definitions(libectool_py PUBLIC VERSION_INFO=${PROJECT_VERSION})
49 changes: 49 additions & 0 deletions src/bindings/ECController.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "ECController.h"
#include "libectool.h"

void ECController::handle_error(int code, const std::string &msg) {
if (code == 0) return;

std::string reason;
switch (code) {
case EC_ERR_INIT: reason = "EC initialization failed"; break;
case EC_ERR_READMEM: reason = "EC memory read failed"; break;
case EC_ERR_EC_COMMAND: reason = "EC command failed"; break;
case EC_ERR_INVALID_PARAM: reason = "Invalid parameter"; break;
default: reason = "Unknown error"; break;
}

throw std::runtime_error(msg + " (" + reason + ", code " + std::to_string(code) + ")");
}


bool ECController::is_on_ac() {
int ac;
int ret = ec_is_on_ac(&ac);
handle_error(ret, "Failed to read AC status");
return ac;
}

void ECController::auto_fan_control() {
int ret = ec_auto_fan_control();
handle_error(ret, "Failed to enable auto fan control");
}

void ECController::set_fan_duty(int duty) {
int ret = ec_set_fan_duty(duty);
handle_error(ret, "Failed to set fan duty");
}

float ECController::get_max_temperature() {
float t;
int ret = ec_get_max_temperature(&t);
handle_error(ret, "Failed to get max temperature");
return t;
}

float ECController::get_max_non_battery_temperature() {
float t;
int ret = ec_get_max_non_battery_temperature(&t);
handle_error(ret, "Failed to get non-battery temperature");
return t;
}
15 changes: 15 additions & 0 deletions src/bindings/ECController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include <stdexcept>
#include <string>

class ECController {
public:
bool is_on_ac();
void auto_fan_control();
void set_fan_duty(int duty);
float get_max_temperature();
float get_max_non_battery_temperature();

private:
void handle_error(int code, const std::string &msg);
};
29 changes: 29 additions & 0 deletions src/bindings/PyECController.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <pybind11/pybind11.h>
#include "ECController.h"

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)

namespace py = pybind11;

PYBIND11_MODULE(libectool_py, m) {
m.doc() = "Python bindings for ectool";

py::class_<ECController>(m, "ECController")
.def(py::init<>())
.def("is_on_ac", &ECController::is_on_ac, "Check if on AC power")
.def("auto_fan_control", &ECController::auto_fan_control, "Enable automatic fan control")
.def("set_fan_duty", &ECController::set_fan_duty,
"Set fan duty cycle (0-100)", py::arg("duty"))
.def("get_max_temperature", &ECController::get_max_temperature,
"Get max temperature")
.def("get_max_non_battery_temperature",
&ECController::get_max_non_battery_temperature,
"Get max non-battery temperature");

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}
31 changes: 0 additions & 31 deletions src/bindings/libectool_py.cc

This file was deleted.

Loading