Class: Rufirmata::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/rufirmata/port.rb

Overview

An 8-bit port on the board

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, port_number) ⇒ Port

Returns a new instance of Port.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rufirmata/port.rb', line 8

def initialize(board, port_number)
  @board = board
  @port_number = port_number
  @reporting = false
  @pins = []
  (0..7).each do |pin|
    pin_num = pin + port_number * 8
    pins << (pin = Rufirmata::Pin.new(board, pin_num, Rufirmata::DIGITAL, self))
    board.digital << pin
  end
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



5
6
7
# File 'lib/rufirmata/port.rb', line 5

def board
  @board
end

#pinsObject (readonly)

Returns the value of attribute pins.



5
6
7
# File 'lib/rufirmata/port.rb', line 5

def pins
  @pins
end

#port_numberObject (readonly)

Returns the value of attribute port_number.



5
6
7
# File 'lib/rufirmata/port.rb', line 5

def port_number
  @port_number
end

#reportingObject

Returns the value of attribute reporting.



6
7
8
# File 'lib/rufirmata/port.rb', line 6

def reporting
  @reporting
end

Instance Method Details

#disable_reportingObject

Disable reporting of values for the whole port



30
31
32
33
# File 'lib/rufirmata/port.rb', line 30

def disable_reporting
  @reporting = false
  board.write_command(Rufirmata::REPORT_DIGITAL + port_number, 0)
end

#enable_reportingObject

Enable reporting of values for the whole port



23
24
25
26
27
# File 'lib/rufirmata/port.rb', line 23

def enable_reporting
  @reporting = true
  board.write_command(Rufirmata::REPORT_DIGITAL + port_number, 1)
  pins.each {|pin|pin.reporting = true}
end

#to_sObject



20
# File 'lib/rufirmata/port.rb', line 20

def to_s; "Digital Port #{port_number}"; end

#update(mask) ⇒ Object

Update the values for the pins marked as input with the mask



50
51
52
53
54
55
56
57
58
59
# File 'lib/rufirmata/port.rb', line 50

def update(mask)
  if reporting
    pins.each do |pin|
      if pin.mode == Rufirmata::INPUT
        pin_nr = pin.pin_number - port_number * 8
        pin.value = (mask & (1 << pin_nr)) > 1
      end
    end
  end
end

#writeObject

Set the output pins of the port to the correct state



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rufirmata/port.rb', line 36

def write
  mask = 0
  pins.each do |pin|
    if (pin.mode == Rufirmata::OUTPUT)
      if (pin.value == 1)
        pin_nr = pin.pin_number - port_number * 8
        mask |= 1 << pin_nr
      end
    end
  end
  board.write_command(Rufirmata::DIGITAL_MESSAGE + port_number, mask % 128, mask >> 7)
end