Skip to content

Commit 5ada201

Browse files
authored
Merge pull request #29 from dkramer95/sensorLimit
Add sensor output limit -n option
2 parents 3e2a967 + 1ef0d7b commit 5ada201

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

scripts/termux-sensor

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ show_usage () {
1111
echo " -l, list Show list of available sensors"
1212
echo " -s, sensors [,,,] Sensors to listen to (can contain just partial name)"
1313
echo " -d, delay [ms] Delay time in milliseconds before receiving new sensor update"
14+
echo " -n, limit [num] Number of times to read sensor(s) (default: continuous) (min: 1)"
1415
exit 0
1516
}
1617

@@ -28,6 +29,7 @@ ALL_SENSORS_FLAG=2
2829
LIST_FLAG=4
2930
CLEANUP_FLAG=8
3031
DELAY_FLAG=16
32+
LIMIT_FLAG=32
3133
FLAGS=0
3234

3335
handle_interrupt () {
@@ -58,6 +60,14 @@ get_delay () {
5860
fi
5961
}
6062

63+
get_limit () {
64+
if [ $# -gt 1 ]; then
65+
usage_error "Too many arguments for -n limit"
66+
elif ! [[ $1 =~ ^[0-9]+$ ]]; then
67+
usage_error "Illegal argument! -n limit arg should be a number!"
68+
fi
69+
}
70+
6171
call_api () {
6272
/data/data/com.termux/files/usr/libexec/termux-api Sensor "$@"
6373
}
@@ -74,7 +84,7 @@ usage_error () {
7484

7585

7686
PARAMS=()
77-
while getopts :h,a,c,l,s:d: option
87+
while getopts :h,a,c,l,s:d:n: option
7888
do
7989
case "$option" in
8090
h) show_usage ;;
@@ -83,6 +93,7 @@ do
8393
l) set_flag $LIST_FLAG; PARAMS=(-a list) ;;
8494
s) set_flag $SENSOR_FLAG; get_sensors "$OPTARG"; PARAMS+=(-a sensors --es sensors "$OPTARG") ;;
8595
d) set_flag $DELAY_FLAG; get_delay $OPTARG; PARAMS+=(--ei delay $OPTARG) ;;
96+
n) set_flag $LIMIT_FLAG; get_limit $OPTARG; PARAMS+=(--ei limit $OPTARG) ;;
8697
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
8798
esac
8899
done

0 commit comments

Comments
 (0)