wall_e 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ harness.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wall_e.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 'Mike Breen'
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,29 @@
1
+ # WallE
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'wall_e'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install wall_e
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "lib"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => [:test]
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ WallE::Assembler.build do
5
+
6
+ led = Led(9)
7
+ rate = 0.75
8
+
9
+ repeat do
10
+ led.toggle
11
+ delay rate
12
+ end
13
+
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ WallE::Assembler.build do
5
+
6
+ led = Led(9)
7
+
8
+ repeat do
9
+
10
+ if led.value == 255
11
+ led.off
12
+ turn_off
13
+ end
14
+
15
+ led.brightness(led.value + 1)
16
+
17
+ delay 0.01
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ WallE::Assembler.build do
5
+
6
+ led = Led(9)
7
+ led.brightness(255)
8
+
9
+ repeat do
10
+ turn_off if led.value == 0
11
+
12
+ led.brightness(led.value - 1)
13
+
14
+ delay 0.01
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ WallE::Assembler.build do
5
+
6
+ led = Led(9)
7
+
8
+ direction = 1000 / (255 * 2)
9
+
10
+ repeat do
11
+
12
+ direction = 1 if led.value.zero?
13
+
14
+ direction = -1 if led.value == 255
15
+
16
+ led.brightness(led.value + direction)
17
+
18
+ delay 0.01
19
+ end
20
+ end
@@ -0,0 +1,83 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ class MorseCode
5
+
6
+ LETTERS = {
7
+ "a" => ".-",
8
+ "b" => "-...",
9
+ "c" => "-.-.",
10
+ "d" => "-..",
11
+ "e" => ".",
12
+ "f" => "..-.",
13
+ "g" => "--.",
14
+ "h" => "....",
15
+ "i" => "..",
16
+ "j" => ".---",
17
+ "k" => "-.-",
18
+ "l" => ".-..",
19
+ "m" => "--",
20
+ "o" => "---",
21
+ "p" => ".--.",
22
+ "q" => "--.-",
23
+ "r" => ".-.",
24
+ "s" => "...",
25
+ "t" => "-",
26
+ "u" => "..-",
27
+ "v" => "...-",
28
+ "w" => ".--",
29
+ "x" => "-..-",
30
+ "y" => "-.--",
31
+ "z" => "--.."
32
+ }
33
+
34
+ def initialize(piezo)
35
+ @piezo = piezo
36
+ @dit_length = 0.15
37
+ @dah_length = @dit_length * 3
38
+ @space_length = @dit_length * 7
39
+ @tone = 1275
40
+ end
41
+
42
+ def dit
43
+ puts '.'
44
+ output(@dit_length)
45
+ end
46
+
47
+ def dah
48
+ puts '-'
49
+ output(@dah_length)
50
+ end
51
+
52
+ def output(duration)
53
+ @piezo.on(@tone)
54
+ sleep(duration)
55
+ @piezo.off
56
+ sleep(@dit_length)
57
+ end
58
+
59
+ def say(string)
60
+ string.each_char do |letter|
61
+ pattern = LETTERS[letter.downcase]
62
+ if pattern.nil?
63
+ sleep @space_length
64
+ else
65
+ pattern.each_char do |p|
66
+ case p
67
+ when '.' then dit
68
+ when '-' then dah
69
+ end
70
+ end
71
+ sleep @dah_length
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ WallE::Assembler.build do
78
+ piezo = Piezo(9)
79
+
80
+ mc = MorseCode.new(piezo)
81
+
82
+ mc.say('Hello World')
83
+ end
@@ -0,0 +1,18 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ WallE::Assembler.build do
5
+
6
+ servo = Servo(9)
7
+ servo.min
8
+
9
+ repeat do
10
+
11
+ servo.min if servo.maxed?
12
+
13
+ servo.move_to(servo.position + 1)
14
+
15
+ delay 0.01
16
+ end
17
+
18
+ end
data/lib/wall_e.rb ADDED
@@ -0,0 +1,97 @@
1
+ require 'wall_e/version'
2
+ require 'wall_e/serial_snoop'
3
+ require 'wall_e/pin'
4
+ require 'pry'
5
+
6
+ require 'wall_e/components/led'
7
+ require 'wall_e/components/servo'
8
+ require 'wall_e/components/piezo'
9
+
10
+
11
+
12
+ module WallE
13
+ class Assembler
14
+
15
+ attr_reader :board
16
+
17
+ def self.build(&block)
18
+
19
+ arduino = SerialSnoop.locate_ports
20
+
21
+ wall_e = new(arduino)
22
+
23
+ wall_e.instance_eval(&block)
24
+
25
+ Pry.start(wall_e, :prompt => [ proc { |obj, *| "wall_e > " }, proc { |obj, *| "wall_e* "} ])
26
+ end
27
+
28
+ def initialize(arduino)
29
+ @board = arduino
30
+ @board.connect unless arduino.connected?
31
+ @running = true
32
+ @group = ThreadGroup.new
33
+
34
+ Thread.new do
35
+ loop do
36
+ begin
37
+ arduino.read_and_process
38
+ sleep(0.5)
39
+ rescue Exception => e
40
+ puts e.message
41
+ puts e.backtrace.inspect
42
+ Thread.kill
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def Led(pin_number)
49
+ pin = Pin.new(pin_number, @board)
50
+ Led.new(pin)
51
+ end
52
+
53
+ def Servo(pin_number, options = {})
54
+ pin = Pin.new(pin_number, @board)
55
+ Servo.new(pin, options)
56
+ end
57
+
58
+ def Piezo(pin_number)
59
+ pin = Pin.new(pin_number, @board)
60
+ Piezo.new(pin)
61
+ end
62
+
63
+ def delay(seconds)
64
+ @board.delay seconds
65
+ end
66
+
67
+ def pause
68
+ @running = false
69
+ end
70
+
71
+ def resume
72
+ @running = true
73
+ @group.list.each(&:wakeup)
74
+ end
75
+
76
+ def turn_off
77
+ @group.list.each(&:kill)
78
+ end
79
+
80
+ def repeat(&block)
81
+ t = Thread.new do
82
+ loop do
83
+ Thread.stop unless @running
84
+ begin
85
+ block.call
86
+ rescue Exception => e
87
+ puts e.message
88
+ puts e.backtrace.inspect
89
+ Thread.kill
90
+ end
91
+ end
92
+ end
93
+
94
+ @group.add(t)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,68 @@
1
+ module WallE
2
+ class Led
3
+
4
+ # Public: Initialize an LED
5
+ #
6
+ # pin - the Pin the LED is attached to.
7
+ def initialize(pin)
8
+ @pin = pin
9
+ @is_on = false
10
+ end
11
+
12
+ # Public: Turn the LED on.
13
+ #
14
+ # Returns nothing.
15
+ def on
16
+ @pin.set_mode(Pin::OUTPUT)
17
+ @pin.digital_write(Pin::HIGH)
18
+ @is_on = true
19
+ end
20
+
21
+ # Public: Turn the LED off.
22
+ #
23
+ # Returns nothing.
24
+ def off
25
+ @pin.set_mode(Pin::OUTPUT)
26
+ @pin.digital_write(Pin::LOW)
27
+ @is_on = false
28
+ end
29
+
30
+ # Public: Set the brightness of the LED.
31
+ #
32
+ # Returns nothing.
33
+ def brightness(value)
34
+ @pin.set_mode(Pin::PWM)
35
+ @pin.analog_write(value)
36
+ end
37
+
38
+ # Public: Indicates if the LED is currently on.
39
+ #
40
+ # Returns Boolean.
41
+ def on?
42
+ @is_on
43
+ end
44
+
45
+ # Public: Indicates if the LED is current off.
46
+ def off?
47
+ !on?
48
+ end
49
+
50
+ # Public: Toggle the LED on or off.
51
+ #
52
+ # Returns nothing.
53
+ def toggle
54
+ if on?
55
+ off
56
+ else
57
+ on
58
+ end
59
+ end
60
+
61
+ # Public: The current value of the LED.
62
+ #
63
+ # Returns Integer value.
64
+ def value
65
+ @pin.value
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,45 @@
1
+ module WallE
2
+ class Piezo
3
+
4
+ # Public: Initialize a Piezo
5
+ #
6
+ # pin - the Pin the piezo is attached to
7
+ def initialize(pin)
8
+ @pin = pin
9
+ @pin.set_mode(Pin::PWM)
10
+ @is_on = false
11
+ end
12
+
13
+ # Public: Turn the piezo on.
14
+ #
15
+ # tone - the Integer tone to play.
16
+ #
17
+ # Returns nothing.
18
+ def on(tone)
19
+ @is_on = true
20
+ @pin.analog_write(tone)
21
+ end
22
+
23
+ # Public: Turn the piezo off.
24
+ #
25
+ # Returns nothing.
26
+ def off
27
+ @is_on = false
28
+ @pin.analog_write(0)
29
+ end
30
+
31
+ # Public: Indicates if the piezo is currently on.
32
+ #
33
+ # Returns Boolean.
34
+ def on?
35
+ @is_on
36
+ end
37
+
38
+ # Pubilc: Indicates if the piezo is currently off.
39
+ #
40
+ # Returns Boolean.
41
+ def off?
42
+ !@is_on
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,56 @@
1
+ module WallE
2
+ class Servo
3
+ OutOfBoundsError = Class.new(StandardError)
4
+
5
+ # Public: Initialize a Servo
6
+ #
7
+ # pin - the Pin the servo is attached to.
8
+ # options - the Hash options to configure the servo (default: {}):
9
+ # :range - the Range to restrict movement (default 0..180).
10
+ def initialize(pin, options = {})
11
+ @pin = pin
12
+ @pin.set_mode(Pin::SERVO)
13
+ @range = options.fetch(:range) { 0..180 }
14
+ end
15
+
16
+ # Public: Move the servo the the minimum degree.
17
+ #
18
+ # Returns nothing.
19
+ def min
20
+ move_to @range.min
21
+ end
22
+
23
+ # Public: Move the servo to the maximum degree.
24
+ #
25
+ # Returns nothing.
26
+ def max
27
+ move_to @range.max
28
+ end
29
+
30
+ # Public: Move the servo to the center degree.
31
+ #
32
+ # Returns nothing.
33
+ def center
34
+ move_to ((min + max) / 2).abs
35
+ end
36
+
37
+ # Public: Move the servo.
38
+ #
39
+ # degrees - the Integer degrees to move to.
40
+ #
41
+ # Returns nothing.
42
+ # Raises OutOfBoundsError if the servo cannot move to the degree.
43
+ def move_to(degrees)
44
+ raise OutOfBoundsError unless @range.include?(degrees)
45
+ @pin.servo_write(degrees)
46
+ end
47
+
48
+ def maxed?
49
+ position == @range.max
50
+ end
51
+
52
+ def position
53
+ @pin.value
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,15 @@
1
+ module WallE
2
+ module Logger
3
+ def info(message)
4
+ log("\033[34m#{message}\033\[0m")
5
+ end
6
+
7
+ def error(message)
8
+ log("\033[31m#{message}\033\[0m")
9
+ end
10
+
11
+ def log(message)
12
+ puts message
13
+ end
14
+ end
15
+ end
data/lib/wall_e/pin.rb ADDED
@@ -0,0 +1,84 @@
1
+ module WallE
2
+ class Pin
3
+ class UnsupportedModeError < StandardError; end
4
+
5
+ # Internal: Fixnum byte for pin mode input.
6
+ INPUT = 0x00
7
+ # Internal: Fixnum byte for pin mode output.
8
+ OUTPUT = 0x01
9
+ # Internal: Fixnum byte for pin mode analog.
10
+ ANALOG = 0x02
11
+ # Internal: Fixnum byte for pin mode pulse width modulation.
12
+ PWM = 0x03
13
+ # Internal: Fixnum byte for pin mode servo.
14
+ SERVO = 0x04
15
+
16
+ LOW = 0
17
+ HIGH = 1
18
+
19
+ # Public: Returns the Integer pin number.
20
+ attr_reader :number
21
+
22
+ # Public: Initialize a Pin
23
+ #
24
+ # number - the Integer pin number on the board.
25
+ # board - the WallE::Board this pin is on.
26
+ # mode - the Fixnum mode to set the pin to (default: OUTPUT).
27
+ def initialize(number, board, mode = OUTPUT)
28
+ @number = number
29
+ @board = board
30
+ @onboard_pin = @board.pins[@number]
31
+ set_mode(mode)
32
+ end
33
+
34
+ # Public: Write a digital value to the pin.
35
+ #
36
+ # value - an Integer value.
37
+ #
38
+ # Returns nothing.
39
+ def digital_write(value)
40
+ @board.digital_write(@number, value)
41
+ end
42
+
43
+ # Public: Write analog value to the pin
44
+ #
45
+ # value - an Integer value.
46
+ #
47
+ # Returns nothing.
48
+ def analog_write(value)
49
+ @board.analog_write(@number, value)
50
+ end
51
+
52
+ # Public: write value to servo
53
+ #
54
+ # value - an Integer value.
55
+ #
56
+ # Returns nothing.
57
+ alias_method :servo_write, :analog_write
58
+
59
+ # Public: Set the pin mode.
60
+ #
61
+ # mode - an Integer mode.
62
+ #
63
+ # Returns nothing.
64
+ # Raises UnsupportedModeError if the pin does not support the mode.
65
+ def set_mode(mode)
66
+ raise UnsupportedModeError unless @onboard_pin.supported_modes.include?(mode)
67
+ @board.set_pin_mode(@number, mode) unless current_mode == mode
68
+ end
69
+
70
+ # Public: Get the current mode.
71
+ #
72
+ # Returns Integer mode.
73
+ def current_mode
74
+ @onboard_pin.mode
75
+ end
76
+
77
+ # Public: Get the current value of the pin.
78
+ #
79
+ # Returns Integer value.
80
+ def value
81
+ @onboard_pin.value
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,37 @@
1
+ require_relative 'logger'
2
+ require 'firmata'
3
+
4
+ module WallE
5
+ module SerialSnoop
6
+ extend self
7
+ extend Logger
8
+
9
+ def locate_ports
10
+ ports = Dir['/dev/*'].grep(/usb|acm/)
11
+ board = nil
12
+ if ports.any?
13
+ what = 'serial port'
14
+ what << 's' unless ports.one?
15
+
16
+ info "Found possible #{what} #{ports}"
17
+ ports.each do |port|
18
+ begin
19
+ info "Connecting to #{port}..."
20
+ board = Firmata::Board.new(port)
21
+ info "Connected to #{port}."
22
+ break # we've found a board
23
+ rescue => e
24
+ error e.message
25
+ board = nil
26
+ end
27
+ end
28
+ else
29
+ error 'Error: No USB devices detected'
30
+ end
31
+
32
+ error 'Error: Unable to connect to USB device' unless board
33
+
34
+ board
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module WallE
2
+ VERSION = "0.0.1"
3
+ end
data/test/led_test.rb ADDED
@@ -0,0 +1,66 @@
1
+ require_relative 'test_helper'
2
+ require_relative '../lib/wall_e/pin'
3
+ require_relative '../lib/wall_e/components/led'
4
+
5
+
6
+
7
+ class LedTest < MiniTest::Unit::TestCase
8
+
9
+ def test_turning_on
10
+ pin = MiniTest::Mock.new
11
+ pin.expect(:set_mode, 1, [WallE::Pin::OUTPUT])
12
+ pin.expect(:digital_write, 1, [WallE::Pin::HIGH])
13
+
14
+ led = WallE::Led.new(pin)
15
+
16
+ led.on
17
+
18
+ assert led.on?
19
+ pin.verify
20
+ end
21
+
22
+ def test_turning_off
23
+ pin = MiniTest::Mock.new
24
+ pin.expect(:set_mode, 1, [WallE::Pin::OUTPUT])
25
+ pin.expect(:digital_write, 1, [WallE::Pin::LOW])
26
+
27
+ led = WallE::Led.new(pin)
28
+
29
+ led.off
30
+
31
+ assert led.off?
32
+ pin.verify
33
+ end
34
+
35
+ def test_toggle
36
+ pin = MiniTest::Mock.new
37
+ pin.expect(:set_mode, 1, [WallE::Pin::OUTPUT])
38
+ pin.expect(:digital_write, 1, [WallE::Pin::HIGH])
39
+
40
+ led = WallE::Led.new(pin)
41
+
42
+ led.toggle
43
+
44
+ assert led.on?, 'led not toggled on'
45
+ pin.verify
46
+
47
+ pin.expect(:set_mode, 1, [WallE::Pin::OUTPUT])
48
+ pin.expect(:digital_write, 1, [WallE::Pin::LOW])
49
+
50
+ led.toggle
51
+
52
+ assert led.off?, 'led not toggled off'
53
+ pin.verify
54
+ end
55
+
56
+ def test_brightness
57
+ pin = MiniTest::Mock.new
58
+ pin.expect(:set_mode, 1, [WallE::Pin::PWM])
59
+ pin.expect(:analog_write, 1, [255])
60
+
61
+ led = WallE::Led.new(pin)
62
+
63
+ led.brightness(255)
64
+ pin.verify
65
+ end
66
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'test_helper'
2
+ require_relative '../lib/wall_e/pin'
3
+ require_relative '../lib/wall_e/components/piezo'
4
+
5
+ class PiezoTest < MiniTest::Unit::TestCase
6
+
7
+ def test_turning_on
8
+ note = 1136 # 'a'
9
+ pin = MiniTest::Mock.new
10
+ pin.expect(:set_mode, 1, [WallE::Pin::PWM])
11
+ pin.expect(:analog_write, 1, [note])
12
+
13
+ piezo = WallE::Piezo.new(pin)
14
+ piezo.on(note)
15
+
16
+ assert piezo.on?
17
+ pin.verify
18
+ end
19
+
20
+ def test_turning_off
21
+ pin = MiniTest::Mock.new
22
+ pin.expect(:set_mode, 1, [WallE::Pin::PWM])
23
+ pin.expect(:analog_write, 1, [0])
24
+
25
+ piezo = WallE::Piezo.new(pin)
26
+ piezo.off
27
+
28
+ assert piezo.off?
29
+ pin.verify
30
+ end
31
+ end
data/test/pin_test.rb ADDED
@@ -0,0 +1,82 @@
1
+ require_relative 'test_helper'
2
+ require_relative '../lib/wall_e/pin'
3
+ require 'ostruct'
4
+
5
+
6
+ DummyBoard = Class.new do
7
+ attr_reader :pins
8
+
9
+ def initialize(pins)
10
+ @pins = pins
11
+ end
12
+
13
+ def digital_write(*args); args.length; end
14
+ def set_pin_mode(pin_number, mode); pin_number; end
15
+
16
+ end
17
+
18
+ class PinTest < MiniTest::Unit::TestCase
19
+
20
+ def test_will_not_set_to_a_mode_pin_does_not_support
21
+ board_pins = []
22
+ board_pins.insert(13, OpenStruct.new(supported_modes: Array(0..3)))
23
+
24
+ board = DummyBoard.new(board_pins)
25
+
26
+ pin = WallE::Pin.new(13, board)
27
+
28
+ assert_raises(WallE::Pin::UnsupportedModeError) { pin.set_mode WallE::Pin::SERVO }
29
+ end
30
+
31
+ def test_set_mode_will_only_set_changes
32
+ pin_number = 13
33
+ board_pins = []
34
+ board_pins.insert(pin_number, OpenStruct.new(supported_modes: Array(0..4)))
35
+
36
+ board = OpenStruct.new(pins: board_pins, set_pin_mode: 1)
37
+
38
+ pin = nil
39
+
40
+ board.stub :set_pin_mode, 1 do
41
+ pin = WallE::Pin.new(pin_number, board, WallE::Pin::PWM)
42
+ end
43
+
44
+ pin.stub :current_mode, WallE::Pin::PWM do
45
+ pin.set_mode WallE::Pin::PWM
46
+ end
47
+ end
48
+
49
+ def test_digital_write_to_board
50
+ pin_number = 13
51
+ board_pins = []
52
+ board_pins.insert(pin_number, OpenStruct.new(supported_modes: Array(0..3)))
53
+
54
+ board = MiniTest::Mock.new
55
+ board.expect(:pins, board_pins)
56
+ board.expect(:set_pin_mode, 1, [pin_number, WallE::Pin::OUTPUT])
57
+ board.expect(:digital_write, 1, [pin_number, 1])
58
+
59
+ pin = WallE::Pin.new(pin_number, board)
60
+
61
+ pin.digital_write(1)
62
+
63
+ board.verify
64
+ end
65
+
66
+ def test_analog_write_to_board
67
+ pin_number = 13
68
+ board_pins = []
69
+ board_pins.insert(pin_number, OpenStruct.new(supported_modes: Array(0..4)))
70
+
71
+ board = MiniTest::Mock.new
72
+ board.expect(:pins, board_pins)
73
+ board.expect(:set_pin_mode, 1, [pin_number, WallE::Pin::PWM])
74
+ board.expect(:analog_write, 1, [pin_number, 255])
75
+
76
+ pin = WallE::Pin.new(pin_number, board, WallE::Pin::PWM)
77
+
78
+ pin.analog_write(255)
79
+
80
+ board.verify
81
+ end
82
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'test_helper'
2
+
3
+ require_relative '../lib/wall_e/serial_snoop'
4
+
5
+ class Dummy; end
6
+
7
+ class SerialSnoopTest < MiniTest::Unit::TestCase
8
+
9
+ def test_no_port_available
10
+ Dir.stub :[], [] do
11
+ assert_nil WallE::SerialSnoop.locate_ports
12
+ end
13
+ end
14
+
15
+ def test_cannot_connect_to_found_port
16
+ Dir.stub :[], ['tty.usbmodemfd13131'] do
17
+ assert_nil WallE::SerialSnoop.locate_ports
18
+ end
19
+ end
20
+
21
+ def test_connect_to_found_port
22
+ Dir.stub :[], ['tty.usbmodemfd13131'] do
23
+ Firmata::Board.stub :new, Dummy.new do
24
+ refute_nil WallE::SerialSnoop.locate_ports
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,71 @@
1
+ require_relative 'test_helper'
2
+ require_relative '../lib/wall_e/pin'
3
+ require_relative '../lib/wall_e/components/servo'
4
+
5
+ class ServoTest < MiniTest::Unit::TestCase
6
+
7
+ def test_set_pin_mode_to_servo
8
+ pin = MiniTest::Mock.new
9
+ pin.expect(:set_mode, 1, [WallE::Pin::SERVO])
10
+
11
+ servo = WallE::Servo.new(pin)
12
+
13
+ pin.verify
14
+ end
15
+
16
+ def test_move_to
17
+ pin = MiniTest::Mock.new
18
+ pin.expect(:set_mode, 1, [WallE::Pin::SERVO])
19
+ pin.expect(:servo_write, 1, [90])
20
+
21
+ servo = WallE::Servo.new(pin)
22
+ servo.move_to(90)
23
+
24
+ pin.verify
25
+ end
26
+
27
+ def test_move_to_will_not_move_past_bounds
28
+ pin = MiniTest::Mock.new
29
+ pin.expect(:set_mode, 1, [WallE::Pin::SERVO])
30
+
31
+ servo = WallE::Servo.new(pin)
32
+
33
+ assert_raises(WallE::Servo::OutOfBoundsError) { servo.move_to(181) }
34
+ end
35
+
36
+ def test_moving_to_min
37
+ pin = MiniTest::Mock.new
38
+ pin.expect(:set_mode, 1, [WallE::Pin::SERVO])
39
+ pin.expect(:servo_write, 1, [0])
40
+
41
+ servo = WallE::Servo.new(pin)
42
+ servo.min
43
+
44
+ pin.verify
45
+ end
46
+
47
+ def test_moving_to_max
48
+ pin = MiniTest::Mock.new
49
+ pin.expect(:set_mode, 1, [WallE::Pin::SERVO])
50
+ pin.expect(:servo_write, 1, [180])
51
+
52
+ servo = WallE::Servo.new(pin)
53
+ servo.max
54
+
55
+ pin.verify
56
+ end
57
+
58
+ def test_moving_to_center
59
+ def test_moving_to_min
60
+ pin = MiniTest::Mock.new
61
+ pin.expect(:set_mode, 1, [WallE::Pin::SERVO])
62
+ pin.expect(:servo_write, 1, [90])
63
+
64
+ servo = WallE::Servo.new(pin)
65
+ servo.center
66
+
67
+ pin.verify
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,3 @@
1
+ gem 'minitest' # use the gem version of minitest not the version bundled with 1.9.3
2
+ require 'minitest/autorun'
3
+ require 'minitest/pride'
data/wall_e.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/wall_e/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["'Mike Breen'"]
6
+ gem.email = ["hardbap@gmail.com"]
7
+ gem.description = %q{A Firmata based Arduino library}
8
+ gem.summary = %q{}
9
+ gem.homepage = "https://github.com/hardbap/wall_e"
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 = "wall_e"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = WallE::VERSION
17
+
18
+
19
+ gem.add_development_dependency("minitest", "~> 3.3.0")
20
+ gem.add_runtime_dependency("firmata", "~> 0.0.6")
21
+ gem.add_runtime_dependency("event_spitter", "~> 0.5.0")
22
+ gem.add_runtime_dependency("pry", "~> 0.9.10")
23
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wall_e
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ! '''Mike Breen'''
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.3.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: firmata
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.6
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.6
46
+ - !ruby/object:Gem::Dependency
47
+ name: event_spitter
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.5.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.10
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.10
78
+ description: A Firmata based Arduino library
79
+ email:
80
+ - hardbap@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - cookbook/Led/blink.rb
91
+ - cookbook/Led/fade_in.rb
92
+ - cookbook/Led/fade_out.rb
93
+ - cookbook/Led/pulse.rb
94
+ - cookbook/Piezo/morse_code.rb
95
+ - cookbook/Servo/sweep.rb
96
+ - lib/wall_e.rb
97
+ - lib/wall_e/components/led.rb
98
+ - lib/wall_e/components/piezo.rb
99
+ - lib/wall_e/components/servo.rb
100
+ - lib/wall_e/logger.rb
101
+ - lib/wall_e/pin.rb
102
+ - lib/wall_e/serial_snoop.rb
103
+ - lib/wall_e/version.rb
104
+ - test/led_test.rb
105
+ - test/piezo_test.rb
106
+ - test/pin_test.rb
107
+ - test/serial_snoop_test.rb
108
+ - test/servo_test.rb
109
+ - test/test_helper.rb
110
+ - wall_e.gemspec
111
+ homepage: https://github.com/hardbap/wall_e
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: ''
135
+ test_files:
136
+ - test/led_test.rb
137
+ - test/piezo_test.rb
138
+ - test/pin_test.rb
139
+ - test/serial_snoop_test.rb
140
+ - test/servo_test.rb
141
+ - test/test_helper.rb