Class: Rbuspirate::Interfaces::LE1WIRE

Inherits:
Abstract
  • Object
show all
Defined in:
lib/rbuspirate/interfaces/1wire.rb

Instance Method Summary collapse

Methods inherited from Abstract

#configure_peripherals

Constructor Details

#initialize(serial, bup) ⇒ LE1WIRE

Returns a new instance of LE1WIRE.



7
8
9
10
# File 'lib/rbuspirate/interfaces/1wire.rb', line 7

def initialize(serial, bup)
  raise 'Bus pirate must be in 1wire mode' unless bup.mode == :'1wire'
  @le_port = serial
end

Instance Method Details

#read(bytes = 1, readbyte_timeout: Timeouts::LE1WIRE::READ) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbuspirate/interfaces/1wire.rb', line 37

def read(bytes = 1, readbyte_timeout: Timeouts::LE1WIRE::READ)
  result = ''.dup.b
  bytes.times do
    @le_port.write(Commands::LE1WIRE::IO::READ.chr)
    Timeout.timeout(readbyte_timeout) do
      result << @le_port.read(1)
    end
  end

  result
end

#resetObject



12
13
14
15
16
17
18
# File 'lib/rbuspirate/interfaces/1wire.rb', line 12

def reset
  simplex_command(
    Commands::LE1WIRE::RESET,
    Timeouts::LE1WIRE::RESET,
    'Unable to reset external device (comm timeout/no device)'
  )
end

#write(data, write_slice_timeout: Timeouts::LE1WIRE::WRITE) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbuspirate/interfaces/1wire.rb', line 20

def write(data, write_slice_timeout: Timeouts::LE1WIRE::WRITE)
  !(data.is_a?(String) && !data.empty?) &&
    raise(ArgumentError, 'data must be non empty String instance')

  data = StringIO.new(data)
  while (slice = data.read(16))
    command = Commands::LE1WIRE::IO::WRITE | slice.bytesize - 1
    simplex_command(
      command, write_slice_timeout, 'Prepare slice write timeout'
    )
    @le_port.write(slice)
    res = @le_port.expect(Responses::SUCCESS, write_slice_timeout)
    raise 'Write timeout' unless res
  end
  true
end