Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
N
nsbin
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nico Schottelius
nsbin
Commits
6ab66655
Commit
6ab66655
authored
Mar 30, 2018
by
Nico Schottelius
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add functioning cbacklight
parent
762ea6bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
137 additions
and
36 deletions
+137
-36
brightness
brightness
+0
-36
cbackblight
cbackblight
+137
-0
No files found.
brightness
deleted
100755 → 0
View file @
762ea6bc
#!/usr/bin/python3
import
glob
import
os
import
argparse
basepath
=
"/sys/class/backlight/"
def
get_dirs
():
return
glob
.
glob
(
"{}*"
.
format
(
basepath
)):
def
get_driver_from_dir
(
directory
):
regexp
=
r"{}(?P<driver>.+)"
.
format
(
basepath
)
return
re
.
match
(
regexp
,
directory
).
groupdict
()[
'driver'
]
for
dir
in
valf
=
os
.
path
.
join
(
dir
,
"brightness"
)
maxf
=
os
.
path
.
join
(
dir
,
"max_brightness"
)
with
open
(
valf
,
"r"
)
as
fd
:
val
=
int
(
""
.
join
(
fd
.
readlines
()))
with
open
(
maxf
,
"r"
)
as
fd
:
maxval
=
int
(
""
.
join
(
fd
.
readlines
()))
percent
=
(
val
/
maxval
)
*
100
print
(
"{} {} = {:.2f}%"
.
format
(
val
,
maxval
,
percent
))
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
description
=
'cbacklight'
)
parser
.
add_argument
(
'--inc'
,
help
=
'Increment by percentage (points)'
)
parser
.
add_argument
(
'--dec'
,
help
=
'Decrement by percentage (points)'
)
parser
.
add_argument
(
'--set'
,
help
=
'Set to percentage'
)
cbackblight
0 → 100644
View file @
6ab66655
#!/usr/bin/python3
# Nico Schottelius
# 2018-03-30
# Copying: GPLv3
#
#
# Set / increase brightness to sysfs directly
#
import
glob
import
os
import
argparse
import
re
import
sys
class
BackLight
(
object
):
basepath
=
"/sys/class/backlight/"
def
__init__
(
self
,
name
):
self
.
name
=
name
self
.
dirname
=
os
.
path
.
join
(
self
.
basepath
,
name
)
self
.
brightness_file
=
os
.
path
.
join
(
self
.
dirname
,
"brightness"
)
self
.
max_brightness_file
=
os
.
path
.
join
(
self
.
dirname
,
"max_brightness"
)
@
staticmethod
def
read_value
(
filename
):
with
open
(
filename
,
"r"
)
as
fd
:
val
=
int
(
""
.
join
(
fd
.
readlines
()))
return
val
@
staticmethod
def
write_value
(
filename
,
value
):
try
:
with
open
(
filename
,
"w"
)
as
fd
:
fd
.
write
(
str
(
value
))
except
PermissionError
as
e
:
print
(
"Cannot write to {}: {}"
.
format
(
filename
,
e
))
sys
.
exit
(
1
)
@
classmethod
def
get_dirs
(
cls
):
return
glob
.
glob
(
"{}*"
.
format
(
cls
.
basepath
))
@
classmethod
def
get_driver_from_dir
(
cls
,
directory
):
regexp
=
r"{}(?P<driver>.+)"
.
format
(
cls
.
basepath
)
return
re
.
match
(
regexp
,
directory
).
groupdict
()[
'driver'
]
@
classmethod
def
get_drivers
(
cls
):
return
[
cls
(
cls
.
get_driver_from_dir
(
x
))
for
x
in
cls
.
get_dirs
()
]
def
get_value
(
self
):
return
self
.
read_value
(
self
.
brightness_file
)
def
get_max_value
(
self
):
return
self
.
read_value
(
self
.
max_brightness_file
)
def
get_percentage
(
self
):
val
=
self
.
get_value
()
maxval
=
self
.
get_max_value
()
percent
=
(
val
/
maxval
)
*
100
return
percent
def
get_percentage_points
(
self
,
percent
):
"""Return integer equivalent to percent"""
points
=
int
((
percent
*
self
.
get_max_value
())
/
100
)
return
points
def
inc_percent
(
self
,
percent
):
points
=
self
.
get_percentage_points
(
percent
)
new_value
=
self
.
get_value
()
+
points
self
.
set_value
(
new_value
)
def
dec_percent
(
self
,
percent
):
points
=
self
.
get_percentage_points
(
percent
)
new_value
=
self
.
get_value
()
-
points
self
.
set_value
(
new_value
)
def
set_percent
(
self
,
percent
):
new_value
=
int
((
percent
*
self
.
get_max_value
())
/
100
)
self
.
set_value
(
new_value
)
def
set_value
(
self
,
value
):
if
value
<
0
:
value
=
0
if
value
>
self
.
get_max_value
():
value
=
self
.
get_max_value
()
self
.
write_value
(
self
.
brightness_file
,
value
)
def
is_readable
(
self
):
return
os
.
access
(
self
.
max_brightness_file
,
os
.
R_OK
)
def
__str__
(
self
):
return
self
.
name
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
description
=
'cbacklight'
)
group
=
parser
.
add_mutually_exclusive_group
()
group
.
add_argument
(
'--inc'
,
help
=
'Increment by percentage (points)'
)
group
.
add_argument
(
'--dec'
,
help
=
'Decrement by percentage (points)'
)
group
.
add_argument
(
'--set'
,
help
=
'Set to percentage'
)
parser
.
add_argument
(
'--get'
,
help
=
'Get percentage (default)'
,
action
=
'store_true'
)
args
=
parser
.
parse_args
()
anyarg
=
args
.
inc
or
args
.
dec
or
args
.
set
drivers
=
BackLight
.
get_drivers
()
method
=
None
if
args
.
inc
:
method
=
"inc_percent"
val
=
args
.
inc
if
args
.
dec
:
method
=
"dec_percent"
val
=
args
.
dec
if
args
.
set
:
method
=
"set_percent"
val
=
args
.
set
if
method
:
for
d
in
drivers
:
func
=
getattr
(
d
,
method
)
func
(
int
(
val
))
if
not
anyarg
or
args
.
get
:
for
d
in
drivers
:
print
(
"{}: {:.2f}%"
.
format
(
d
,
d
.
get_percentage
()))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment