wiringpi-ruby 2.1.0 → 2.1.5

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,7 +1,14 @@
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
+
5
12
  # GPIO signal
6
13
  HIGH = 1
7
14
  LOW = 0
@@ -10,6 +17,7 @@ module WiringPi
10
17
  FALLING = :falling
11
18
  RISING = :rising
12
19
 
20
+ # PIN mode
13
21
  INPUT = 0
14
22
  OUTPUT = 1
15
23
  PWM_OUTPUT = 1
data/lib/wiringpi/gpio.rb CHANGED
@@ -2,8 +2,25 @@ module WiringPi
2
2
  class GPIO
3
3
  attr_reader :modules, :pins
4
4
 
5
+ @@system_ready = false
6
+
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
20
+
5
21
  def initialize(&block)
6
- Wiringpi.wiringPiSetup
22
+ raise 'system is not ready, please call WiringPi::GPIO.setup first' unless @@system_ready
23
+
7
24
  @pins = []
8
25
  @modules = []
9
26
  instance_eval &block if block_given?
@@ -23,13 +40,25 @@ module WiringPi
23
40
  end
24
41
 
25
42
  def digital_read(pin)
26
- pin = [pin] unless pin.respond_to?(:each)
27
- pin.collect { |pin| Wiringpi.digitalRead(pin) }
43
+ pins = [pin] unless pin.is_a?(Array)
44
+ res = pins.collect { |pin| Wiringpi.digitalRead(pin) }
45
+ res.count == 1 ? res.first : res
28
46
  end
29
47
 
30
48
  def digital_write(pin, value)
31
- pin = [pin] unless pin.respond_to?(:each)
32
- pin.each { |pin| Wiringpi.digitalWrite(pin, value) }
49
+ pins = [pin] unless pin.is_a?(Array)
50
+ pins.each { |pin| Wiringpi.digitalWrite(pin, value) }
51
+ end
52
+
53
+ def analog_read(pin)
54
+ pins = [pin] unless pin.is_a?(Array)
55
+ res = pins.collect { |pin| Wiringpi.analogRead(pin) }
56
+ res.count == 1 ? res.first : res
57
+ end
58
+
59
+ def analog_write(pin, value)
60
+ pins = [pin] unless pin.is_a?(Array)
61
+ pins.each { |pin| Wiringpi.analogWrite(pin, value) }
33
62
  end
34
63
 
35
64
  def pin_mode(pin, mode)
@@ -1,14 +1,16 @@
1
1
  module WiringPi
2
- class Mcp23s17 < ModuleBase
3
- attr_reader :spi_port, :device_id
2
+ class Modules
3
+ class Mcp23s17 < ModuleBase
4
+ attr_reader :spi_port, :device_id
4
5
 
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)
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
12
14
  end
13
15
  end
14
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wiringpi-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rifqi