Module: Raudi::AVR::PinStates

Included in:
Pin
Defined in:
lib/raudi/avr/pin_states.rb

Constant Summary collapse

DELIMITER =
'_'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/raudi/avr/pin_states.rb', line 9

def self.included(klass)
  
  klass.class_eval do

    attr_accessor :states, :state, :state_params

    Info.pin_states.each do |pin_state|

      define_method :get_state do |pin_state|
        states.detect { |state| state =~ Regexp.new("\\A(#{pin_state})(_\d+)?") }
      end

      define_method :get_state! do |pin_state|
        valid_state = get_state(pin_state)
        raise "#{to_s} can be #{pin_state}" unless valid_state
        valid_state
      end

      define_method "#{pin_state}!" do |*args|
        self.state = get_state!(pin_state)
        self.state_params = args.first
      end

      define_method "#{pin_state}?" do |*args|
        self.state == get_state(pin_state)
      end

      define_method "can_be_#{pin_state}?" do |*args|
        return unless full_state = get_state(pin_state)
        if(number = args.first)
          return unless full_state[number.to_s]
        end
        true
      end

    end

  end

end

Instance Method Details

#state_numberObject



50
51
52
# File 'lib/raudi/avr/pin_states.rb', line 50

def state_number
  state.split(DELIMITER).last
end

#to_cObject



54
55
56
57
58
59
60
# File 'lib/raudi/avr/pin_states.rb', line 54

def to_c
  if Info.gpio_states.include?(state)
    "PIN#{name}#{number}"
  else
    state.to_s.gsub(DELIMITER, '').upcase
  end
end