-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbert_ev3dev_functions
More file actions
241 lines (207 loc) · 6.55 KB
/
bert_ev3dev_functions
File metadata and controls
241 lines (207 loc) · 6.55 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/bash -u
# Script_name : bert_ev3dev_functions
# Author : Bert Lindeman
# Description : functions that can be used by more than one bash script related to ev3dev
# Version : v1.3
# Date : 2014-11-04
# Usage : . /usr/local/bin/bert_ev3dev_functions
# Notes :
# Copyright : 2014 Bert Lindeman
# License : GPL-3.0+
#
#============================================================================
#
# Changelog
# Date By Description
# 20141104 BL prepare for release v1.3
# 20141020 BL Added reverse colors
# 20141213 BL Add escapes to the colors so Bash knows they are
# "non-printable"
# 20160103 BL Adapt to image 20151230 (change led file names)
#============================================================================
#
# color codes ascii
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
black='\e[0;30m' ; brblack='\e[1;30m' ; rvblack='\e[7;30m' ;
red='\e[0;31m' ; brred='\e[1;31m' ; rvred='\e[7;31m' ;
green='\e[0;32m' ; brgreen='\e[1;32m' ; rvgreen='\e[7;32m' ;
yellow='\e[0;33m' ; bryellow='\e[1;33m' ; rvyellow='\e[7;33m' ;
blue='\e[0;34m' ; brblue='\e[1;34m' ; rvblue='\e[7;34m' ;
magenta='\e[0;35m' ; brmagenta='\e[1;35m' ; rvmagenta='\e[7;35m' ;
cyan='\e[0;36m' ; brcyan='\e[1;36m' ; rvcyan='\e[7;36m' ;
white='\e[0;37m' ; brwhite='\e[1;37m' ; rvwhite='\e[7;37m' ;
endColor='\e[0m'
blackp='\[\e[0;30m\]' ; brblackp='\[\e[1;30m\]' ; rvblackp='\[\e[7;30m\]' ;
redp='\[\e[0;31m\]' ; brredp='\[\e[1;31m\]' ; rvredp='\[\e[7;31m\]' ;
greenp='\[\e[0;32m\]' ; brgreenp='\[\e[1;32m\]' ; rvgreenp='\[\e[7;32m\]' ;
yellowp='\[\e[0;33m\]' ; bryellowp='\[\e[1;33m\]' ; rvyellowp='\[\e[7;33m\]' ;
bluep='\[\e[0;34m\]' ; brbluep='\[\e[1;34m\]' ; rvbluep='\[\e[7;34m\]' ;
magentap='\[\e[0;35m\]' ; brmagentap='\[\e[1;35m\]' ; rvmagentap='\[\e[7;35m\]' ;
cyanp='\[\e[0;36m\]' ; brcyanp='\[\e[1;36m\]' ; rvcyanp='\[\e[7;36m\]' ;
whitep='\[\e[0;37m\]' ; brwhitep='\[\e[1;37m\]' ; rvwhitep='\[\e[7;37m\]' ;
endColorp='\[\e[0m\]'
#
# Assign "non-cron" if ENVIRONMENT is unset or null, and thus prevent error "variable unbound":
: "${ENVIRONMENT:=non-cron}"
#
ascii2hex(){
printf "$@" | hexdump -v -e '/1 "%02x"'
}
findsensorctl () {
#
# function to find which directory structure is used to control "this"
# sensor
# find which in% sensor is connected to msensor/sensor%
#
# parm1: input port NUMBER
#
local portparm
local port
portparm=${1:-}
port=""
CURSENSOR="???????"
SEENPORT=""
# for d in /sys/class/msensor/sensor*;
for d in /sys/class/lego-sensor/sensor*;
do
if [ -r "$d/address" ]; then
port=`cat $d/address`
if [ "$port" == "in${portparm}" ]; then
CURSENSOR=${d: -1:1}
# take the numeric part at the end of the basename:
CURSENSOR=`basename ${d}`
CURSENSOR=${CURSENSOR:6} # drop "sensor" from the name
return
fi
fi
done
}
### general things:
# Usage:
# listcontains haystack needle
# haystack is a space separated list of strings
listcontains() {
for word in $1; do
[[ $word == $2 ]] && return 0
done
return 1
}
# Usage : stringcontains needle haystack
# needle if missing: empty string
# haystack if missing: empty string
# An empty string contains nothing
stringcontains() {
substring="${1-""}"
string="${2:-""}"
if [ -z "$string" ]; then return 1 ; fi
if [[ "$string" == *"$substring"* ]]; then return 0
else return 1
fi
}
Btrim() {
trimmed=$(echo "${*}" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
}
Ftrim() {
trimmed=$(echo "${*}" | sed 's/^[[:space:]]*//')
}
Ttrim() {
trimmed=$(echo "${*}" | sed 's/[[:space:]]*$//')
}
### screen manipulations:
# echo -ne "\033[0;0H"
# COLUMNS=$(tput cols)
# printf "%${COLUMNS}s\n"
# # top and clear rest of the line:
topofscreen() {
echo -ne "\033[0;0H\033[2K"
}
# parm1 is line (height) 0 = top
# parm2 is column (width) 0 = left
putcursor() {
echo -ne "\033[$1;$2f"
}
getxy(){
tput sc # save cursor
echo -en "$black\E[6n";read -sdR CURPOS; CURPOS=${CURPOS#*[}
echo -en "$endColor"
tput rc # restore cursor
CURY=`echo "${CURPOS}" | cut -d ";" -f 1`
CURX=`echo "${CURPOS}" | cut -d ";" -f 2`
}
### ev3 functions:
# parm1: the words from the whole trigger "file"
showtrigger () {
#echo "showing trigger from $1"
#set -x
for word in ${1} ;
do
delim=${word:0:1}${word:${#word}-1:1}
if [ $delim == "[]" ]; then
trigger=${word:1:${#word}-2}
break # do not search further we found it here.
else
trigger=$word
fi
done
currenttrigger=$trigger
}
findmotorctl () {
#
# function to find which directory structure is used to control "this"
# motor
#
# parm1: motor port LETTER
# uppercase parm1:
local parm1
local port
parm1=`echo $1 | tr '[:lower:]' '[:upper:]'`
port=""
CURMOTOR="???????"
SEENPORT=""
# for d in /sys/class/tacho-motor/tacho-motor*/ /sys/class/dc-motor/dc-motor*/ /sys/class/servo-motor/servo-motor*/ ;
# for d in /sys/class/tacho-motor/motor*/ /sys/class/dc-motor/motor*/ /sys/class/servo-motor/motor*/ ;
for d in /sys/class/*motor/motor*/ ;
do
if [ -r "$d/address" ]; then
port=`cat $d/address`
SEENPORT="$SEENPORT $port"
if [ "$port" == "out$parm1" ]; then
CURMOTOR=${d:0:-1}
return
fi
fi
done
if [ "$ENVIRONMENT" == "non-cron" ]; then
echo "CURMOTOR: \"${CURMOTOR}\" SEENPORT: \"$SEENPORT\" port: \"${port}\""
fi
}
OFFred () {
#
# function to switch OFF the red EV3-leds (left and right)
#
echo none > /sys/class/leds/ev3\:right\:red\:ev3dev/trigger
echo 0 > /sys/class/leds/ev3\:right\:red\:ev3dev/brightness
echo none > /sys/class/leds/ev3\:left\:red\:ev3dev/trigger
echo 0 > /sys/class/leds/ev3\:left\:red\:ev3dev/brightness
}
OFFgreen () {
#
# function to switch OFF the green EV3-leds (left and right)
#
echo none > /sys/class/leds/ev3\:left\:green\:ev3dev/trigger
echo 0 > /sys/class/leds/ev3\:left\:green\:ev3dev/brightness
echo none > /sys/class/leds/ev3\:right\:green\:ev3dev/trigger
echo 0 > /sys/class/leds/ev3\:right\:green\:ev3dev/brightness
}
OFFleds () {
#
# function to switch OFF both red and green EV3-leds (left and right)
#
OFFred
OFFgreen
}