Class: Elemac::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(vendor: 0x4d8, product: 0x3f, debug: false) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
# File 'lib/elemac/connection.rb', line 5

def initialize(vendor: 0x4d8, product: 0x3f, debug: false)
  @debug = debug
  @dev = HIDAPI::open(vendor, product)
  throw 'Cannot open ELEMAC HID' unless @dev
end

Instance Method Details

#read_dataObject

Reading data is in form ‘rXYYY’ where X is bit length, YYY is the address in hex Read data ends on byte 0



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/elemac/connection.rb', line 24

def read_data
  raw = @dev.read
  response = ""
  debug_read(raw) if @debug

  raw.each_byte { |x| 
    break if x == 0 
    response += x.chr # Get char (for hex)

  }
  # Reverse bit order

  response = response.scan(/../).reverse.join
  response
end

#request_data(address) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/elemac/connection.rb', line 11

def request_data(address)
  bytes = Array.new(64, 0)
  length = address.length - 1
  bytes[0..length] = address.bytes 
  # Write command to device

  @dev.write(bytes)
  # Read result. 

  response = read_data # this hex form, transform it to number

  response.hex
end