Module: Denko::UART::Common

Included in:
BitBang, Hardware
Defined in:
lib/denko/uart/common.rb

Instance Method Summary collapse

Instance Method Details

#flushObject



26
27
28
29
30
# File 'lib/denko/uart/common.rb', line 26

def flush
  @buffer_mutex.lock
  @buffer = ""
  @buffer_mutex.unlock
end

#getsObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/denko/uart/common.rb', line 14

def gets
  line = nil
  @buffer_mutex.lock
  newline = @buffer.index("\n")
  if newline
    line = @buffer[0..newline-1]
    @buffer = @buffer[newline+1..-1]
  end
  @buffer_mutex.unlock
  line
end

#initialize_bufferObject



4
5
6
7
8
9
10
11
12
# File 'lib/denko/uart/common.rb', line 4

def initialize_buffer
  @buffer       = String.new
  @buffer_mutex = Denko.gil? ? Denko::MutexStub.new : Mutex.new
  self.add_callback(:buffer) do |data|
    @buffer_mutex.lock
    @buffer = "#{@buffer}#{data}"
    @buffer_mutex.unlock
  end
end