Skip to content
cross-platform, normalized battery information library
Branch: master
Clone or download
KenjiTakahashi OpenBSD: Extend `maxnumt` for the new field
Fixes `-CURRENT`.
No need to consider it later in the code, because highest value we ever
read is at `10`.
Latest commit 5196957 Apr 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd/battery
.gitignore First part of tests, fixes and refactorings May 14, 2016
.travis.yml Scrap some old Go's Aug 9, 2016
LICENSE.md
README.md
battery.go Fix typo in docs Apr 19, 2017
battery_darwin.go Fix plist import Feb 6, 2019
battery_freebsd_dragonfly_.go
battery_linux.go
battery_netbsd.go Fix plist import Feb 6, 2019
battery_openbsd.go
battery_solaris.go
battery_test.go Prepare structures for exposing voltage info Apr 1, 2017
battery_windows.go windows: Use consts for State Apr 19, 2017
errors.go Prepare structures for exposing voltage info Apr 1, 2017
errors_test.go Prepare structures for exposing voltage info Apr 1, 2017
ioctl.go

README.md

battery Build Status Go Report Card GoDoc

Cross-platform, normalized battery information library.

Gives access to a system independent, typed battery state, capacity, charge and voltage values recalculated as necessary to be returned in mW, mWh or V units.

Currently supported systems:

  • Linux 2.6.39+
  • OS X 10.10+
  • Windows XP+
  • FreeBSD
  • DragonFlyBSD
  • NetBSD
  • OpenBSD
  • Solaris

Installation

$ go get -u github.com/distatus/battery

Code Example

import (
	"fmt"

	"github.com/distatus/battery"
)

func main() {
	batteries, err := battery.GetAll()
	if err != nil {
		fmt.Println("Could not get battery info!")
		return
	}
	for i, battery := range batteries {
		fmt.Printf("Bat%d: ", i)
		fmt.Printf("state: %f, ", battery.State)
		fmt.Printf("current capacity: %f mWh, ", battery.Current)
		fmt.Printf("last full capacity: %f mWh, ", battery.Full)
		fmt.Printf("design capacity: %f mWh, ", battery.Design)
		fmt.Printf("charge rate: %f mW, ", battery.ChargeRate)
		fmt.Printf("voltage: %f V, ", battery.Voltage)
		fmt.Printf("design voltage: %f V\n", battery.DesignVoltage)
	}
}

CLI

There is also a little utility which - more or less - mimicks the GNU/Linux acpi -b command.

Installation

$ go get -u github.com/distatus/battery/cmd/battery

Usage

$ battery
BAT0: Full, 95.61% [Voltage: 12.15V (design: 12.15V)]
You can’t perform that action at this time.