Class: UsbPdMatch::Device

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

Overview

A USB-C Power Delivery sink (the device being charged: phone, laptop, power bank, earbuds case, etc.). It accepts up to a maximum voltage and draws up to a maximum power.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, max_watts:, max_voltage: 20) ⇒ Device

Returns a new instance of Device.



10
11
12
13
14
15
# File 'lib/usb_pd_match/device.rb', line 10

def initialize(name:, max_watts:, max_voltage: 20)
  @name = name
  @max_watts = max_watts.to_f
  @max_voltage = Integer(max_voltage)
  freeze
end

Instance Attribute Details

#max_voltageObject (readonly)

Returns the value of attribute max_voltage.



8
9
10
# File 'lib/usb_pd_match/device.rb', line 8

def max_voltage
  @max_voltage
end

#max_wattsObject (readonly)

Returns the value of attribute max_watts.



8
9
10
# File 'lib/usb_pd_match/device.rb', line 8

def max_watts
  @max_watts
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/usb_pd_match/device.rb', line 8

def name
  @name
end

Instance Method Details

#current_ceiling_at(voltage) ⇒ Object

Max current this device will pull at a given voltage, bounded by its power ceiling and the 5 A cable limit.



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

def current_ceiling_at(voltage)
  return 0.0 if voltage > max_voltage

  [(max_watts / voltage).round(2), 5.0].min
end

#to_hObject



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

def to_h
  { name: name, max_watts: max_watts, max_voltage: max_voltage }
end