Class: Denko::UART::Hardware

Inherits:
Object
  • Object
show all
Includes:
Behaviors::Callbacks, Behaviors::SinglePin
Defined in:
lib/denko/uart/hardware.rb

Instance Attribute Summary collapse

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::SinglePin

#mode, #pin

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#baudObject (readonly)

Returns the value of attribute baud.



7
8
9
# File 'lib/denko/uart/hardware.rb', line 7

def baud
  @baud
end

#indexObject (readonly)

Returns the value of attribute index.



7
8
9
# File 'lib/denko/uart/hardware.rb', line 7

def index
  @index
end

Instance Method Details

#after_initialize(options = {}) ⇒ Object



20
21
22
23
# File 'lib/denko/uart/hardware.rb', line 20

def after_initialize(options={})
  initialize_buffer
  start(options[:baud] ||= 9600)
end

#before_initialize(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/denko/uart/hardware.rb', line 9

def before_initialize(options={})
  if options[:index] && (options[:index] > 0) && (options[:index] < 4)
    @index = options[:index]
  else
    raise ArgumentError, "UART index (#{options[:index]}) not given or out of range (1..3)"
  end

  # Set pin to a "virtual pin" in 251 - 253 that will match the board.
  options[:pin] = 250 + options[:index]
end

#flushObject



45
46
47
48
49
# File 'lib/denko/uart/hardware.rb', line 45

def flush
  @buffer_mutex.synchronize do
    @buffer = ""
  end
end

#getsObject



35
36
37
38
39
40
41
42
43
# File 'lib/denko/uart/hardware.rb', line 35

def gets
  @buffer_mutex.synchronize do
    newline = @buffer.index("\n")
    return nil unless newline
    line = @buffer[0..newline-1]
    @buffer = @buffer[newline+1..-1]
    return line
  end
end

#initialize_bufferObject



25
26
27
28
29
30
31
32
33
# File 'lib/denko/uart/hardware.rb', line 25

def initialize_buffer
  @buffer       = ""
  @buffer_mutex = Mutex.new
  self.add_callback(:buffer) do |data|
    @buffer_mutex.synchronize do
      @buffer = "#{@buffer}#{data}"
    end
  end
end

#start(baud) ⇒ Object



51
52
53
54
# File 'lib/denko/uart/hardware.rb', line 51

def start(baud)
  @baud = baud
  board.uart_start(index, baud, true)
end

#stopObject



56
57
58
# File 'lib/denko/uart/hardware.rb', line 56

def stop()
  board.uart_stop(index)
end

#write(data) ⇒ Object



60
61
62
# File 'lib/denko/uart/hardware.rb', line 60

def write(data)
  board.uart_write(index, data)
end