initial commit
[ETG_Helmet] / SOFTWARE / OMEGA2 / adc.sh
1 #/bin/bash
2
3 if [ "$TMPFS" == "" ]; then 
4         . "common.sh"
5 fi
6
7 echo $$ > $PID/$0
8
9 # settings 
10
11 devHEXNum="48"
12
13 BATT_20MIN=$(( 0x3b ))  # about 35-40 min
14 BATT_10MIN=$(( 0x3a ))
15 BATT_5MIN=$(( 0x39 ))
16 BATT_0MIN=$(( 0x38 ))
17
18 battCnt=0
19 currentBat=255
20 prevBatt=255
21 battMessage=""
22 prevBattMessage=""
23
24 # test ADC presence
25
26 if i2cdetect -y 1 | grep " $devHEXNum " > /dev/null; then 
27
28         i2cset -y 1 0x48 0x01 0x42E0 w 
29         sleep 1; 
30
31         while [ 1 ]; do 
32
33                 battHEX=$(i2cget -y 1 0x$devHEXNum 0x00 w)
34
35                 batt=$(( 0x$(echo $battHEX | awk '{print substr($0,5)}') ))
36
37                 echo "battery: $battHEX, $batt" | myLog
38
39                 if [ "$batt" == "$prevBatt" ]; then battCnt=$(( $battCnt + 1 )); else battCnt=0; fi 
40
41                 prevBatt=$batt
42
43                 echo "battCnt: $battCnt"
44
45                 if [ "$battCnt" == "3" ]; then 
46                         currentBat=$batt
47                         battCnt=0
48                 fi
49
50                 # ------ compare -----------
51
52                 if [ "$currentBat" -le "$BATT_20MIN" ] && [ "$currentBat" -gt "$BATT_10MIN" ] && [ "$battMessage" != "$BATTERY_20" ]; then 
53                         battMessage=$BATTERY_20
54                 fi
55
56                 if [ "$currentBat" -le "$BATT_10MIN" ] && [ "$currentBat" -gt "$BATT_5MIN" ] && [ "$battMessage" != "$BATTERY_10" ]; then 
57                         battMessage=$BATTERY_10
58                 fi
59
60                 if [ "$currentBat" -le "$BATT_5MIN" ] && [ "$currentBat" -gt "$BATT_0MIN" ] && [ "$battMessage" != "$BATTERY_5" ]; then 
61                         battMessage=$BATTERY_5
62                 fi
63
64                 if [ "$currentBat" -le "$BATT_0MIN" ] && [ "$battMessage" != "$BATTERY_0" ]; then 
65                         battMessage=$BATTERY_0
66                         shutdownDevice $BATTERY_SHUTDOWN_DELAY "$DEV_JINGLE" "$BATTERY_0"
67                 fi
68
69                 # ------ send message ------
70
71                 if [ "$prevBattMessage" != "$battMessage" ]; then 
72                         echo "battMessage: $battMessage" | myLog
73
74                         echo "$DEV_JINGLE|$battMessage|$DEV_JINGLE_OUT" > $TMPFS/$message
75
76                         prevBattMessage=$battMessage
77                 fi
78
79                 sleep 30; 
80         done
81
82 else 
83         echo "ADC module not detected" | myLog
84 fi 
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717