Class: UsbPdMatch::Charger
- Inherits:
-
Object
- Object
- UsbPdMatch::Charger
- 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
-
#max_watts ⇒ Object
readonly
Returns the value of attribute max_watts.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pdos ⇒ Object
readonly
Returns the value of attribute pdos.
-
#ports ⇒ Object
readonly
Returns the value of attribute ports.
Instance Method Summary collapse
-
#current_at(voltage) ⇒ Object
Max current available at a given voltage (0.0 if the voltage isn’t offered).
-
#initialize(name:, max_watts:, pdos: nil, ports: 1) ⇒ Charger
constructor
pdos: array of [voltage, max_current_amps].
-
#max_voltage ⇒ Object
Highest voltage this charger can output.
- #supports?(voltage) ⇒ Boolean
- #to_h ⇒ Object
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_watts ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/usb_pd_match/charger.rb', line 16 def name @name end |
#pdos ⇒ Object (readonly)
Returns the value of attribute pdos.
16 17 18 |
# File 'lib/usb_pd_match/charger.rb', line 16 def pdos @pdos end |
#ports ⇒ Object (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_voltage ⇒ Object
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
39 40 41 |
# File 'lib/usb_pd_match/charger.rb', line 39 def supports?(voltage) current_at(voltage).positive? end |
#to_h ⇒ Object
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 |