Class: UsbPdMatch::Charger

Inherits:
Object
  • Object
show all
Defined in:
lib/usb_pd_match/charger.rb

Overview

A USB-C Power Delivery source (charger). It advertises a set of fixed Power Data Objects (PDOs) — the (voltage, max_current) pairs it can supply.

USB Power Delivery negotiates the highest mutually-supported fixed PDO, so a charger is modeled as the list of PDOs it exposes plus the number of physical USB-C ports (shared power budget across ports is left to the caller).

Constant Summary collapse

STANDARD_VOLTAGES =

Common USB-PD fixed-supply voltages (Standard Power Range).

[5, 9, 15, 20].freeze
EPR_VOLTAGES =

Extended Power Range (EPR) adds these for 100W+ chargers.

[28, 36, 48].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, max_watts:, pdos: nil, ports: 1) ⇒ Charger

pdos: array of [voltage, max_current_amps]. If omitted, a sane fixed-PDO set is derived from max_watts using the standard voltage ladder.



20
21
22
23
24
25
26
# File 'lib/usb_pd_match/charger.rb', line 20

def initialize(name:, max_watts:, pdos: nil, ports: 1)
  @name = name
  @max_watts = max_watts.to_f
  @ports = Integer(ports)
  @pdos = (pdos || derive_pdos(@max_watts)).map { |v, a| [Integer(v), a.to_f] }
  freeze
end

Instance Attribute Details

#max_wattsObject (readonly)

Returns the value of attribute max_watts.



16
17
18
# File 'lib/usb_pd_match/charger.rb', line 16

def max_watts
  @max_watts
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/usb_pd_match/charger.rb', line 16

def name
  @name
end

#pdosObject (readonly)

Returns the value of attribute pdos.



16
17
18
# File 'lib/usb_pd_match/charger.rb', line 16

def pdos
  @pdos
end

#portsObject (readonly)

Returns the value of attribute ports.



16
17
18
# File 'lib/usb_pd_match/charger.rb', line 16

def ports
  @ports
end

Instance Method Details

#current_at(voltage) ⇒ Object

Max current available at a given voltage (0.0 if the voltage isn’t offered).



34
35
36
37
# File 'lib/usb_pd_match/charger.rb', line 34

def current_at(voltage)
  match = pdos.find { |v, _a| v == voltage }
  match ? match[1] : 0.0
end

#max_voltageObject

Highest voltage this charger can output.



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

def max_voltage
  pdos.map(&:first).max
end

#supports?(voltage) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/usb_pd_match/charger.rb', line 39

def supports?(voltage)
  current_at(voltage).positive?
end

#to_hObject



43
44
45
# File 'lib/usb_pd_match/charger.rb', line 43

def to_h
  { name: name, max_watts: max_watts, ports: ports, pdos: pdos }
end