rbone 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +31 -0
- data/Rakefile +7 -0
- data/lib/rbone.rb +68 -0
- data/lib/rbone/app.rb +129 -0
- data/lib/rbone/version.rb +3 -0
- data/rbone.gemspec +21 -0
- data/spec/rbone/app_spec.rb +59 -0
- data/spec/rbone_spec.rb +4 -0
- data/spec/spec_helper.rb +14 -0
- metadata +102 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.2
|
5
|
+
- 1.9.3
|
6
|
+
# - jruby-18mode # JRuby in 1.8 mode
|
7
|
+
# - jruby-19mode # JRuby in 1.9 mode
|
8
|
+
# - rbx-18mode
|
9
|
+
# - rbx-19mode
|
10
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
11
|
+
# script: bundle exec rspec spec
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Matt Mayers
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Rbone
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/majormajors/rbone.png)](http://travis-ci.org/majormajors/rbone)
|
4
|
+
|
5
|
+
Rbone makes it easy to write Arduino-style applications for your BeagleBone in Ruby.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rbone'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rbone
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rbone.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require "rbone/version"
|
2
|
+
|
3
|
+
module Rbone; end
|
4
|
+
|
5
|
+
LOW = 0x00
|
6
|
+
HIGH = 0x01
|
7
|
+
|
8
|
+
INPUT = 0x00
|
9
|
+
OUTPUT = 0x01
|
10
|
+
|
11
|
+
GPIO0 = 0x00
|
12
|
+
GPIO1 = GPIO0 + 0x20
|
13
|
+
GPIO2 = GPIO1 + 0x20
|
14
|
+
GPIO3 = GPIO2 + 0x20
|
15
|
+
|
16
|
+
# Digital IO pins
|
17
|
+
P8_3 = GPIO1 + 0x06
|
18
|
+
P8_4 = GPIO1 + 0x07
|
19
|
+
P8_5 = GPIO1 + 0x02
|
20
|
+
P8_6 = GPIO1 + 0x03
|
21
|
+
P8_11 = GPIO1 + 0x0D
|
22
|
+
P8_12 = GPIO1 + 0x0C
|
23
|
+
P8_14 = GPIO0 + 0x1A
|
24
|
+
P8_15 = GPIO1 + 0x0F
|
25
|
+
P8_16 = GPIO1 + 0x0E
|
26
|
+
P8_17 = GPIO0 + 0x1B
|
27
|
+
P8_18 = GPIO2 + 0x01
|
28
|
+
P8_20 = GPIO1 + 0x1F
|
29
|
+
P8_21 = GPIO1 + 0x1E
|
30
|
+
P8_22 = GPIO1 + 0x05
|
31
|
+
P8_23 = GPIO1 + 0x04
|
32
|
+
P8_24 = GPIO1 + 0x01
|
33
|
+
P8_25 = GPIO1 + 0x00
|
34
|
+
P8_26 = GPIO1 + 0x1D
|
35
|
+
P8_27 = GPIO2 + 0x16
|
36
|
+
P8_28 = GPIO2 + 0x18
|
37
|
+
P8_29 = GPIO2 + 0x17
|
38
|
+
P8_30 = GPIO2 + 0x19
|
39
|
+
P8_39 = GPIO2 + 0x0C
|
40
|
+
P8_40 = GPIO2 + 0x0D
|
41
|
+
P8_41 = GPIO2 + 0x0A
|
42
|
+
P8_42 = GPIO2 + 0x0B
|
43
|
+
P8_43 = GPIO2 + 0x08
|
44
|
+
P8_44 = GPIO2 + 0x09
|
45
|
+
P8_45 = GPIO2 + 0x06
|
46
|
+
P8_46 = GPIO2 + 0x07
|
47
|
+
P9_12 = GPIO1 + 0x1C
|
48
|
+
P9_15 = GPIO1 + 0x10
|
49
|
+
P9_23 = GPIO1 + 0x11
|
50
|
+
P9_25 = GPIO3 + 0x15
|
51
|
+
P9_27 = GPIO3 + 0x13
|
52
|
+
P9_42 = GPIO0 + 0x07
|
53
|
+
|
54
|
+
USR0 = GPIO1 + 0x15
|
55
|
+
USR1 = GPIO1 + 0x16
|
56
|
+
USR2 = GPIO1 + 0x17
|
57
|
+
USR3 = GPIO1 + 0x18
|
58
|
+
|
59
|
+
# Analog input pins
|
60
|
+
P9_33 = 'ain4'
|
61
|
+
P9_35 = 'ain6'
|
62
|
+
P9_36 = 'ain5'
|
63
|
+
P9_37 = 'ain2'
|
64
|
+
P9_38 = 'ain3'
|
65
|
+
P9_39 = 'ain0'
|
66
|
+
P9_40 = 'ain1'
|
67
|
+
|
68
|
+
require "rbone/app"
|
data/lib/rbone/app.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'rbone'
|
2
|
+
|
3
|
+
module Rbone
|
4
|
+
class PinRefError < StandardError; end
|
5
|
+
|
6
|
+
class App
|
7
|
+
|
8
|
+
DIGITAL_PINS = [
|
9
|
+
P8_3, P8_4, P8_5, P8_6, P8_11, P8_12,
|
10
|
+
P8_14, P8_15, P8_16, P8_17, P8_18, P8_20,
|
11
|
+
P8_21, P8_22, P8_23, P8_24, P8_25, P8_26,
|
12
|
+
P8_27, P8_28, P8_29, P8_30, P8_39, P8_40,
|
13
|
+
P8_41, P8_42, P8_43, P8_44, P8_45, P8_46,
|
14
|
+
P9_12, P9_15, P9_23, P9_25, P9_27, P9_42,
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
USR_LEDS = [
|
18
|
+
USR0, USR1, USR2, USR3
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
ANALOG_PINS = [
|
22
|
+
P9_33, P9_35, P9_36, P9_37, P9_38, P9_39, P9_40
|
23
|
+
].freeze
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@exported_pins = []
|
27
|
+
if block_given?
|
28
|
+
yield(self)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def setup(&block)
|
33
|
+
@setup = block
|
34
|
+
end
|
35
|
+
|
36
|
+
def loop(delay=nil, &block)
|
37
|
+
@loop = block
|
38
|
+
self.run unless delay == :delay
|
39
|
+
end
|
40
|
+
|
41
|
+
def run
|
42
|
+
self.instance_eval(&@setup)
|
43
|
+
self.instance_eval(&@loop) while true
|
44
|
+
rescue Interrupt => e
|
45
|
+
cleanup!
|
46
|
+
rescue Exception => e
|
47
|
+
cleanup!
|
48
|
+
$stderr.puts e
|
49
|
+
end
|
50
|
+
|
51
|
+
def pinMode(pin, direction)
|
52
|
+
if DIGITAL_PINS.include?(pin)
|
53
|
+
File.open('/sys/class/gpio/export', 'w') do |f|
|
54
|
+
f.write("%s\n" % [pin])
|
55
|
+
end
|
56
|
+
|
57
|
+
filename = '/sys/class/gpio/gpio%d/direction' % [pin]
|
58
|
+
File.open(filename, 'w') do |f|
|
59
|
+
if direction == INPUT
|
60
|
+
f.write("in\n")
|
61
|
+
else
|
62
|
+
f.write("out\n")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
@exported_pins.push(pin)
|
67
|
+
else
|
68
|
+
raise PinRefError, "#{pin} is not a valid digital pin reference"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def digitalWrite(pin, value)
|
73
|
+
if DIGITAL_PINS.include?(pin)
|
74
|
+
filename = '/sys/class/gpio/gpio%d/value' % [pin]
|
75
|
+
elsif USR_LEDS.include?(pin)
|
76
|
+
led_id = pin - USR0
|
77
|
+
filename = '/sys/devices/platform/leds-gpio/leds/beaglebone::usr%s/brightness' % [led_id]
|
78
|
+
else
|
79
|
+
raise PinRefError, "#{pin} is not a valid digital pin reference"
|
80
|
+
end
|
81
|
+
|
82
|
+
if value == HIGH || value == LOW
|
83
|
+
File.open(filename, 'w') do |f|
|
84
|
+
f.write("%s" % [value])
|
85
|
+
end
|
86
|
+
return true
|
87
|
+
else
|
88
|
+
return false
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def digitalRead(pin)
|
93
|
+
if DIGITAL_PINS.include?(pin)
|
94
|
+
filename = '/sys/class/gpio/gpio%d/value' % [pin]
|
95
|
+
value = open(filename).read
|
96
|
+
case value.chomp
|
97
|
+
when '0' then LOW
|
98
|
+
when '1' then HIGH
|
99
|
+
else nil
|
100
|
+
end
|
101
|
+
else
|
102
|
+
raise PinRefError, "#{pin} is not a valid digital pin reference"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def analogRead(pin)
|
107
|
+
if ANALOG_PINS.include?(pin)
|
108
|
+
filename = "/sys/devices/platform/tsc/#{pin}"
|
109
|
+
File.open(filename, 'r') do |f|
|
110
|
+
return
|
111
|
+
end
|
112
|
+
else
|
113
|
+
raise PinRefError, "#{pin} is not a valid analog pin reference"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def delay(ms)
|
118
|
+
sleep(ms.to_f / 1000.0)
|
119
|
+
end
|
120
|
+
|
121
|
+
def cleanup!
|
122
|
+
puts "cleanup!"
|
123
|
+
end
|
124
|
+
|
125
|
+
def pinUnexport(pin)
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/rbone.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rbone/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matt Mayers"]
|
6
|
+
gem.email = ["matt@mattmayers.com"]
|
7
|
+
gem.description = %q{This gem provides a simple interface for writing Arduino-style programs for the BeagleBone.}
|
8
|
+
gem.summary = %q{Write simple Arduino-style programs for your BeagleBone}
|
9
|
+
gem.homepage = "http://github.com/majormajors/rbone"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rbone"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rbone::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_development_dependency "fakefs"
|
21
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rbone/app'
|
3
|
+
|
4
|
+
describe Rbone::App do
|
5
|
+
before :all do
|
6
|
+
FileUtils.mkdir_p('/sys/class/gpio')
|
7
|
+
|
8
|
+
# mock USR leds
|
9
|
+
%w(0 1 2 3).map{ |n| "/sys/devices/platform/leds-gpio/leds/beaglebone::usr#{n}" }.each do |base_dir|
|
10
|
+
FileUtils.mkdir_p(base_dir)
|
11
|
+
FileUtils.touch(%w(brightness max_brightness trigger uevent).map{ |f| "#{base_dir}/#{f}" })
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
@app = Rbone::App.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def mock_export(pin)
|
20
|
+
base_dir = "/sys/class/gpio/gpio%s" % [pin]
|
21
|
+
FileUtils.mkdir_p(base_dir)
|
22
|
+
FileUtils.touch(%w(active_low direction edge uevent value).map{ |f| "#{base_dir}/#{f}" })
|
23
|
+
end
|
24
|
+
|
25
|
+
def read_value(file)
|
26
|
+
f = File.open(file, 'r')
|
27
|
+
value = f.read
|
28
|
+
f.close
|
29
|
+
value
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#digitalWrite' do
|
33
|
+
it "sets the GPIO pin to HIGH" do
|
34
|
+
mock_export(P8_3)
|
35
|
+
@app.digitalWrite(P8_3, HIGH).should be_true
|
36
|
+
read_value("/sys/class/gpio/gpio38/value").should == '1'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sets the GPIO pin to LOW" do
|
40
|
+
mock_export(P8_3)
|
41
|
+
@app.digitalWrite(P8_3, LOW).should be_true
|
42
|
+
read_value("/sys/class/gpio/gpio38/value").should == '0'
|
43
|
+
end
|
44
|
+
|
45
|
+
it "sets the USR LED to HIGH" do
|
46
|
+
@app.digitalWrite(USR1, HIGH).should be_true
|
47
|
+
read_value("/sys/devices/platform/leds-gpio/leds/beaglebone::usr1/brightness").should == '1'
|
48
|
+
end
|
49
|
+
|
50
|
+
it "sets the USR LED to LOW" do
|
51
|
+
@app.digitalWrite(USR1, LOW).should be_true
|
52
|
+
read_value("/sys/devices/platform/leds-gpio/leds/beaglebone::usr1/brightness").should == '0'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns false if an invalid value is supplied" do
|
56
|
+
@app.digitalWrite(USR0, 3).should be_false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/rbone_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'pp'
|
14
|
+
require 'fakefs'
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbone
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Mayers
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70355878496380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70355878496380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70355878495480 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70355878495480
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: fakefs
|
38
|
+
requirement: &70355888796100 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70355888796100
|
47
|
+
description: This gem provides a simple interface for writing Arduino-style programs
|
48
|
+
for the BeagleBone.
|
49
|
+
email:
|
50
|
+
- matt@mattmayers.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- .travis.yml
|
58
|
+
- Gemfile
|
59
|
+
- LICENSE
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- lib/rbone.rb
|
63
|
+
- lib/rbone/app.rb
|
64
|
+
- lib/rbone/version.rb
|
65
|
+
- rbone.gemspec
|
66
|
+
- spec/rbone/app_spec.rb
|
67
|
+
- spec/rbone_spec.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
homepage: http://github.com/majormajors/rbone
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
hash: -1113520713437468281
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: -1113520713437468281
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.11
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Write simple Arduino-style programs for your BeagleBone
|
99
|
+
test_files:
|
100
|
+
- spec/rbone/app_spec.rb
|
101
|
+
- spec/rbone_spec.rb
|
102
|
+
- spec/spec_helper.rb
|