Module: UsbPdMatch

Defined in:
lib/usb_pd_match.rb,
lib/usb_pd_match/device.rb,
lib/usb_pd_match/charger.rb,
lib/usb_pd_match/version.rb,
lib/usb_pd_match/negotiator.rb

Overview

UsbPdMatch — a small, dependency-free toolkit for reasoning about USB-C Power Delivery (USB-PD) compatibility between chargers and devices.

Quick start:

charger = UsbPdMatch.charger(name: "65W GaN", max_watts: 65)
device  = UsbPdMatch.device(name: "Laptop", max_watts: 60, max_voltage: 20)
UsbPdMatch.negotiate(charger, device).summary
# => "65W GaN → Laptop: 20.0V @ 3.00A = 60.0W (100% of device rating)"

Defined Under Namespace

Modules: Negotiator Classes: Charger, Device, Error, Result

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.best_charger_for(device, chargers) ⇒ Object

Rank a list of chargers by how well each charges the given device, best first. Each entry is the negotiation Result.



35
36
37
38
39
40
# File 'lib/usb_pd_match.rb', line 35

def best_charger_for(device, chargers)
  chargers
    .map { |c| Negotiator.negotiate(c, device) }
    .compact
    .sort_by { |r| -r.watts }
end

.charger(**kwargs) ⇒ Object



21
22
23
# File 'lib/usb_pd_match.rb', line 21

def charger(**kwargs)
  Charger.new(**kwargs)
end

.device(**kwargs) ⇒ Object



25
26
27
# File 'lib/usb_pd_match.rb', line 25

def device(**kwargs)
  Device.new(**kwargs)
end

.negotiate(charger, device) ⇒ Object



29
30
31
# File 'lib/usb_pd_match.rb', line 29

def negotiate(charger, device)
  Negotiator.negotiate(charger, device)
end