Class: UsbPdMatch::Result

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

Overview

Result of a USB-PD negotiation between a Charger (source) and Device (sink).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chargerObject

Returns the value of attribute charger

Returns:

  • (Object)

    the current value of charger



5
6
7
# File 'lib/usb_pd_match/negotiator.rb', line 5

def charger
  @charger
end

#currentObject

Returns the value of attribute current

Returns:

  • (Object)

    the current value of current



5
6
7
# File 'lib/usb_pd_match/negotiator.rb', line 5

def current
  @current
end

#deviceObject

Returns the value of attribute device

Returns:

  • (Object)

    the current value of device



5
6
7
# File 'lib/usb_pd_match/negotiator.rb', line 5

def device
  @device
end

#voltageObject

Returns the value of attribute voltage

Returns:

  • (Object)

    the current value of voltage



5
6
7
# File 'lib/usb_pd_match/negotiator.rb', line 5

def voltage
  @voltage
end

#wattsObject

Returns the value of attribute watts

Returns:

  • (Object)

    the current value of watts



5
6
7
# File 'lib/usb_pd_match/negotiator.rb', line 5

def watts
  @watts
end

Instance Method Details

#efficiencyObject

Fraction of the device’s requested power actually delivered (0.0–1.0).



7
8
9
10
11
# File 'lib/usb_pd_match/negotiator.rb', line 7

def efficiency
  return 1.0 if device.max_watts.zero?

  [(watts / device.max_watts).round(4), 1.0].min
end

#full_power?Boolean

True when the device receives its full rated power.

Returns:

  • (Boolean)


14
15
16
# File 'lib/usb_pd_match/negotiator.rb', line 14

def full_power?
  watts >= device.max_watts - 0.01
end

#summaryObject



18
19
20
21
# File 'lib/usb_pd_match/negotiator.rb', line 18

def summary
  format("%s → %s: %.1fV @ %.2fA = %.1fW (%d%% of device rating)",
         charger.name, device.name, voltage, current, watts, (efficiency * 100).round)
end

#to_hObject



23
24
25
26
# File 'lib/usb_pd_match/negotiator.rb', line 23

def to_h
  { voltage: voltage, current: current, watts: watts,
    efficiency: efficiency, full_power: full_power? }
end