class PiDriver::Pin

Attributes

gpio_number[R]

Public Class Methods

new(gpio_number, options = {}) click to toggle source
# File lib/pi_driver/pin.rb, line 9
def initialize(gpio_number, options = {})
  @argument_helper = Utils::ArgumentHelper.new prefix: 'PiDriver::Pin'

  @gpio_number = gpio_number
  @argument_helper.check(:gpio_number, @gpio_number, Board::VALID_NUMBERS)

  @argument_helper.prefix = "PiDriver::Pin ##{gpio_number}"

  @direction = options[:direction] || Direction::INPUT
  @argument_helper.check(:direction, @direction, Direction::VALID_DIRECTIONS)

  @state = options[:state] || Utils::State::LOW
  @argument_helper.check(:state, @state, Utils::State::VALID_STATES)

  @file_helper = FileHelper.new @gpio_number
  @file_helper.write_export
  @file_helper.write_direction(@direction)
  input? ? @file_helper.read_value : @file_helper.write_value(@state)
end

Public Instance Methods

clear() click to toggle source
# File lib/pi_driver/pin.rb, line 50
def clear
  return unless output?
  @state = Utils::State::LOW
  @file_helper.write_value(@state)
  @state
end
Also aliased as: off
clear?() click to toggle source
# File lib/pi_driver/pin.rb, line 59
def clear?
  state == Utils::State::LOW
end
Also aliased as: off?
clear_interrupt() click to toggle source
# File lib/pi_driver/pin.rb, line 91
def clear_interrupt
  @interrupt&.clear
end
input() click to toggle source
# File lib/pi_driver/pin.rb, line 29
def input
  @direction = Direction::INPUT
  @file_helper.write_direction(@direction)
end
input?() click to toggle source
# File lib/pi_driver/pin.rb, line 34
def input?
  @direction == Direction::INPUT
end
interrupt(edge = Utils::Edge::RISING) { || ... } click to toggle source
# File lib/pi_driver/pin.rb, line 84
def interrupt(edge = Utils::Edge::RISING)
  @argument_helper.check(:edge, edge, Utils::Edge::VALID_EDGES)
  @edge = edge
  @interrupt = Utils::Interrupt.new(@edge) { @file_helper.read_value }
  @interrupt.start { yield }
end
off()
Alias for: clear
off?()
Alias for: clear?
on()
Alias for: set
on?()
Alias for: set?
output(state = Utils::State::LOW) click to toggle source
# File lib/pi_driver/pin.rb, line 38
def output(state = Utils::State::LOW)
  @argument_helper.check(:state, state, Utils::State::VALID_STATES)
  @state = state
  @direction = Direction::OUTPUT
  @file_helper.write_direction(@direction)
  @file_helper.write_value(@state)
end
output?() click to toggle source
# File lib/pi_driver/pin.rb, line 46
def output?
  @direction == Direction::OUTPUT
end
set() click to toggle source
# File lib/pi_driver/pin.rb, line 65
def set
  return unless output?
  @state = Utils::State::HIGH
  @file_helper.write_value(@state)
  @state
end
Also aliased as: on
set?() click to toggle source
# File lib/pi_driver/pin.rb, line 74
def set?
  state == Utils::State::HIGH
end
Also aliased as: on?
state() click to toggle source
# File lib/pi_driver/pin.rb, line 80
def state
  input? ? @file_helper.read_value : @state
end