Module: PiPiper

Extended by:
PiPiper
Included in:
PiPiper
Defined in:
lib/pi_piper.rb,
lib/pi_piper/i2c.rb,
lib/pi_piper/pin.rb,
lib/pi_piper/pwm.rb,
lib/pi_piper/spi.rb,
lib/pi_piper/driver.rb,
lib/pi_piper/version.rb

Defined Under Namespace

Classes: Driver, I2C, Pin, Pwm, Spi

Constant Summary collapse

VERSION =
'2.0.0'

Instance Method Summary collapse

Instance Method Details

#after(options, &block) ⇒ Object

Defines an event block to be executed after a pin either goes high or low.

Parameters:

  • options (Hash)

    A hash of options

Options Hash (options):

  • :pin (Fixnum)

    The pin number to initialize. Required.

  • :goes (Symbol)

    The event to watch for. Either :high or :low. Required.



63
64
65
66
# File 'lib/pi_piper.rb', line 63

def after(options, &block)
  options[:trigger] = options.delete(:goes) == :high ? :rising : :falling
  watch options, &block
end

#driverObject

Returns the loaded driver.



30
31
32
# File 'lib/pi_piper.rb', line 30

def driver
  @driver ||= PiPiper::Driver.new
end

#driver=(klass) ⇒ Object

Defines an event block to be executed when an pin event occurs.

and use with all PiPiper objects.

Parameters:



19
20
21
22
23
24
25
26
# File 'lib/pi_piper.rb', line 19

def driver=(klass) 
  if !klass.nil? && (klass <= PiPiper::Driver)
    @driver.close if @driver
    @driver = klass.new
  else
    raise ArgumentError, 'Supply a PiPiper::Driver subclass for driver'
  end
end

#poll_spi(options, &block) ⇒ Object

Defines an event block to be called on a regular schedule. The block will be passed the slave output.

Parameters:

  • options (Hash)

    A hash of options.

Options Hash (options):

  • :every (Fixnum)

    A frequency of time (in seconds) to poll the SPI slave.

  • :slave (Fixnum)

    The slave number to poll.

  • :write (Number|Array)

    Data to poll the SPI slave with.



74
75
76
77
78
79
80
81
# File 'lib/pi_piper.rb', line 74

def poll_spi(options, &block)
  EM::PeriodicTimer.new(options[:every]) do
    Spi.begin options[:slave] do
      output = write options[:write]
      block.call output
    end
  end
end

#waitObject

Deprecated.

Please use EventMachine.run instead

Prevents the main thread from exiting. Required when using PiPiper.watch



105
106
107
# File 'lib/pi_piper.rb', line 105

def wait
  loop do sleep 1 end
end

#watch(options, &block) ⇒ Object

Defines an event block to be executed when an pin event occurs.

Parameters:

options:

Options hash. Options include `:pin`, `:invert` and `:trigger`.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pi_piper.rb', line 42

def watch(options, &block)
  new_thread = Thread.new do
    pin = PiPiper::Pin.new(options)
    loop do
      pin.wait_for_change 
      if block.arity > 0
        block.call pin
      else
        pin.instance_exec &block
      end
    end 
  end
  new_thread.abort_on_exception = true
  new_thread
end

#when_spi(options, &block) ⇒ Object

Defines an event block to be called when SPI slave output meets certain characteristics. The block will be passed the slave output.

Parameters:

  • options (Hash)

    A hash of options.

Options Hash (options):

  • :every (Fixnum)

    A frequency of time (in seconds) to poll the SPI slave.

  • :slave (Fixnum)

    The slave number to poll.

  • :write (Number|Array)

    Data to poll the SPI slave with.

  • :eq (Fixnum)

    Tests for SPI slave output equality.

  • :lt (Fixnum)

    Tests for SPI slave output less than supplied value.

  • :gt (Fixnum)

    Tests for SPI slave output greater than supplied value.



93
94
95
96
97
98
99
100
101
# File 'lib/pi_piper.rb', line 93

def when_spi(options, &block)
  poll_spi options do |value|
    if (options[:eq] && value == options[:eq]) || 
       (options[:lt] && value < options[:lt])  ||
       (options[:gt] && value > options[:gt])
        block.call value
    end
  end
end