wiringpi-ruby 2.0.1 → 2.1.0

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
@@ -2,9 +2,14 @@ require 'wiringpi/wiringpi'
2
2
  module WiringPi
3
3
  extend self
4
4
 
5
+ # GPIO signal
5
6
  HIGH = 1
6
7
  LOW = 0
7
8
 
9
+ # GPIO signal event
10
+ FALLING = :falling
11
+ RISING = :rising
12
+
8
13
  INPUT = 0
9
14
  OUTPUT = 1
10
15
  PWM_OUTPUT = 1
@@ -18,25 +23,25 @@ module WiringPi
18
23
 
19
24
  class Modules
20
25
  class ModuleBase
21
-
22
- @name = 'Unknown'
23
- @pin_base = 0
24
- @pin_count = 0
25
- @pin_end = 0
26
-
27
26
  attr_reader :name, :pin_base, :pin_count, :pin_end
28
27
 
28
+ def initialize
29
+ @name = 'Unknown'
30
+ @pin_base = 0
31
+ @pin_count = 0
32
+ @pin_end = 0
33
+ end
34
+
29
35
  def new
30
36
  @pin_end = @pin_base + @pin_count
31
- updateName
37
+ update_name
32
38
  end
33
39
 
34
- def updateName
35
- @name = ' at offset ' + @pin_base.to_s
40
+ def update_name
41
+ @name = " at offset #{@pin_base}"
36
42
  end
37
-
38
43
  end
39
44
  end
40
-
41
45
  end
42
- Dir[File.dirname(__FILE__) + '/wiringpi/*.rb'].each {|file| require file }
46
+
47
+ Dir[File.dirname(__FILE__) + '/wiringpi/**/*.rb'].each { |file| require file }
data/lib/wiringpi/gpio.rb CHANGED
@@ -1,135 +1,117 @@
1
1
  module WiringPi
2
2
  class GPIO
3
-
4
- @modules = Array.new
5
- @pins = Array.new
6
-
7
3
  attr_reader :modules, :pins
8
4
 
9
5
  def initialize(&block)
10
6
  Wiringpi.wiringPiSetup
11
- @pins = Array.new
7
+ @pins = []
8
+ @modules = []
12
9
  instance_eval &block if block_given?
13
10
  end
14
11
 
15
12
  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
13
+ 8.times.map { |time| Wiringpi.digitalRead(starting_pin + time) }.join
21
14
  end
22
15
 
23
16
  def write_byte(starting_pin, byte)
24
- byte = byte.to_s(2) unless byte.length = 8
17
+ bits = byte.to_s(2) unless byte.length == 8
25
18
  offset = starting_pin
26
- byte.each_char do |bit|
19
+ bits.each_char do |bit|
27
20
  Wiringpi.digitalWrite(offset, bit)
28
21
  offset += 1
29
22
  end
30
23
  end
31
24
 
32
25
  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
26
+ pin = [pin] unless pin.respond_to?(:each)
27
+ pin.collect { |pin| Wiringpi.digitalRead(pin) }
40
28
  end
41
29
 
42
30
  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
31
+ pin = [pin] unless pin.respond_to?(:each)
32
+ pin.each { |pin| Wiringpi.digitalWrite(pin, value) }
50
33
  end
51
34
 
52
35
  def pin_mode(pin, mode)
53
36
  Wiringpi.pinMode(pin, mode)
54
37
  end
55
38
 
56
- def pull_up_dn_control(pin,mode)
39
+ def pull_up_dn_control(pin, mode)
57
40
  Wiringpi.pullUpDnControl(pin, mode)
58
41
  end
59
42
 
60
- def delay(ms)
61
- Wiringpi.delay(ms)
62
- end
43
+ def delay(ms)
44
+ Wiringpi.delay(ms)
45
+ end
63
46
 
64
- def delay_microseconds(ms)
65
- Wiringpi.delayMicroseconds(ms)
66
- end
47
+ def delay_microseconds(ms)
48
+ Wiringpi.delayMicroseconds(ms)
49
+ end
67
50
 
68
- def millis()
69
- return Wiringpi.millis()
70
- end
51
+ def millis
52
+ Wiringpi.millis
53
+ end
71
54
 
72
- def micros()
73
- return Wiringpi.micros()
74
- end
55
+ def micros
56
+ Wiringpi.micros
57
+ end
75
58
 
