Module: UsbPdMatch::Negotiator
- Defined in:
- lib/usb_pd_match/negotiator.rb
Overview
Negotiates the optimal fixed-PDO contract between a charger and a device, mirroring how a real USB-C PD sink picks the highest mutually-supported voltage and the current both sides can sustain.
Class Method Summary collapse
-
.full_power?(charger, device) ⇒ Boolean
True when the device will charge at its full rated power on this charger.
-
.negotiate(charger, device) ⇒ Object
Returns a Result, or nil if there is no mutually supported voltage.
-
.watts(charger, device) ⇒ Object
Convenience: just the delivered wattage (0.0 if incompatible).
Class Method Details
.full_power?(charger, device) ⇒ Boolean
True when the device will charge at its full rated power on this charger.
58 59 60 61 |
# File 'lib/usb_pd_match/negotiator.rb', line 58 def full_power?(charger, device) result = negotiate(charger, device) result ? result.full_power? : false end |
.negotiate(charger, device) ⇒ Object
Returns a Result, or nil if there is no mutually supported voltage.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/usb_pd_match/negotiator.rb', line 36 def negotiate(charger, device) candidates = charger.pdos.select do |voltage, _charger_current| voltage <= device.max_voltage end return nil if candidates.empty? best = candidates.map do |voltage, charger_current| current = [charger_current, device.current_ceiling_at(voltage)].min [voltage, current, (voltage * current).round(2)] end.max_by { |voltage, _current, watts| [watts, voltage] } # tie → higher voltage (lower loss) Result.new(voltage: best[0], current: best[1], watts: best[2], charger: charger, device: device) end |
.watts(charger, device) ⇒ Object
Convenience: just the delivered wattage (0.0 if incompatible).
52 53 54 55 |
# File 'lib/usb_pd_match/negotiator.rb', line 52 def watts(charger, device) result = negotiate(charger, device) result ? result.watts : 0.0 end |