Class: Rufirmata::Pin

Inherits:
Object
  • Object
show all
Includes:
Observables::Base
Defined in:
lib/rufirmata/pin.rb

Overview

A Pin representation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, pin_number, pin_type, port = nil) ⇒ Pin

Returns a new instance of Pin.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rufirmata/pin.rb', line 8

def initialize(board,pin_number,pin_type,port = nil)
  @board = board
  @port = port
  @pin_number = pin_number
  @pin_type = pin_type
  @reporting = false
  @value = nil
  @pwm = false
  @mode = Rufirmata::INPUT
  if pin_type == Rufirmata::DIGITAL
    @pwm = board.board_type[:pwm_pins].include?(pin_number)
    @mode = board.board_type[:disabled_pins].include?(pin_number) ?
    Rufirmata::UNAVAILABLE : Rufirmata::OUTPUT
  end
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



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

def board
  @board
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#pin_numberObject (readonly)

Returns the value of attribute pin_number.



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

def pin_number
  @pin_number
end

#pin_typeObject (readonly)

Returns the value of attribute pin_type.



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

def pin_type
  @pin_type
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#pwmObject (readonly)

Returns the value of attribute pwm.



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

def pwm
  @pwm
end

#reportingObject

Returns the value of attribute reporting.



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

def reporting
  @reporting
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#disable_reportingObject

Disable the reporting of an input pin



75
76
77
78
79
80
81
82
# File 'lib/rufirmata/pin.rb', line 75

def disable_reporting
  if pin_type == Rufirmata::ANALOG
    @reporting = false
    board.write_command(Rufirmata::REPORT_ANALOG + pin_number, 0)
  elsif port
    port.disable_reporting
  end
end

#enable_reportingObject

Set an input pin to report values



64
65
66
67
68
69
70
71
72
# File 'lib/rufirmata/pin.rb', line 64

def enable_reporting
  raise "#{to_s} is not an input and therefore cannot report" unless mode == Rufirmata::INPUT
  if pin_type == Rufirmata::ANALOG
    self.reporting = true
    board.write_command(Rufirmata::REPORT_ANALOG + pin_number, 1)
  elsif port
    port.enable_reporting
  end
end

#readObject

Returns the output value of the pin. This value is updated by the boards ‘Board.iterate` method. Value is always in the range 0.0 - 1.0



86
87
88
89
# File 'lib/rufirmata/pin.rb', line 86

def read
  raise "Cannot read pin #{to_s} because it is marked as UNAVAILABLE" if mode == Rufirmata::UNAVAILABLE
  value
end

#send_sysex(sysex_cmd, data = []) ⇒ Object

write



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rufirmata/pin.rb', line 110

def send_sysex(sysex_cmd, data=[])
  #Sends a SysEx message.
  # :arg sysex_cmd: A sysex command byte
  # :arg data: A list of data values
  board.write_command(Rufirmata::START_SYSEX)
  board.write_command(systex_cmd)
  data.each do |byte|
    byte = begin; byte.chr; rescue RangeError; (byte >> 7).chr; end
    board.write(byte)
  end
  board.write_command(Rufirmata::END_SYSEX)
end

#to_sObject



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

def to_s
  type = self.pin_type == Rufirmata::ANALOG ? "Analog" : "Digital"
  "#{type} pin #{pin_number}"
end

#write(new_value) ⇒ Object

Output a voltage from the pin

:arg value: Uses value as a boolean if the pin is in output mode, or
    expects a float from 0 to 1 if the pin is in PWM mode.


95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rufirmata/pin.rb', line 95

def write(new_value)
  raise "#{to_s} cannot be used through Firmata" if mode == Rufirmata::UNAVAILABLE
  raise "#{to_s} is set up as an INPUT and therefore cannot be written to" if mode == Rufirmata::INPUT
  if (new_value != value)
    self.value = new_value
    if mode == Rufirmata::OUTPUT
      port ? port.write() :
        board.write_command(Rufirmata::DIGITAL_MESSAGE, pin_number, value)
    elsif mode == Rufirmata::PWM
      val = (@value * 255).to_i
      board.write_command(Rufirmata::ANALOG_MESSAGE + pin_number, val % 128, val >> 7)
    end
  end
end