Module: PiPiper

Extended by:
PiPiper
Included in:
PiPiper
Defined in:
lib/pi_piper.rb,
lib/pi_piper/i2c.rb,
lib/pi_piper/pwm.rb,
lib/pi_piper/spi.rb,
lib/pi_piper/pin.rb,
lib/pi_piper/version.rb,
lib/pi_piper/bcm2835.rb,
lib/pi_piper/platform.rb,
lib/pi_piper/pin_error.rb,
lib/pi_piper/pin_values.rb,
lib/pi_piper/stub_driver.rb

Defined Under Namespace

Modules: Bcm2835, PinValues, StubDriver Classes: I2C, NullLogger, Pin, PinError, Platform, Pwm, Spi

Constant Summary

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.



34
35
36
37
# File 'lib/pi_piper.rb', line 34

def after(options, &block)
  options[:trigger] = options.delete(:goes) == :high ? :rising : :falling
  watch options, &block
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.



45
46
47
48
49
50
51
52
# File 'lib/pi_piper.rb', line 45

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



76
77
78
# File 'lib/pi_piper.rb', line 76

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`.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pi_piper.rb', line 13

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.



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

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