raspi-gpio 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/raspi-gpio.rb +31 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4090a815424c69468a90e4967471b31fdad4b546101146c1defda003400a8faa
|
4
|
+
data.tar.gz: 6d90479e68bec065740ab4ffcd18e065ce21e0ba98db2b868620ae5ee6cb1efa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85656902aa5de465d735d7bba0954164ae5f9d8e736957f1b14688f4ff0387550b570f37a30ebe78f3228334290a0f40c9dc7ca1ef37a0e98a336bd8fc62ec79
|
7
|
+
data.tar.gz: 26ce7953054d037975ef57780af70f1162677e9f824f67bfdc7ce4552104122944c0f2355d71563ef5b20ae985de67806eb67867f1d3631fdee63fb2617cb522
|
data/lib/raspi-gpio.rb
CHANGED
@@ -1,46 +1,62 @@
|
|
1
|
+
# raspi-gpio - A simple and light interface to interact with GPIO pins of the Raspberry Pi.
|
2
|
+
# Guide and source code : https://github.com/exybore/raspi-gpio-rb
|
3
|
+
# Documentation : https://rubydoc.info/gems/raspi-gpio
|
4
|
+
#
|
5
|
+
# @author exybore
|
6
|
+
|
1
7
|
# GPIO core library path
|
2
8
|
LIB_PATH = '/sys/class/gpio'
|
3
9
|
|
4
|
-
#
|
10
|
+
# Shortcut for low pin value (0)
|
5
11
|
LOW = 0
|
12
|
+
|
13
|
+
# Shortcut for high pin value (1)
|
6
14
|
HIGH = 1
|
7
15
|
|
16
|
+
# Shortcut for 'in' pin mode
|
8
17
|
IN = 'in'
|
18
|
+
|
19
|
+
# Shortcut for 'out' pin mode
|
9
20
|
OUT = 'out'
|
10
21
|
|
11
|
-
# Exception to handle unknown pin mode (not
|
22
|
+
# Exception to handle unknown pin mode (not IN or OUT)
|
12
23
|
class UnknownMode < StandardError
|
13
24
|
end
|
14
25
|
|
26
|
+
# Exception to handle pin writing when not in OUT mode
|
15
27
|
class NotOutMode < StandardError
|
16
28
|
end
|
17
29
|
|
18
|
-
# Exception to handle bad pin value (not
|
30
|
+
# Exception to handle bad pin value (not LOW or HIGH)
|
19
31
|
class BadValue < StandardError
|
20
32
|
end
|
21
33
|
|
22
|
-
# GPIO
|
34
|
+
# Main class, to manage GPIO pins
|
23
35
|
class GPIO
|
24
36
|
|
25
37
|
# Initialize the GPIO pin
|
26
38
|
#
|
27
|
-
# @param pin [Integer] GPIO pin to use
|
28
|
-
# @param mode [String
|
39
|
+
# @param pin [Integer] GPIO pin number to use
|
40
|
+
# @param mode [String] pin mode : IN or OUT
|
29
41
|
def initialize(pin, mode = OUT)
|
30
42
|
@pin = pin
|
31
43
|
begin
|
32
|
-
File.open("#{LIB_PATH}/
|
44
|
+
File.open("#{LIB_PATH}/unexport", 'w') do |file|
|
33
45
|
file.write(@pin)
|
34
46
|
end
|
35
|
-
rescue Errno::
|
36
|
-
# -
|
47
|
+
rescue Errno::EINVAL
|
48
|
+
# Do nothing - the pin is already unexported
|
49
|
+
end
|
50
|
+
File.open("#{LIB_PATH}/export", 'w') do |file|
|
51
|
+
file.write(@pin)
|
37
52
|
end
|
38
53
|
@mode = mode
|
39
54
|
end
|
40
55
|
|
41
56
|
# Set the pin mode
|
42
57
|
#
|
43
|
-
# @param mode [String] pin mode
|
58
|
+
# @param mode [String] pin mode : IN or OUT
|
59
|
+
# @raise [UnknownMode] if the mode isn't IN or OUT
|
44
60
|
def set_mode(mode)
|
45
61
|
raise UnknownMode, "gpio error : unknown mode #{mode}" unless mode == IN or mode == OUT
|
46
62
|
File.open("#{LIB_PATH}/gpio#{@pin}/direction", 'w') do |file|
|
@@ -51,14 +67,17 @@ class GPIO
|
|
51
67
|
|
52
68
|
# Read the value of the pin
|
53
69
|
#
|
54
|
-
# @return [
|
70
|
+
# @return [Integer] pin's value : 0 or 1
|
55
71
|
def get_value
|
56
72
|
File.open("#{LIB_PATH}/gpio#{@pin}/value", 'r').read
|
57
73
|
end
|
58
74
|
|
59
75
|
# Set a value to the pin
|
76
|
+
# This method can only be used when the pin is in OUT mode
|
60
77
|
#
|
61
|
-
# @param v [Integer] the value
|
78
|
+
# @param v [Integer] the value : LOW or HIGH
|
79
|
+
# @raise [NotOutMode] if the pin isn't in OUT mode
|
80
|
+
# @raise [BadValue] if the provided value isn't LOW or HIGH
|
62
81
|
def set_value(v)
|
63
82
|
raise NotOutMode, "error : mode isn't OUT" unless @mode == OUT
|
64
83
|
raise BadValue, "error : bad pin value" unless v.between? 0,1
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raspi-gpio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Exybore
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple and light interface to interact with GPIO pins of the Raspberry
|
14
14
|
Pi.
|
@@ -41,5 +41,5 @@ requirements: []
|
|
41
41
|
rubygems_version: 3.0.1
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
|
-
summary: Simple & light Raspberry Pi GPIO interface.
|
44
|
+
summary: "\U0001F50C Simple & light Raspberry Pi GPIO interface."
|
45
45
|
test_files: []
|