wiringpi-ruby 2.0.1 → 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/wiringpi.rb CHANGED
@@ -1,10 +1,23 @@
1
1
  require 'wiringpi/wiringpi'
2
+
2
3
  module WiringPi
3
4
  extend self
4
5
 
6
+ # GPIO WiringPi PIN layout mode
7
+ GPIO_WPI = 0
8
+ GPIO_NORMAL = 1
9
+ GPIO_PHYSICAL = 2
10
+ GPIO_SYSTEM = 3
11
+
12
+ # GPIO signal
5
13
  HIGH = 1
6
14
  LOW = 0
7
15
 
16
+ # GPIO signal event
17
+ FALLING = :falling
18
+ RISING = :rising
19
+
20
+ # PIN mode
8
21
  INPUT = 0
9
22
  OUTPUT = 1
10
23
  PWM_OUTPUT = 1
@@ -18,25 +31,25 @@ module WiringPi
18
31
 
19
32
  class Modules
20
33
  class ModuleBase
21
-
22
- @name = 'Unknown'
23
- @pin_base = 0
24
- @pin_count = 0
25
- @pin_end = 0
26
-
27
34
  attr_reader :name, :pin_base, :pin_count, :pin_end
28
35
 
36
+ def initialize
37
+ @name = 'Unknown'
38
+ @pin_base = 0
39
+ @pin_count = 0
40
+ @pin_end = 0
41
+ end
42
+
29
43
  def new
30
44
  @pin_end = @pin_base + @pin_count
31
- updateName
45
+ update_name
32
46
  end
33
47
 
34
- def updateName
35
- @name = ' at offset ' + @pin_base.to_s
48
+ def update_name
49
+ @name = " at offset #{@pin_base}"
36
50
  end
37
-
38
51
  end
39
52
  end
40
-
41
53
  end
42
- Dir[File.dirname(__FILE__) + '/wiringpi/*.rb'].each {|file| require file }
54
+
55
+ Dir[File.dirname(__FILE__) + '/wiringpi/**/*.rb'].each { |file| require file }
data/lib/wiringpi/gpio.rb CHANGED
@@ -1,135 +1,147 @@
1
1
  module WiringPi
2
2
  class GPIO
3
+ attr_reader :modules, :pins
3
4
 
4
- @modules = Array.new
5
- @pins = Array.new
5
+ @@system_ready = false
6
6
 
7
- attr_reader :modules, :pins
7
+ def self.setup(pin_layout = GPIO_WPI)
8
+ return if @@system_ready
9
+
10
+ case pin_layout
11
+ when GPIO_WPI; Wiringpi.wiringPiSetup
12
+ when GPIO_NORMAL; Wiringpi.wiringPiSetupGpio
13
+ when GPIO_PHYSICAL; Wiringpi.wiringPiSetupPhys
14
+ when GPIO_SYSTEM; Wiringpi.wiringPiSetupSys
15
+ else raise 'unknown pin layout to setup'
16
+ end
17
+
18
+ @@system_ready = true
19
+ end
8
20
 
9
21
  def initialize(&block)
10
- Wiringpi.wiringPiSetup
11
- @pins = Array.new
22
+ raise 'system is not ready, please call WiringPi::GPIO.setup first' unless @@system_ready
23
+
24
+ @pins = []
25
+ @modules = []
26
+ setup_wiring_pi
12
27
  instance_eval &block if block_given?
13
28
  end
14
29
 
15
30
  def read_byte(starting_pin)
16
- bits = Array.new
17
- 8.times do |time|
18
- bits << Wiringpi.digitalRead(starting_pin + time)
19
- end
20
- bits.join
31
+ 8.times.map { |time| Wiringpi.digitalRead(starting_pin + time) }.join
21
32
  end
22
33
 
23
34
  def write_byte(starting_pin, byte)
24
- byte = byte.to_s(2) unless byte.length = 8
35
+ bits = byte.to_s(2) unless byte.length == 8
25
36
  offset = starting_pin
26
- byte.each_char do |bit|
37
+ bits.each_char do |bit|
27
38
  Wiringpi.digitalWrite(offset, bit)
28
39
  offset += 1
29
40
  end
30
41
  end
31
42
 
32
43
  def digital_read(pin)
