mini_gpio 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +3 -0
- data/lib/mini_gpio.rb +99 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fa0fb9630fe2bd0541dc3c475b68b26e8ee2fd35ef23a2e3b0b891819d821b7f
|
4
|
+
data.tar.gz: 1104d20b03d9bce3815d5cfdd3ae18ac1020baa9203c7dc703366c78a08f2b83
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f421e15bf431a95eaef9949fe75682353d3bc637f68e85b146591d9e7c5388f34a6317ac94cb1ac2a97558c12bb750126e3b29cd75f431017ccd73d4ea2202aa
|
7
|
+
data.tar.gz: 823db4751fcc798fb6fb28c94cd0575f44740e2c87fa72eb42d2d714386a34b340c34b0d3a6aee1b87652d9d6ba25f690101256f6ff26d92deca6eb1fb85be5c
|
data/README.md
ADDED
data/lib/mini_gpio.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require "fiddle"
|
2
|
+
require "fiddle/import"
|
3
|
+
|
4
|
+
class MiniGPIO
|
5
|
+
module Mmap
|
6
|
+
PROT_READ = 0x01
|
7
|
+
PROT_WRITE = 0x02
|
8
|
+
MAP_SHARED = 0x001
|
9
|
+
|
10
|
+
extend Fiddle::Importer
|
11
|
+
|
12
|
+
dlload Fiddle::Handle::DEFAULT
|
13
|
+
|
14
|
+
extern "uint32_t * mmap(void *addr, size_t length, int prot, int flags, int fd, int offset)"
|
15
|
+
extern "int munmap(void *addr, size_t length)"
|
16
|
+
end
|
17
|
+
|
18
|
+
GPSET0 = 7
|
19
|
+
GPSET1 = 8
|
20
|
+
GPCLR0 = 10
|
21
|
+
GPCLR1 = 11
|
22
|
+
GPLEV0 = 13
|
23
|
+
|
24
|
+
# GPIO modes
|
25
|
+
module Modes
|
26
|
+
INPUT = 0
|
27
|
+
OUTPUT = 1
|
28
|
+
ALT0 = 4
|
29
|
+
ALT1 = 5
|
30
|
+
ALT2 = 6
|
31
|
+
ALT3 = 7
|
32
|
+
ALT4 = 3
|
33
|
+
ALT5 = 2
|
34
|
+
end
|
35
|
+
|
36
|
+
attr_reader :ptr
|
37
|
+
|
38
|
+
def initialize
|
39
|
+
@ptr = File.open("/dev/gpiomem", File::Constants::RDWR | File::Constants::SYNC) do |f|
|
40
|
+
Mmap.mmap(nil, 4*1024, Mmap::PROT_READ|Mmap::PROT_WRITE, Mmap::MAP_SHARED, f.to_i, 0)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Get the pin mode
|
45
|
+
def mode pin
|
46
|
+
reg = pin / 10
|
47
|
+
shift = (pin % 10) * 3
|
48
|
+
(get_int_at(reg) >> shift) & 7
|
49
|
+
end
|
50
|
+
|
51
|
+
# Set the pin mode
|
52
|
+
def set_mode pin, mode
|
53
|
+
reg = pin / 10
|
54
|
+
shift = (pin % 10) * 3
|
55
|
+
new_value = (get_int_at(reg) & ~(7 << shift)) | (mode << shift)
|
56
|
+
set_int_at(reg, new_value)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Read the pin
|
60
|
+
def read pin
|
61
|
+
0 != get_int_at(GPLEV0 + PI_BANK(pin)) & PI_BIT(pin) ? 1 : 0
|
62
|
+
end
|
63
|
+
|
64
|
+
# Write to the pin
|
65
|
+
def write pin, value
|
66
|
+
if value == 0
|
67
|
+
set_int_at(GPCLR0 + PI_BANK(pin), PI_BIT(pin))
|
68
|
+
else
|
69
|
+
set_int_at(GPSET0 + PI_BANK(pin), PI_BIT(pin))
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def PI_BANK gpio ; gpio >> 5; end
|
76
|
+
def PI_BIT gpio ; (1 << (gpio&0x1F)); end
|
77
|
+
|
78
|
+
def get_int_at offset
|
79
|
+
@ptr[offset * Fiddle::SIZEOF_INT, Fiddle::SIZEOF_INT].unpack1("L")
|
80
|
+
end
|
81
|
+
|
82
|
+
def set_int_at offset, value
|
83
|
+
@ptr[offset * Fiddle::SIZEOF_INT, Fiddle::SIZEOF_INT] = [value].pack("L")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
if __FILE__ == $0
|
88
|
+
gpio = MiniGPIO.new
|
89
|
+
|
90
|
+
54.times do |i|
|
91
|
+
puts "gpio=#{i} mode=%d level=%d" % [gpio.mode(i), gpio.read(i)]
|
92
|
+
end
|
93
|
+
|
94
|
+
gpio.set_mode 0, MiniGPIO::Modes::OUTPUT
|
95
|
+
gpio.write 0, 1
|
96
|
+
p gpio.read 0
|
97
|
+
gpio.write 0, 0
|
98
|
+
p gpio.read 0
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mini_gpio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Patterson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Provides GPIO access on the Raspberry Pi in Ruby (using Fiddle)
|
14
|
+
email:
|
15
|
+
- aaron@tenderlovemaking.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/mini_gpio.rb
|
22
|
+
homepage: https://github.com/tenderlove/mini_gpio
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.3.0
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.1.2
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: GPIO on the Raspberry Pi
|
45
|
+
test_files: []
|