Class: Rpremote::Resetter

Inherits:
Object
  • Object
show all
Defined in:
lib/rpremote/resetter.rb,
sig/rpremote.rbs

Defined Under Namespace

Classes: TimeoutError

Constant Summary collapse

DEFAULT_TIMEOUT =

Returns:

  • (Float)
20.0
RETRY_INTERVAL =

Returns:

  • (Float)
0.1
PROBE_TIMEOUT =

Returns:

  • (Float)
1.0

Instance Method Summary collapse

Constructor Details

#initialize(serial: Serial, timeout: DEFAULT_TIMEOUT, shell_class: Shell, sleeper: nil, clock: nil, port_probe: nil) ⇒ Resetter

Returns a new instance of Resetter.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rpremote/resetter.rb', line 11

def initialize(
  serial: Serial,
  timeout: DEFAULT_TIMEOUT,
  shell_class: Shell,
  sleeper: nil,
  clock: nil,
  port_probe: nil
)
  @serial = serial
  @timeout = Float(timeout)
  @shell_class = shell_class
  @sleeper = sleeper || ->(seconds) { sleep(seconds) }
  @clock = clock || -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
  @port_probe = port_probe || ->(path) { File.exist?(path) }
  raise ArgumentError, "timeout must be positive" unless @timeout.positive?
end

Instance Method Details

#reset(port_path, baud: Serial::BAUD_RATE) ⇒ String

Parameters:

  • port_path (String)
  • baud: (Integer) (defaults to: Serial::BAUD_RATE)

Returns:

  • (String)


28
29
30
31
32
33
34
# File 'lib/rpremote/resetter.rb', line 28

def reset(port_path, baud: Serial::BAUD_RATE)
  deadline = clock.call + timeout
  send_reboot(port_path, baud)
  wait_until("USB serial port to disconnect", deadline) { !port_probe.call(port_path) }
  wait_until_ready(port_path, baud, deadline)
  port_path
end