-
Notifications
You must be signed in to change notification settings - Fork 5
/
blinkvoltage
134 lines (119 loc) · 4.37 KB
/
blinkvoltage
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
#!/bin/bash -u
# Script_name : blinkvoltage
# Author : Bert Lindeman
# Description : Set the left EV3-led according to the battery voltage
# Version : v1.3
# Date : 2014-11-04
# Usage : blinkvoltage [color]
# Notes : o color is [ none | red | green | amber ]
# o very good to add this script to a crontab
# so you get warned by a blinking led if power gets low
# Copyright : 2014 Bert Lindeman
# License : GPL-3.0+
#
#============================================================================
#
# Changelog
# Date By Description
# 20141104 BL prepare for release v1.3
# 20140914 BL Also use the actual voltage to set the blinkling
# 20140826 BL o Add script header
# o try to prevent abmber blinking flipflop red/green
# 20151230 BL Adapt to image 20151230. New file names for leds.
#
#============================================================================
side=left # side to use for the blinking
v0=6000000 # low
v1=7100000
v2=7500000
v3=8000000 # very good
get_delay() {
# purpose: Determine delay_on and delay_off values depending on current voltage
# make the sum 1000 so a one second interval
pctOK=$(bc <<< "scale=0; ($voltage * 100) / ${v3}")
if [ $pctOK -ge 95 ]; then # 7.6V
intens=10
# dead slow
delay_on=10
delay_off=990
elif [ $pctOK -ge 85 ]; then # 6.8V
intens=127
# 50/50 but a bit faster
delay_on=250
delay_off=250
elif [ $pctOK -ge 75 ]; then # 6.0V
intens=255
#rapid blink
delay_on=50
delay_off=150
else
intens=255
#rapid blink
delay_on=20
delay_off=180
fi
# echo "\$delay_off=$delay_off \$delay_on=$delay_on \$intens=$intens"
}
# to prevent flipflopping red<-->green set triggers close to eachother
# not ALWAYS successful. Do not have a way (yet) to ensure
# AMBER leds and not flip=flopping green/red
setAmber() {
echo timer > /sys/class/leds/ev3:$side:green:ev3dev/trigger
echo timer > /sys/class/leds/ev3:$side:red:ev3dev/trigger
echo 500 > /sys/class/leds/ev3:$side:green:ev3dev/delay_on
echo 500 > /sys/class/leds/ev3:$side:red:ev3dev/delay_on
echo 500 > /sys/class/leds/ev3:$side:green:ev3dev/delay_off
echo 500 > /sys/class/leds/ev3:$side:red:ev3dev/delay_off
echo $delay_off > /sys/class/leds/ev3:$side:green:ev3dev/delay_off
echo $delay_off > /sys/class/leds/ev3:$side:red:ev3dev/delay_off
echo $delay_on > /sys/class/leds/ev3:$side:green:ev3dev/delay_on
echo $delay_on > /sys/class/leds/ev3:$side:red:ev3dev/delay_on
echo $intens > /sys/class/leds/ev3:$side:green:ev3dev/brightness
echo $intens > /sys/class/leds/ev3:$side:red:ev3dev/brightness
}
setRed() {
echo timer > /sys/class/leds/ev3:$side:red:ev3dev/trigger
echo $intens > /sys/class/leds/ev3:$side:red:ev3dev/brightness
echo $delay_on > /sys/class/leds/ev3:$side:red:ev3dev/delay_on
echo $delay_off > /sys/class/leds/ev3:$side:red:ev3dev/delay_off
}
setGrn() {
echo timer > /sys/class/leds/ev3:$side:green:ev3dev/trigger
echo $intens > /sys/class/leds/ev3:$side:green:ev3dev/brightness
echo $delay_on > /sys/class/leds/ev3:$side:green:ev3dev/delay_on
echo $delay_off > /sys/class/leds/ev3:$side:green:ev3dev/delay_off
}
# make sure we do have the variables
trigger=""
voltage=0
# reset BOTH red and green leds unless we want amber
if [ "$#" -lt 1 ]; then
trigger="none"
else
if [ "$#" -ge 1 ]; then
trigger=$1
if [ "$#" -ge 2 ]; then
voltage=$2
fi
fi
fi
# first set all left colors OFF
ledbase="/sys/class/leds/ev3:$side:red:ev3dev"
echo none > $ledbase/trigger
echo 0 > $ledbase/brightness
ledbase="/sys/class/leds/ev3:$side:green:ev3dev"
echo none > $ledbase/trigger
echo 0 > $ledbase/brightness
if [ "$trigger" == none ]; then
exit 0 # a good point to leave here leds have been set already
elif [ "$trigger" == "amber" ]; then
get_delay
setAmber $delay_on $delay_off
elif [ "$trigger" == "red" ]; then
get_delay
setRed $delay_on $delay_off
elif [ "$trigger" == "green" ]; then
get_delay
setGrn $delay_on $delay_off
fi
exit 0