33
- if pin.respond_to?(:each)
34
- pin.collect do |pin|
35
- Wiringpi.digitalRead(pin)
36
- end
37
- else
38
- Wiringpi.digitalRead(pin)
39
- end
44
+ pins = [pin] unless pin.is_a?(Array)
45
+ res = pins.collect { |pin| Wiringpi.digitalRead(pin) }
46
+ res.count == 1 ? res.first : res
40
47
  end
41
48
 
42
49
  def digital_write(pin, value)
43
- if pin.respond_to?(:each)
44
- pin.each do |pin|
45
- Wiringpi.digitalWrite(pin, value)
46
- end
47
- else
48
- Wiringpi.digitalWrite(pin, value)
49
- end
50
+ pins = [pin] unless pin.is_a?(Array)
51
+ pins.each { |pin| Wiringpi.digitalWrite(pin, value) }
52
+ end
53
+
54
+ def analog_read(pin)
55
+ pins = [pin] unless pin.is_a?(Array)
56
+ res = pins.collect { |pin| Wiringpi.analogRead(pin) }
57
+ res.count == 1 ? res.first : res
58
+ end
59
+
60
+ def analog_write(pin, value)
61
+ pins = [pin] unless pin.is_a?(Array)
62
+ pins.each { |pin| Wiringpi.analogWrite(pin, value) }
50
63
  end
51
64
 
52
65
  def pin_mode(pin, mode)
53
66
  Wiringpi.pinMode(pin, mode)
54
67
  end
55
68
 
56
- def pull_up_dn_control(pin,mode)
69
+ def pull_up_dn_control(pin, mode)
57
70
  Wiringpi.pullUpDnControl(pin, mode)
58
71
  end
59
72
 
60
- def delay(ms)
61
- Wiringpi.delay(ms)
62
- end
73
+ def delay(ms)
74
+ Wiringpi.delay(ms)
75
+ end
63
76
 
64
- def delay_microseconds(ms)
65
- Wiringpi.delayMicroseconds(ms)
66
- end
77
+ def delay_microseconds(ms)
78
+ Wiringpi.delayMicroseconds(ms)
79
+ end
67
80
 
68
- def millis()
69
- return Wiringpi.millis()
70
- end
81
+ def millis
82
+ Wiringpi.millis
83
+ end
71
84
 
72
- def micros()
73
- return Wiringpi.micros()
74
- end
85
+ def micros
86
+ Wiringpi.micros
87
+ end
75
88
 
76
- def pi_board_rev()
77
- return Wiringpi.piBoardRev()
78
- end
89
+ # NOTE: deprecated call to Wiringpi.piBoardRev
90
+ def pi_board_rev
91
+ Wiringpi.piBoardRev
92
+ end
79
93
 
80
- def wpi_pin_to_gpio(pin)
81
- return Wiringpi.wpiPinToGpio(pin)
82
- end
94
+ def wpi_pin_to_gpio(pin)
95
+ Wiringpi.wpiPinToGpio(pin)
96
+ end
83
97
 
84
- def phys_pin_to_gpio(pin)
85
- return Wiringpi.physPinToGpio(pin)
86
- end
98
+ def phys_pin_to_gpio(pin)
99
+ Wiringpi.physPinToGpio(pin)
100
+ end
87
101
 
88
- def pwm_set_mode(mode)
89
- return Wiringpi.pwmSetMode(mode)
90
- end
102
+ def pwm_set_mode(mode)
103
+ Wiringpi.pwmSetMode(mode)
104
+ end
91
105
 
92
- def pwm_set_range(range)
93
- return Wiringpi.pwmSetRange(range)
94
- end
106
+ def pwm_set_range(range)
107
+ Wiringpi.pwmSetRange(range)
108
+ end
95
109
 
96
- def pwm_set_clock(divisor)
97
- return Wiringpi.pwmSetClock(divisor)
98
- end
110
+ def pwm_set_clock(divisor)
111
+ Wiringpi.pwmSetClock(divisor)
112
+ end
99
113
 
100
- def soft_pwm_create(pin, initial_value, pwm_range)
101
- return Wiringpi.softPwmCreate(pin, initial_value, pwm_range)
102
- end
114
+ def soft_pwm_create(pin, initial_value, pwm_range)
115
+ Wiringpi.softPwmCreate(pin, initial_value, pwm_range)
116
+ end
103
117
 
104
- def soft_pwm_write(pin, value)
105
- Wiringpi.softPwmWrite(pin, value)
106
- end
118
+ def soft_pwm_write(pin, value)
119
+ Wiringpi.softPwmWrite(pin, value)
120
+ end
107
121
 
