-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysfsbatmon
executable file
·168 lines (137 loc) · 4.21 KB
/
sysfsbatmon
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
#!/bin/bash
# sysfsbatmon: A simple script to display battery status on screen
#
# Author: Dimitris Lampridis <dlampridis_at_gmail.com>
# (C) Copyright 2010 Dimitris Lampridis
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------
# configuration settings below, adjust as necessary
####################
# SYSFS SETTINGS #
####################
# Set this to your power supply's sysfs path
PSUPATH="/sys/class/power_supply"
# Change this to match the string assigned to your battery
# and ac adapter by sysfs. To find out the available strings,
# have a look at the SYSFSPATH subfolder names.
BTNAME="BAT1"
ACNAME="ACAD"
####################
# OSD SETTINGS #
####################
# Font name and size (default = FreeSerif, size 64)
# Try reducing the size of the OSD is too large for your
# desktop resolution
FONT="FreeSerif"
FONTSIZE="36"
# Character to use for OSD display (default = "|")
OSDCHAR="|"
# Horizontal scaling factor
# Try reducing this if the OSD is so long that it continues
# on a second line (default = 2)
HORIZSCALE="1"
# OSD position, X and Y (default x=10, y=-10)
OSDX="10"
OSDY="-10"
# OSD Fade in/out, in milliseconds (default = 0ms)
FADEIN="0"
FADEOUT="0"
#####################
# PROGRAM LOCATIONS #
#####################
# Adjust the location of required binaries (if needed)
# Should be OK for most Linux distributions
AOSD=/usr/bin/aosd_cat
AWK=/usr/bin/awk
GREP=/bin/grep
ECHO=/bin/echo
CAT=/bin/cat
KILLALL=/usr/bin/killall
TR=/usr/bin/tr
SEQ=/usr/bin/seq
PRINTF=/usr/bin/printf
EXPR=/usr/bin/expr
# end of configuration settings, nothing requires changing below this line
#-------------------------------------------------------------------------
# check that all binaries exist
for i in $AOSD $AWK $GREP $ECHO $CAT $KILLALL $TR $SEQ $PRINTF $EXPR
do
if [ ! -e $i ]; then
$ECHO $i" does not exist"
$ECHO "please install it if missing, and/or adjust the configuration settings to point to the correct location"
exit
fi
done
ACPATH=$PSUPATH/$ACNAME
ACSTATPATH=$ACPATH/online
for i in $ACPATH $ACSTATPATH
do
if [ ! -e $i ]; then
$ECHO $i" does not exist"
$ECHO "please adjust script settings"
exit
else
# get AC status
ACSTATUS=$($CAT $ACSTATPATH)
fi
done
BTPATH=$PSUPATH/$BTNAME
BTSTATPATH=$BTPATH/present
BTPWRNOWPATH=$BTPATH/energy_now
BTPWRFULPATH=$BTPATH/energy_full
if [ -e $BTPATH ]; then
for i in $BTSTATPATH $BTPWRFULPATH $BTPWRNOWPATH
do
if [ ! -e $i ]; then
$ECHO $i" does not exist"
$ECHO "please adjust script settings"
exit
fi
done
# get battery status
BTSTATUS=$($CAT $BTSTATPATH)
BTPWRNOW=$($CAT $BTPWRNOWPATH)
BTPWRFUL=$($CAT $BTPWRFULPATH)
# determine battery percentage and adjust text color
BTPERCENT=$($EXPR \( 100 \* $BTPWRNOW \) \/ $BTPWRFUL)
if [ $BTPERCENT -ge 50 ]; then
GREEN=255
RED=$($EXPR \( 5 \* \( 100 - $BTPERCENT \) \) )
else
RED=255
GREEN=$($EXPR \( 5 \* $BTPERCENT \) )
fi
OSDCOLOR=$($PRINTF "#%.2x%.2x00" $RED $GREEN)
# determine number of characters to draw on the OSD
STEPS=$($EXPR $BTPERCENT \/ $HORIZSCALE)
# "draw" the OSD characters on a temporary string
OSDI=$BTPERCENT"%"
if [ $ACSTATUS == "1" ]; then
OSDI=$OSDI" + "
else
OSDI=$OSDI" - "
fi
for i in $($SEQ 1 $STEPS)
do
OSDI=$OSDI$OSDCHAR
done
else
OSDCOLOR="green"
OSDI="on AC, no battery present..."
fi
# kill any existing instances of AOSD_CAT
$KILLALL aosd_cat &> /dev/null
# Print the temporary string on screen with AOSD_CAT
$ECHO "$OSDI" | $AOSD -n "$FONT $FONTSIZE" -R $OSDCOLOR -o $FADEOUT -f $FADEIN -x $OSDX -y $OSDY