-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm
executable file
·50 lines (42 loc) · 925 Bytes
/
pwm
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
#!/bin/bash
### Functions
function set_pwm () {
if [[ $# == 0 ]]; then
return
fi
PWM=$1
# Sanity checks
if ! [[ $PWM =~ ^-?[0-9]+(\.[0-9]+)?$ ]]; then
echo "PWM value not a number; ignored" 1>&2
return
fi
MILLIPWM=`bc <<< "scale=0; $PWM * 1000 / 1"`
if (( MILLIPWM > 100000
|| MILLIPWM < 0 )); then
echo "PWM value out of range; ignored." 1>&2
return
fi
PWMR=`bc <<< "scale=2; $PWM * 256 / 100"`
PWMR=`printf "%.0f" $PWMR` # Rounds here.
PWMR=`printf "0x%02x" $PWMR` # Hex here (for show).
if (( PWMR > 0xff )); then
PWMR=0xff
fi
./set 0x50 $PWMR
return
}
function get_pwm () {
PWMV=`./get 0x51`
if (( PWMV == 0xff )); then
PWM="100.00"
else
PWM=`bc <<< "scale=2; $(( PWMV )) * 100 / 256"`
fi
echo -e "PWM:\t$PWM\t%"
return
}
### Main flow
# Set value if requested
set_pwm $1
# Read current value
get_pwm