Class: WallE::Pin

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wall_e/pin.rb

Constant Summary collapse

UnsupportedModeError =
Class.new(StandardError)
INPUT =

Internal: Fixnum byte for pin mode input.

0x00
OUTPUT =

Internal: Fixnum byte for pin mode output.

0x01
ANALOG =

Internal: Fixnum byte for pin mode analog.

0x02
PWM =

Internal: Fixnum byte for pin mode pulse width modulation.

0x03
SERVO =

Internal: Fixnum byte for pin mode servo.

0x04
LOW =
0
HIGH =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, board, mode = OUTPUT) ⇒ Pin

Public: Initialize a Pin

number - the Integer pin number on the board. board - the WallE::Board this pin is on. mode - the Fixnum mode to set the pin to (default: OUTPUT).



32
33
34
35
36
37
38
# File 'lib/wall_e/pin.rb', line 32

def initialize(number, board, mode = OUTPUT)
  @number = number
  @board = board
  @onboard_pin = @board.pins[@number]
  @reporting = false
  set_mode(mode)
end

Instance Attribute Details

#numberObject (readonly)

Public: Returns the Integer pin number.



23
24
25
# File 'lib/wall_e/pin.rb', line 23

def number
  @number
end

Instance Method Details

#analog_write(value) ⇒ Object Also known as: servo_write

Public: Write analog value to the pin

value - an Integer value.

Returns nothing.



54
55
56
# File 'lib/wall_e/pin.rb', line 54

def analog_write(value)
  @board.analog_write(@number, value)
end

#current_modeObject

Public: Get the current mode.

Returns Integer mode.



79
80
81
# File 'lib/wall_e/pin.rb', line 79

def current_mode
  @onboard_pin.mode
end

#digital_write(value) ⇒ Object

Public: Write a digital value to the pin.

value - an Integer value.

Returns nothing.



45
46
47
# File 'lib/wall_e/pin.rb', line 45

def digital_write(value)
  @board.digital_write(@number, value)
end

#reporting?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/wall_e/pin.rb', line 108

def reporting?
  @reporting
end

#set_mode(mode) ⇒ Object

Public: Set the pin mode.

mode - an Integer mode.

Returns nothing. Raises UnsupportedModeError if the pin does not support the mode.



71
72
73
74
# File 'lib/wall_e/pin.rb', line 71

def set_mode(mode)
  raise UnsupportedModeError unless @onboard_pin.supported_modes.include?(mode)
  @board.set_pin_mode(@number, mode) unless current_mode == mode
end

#start_reportingObject

Public: Start pin reporting.

Returns nothing. Raises UnsupportedModeError if the pin’s mode is not INPUT.



94
95
96
97
98
# File 'lib/wall_e/pin.rb', line 94

def start_reporting
  raise UnsupportedModeError unless current_mode == INPUT
  @board.toggle_pin_reporting(@number)
  @reporting = true
end

#stop_reportingObject

Public: Stop pin reporting.

Returns nothing.



103
104
105
106
# File 'lib/wall_e/pin.rb', line 103

def stop_reporting
  @reporting = false
  @board.toggle_pin_reporting(@number, LOW)
end

#valueObject

Public: Get the current value of the pin.

Returns Integer value.



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

def value
  @onboard_pin.value
end