ev3dev_ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +129 -0
- data/Rakefile +2 -0
- data/ev3dev.gemspec +30 -0
- data/lib/ev3dev.rb +12 -0
- data/lib/ev3dev/battery.rb +15 -0
- data/lib/ev3dev/button.rb +92 -0
- data/lib/ev3dev/device.rb +21 -0
- data/lib/ev3dev/led.rb +182 -0
- data/lib/ev3dev/motor.rb +14 -0
- data/lib/ev3dev/sensor.rb +14 -0
- data/lib/ev3dev/sound.rb +43 -0
- data/lib/ev3dev/version.rb +3 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08e8e16ff768c82946d2945cf2dafd688ea4b600
|
4
|
+
data.tar.gz: e4e8e70bcead95f67037543e7eee65594e4faef7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9dc9721d426220c757d6124f06314c1a90a094ed6d39217ade817cd33833b73593ba206a68de7b1062ccf8d8da861b0566b33ef1bd36913f1debc1bc154236f5
|
7
|
+
data.tar.gz: 2b6cd0b9fd22073661775baafb66016833c14269b7078df466c011677d11f2f79c2d4475e2e6bc4df54a2fc8c59b0608ea619f9aea57883fde98e1ff61e86555
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2015 quake wang, Yoshiyuki Shibata
|
2
|
+
|
3
|
+
The MIT License (MIT)
|
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,129 @@
|
|
1
|
+
# ev3dev ruby binding for LEGO Mindstorms EV3
|
2
|
+
|
3
|
+
|
4
|
+
ev3dev_ruby is a gem to controll sensors and motors on EV3 using Ruby.
|
5
|
+
|
6
|
+
- [ev3dev](http://www.ev3dev.org) version; ev3dev-jessie-2015-12-30 or later
|
7
|
+
|
8
|
+
|
9
|
+
## Install ev3dev_ruby on EV3
|
10
|
+
SSH remote access to the EV3 from PC
|
11
|
+
|
12
|
+
(default ev3dev user; robot, password; maker)
|
13
|
+
|
14
|
+
```
|
15
|
+
$ ssh robot@ev3dev.local
|
16
|
+
```
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
then
|
21
|
+
|
22
|
+
```
|
23
|
+
robot@ev3dev:~$ sudo gem install ev3dev
|
24
|
+
```
|
25
|
+
|
26
|
+
## Run examples
|
27
|
+
### 1\. Copy the examples to the **/home/robot** directory in ev3dev from PC.
|
28
|
+
|
29
|
+
(for OS X user; )
|
30
|
+
```
|
31
|
+
$ scp -r /Users/xxx/Downloads/ev3dev_ruby-master/examples robot@ev3dev.local:/home/robot
|
32
|
+
```
|
33
|
+
|
34
|
+
### 2\. Run the program
|
35
|
+
There are two different ways to run the program.
|
36
|
+
#### A. Remote access from PC
|
37
|
+
|
38
|
+
```
|
39
|
+
robot@ev3dev:~$ cd /home/robot/examples
|
40
|
+
robot@ev3dev:~$ cd motor
|
41
|
+
robot@ev3dev:~$ ruby midiummotor.rb
|
42
|
+
```
|
43
|
+
|
44
|
+
#### B. File Browser on EV3
|
45
|
+
##### 1. Add execute permission
|
46
|
+
|
47
|
+
```
|
48
|
+
robot@ev3dev:~$ cd /home/robot/examples
|
49
|
+
robot@ev3dev:~$ cd motor
|
50
|
+
robot@ev3dev:~$ ls -l
|
51
|
+
...
|
52
|
+
-rw-r--r-- 1 robot robot 474 Jan 18 16:18 midiummotor.rb
|
53
|
+
...
|
54
|
+
robot@ev3dev:~$ chmod +x midiummotor.rb
|
55
|
+
robot@ev3dev:~$ ls -l
|
56
|
+
...
|
57
|
+
-rwxr-xr-x 1 robot robot 474 Jan 18 16:18 midiummotor.rb
|
58
|
+
...
|
59
|
+
```
|
60
|
+
|
61
|
+
##### 2. File Browser
|
62
|
+
Select the program and push the center button on EV3.
|
63
|
+
|
64
|
+
|
65
|
+
## irb
|
66
|
+
You can also run programs using irb.
|
67
|
+
```
|
68
|
+
robot@ev3dev:~$ irb
|
69
|
+
irb(main):001:0> require 'ev3dev'
|
70
|
+
=> true
|
71
|
+
irb(main):002:0> mm = Ev3dev::Motor.new 'B'
|
72
|
+
=> #<Ev3dev::Motor:0x193890 @device_path="/sys/class/tacho-motor/motor0">
|
73
|
+
irb(main):003:0> mm.device_path
|
74
|
+
=> "/sys/class/tacho-motor/motor0"
|
75
|
+
irb(main):004:0> mm.driver_name
|
76
|
+
=> "lego-ev3-l-motor"
|
77
|
+
irb(main):005:0> mm.address
|
78
|
+
=> "outB"
|
79
|
+
irb(main):006:0> mm.commands
|
80
|
+
=> "run-forever run-to-abs-pos run-to-rel-pos run-timed run-direct stop reset"
|
81
|
+
irb(main):007:0> mm.stop_commands
|
82
|
+
=> "coast brake hold"
|
83
|
+
irb(main):008:0> mm.duty_cycle_sp 100
|
84
|
+
=> #<Ev3dev::Motor:0x193890 @device_path="/sys/class/tacho-motor/motor0">
|
85
|
+
irb(main):009:0> mm.command 'run-forever'
|
86
|
+
=> #<Ev3dev::Motor:0x193890 @device_path="/sys/class/tacho-motor/motor0">
|
87
|
+
irb(main):010:0> sleep 3
|
88
|
+
|
89
|
+
=> 3
|
90
|
+
irb(main):011:0> mm.command 'stop'
|
91
|
+
=> #<Ev3dev::Motor:0x193890 @device_path="/sys/class/tacho-motor/motor0">
|
92
|
+
irb(main):012:0>
|
93
|
+
```
|
94
|
+
|
95
|
+
## ev3dev API
|
96
|
+
(Also checkout examples)
|
97
|
+
|
98
|
+
### [Motors](http://www.ev3dev.org/docs/motors/)
|
99
|
+
- [EV3 Medium Servo Motor](http://www.ev3dev.org/docs/motors/lego-ev3-medium-servo-motor/)
|
100
|
+
- [EV3 Large Servo Motor](http://www.ev3dev.org/docs/motors/lego-ev3-large-servo-motor/)
|
101
|
+
- [Tacho Motor Class API](http://www.ev3dev.org/docs/drivers/tacho-motor-class/)
|
102
|
+
- [Tacho-Motor Class Tutorial](http://www.ev3dev.org/docs/tutorials/tacho-motors/)
|
103
|
+
|
104
|
+
### [Sensors](http://www.ev3dev.org/docs/sensors/)
|
105
|
+
- [EV3 Touch Sensor](http://www.ev3dev.org/docs/sensors/lego-ev3-touch-sensor/)
|
106
|
+
- [EV3 Color Sensor](http://www.ev3dev.org/docs/sensors/lego-ev3-color-sensor/)
|
107
|
+
- [EV3 Gyro Sensor](http://www.ev3dev.org/docs/sensors/lego-ev3-gyro-sensor/)
|
108
|
+
- [EV3 Ultrasonic Sensor](http://www.ev3dev.org/docs/sensors/lego-ev3-ultrasonic-sensor/)
|
109
|
+
- [LEGO Sensor Class API](http://www.ev3dev.org/docs/drivers/lego-sensor-class/)
|
110
|
+
|
111
|
+
### EV3 Devices
|
112
|
+
|
113
|
+
- [Battery](https://github.com/ev3dev/ev3dev/issues/68)
|
114
|
+
- [Sound](https://github.com/ev3dev/ev3dev/wiki/Using-Sound)
|
115
|
+
- [aplay (ALSA sound driver)](http://linux.die.net/man/1/aplay)
|
116
|
+
- [espeak (speech engine)](http://espeak.sourceforge.net/commands.html)
|
117
|
+
- [LEDs](https://github.com/ev3dev/ev3dev/wiki/Using-the-LEDs)
|
118
|
+
- [LED Transient Trigger](https://www.kernel.org/doc/Documentation/leds/ledtrig-transient.txt)
|
119
|
+
- Original Led Class APIs are added. Checkout examples.
|
120
|
+
- [Buttons](http://www.ev3dev.org/docs/tutorials/using-ev3-buttons/)
|
121
|
+
|
122
|
+
#### These EV3 devices below are not supported yet.
|
123
|
+
|
124
|
+
- [LCD](http://www.ev3dev.org/docs/tutorials/using-ev3-lcd/)
|
125
|
+
- [Bluetooth](https://github.com/ev3dev/ev3dev/wiki/Using-Bluetooth)
|
126
|
+
|
127
|
+
|
128
|
+
## More Info
|
129
|
+
- [Getting Started with ev3dev - ev3dev.org](http://www.ev3dev.org/docs/getting-started/)
|
data/Rakefile
ADDED
data/ev3dev.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ev3dev/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ev3dev_ruby"
|
8
|
+
spec.version = Ev3dev::VERSION
|
9
|
+
spec.authors = ["quake wang", "Yoshiyuki Shibata"]
|
10
|
+
spec.email = ["aqua055@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ev3dev_ruby}
|
13
|
+
spec.description = %q{ev3dev ruby binding}
|
14
|
+
spec.homepage = "https://github.com/noanoa07/ev3dev_ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
#if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
#end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples)/}) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
end
|
data/lib/ev3dev.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Ev3dev
|
2
|
+
class Battery < Device
|
3
|
+
PATH = "/sys/class/power_supply"
|
4
|
+
|
5
|
+
def initialize()
|
6
|
+
Dir.glob("#{PATH}/*").each do |path|
|
7
|
+
if File.exist?("#{path}/voltage_now")
|
8
|
+
super path
|
9
|
+
return
|
10
|
+
end
|
11
|
+
end
|
12
|
+
raise "couldn't find battery attributes"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# http://www.ev3dev.org/docs/tutorials/using-ev3-buttons/
|
2
|
+
|
3
|
+
module Ev3dev
|
4
|
+
class Button
|
5
|
+
# from linux/input.h
|
6
|
+
KEY_UP = 103
|
7
|
+
KEY_DOWN = 108
|
8
|
+
KEY_LEFT = 105
|
9
|
+
KEY_RIGHT = 106
|
10
|
+
KEY_ENTER = 28
|
11
|
+
KEY_BACK = 14
|
12
|
+
|
13
|
+
KEY_MAX = 0x2ff
|
14
|
+
|
15
|
+
def EVIOCGKEY(length)
|
16
|
+
2 << (14+8+8) | length << (8+8) | 'E'.ord << 8 | 0x18
|
17
|
+
end
|
18
|
+
# end of stuff from linux/input.h
|
19
|
+
|
20
|
+
BUF_LEN = (KEY_MAX + 7) / 8
|
21
|
+
PATH = "/dev/input/by-path/platform-gpio-keys.0-event"
|
22
|
+
|
23
|
+
def initialize()
|
24
|
+
raise "couldn't find LED attributes" unless File.exist?(PATH)
|
25
|
+
@buttons = {up: KEY_UP, down: KEY_DOWN, left: KEY_LEFT, right: KEY_RIGHT, enter: KEY_ENTER, back: KEY_BACK}
|
26
|
+
end
|
27
|
+
|
28
|
+
def up?
|
29
|
+
buf = get_key_buf
|
30
|
+
key_pressed?(KEY_UP, buf)
|
31
|
+
end
|
32
|
+
|
33
|
+
def down?
|
34
|
+
buf = get_key_buf
|
35
|
+
key_pressed?(KEY_DOWN, buf)
|
36
|
+
end
|
37
|
+
|
38
|
+
def left?
|
39
|
+
buf = get_key_buf
|
40
|
+
key_pressed?(KEY_LEFT, buf)
|
41
|
+
end
|
42
|
+
|
43
|
+
def right?
|
44
|
+
buf = get_key_buf
|
45
|
+
key_pressed?(KEY_RIGHT, buf)
|
46
|
+
end
|
47
|
+
|
48
|
+
def enter?
|
49
|
+
buf = get_key_buf
|
50
|
+
key_pressed?(KEY_ENTER, buf)
|
51
|
+
end
|
52
|
+
|
53
|
+
def back?
|
54
|
+
buf = get_key_buf
|
55
|
+
key_pressed?(KEY_BACK, buf)
|
56
|
+
end
|
57
|
+
|
58
|
+
def pressed
|
59
|
+
pressed_buttons =[]
|
60
|
+
buf = get_key_buf
|
61
|
+
@buttons.each do |button, key_code|
|
62
|
+
pressed_buttons << button.to_s if key_pressed?(key_code, buf)
|
63
|
+
end
|
64
|
+
pressed_buttons
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def get_key_buf
|
69
|
+
buf = Array.new(BUF_LEN){ 0 }.pack("C*")
|
70
|
+
|
71
|
+
File.open(PATH, 'r') do |fd|
|
72
|
+
ret = fd.ioctl(EVIOCGKEY(buf.length), buf)
|
73
|
+
raise "couldn't find Button attributes" if ret < 0
|
74
|
+
|
75
|
+
buf = buf.unpack("C*")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def key_pressed?(key_code, buf)
|
80
|
+
test_bit(key_code, buf)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_bit(bit, bytes)
|
84
|
+
# bit in bytes is 1 when released and 0 when pressed
|
85
|
+
if (bytes[bit / 8] & (1 << (bit % 8))) == 0
|
86
|
+
true # pressed
|
87
|
+
else
|
88
|
+
false # released
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Ev3dev
|
2
|
+
class Device
|
3
|
+
attr :device_path
|
4
|
+
|
5
|
+
def initialize(device_path)
|
6
|
+
@device_path = device_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(name, *args, &block)
|
10
|
+
raise "no device connected" if @device_path.nil?
|
11
|
+
param = File.join @device_path, name.to_s
|
12
|
+
raise "no such attribute: #{param}" unless File.exist? param
|
13
|
+
if args.first.nil?
|
14
|
+
IO.read(param).strip
|
15
|
+
else
|
16
|
+
IO.write param, args.first.to_s
|
17
|
+
self
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/ev3dev/led.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
module Ev3dev
|
2
|
+
class Led < Device
|
3
|
+
attr :paths
|
4
|
+
|
5
|
+
PATH = "/sys/class/leds"
|
6
|
+
|
7
|
+
GREEN = [255, 0]
|
8
|
+
RED = [ 0, 255]
|
9
|
+
AMBER = [255, 255]
|
10
|
+
|
11
|
+
MAX_BRIGHTNESS = 255
|
12
|
+
|
13
|
+
def initialize()
|
14
|
+
raise "couldn't find LED attributes" unless File.exist?(PATH)
|
15
|
+
|
16
|
+
@left_green = set_led_path("left" , "green")
|
17
|
+
@left_red = set_led_path("left" , "red" )
|
18
|
+
@right_green = set_led_path("right", "green")
|
19
|
+
@right_red = set_led_path("right", "red" )
|
20
|
+
|
21
|
+
@default_paths = [@left_green, @left_red, @right_green, @right_red]
|
22
|
+
@left_paths = [@left_green, @left_red]
|
23
|
+
@right_paths = [@right_green, @right_red]
|
24
|
+
|
25
|
+
@paths = @default_paths
|
26
|
+
end
|
27
|
+
|
28
|
+
def left()
|
29
|
+
@paths = @left_paths
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def right()
|
34
|
+
@paths = @right_paths
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def left_green()
|
39
|
+
@paths = [@left_green]
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def left_red()
|
44
|
+
@paths = [@left_red]
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def right_green()
|
49
|
+
@paths = [@right_green]
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def right_red()
|
54
|
+
@paths = [@right_red]
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def on(*args)
|
59
|
+
args = args.flatten
|
60
|
+
args = [MAX_BRIGHTNESS, MAX_BRIGHTNESS] if args.empty?
|
61
|
+
set_each_trigger_brightness(@paths, "none", args)
|
62
|
+
|
63
|
+
@paths = @default_paths
|
64
|
+
end
|
65
|
+
|
66
|
+
def off()
|
67
|
+
self.on(0, 0)
|
68
|
+
end
|
69
|
+
|
70
|
+
def blink(*args)
|
71
|
+
args = args.flatten
|
72
|
+
args = [MAX_BRIGHTNESS, MAX_BRIGHTNESS] if args.empty?
|
73
|
+
set_each_trigger_brightness(@paths, "timer", args)
|
74
|
+
|
75
|
+
if args.size == 3
|
76
|
+
if @paths.size == 1
|
77
|
+
@paths.each do |path|
|
78
|
+
brightness = args[0]
|
79
|
+
next if brightness == 0
|
80
|
+
|
81
|
+
delay_on_time = args[1]
|
82
|
+
delay_off_time = args[2]
|
83
|
+
|
84
|
+
write_value_with_check(path, "delay_on" , delay_on_time)
|
85
|
+
write_value_with_check(path, "delay_off", delay_off_time)
|
86
|
+
end
|
87
|
+
else
|
88
|
+
raise "ArgumentError: wrong number of arguments (3 for 4)"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
if args.size == 4
|
93
|
+
@paths.each_with_index do |path, index|
|
94
|
+
brightness = args[index % 2]
|
95
|
+
next if brightness == 0
|
96
|
+
|
97
|
+
delay_on_time = args[2]
|
98
|
+
delay_off_time = args[3]
|
99
|
+
|
100
|
+
write_value_with_check(path, "delay_on" , delay_on_time)
|
101
|
+
write_value_with_check(path, "delay_off", delay_off_time)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
@paths = @default_paths
|
106
|
+
end
|
107
|
+
|
108
|
+
def set_flash(duration_time = 10)
|
109
|
+
raise "couldn't specify the left/right green/red LED" if @paths.size != 1
|
110
|
+
path = @paths[0]
|
111
|
+
write_value_to_file(path, "brightness", 0)
|
112
|
+
write_value_to_file(path, "trigger" , "transient")
|
113
|
+
write_value_with_check(path, "duration" , duration_time)
|
114
|
+
write_value_with_check(path, "state" , 1)
|
115
|
+
|
116
|
+
@paths = @default_paths
|
117
|
+
end
|
118
|
+
|
119
|
+
def flash()
|
120
|
+
raise "couldn't specify the left/right green/red LED" if @paths.size != 1
|
121
|
+
path = @paths[0]
|
122
|
+
write_value_to_file(path, "activate", 1)
|
123
|
+
|
124
|
+
@paths = @default_paths
|
125
|
+
end
|
126
|
+
|
127
|
+
def method_missing(name, *args, &block)
|
128
|
+
raise "couldn't specify the left/right green/red LED" if @paths.size != 1
|
129
|
+
@device_path = @paths[0]
|
130
|
+
|
131
|
+
super
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
private
|
136
|
+
def set_led_path(side, color)
|
137
|
+
path_array = Dir.glob(["#{PATH}/*#{side}*#{color}*", "#{PATH}/*#{color}*#{side}*"],
|
138
|
+
File::FNM_CASEFOLD)
|
139
|
+
if path_array.size == 1
|
140
|
+
path_array.first
|
141
|
+
else
|
142
|
+
raise "no such LED attribute: #{side} #{color}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def set_each_trigger_brightness(led_paths, trigger_mode, *led_brightness)
|
147
|
+
led_brightness = led_brightness.flatten
|
148
|
+
raise "ArgumentError: no led_brightness" if led_brightness.empty?
|
149
|
+
|
150
|
+
led_paths.each_with_index do |path, index|
|
151
|
+
brightness = led_brightness[index % 2]
|
152
|
+
brightness = MAX_BRIGHTNESS if brightness > MAX_BRIGHTNESS
|
153
|
+
|
154
|
+
write_value_to_file(path, "trigger" , trigger_mode)
|
155
|
+
write_value_to_file(path, "brightness", brightness)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def write_value_to_file(path, attribute, value)
|
160
|
+
file_path = File.join path, attribute
|
161
|
+
raise "no such LED attribute: #{file_path}" unless File.exist? file_path
|
162
|
+
IO.write file_path, value
|
163
|
+
end
|
164
|
+
|
165
|
+
# sysfs group permissions created too late - Issue #225 - ev3dev/ev3dev
|
166
|
+
# https://github.com/ev3dev/ev3dev/issues/225
|
167
|
+
def write_value_with_check(path, attribute, value)
|
168
|
+
file_path = File.join path, attribute
|
169
|
+
raise "no such LED attribute: #{file_path}" unless File.exist? file_path
|
170
|
+
|
171
|
+
wait_counter = 0
|
172
|
+
until File.writable? file_path
|
173
|
+
sleep 0.05
|
174
|
+
wait_counter += 1
|
175
|
+
raise "writable timeout: #{file_path}" if wait_counter > 20
|
176
|
+
end
|
177
|
+
IO.write file_path, value
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
|
data/lib/ev3dev/motor.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Ev3dev
|
2
|
+
class Motor < Device
|
3
|
+
PATH = "/sys/class/tacho-motor"
|
4
|
+
|
5
|
+
def initialize(port)
|
6
|
+
Dir.glob("#{PATH}/motor*").each do |path|
|
7
|
+
if IO.read("#{path}/address").strip == "out#{port.to_s.upcase}"
|
8
|
+
super path
|
9
|
+
return
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Ev3dev
|
2
|
+
class Sensor < Device
|
3
|
+
PATH = "/sys/class/lego-sensor"
|
4
|
+
|
5
|
+
def initialize(port)
|
6
|
+
Dir.glob("#{PATH}/sensor*").each do |path|
|
7
|
+
if IO.read("#{path}/address").strip == "in#{port.to_s}"
|
8
|
+
super path
|
9
|
+
return
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/ev3dev/sound.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Ev3dev
|
2
|
+
class Sound < Device
|
3
|
+
PATH = "/sys/devices/platform"
|
4
|
+
|
5
|
+
def initialize()
|
6
|
+
Dir.glob("#{PATH}/*").each do |path|
|
7
|
+
if File.exist?("#{path}/tone")
|
8
|
+
super path
|
9
|
+
return
|
10
|
+
end
|
11
|
+
end
|
12
|
+
raise "couldn't find sound attributes"
|
13
|
+
end
|
14
|
+
|
15
|
+
def tone(*args)
|
16
|
+
raise "couldn't find sound attributes" if @device_path.nil?
|
17
|
+
param = File.join @device_path, "tone"
|
18
|
+
raise "no such attribute: #{param}" unless File.exist? param
|
19
|
+
if args.first.nil?
|
20
|
+
IO.read(param).strip
|
21
|
+
else
|
22
|
+
IO.write param, args.join(" ")
|
23
|
+
self
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def beep(*args)
|
28
|
+
`beep #{args.first}`
|
29
|
+
end
|
30
|
+
|
31
|
+
def aplay(*args)
|
32
|
+
`aplay #{args.first}`
|
33
|
+
end
|
34
|
+
|
35
|
+
def espeak(arg)
|
36
|
+
`espeak #{arg} --stdout | aplay`
|
37
|
+
end
|
38
|
+
|
39
|
+
def mpg123(*args)
|
40
|
+
`mpg123 #{args.first}`
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ev3dev_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- quake wang
|
8
|
+
- Yoshiyuki Shibata
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-06-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.12'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.12'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
description: ev3dev ruby binding
|
43
|
+
email:
|
44
|
+
- aqua055@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- ev3dev.gemspec
|
55
|
+
- lib/ev3dev.rb
|
56
|
+
- lib/ev3dev/battery.rb
|
57
|
+
- lib/ev3dev/button.rb
|
58
|
+
- lib/ev3dev/device.rb
|
59
|
+
- lib/ev3dev/led.rb
|
60
|
+
- lib/ev3dev/motor.rb
|
61
|
+
- lib/ev3dev/sensor.rb
|
62
|
+
- lib/ev3dev/sound.rb
|
63
|
+
- lib/ev3dev/version.rb
|
64
|
+
homepage: https://github.com/noanoa07/ev3dev_ruby
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.2.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: ev3dev_ruby
|
88
|
+
test_files: []
|