initial commit
[ETG_Helmet] / SOFTWARE / OMEGA2 / common.sh
1 #/bin/bash
2
3 . "settings.sh"
4 . "settings.messages.sh"
5
6 # -----------------------
7
8 myLog(){
9
10 #       if [ -n "$1" ]; then IN="$1"; else read IN; fi  
11 #       echo "[ETG] [$0] $IN" | tee /dev/console
12
13         : > /dev/null
14 }
15
16 # ------- save PID ------
17
18 ## if [ ! -d "$PID" ]; then 
19 ##      mkdir $PID
20 ## fi 
21
22 ## echo "[start] --------- $0 ---------" | myLog
23 ## if [ "$0" != "./kbd.sh" ] echo $$ > $PID/$0
24
25 # ------- update --------
26 FWmd5q=fwmd5q
27 FWmd5=fwmd5
28
29 volume=volume
30
31 # ------- player --------
32
33 prevLocation=prevlocatoin
34 prevLayer=prevlayer
35 currentLang=currentlang
36 currentLangNum=currentlangNum
37
38 # ------- messages ------
39
40 message=message
41 state=state
42 kbdExec=kbdexec
43
44 # ------- GPS -----------
45
46 CURRENTGPS=$TMPFS/currentgps
47 TIMENMEA=$TMPFS/timenmea
48
49 # ------ navigation -----
50
51 navMemo=$TMPFS/navmemo
52 navOn=$TMPFS/navon
53
54 # ------ transmitter ----
55
56 recDone=$TMPFS/recdone
57 recStart=$TMPFS/recstart
58
59 # ------ wifi -----------
60
61 wifiButton=$TMPFS/wifibutton
62 wifiState=$TMPFS/wifistate
63
64 # ------ misc -----------
65 shutDown=$TMPFS/shutdown
66
67 # -----------------------
68
69 ledInint(){
70         killall fast-gpio
71         ./fast-gpio set-output $PIN_WIFI
72         ./fast-gpio set-output $PIN_MAIN
73 }
74
75 led(){
76
77         if [ "$1" == "wifi" ]; then 
78                 led_pin=$PIN_WIFI
79         else 
80                 led_pin=$PIN_MAIN
81         fi 
82
83         if [ "$2" != "off" ]; then 
84
85                 case $2 in
86
87                         "fast")
88                                 led_mode="50 50"
89                         ;;
90                         "midd")
91                                 led_mode="5 50"
92                         ;;
93                         "slow")
94                                 led_mode="2 1"
95                         ;;
96                         "perm")
97                                 led_mode="1 100"
98                         ;;
99
100                 esac
101
102                 ./fast-gpio pwm $led_pin $led_mode
103         else 
104                 ./fast-gpio set $led_pin 0
105         fi 
106 }
107
108 # -----------------------
109
110 leaderMode(){
111
112                         led wifi fast
113
114                         uci set wireless.ra0.disabled=0
115                         uci set wireless.@wifi-iface[0].ApCliEnable='0'
116                         uci set wireless.@wifi-iface[0].hidden='0'      # make AP visible for followers
117                         uci commit wireless
118
119                         uci set network.wlan.ipaddr=$LEADER_IP
120                         uci commit network
121
122                         echo 'l' > mode
123
124                         wifi reload
125
126                         led wifi perm
127 }
128
129 serviceMode(){
130
131                         led wifi fast
132
133                         uci set wireless.ra0.disabled=0
134                         uci set wireless.@wifi-iface[0].ApCliSsid=$AP_CLI_SSID
135                         uci set wireless.@wifi-iface[0].ApCliPassWord=$AP_CLI_PASS
136                         uci set wireless.@wifi-iface[0].ApCliEnable='1'
137                         uci set wireless.@wifi-iface[0].hidden='0'      # make AP visible
138
139                         uci set wireless.@wifi-config[0].ssid=$AP_CLI_SSID
140                         uci set wireless.@wifi-config[0].key=$AP_CLI_PASS
141         #               uci set wireless.@wifi-config[0].encryption='AES'
142
143                         uci commit wireless
144
145                         uci set network.wlan.ipaddr=$SERVICE_IP
146                         uci commit network
147
148                         echo 's' > mode
149
150                         wifi reload
151
152                         led wifi midd
153 }
154
155 followingMode(){
156
157                         led wifi midd
158
159                         targetAP=$(aps 2>/dev/null | grep "ETG-" | awk '{print $2; exit}' | head -1)    # grab Leader's SSID
160
161                         if [ "$targetAP" != "" ]; then
162  
163                                 echo "follower targetAP: $targetAP" | myLog
164                                 uci set wireless.@wifi-iface[0].ApCliSsid=$targetAP
165                                 uci set wireless.@wifi-config[0].ssid=$targetAP
166
167                         fi                      
168
169                         currentApCliSsid=$(uci get wireless.@wifi-iface[0].ApCliSsid 2>/dev/null)
170
171                         if [ "$currentApCliSsid" != "" ]; then 
172                                 uci set wireless.@wifi-iface[0].ApCliEnable='1'
173                         else 
174                                 uci set wireless.@wifi-iface[0].ApCliEnable='0'
175                         fi 
176
177                         uci set wireless.ra0.disabled=0
178                         uci set wireless.@wifi-iface[0].ApCliPassWord=$FOLLOW_PASS
179                         uci set wireless.@wifi-iface[0].hidden='1'      # make AP invisible for future reboots
180
181                         uci set wireless.@wifi-config[0].key=$FOLLOW_PASS
182
183                         uci commit wireless
184
185                         uci set network.wlan.ipaddr=$FOLLOWER_IP
186                         uci commit network
187
188                         echo 'f' > mode
189
190                         wifi reload
191
192                         ifconfig ra0 down       # disable AP radio interface
193
194                         sleep 1
195
196                         # test connection
197                         { 
198                                 conCnt=0
199                                 while [ 1 ]; do
200                                         if ping -W 4 -w 4 -c 2 $LEADER_IP &> /dev/null; then 
201                                                 led wifi slow
202                                                 myLog "followingMode: Connection to group network has been esteblished."
203                                                 break
204                                         else 
205                                                 myLog "followingMode: connecting... $conCnt"
206                                         fi
207                                         if [ "$conCnt" -gt "20" ]; then 
208                                                 break
209                                         fi
210                                         conCnt=$(( $conCnt + 1 ))
211                                         sleep 3
212                                 done
213                         } &
214
215                         myLog "followingMod exit"
216 }
217
218 wifiOff(){
219
220 #                       uci get wireless.ra0.disabled=1
221
222                         ifconfig ra0 down
223                         ifconfig apcli0 down
224                         led wifi off
225 }
226
227
228 # ----------- sound ------------
229
230 getAudioDevice (){
231         if [ ! -e /dev/dsp ]; then
232                 amixer scontrols -D pulse | head -1 | grep -oE "'.*'"
233         else
234                 amixer scontrols | head -1 | grep -oE "'.*'"
235         fi
236 }
237
238 getMicDevice (){
239         if [ ! -e /dev/dsp ]; then
240                 amixer scontrols -D pulse  | awk -v a=2 'NR == a {print $4; exit}' | grep -oE "'.*'"
241         else
242                 amixer scontrols | awk -v a=2 'NR == a {print $4; exit}' | grep -oE "'.*'"
243         fi
244 }
245
246 setMicVolume(){
247         if [ ! -e /dev/dsp ]; then
248                 amixer sset $micDevice $1% -D pulse > /dev/null
249         else
250 #               amixer set $micDevice $1% > /dev/null
251                 amixer set $micDevice $1% | myLog
252         fi
253 }
254
255 setVolume (){
256         if [ ! -e /dev/dsp ]; then
257                 amixer sset $audioDevice $1% -D pulse 2>&1 > /dev/null
258         else
259                 amixer set $audioDevice $1% 2>&1 > /dev/null
260         fi
261 }
262
263 playRoboMessage(){
264         echo "[robot] $1" | myLog
265         espeak -v en -p 30 -s 150 "$1" 2> /dev/null
266 }
267
268 generateRoboMessage(){
269         time=$(date +%s)
270         espeak -v en -p 30 -s 150 "$1" -a 100 -w "$TMPFS/robo_$time.wav" 2> /dev/null
271 ##      echo "$TMPFS/robo_$time.wav" >> $TMPFS/$message
272         echo "$TMPFS/robo_$time.wav"
273 }
274
275 playFile (){
276
277         start=$1
278
279         if [ ! -e /dev/dsp ]; then
280                 # -G $GAINMODE
281                 nice -n -20 madplay "$2" "$3" "$4" "$5" -A $AMP -Q --downsample --no-tty-control --start=$(date +%H:%M:%S -d @$start -u) -o wav:- 2>/dev/null | aplay -q > /dev/null 2>&1
282         else 
283                 # -d --downsample  
284                 nice -n -20 madplay "$2" "$3" "$4" "$5" -A $AMP -Q --downsample --no-tty-control --start=$(date +%H:%M:%S -d @$start -u) > /dev/null 2>&1
285         fi 
286
287 }
288
289 playRecMessage(){
290
291         if echo "$1" | grep -q 'robo_' ; then
292
293                 # playing file generated by generateRoboMessage, no additional filtering
294
295                 if [ "$BBOARD" ]; then
296                         aplay "$1" # for bboard
297                 else 
298                         sox "$1" -t alsa plughw:0,0
299                 fi
300         else 
301 #               play $1 gain $REC_PLAY_GAIN bass $REC_PLAY_BASS norm fade 0.3 # norm fade 0.3 # contrast 100 hain +10
302 #               sox "$1" -t alsa plughw:0,0 gain $REC_PLAY_GAIN bass $REC_PLAY_BASS norm fade 0.3 # norm fade 0.3 # contrast 100 gain +10
303                 sox "$1" -t alsa plughw:0,0 fade 0.3
304         fi
305 }
306
307 sayDir(){
308
309         angle=$1
310         angleABS=$(echo $angle | tr -d '-')
311
312         if [ "$angleABS" -le "180" ] && [ "$angleABS" -ge "135" ]; then dirMessage=$NAV_GO_STRAIGHT; fi
313         if [ "$angle" -ge "-135" ] && [ "$angle" -lt "-45" ]; then dirMessage=$NAV_GO_RIGHT; fi
314         if [ "$angle" -le "135" ] && [ "$angle" -gt "40" ]; then dirMessage=$NAV_GO_LEFT; fi
315         if [ "$angleABS" -le "45" ] && [ "$angleABS" -ge "0" ]; then dirMessage=$NAV_GO_BACK; fi
316
317         # message 
318
319         echo $dirMessage
320 }
321
322 beep(){
323         play -n synth sin %41 fade q 0.0 0.1 0.01 gain -25
324 }
325
326 # --------------- gps --------------------------
327
328 GPSenable(){
329         fast-gpio set $GPS_EN_PIN 1
330         echo "GPS enabled" | myLog
331 }
332
333 GPSdisable(){
334         fast-gpio set $GPS_EN_PIN 0
335         echo "GPS disabled" | myLog
336 }
337
338 # -------------- FS ----------------------------
339
340 doFSCK(){
341
342         umount  /tmp/mounts/SD-P1/
343         sync
344         e2fsck -p /dev/mmcblk0p1
345         sync
346         mountd
347 }
348
349 # ------------------------ create appropriate TMP files for each location -----------------------------
350
351 narrativeStructureReset(){
352
353         TMPIFS=$IFS
354         IFS=$'\n'       # make newlines the only separator
355
356         if [ -d "$TMPMAPDIR" ]; then 
357                 rm -r "$TMPMAPDIR"
358         fi 
359
360         # TODO: CREATE FILES FOR EACH ASSET
361
362         IFS=$TMPIFS
363 }
364
365 # ----------------------- shut down ------------------------------------------------------------------
366
367 shutdownDevice(){
368
369         : > "$shutDown"
370
371         echo "going to sleep..." | myLog
372
373         # interrupt current possible message 
374
375 ##      if [ -s $TMPFS/$message ]; then 
376                 # killall madplay 2> /dev/null;
377 ##              while killall madplay 2> /dev/null; do sleep 0; done 
378 ##      fi 
379
380         # say shutdown message 
381
382         if [ "$2" != "" ]; then finMess=$2; fi
383         if [ "$3" != "" ]; then finMess="$finMess|$3"; fi
384         if [ "$finMess" != "" ]; then echo $finMess > $TMPFS/$message; fi
385
386         sleep $1
387
388         # shutdown
389
390         umount -f $SD
391         sync 
392         GPSdisable
393         wifiOff
394         led wifi off
395         led main off
396         # poweroff
397         echo "o" >/proc/sysrq-trigger
398
399 }
400
401 # ----------------------- get audio devices ---------------------------------------------------------
402
403 if [ "$0" != "./kbd.sh" ]; then
404         audioDevice=$(getAudioDevice)
405         micDevice=$(getMicDevice)
406 fi
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717