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
-
#after(options, &block) ⇒ Object
Defines an event block to be executed after a pin either goes high or low.
-
#driver ⇒ Object
Returns the loaded driver.
-
#driver=(klass) ⇒ Object
Defines an event block to be executed when an pin event occurs.
-
#poll_spi(options, &block) ⇒ Object
Defines an event block to be called on a regular schedule.
-
#wait ⇒ Object
deprecated
Deprecated.
Please use EventMachine.run instead
-
#watch(options, &block) ⇒ Object
Defines an event block to be executed when an pin event occurs.
-
#when_spi(options, &block) ⇒ Object
Defines an event block to be called when SPI slave output meets certain characteristics.
Instance Method Details
#after(options, &block) ⇒ Object
Defines an event block to be executed after a pin either goes high or low.
63 64 65 66 |
# File 'lib/pi_piper.rb', line 63 def after(, &block) [:trigger] = .delete(:goes) == :high ? :rising : :falling watch , &block end |
#driver ⇒ Object
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.
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.
74 75 76 77 78 79 80 81 |
# File 'lib/pi_piper.rb', line 74 def poll_spi(, &block) EM::PeriodicTimer.new([:every]) do Spi.begin [:slave] do output = write [:write] block.call output end end end |
#wait ⇒ Object
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(, &block) new_thread = Thread.new do pin = PiPiper::Pin.new() 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.
93 94 95 96 97 98 99 100 101 |
# File 'lib/pi_piper.rb', line 93 def when_spi(, &block) poll_spi do |value| if ([:eq] && value == [:eq]) || ([:lt] && value < [:lt]) || ([:gt] && value > [:gt]) block.call value end end end |