Changeset 15247 in project


Ignore:
Timestamp:
07/25/09 13:42:31 (10 years ago)
Author:
sjamaan
Message:

Document how to display the battery charge under NetBSD

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/wmiirc snippets

    r8643 r15247  
    2525           (wmii:goto-tag (cadr tag)))))
    2626</enscript>
     27
     28=== Battery charge in status bar on NetBSD
     29
     30On my notebook, I want to have a quick view on my battery charge at
     31all times.  Starting with NetBSD 5.0, you can use the program
     32"envstat" to obtain battery info (before you'd use "apm").
     33Unfortunately, there's no easy way to parse its output in a script.
     34You could use the XML output, but then you'd have to wade through the
     35entire output because the {{-s}} switch does not restrict the proplib
     36output to the given sensors.
     37
     38On my iBook G4 laptop, the battery is called {{smartbat0}}.
     39
     40<enscript highlight=scheme>
     41(define (get-charge)
     42  ;; Note that ordering of -s args doesn't influence ordering of output!
     43  (let* ((lines (cddr (with-input-from-pipe "envstat -s 'smartbat0:Battery design cap,smartbat0:Battery charge'" read-lines)))
     44         (max (string->number
     45               (cadr (string-match ".*([0-9]+\\.[0-9]+).*" (car lines)))))
     46         (charge (string->number
     47                  (cadr (string-match ".*([0-9]+\\.[0-9]+).*" (cadr lines))))))
     48    (/ (round (/ (* charge 1000) max)) 10)))
     49</enscript>
     50
     51This procedure gives the output at a granularity of one tenth percent.
     52I check the output in my status procedure that is run once every
     53second and display a warning if the charge drops below a threshold:
     54
     55<enscript highlight=scheme>
     56(let loop ()
     57   (let ((charge (get-charge)))
     58     (wmii:write-tab
     59       "rbar" "status"
     60       (conc charge "% | "
     61             (with-input-from-pipe "echo -n $(uptime | sed 's/.*://; s/,//g') '|' $(date)" read-string)))
     62     (when (<= charge 5)
     63       (process-run "xmessage -center 'Low battery power!'")))
     64     (thread-sleep! 1)
     65     (loop))
     66</enscript>
Note: See TracChangeset for help on using the changeset viewer.