76
- def pi_board_rev()
77
- return Wiringpi.piBoardRev()
78
- end
59
+ # NOTE: deprecated call to Wiringpi.piBoardRev
60
+ def pi_board_rev
61
+ Wiringpi.piBoardRev
62
+ end
79
63
 
80
- def wpi_pin_to_gpio(pin)
81
- return Wiringpi.wpiPinToGpio(pin)
82
- end
64
+ def wpi_pin_to_gpio(pin)
65
+ Wiringpi.wpiPinToGpio(pin)
66
+ end
83
67
 
84
- def phys_pin_to_gpio(pin)
85
- return Wiringpi.physPinToGpio(pin)
86
- end
68
+ def phys_pin_to_gpio(pin)
69
+ Wiringpi.physPinToGpio(pin)
70
+ end
87
71
 
88
- def pwm_set_mode(mode)
89
- return Wiringpi.pwmSetMode(mode)
90
- end
72
+ def pwm_set_mode(mode)
73
+ Wiringpi.pwmSetMode(mode)
74
+ end
91
75
 
92
- def pwm_set_range(range)
93
- return Wiringpi.pwmSetRange(range)
94
- end
76
+ def pwm_set_range(range)
77
+ Wiringpi.pwmSetRange(range)
78
+ end
95
79
 
96
- def pwm_set_clock(divisor)
97
- return Wiringpi.pwmSetClock(divisor)
98
- end
80
+ def pwm_set_clock(divisor)
81
+ Wiringpi.pwmSetClock(divisor)
82
+ end
99
83
 
100
- def soft_pwm_create(pin, initial_value, pwm_range)
101
- return Wiringpi.softPwmCreate(pin, initial_value, pwm_range)
102
- end
84
+ def soft_pwm_create(pin, initial_value, pwm_range)
85
+ Wiringpi.softPwmCreate(pin, initial_value, pwm_range)
86
+ end
103
87
 
104
- def soft_pwm_write(pin, value)
105
- Wiringpi.softPwmWrite(pin, value)
106
- end
88
+ def soft_pwm_write(pin, value)
89
+ Wiringpi.softPwmWrite(pin, value)
90
+ end
107
91
 
108
- def gpio_clock_set(pin, freq)
109
- return Wiringpi.gpioClockSet(pin, freq)
110
- end
92
+ def gpio_clock_set(pin, freq)
93
+ Wiringpi.gpioClockSet(pin, freq)
94
+ end
111
95
 
112
- def wait_for_interrupt(pin, ms)
113
- Wiringpi.waitForInterrupt(pin, ms)
114
- end
96
+ def wait_for_interrupt(pin, ms)
97
+ Wiringpi.waitForInterrupt(pin, ms)
98
+ end
115
99
 
116
- def wiringpi_isr(pin, mode, fn)
117
- Wiringpi.wiringPiISR(pin, mode, fn)
118
- end
100
+ def wiringpi_isr(pin, mode, fn)
101
+ Wiringpi.wiringPiISR(pin, mode, fn)
102
+ end
119
103
 
120
- def shift_out(dpin, cpin, order, val )
121
- Wiringpi.shiftOut(dpin,cpin,order,val)
122
- end
104
+ def shift_out(dpin, cpin, order, val )
105
+ Wiringpi.shiftOut(dpin,cpin,order,val)
106
+ end
123
107
 
124
- def shift_in(dpin, cpin, order)
125
- Wiringpi.shiftIn(dpin,cpin,order)
126
- end
108
+ def shift_in(dpin, cpin, order)
109
+ Wiringpi.shiftIn(dpin,cpin,order)
110
+ end
127
111
 
128
112
  def add_module(module_instance)
129
- @modules = Array.new if @modules.nil?
130
-
131
113
  @modules << module_instance
132
- puts 'Added module ' + module_instance.name.to_s
114
+ puts "Added module #{module_instance.name}"
133
115
 
134
116
  module_instance.pin_count.times do |offset|
135
117
  @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,14 @@
1
+ module WiringPi
2
+ class Mcp23s17 < ModuleBase
3
+ attr_reader :spi_port, :device_id
4
+
5
+ def initialize(pin_base, spi_port, device_id)
6
+ super
7
+ @pin_base = pin_base
8
+ @pin_count = 16
9
+ @spi_port = spi_port || 0
10
+ @device_id = device_id || 0
11
+ Wiringpi.mcp23s17Setup(pin_base, spi_port, device_id)
12
+ end
13
+ end
14
+ 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