Class: Mindset::Connection

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

Overview

A connection to a Mindset device. This wraps the Serial connection to the device. Device must already be paired and have a serial bluetooth connection established.

Defined Under Namespace

Classes: TimeoutError

Constant Summary collapse

SERIAL_PORT =
"/dev/rfcomm0"
BAUD_RATE =
57600
BT_SYNC =
0xAA

Instance Method Summary collapse

Constructor Details

#initialize(device = nil) ⇒ Connection

Returns a new instance of Connection.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mindset.rb', line 156

def initialize(device=nil)
  if device.is_a?(String)
    initialize_serialport device
  elsif device.nil?
    initialize_serialport SERIAL_PORT
  else
    @sp = device
  end
  # Note: Mutex causes crashes when used with qtbindings
  @locked = false
end

Instance Method Details

#disconnectObject



194
195
196
# File 'lib/mindset.rb', line 194

def disconnect
  @sp.close
end

#initialize_serialport(dev) ⇒ Object



169
170
171
# File 'lib/mindset.rb', line 169

def initialize_serialport dev
  @sp = Serial.new dev, BAUD_RATE
end

#read_packet(verbose = false) ⇒ Object

Return an Array of Packet objects. Note: this will perform a blocking read on the serial device.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mindset.rb', line 177

def read_packet(verbose=false)
  return [] if @locked
  @locked = true

  pkts = []
  if wait_for_byte(BT_SYNC) and wait_for_byte(BT_SYNC)
    plen = @sp.getbyte
    if plen and plen < BT_SYNC
      pkts = read_payload(plen, verbose)
    else
      $stderr.puts "Invalid packet size: #{plen} bytes" if verbose
    end
  end
  @locked = false
  pkts
end