initial commit
[ETG_Helmet] / SOFTWARE / OMEGA2 / kbd.sh
1 #/bin/bash
2
3 . "common.sh"
4
5 audioDevice=$1
6 micDevice=$2
7
8 # ----------------------- button beep ----------------------------------------------------------------
9
10 beep (){
11 #       ./fast-gpio pwm 38 500 50 7
12         : > /dev/null
13 #       play -n synth sin %41 fade q 0.0 0.1 0.01 gain -25
14 }
15
16 # ----------------------------------------------------------------------------------------------------
17
18 # KBD_READ=$(( $KBD_MASK ^ ($1 & $KBD_MASK) ))
19
20 KBD_READ=$(( $3 ))
21
22 ## curentNumberOfMessages=$(awk 'END {print NR}' $TMPFS/$message)
23
24 if [ ! -s "$TMPFS/$kbdExec" ]; then 
25
26         echo $1, $KBD_READ
27         echo $KBD_READ > $TMPFS/$kbdExec
28
29         case $KBD_READ in
30
31                 "$KBD_V_UP") # ----------------------- volume up --------------------------
32
33                         beep
34
35                         echo "vol+" | myLog
36
37                         currentVolume=$(cat $TMPFS/$volume)
38                         if [ "$currentVolume" -lt "$VOLMAX" ]; then 
39                                 currentVolume=$(( $currentVolume + $VOLSTEP ))
40                         else 
41                                 currentVolume=$VOLMAX
42                         fi 
43
44                         setVolume $currentVolume > /dev/null
45                         echo $currentVolume > $TMPFS/$volume
46                 ;;
47
48                 "$KBD_V_DOWN") # ----------------------- volume down ----------------------
49
50                         beep
51
52                         echo "vol-" | myLog
53
54                         currentVolume=$(cat $TMPFS/$volume)
55                         if [ "$currentVolume" -gt "$VOLMIN" ]; then 
56                                 currentVolume=$(( $currentVolume - $VOLSTEP ))
57                         else 
58                                 currentVolume=$VOLMIN
59                         fi 
60                         setVolume $currentVolume > /dev/null
61                         echo $currentVolume > $TMPFS/$volume
62                 ;;
63
64                 # ------------------- vol + & vol - shutdown ------------------------------
65
66                 "$(( $KBD_V_UP ^ $KBD_V_DOWN ))") 
67
68                         echo "vol + - pressed" | myLog  
69
70                         if [ ! -s "$shutDown" ]; then 
71                                 date +%s > "$shutDown"
72                         else 
73                                 time=$(date +%s)
74                                 prevtime=$(cat "$shutDown" 2>/dev/null)
75                                 if [ "$prevtime" == "" ]; then 
76                                         prevtime=0; 
77                                         tdiff=0;
78                                 else
79                                         tdiff=$(( $time - $prevtime ))
80                                 fi
81
82                                 if [ "$tdiff" -gt "$SHORT_BUTTON_HOLD" ]; then 
83
84                                         shutdownDevice $BUTTON_SHUTDOWN_DELAY "$DEV_JINGLE" "$DEVICE_OFF"
85                                 fi 
86                         fi
87
88                         sleep 1
89         
90                 ;;
91
92                 # ------------------- vol - & L, number of device -------------------------
93
94                 # firmware version and date of updating
95
96                 "$(( $KBD_V_UP ^ $KBD_L ))") 
97                         
98                         fwVER=$(cat version |  tr "." " ")
99
100                         if [ ! -f instime ]; then
101                                 date +%s > instime
102                         fi 
103
104                         insTime=$(date -d @$(cat instime) "+%d %B %Y" | sed "s/^0*//g; s/\.0*/./g")
105
106                         CTfile=$(cat contenttime 2>/dev/null)
107
108                         MAC=$(cat /proc/sys/kernel/hostname | awk -F'-' '{print $NF}')
109
110                         if [ "$CTfile" == "" ]; then 
111                                 contentTime=$insTime
112                         else 
113                                 contentTime=$(date -d @$CTfile "+%d %B %Y" | sed "s/^0*//g; s/\.0*/./g")                        
114                         fi
115
116                         m1=$(generateRoboMessage "Device number is, $MAC")
117                         m2=$(generateRoboMessage "Current firmware verson is, $fwVER, updated on, $insTime.")
118                         m3=$(generateRoboMessage "Date of last content update is, $contentTime")
119                         echo "$DEV_JINGLE|$m1|$m2|$m3|$DEV_JINGLE_OUT" > $TMPFS/$message
120
121                         sleep 3
122                 ;;
123
124                 # ------------------- vol - & T, History Reset ----------------------------
125
126                 "$(( $KBD_V_DOWN ^ $KBD_T ))")
127
128                         echo "history reset" | myLog
129
130                         narrativeStructureReset
131
132                         m1=$(generateRoboMessage "The history of narration has been reset.")
133                         echo "$DEV_JINGLE|$m1|$DEV_JINGLE_OUT" > $TMPFS/$message
134
135                         sleep 3
136                 ;;
137
138                 "$KBD_T")  # ----------------------- theme (language <) -------------------
139
140                         beep
141
142                         echo "theme" | myLog
143
144                         listOfLanguages=$(ls -c1 $CONTENT)
145                         numOflang=$( echo $listOfLanguages | awk '{print NF}' )
146                         currentLN=$(cat $TMPFS/$currentLangNum)
147
148                         if [ "$currentLN" -gt "1" ]; then 
149                                 currentLN=$(( $currentLN - 1 ))
150                         else 
151                                 currentLN=$numOflang
152                         fi
153
154                         echo $currentLN > $TMPFS/$currentLangNum
155                         currentL=$(echo $listOfLanguages | awk -v a=$currentLN '{print $a}')
156                         echo $currentL > $TMPFS/$currentLang
157
158                         echo "$DEV_JINGLE|$DEV_LANGUAGE" > $TMPFS/$message
159
160                         echo $currentL $currentLN | myLog
161
162                         sleep 1
163                 ;;
164
165                 "$KBD_L") # ----------------------- language > ----------------------------
166
167                         beep
168
169                         echo "language" | myLog
170
171                         if [ -s $TMPFS/$message ]; then 
172                                 while killall madplay 2> /dev/null; do sleep 0; done
173                         fi 
174
175                         listOfLanguages=$(ls -c1 $CONTENT)
176                         numOflang=$( echo $listOfLanguages | awk '{print NF}' )
177                         currentLN=$(cat $TMPFS/$currentLangNum)
178
179                         if [ "$currentLN" -lt "$numOflang" ]; then 
180                                 currentLN=$(( $currentLN + 1 ))
181                         else 
182                                 currentLN=1
183                         fi
184
185                         echo $currentLN > $TMPFS/$currentLangNum
186                         currentL=$(echo $listOfLanguages | awk -v a=$currentLN '{print $a}')
187                         echo $currentL > $TMPFS/$currentLang
188
189                         echo "$DEV_JINGLE|$DEV_LANGUAGE" > $TMPFS/$message
190
191                         echo $currentL $currentLN | myLog
192
193                         sleep 1
194                 ;;
195
196                 "$KBD_M") # ---------------------- nav memorize GPS ----------------------
197
198                         beep
199
200                         echo "map marker" | myLog
201
202                         time=$(date +%s)
203
204                         CURRENTGPSStr=$(cat $CURRENTGPS)
205                         prevtime=$(echo $CURRENTGPSStr | awk -F'|' '{print $3; exit}')
206
207                         if [ "$prevtime" == "" ]; then prevtime=0; fi
208                         tdiff=$(( $time - $prevtime ))
209
210                         if [ "$prevtime" != "" ] && [ "$tdiff" -lt "$MAX_GPS_AGE_THRESHOLD" ]; then     # valid GPS data checking 
211
212                                 currentGPS=$(echo $CURRENTGPSStr | awk -F'|' '{print $1; exit}')
213                                 echo $currentGPS > $navMemo
214
215                                 # coordinates has been saved
216
217                                 echo "$DEV_JINGLE|$NAV_YOU_COORD_SAVED" > $TMPFS/$message
218
219                                 echo "saved" | myLog
220                 
221                         else    # GPS not fixed 
222
223                                 echo "$DEV_JINGLE|$DEV_NO_COORD" > $TMPFS/$message
224
225                                 echo "GPS not fixed" | myLog
226                         fi
227
228                         sleep 5
229                 ;;
230
231                 "$KBD_R") # ----------------------- nav reminder -------------------------
232
233                         beep
234
235                         echo "navigation" | myLog
236
237                         memo=$(cat $navMemo)
238                         
239                         if [ "$memo" != "" ]; then      # GPS pont was saved early
240
241                                 navTurnedOn=$(cat $navOn)
242
243                                 if [ "$navTurnedOn" == "" ]; then  
244
245                                         echo 1 > $navOn
246
247                                         NAVMEMOGPS=$(cat $navMemo)
248
249                                         # navigation has been activated
250
251                                         echo "$DEV_JINGLE|$NAV_FUNC_ACTIVE" > $TMPFS/$message
252
253                                         echo "nav activated" | myLog
254                                 else 
255
256                                         : > $navOn
257
258                                         # navigation has been deactivated
259
260                                         echo $NAV_FUNC_TURNED_OFF >> $TMPFS/$message
261
262                                         echo "nav off" | myLog
263                                 fi 
264                 
265                         else    # device has no points in memory 
266
267                                 echo $NAV_NO_POINTS >> $TMPFS/$message
268
269                                 echo "nav no saved points" | myLog
270                         fi
271
272                         sleep 5
273
274                 ;;
275
276                 "$KBD_MIC") # ----------------------- mic --------------------------------
277
278                         beep
279
280                         if [ -s "$wifiState" ]; then 
281                                 myLog "mic"
282                                 echo 1 > $recStart
283                         else 
284                                 myLog "mic wifi disabled"
285                         fi 
286
287                         sleep 3
288                         
289                 ;;
290
291                 "$KBD_WIFI") # ----------------------- wifi ------------------------------
292
293                         beep
294
295                         myLog "wifi pressed"
296                         
297                         if [ ! -s "$wifiButton" ]; then 
298                                 date +%s > "$wifiButton"
299                         else 
300                                 time=$(date +%s)
301                                 prevtime=$(cat "$wifiButton" 2>/dev/null)
302                                 if [ "$prevtime" == "" ]; then 
303                                         prevtime=0; 
304                                         tdiff=0;
305                                 else
306                                         tdiff=$(( $time - $prevtime ))
307                                 fi
308
309                                 if [ "$tdiff" -gt "$SHORT_BUTTON_HOLD" ]; then
310
311                                         : > "$wifiButton"
312
313                                         if [ -s "$wifiState" ]; then 
314
315                                                 if [ "$MODE" == "l" ]; then 
316                                                         m1=$(generateRoboMessage "Leading mode deactivated.")
317                                                 fi
318
319                                                 echo "$DEV_JINGLE|$DEV_WIFI_OFF|$m1" > $TMPFS/$message
320
321                                                 : > "$wifiState"
322                                                 wifiOff
323                                                 myLog "wifi disabled"
324
325                                                 MODE=$(cat mode)
326
327                                         else 
328
329                                                 echo "$DEV_JINGLE|$DEV_WIFI_ON|$DEV_JINGLE_OUT" > $TMPFS/$message
330
331                                                 echo 1 > "$wifiState"
332                                                 followingMode
333                                                 myLog "wifi ebabled"
334
335                                         fi 
336
337                                         sleep 3
338                                         
339                                 fi 
340                         fi
341
342                         sleep 1
343                 ;;
344
345                 "$KBD_RELEASE") # ----------------------- release buttons ----------------
346
347                         echo "kbd release" | myLog
348
349                         # mic 
350
351                         if [ -s "$recStart" ]; then 
352 #                               killall rec
353 #                               killall arecord
354 #                               kill -9 $(pidof arecord)
355 #                               kill -9 $(pidof rec)
356                                 kill -15 $(cat /tmp/rec.pid)
357                                 echo "rec release" | myLog
358                         fi
359
360                         # wifi 
361
362                         if [ -s "$wifiButton" ]; then 
363                                 : > "$wifiButton"
364                         fi 
365
366                         # shutdown (vol + -)
367
368                         if [ -s "$shutDown" ]; then 
369                                 : > "$shutDown"
370
371                                 echo "$DEV_JINGLE|$INTRO_INSTRUCTION|$INTRO_INSTRUCTION_EXTRA|$INTRO_INSTRUCTION_EXTRA2|$DEV_JINGLE_OUT" > $TMPFS/$message
372
373                                 sleep 2
374                         fi 
375         
376                         # --------
377                 ;;
378
379         esac
380
381         : > $TMPFS/$kbdExec
382 fi
383
384
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717