Class: Pigpio::BitBangSerialRx

Inherits:
Object
  • Object
show all
Defined in:
lib/pigpio/bit_bang_serial_rx.rb

Overview

Set a UserGPIO pin as a serial Rx.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rx, baud = 9600, data_bits = 8) ⇒ BitBangSerialRx

Set a UserGPIO pin as a serial Rx.

The serial data recieves in a internal cyclic buffer and is read using #read method. It is the caller’s responsibility to read data from the cyclic buffer in a timely fashion.

Parameter
rx [ Pigpio::UserGPIO ]

Set This pgio as a serial Rx.

baud [ Integer ]

50-250000 [bps]. default 9600.

data_bits [ Integer ]

1-32 [bit/word]. default 8.

See also: pigpio site



25
26
27
28
29
30
31
# File 'lib/pigpio/bit_bang_serial_rx.rb', line 25

def initialize(rx, baud = 9600, data_bits = 8)
  @rx = rx
  @valid = IF.bb_serial_read_open(@rx.pi, @rx.gpio, baud, data_bits)
  if @valid == 0
    @valid = IF.bb_serial_invert(@rx.pi, @rx.gpio, 0)
  end
end

Instance Attribute Details

#rxObject (readonly)

UserGPIO#.

This attribute is set at the time of ::new.



7
8
9
# File 'lib/pigpio/bit_bang_serial_rx.rb', line 7

def rx
  @rx
end

#validObject (readonly)

Set 0 if OK, otherwise PI_BAD_USER_GPIO, PI_BAD_WAVE_BAUD, PI_GPIO_IN_USE, PI_NOT_SERIAL_GPIO or PI_BAD_SER_INVERT.

This attribute is set at the time of ::new.



12
13
14
# File 'lib/pigpio/bit_bang_serial_rx.rb', line 12

def valid
  @valid
end

Instance Method Details

#closeObject

This method closes a GPIO for bit bang reading of serial data.

Return
Integer

Returns 0 if OK, otherwise PI_BAD_USER_GPIO, or PI_NOT_SERIAL_GPIO.

See also: pigpio site



38
39
40
# File 'lib/pigpio/bit_bang_serial_rx.rb', line 38

def close
  IF.bb_serial_read_close(@rx.pi, @rx.gpio)
end

#invert(invert = 1) ⇒ Object

This method inverts serial logic for big bang serial reads.

Parameter
Integer

invert 0-1.1 invert, 0 normal.

Return
Integer

Returns 0 if OK, otherwise PI_NOT_SERIAL_GPIO or PI_BAD_SER_INVERT.

See also: pigpio site



66
67
68
# File 'lib/pigpio/bit_bang_serial_rx.rb', line 66

def invert(invert = 1)
  IF.bb_serial_invert(@rx.pi, @rx.gpio, invert)
end

#read(bufsize = 1) ⇒ Object

This method copies up to bufSize bytes of data read from the bit bang serial cyclic buffer to the buffer starting at returned value.

Parameter
bufSize [ Integer ]

How many bytes receive.

Return
Pigpio::Buffer

A copied buffer and status.

The details of the returned status is the number of bytes copied if OK, otherwise PI_BAD_USER_GPIO or PI_NOT_SERIAL_GPIO.

The bytes returned for each character depend upon the number of data bits data_bits specified in the ::new method.

  • For data_bits 1-8 there will be one byte per character.

  • For data_bits 9-16 there will be two bytes per character.

  • For data_bits 17-32 there will be four bytes per character.

See also: pigpio site



56
57
58
# File 'lib/pigpio/bit_bang_serial_rx.rb', line 56

def read(bufsize = 1)
  IF.bb_serial_read(@rx.pi, @rx.gpio, bufsize)
end