Skip to content

Commit 9ff5262

Browse files
committed
screen.rb wraps for screen command
- It watches reconnect of screen - Besides, picoruby updated
1 parent b811388 commit 9ff5262

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

bin/screen.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'fileutils'
4+
5+
DEFAULT_DEVICE = '/dev/ttyUSB0'
6+
DEFAULT_BAUDRATE = '115200'
7+
8+
if ARGV.include?('--help') || ARGV.include?('-h')
9+
puts <<~HELP
10+
This script connects to a serial device using 'screen' and automatically
11+
reconnects if the connection is lost. It waits for the device to become
12+
available if it's not currently connected.
13+
Usage: ruby screen.rb [device] [baud_rate]
14+
Arguments:
15+
device - The serial device to connect to (default: #{DEFAULT_DEVICE})
16+
baud_rate - The baud rate for the connection (default: #{DEFAULT_BAUDRATE})
17+
Example:
18+
ruby screen.rb #{DEFAULT_DEVICE} #{DEFAULT_BAUDRATE}
19+
HELP
20+
exit
21+
end
22+
23+
DEVICE = ARGV[0] || DEFAULT_DEVICE
24+
BAUD_RATE = ARGV[1] || DEFAULT_BAUDRATE
25+
26+
def device_exists?(device)
27+
File.exist?(device) && File.readable?(device)
28+
end
29+
30+
def start_screen(device, baud_rate)
31+
puts "#{Time.now}: Connecting to #{device} at #{baud_rate} baud..."
32+
system("screen #{device} #{baud_rate}")
33+
exit_status = $?.exitstatus
34+
puts "#{Time.now}: Screen session exited with status #{exit_status}."
35+
end
36+
37+
def wait_for_device(device)
38+
puts "#{Time.now}: Waiting for #{device}..."
39+
until device_exists?(device)
40+
sleep 1
41+
print "."
42+
end
43+
puts "\n#{Time.now}: Device #{device} is available!"
44+
sleep 0.5
45+
end
46+
47+
puts "Auto-reconnecting screen wrapper"
48+
puts "Device: #{DEVICE}, Baud rate: #{BAUD_RATE}"
49+
puts "Press Ctrl+C to exit"
50+
puts "-" * 40
51+
52+
trap("INT") { puts "\nExiting..."; exit 0 }
53+
54+
loop do
55+
if device_exists?(DEVICE)
56+
start_screen(DEVICE, BAUD_RATE)
57+
puts "\n#{Time.now}: Screen session ended. Reconnecting..."
58+
sleep 1
59+
else
60+
wait_for_device(DEVICE)
61+
end
62+
end

lib/picoruby

Submodule picoruby updated 40 files

0 commit comments

Comments
 (0)