class CW::Winkey
Public Class Methods
new()
click to toggle source
# File lib/cw/winkey.rb, line 6 def initialize @print = Print.new STDERR.puts "Attempting to open winkey connection" port_str = "/dev/cu.usbserial-AD01XY9H" #may be different for you baud_rate = 1200 data_bits = 8 begin @serial = Serial.new(port_str, baud_rate, data_bits) @serial.closed? rescue => e p e p @serial STDERR.puts "Failed to open serial port - Exiting!" exit 1 end end
Public Instance Methods
check_status(byte)
click to toggle source
# File lib/cw/winkey.rb, line 36 def check_status byte status = byte & 192 if status == 192 # puts "status" true elsif status == 128 # puts "wpm" true else # puts "byte 0x#{byte.to_s()}" false end end
close()
click to toggle source
# File lib/cw/winkey.rb, line 31 def close puts 'Closing' @serial.close end
command(cmd)
click to toggle source
# File lib/cw/winkey.rb, line 79 def command cmd { on: "\x00\x02", no_weighting: "\x03\x32", echo: "\x00\x04\x5A" }[cmd] end
echo()
click to toggle source
# File lib/cw/winkey.rb, line 98 def echo puts 'echo test' write command :echo read 'Z'.ord, "echo success" end
getbyte()
click to toggle source
# File lib/cw/winkey.rb, line 27 def getbyte @serial.getbyte end
no_weighting()
click to toggle source
# File lib/cw/winkey.rb, line 93 def no_weighting puts 'no weighting' write command :no_weighting end
on()
click to toggle source
# File lib/cw/winkey.rb, line 87 def on puts 'host on' write command :on read 23, "on ack" end
read(match, match_msg)
click to toggle source
# File lib/cw/winkey.rb, line 59 def read match, match_msg loop_delay = 0.05 count = 1 25.times do byte = getbyte unless byte.nil? unless check_status(byte) # puts "count is, #{count}, byte is #{byte.inspect}, match is #{match.inspect}" if byte == match # print byte.ord # puts match_msg return end end end count += 1 sleep loop_delay end end
sent?()
click to toggle source
# File lib/cw/winkey.rb, line 110 def sent? true end
string(str)
click to toggle source
# File lib/cw/winkey.rb, line 50 def string str # puts str write str str.split('').each do |ip| # puts "ip = #{ip}" read ip.ord, "sent and received #{ip.chr}" end end
wait_while_sending()
click to toggle source
# File lib/cw/winkey.rb, line 114 def wait_while_sending until sent? sleep 0.01 end end
wpm(wpm)
click to toggle source
# File lib/cw/winkey.rb, line 104 def wpm wpm puts "set wpm to #{wpm}" write "\x02" write [wpm].pack('U') end
write(data)
click to toggle source
# File lib/cw/winkey.rb, line 23 def write data @serial.write data end