Help us shape the future of Go! Take the 2026 Go Developer Survey

usbpdprofiles

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 1 Imported by: 0

README

usbpdprofiles

Compute USB Power Delivery profiles and the wattage a device actually receives once charger, cable and device limits are applied.

Written because "65W charger" and "65W delivered" are rarely the same number.

Install

go get github.com/taysirco/usbpdprofiles

Usage

import "github.com/taysirco/usbpdprofiles"

// 65W charger, 30W phone, plain 3A cable — the phone is its own limit
usbpdprofiles.Negotiate(65, 30, 3)
// => {DeliveredWatts:27 LimitedBy:"device" Profile:{Volts:9 Amps:3 Watts:27}}

// Same charger, a 65W laptop, still a 3A cable — now the cable limits it,
// because 3A tops out at 60W (20V x 3A) and 5A e-marking is required above that
usbpdprofiles.Negotiate(65, 65, 3).LimitedBy  // "cable"
usbpdprofiles.Negotiate(65, 65, 5).LimitedBy  // "device"

// One 65W budget shared by a laptop and a phone
usbpdprofiles.SplitBudget(65, []float64{65, 20})  // [65 0]

The useful output is LimitedBy: knowing whether the source, the cable or the device is the bottleneck tells you what to replace.

Why the numbers differ from the box

  • Cable e-marker. A plain USB-C cable is rated 3A, capping the link at 60W regardless of what charger and device negotiated. Extended Power Range voltages (above 20V) require a 5A e-marked cable, and nothing on the jacket tells you which one you are holding.
  • Shared budget. On multi-port chargers the headline figure is a total, allocated by a controller rather than split evenly.
  • Device ceiling. A phone that tops out at 20W requests that profile and ignores the rest.
  • Gallium nitride switching is why modern chargers shrank — not why they deliver more power.

Reference data

Per-model wattage ratings and port-split tables used to validate this package come from the Anker wall charger specifications published by CairoVolt.

License

MIT

Documentation

Overview

Package usbpdprofiles computes USB Power Delivery profiles and the wattage a device actually receives once charger, cable and device limits are applied.

A charger's advertised wattage is a ceiling, not a promise. Three independent limits sit between the wall and the battery — the source profile set, the cable current rating, and the device's own maximum — and the smallest one wins.

Reference specifications and per-model wattage data used to validate this package are published at https://cairovolt.com/en/anker/wall-chargers

Index

Constants

View Source
const (
	// DefaultCableAmps is the current ceiling for a cable without an e-marker chip.
	DefaultCableAmps = 3.0
	// EMarkedCableAmps is the current ceiling for a 5A e-marked cable.
	EMarkedCableAmps = 5.0
	// SPRMaxVolts is the highest voltage in Standard Power Range. Voltages above
	// this belong to Extended Power Range, which the specification only permits
	// over a 5A e-marked EPR cable.
	SPRMaxVolts = 20
)

Variables

View Source
var PDVoltages = []int{5, 9, 12, 15, 20, 28, 36, 48}

PDVoltages are the fixed supply voltages defined by the USB Power Delivery specification.

Functions

func SplitBudget

func SplitBudget(totalWatts float64, demands []float64) []float64

SplitBudget allocates a shared multi-port budget, highest demand claiming first.

Types

type Outcome

type Outcome struct {
	DeliveredWatts float64
	LimitedBy      string
	Profile        Profile
}

Outcome reports delivered wattage and which component limited it. LimitedBy is one of "source", "cable" or "device".

func Negotiate

func Negotiate(sourceWatts, deviceWatts, cableAmps float64) Outcome

Negotiate resolves the delivered wattage and identifies the limiting component.

type Profile

type Profile struct {
	Volts int
	Amps  float64
	Watts float64
}

Profile is a single negotiated power profile.

func ProfilesOverCable

func ProfilesOverCable(sourceWatts, cableAmps float64) []Profile

ProfilesOverCable returns the profiles actually reachable through a given cable. A cable rated below 5A cannot carry Extended Power Range voltages at all, and caps current on every remaining profile.

func SourceProfiles

func SourceProfiles(sourceWatts float64) []Profile

SourceProfiles returns the fixed PD profiles a source of the given rating can advertise, ignoring cable limits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL