summaryrefslogtreecommitdiff
path: root/power_now
blob: f8efd8ae14d872998c731a28f66bbfe4510437ec (plain)
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
#!/bin/sh
unset QUIET MILLIWATTS TOTAL
BATTERY="ALL"

printhelp () {
  printf "power_now: get the current power draw from the battery.\n"
  printf "\t-q) print only the number\n"
  printf "\t-m) use milliwatts instead of watts\n"
  printf "\t-t) use the total of all batteries\n"
  printf "\t-b X) use battery X\n"
  exit 1
}

while getopts "b:mqt" o; do case "${o}" in
  q) QUIET="y" ;;
  m) MILLIWATTS="y" ;;
  t)
    TOTAL="y"
    BATTERY="ALL"
    ;;
  b) BATTERY="$OPTARG" ;;
  *) printhelp ;;
esac done

error () {
  printf "%s\n" "$@"
  exit 1
}

#TOTALPOWER='0'

case "$BATTERY" in
  "ALL")
    find /sys/class/power_supply -name 'BAT*' || error "Are there no batteries?"
    for battery in /sys/class/power_supply/BAT*; do
      battery="$(basename "$battery")"
      DIRNAME="/sys/class/power_supply/${battery}"
      if [ -f "${DIRNAME}/power_now" ]; then
        if [ -n "${MILLIWATTS}" ]; then
          POWER="$(awk '{print $1/1e3}' "${DIRNAME}/power_now" | head -1)"
        else
          POWER="$(awk '{print $1/1e6}' "${DIRNAME}/power_now" | head -1)"
        fi
      elif [ -f "${DIRNAME}/current_now" ]; then
        if [ -n "${MILLIWATTS}" ]; then
          POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e9}' | head -1)"
        else
          POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e12}' | head -1)"
        fi
      fi
      if [ -n "${TOTAL}" ]; then
        TOTALPOWER="$(echo "${TOTALPOWER}" "${POWER}" | awk '{print $1+$2}'| head -1)"
      else
        if [ -z "${QUIET}" ]; then
          printf "Power in %s is " "${battery}"
        fi
        printf "%s" "${POWER}"
        if [ -n "${MILLIWATTS}" ]; then
          echo " mW"
        else
          echo " W"
        fi
      fi
    done
    if [ -n "${TOTAL}" ]; then
      if [ -z "${QUIET}" ]; then
        printf "Total power is "
      fi
      printf "%s" "${TOTALPOWER}"
      if [ -n "${MILLIWATTS}" ]; then
        echo " mW"
      else
        echo " W"
      fi
    fi
    ;;
  *)
    DIRNAME="/sys/class/power_supply/${BATTERY}"
    [ -d "${DIRNAME}" ] || error "No such battery!"
    if [ -f "${DIRNAME}/power_now" ]; then
      if [ -n "${MILLIWATTS}" ]; then
        POWER="$(awk '{print $1/1e3}' "${DIRNAME}/power_now" | head -1)"
      else
        POWER="$(awk '{print $1/1e6}' "${DIRNAME}/power_now" | head -1)"
      fi
    elif [ -f "${DIRNAME}/current_now" ]; then
      if [ -n "${MILLIWATTS}" ]; then
        POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e9}' | head -1)"
      else
        POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e12}' | head -1)"
      fi
    fi
    if [ -z "${QUIET}" ]; then
      printf "Power in %s is " "${BATTERY}"
    fi
    printf "%s" "${POWER}"
    if [ -n "${MILLIWATTS}" ]; then
      echo " mW"
    else
      echo " W"
    fi
    ;;
esac