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



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

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.



688
689
690
# File 'lib/beaglebone/uart.rb', line 688

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}" }


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

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



617
618
619
# File 'lib/beaglebone/uart.rb', line 617

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}" }


625
626
627
# File 'lib/beaglebone/uart.rb', line 625

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



577
578
579
# File 'lib/beaglebone/uart.rb', line 577

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



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

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



599
600
601
# File 'lib/beaglebone/uart.rb', line 599

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:



676
677
678
# 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) ⇒ 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



658
659
660
# 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) ⇒ 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



638
639
640
# 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) ⇒ Object

Convenience method for run_on_each_chars with chars and repeats set to 1

See Also:



664
665
666
# 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) ⇒ Object

Convenience method for run_on_each_chars with chars and repeats set to 1

See Also:



670
671
672
# 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) ⇒ Object

Convenience method for run_on_each_line with repeats set to 1

See Also:



644
645
646
# 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) ⇒ Object

Set the speed of the UART

Examples:

uart1.set_speed(9600)

Parameters:

  • speed

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



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

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

#stop_read_waitObject

Stops any threads waiting for data on the specified UART



681
682
683
# File 'lib/beaglebone/uart.rb', line 681

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



553
554
555
# File 'lib/beaglebone/uart.rb', line 553

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:



567
568
569
# File 'lib/beaglebone/uart.rb', line 567

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