sunxi_gpio 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,145 @@
1
+ require 'sunxi-gpio/gpio_lib'
2
+ require 'sunxi_gpio/pin_values'
3
+
4
+ module SunxiGPIO
5
+ class Pin
6
+ attr_reader :pin, :last_value, :direction, :invert
7
+
8
+ include SunxiGPIO::PinValues
9
+
10
+
11
+ # Cleanup GPIO interface to make it available for other programs
12
+
13
+ def self.close
14
+ ::Gpio_lib.sunxi_gpio_cleanup
15
+ end
16
+
17
+
18
+ def self.open
19
+ ::Gpio_lib.sunxi_gpio_init
20
+ end
21
+
22
+
23
+ def initialize(options)
24
+ options = {
25
+ direction: :in,
26
+ invert: false,
27
+ pull: :off
28
+ }.merge(options)
29
+
30
+ @pin = symbol_to_pin(options[:pin])
31
+ @direction = options[:direction]
32
+ @invert = options[:invert]
33
+ @pull = options[:pull]
34
+
35
+ raise "Invalid pull mode. Options are :up, :down or :float (default)" unless [:up, :down, :float, :off].include? @pull
36
+ raise "Unable to use pull-ups : pin direction must be ':in' for this" if @direction != :in && [:up, :down].include?(@pull)
37
+ raise "Invalid direction. Options are :in or :out" unless [:in, :out].include? @direction
38
+
39
+ if @direction == :out
40
+ ::Gpio_lib.sunxi_gpio_set_cfgpin(@pin, GPIO_DIRECTION_OUTPUT)
41
+ else
42
+ ::Gpio_lib.sunxi_gpio_set_cfgpin(@pin, GPIO_DIRECTION_INPUT)
43
+ end
44
+
45
+ pull!(@pull)
46
+ read
47
+ end
48
+
49
+
50
+ # If the pin has been initialized for output this method will set the logic level high.
51
+
52
+ def on
53
+ ::Gpio_lib.sunxi_gpio_output(@pin, GPIO_HIGH) if direction == :out
54
+ end
55
+
56
+ # If the pin has been initialized for output this method will set the logic level low
57
+
58
+ def off
59
+ ::Gpio_lib.sunxi_gpio_output(@pin, GPIO_LOW) if direction == :out
60
+ end
61
+
62
+
63
+ def read
64
+ @last_value = @value
65
+ val = ::Gpio_lib.sunxi_gpio_input(@pin)
66
+ @value = invert ? (val ^ 1) : val
67
+ end
68
+
69
+
70
+ def pull!(state)
71
+ return nil if @direction != :in
72
+
73
+ @pull = case state
74
+ when :up then
75
+ GPIO_PUD_UP
76
+ when :down then
77
+ GPIO_PUD_DOWN
78
+ # :float and :off are just aliases
79
+ when :float, :off then
80
+ GPIO_PUD_OFF
81
+ else
82
+ nil
83
+ end
84
+
85
+ #### Not working yet
86
+ # ::Gpio_lib.sunxi_gpio_set_pull(@pin, @pull) if @pull
87
+ @pull
88
+ end
89
+
90
+
91
+ # If the pin direction is input, it will return the current state of pull-up/pull-down resistor,
92
+ # either :up, :down or :off.
93
+
94
+ def pull?
95
+ case @pull
96
+ when GPIO_PUD_UP then
97
+ :up
98
+ when GPIO_PUD_DOWN then
99
+ :down
100
+ else
101
+ :off
102
+ end
103
+ end
104
+
105
+
106
+ # Tests if the logic level has changed since the pin was last read.
107
+
108
+ def changed?
109
+ last_value != value
110
+ end
111
+
112
+
113
+ # Watch the pin to change to the watch_value (ON or OFF) - it only triggered
114
+ # when switching from invert value to new value
115
+
116
+ def watch(watch_value, &block)
117
+ new_thread = Thread.new do
118
+ prev_value = (self.read == GPIO_HIGH ? GPIO_LOW : GPIO_HIGH)
119
+
120
+ loop do
121
+ current_value = self.read
122
+ flip = (prev_value != current_value)
123
+
124
+ if current_value == watch_value && flip
125
+ self.instance_exec &block
126
+ end
127
+
128
+ prev_value = current_value
129
+
130
+ sleep WATCH_POLLING_SEC
131
+ end
132
+ end
133
+
134
+ new_thread.abort_on_exception = true
135
+ new_thread
136
+ end
137
+
138
+
139
+ private
140
+
141
+ def symbol_to_pin(symbol)
142
+ PINS[symbol]
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,65 @@
1
+ module SunxiGPIO
2
+ module PinValues
3
+ INIT_ERRORS = [
4
+ :SETUP_OK,
5
+ :SETUP_DEVMEM_FAIL,
6
+ :SETUP_MALLOC_FAIL,
7
+ :SETUP_MMAP_FAIL
8
+ ]
9
+
10
+ GPIO_PUD_OFF = 0
11
+ GPIO_PUD_DOWN = 1
12
+ GPIO_PUD_UP = 2
13
+
14
+ GPIO_HIGH = 1
15
+ GPIO_LOW = 0
16
+
17
+ GPIO_DIRECTION_INPUT=0
18
+ GPIO_DIRECTION_OUTPUT=1
19
+
20
+ WATCH_POLLING_SEC=0.1
21
+
22
+ # A = 0
23
+ # B = 32
24
+ # C = 64
25
+ # D = 95
26
+ # E = 128
27
+ # F = 160
28
+ # G = 192
29
+ # H = 224
30
+ # I = 256
31
+
32
+ PINS = {
33
+ PB14: 46,
34
+ PB15: 47,
35
+ PB16: 48,
36
+ PB17: 49,
37
+ PB18: 50,
38
+ PB19: 51,
39
+ PB2: 34,
40
+ PB3: 35,
41
+ PB4: 36,
42
+ PC19: 83,
43
+ PC20: 84,
44
+ PC21: 85,
45
+ PC22: 86,
46
+ PG0: 192,
47
+ PG1: 193,
48
+ PG10: 202,
49
+ PG11: 203,
50
+ PG2: 194,
51
+ PG3: 195,
52
+ PG4: 196,
53
+ PG5: 197,
54
+ PG6: 198,
55
+ PG7: 199,
56
+ PG8: 200,
57
+ PG9: 201,
58
+ PI14: 270,
59
+ PI15: 271,
60
+ PI20: 276,
61
+ PI21: 277,
62
+ PI3: 259
63
+ }
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunxi_gpio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - phortx
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ description: Native Ruby Extension to work with Sunxi GPIO
42
+ email: benny@itws.de
43
+ executables: []
44
+ extensions:
45
+ - ext/sunxi_gpio/extconf.rb
46
+ extra_rdoc_files:
47
+ - README.md
48
+ files:
49
+ - README.md
50
+ - ext/sunxi_gpio/extconf.rb
51
+ - ext/sunxi_gpio/gpio_lib.c
52
+ - ext/sunxi_gpio/gpio_lib.h
53
+ - ext/sunxi_gpio/gpio_lib_wrap.c
54
+ - lib/sunxi_gpio/pin.rb
55
+ - lib/sunxi_gpio/pin_values.rb
56
+ homepage: https://github.com/phortx/sunxi_gpio_gem
57
+ licenses:
58
+ - GNU General Public License v3
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.9.3
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Native Ruby Extension to work with Sunxi GPIO
80
+ test_files: []