cros_ec_python.commands.leds

LED control commands

 1"""
 2LED control commands
 3"""
 4
 5from typing import Final
 6from enum import Enum, auto
 7import struct
 8from ..baseclass import CrosEcClass
 9from ..constants.COMMON import *
10
11EC_CMD_LED_CONTROL: Final = 0x0029
12
13
14class EcLedId(Enum):
15    # LED to indicate battery state of charge
16    EC_LED_ID_BATTERY_LED = 0
17    #
18    # LED to indicate system power state (on or in suspend).
19    # May be on power button or on C-panel.
20
21    EC_LED_ID_POWER_LED = auto()
22    # LED on power adapter or its plug
23    EC_LED_ID_ADAPTER_LED = auto()
24    # LED to indicate left side
25    EC_LED_ID_LEFT_LED = auto()
26    # LED to indicate right side
27    EC_LED_ID_RIGHT_LED = auto()
28    # LED to indicate recovery mode with HW_REINIT
29    EC_LED_ID_RECOVERY_HW_REINIT_LED = auto()
30    # LED to indicate sysrq debug mode.
31    EC_LED_ID_SYSRQ_DEBUG_LED = auto()
32
33    EC_LED_ID_COUNT = auto()
34
35
36# LED control flags
37EC_LED_FLAGS_QUERY = BIT(0)  # Query LED capability only
38EC_LED_FLAGS_AUTO = BIT(1)  # Switch LED back to automatic control
39
40
41class EcLedColors(Enum):
42    EC_LED_COLOR_RED = 0
43    EC_LED_COLOR_GREEN = auto()
44    EC_LED_COLOR_BLUE = auto()
45    EC_LED_COLOR_YELLOW = auto()
46    EC_LED_COLOR_WHITE = auto()
47    EC_LED_COLOR_AMBER = auto()
48
49    EC_LED_COLOR_COUNT = auto()
50
51
52def led_control(ec: CrosEcClass, led_id: EcLedId, flags: UInt8, brightnesses: list[UInt8]) -> list[UInt8]:
53    """
54    Control an LED
55    :param ec: The CrOS_EC object.
56    :param led_id: The LED to control.
57    :param flags: Control flags. See EC_LED_FLAGS_*.
58    :param brightnesses: Brightness values for each color. (6 colors, see EcLedColors)
59    :return: The available brightness value range for each color.
60    """
61    data = struct.pack(f"<BB{EcLedColors.EC_LED_COLOR_COUNT.value}B", led_id.value, flags, *brightnesses)
62    resp = ec.command(1, EC_CMD_LED_CONTROL, len(data), EcLedColors.EC_LED_COLOR_COUNT.value, data)
63    return list(resp)
64
65
66def led_control_set_color(ec: CrosEcClass, led_id: EcLedId, brightness: UInt8, color: EcLedColors) -> list[UInt8]:
67    """
68    Set the color of an LED
69    :param ec: The CrOS_EC object.
70    :param led_id: The LED to control.
71    :param brightness: Brightness value.
72    :param color: Color to set the LED to.
73    :return: The available brightness value range for each color.
74    """
75    brightnesses = [0] * EcLedColors.EC_LED_COLOR_COUNT.value
76    brightnesses[color.value] = brightness
77    return led_control(ec, led_id, 0, brightnesses)
78
79
80def led_control_get_max_values(ec: CrosEcClass, led_id: EcLedId) -> list[UInt8]:
81    """
82    Get the available brightness value range for each color of an LED
83    :param ec: The CrOS_EC object.
84    :param led_id: The LED to get the brightness values of.
85    :return: The available brightness value range for each color.
86    """
87    return led_control(ec, led_id, EC_LED_FLAGS_QUERY, [0] * EcLedColors.EC_LED_COLOR_COUNT.value)
88
89
90def led_control_set_auto(ec: CrosEcClass, led_id: EcLedId) -> list[UInt8]:
91    """
92    Set an LED back to automatic control
93    :param ec: The CrOS_EC object.
94    :param led_id: The LED to get the brightness values of.
95    :return: The available brightness value range for each color.
96    """
97    return led_control(ec, led_id, EC_LED_FLAGS_AUTO, [0] * EcLedColors.EC_LED_COLOR_COUNT.value)
EC_CMD_LED_CONTROL: Final = 41
class EcLedId(enum.Enum):
15class EcLedId(Enum):
16    # LED to indicate battery state of charge
17    EC_LED_ID_BATTERY_LED = 0
18    #
19    # LED to indicate system power state (on or in suspend).
20    # May be on power button or on C-panel.
21
22    EC_LED_ID_POWER_LED = auto()
23    # LED on power adapter or its plug
24    EC_LED_ID_ADAPTER_LED = auto()
25    # LED to indicate left side
26    EC_LED_ID_LEFT_LED = auto()
27    # LED to indicate right side
28    EC_LED_ID_RIGHT_LED = auto()
29    # LED to indicate recovery mode with HW_REINIT
30    EC_LED_ID_RECOVERY_HW_REINIT_LED = auto()
31    # LED to indicate sysrq debug mode.
32    EC_LED_ID_SYSRQ_DEBUG_LED = auto()
33
34    EC_LED_ID_COUNT = auto()
EC_LED_ID_BATTERY_LED = <EcLedId.EC_LED_ID_BATTERY_LED: 0>
EC_LED_ID_POWER_LED = <EcLedId.EC_LED_ID_POWER_LED: 1>
EC_LED_ID_ADAPTER_LED = <EcLedId.EC_LED_ID_ADAPTER_LED: 2>
EC_LED_ID_LEFT_LED = <EcLedId.EC_LED_ID_LEFT_LED: 3>
EC_LED_ID_RIGHT_LED = <EcLedId.EC_LED_ID_RIGHT_LED: 4>
EC_LED_ID_RECOVERY_HW_REINIT_LED = <EcLedId.EC_LED_ID_RECOVERY_HW_REINIT_LED: 5>
EC_LED_ID_SYSRQ_DEBUG_LED = <EcLedId.EC_LED_ID_SYSRQ_DEBUG_LED: 6>
EC_LED_ID_COUNT = <EcLedId.EC_LED_ID_COUNT: 7>
EC_LED_FLAGS_QUERY = 1
EC_LED_FLAGS_AUTO = 2
class EcLedColors(enum.Enum):
42class EcLedColors(Enum):
43    EC_LED_COLOR_RED = 0
44    EC_LED_COLOR_GREEN = auto()
45    EC_LED_COLOR_BLUE = auto()
46    EC_LED_COLOR_YELLOW = auto()
47    EC_LED_COLOR_WHITE = auto()
48    EC_LED_COLOR_AMBER = auto()
49
50    EC_LED_COLOR_COUNT = auto()
EC_LED_COLOR_RED = <EcLedColors.EC_LED_COLOR_RED: 0>
EC_LED_COLOR_GREEN = <EcLedColors.EC_LED_COLOR_GREEN: 1>
EC_LED_COLOR_BLUE = <EcLedColors.EC_LED_COLOR_BLUE: 2>
EC_LED_COLOR_YELLOW = <EcLedColors.EC_LED_COLOR_YELLOW: 3>
EC_LED_COLOR_WHITE = <EcLedColors.EC_LED_COLOR_WHITE: 4>
EC_LED_COLOR_AMBER = <EcLedColors.EC_LED_COLOR_AMBER: 5>
EC_LED_COLOR_COUNT = <EcLedColors.EC_LED_COLOR_COUNT: 6>
def led_control( ec: cros_ec_python.baseclass.CrosEcClass, led_id: EcLedId, flags: int, brightnesses: list[int]) -> list[int]:
53def led_control(ec: CrosEcClass, led_id: EcLedId, flags: UInt8, brightnesses: list[UInt8]) -> list[UInt8]:
54    """
55    Control an LED
56    :param ec: The CrOS_EC object.
57    :param led_id: The LED to control.
58    :param flags: Control flags. See EC_LED_FLAGS_*.
59    :param brightnesses: Brightness values for each color. (6 colors, see EcLedColors)
60    :return: The available brightness value range for each color.
61    """
62    data = struct.pack(f"<BB{EcLedColors.EC_LED_COLOR_COUNT.value}B", led_id.value, flags, *brightnesses)
63    resp = ec.command(1, EC_CMD_LED_CONTROL, len(data), EcLedColors.EC_LED_COLOR_COUNT.value, data)
64    return list(resp)

