Class: UART

Inherits:
Object
  • Object
show all
Defined in:
mrbgems/hw-uart/mrblib/uart.rb

Constant Summary collapse

PARITY_NONE =
0
PARITY_EVEN =
1
PARITY_ODD =
2
FLOW_CONTROL_NONE =
0
FLOW_CONTROL_RTS_CTS =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit:, tx_pin: -1,, rx_pin: -1,, baudrate: 9600, data_bits: 8, stop_bits: 1, parity: PARITY_NONE, flow_control: FLOW_CONTROL_NONE, rx_buffer_size: 256) ⇒ UART

Returns a new instance of UART.



10
11
12
13
14
15
16
17
18
19
# File 'mrbgems/hw-uart/mrblib/uart.rb', line 10

def initialize(unit:, tx_pin: -1, rx_pin: -1, baudrate: 9600,
               data_bits: 8, stop_bits: 1, parity: PARITY_NONE,
               flow_control: FLOW_CONTROL_NONE, rx_buffer_size: 256)
  __open_rx_buffer(rx_buffer_size)
  @unit_num = __open_connection(unit.to_s, tx_pin, rx_pin)
  @baudrate = __set_baudrate(baudrate)
  __set_format(data_bits, stop_bits, parity)
  set_flow_control(flow_control)
  @line_ending = "\n"
end

Instance Attribute Details

#baudrate ⇒ Object (readonly)

Returns the value of attribute baudrate.



8
9
10
# File 'mrbgems/hw-uart/mrblib/uart.rb', line 8

def baudrate
  @baudrate
end

Instance Method Details

#line_ending=(ending) ⇒ Object



29
30
31
32
33
34
# File 'mrbgems/hw-uart/mrblib/uart.rb', line 29

def line_ending=(ending)
  unless ["\n", "\r", "\r\n"].include?(ending)
    raise ArgumentError, "invalid line ending"
  end
  @line_ending = ending
end

#puts(str) ⇒ Object



36
37
38
39
40
# File 'mrbgems/hw-uart/mrblib/uart.rb', line 36

def puts(str)
  write str
  write @line_ending unless str.end_with?(@line_ending)
  nil
end

#setmode(baudrate: nil, data_bits: nil, stop_bits: nil, parity: nil, flow_control: nil) ⇒ Object



21
22
23
24
25
26
27
# File 'mrbgems/hw-uart/mrblib/uart.rb', line 21

def setmode(baudrate: nil, data_bits: nil, stop_bits: nil,
            parity: nil, flow_control: nil)
  @baudrate = __set_baudrate(baudrate) if baudrate
  __set_format(data_bits || 8, stop_bits || 1, parity || PARITY_NONE)
  set_flow_control(flow_control || FLOW_CONTROL_NONE)
  self
end