Skip to content

Commit 1a766f4

Browse files
committed
Add thermal_set_thresholds
1 parent 55ff463 commit 1a766f4

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

cros_ec_python/commands/thermal.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ def thermal_get_thresholds(
3636
:param adjust: The adjustment to apply to the temperature. Default is -273 to convert from Kelvin to Celsius.
3737
:return: A list of tuples containing the (warn, high, halt) thresholds.
3838
"""
39-
data = struct.pack("<B", sensor_num)
39+
data = struct.pack("<I", sensor_num)
4040
thresh_count: Final = EcTempThresholds.EC_TEMP_THRESH_COUNT.value
41-
resp = ec.command(1, EC_CMD_THERMAL_GET_THRESHOLD, 1, 4*thresh_count + 4*thresh_count + 4 + 4, data)
41+
resp = ec.command(
42+
1,
43+
EC_CMD_THERMAL_GET_THRESHOLD,
44+
4,
45+
4 * thresh_count + 4 * thresh_count + 4 + 4,
46+
data,
47+
)
4248
config = struct.unpack(f"<{thresh_count}I{thresh_count}III", resp)
4349
return {
4450
"temp_host": [(i + adjust) for i in config[0:thresh_count]],
@@ -50,6 +56,31 @@ def thermal_get_thresholds(
5056
}
5157

5258

59+
def thermal_set_thresholds(
60+
ec: CrosEcClass,
61+
sensor_num: int,
62+
config: dict[str, list[int | float] | int | float],
63+
adjust: int | float = 273,
64+
) -> None:
65+
"""
66+
Set the temperature thresholds for a given sensor.
67+
:param ec: The CrOS_EC object.
68+
:param sensor_num: The sensor number.
69+
:param config: A dictionary containing the threshold configuration.
70+
:param adjust: The adjustment to apply to the temperature. Default is 273 to convert from Celsius to Kelvin.
71+
"""
72+
thresh_count: Final = EcTempThresholds.EC_TEMP_THRESH_COUNT.value
73+
data = struct.pack(
74+
f"<I{thresh_count}I{thresh_count}III",
75+
sensor_num,
76+
*[int(i + adjust) for i in config["temp_host"]],
77+
*[int(i + adjust) for i in config["temp_host_release"]],
78+
int(config["temp_fan_off"] + adjust),
79+
int(config["temp_fan_max"] + adjust),
80+
)
81+
ec.command(1, EC_CMD_THERMAL_SET_THRESHOLD, len(data), 0, data)
82+
83+
5384
EC_CMD_THERMAL_AUTO_FAN_CTRL: Final = 0x0052
5485

5586

tests/thermal.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,34 @@
33

44
ec = get_cros_ec()
55

6+
# Some tests are commented out because I don't want to reconfigure the EC
7+
# on my laptop every time I run the tests.
8+
9+
610
class TestGetThresholds(unittest.TestCase):
711
def test(self):
812
resp = ec_thermal.thermal_get_thresholds(ec, 0)
913
print(type(self).__name__, "-", "Resp:", resp)
1014
self.assertIsInstance(resp, dict)
1115

16+
17+
# class TestSetThresholds(unittest.TestCase):
18+
# def test(self):
19+
# new_config = {
20+
# "temp_host": [70, 80, 120],
21+
# "temp_host_release": [50, 55, 115],
22+
# "temp_fan_off": 40,
23+
# "temp_fan_max": 70,
24+
# }
25+
# ec_thermal.thermal_set_thresholds(ec, 0, new_config)
26+
# resp = ec_thermal.thermal_get_thresholds(ec, 0)
27+
# print(type(self).__name__, "-", "Resp after set:", resp)
28+
# self.assertEqual(resp["temp_host"], new_config["temp_host"])
29+
# self.assertEqual(resp["temp_host_release"], new_config["temp_host_release"])
30+
# self.assertEqual(resp["temp_fan_off"], new_config["temp_fan_off"])
31+
# self.assertEqual(resp["temp_fan_max"], new_config["temp_fan_max"])
32+
33+
1234
class TestAutoFanControl(unittest.TestCase):
1335
def test_version0(self):
1436
ec_thermal.thermal_auto_fan_ctrl(ec)

0 commit comments

Comments
 (0)