Class: Radio::Controls::CIV

Inherits:
Object
  • Object
show all
Defined in:
lib/radio/controls/civ.rb

Overview

You can use this somewhat asynchronously but if you queue up multiple commands of the same type or multiple commands that return OK/NG then it can’t be perfect. This is a limitation of the CI-V protocol, not the implementation. While this makes my inner engineer cringe, it does work perfectly fine for user interfaces and typical applications.

Constant Summary collapse

TIMEOUT =

An exception is raised when commands do not get a response.

0.5
WATCHDOG =

Commands are retried automatically when we’re not seeing messages. This happens when you have a collision. Also consider that older radios will need longer to respond.

0.2
DWELL =

Cache responses briefly

0.1

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CIV

Returns a new instance of CIV.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/radio/controls/civ.rb', line 44

def initialize options={}
  @semaphore = Mutex.new
  @queue = Queue.new
  @host = options[:host]|| 0xE0 # my address
  @device = options[:device]|| 0x50 # radio address
  port = options[:port]|| 0
  @baud = options[:baud]|| 1200
  bits = options[:bits]|| 8
  stop = options[:stop]|| 1
  parity = options[:parity]|| SerialPort::NONE
  @io = SerialPort.new port, @baud, bits, stop, parity
  setup options[:compat]
  @state = :WaitPreamble1
  @carrier_sense = 0
  @last_message = Time.now
  @machine = Thread.new &method(:machine)
  @watchdog = Thread.new &method(:watchdog)
  @lo = nil
  raise "Icom CI-V device not found" unless lo
end

Instance Method Details

#loObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/radio/controls/civ.rb', line 73

def lo
  @semaphore.synchronize do
    return @lo if @lo and Time.now < @lo_expires
  end
  begin
    lo = command 3
    @semaphore.synchronize do
      @lo_expires = Time.now + DWELL
      @lo = lo
    end
  rescue RuntimeError => e
    # defeat timeout when spinning the dial
    # we can pick up the value from the 0x00 updates
    @lo
    # will all commands timeout while spinning?
  end
end

#lo=(freq) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/radio/controls/civ.rb', line 91

def lo= freq
  unless command 5, num_to_bcd(freq * 1000000, 5)
    raise 'Unsupported frequency'
  end
  @semaphore.synchronize do
    @lo = nil
  end
end

#stopObject



65
66
67
68
69
70
71
# File 'lib/radio/controls/civ.rb', line 65

def stop
  @machine.kill
  @watchdog.kill
  @machine.join
  @watchdog.join
  @io.close
end