forked from linusg/rpi-backlight
/
__init__.py
276 lines (243 loc) 路 10.4 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import errno
import time
from contextlib import contextmanager
from enum import Enum
from os import PathLike
from pathlib import Path
from tempfile import gettempdir
from typing import Generator, Union, Optional
from . import utils
__author__ = "Linus Groh"
__version__ = "2.6.0"
__all__ = ["Backlight", "BoardType"]
class BoardType(Enum):
"""Enum to specify a board type in the :class:`~rpi_backlight.Backlight` constructor."""
#: Raspberry Pi
RASPBERRY_PI5 = 1
#: Raspberry Pi
RASPBERRY_PI = 2
#: Tinker Board
TINKER_BOARD = 3
#: Tinker Board 2
TINKER_BOARD_2 = 4
#: Microsoft Surface RT
MICROSOFT_SURFACE_RT = 5
_BACKLIGHT_SYSFS_PATHS = {
BoardType.RASPBERRY_PI5: (
"/sys/class/backlight/6-0045/"
if Path("/sys/class/backlight/6-0045/").exists()
else "/sys/class/backlight/rpi_backlight/"
),
BoardType.RASPBERRY_PI: (
"/sys/class/backlight/10-0045/"
if Path("/sys/class/backlight/10-0045/").exists()
else "/sys/class/backlight/rpi_backlight/"
),
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/",
}
_EMULATOR_SYSFS_TMP_FILE_PATH = Path(gettempdir()) / "rpi-backlight-emulator.sysfs"
_EMULATOR_MAGIC_STRING = ":emulator:"
def _permission_denied() -> None:
raise PermissionError(
"You must either run this program as root or change the permissions "
"for the backlight access as described in README.md."
)
class Backlight:
"""Main class to access and control the display backlight power and brightness."""
def __init__(
self,
backlight_sysfs_path: Optional[Union[str, "PathLike[str]"]] = None,
board_type: BoardType = utils.detect_board_type() or BoardType.RASPBERRY_PI,
):
"""Set ``backlight_sysfs_path`` to ``":emulator:"`` to use with rpi-backlight-emulator."""
if not isinstance(board_type, BoardType):
raise TypeError(
f"board_type must be a member of the BoardType enum, got {type(board_type)}"
)
if not backlight_sysfs_path:
backlight_sysfs_path = _BACKLIGHT_SYSFS_PATHS[board_type]
elif backlight_sysfs_path == _EMULATOR_MAGIC_STRING:
if not _EMULATOR_SYSFS_TMP_FILE_PATH.exists():
raise RuntimeError(
f"Emulator seems to be not running, {_EMULATOR_SYSFS_TMP_FILE_PATH} not found"
)
backlight_sysfs_path = _EMULATOR_SYSFS_TMP_FILE_PATH.read_text()
# The emulator only knows about Raspberry Pi sysfs files
# (brightness, bl_power), ignore board_type
board_type = BoardType.RASPBERRY_PI
self._backlight_sysfs_path = Path(backlight_sysfs_path)
self._board_type = board_type
self._fade_duration = 0.0 # in seconds
if self._board_type in (BoardType.RASPBERRY_PI5, BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
self._max_brightness = self._get_value("max_brightness") # 255
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
):
self._max_brightness = 255
def _get_value(self, name: str) -> int:
try:
return int((self._backlight_sysfs_path / name).read_text())
except ValueError:
# Reading failed, sometimes file is empty when updating
# Try again
return self._get_value(name)
except (OSError, IOError) as e:
if e.errno == errno.EPERM:
_permission_denied()
raise e
def _set_value(self, name: str, value: int) -> None:
try:
(self._backlight_sysfs_path / name).write_text(str(value))
except (OSError, IOError) as e:
if e.errno == errno.EPERM:
_permission_denied()
raise e
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)
@contextmanager
def fade(self, duration: float) -> Generator:
"""Context manager for temporarily changing the fade duration.
>>> backlight = Backlight()
>>> with backlight.fade(duration=0.5):
... backlight.brightness = 1 # Fade to 100% brightness for 0.5s
...
>>> with backlight.fade(duration=0):
... backlight.brightness = 0 # Set to 0% brightness without fading, use if you have set `backlight.fade_duration` > 0
"""
old_duration = self.fade_duration
self.fade_duration = duration
yield
self.fade_duration = old_duration
@property
def fade_duration(self) -> float:
"""The brightness fade duration in seconds, defaults to 0.
Also see :meth:`~rpi_backlight.Backlight.fade`.
>>> backlight = Backlight()
>>> backlight.fade_duration # Fading is disabled by default
0
>>> backlight.fade_duration = 0.5 # Set to 500ms
:getter: Return the fade duration.
:setter: Set the fade duration.
:type: float
"""
return self._fade_duration
@fade_duration.setter
def fade_duration(self, duration: float) -> None:
"""Set the fade duration."""
# isinstance(True, int) is True, so additional check for bool.
if not isinstance(duration, (int, float)) or isinstance(duration, bool):
raise TypeError(f"value must be a number, got {type(duration)}")
if duration < 0:
raise ValueError(f"value must be >= 0, got {duration}")
self._fade_duration = duration
@property
def brightness(self) -> float:
"""The display brightness in range 0-100.
>>> backlight = Backlight()
>>> backlight.brightness # Display is at 50% brightness
50
>>> backlight.brightness = 100 # Set to full brightness
:getter: Return the display brightness.
:setter: Set the display brightness.
:type: float
"""
if self._board_type in (BoardType.RASPBERRY_PI5, BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
return self._normalize_brightness(self._get_value("actual_brightness"))
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
):
return self._normalize_brightness(self._get_value("tinker_mcu_bl"))
else:
raise RuntimeError("Invalid board type")
@brightness.setter
def brightness(self, value: float) -> None:
"""Set the display brightness."""
# isinstance(True, int) is True, so additional check for bool.
if not isinstance(value, (int, float)) or isinstance(value, bool):
raise TypeError(f"value must be a number, got {type(value)}")
if value < 0 or value > 100:
raise ValueError(f"value must be in range 0-100, got {value}")
if self.fade_duration > 0:
current_value = self.brightness
step = 1 if current_value < value else -1
diff = abs(value - current_value)
while (
0.0 <= current_value
and current_value != value
and current_value <= 100.0
):
current_value += step
if self._board_type in (
BoardType.RASPBERRY_PI5,
BoardType.RASPBERRY_PI,
BoardType.MICROSOFT_SURFACE_RT,
):
self._set_value(
"brightness", self._denormalize_brightness(current_value)
)
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
):
self._set_value(
"tinker_mcu_bl", self._denormalize_brightness(current_value)
)
else:
raise RuntimeError("Invalid board type")
time.sleep(self.fade_duration / diff)
else:
if self._board_type in (
BoardType.RASPBERRY_PI5,
BoardType.RASPBERRY_PI,
BoardType.MICROSOFT_SURFACE_RT,
):
self._set_value("brightness", self._denormalize_brightness(value))
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
):
self._set_value("tinker_mcu_bl", self._denormalize_brightness(value))
else:
raise RuntimeError("Invalid board type")
@property
def power(self) -> bool:
"""Turn the display on and off.
>>> backlight = Backlight()
>>> backlight.power # Display is on
True
>>> backlight.power = False # Turn display off
:getter: Return whether the display is powered on or off.
:setter: Set the display power on or off.
:type: bool
"""
if self._board_type in (BoardType.RASPBERRY_PI5, BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
# 0 is on, 1 is off
return not self._get_value("bl_power")
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
):
return bool(self._get_value("tinker_mcu_bl"))
else:
raise RuntimeError("Invalid board type")
@power.setter
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_PI5, BoardType.RASPBERRY_PI, BoardType.MICROSOFT_SURFACE_RT):
# 0 is on, 1 is off
self._set_value("bl_power", int(not on))
elif (
self._board_type == BoardType.TINKER_BOARD
or self._board_type == BoardType.TINKER_BOARD_2
):
self._set_value("tinker_mcu_bl", 255 if on else 0)
else:
raise RuntimeError("Invalid board type")