108
- def gpio_clock_set(pin, freq)
109
- return Wiringpi.gpioClockSet(pin, freq)
110
- end
122
+ def gpio_clock_set(pin, freq)
123
+ Wiringpi.gpioClockSet(pin, freq)
124
+ end
111
125
 
112
- def wait_for_interrupt(pin, ms)
113
- Wiringpi.waitForInterrupt(pin, ms)
114
- end
126
+ def wait_for_interrupt(pin, ms)
127
+ Wiringpi.waitForInterrupt(pin, ms)
128
+ end
115
129
 
116
- def wiringpi_isr(pin, mode, fn)
117
- Wiringpi.wiringPiISR(pin, mode, fn)
118
- end
130
+ def wiringpi_isr(pin, mode, fn)
131
+ Wiringpi.wiringPiISR(pin, mode, fn)
132
+ end
119
133
 
120
- def shift_out(dpin, cpin, order, val )
121
- Wiringpi.shiftOut(dpin,cpin,order,val)
122
- end
134
+ def shift_out(dpin, cpin, order, val )
135
+ Wiringpi.shiftOut(dpin,cpin,order,val)
136
+ end
123
137
 
124
- def shift_in(dpin, cpin, order)
125
- Wiringpi.shiftIn(dpin,cpin,order)
126
- end
138
+ def shift_in(dpin, cpin, order)
139
+ Wiringpi.shiftIn(dpin,cpin,order)
140
+ end
127
141
 
128
142
  def add_module(module_instance)
129
- @modules = Array.new if @modules.nil?
130
-
131
143
  @modules << module_instance
132
- puts 'Added module ' + module_instance.name.to_s
144
+ puts "Added module #{module_instance.name}"
133
145
 
134
146
  module_instance.pin_count.times do |offset|
135
147
  @pins[offset + module_instance.pin_base] = 'ENABLED'
data/lib/wiringpi/i2c.rb CHANGED
@@ -1,26 +1,33 @@
1
1
  module WiringPi
2
2
  class I2C
3
- @device_id = 0
4
- def initialize(device_address)
5
- @device_id = Wiringpi.wiringPiI2CSetup(device_address)
6
- end
7
- def read()
8
- return Wiringpi.wiringPiI2CRead(@device_id)
9
- end
10
- def write(data)
11
- return Wiringpi.wiringPiI2CWrite(@device_id,data)
12
- end
13
- def read_reg_8(reg)
14
- return Wiringpi.wiringPiI2CReadReg8(@device_id,reg)
15
- end
16
- def write_reg_8(reg,data)
17
- return Wiringpi.wiringPiI2CWriteReg8(@device_id,reg,data)
18
- end
19
- def read_reg_16(reg)
20
- return Wiringpi.wiringPiI2CReadReg16(@device_id,reg)
21
- end
22
- def write_reg_16(reg,data)
23
- return Wiringpi.wiringPiI2CWriteReg16(@device_id,reg,data)
24
- end
3
+ attr_reader :device_id
4
+
5
+ def initialize(device_address)
6
+ @device_id = Wiringpi.wiringPiI2CSetup(device_address)
7
+ end
8
+
9
+ def read
10
+ Wiringpi.wiringPiI2CRead(@device_id)
11
+ end
12
+
13
+ def write(data)
14
+ Wiringpi.wiringPiI2CWrite(@device_id, data)
15
+ end
16
+
17
+ def read_reg_8(reg)
18
+ Wiringpi.wiringPiI2CReadReg8(@device_id, reg)
19
+ end
20
+
21
+ def write_reg_8(reg, data)
22
+ Wiringpi.wiringPiI2CWriteReg8(@device_id, reg, data)
23
+ end
24
+
25
+ def read_reg_16(reg)
26
+ Wiringpi.wiringPiI2CReadReg16(@device_id, reg)
27
+ end
28
+
29
+ def write_reg_16(reg, data)
30
+ Wiringpi.wiringPiI2CWriteReg16(@device_id, reg, data)
31
+ end
25
32
  end
26
33
  end
