Class: Denko::UART::BitBang

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

Instance Attribute Summary collapse

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::MultiPin

#pin, #pins, #proxies

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::MultiPin

#before_initialize, #convert_pins, #proxy_pin, #proxy_states, #require_pin, #require_pins

Methods included from Behaviors::Component

#initialize, #micro_delay

Instance Attribute Details

#baudObject (readonly)

Returns the value of attribute baud.



12
13
14
# File 'lib/denko/uart/bit_bang.rb', line 12

def baud
  @baud
end

Instance Method Details

#after_initialize(options = {}) ⇒ Object



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

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

#flushObject



45
46
47
48
49
# File 'lib/denko/uart/bit_bang.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/bit_bang.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

#hook_rx_callbackObject



64
65
66
# File 'lib/denko/uart/bit_bang.rb', line 64

def hook_rx_callback
  rx.add_callback {|data| self.update(data)}
end

#initialize_bufferObject



25
26
27
28
29
30
31
32
33
# File 'lib/denko/uart/bit_bang.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

#initialize_pins(options = {}) ⇒ Object



14
15
16
17
# File 'lib/denko/uart/bit_bang.rb', line 14

def initialize_pins(options={})
  require_pin(:tx)
  proxy_pin(:rx, UARTRxPin)
end

#start(baud) ⇒ Object



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

def start(baud)
  @baud = baud
  board.uart_bb_start(pins[:tx], pins[:rx], @baud)
end

#stopObject



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

def stop()
  board.uart_bb_stop
end

#write(data) ⇒ Object



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

def write(data)
  board.uart_bb_write(data)
end