lgpio 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3694843b060710d0fdf63c790b2857cd00bb37943fb80b52b4bb0e884c9eac63
4
+ data.tar.gz: 5749c5dbcaca251af6f5b2c6c3d189bcf1226f67ebabfc0dd418dee49361da77
5
+ SHA512:
6
+ metadata.gz: e7cd82024168e24f5a776a39b364a7c9f2432de7de6d1966517c686d04acaddf338186074b7b7597051e9521bc93115292223e66a7fa34b2fcf7bb02090ae0ae
7
+ data.tar.gz: 6855f0811581f7c50803cf1372a9d3927399e547b9d3b9f05dd6162106949d3793d9ea40ee50d5253b9d3c252ed137bfe0859cf7bc38d00a3a2686e64d4be82e
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .*.swp
4
+ .*.swo
5
+ junk.*
6
+ .idea/*
7
+ .bundle
8
+ .config
9
+ .yardoc
10
+ Gemfile.lock
11
+ InstalledFiles
12
+ _yardoc
13
+ coverage
14
+ doc/
15
+ lib/bundler/man
16
+ pkg
17
+ rdoc
18
+ spec/reports
19
+ test/tmp
20
+ test/version_tmp
21
+ tmp
22
+ *DS_Store*
23
+ .ruby-version
24
+ .rvmrc
25
+ .zed
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 denko-rb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # lgpio
2
+
3
+ Ruby bindings for the [lgpio (lg)](https://github.com/joan2937/lg) C library, which provides GPIO, PWM, I2C, SPI and Serial features on single-board-computers (SBCs) running Linux.
4
+
5
+ - [lg homepage](https://abyz.me.uk/lg/index.html)
6
+
7
+ ### Progress:
8
+
9
+ - [x] GPIO Read/Write
10
+ - [ ] GPIO Group Read/Write
11
+ - [ ] GPIO Callbacks
12
+ - [ ] PWM
13
+ - [ ] Wave
14
+ - [ ] I2C
15
+ - [ ] SPI
16
+ - [ ] Serial
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "rake/extensiontask"
2
+
3
+ Rake::ExtensionTask.new "lgpio" do |ext|
4
+ ext.lib_dir = "lib/lgpio"
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'lgpio'
2
+
3
+ GPIO_CHIP = 0
4
+ PIN = 258
5
+ COUNT = 1000000
6
+
7
+ chip_handle = LGPIO.chip_open(GPIO_CHIP)
8
+ LGPIO.gpio_claim_input(chip_handle, LGPIO::SET_PULL_UP, PIN)
9
+
10
+ t1 = Time.now
11
+ COUNT.times do
12
+ value = LGPIO.gpio_read(chip_handle, PIN)
13
+ end
14
+ t2 = Time.now
15
+
16
+ puts "Reads per second: #{COUNT.to_f / (t2 - t1).to_f}"
@@ -0,0 +1,17 @@
1
+ require 'lgpio'
2
+
3
+ GPIO_CHIP = 0
4
+ PIN = 260
5
+ COUNT = 1000000
6
+
7
+ chip_handle = LGPIO.chip_open(GPIO_CHIP)
8
+ LGPIO.gpio_claim_output(chip_handle, LGPIO::SET_PULL_NONE, PIN, LGPIO::LOW)
9
+
10
+ t1 = Time.now
11
+ COUNT.times do
12
+ LGPIO.gpio_write(chip_handle, PIN, 1)
13
+ LGPIO.gpio_write(chip_handle, PIN, 0)
14
+ end
15
+ t2 = Time.now
16
+
17
+ puts "Toggles per second: #{COUNT.to_f / (t2 - t1).to_f}"
data/examples/blink.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'lgpio'
2
+
3
+ GPIO_CHIP = 0
4
+ LED = 260
5
+ INTERVAL = 0.25
6
+
7
+ chip_handle = LGPIO.chip_open(GPIO_CHIP)
8
+ LGPIO.gpio_claim_output(chip_handle, LGPIO::SET_PULL_NONE, LED, LGPIO::LOW)
9
+
10
+ loop do
11
+ LGPIO.gpio_write(chip_handle, LED, 1)
12
+ sleep INTERVAL
13
+ LGPIO.gpio_write(chip_handle, LED, 0)
14
+ sleep INTERVAL
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'lgpio'
2
+
3
+ GPIO_CHIP = 0
4
+ BUTTON = 258
5
+ LED = 260
6
+
7
+ chip_handle = LGPIO.chip_open(GPIO_CHIP)
8
+ LGPIO.gpio_claim_input(chip_handle, LGPIO::SET_PULL_UP, BUTTON)
9
+ LGPIO.gpio_claim_output(chip_handle, LGPIO::SET_PULL_NONE, LED, LGPIO::LOW)
10
+
11
+ loop do
12
+ if LGPIO.gpio_read(chip_handle, BUTTON) == 0
13
+ LGPIO.gpio_write(chip_handle, LED, 1)
14
+ else
15
+ LGPIO.gpio_write(chip_handle, LED, 0)
16
+ end
17
+ sleep 0.001
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+
3
+ #
4
+ # Need lgpio installed.
5
+ # See: https://github.com/joan2937/lg for instructions.
6
+ #
7
+ $libs += " -llgpio"
8
+
9
+ create_makefile('lgpio/lgpio')
data/ext/lgpio/lgpio.c ADDED
@@ -0,0 +1,53 @@
1
+ #include <lgpio.h>
2
+ #include <ruby.h>
3
+
4
+ static VALUE chip_open(VALUE self, VALUE gpio_dev) {
5
+ int result = lgGpiochipOpen(NUM2INT(gpio_dev));
6
+ return INT2NUM(result);
7
+ }
8
+
9
+ static VALUE chip_close(VALUE self, VALUE gpio_dev) {
10
+ int result = lgGpiochipClose(NUM2INT(gpio_dev));
11
+ return INT2NUM(result);
12
+ }
13
+
14
+ static VALUE gpio_claim_output(VALUE self, VALUE handle, VALUE flags, VALUE gpio, VALUE level) {
15
+ int result = lgGpioClaimOutput(NUM2INT(handle), NUM2INT(flags), NUM2INT(gpio), NUM2INT(level));
16
+ return INT2NUM(result);
17
+ }
18
+
19
+ static VALUE gpio_claim_input(VALUE self, VALUE handle, VALUE flags, VALUE gpio) {
20
+ int result = lgGpioClaimInput(NUM2INT(handle), NUM2INT(flags), NUM2INT(gpio));
21
+ return INT2NUM(result);
22
+ }
23
+
24
+ static VALUE gpio_read(VALUE self, VALUE handle, VALUE gpio) {
25
+ int result = lgGpioRead(NUM2INT(handle), NUM2INT(gpio));
26
+ return INT2NUM(result);
27
+ }
28
+
29
+ static VALUE gpio_write(VALUE self, VALUE handle, VALUE gpio, VALUE level) {
30
+ int result = lgGpioWrite(NUM2INT(handle), NUM2INT(gpio), NUM2INT(level));
31
+ return INT2NUM(result);
32
+ }
33
+
34
+ void Init_lgpio(void) {
35
+ // Modules
36
+ VALUE mLGPIO = rb_define_module("LGPIO");
37
+
38
+ // Constants
39
+ rb_define_const(mLGPIO, "SET_ACTIVE_LOW", INT2NUM(LG_SET_ACTIVE_LOW));
40
+ rb_define_const(mLGPIO, "SET_OPEN_DRAIN", INT2NUM(LG_SET_OPEN_DRAIN));
41
+ rb_define_const(mLGPIO, "SET_OPEN_SOURCE", INT2NUM(LG_SET_OPEN_SOURCE));
42
+ rb_define_const(mLGPIO, "SET_PULL_UP", INT2NUM(LG_SET_PULL_UP));
43
+ rb_define_const(mLGPIO, "SET_PULL_DOWN", INT2NUM(LG_SET_PULL_DOWN));
44
+ rb_define_const(mLGPIO, "SET_PULL_NONE", INT2NUM(LG_SET_PULL_NONE));
45
+
46
+ // Methods
47
+ rb_define_singleton_method(mLGPIO, "chip_open", chip_open, 1);
48
+ rb_define_singleton_method(mLGPIO, "chip_close", chip_close, 1);
49
+ rb_define_singleton_method(mLGPIO, "gpio_claim_input", gpio_claim_input, 3);
50
+ rb_define_singleton_method(mLGPIO, "gpio_claim_output", gpio_claim_output, 4);
51
+ rb_define_singleton_method(mLGPIO, "gpio_read", gpio_read, 2);
52
+ rb_define_singleton_method(mLGPIO, "gpio_write", gpio_write, 3);
53
+ }
data/lgpio.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ require_relative 'lib/lgpio/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'lgpio'
5
+ s.version = LGPIO::VERSION
6
+ s.licenses = ['MIT']
7
+ s.summary = "lgpio (lg) bindings for Ruby"
8
+ s.description = "Use GPIO / PWM / I2C / SPI / UART on Linux SBCs in Ruby"
9
+
10
+ s.authors = ["vickash"]
11
+ s.email = 'mail@vickash.com'
12
+ s.files = `git ls-files`.split($\)
13
+ s.homepage = 'https://github.com/denko-rb/lgpio'
14
+ s.metadata = { "source_code_uri" => "https://github.com/denko-rb/lgpio" }
15
+
16
+ s.extensions = %w[ext/lgpio/extconf.rb]
17
+ end
@@ -0,0 +1,3 @@
1
+ module LGPIO
2
+ VERSION = "0.1.0"
3
+ end
data/lib/lgpio.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_relative 'lgpio/lgpio'
2
+
3
+ module LGPIO
4
+ LOW = 0
5
+ HIGH = 1
6
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lgpio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - vickash
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use GPIO / PWM / I2C / SPI / UART on Linux SBCs in Ruby
14
+ email: mail@vickash.com
15
+ executables: []
16
+ extensions:
17
+ - ext/lgpio/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - examples/bench_in.rb
25
+ - examples/bench_out.rb
26
+ - examples/blink.rb
27
+ - examples/momentary.rb
28
+ - ext/lgpio/extconf.rb
29
+ - ext/lgpio/lgpio.c
30
+ - lgpio.gemspec
31
+ - lib/lgpio.rb
32
+ - lib/lgpio/version.rb
33
+ homepage: https://github.com/denko-rb/lgpio
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ source_code_uri: https://github.com/denko-rb/lgpio
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.5.9
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: lgpio (lg) bindings for Ruby
57
+ test_files: []