Class: WiringPi::GPIO

Inherits:
Object
  • Object
show all
Defined in:
lib/wiringpi/gpio.rb,
lib/wiringpi/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ GPIO

Returns a new instance of GPIO.



9
10
11
12
13
# File 'lib/wiringpi/gpio.rb', line 9

def initialize(&block)
  Wiringpi2.wiringPiSetup
  @pins = Array.new
  instance_eval &block if block_given?
end

Instance Attribute Details

#modulesObject (readonly)

Returns the value of attribute modules.



7
8
9
# File 'lib/wiringpi/gpio.rb', line 7

def modules
  @modules
end

#pinsObject (readonly)

Returns the value of attribute pins.



7
8
9
# File 'lib/wiringpi/gpio.rb', line 7

def pins
  @pins
end

Instance Method Details

#add_module(module_instance) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/wiringpi/gpio.rb', line 128

def add_module(module_instance)
  @modules = Array.new if @modules.nil?

  @modules << module_instance
  puts 'Added module ' + module_instance.name.to_s

  module_instance.pin_count.times do |offset|
    @pins[offset + module_instance.pin_base] = 'ENABLED'
  end
end

#delay(ms) ⇒ Object



60
61
62
# File 'lib/wiringpi/gpio.rb', line 60

def delay(ms)
  Wiringpi2.delay(ms)
end

#delay_microseconds(ms) ⇒ Object



64
65
66
# File 'lib/wiringpi/gpio.rb', line 64

def delay_microseconds(ms)
  Wiringpi2.delayMicroseconds(ms)
end

#digital_read(pin) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/wiringpi/gpio.rb', line 32

def digital_read(pin)
  if pin.respond_to?(:each)
    pin.collect do |pin|
      Wiringpi2.digitalRead(pin)
    end
  else
    Wiringpi2.digitalRead(pin)
  end
end

#digital_write(pin, value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/wiringpi/gpio.rb', line 42

def digital_write(pin, value)
  if pin.respond_to?(:each)
    pin.each do |pin|
      Wiringpi2.digitalWrite(pin, value)
    end
  else
    Wiringpi2.digitalWrite(pin, value)
  end
end

#gpio_clock_set(pin, freq) ⇒ Object



108
109
110
# File 'lib/wiringpi/gpio.rb', line 108

def gpio_clock_set(pin, freq)
  return Wiringpi2.gpioClockSet(pin, freq)
end

#interrupt(pin, edge, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/wiringpi/event.rb', line 3

def interrupt(pin, edge, &block)
	Thread.new do
		@value = digital_read(pin)
		@last_value = value
		loop do
			@last_value = @value
			@value = digital_read(pin)

			if @value != @last_value
				next if @value == 0 and edge == :falling_edge
				next if @value == 1 and edge == :rising_edge
				break
			end
			block.call @value				
		end
	end
end

#microsObject



72
73
74
# File 'lib/wiringpi/gpio.rb', line 72

def micros()
  return Wiringpi2.micros()
end

#millisObject



68
69
70
# File 'lib/wiringpi/gpio.rb', line 68

def millis()
  return Wiringpi2.millis()
end

#phys_pin_to_gpio(pin) ⇒ Object



84
85
86
# File 'lib/wiringpi/gpio.rb', line 84

def phys_pin_to_gpio(pin)
   return Wiringpi2.physPinToGpio(pin)
end

#pi_board_revObject



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

def pi_board_rev()
   return Wiringpi2.piBoardRev()
end

#pin_mode(pin, mode) ⇒ Object



52
53
54
# File 'lib/wiringpi/gpio.rb', line 52

def pin_mode(pin, mode)
  Wiringpi2.pinMode(pin, mode)
end

#pull_up_dn_control(pin, mode) ⇒ Object



56
57
58
# File 'lib/wiringpi/gpio.rb', line 56

def pull_up_dn_control(pin,mode)
  Wiringpi2.pullUpDnControl(pin, mode)
end

#pwm_set_clock(divisor) ⇒ Object



96
97
98
# File 'lib/wiringpi/gpio.rb', line 96

def pwm_set_clock(divisor)
  return Wiringpi2.pwmSetClock(divisor)
end

#pwm_set_mode(mode) ⇒ Object



88
89
90
# File 'lib/wiringpi/gpio.rb', line 88

def pwm_set_mode(mode)
  return Wiringpi2.pwmSetMode(mode)
end

#pwm_set_range(range) ⇒ Object



92
93
94
# File 'lib/wiringpi/gpio.rb', line 92

def pwm_set_range(range)
  return Wiringpi2.pwmSetRange(range)
end

#read_byte(starting_pin) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/wiringpi/gpio.rb', line 15

def read_byte(starting_pin)
  bits = Array.new
  8.times do |time|
    bits << Wiringpi2.digitalRead(starting_pin + time)
  end
  bits.join
end

#shift_in(dpin, cpin, order) ⇒ Object



124
125
126
# File 'lib/wiringpi/gpio.rb', line 124

def shift_in(dpin, cpin, order)
  Wiringpi2.shiftIn(dpin,cpin,order)
end

#shift_out(dpin, cpin, order, val) ⇒ Object



120
121
122
# File 'lib/wiringpi/gpio.rb', line 120

def shift_out(dpin, cpin, order, val )
  Wiringpi2.shiftOut(dpin,cpin,order,val)
end

#soft_pwm_create(pin, initial_value, pwm_range) ⇒ Object



100
101
102
# File 'lib/wiringpi/gpio.rb', line 100

def soft_pwm_create(pin, initial_value, pwm_range)
  return Wiringpi2.softPwmCreate(pin, initial_value, pwm_range)
end

#soft_pwm_write(pin, value) ⇒ Object



104
105
106
# File 'lib/wiringpi/gpio.rb', line 104

def soft_pwm_write(pin, value)
  Wiringpi2.softPwmWrite(pin, value)
end

#wait_for_interrupt(pin, ms) ⇒ Object



112
113
114
# File 'lib/wiringpi/gpio.rb', line 112

def wait_for_interrupt(pin, ms)
  Wiringpi2.waitForInterrupt(pin, ms)
end

#wiringpi_isr(pin, mode, fn) ⇒ Object



116
117
118
# File 'lib/wiringpi/gpio.rb', line 116

def wiringpi_isr(pin, mode, fn)
  Wiringpi2.wiringPiISR(pin, mode, fn)
end

#wpi_pin_to_gpio(pin) ⇒ Object



80
81
82
# File 'lib/wiringpi/gpio.rb', line 80

def wpi_pin_to_gpio(pin)
   return Wiringpi2.wpiPinToGpio(pin)
end

#write_byte(starting_pin, byte) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/wiringpi/gpio.rb', line 23

def write_byte(starting_pin, byte)
  byte = byte.to_s(2) unless byte.length = 8
  offset = starting_pin
  byte.each_char do |bit|
    Wiringpi2.digitalWrite(offset, bit)
    offset += 1
  end
end