artoo-gpio 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +59 -0
- data/LICENSE +13 -0
- data/README.md +53 -0
- data/Rakefile +10 -0
- data/artoo-gpio.gemspec +27 -0
- data/lib/artoo/drivers/button.rb +43 -0
- data/lib/artoo/drivers/led.rb +59 -0
- data/lib/artoo/drivers/maxbotix.rb +44 -0
- data/lib/artoo/drivers/motor.rb +167 -0
- data/lib/artoo/drivers/servo.rb +49 -0
- data/lib/artoo-gpio/version.rb +5 -0
- data/lib/artoo-gpio.rb +3 -0
- data/test/drivers/led_test.rb +92 -0
- data/test/drivers/maxbotix_test.rb +25 -0
- data/test/drivers/motor_test.rb +204 -0
- data/test/drivers/servo_test.rb +43 -0
- data/test/test_helper.rb +10 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dddfa7c2d25bd688baa7eb5d4311cf357aed9a91
|
4
|
+
data.tar.gz: b58a3d1be358edbc5c1261384af7a4677288d06a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 932aba40c52749c0deb31af3b96091c90fb791be0f41548d2b9d33e7c1787b442f6cdaae8f23cd414adf74b0ffa6f038cb870feb6cd87c26b67d1f114b44a67b
|
7
|
+
data.tar.gz: f23c0cd415319400a44c2ea49dabf55127cf8371b465f726778210520b2d9482f4e62eb63ab7fe15a28d60c95ca48be9049096c465fcc667d6ab6ce5af959bbb
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
artoo-gpio (0.0.1)
|
5
|
+
artoo (~> 1.1.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
artoo (1.1.0)
|
11
|
+
celluloid (~> 0.15.0)
|
12
|
+
celluloid-io (~> 0.15.0)
|
13
|
+
multi_json (~> 1.6)
|
14
|
+
pry (~> 0.9)
|
15
|
+
rake (~> 10.0)
|
16
|
+
reel (~> 0.4.0.pre)
|
17
|
+
thor (~> 0.18.1)
|
18
|
+
celluloid (0.15.1)
|
19
|
+
timers (~> 1.1.0)
|
20
|
+
celluloid-io (0.15.0)
|
21
|
+
celluloid (>= 0.15.0)
|
22
|
+
nio4r (>= 0.5.0)
|
23
|
+
coderay (1.0.9)
|
24
|
+
http (0.5.0.pre)
|
25
|
+
http_parser.rb
|
26
|
+
http_parser.rb (0.6.0.beta.2)
|
27
|
+
metaclass (0.0.1)
|
28
|
+
method_source (0.8.2)
|
29
|
+
minitest (5.0.7)
|
30
|
+
minitest-happy (1.0.0)
|
31
|
+
mocha (0.14.0)
|
32
|
+
metaclass (~> 0.0.1)
|
33
|
+
multi_json (1.8.0)
|
34
|
+
nio4r (0.5.0)
|
35
|
+
pry (0.9.12.2)
|
36
|
+
coderay (~> 1.0.5)
|
37
|
+
method_source (~> 0.8)
|
38
|
+
slop (~> 3.4)
|
39
|
+
rake (10.1.0)
|
40
|
+
reel (0.4.0.pre5)
|
41
|
+
celluloid (>= 0.15.0)
|
42
|
+
celluloid-io (>= 0.15.0)
|
43
|
+
http (>= 0.5.0.pre)
|
44
|
+
http_parser.rb (>= 0.6.0.beta.2)
|
45
|
+
websocket_parser (>= 0.1.4)
|
46
|
+
slop (3.4.6)
|
47
|
+
thor (0.18.1)
|
48
|
+
timers (1.1.0)
|
49
|
+
websocket_parser (0.1.4)
|
50
|
+
http
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
artoo-gpio!
|
57
|
+
minitest (~> 5.0)
|
58
|
+
minitest-happy
|
59
|
+
mocha (~> 0.14.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2012, 2013 The Hybrid Group
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Artoo Drivers For GPIO Devices
|
2
|
+
|
3
|
+
This repository contains the Artoo (http://artoo.io/) standard drivers for analog, digital, PWM, and servo devices.
|
4
|
+
|
5
|
+
Artoo is a open source micro-framework for robotics using Ruby.
|
6
|
+
|
7
|
+
For more information abut Artoo, check out our repo at https://github.com/hybridgroup/artoo
|
8
|
+
|
9
|
+
[![Code Climate](https://codeclimate.com/github/hybridgroup/artoo-gpio.png)](https://codeclimate.com/github/hybridgroup/artoo-gpio) [![Build Status](https://travis-ci.org/hybridgroup/artoo-gpio.png?branch=master)](https://travis-ci.org/hybridgroup/artoo-gpio)
|
10
|
+
|
11
|
+
## Installing
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install artoo-gpio
|
15
|
+
```
|
16
|
+
|
17
|
+
## Using
|
18
|
+
|
19
|
+
Normally, this gem is automatically included as part of using an Artoo adaptor that can connect to your hardware. For example, artoo-arduino and artoo-raspi both make use of the drivers in this gem.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'artoo'
|
23
|
+
|
24
|
+
connection :arduino, :adaptor => :firmata, :port => '127.0.0.1:8023'
|
25
|
+
device :board
|
26
|
+
device :led, :driver => :led, :pin => 13
|
27
|
+
|
28
|
+
work do
|
29
|
+
puts "Firmware name #{board.firmware_name}"
|
30
|
+
puts "Firmata version #{board.version}"
|
31
|
+
every 1.second do
|
32
|
+
led.toggle
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## Devices supported
|
38
|
+
|
39
|
+
The following GPIO hardware devices have driver support:
|
40
|
+
- Button
|
41
|
+
- LED
|
42
|
+
- Maxbotix ultrasonic range finder
|
43
|
+
- Motor (DC)
|
44
|
+
- Servo
|
45
|
+
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/artoo-gpio.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "artoo-gpio/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "artoo-gpio"
|
7
|
+
s.version = Artoo::Gpio::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ron Evans", "Adrian Zankich", "Rafael Magaña", "Edgar Silva"]
|
10
|
+
s.email = ["artoo@hybridgroup.com"]
|
11
|
+
s.homepage = "https://github.com/hybridgroup/artoo-gpio"
|
12
|
+
s.summary = %q{Artoo standard drivers for GPIO devices}
|
13
|
+
s.description = %q{Artoo standard drivers for GPIO devices such as digital IO, analog IO, PWM IO, and servos}
|
14
|
+
s.license = 'Apache 2.0'
|
15
|
+
|
16
|
+
s.rubyforge_project = "artoo-gpio"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
s.add_runtime_dependency 'artoo', '~> 1.1.0'
|
24
|
+
s.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
s.add_development_dependency 'minitest-happy'
|
26
|
+
s.add_development_dependency 'mocha', '~> 0.14.0'
|
27
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'artoo/drivers/driver'
|
2
|
+
|
3
|
+
module Artoo
|
4
|
+
module Drivers
|
5
|
+
# Button driver behaviors
|
6
|
+
class Button < Driver
|
7
|
+
COMMANDS = [:is_pressed?].freeze
|
8
|
+
|
9
|
+
DOWN = 1
|
10
|
+
UP = 0
|
11
|
+
|
12
|
+
# @return [Boolean] True if pressed
|
13
|
+
def is_pressed?
|
14
|
+
(@pressed_val == 1) ? true : false
|
15
|
+
end
|
16
|
+
|
17
|
+
def start_driver
|
18
|
+
@pressed_val = 0
|
19
|
+
|
20
|
+
every(interval) do
|
21
|
+
new_value = connection.digital_read(pin)
|
22
|
+
update(new_value) if !new_value.nil? && new_value != is_pressed?
|
23
|
+
end
|
24
|
+
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
# Publishes events according to the button feedback
|
30
|
+
def update(new_val)
|
31
|
+
if new_val == 1
|
32
|
+
@pressed_val = 1
|
33
|
+
publish(event_topic_name("update"), "push", new_val)
|
34
|
+
publish(event_topic_name("push"), new_val)
|
35
|
+
else
|
36
|
+
@pressed_val = 0
|
37
|
+
publish(event_topic_name("update"), "release", new_val)
|
38
|
+
publish(event_topic_name("release"), new_val)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'artoo/drivers/driver'
|
2
|
+
|
3
|
+
module Artoo
|
4
|
+
module Drivers
|
5
|
+
# The LED driver behaviors
|
6
|
+
class Led < Driver
|
7
|
+
|
8
|
+
COMMANDS = [:on, :off, :toggle,
|
9
|
+
:brightness,
|
10
|
+
:on?, :off?].freeze
|
11
|
+
|
12
|
+
def initialize(params = {})
|
13
|
+
@is_on = false
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [Boolean] True if on
|
18
|
+
def on?
|
19
|
+
@is_on
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Boolean] True if off
|
23
|
+
def off?
|
24
|
+
(not on?)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Sets led to level HIGH
|
28
|
+
def on
|
29
|
+
change_state(pin, :high)
|
30
|
+
@is_on = true
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
# Sets led to level LOW
|
35
|
+
def off
|
36
|
+
change_state(pin, :low)
|
37
|
+
@is_on = false
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
# Toggle status
|
42
|
+
# @example on > off, off > on
|
43
|
+
def toggle
|
44
|
+
on? ? off : on
|
45
|
+
end
|
46
|
+
|
47
|
+
# Change brightness level
|
48
|
+
# @param [Integer] level
|
49
|
+
def brightness(level=0)
|
50
|
+
connection.pwm_write(pin, level)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def change_state(pin, level)
|
55
|
+
connection.digital_write(pin, level)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'artoo/drivers/driver'
|
2
|
+
|
3
|
+
module Artoo
|
4
|
+
module Drivers
|
5
|
+
# Maxbotix ultrasonic range finder driver behaviors for Firmata
|
6
|
+
class Maxbotix < Driver
|
7
|
+
COMMANDS = [:range].freeze
|
8
|
+
|
9
|
+
attr_accessor :last_reading
|
10
|
+
|
11
|
+
def initialize(params={})
|
12
|
+
@last_reading = 0.0
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [float] last range reading in inches
|
17
|
+
def range
|
18
|
+
return ( 254.0 / 1024.0 ) * 2.0 * last_reading
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [float] last range reading in cm
|
22
|
+
def range_cm
|
23
|
+
return (last_reading / 2.0) * 2.54
|
24
|
+
end
|
25
|
+
|
26
|
+
# Sets values to read from ultrasonic range finder
|
27
|
+
# and starts driver
|
28
|
+
def start_driver
|
29
|
+
every(interval) do
|
30
|
+
update(connection.analog_read(pin))
|
31
|
+
end
|
32
|
+
|
33
|
+
super
|
34
|
+
end
|
35
|
+
|
36
|
+
# Publishes events according to the ultrasonic rangefinder value
|
37
|
+
def update(value)
|
38
|
+
last_reading = value
|
39
|
+
publish(event_topic_name("update"), "range", range)
|
40
|
+
publish(event_topic_name("range"), range)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'artoo/drivers/driver'
|
2
|
+
|
3
|
+
module Artoo
|
4
|
+
module Drivers
|
5
|
+
class Motor < Driver
|
6
|
+
|
7
|
+
COMMANDS = [:stop, :start, :on?, :off?, :toggle, :speed, :min, :max, :forward, :backward, :current_speed].freeze
|
8
|
+
|
9
|
+
attr_reader :speed_pin, :switch_pin, :current_speed
|
10
|
+
|
11
|
+
def initialize(params={})
|
12
|
+
super
|
13
|
+
|
14
|
+
additional_params = params[:additional_params]
|
15
|
+
@speed_pin = additional_params[:speed_pin]
|
16
|
+
@switch_pin = additional_params[:switch_pin] if additional_params[:switch_pin]
|
17
|
+
|
18
|
+
@forward_pin = additional_params[:forward_pin]
|
19
|
+
@backward_pin = additional_params[:backward_pin]
|
20
|
+
|
21
|
+
@current_state = :low
|
22
|
+
@current_speed = 0
|
23
|
+
|
24
|
+
# digital: just to switch the motor on or off, no speed control
|
25
|
+
# analog: speed control
|
26
|
+
@current_mode = :digital
|
27
|
+
|
28
|
+
@current_direction = :forward
|
29
|
+
|
30
|
+
@@modules_to_include = modules_to_include
|
31
|
+
|
32
|
+
class << self
|
33
|
+
@@modules_to_include.each do |m|
|
34
|
+
include m
|
35
|
+
end if @@modules_to_include
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def digital?
|
41
|
+
@current_mode == :digital
|
42
|
+
end
|
43
|
+
|
44
|
+
def analog?
|
45
|
+
@current_mode == :analog
|
46
|
+
end
|
47
|
+
|
48
|
+
def stop
|
49
|
+
if digital?
|
50
|
+
change_state(:low)
|
51
|
+
else
|
52
|
+
speed(0)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def start
|
57
|
+
if digital?
|
58
|
+
change_state(:high)
|
59
|
+
else
|
60
|
+
speed(@current_speed.zero? ? 255 : @current_speed)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def min
|
65
|
+
stop
|
66
|
+
end
|
67
|
+
|
68
|
+
def max
|
69
|
+
speed(255)
|
70
|
+
end
|
71
|
+
|
72
|
+
def on?
|
73
|
+
if digital?
|
74
|
+
@current_state == :high
|
75
|
+
else
|
76
|
+
@current_speed > 0
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def off?
|
81
|
+
!on?
|
82
|
+
end
|
83
|
+
|
84
|
+
def toggle
|
85
|
+
on? ? stop : start
|
86
|
+
end
|
87
|
+
|
88
|
+
def change_state(state)
|
89
|
+
@current_state = state
|
90
|
+
@current_speed = state == :low ? 0 : 255
|
91
|
+
connection.digital_write(@speed_pin, state)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Set motor speed
|
95
|
+
# @param [Integer] value (must be an integer between 0-255)
|
96
|
+
def speed(value)
|
97
|
+
@current_mode = :analog
|
98
|
+
raise "Motor speed must be an integer between 0-255" unless (value.is_a?(Numeric) && value >= 0 && value <= 255)
|
99
|
+
@current_speed = value
|
100
|
+
connection.pwm_write(speed_pin, value)
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def modules_to_include
|
105
|
+
if @forward_pin and @backward_pin
|
106
|
+
[BidirectionalWithForwardBackwardPins]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
module BidirectionalWithForwardBackwardPins
|
112
|
+
|
113
|
+
# Sets movement forward
|
114
|
+
# @param [Integer] speed
|
115
|
+
def forward(speed = nil)
|
116
|
+
direction(:forward)
|
117
|
+
speed ? self.speed(speed) : start
|
118
|
+
end
|
119
|
+
|
120
|
+
# Sets movement backward
|
121
|
+
# @param [Integer] speed
|
122
|
+
def backward(speed = nil)
|
123
|
+
direction(:backward)
|
124
|
+
speed ? self.speed(speed) : start
|
125
|
+
end
|
126
|
+
|
127
|
+
def forward?
|
128
|
+
@current_direction == :forward
|
129
|
+
end
|
130
|
+
|
131
|
+
def backward?
|
132
|
+
(not forward?)
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
def change_state(state)
|
137
|
+
@current_state = state
|
138
|
+
@current_speed = state.zero? ? 0 : 255
|
139
|
+
if state == :high
|
140
|
+
direction(@current_direction)
|
141
|
+
speed(@current_speed) if speed_pin
|
142
|
+
elsif state == :low
|
143
|
+
direction(:none)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def direction(direction)
|
148
|
+
@current_direction = direction
|
149
|
+
case direction
|
150
|
+
when :forward
|
151
|
+
forward_pin_level = :high
|
152
|
+
backward_pin_level = :low
|
153
|
+
when :backward
|
154
|
+
forward_pin_level = :low
|
155
|
+
backward_pin_level = :high
|
156
|
+
when :none
|
157
|
+
forward_pin_level = :low
|
158
|
+
backward_pin_level = :low
|
159
|
+
end
|
160
|
+
|
161
|
+
connection.digital_write(@forward_pin, forward_pin_level)
|
162
|
+
connection.digital_write(@backward_pin, backward_pin_level)
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'artoo/drivers/driver'
|
2
|
+
|
3
|
+
module Artoo
|
4
|
+
module Drivers
|
5
|
+
# Servo behaviors for Firmata
|
6
|
+
class Servo < Driver
|
7
|
+
COMMANDS = [:move, :min, :center, :max, :current_angle].freeze
|
8
|
+
|
9
|
+
attr_reader :current_angle
|
10
|
+
|
11
|
+
# Create new Servo with angle=0
|
12
|
+
def initialize(params={})
|
13
|
+
super
|
14
|
+
|
15
|
+
@current_angle = 0
|
16
|
+
end
|
17
|
+
|
18
|
+
# Moves to specified angle
|
19
|
+
# @param [Integer] angle must be between 0-180
|
20
|
+
def move(angle)
|
21
|
+
raise "Servo angle must be an integer between 0-180" unless (angle.is_a?(Numeric) && angle >= 0 && angle <= 180)
|
22
|
+
|
23
|
+
@current_angle = angle
|
24
|
+
connection.servo_write(pin, angle_to_span(angle))
|
25
|
+
end
|
26
|
+
|
27
|
+
# Moves to min position
|
28
|
+
def min
|
29
|
+
move(0)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Moves to center position
|
33
|
+
def center
|
34
|
+
move(90)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Moves to max position
|
38
|
+
def max
|
39
|
+
move(180)
|
40
|
+
end
|
41
|
+
|
42
|
+
# converts an angle to a span between 0-255
|
43
|
+
# @param [Integer] angle
|
44
|
+
def angle_to_span(angle)
|
45
|
+
(angle * 255 / 180).to_i
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/artoo-gpio.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
|
2
|
+
require 'artoo/drivers/led'
|
3
|
+
|
4
|
+
describe Artoo::Drivers::Led do
|
5
|
+
before do
|
6
|
+
@device = mock('device')
|
7
|
+
@pin = 13
|
8
|
+
@device.stubs(:pin).returns(@pin)
|
9
|
+
@led = Artoo::Drivers::Led.new(:parent => @device)
|
10
|
+
@connection = mock('connection')
|
11
|
+
@device.stubs(:connection).returns(@connection)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'state switching' do
|
15
|
+
describe '#on' do
|
16
|
+
it 'must turn the led on' do
|
17
|
+
@connection.expects(:digital_write).with(@pin, :high)
|
18
|
+
@led.on
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#off' do
|
23
|
+
it 'must turn the led off' do
|
24
|
+
@connection.expects(:digital_write).with(@pin, :low)
|
25
|
+
@led.off
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'state checking' do
|
31
|
+
|
32
|
+
before do
|
33
|
+
@led.stubs(:change_state)
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#on?' do
|
37
|
+
it 'must return true if led is on' do
|
38
|
+
@led.on
|
39
|
+
@led.on?.must_equal true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'must return false if led is off' do
|
43
|
+
@led.off
|
44
|
+
@led.on?.must_equal false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#off?' do
|
49
|
+
it 'must return true if led is off' do
|
50
|
+
@led.off
|
51
|
+
@led.off?.must_equal true
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'must return false if led is on' do
|
55
|
+
@led.on
|
56
|
+
@led.off?.must_equal false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#toggle' do
|
62
|
+
it 'must toggle the state of the led' do
|
63
|
+
@led.stubs(:pin_state_on_board).returns(false)
|
64
|
+
@connection.stubs(:set_pin_mode)
|
65
|
+
@connection.stubs(:digital_write)
|
66
|
+
@led.off?.must_equal true
|
67
|
+
@led.toggle
|
68
|
+
@led.on?.must_equal true
|
69
|
+
@led.toggle
|
70
|
+
@led.off?.must_equal true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#brightness' do
|
75
|
+
it 'must change the brightness of the led' do
|
76
|
+
val = 100
|
77
|
+
@connection.expects(:pwm_write).with(@pin, val)
|
78
|
+
@led.brightness(val)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#commands' do
|
83
|
+
it 'must contain all the necessary commands' do
|
84
|
+
@led.commands.must_include :on
|
85
|
+
@led.commands.must_include :off
|
86
|
+
@led.commands.must_include :toggle
|
87
|
+
@led.commands.must_include :brightness
|
88
|
+
@led.commands.must_include :on?
|
89
|
+
@led.commands.must_include :off?
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
|
2
|
+
require 'artoo/drivers/maxbotix'
|
3
|
+
|
4
|
+
describe Artoo::Drivers::Maxbotix do
|
5
|
+
before do
|
6
|
+
@device = mock('device')
|
7
|
+
@pin = 0
|
8
|
+
@device.stubs(:pin).returns(@pin)
|
9
|
+
@maxbotix = Artoo::Drivers::Maxbotix.new(:parent => @device)
|
10
|
+
@connection = mock('connection')
|
11
|
+
@device.stubs(:connection).returns(@connection)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#range' do
|
15
|
+
it 'must have initial value' do
|
16
|
+
@maxbotix.range.must_equal 0.0
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'must adjust based on last_reading' do
|
20
|
+
@maxbotix.last_reading = 13.5
|
21
|
+
@maxbotix.range.must_equal 6.697265625
|
22
|
+
@maxbotix.range_cm.must_equal 17.145
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
|
2
|
+
require 'artoo/drivers/motor'
|
3
|
+
|
4
|
+
describe Artoo::Drivers::Motor do
|
5
|
+
|
6
|
+
let(:connection) { mock('connection') }
|
7
|
+
|
8
|
+
before do
|
9
|
+
@device = mock('device')
|
10
|
+
connection.stubs(:pwm_write)
|
11
|
+
connection.stubs(:digital_write)
|
12
|
+
@device.stubs(:connection).returns(connection)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'mode predicate methods' do
|
16
|
+
|
17
|
+
let(:motor) { Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {}) }
|
18
|
+
|
19
|
+
it '#digital?' do
|
20
|
+
motor.digital?.must_equal true #initial state
|
21
|
+
motor.speed(1)
|
22
|
+
motor.digital?.must_equal false
|
23
|
+
end
|
24
|
+
|
25
|
+
it '#analog?' do
|
26
|
+
motor.analog?.must_equal false
|
27
|
+
motor.speed(1)
|
28
|
+
motor.analog?.must_equal true
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'when unidirectional' do
|
34
|
+
|
35
|
+
describe 'when switching states (digital)' do
|
36
|
+
|
37
|
+
let(:motor) { Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {switch_pin: 1}) }
|
38
|
+
|
39
|
+
it '#start' do
|
40
|
+
motor.start
|
41
|
+
motor.on?.must_equal true
|
42
|
+
end
|
43
|
+
|
44
|
+
it '#stop' do
|
45
|
+
motor.stop
|
46
|
+
motor.off?.must_equal true
|
47
|
+
end
|
48
|
+
|
49
|
+
it '#max' do
|
50
|
+
motor.expects(:speed).with(255)
|
51
|
+
motor.max
|
52
|
+
end
|
53
|
+
|
54
|
+
it '#min' do
|
55
|
+
motor.min
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#toggle' do
|
59
|
+
it 'toggles to on when off' do
|
60
|
+
motor.start
|
61
|
+
motor.toggle
|
62
|
+
motor.off?.must_equal true
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'toggles to off when on' do
|
66
|
+
motor.stop
|
67
|
+
motor.toggle
|
68
|
+
motor.on?.must_equal true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'when changing speed (analog)' do
|
74
|
+
|
75
|
+
let(:motor) { Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {speed_pin: 3}) }
|
76
|
+
|
77
|
+
it '#speed' do
|
78
|
+
connection.expects(:pwm_write).with(3, 255)
|
79
|
+
motor.speed(255)
|
80
|
+
end
|
81
|
+
|
82
|
+
it '#start' do
|
83
|
+
motor.speed(0)
|
84
|
+
motor.start
|
85
|
+
motor.current_speed.must_equal 255
|
86
|
+
end
|
87
|
+
|
88
|
+
it '#stop' do
|
89
|
+
motor.speed(255)
|
90
|
+
motor.stop
|
91
|
+
motor.current_speed.must_equal 0
|
92
|
+
end
|
93
|
+
|
94
|
+
it '#max' do
|
95
|
+
motor.speed(0)
|
96
|
+
motor.max
|
97
|
+
motor.current_speed.must_equal 255
|
98
|
+
end
|
99
|
+
|
100
|
+
it '#min' do
|
101
|
+
motor.speed(255)
|
102
|
+
motor.min
|
103
|
+
motor.current_speed.must_equal 0
|
104
|
+
end
|
105
|
+
|
106
|
+
it '#on?' do
|
107
|
+
motor.speed(1)
|
108
|
+
motor.on?.must_equal true
|
109
|
+
motor.speed(0)
|
110
|
+
motor.on?.must_equal false
|
111
|
+
end
|
112
|
+
|
113
|
+
it '#off?' do
|
114
|
+
motor.speed(1)
|
115
|
+
motor.off?.must_equal false
|
116
|
+
motor.speed(0)
|
117
|
+
motor.off?.must_equal true
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'bididirectional' do
|
124
|
+
let(:motor) { Artoo::Drivers::Motor.new(:parent => @device,
|
125
|
+
:additional_params =>
|
126
|
+
{:forward_pin => 1,
|
127
|
+
:backward_pin => 2}) }
|
128
|
+
|
129
|
+
describe '#forward' do
|
130
|
+
|
131
|
+
before do
|
132
|
+
connection.expects(:digital_write).with(1, :high)
|
133
|
+
connection.expects(:digital_write).with(2, :low)
|
134
|
+
end
|
135
|
+
|
136
|
+
describe 'when no parameter' do
|
137
|
+
|
138
|
+
it '#forward' do
|
139
|
+
motor.expects(:start)
|
140
|
+
motor.forward
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
describe 'when speed parameter' do
|
146
|
+
|
147
|
+
it '#forward' do
|
148
|
+
motor.expects(:speed).with(255)
|
149
|
+
motor.forward(255)
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
describe '#backward' do
|
157
|
+
|
158
|
+
before do
|
159
|
+
connection.expects(:digital_write).with(1, :low)
|
160
|
+
connection.expects(:digital_write).with(2, :high)
|
161
|
+
end
|
162
|
+
|
163
|
+
describe 'when no parameter' do
|
164
|
+
|
165
|
+
it '#backward' do
|
166
|
+
motor.expects(:start)
|
167
|
+
motor.backward
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
describe 'when speed parameter' do
|
173
|
+
|
174
|
+
it '#backward' do
|
175
|
+
motor.expects(:speed).with(255)
|
176
|
+
motor.backward(255)
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
|
185
|
+
#it 'Motor#speed must be valid' do
|
186
|
+
#invalid_speed = lambda { @motor2 = Artoo::Drivers::Motor.new(:parent => @device, :additional_params => {}); @motor2.speed("ads") }
|
187
|
+
#invalid_speed.must_raise RuntimeError
|
188
|
+
#error = invalid_speed.call rescue $!
|
189
|
+
#error.message.must_equal 'Motor speed must be an integer between 0-255'
|
190
|
+
#end
|
191
|
+
|
192
|
+
#it 'Motor#forward' do
|
193
|
+
#@motor.expects(:set_legs)
|
194
|
+
#@motor.forward(100)
|
195
|
+
#@motor.current_speed.must_equal 100
|
196
|
+
#end
|
197
|
+
|
198
|
+
#it 'Motor#backward' do
|
199
|
+
#@motor.expects(:set_legs)
|
200
|
+
#@motor.backward(100)
|
201
|
+
#@motor.current_speed.must_equal 100
|
202
|
+
#end
|
203
|
+
|
204
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
|
2
|
+
require 'artoo/drivers/servo'
|
3
|
+
|
4
|
+
describe Artoo::Drivers::Servo do
|
5
|
+
before do
|
6
|
+
@device = mock('device')
|
7
|
+
@device.stubs(:pin).returns(3)
|
8
|
+
@servo = Artoo::Drivers::Servo.new(:parent => @device)
|
9
|
+
|
10
|
+
@connection = mock('connection')
|
11
|
+
@connection.stubs(:servo_write)
|
12
|
+
@device.stubs(:connection).returns(@connection)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "Servo#angle_to_span" do
|
16
|
+
@servo.angle_to_span(0).must_equal 0
|
17
|
+
@servo.angle_to_span(30).must_equal 42
|
18
|
+
@servo.angle_to_span(90).must_equal 127
|
19
|
+
@servo.angle_to_span(180).must_equal 255
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'Servo#move must be valid' do
|
23
|
+
invalid_angle = lambda { @servo2 = Artoo::Drivers::Servo.new(:parent => @device); @servo2.move(360) }
|
24
|
+
invalid_angle.must_raise RuntimeError
|
25
|
+
error = invalid_angle.call rescue $!
|
26
|
+
error.message.must_equal 'Servo angle must be an integer between 0-180'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'Servo#min' do
|
30
|
+
@servo.min
|
31
|
+
@servo.current_angle.must_equal 0
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'Servo#center' do
|
35
|
+
@servo.center
|
36
|
+
@servo.current_angle.must_equal 90
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'Servo#max' do
|
40
|
+
@servo.max
|
41
|
+
@servo.current_angle.must_equal 180
|
42
|
+
end
|
43
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: artoo-gpio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ron Evans
|
8
|
+
- Adrian Zankich
|
9
|
+
- Rafael Magaña
|
10
|
+
- Edgar Silva
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2013-09-11 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: artoo
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.1.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ~>
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '5.0'
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '5.0'
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: minitest-happy
|
46
|
+
requirement: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: mocha
|
60
|
+
requirement: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ~>
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 0.14.0
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 0.14.0
|
72
|
+
description: Artoo standard drivers for GPIO devices such as digital IO, analog IO,
|
73
|
+
PWM IO, and servos
|
74
|
+
email:
|
75
|
+
- artoo@hybridgroup.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- artoo-gpio.gemspec
|
86
|
+
- lib/artoo-gpio.rb
|
87
|
+
- lib/artoo-gpio/version.rb
|
88
|
+
- lib/artoo/drivers/button.rb
|
89
|
+
- lib/artoo/drivers/led.rb
|
90
|
+
- lib/artoo/drivers/maxbotix.rb
|
91
|
+
- lib/artoo/drivers/motor.rb
|
92
|
+
- lib/artoo/drivers/servo.rb
|
93
|
+
- test/drivers/led_test.rb
|
94
|
+
- test/drivers/maxbotix_test.rb
|
95
|
+
- test/drivers/motor_test.rb
|
96
|
+
- test/drivers/servo_test.rb
|
97
|
+
- test/test_helper.rb
|
98
|
+
homepage: https://github.com/hybridgroup/artoo-gpio
|
99
|
+
licenses:
|
100
|
+
- Apache 2.0
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project: artoo-gpio
|
118
|
+
rubygems_version: 2.0.3
|
119
|
+
signing_key:
|
120
|
+
specification_version: 4
|
121
|
+
summary: Artoo standard drivers for GPIO devices
|
122
|
+
test_files: []
|