Class: Rpremote::Flasher
- Inherits:
-
Object
- Object
- Rpremote::Flasher
- Defined in:
- lib/rpremote/flasher.rb,
sig/rpremote.rbs
Defined Under Namespace
Classes: Error, InvalidTargetError, MountNotFoundError, Result, TimeoutError
Constant Summary collapse
- DEFAULT_VOLUMES_ROOT =
"/Volumes"- DEFAULT_TIMEOUT =
20.0- INFO_FILE =
"INFO_UF2.TXT"- UF2_BLOCK_SIZE =
512- UF2_MAGIC_START0 =
0x0A324655- UF2_MAGIC_START1 =
0x9E5D5157- UF2_MAGIC_END =
0x0AB16F30
Instance Attribute Summary collapse
-
#timeout ⇒ Float
readonly
Returns the value of attribute timeout.
-
#volumes_root ⇒ String
readonly
Returns the value of attribute volumes_root.
Instance Method Summary collapse
- #find_mount(explicit_mount = nil) ⇒ String
- #find_mounted(explicit_mount = nil) ⇒ String?
- #flash(uf2_path, mount: nil, port: nil, wait_for_port: true) ⇒ Result
-
#initialize(volumes_root: DEFAULT_VOLUMES_ROOT, timeout: DEFAULT_TIMEOUT, mount_probe: nil, port_probe: nil, sleeper: nil) ⇒ Flasher
constructor
A new instance of Flasher.
- #wait_for_mount(mount: nil) ⇒ String
Constructor Details
#initialize(volumes_root: DEFAULT_VOLUMES_ROOT, timeout: DEFAULT_TIMEOUT, mount_probe: nil, port_probe: nil, sleeper: nil) ⇒ Flasher
Returns a new instance of Flasher.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rpremote/flasher.rb', line 24 def initialize( volumes_root: DEFAULT_VOLUMES_ROOT, timeout: DEFAULT_TIMEOUT, mount_probe: nil, port_probe: nil, sleeper: nil ) @volumes_root = volumes_root @timeout = Float(timeout) @mount_probe = mount_probe || ->(path) { Dir.exist?(path) } @port_probe = port_probe || -> { Device.main_port } @sleeper = sleeper || ->(seconds) { sleep(seconds) } raise ArgumentError, "timeout must be positive" unless @timeout.positive? end |
Instance Attribute Details
#timeout ⇒ Float (readonly)
Returns the value of attribute timeout.
22 23 24 |
# File 'lib/rpremote/flasher.rb', line 22 def timeout @timeout end |
#volumes_root ⇒ String (readonly)
Returns the value of attribute volumes_root.
22 23 24 |
# File 'lib/rpremote/flasher.rb', line 22 def volumes_root @volumes_root end |
Instance Method Details
#find_mount(explicit_mount = nil) ⇒ String
51 52 53 54 55 56 57 |
# File 'lib/rpremote/flasher.rb', line 51 def find_mount(explicit_mount = nil) mounted = find_mounted(explicit_mount) return mounted if mounted raise MountNotFoundError, "Pico 2 BOOTSEL drive not found; reconnect it while holding BOOTSEL or use --mount DIR" end |
#find_mounted(explicit_mount = nil) ⇒ String?
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rpremote/flasher.rb', line 59 def find_mounted(explicit_mount = nil) if explicit_mount path = File.(explicit_mount) return unless Dir.exist?(path) return validate_mount!(path) end matches = bootsel_mounts return if matches.empty? return matches.first if matches.one? raise MountNotFoundError, "multiple Pico 2 BOOTSEL drives found; use --mount DIR" end |
#flash(uf2_path, mount: nil, port: nil, wait_for_port: true) ⇒ Result
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rpremote/flasher.rb', line 39 def flash(uf2_path, mount: nil, port: nil, wait_for_port: true) validate_firmware!(uf2_path) target = find_mount(mount) destination = File.join(target, File.basename(uf2_path)) copy_firmware(uf2_path, destination) wait_until("BOOTSEL drive to disappear") { !mount_probe.call(target) } return Result.new(mount: target, destination: destination, port: nil) unless wait_for_port detected_port = wait_until("R2P2 serial port to appear") { detect_port(port) } Result.new(mount: target, destination: destination, port: detected_port) end |
#wait_for_mount(mount: nil) ⇒ String
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rpremote/flasher.rb', line 74 def wait_for_mount(mount: nil) if mount path = File.(mount) return wait_until("Pico 2 BOOTSEL drive to appear") do next unless Dir.exist?(path) next unless valid_target?(path) path end end wait_until("Pico 2 BOOTSEL drive to appear") do matches = bootsel_mounts raise MountNotFoundError, "multiple Pico 2 BOOTSEL drives found; use --mount DIR" if matches.length > 1 matches.first end end |