Class: OrigenLink::Server::Pin

Inherits:
Object
  • Object
show all
Defined in:
lib/origen_link/server/pin.rb

Overview

The server pin class is used to perform IO using the UDOO pins

Constant Summary collapse

@@pin_setup =
{
  in:  'in',
  out: 'out'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ionumber, direction = :in) ⇒ Pin

Export the pin io files through the system and take control of the IO

This method will execute system command “echo #ionumber > /sys/class/gpio/export” to create the IO file interface. It will set the direction, initial pin state and initialize instance variables

Receives

ionumber - required, value indicating the pin number (BCM IO number,
           not the header pin number)
direction - optional, specifies the pin direction.  A pin is
            initialized as an input if a direction isn't specified.


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/origen_link/server/pin.rb', line 38

def initialize(ionumber, direction = :in)
  @ionumber = Integer(ionumber)
  @pin_dir_name = "#{Server.gpio_dir}/gpio#{@ionumber}/direction"
  @pin_val_name = "#{Server.gpio_dir}/gpio#{@ionumber}/value"
  if !File.exist?(@pin_dir_name)
    system("echo #{@ionumber} > #{Server.gpio_dir}/export")
    sleep 0.05
    if $CHILD_STATUS == 0
      @gpio_valid = true
    else
      @gpio_valid = false
    end
  else
    @gpio_valid = true
  end
  if @gpio_valid
    if File.writable?(@pin_dir_name)
      @pin_dir_obj = File.open(@pin_dir_name, 'w')
      update_direction(direction)
    else
      @gpio_valid = false
      puts "#{@pin_dir_name} is not writable. Fix permissions or run as super user."
    end
    @pin_val_obj = File.open(@pin_val_name, 'r+') if @gpio_valid
  end
  @pattern_data = ''
  @pattern_index = -1
  @response = 'W'
  @cycle_failure = false
  @data_is_drive = false
end

Instance Attribute Details

#gpio_validObject (readonly)

True if this pin exists in /sys/class/gpio after export. False otherwise.



16
17
18
# File 'lib/origen_link/server/pin.rb', line 16

def gpio_valid
  @gpio_valid
end

#pattern_dataObject

This pin’s vector data for the pattern cycle being executed



18
19
20
# File 'lib/origen_link/server/pin.rb', line 18

def pattern_data
  @pattern_data
end

#pattern_indexObject

This pin’s index in the vector data received from the plug-in app



20
21
22
# File 'lib/origen_link/server/pin.rb', line 20

def pattern_index
  @pattern_index
end

#responseObject

This pin’s response for the executing vector



22
23
24
# File 'lib/origen_link/server/pin.rb', line 22

def response
  @response
end

Instance Method Details

#cycle_failureObject

cycle_failure

returns a boolean indicating pass/fail status of this pin


84
85
86
87
88
89
90
# File 'lib/origen_link/server/pin.rb', line 84

def cycle_failure
  if @response == 'W'
    true		# force failure if no operation performed
  else
    @cycle_failure
  end
end

#data_is_compare?Boolean

data_is_compare?

returns whether the current pattern data is compare

Returns:

  • (Boolean)


78
79
80
# File 'lib/origen_link/server/pin.rb', line 78

def data_is_compare?
  !@data_is_drive
end

#data_is_drive?Boolean

data_is_drive?

returns whether the current pattern data is drive

Returns:

  • (Boolean)


72
73
74
# File 'lib/origen_link/server/pin.rb', line 72

def data_is_drive?
  @data_is_drive
end

#destroyObject

Close the file IO objects associated with this pin



151
152
153
154
155
156
157
158
# File 'lib/origen_link/server/pin.rb', line 151

def destroy
  if @gpio_valid
    @pin_dir_obj.close
    @pin_val_obj.close
    # system("echo #{@ionumber} > /sys/class/gpio/unexport")
    # puts "pin #{@ionumber} is no longer exported"
  end
end

#inObject

Reads and returns state of the pin.

If the pin is setup as an output, the direction will first be changed to input.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/origen_link/server/pin.rb', line 180

def in
  if @gpio_valid
    if @direction == :out
      update_direction(:in)
    end
    # below is original read - slow to reopen every time
    # File.open(@pin_val_name, 'r') do |file|
    #  file.read#.chomp
    # end
    # end original read
    @pin_val_obj.pos = 0
    @pin_val_obj.getc
  end
end

#load_pattern_data(cycle) ⇒ Object

load_pattern_data

Grab this pin's data character from the pattern data


94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/origen_link/server/pin.rb', line 94

def load_pattern_data(cycle)
  if @pattern_index > -1
    @pattern_data = cycle[@pattern_index]
    @response = 'W'
    @cycle_failure = false
    if @pattern_data == '1' || @pattern_data == '0'
      @data_is_drive = true
    else
      @data_is_drive = false
    end
  else
    @gpio_valid = false
  end
end

#out(value) ⇒ Object

Sets the output state of the pin.

If the pin is setup as an input, the direction will first be changed to output.



165
166
167
168
169
170
171
172
173
# File 'lib/origen_link/server/pin.rb', line 165

def out(value)
  if @gpio_valid
    if @direction == :in
      update_direction(:out)
    end
    @pin_val_obj.write(value)
    @pin_val_obj.flush
  end
end

#process_event(operation, requested_action) ⇒ Object

process_event(event)

perform the requested pin operation and update the response (if required)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/origen_link/server/pin.rb', line 111

def process_event(operation, requested_action)
  if operation == :drive
    if data_is_drive?
      if requested_action == 'data'
        out(@pattern_data)
        @response = @pattern_data
      else
        out(requested_action)
      end # requested_action == 'data'
    end # data_is_drive?
  end # operation == :drive
  if operation == :compare
    if data_is_compare?
      if requested_action == 'data'
        case self.in
          when '0'
            @cycle_failure = true if @pattern_data == 'H'
            if @pattern_data == 'X'
              @response = '.'
            else
              @response = 'L'
            end
          # end of when '0'
          when '1'
            @cycle_failure = true if @pattern_data == 'L'
            if @pattern_data == 'X'
              @response = '`'
            else
              @response = 'H'
            end
          # end of when '1'
          else
            @response = 'W'
        end # case
      end # requested_action == 'data'
    end # data_is_compare?
  end # operation == :compare
end

#to_sObject

Returns ‘OrigenLinPin’ + io number



213
214
215
# File 'lib/origen_link/server/pin.rb', line 213

def to_s
  'OrigenLinkPin' + @ionumber.to_s
end

#update_direction(direction) ⇒ Object

Sets the pin direction

Receives:

direction - specifies the pin direction.  Input is default.

Valid direction values:
  :in	-	input
  :out	-	output


203
204
205
206
207
208
209
210
# File 'lib/origen_link/server/pin.rb', line 203

def update_direction(direction)
  if @gpio_valid
    @pin_dir_obj.pos = 0
    @pin_dir_obj.write(@@pin_setup[direction])
    @pin_dir_obj.flush
    @direction = direction
  end
end