@@ -0,0 +1,15 @@
1
+ module WiringPi
2
+ class Modules
3
+ class Mcp23017 < ModuleBase
4
+ attr_reader :i2c_address
5
+
6
+ def initialize(pin_base, i2c_address = 0x0)
7
+ super
8
+ @pin_base = pin_base
9
+ @pin_count = 16
10
+ @i2c_address = i2c_address
11
+ Wiringpi.mcp23017Setup(pin_base, i2c_address)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module WiringPi
2
+ class Modules
3
+ class Mcp23s17 < ModuleBase
4
+ attr_reader :spi_port, :device_id
5
+
6
+ def initialize(pin_base, spi_port, device_id)
7
+ super
8
+ @pin_base = pin_base
9
+ @pin_count = 16
10
+ @spi_port = spi_port || 0
11
+ @device_id = device_id || 0
12
+ Wiringpi.mcp23s17Setup(pin_base, spi_port, device_id)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,35 +1,35 @@
1
1
  module WiringPi
2
2
  class ScrollPhat
3
- def initialize()
4
- Wiringpi.scrollPhatSetup()
3
+ def initialize
4
+ Wiringpi.scrollPhatSetup
5
5
  end
6
6
 
7
7
  def printf(text)
8
8
  Wiringpi.scrollPhatPrintf(text)
9
9
  end
10
10
 
11
- def point(x, y, colour)
12
- Wiringpi.scrollPhatPoint(x, y, colour)
11
+ def point(x, y, color)
12
+ Wiringpi.scrollPhatPoint(x, y, color)
13
13
  end
14
14
 
15
- def line(x0, y0, x1, y1, colour)
16
- Wiringpi.scrollPhatLine(x0, y0, x1, y1, colour)
15
+ def line(x0, y0, x1, y1, color)
16
+ Wiringpi.scrollPhatLine(x0, y0, x1, y1, color)
17
17
  end
18
18
 
19
- def lineTo(x, y, colour)
20
- Wiringpi.scrollPhatLineTo(x, y, colour)
19
+ def lineTo(x, y, color)
20
+ Wiringpi.scrollPhatLineTo(x, y, color)
21
21
  end
22
22
 
23
- def rectangle(x1, y1, x2, y2, colour)
24
- Wiringpi.scrollPhatRectangle(x1, y1, x2, y2, colour)
23
+ def rectangle(x1, y1, x2, y2, color)
24
+ Wiringpi.scrollPhatRectangle(x1, y1, x2, y2, color)
25
25
  end
26
26
 
27
- def update()
28
- Wiringpi.scrollPhatUpdate()
27
+ def update
28
+ Wiringpi.scrollPhatUpdate
29
29
  end
30
30
 
31
- def clear()
32
- Wiringpi.scrollPhatClear()
31
+ def clear
32
+ Wiringpi.scrollPhatClear
33
33
  end
34
34
 
35
35
  def putChar(char)
@@ -1,49 +1,39 @@
1
1
  module WiringPi
2
2
  class Serial
3
-
3
+ attr_reader :id, :device, :baud
4
4
  @id = 0
5
5
  @device = '/dev/ttyAMA0'
6
6
  @baud = 9600
7
7
 
8
- def initialize(device='/dev/ttyAMA0', baud=9600)
9
-
8
+ def initialize(device = '/dev/ttyAMA0', baud = 9600)
9
+ @id = Wiringpi.serialOpen(device, baud)
10
10
  @device = device
11
11
  @baud = baud
12
-
13
- @id = Wiringpi.serialOpen(@device, @baud)
14
-
15
12
  end
16
13
 
17
14
  def serial_close
18
-
19
15
  Wiringpi.serialClose(@id)
20
- @id = 0
21
-
16
+ @id = nil
22
17
  end
23
18
 
24
19
  def serial_put_char(char)
25
-
20
+ raise 'serial connection is closed' if @id.nil?
26
21
  Wiringpi.serialPutchar(@id, char)
27
-
28
22
  end
29
23
 
30
24
  def serial_puts(string)
31
-
25
+ raise 'serial connection is closed' if @id.nil?
32
26
  Wiringpi.serialPuts(@id, string)
33
-
34
27
  end
35
28
 
36
29
  def serial_data_avail
37
-
30
+ raise 'serial connection is closed' if @id.nil?
38
31
  Wiringpi.serialDataAvail(@id)
39
-
40
32
  end
41
33
 
42
34
  def serial_get_char
43
-
35
+ raise 'serial connection is closed' if @id.nil?
44
36
  Wiringpi.serialGetchar(@id)
45
-
46
37
  end
47
-
48
38
  end
49
39
  end