Class: CW::Winkey

Inherits:
Object
  • Object
show all
Defined in:
lib/cw/winkey.rb

Instance Method Summary collapse

Constructor Details

#initializeWinkey

Returns a new instance of Winkey.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#check_status(byte) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#closeObject



31
32
33
34
# File 'lib/cw/winkey.rb', line 31

def close
  puts 'Closing'
  @serial.close
end

#command(cmd) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/cw/winkey.rb', line 79

def command cmd
  {
    on: "\x00\x02",
    no_weighting: "\x03\x32",
    echo: "\x00\x04\x5A"
  }[cmd]
end

#echoObject



98
99
100
101
102
# File 'lib/cw/winkey.rb', line 98

def echo
  puts 'echo test'
  write command :echo
  read 'Z'.ord, "echo success"
end

#getbyteObject



27
28
29
# File 'lib/cw/winkey.rb', line 27

def getbyte
  @serial.getbyte
end

#no_weightingObject



93
94
95
96
# File 'lib/cw/winkey.rb', line 93

def no_weighting
  puts 'no weighting'
  write command :no_weighting
end

#onObject



87
88
89
90
91
# File 'lib/cw/winkey.rb', line 87

def on
  puts 'host on'
  write command :on
  read 23, "on ack"
end

#read(match, match_msg) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/cw/winkey.rb', line 110

def sent?
  true
end

#string(str) ⇒ Object



50
51
52
53
54
55
56
57
# 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_sendingObject



114
115
116
117
118
# File 'lib/cw/winkey.rb', line 114

def wait_while_sending
  until sent?
    sleep 0.01
  end
end

#wpm(wpm) ⇒ Object



104
105
106
107
108
# File 'lib/cw/winkey.rb', line 104

def wpm wpm
  puts "set wpm to #{wpm}"
  write "\x02"
  write [wpm].pack('U')
end

#write(data) ⇒ Object



23
24
25
# File 'lib/cw/winkey.rb', line 23

def write data
  @serial.write data
end