Class: Beaglebone::UARTDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/beaglebone/uart.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(uart, speed = 9600) ⇒ UARTDevice

Initialize a UART device. Returns a UARTDevice object

Examples:

uart1 = UARTDevice.new(:UART1, 9600)

Parameters:

  • uart

    should be a symbol representing the UART device

  • speed (defaults to: 9600)

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



455
456
457
458
# File 'lib/beaglebone/uart.rb', line 455

def initialize(uart, speed=9600)
  @uart = uart
  UART::setup(@uart, speed)
end

Instance Method Details

#disableObject

Note:

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

Disable a UART device.



613
614
615
# File 'lib/beaglebone/uart.rb', line 613

def disable
  UART::disable(@uart)
end

#each_char(&block) ⇒ Object

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

Examples:

uart1.each_char { |x| puts "read: #{x}" }


532
533
534
# File 'lib/beaglebone/uart.rb', line 532

def each_char(&block)
  UART::each_char(@uart, &block)
end

#each_chars(chars, &block) ⇒ Object

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

Examples:

uart1.each_chars(2) { |x| puts "read: #{x}" }

Parameters:

  • chars

    should be the number of chars to read



542
543
544
# File 'lib/beaglebone/uart.rb', line 542

def each_chars(chars, &block)
  UART::each_chars(@uart, chars, &block)
end

#each_line(&block) ⇒ Object

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

Examples:

uart1.each_line { |x| puts "read: #{x}" }


550
551
552
# File 'lib/beaglebone/uart.rb', line 550

def each_line(&block)
  UART::each_line(@uart, &block)
end

#readcharObject

Read one character from a UART device

Examples:

uart1.readchars => "x"

Returns:

  • String the character read from the UART device



502
503
504
# File 'lib/beaglebone/uart.rb', line 502

def readchar
  UART::readchar(@uart)
end

#readchars(bytes) ⇒ Object

Read characters from a UART device

Examples:

uart1.readchars(2) => "xx"

Parameters:

  • bytes

    number of bytes to read

Returns:

  • String the characters read from the UART device



514
515
516
# File 'lib/beaglebone/uart.rb', line 514

def readchars(bytes)
  UART::readchars(@uart, bytes)
end

#readlineObject

Read a line from a UART device

Examples:

uart1.readline => "A line of text"

Returns:

  • String the line read from the UART device



524
525
526
# File 'lib/beaglebone/uart.rb', line 524

def readline
  UART::readline(@uart)
end

#run_on_each_char(callback, repeats = nil) ⇒ Object

Convenience method for run_on_each_chars with chars set to 1

See Also:



601
602
603
# File 'lib/beaglebone/uart.rb', line 601

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) ⇒ Object

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

Examples:

callback = lambda { |uart, data, count| puts "[#{uart}:#{count}] #{data} "}
uart1.run_on_each_chars(callback, 2)

Parameters:

  • callback

    A method to call when the data is received. This method should take 3 arguments, the UART, the line read, and the counter

  • chars (defaults to: 1)

    should be the number of chars to read

  • repeats (defaults to: nil)

    is optional and specifies the number of times the callback will be run



583
584
585
# File 'lib/beaglebone/uart.rb', line 583

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) ⇒ Object

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

Examples:

callback = lambda { |uart, line, count| puts "[#{uart}:#{count}] #{line} "}
uart1.run_on_each_line(callback)

Parameters:

  • callback

    A method to call when the data is received. This method should take 3 arguments, the UART, the line read, and the counter

  • repeats (defaults to: nil)

    is optional and specifies the number of times the callback will be run



563
564
565
# File 'lib/beaglebone/uart.rb', line 563

def run_on_each_line(callback, repeats=nil)
  UART::run_on_each_line(callback, @uart, repeats)
end

#run_once_on_each_char(callback) ⇒ Object

Convenience method for run_on_each_chars with chars and repeats set to 1

See Also:



589
590
591
# File 'lib/beaglebone/uart.rb', line 589

def run_once_on_each_char(callback)
  UART::run_once_on_each_char(callback, @uart)
end

#run_once_on_each_chars(callback, chars = 1) ⇒ Object

Convenience method for run_on_each_chars with chars and repeats set to 1

See Also:



595
596
597
# File 'lib/beaglebone/uart.rb', line 595

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) ⇒ Object

Convenience method for run_on_each_line with repeats set to 1

See Also:



569
570
571
# File 'lib/beaglebone/uart.rb', line 569

def run_once_on_each_line(callback)
  UART::run_once_on_each_line(callback, @uart)
end

#set_speed(speed) ⇒ Object

Set the speed of the UART

Examples:

uart1.set_speed(9600)

Parameters:

  • speed

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



466
467
468
# File 'lib/beaglebone/uart.rb', line 466

def set_speed(speed)
  UART::set_speed(@uart, speed)
end

#stop_read_waitObject

Stops any threads waiting for data on the specified UART



606
607
608
# File 'lib/beaglebone/uart.rb', line 606

def stop_read_wait
  UART::stop_read_wait(@uart)
end

#write(data) ⇒ Object

Write data to a UART device

Examples:

uart1.write("1234") => 4

Parameters:

  • data

    the data to write

Returns:

  • Integer the number of bytes written



478
479
480
# File 'lib/beaglebone/uart.rb', line 478

def write(data)
  UART::write(@uart, data)
end

#writeln(data) ⇒ Object

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

Examples:

uart1.writeln("1234") => 5

Parameters:

  • data

    the data to write

Returns:

  • Integer the number of bytes written

See Also:



492
493
494
# File 'lib/beaglebone/uart.rb', line 492

def writeln(data)
  UART::writeln(@uart, data)
end