class Beaglebone::UARTDevice

Object Oriented UART Implementation. This treats the UART device as an object.

Public Class Methods

new(uart, speed=9600) click to toggle source

Initialize a UART device. Returns a UARTDevice object

@param uart should be a symbol representing the UART device @param speed should be an integer thats a valid speed. @see SPEEDS

@example

uart1 = UARTDevice.new(:UART1, 9600)
# File lib/beaglebone/uart.rb, line 530
def initialize(uart, speed=9600)
  @uart = uart
  UART::setup(@uart, speed)
end

Public Instance Methods

disable() click to toggle source

Disable a UART device.

@note device trees cannot be unloaded at this time without kernel panic.

# File lib/beaglebone/uart.rb, line 688
def disable
  UART::disable(@uart)
end
each_char(&block) click to toggle source

Read a character from a UART device and pass it to the specified block

@example

uart1.each_char { |x| puts "read: #{x}" }
# File lib/beaglebone/uart.rb, line 607
def each_char(&block)
  UART::each_char(@uart, &block)
end
each_chars(chars, &block) click to toggle source

Read characters from a UART device and pass them to the specified block

@param chars should be the number of chars to read

@example

uart1.each_chars(2) { |x| puts "read: #{x}" }
# File lib/beaglebone/uart.rb, line 617
def each_chars(chars, &block)
  UART::each_chars(@uart, chars, &block)
end
each_line(&block) click to toggle source

Read lines from a UART device and pass them to the specified block

@example

uart1.each_line { |x| puts "read: #{x}" }
# File lib/beaglebone/uart.rb, line 625
def each_line(&block)
  UART::each_line(@uart, &block)
end
readchar() click to toggle source

Read one character from a UART device

@return String the character read from the UART device

@example

uart1.readchars => "x"
# File lib/beaglebone/uart.rb, line 577
def readchar
  UART::readchar(@uart)
end
readchars(bytes) click to toggle source

Read characters from a UART device

@param bytes number of bytes to read

@return String the characters read from the UART device

@example

uart1.readchars(2) => "xx"
# File lib/beaglebone/uart.rb, line 589
def readchars(bytes)
  UART::readchars(@uart, bytes)
end
readline() click to toggle source

Read a line from a UART device

@return String the line read from the UART device

@example

uart1.readline => "A line of text"
# File lib/beaglebone/uart.rb, line 599
def readline
  UART::readline(@uart)
end
run_on_each_char(callback, repeats=nil) click to toggle source

Convenience method for run_on_each_chars with chars set to 1 @see run_on_each_chars

# File lib/beaglebone/uart.rb, line 676
def run_on_each_char(callback, repeats=nil)
  UART::run_on_each_char(callback, @uart, repeats)
end
run_on_each_chars(callback, chars=1, repeats=nil) click to toggle source

Runs a callback after receiving data from a UART device This creates a new thread that runs in the background

@param callback A method to call when the data is received. This method should take 3 arguments, the UART, the line read, and the counter @param chars should be the number of chars to read @param repeats is optional and specifies the number of times the callback will be run

@example

callback = lambda { |uart, data, count| puts "[#{uart}:#{count}] #{data} "}
uart1.run_on_each_chars(callback, 2)
# File lib/beaglebone/uart.rb, line 658
def run_on_each_chars(callback, chars=1, repeats=nil)
  UART::run_on_each_chars(callback, @uart, chars, repeats)
end
run_on_each_line(callback, repeats=nil) click to toggle source

Runs a callback after receiving a line of data from a UART device This creates a new thread that runs in the background

@param callback A method to call when the data is received. This method should take 3 arguments, the UART, the line read, and the counter @param repeats is optional and specifies the number of times the callback will be run

@example

callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
uart1.run_on_each_line(callback)
# File lib/beaglebone/uart.rb, line 638
def run_on_each_line(callback, repeats=nil)
  UART::run_on_each_line(callback, @uart, repeats)
end
run_once_on_each_char(callback) click to toggle source

Convenience method for run_on_each_chars with chars and repeats set to 1 @see run_on_each_chars

# File lib/beaglebone/uart.rb, line 664
def run_once_on_each_char(callback)
  UART::run_once_on_each_char(callback, @uart)
end
run_once_on_each_chars(callback, chars=1) click to toggle source

Convenience method for run_on_each_chars with chars and repeats set to 1 @see run_on_each_chars

# File lib/beaglebone/uart.rb, line 670
def run_once_on_each_chars(callback, chars=1)
  UART::run_once_on_each_chars(callback, @uart, chars)
end
run_once_on_each_line(callback) click to toggle source

Convenience method for run_on_each_line with repeats set to 1 @see run_on_each_line

# File lib/beaglebone/uart.rb, line 644
def run_once_on_each_line(callback)
  UART::run_once_on_each_line(callback, @uart)
end
set_speed(speed) click to toggle source

Set the speed of the UART

@param speed should be an integer thats a valid speed. @see SPEEDS

@example

uart1.set_speed(9600)
# File lib/beaglebone/uart.rb, line 541
def set_speed(speed)
  UART::set_speed(@uart, speed)
end
stop_read_wait() click to toggle source

Stops any threads waiting for data on the specified UART

# File lib/beaglebone/uart.rb, line 681
def stop_read_wait
  UART::stop_read_wait(@uart)
end
write(data) click to toggle source

Write data to a UART device

@param data the data to write

@return Integer the number of bytes written

@example

uart1.write("1234") => 4
# File lib/beaglebone/uart.rb, line 553
def write(data)
  UART::write(@uart, data)
end
writeln(data) click to toggle source

Write a line data to a UART device. This is a convenience method using write @see write

@param data the data to write

@return Integer the number of bytes written

@example

uart1.writeln("1234") => 5
# File lib/beaglebone/uart.rb, line 567
def writeln(data)
  UART::writeln(@uart, data)
end