Module: Denko::Connection::Handshake

Included in:
Base
Defined in:
lib/denko/connection/handshake.rb

Constant Summary collapse

HANDSHAKE_TRIES =
3
HANDSHAKE_TIMEOUT =
2

Instance Method Summary collapse

Instance Method Details

#handshakeObject

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/denko/connection/handshake.rb', line 24

def handshake
  io_reset
  HANDSHAKE_TRIES.times do |retries|
    begin
      print "Sending handshake to: #{self.to_s}... "
      self.add_observer(attempt = HandshakeAttempt.new)
      write Message.encode(command: 90)

      Timeout.timeout(HANDSHAKE_TIMEOUT) do
        loop do
          sleep 0.001
          if attempt.acknowledged
            puts "Acknowledged. Hardware ready...\n\n"
            
            # Stop the handshake attempt from observing.
            self.delete_observer(attempt)
            
            # We reset the board, so reset the transit mutex.
            @transit_mutex.synchronize { @transit_bytes = 0 }
            
            # Return the data part of the ACK line.
            return attempt.result
          end
        end
      end
    rescue Timeout::Error
      self.delete_observer(attempt)
      print "No response, "
      puts (retries + 1 < HANDSHAKE_TRIES ? "retrying..." : "exiting...")
      next
    end
  end
  raise HandshakeError, "Connected to wrong device, or device not running denko"
end