Class: PiFacer::FIO

Inherits:
Object
  • Object
show all
Defined in:
lib/pi_facer/io.rb

Overview

Represents a Piface io on the Raspberry Pi

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction: :in, invert: true, trigger: :both, **options) ⇒ FIO

Initializes a new Piface io.

Parameters:

  • options (Hash)

    A hash of options

Options Hash (**options):

  • :io (Fixnum)

    The io number to initialize. Required.

  • :direction (Symbol)

    The direction of communication, either :in or :out. Defaults to :in.

  • :invert (Boolean)

    Indicates if the value read from the physical io should be inverted. Defaults to false.

  • :trigger (Symbol)

    Indicates when the wait_for_change method will detect a change, either :rising, :falling, or :both edge triggers. Defaults to :both.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pi_facer/io.rb', line 13

def initialize(direction: :in, invert: true, trigger: :both, **options)
	raise "Invalid direction. Options are :in or :out" unless [:in, :out].include? direction
	raise "Invalid IO, Piface IO numbers go from 1 to 8" unless (1..8).include? options[:io]
	raise 'Invalid trigger' unless %I[rising falling both].include? trigger 

	@io        = options[:io]
	@direction = direction
	@invert    = invert
	@trigger   = trigger
 
	read
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



4
5
6
# File 'lib/pi_facer/io.rb', line 4

def direction
  @direction
end

#invertObject (readonly)

Returns the value of attribute invert.



4
5
6
# File 'lib/pi_facer/io.rb', line 4

def invert
  @invert
end

#ioObject (readonly)

Returns the value of attribute io.



4
5
6
# File 'lib/pi_facer/io.rb', line 4

def io
  @io
end

#last_valueObject (readonly)

Returns the value of attribute last_value.



4
5
6
# File 'lib/pi_facer/io.rb', line 4

def last_value
  @last_value
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/pi_facer/io.rb', line 4

def value
  @value
end

Instance Method Details

#changed?Boolean

Tests if the logic level has changed since the io was last read.

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/pi_facer/io.rb', line 53

def changed?
	read
	last_value != value
end

#offObject

If the io has been initialized for output this method will set the logic level low.



37
38
39
# File 'lib/pi_facer/io.rb', line 37

def off
	Piface.write( @io, 0 )  if direction == :out
end

#off?Boolean

Tests if the logic level is low.

Returns:

  • (Boolean)


42
43
44
# File 'lib/pi_facer/io.rb', line 42

def off?
	value == 0
end

#onObject

If the io has been initialized for output this method will set the logic level high.



27
28
29
# File 'lib/pi_facer/io.rb', line 27

def on
	Piface.write( @io, 1 )  if direction == :out
end

#on?Boolean

Tests if the logic level is high.

Returns:

  • (Boolean)


32
33
34
# File 'lib/pi_facer/io.rb', line 32

def on?
	not off?
end

#readObject



69
70
71
72
73
# File 'lib/pi_facer/io.rb', line 69

def read 
	@last_value = @value
	val         = Piface.read @io
	@value      = invert ? (val ^ 1) : val
end

#update_value(new_value) ⇒ Object

If the io has been initialized for output this method will either raise or lower the logic level depending on ‘new_value`.

Parameters:

  • new_value (Object)

    If false or 0 the io will be set to off, otherwise on.



48
49
50
# File 'lib/pi_facer/io.rb', line 48

def update_value(new_value)
	!new_value || new_value == 0 ? off : on
end

#wait_for_changeObject

blocks until a logic level change occurs. The initializer option ‘:trigger` modifies what edge this method will release on.



59
60
61
62
63
64
65
66
67
# File 'lib/pi_facer/io.rb', line 59

def wait_for_change
	loop do
		if changed?
			next if @trigger == :rising  and @value == 0
			next if @trigger == :falling and @value == 1
			break
		end
	end
end