Skip to content

Commit

Permalink
Add support for Chromebooks by converting Surface RT to 'generic'
Browse files Browse the repository at this point in the history
  • Loading branch information
archisman-panigrahi authored Sep 23, 2024
1 parent 4835019 commit 4e998a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
## Requirements

- A **Raspberry Pi or ASUS Tinker Board** including a correctly assembled **7" touch display v1.1 or higher**
(look on the display's circuit board to see its version), or a [**Surface RT**](https://openrt.gitbook.io/open-surfacert/surface-rt/linux/root-filesystem/distros/raspberry-pi-os) running a Linux-based OS
(look on the display's circuit board to see its version), or a [**Surface RT**](https://openrt.gitbook.io/open-surfacert/surface-rt/linux/root-filesystem/distros/raspberry-pi-os) or an [**ARM Chromebook**](https://askubuntu.com/q/1506894) running a Linux-based OS
- Python 3.7+
- Optional: Raspberry Pi: ``pygobject`` for the GUI, already installed on a recent Raspbian
- Optional: Tinker Board: ``gir1.2-gtk-3.0`` for the GUI install
Expand Down
37 changes: 26 additions & 11 deletions rpi_backlight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BoardType(Enum):
TINKER_BOARD = 2
#: Tinker Board 2
TINKER_BOARD_2 = 3
#: Microsoft Surface RT
MICROSOFT_SURFACE_RT = 4
#: Microsoft Surface RT or Google Chromebooks
GENERIC = 4


_BACKLIGHT_SYSFS_PATHS = {
Expand All @@ -35,7 +35,7 @@ class BoardType(Enum):
),
BoardType.TINKER_BOARD: "/sys/devices/platform/ff150000.i2c/i2c-3/3-0045/",
BoardType.TINKER_BOARD_2: "/sys/devices/platform/ff3e0000.i2c/i2c-8/8-0045/",
BoardType.MICROSOFT_SURFACE_RT: "/sys/class/backlight/backlight/",
BoardType.GENERIC: "/sys/class/backlight/backlight/",
}
_EMULATOR_SYSFS_TMP_FILE_PATH = Path(gettempdir()) / "rpi-backlight-emulator.sysfs"
_EMULATOR_MAGIC_STRING = ":emulator:"
Expand Down Expand Up @@ -78,8 +78,12 @@ def __init__(
self._board_type = board_type
self._fade_duration = 0.0 # in seconds

if self._board_type in (BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
self._max_brightness = self._get_value("max_brightness") # 255
if self._board_type in (
BoardType.RASPBERRY_PI,
BoardType.GENERIC,
):
# This is 255 in RPi, but maybe different in other devices
self._max_brightness = self._get_value("max_brightness")
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
Expand Down Expand Up @@ -110,7 +114,9 @@ def _normalize_brightness(self, value: float) -> int:
return max(min(100, int(round(value / self._max_brightness * 100))), 0)

def _denormalize_brightness(self, value: float) -> int:
return max(min(255, int(round(value * self._max_brightness / 100))), 0)
return max(
min(self._max_brightness, int(round(value * self._max_brightness / 100))), 0
)

@contextmanager
def fade(self, duration: float) -> Generator:
Expand Down Expand Up @@ -167,7 +173,10 @@ def brightness(self) -> float:
:setter: Set the display brightness.
:type: float
"""
if self._board_type in (BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
if self._board_type in (
BoardType.RASPBERRY_PI,
BoardType.GENERIC,
):
return self._normalize_brightness(self._get_value("actual_brightness"))
elif (
self._board_type == BoardType.TINKER_BOARD
Expand Down Expand Up @@ -197,7 +206,7 @@ def brightness(self, value: float) -> None:
current_value += step
if self._board_type in (
BoardType.RASPBERRY_PI,
BoardType.MICROSOFT_SURFACE_RT,
BoardType.GENERIC,
):
self._set_value(
"brightness", self._denormalize_brightness(current_value)
Expand All @@ -215,7 +224,7 @@ def brightness(self, value: float) -> None:
else:
if self._board_type in (
BoardType.RASPBERRY_PI,
BoardType.MICROSOFT_SURFACE_RT,
BoardType.GENERIC,
):
self._set_value("brightness", self._denormalize_brightness(value))
elif (
Expand All @@ -239,7 +248,10 @@ def power(self) -> bool:
:setter: Set the display power on or off.
:type: bool
"""
if self._board_type in (BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
if self._board_type in (
BoardType.RASPBERRY_PI,
BoardType.GENERIC,
):
# 0 is on, 1 is off
return not self._get_value("bl_power")
elif (
Expand All @@ -255,7 +267,10 @@ def power(self, on: bool) -> None:
"""Set the display power on or off."""
if not isinstance(on, bool):
raise TypeError(f"value must be a bool, got {type(on)}")
if self._board_type in (BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
if self._board_type in (
BoardType.RASPBERRY_PI,
BoardType.GENERIC,
):
# 0 is on, 1 is off
self._set_value("bl_power", int(not on))
elif (
Expand Down
4 changes: 2 additions & 2 deletions rpi_backlight/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"raspberry-pi": BoardType.RASPBERRY_PI,
"tinker-board": BoardType.TINKER_BOARD,
"tinker-board-2": BoardType.TINKER_BOARD_2,
"microsoft-surface-rt": BoardType.MICROSOFT_SURFACE_RT,
"generic": BoardType.GENERIC,
}

BOARD_TYPE_TO_STRING = {
BoardType.RASPBERRY_PI: "raspberry-pi",
BoardType.TINKER_BOARD: "tinker-board",
BoardType.TINKER_BOARD_2: "tinker-board-2",
BoardType.MICROSOFT_SURFACE_RT: "microsoft-surface-rt",
BoardType.GENERIC: "generic",
}


Expand Down
5 changes: 4 additions & 1 deletion rpi_backlight/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def detect_board_type() -> Optional["BoardType"]:
return BoardType.RASPBERRY_PI
# Microsoft Surface RT starts with Microsoft Surface RT
elif "Microsoft Surface RT" in model:
return BoardType.MICROSOFT_SURFACE_RT
return BoardType.GENERIC
# Google Chromebooks start with Google
elif "Google" in model:
return BoardType.GENERIC
else:
return None

Expand Down

0 comments on commit 4e998a1

Please sign in to comment.