Class: Denko::UART::Hardware
Instance Attribute Summary collapse
#callback_mutex
#mode, #pin
#board
Instance Method Summary
collapse
#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update
#initialize, #state
#initialize, #micro_delay
Instance Attribute Details
#baud ⇒ Object
Returns the value of attribute baud.
7
8
9
|
# File 'lib/denko/uart/hardware.rb', line 7
def baud
@baud
end
|
#index ⇒ Object
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
options[:pin] = 250 + options[:index]
end
|
#flush ⇒ Object
45
46
47
48
49
|
# File 'lib/denko/uart/hardware.rb', line 45
def flush
@buffer_mutex.synchronize do
@buffer = ""
end
end
|
#gets ⇒ Object
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_buffer ⇒ Object
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
|
#stop ⇒ Object
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
|