Control an LED

Parameters
  • ec: The CrOS_EC object.
  • led_id: The LED to control.
  • flags: Control flags. See EC_LED_FLAGS_*.
  • brightnesses: Brightness values for each color. (6 colors, see EcLedColors)
Returns

The available brightness value range for each color.

def led_control_set_color( ec: cros_ec_python.baseclass.CrosEcClass, led_id: EcLedId, brightness: int, color: EcLedColors) -> list[int]:
67def led_control_set_color(ec: CrosEcClass, led_id: EcLedId, brightness: UInt8, color: EcLedColors) -> list[UInt8]:
68    """
69    Set the color of an LED
70    :param ec: The CrOS_EC object.
71    :param led_id: The LED to control.
72    :param brightness: Brightness value.
73    :param color: Color to set the LED to.
74    :return: The available brightness value range for each color.
75    """
76    brightnesses = [0] * EcLedColors.EC_LED_COLOR_COUNT.value
77    brightnesses[color.value] = brightness
78    return led_control(ec, led_id, 0, brightnesses)

Set the color of an LED

Parameters
  • ec: The CrOS_EC object.
  • led_id: The LED to control.
  • brightness: Brightness value.
  • color: Color to set the LED to.
Returns

The available brightness value range for each color.

def led_control_get_max_values( ec: cros_ec_python.baseclass.CrosEcClass, led_id: EcLedId) -> list[int]:
81def led_control_get_max_values(ec: CrosEcClass, led_id: EcLedId) -> list[UInt8]:
82    """
83    Get the available brightness value range for each color of an LED
84    :param ec: The CrOS_EC object.
85    :param led_id: The LED to get the brightness values of.
86    :return: The available brightness value range for each color.
87    """
88    return led_control(ec, led_id, EC_LED_FLAGS_QUERY, [0] * EcLedColors.EC_LED_COLOR_COUNT.value)

Get the available brightness value range for each color of an LED

Parameters
  • ec: The CrOS_EC object.
  • led_id: The LED to get the brightness values of.
Returns

The available brightness value range for each color.

def led_control_set_auto( ec: cros_ec_python.baseclass.CrosEcClass, led_id: EcLedId) -> list[int]:
91def led_control_set_auto(ec: CrosEcClass, led_id: EcLedId) -> list[UInt8]:
92    """
93    Set an LED back to automatic control
94    :param ec: The CrOS_EC object.
95    :param led_id: The LED to get the brightness values of.
96    :return: The available brightness value range for each color.
97    """
98    return led_control(ec, led_id, EC_LED_FLAGS_AUTO, [0] * EcLedColors.EC_LED_COLOR_COUNT.value)

Set an LED back to automatic control

Parameters
  • ec: The CrOS_EC object.
  • led_id: The LED to get the brightness values of.
Returns

The available brightness value range for each color.