wall_e 0.0.3 → 0.0.4

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/README.md CHANGED
@@ -1,6 +1,23 @@
1
- # WallE
1
+ # Wall·E
2
2
 
3
- TODO: Write a gem description
3
+ Ruby powered robots.
4
+
5
+ ## Shout-outs
6
+
7
+ Wall·E would not have been possible if not for the awesome goings on in
8
+ the Node community. Big props to:
9
+
10
+ * [Rick Waldron](https://github.com/rwldrn) creator of [johnny-five](https://github.com/rwldrn/johnny-five)
11
+ * [Chris Williams](https://github.com/voodootikigod) creator of [node-serialport](https://github.com/voodootikigod/node-serialport)
12
+ * [Julian Gautier](https://github.com/jgautier) creator [firmata](https://github.com/jgautier/firmata)
13
+
14
+ ## Prerequisites
15
+
16
+ 1. Download and install the [Arduio IDE](http://www.arduino.cc/en/Main/Software) for your OS
17
+ 2. Download and unzip [Firmata 2.2](http://at.or.at/hans/pd/Firmata-2.2.zip)
18
+ 3. Plug in your Arduino via USB
19
+ 4. Open the Arduino IDE, select: File > Open > [Path from step 2] > examples > StandardFirmata
20
+ 5. Click the Upload button
4
21
 
5
22
  ## Installation
6
23
 
@@ -18,7 +35,19 @@ Or install it yourself as:
18
35
 
19
36
  ## Usage
20
37
 
21
- TODO: Write usage instructions here
38
+ require 'wall_e'
39
+
40
+ WallE::Assembler.build do
41
+
42
+ led = Led(3)
43
+ rate = 0.75
44
+
45
+ repeat do
46
+ led.toggle
47
+ delay rate
48
+ end
49
+
50
+ end
22
51
 
23
52
  ## Contributing
24
53
 
@@ -27,3 +56,4 @@ TODO: Write usage instructions here
27
56
  3. Commit your changes (`git commit -am 'Added some feature'`)
28
57
  4. Push to the branch (`git push origin my-new-feature`)
29
58
  5. Create new Pull Request
59
+
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ require 'wall_e'
3
+
4
+ WallE::Assembler.build do
5
+
6
+ button = Button(7)
7
+ led = Led(13) # use the onboard LED on pin 13
8
+
9
+ button.pressed { led.toggle }
10
+
11
+ end
@@ -9,7 +9,7 @@ WallE::Assembler.build do
9
9
  # YMMV especially if you are using a different servo.
10
10
  #
11
11
  # The Hitec HS-322 (http://amzn.com/B0006O3XEA) has a range of 2..180.
12
- servo = Servo(9, range: 15..170)
12
+ servo = Servo(9, range: 2..180)
13
13
  servo.min
14
14
 
15
15
  repeat do
@@ -8,10 +8,10 @@ require 'wall_e/components/led'
8
8
  require 'wall_e/components/servo'
9
9
  require 'wall_e/components/piezo'
10
10
  require 'wall_e/components/claw'
11
+ require 'wall_e/components/button'
11
12
 
12
13
  module WallE
13
14
  class Assembler
14
-
15
15
  attr_reader :board
16
16
 
17
17
  def self.build(&block)
@@ -51,23 +51,49 @@ module WallE
51
51
  # TODO some metaprogramming sauce to reduce the component helper code.
52
52
  def Led(pin_number)
53
53
  pin = Pin.new(pin_number, @board)
54
- Led.new(pin)
54
+ (leds << Led.new(pin)).last
55
55
  end
56
56
 
57
57
  def Servo(pin_number, options = {})
58
58
  pin = Pin.new(pin_number, @board)
59
- Servo.new(pin, options)
59
+ (servos << Servo.new(pin, options)).last
60
60
  end
61
61
 
62
62
  def Piezo(pin_number)
63
63
  pin = Pin.new(pin_number, @board)
64
- Piezo.new(pin)
64
+ (piezos << Piezo.new(pin)).last
65
65
  end
66
66
 
67
67
  def Claw(claw_pin_number, pan_pin_number)
68
68
  claw_servo = Servo(claw_pin_number, range: 60..144)
69
69
  pan_servo = Servo(pan_pin_number)
70
- Claw.new(claw_servo, pan_servo)
70
+ (claws << Claw.new(claw_servo, pan_servo)).last
71
+ end
72
+
73
+ def Button(pin_number)
74
+ pin = Pin.new(pin_number, @board)
75
+ (buttons << Button.new(pin)).last
76
+ end
77
+
78
+ # TODO wrap up these collections in some metaprogramming sauce too.
79
+ def leds
80
+ @leds ||= []
81
+ end
82
+
83
+ def servos
84
+ @servos ||= []
85
+ end
86
+
87
+ def piezos
88
+ @piezos ||= []
89
+ end
90
+
91
+ def claws
92
+ @claws ||= []
93
+ end
94
+
95
+ def buttons
96
+ @buttons ||= []
71
97
  end
72
98
 
73
99
  def delay(seconds)
@@ -79,11 +105,11 @@ module WallE
79
105
  end
80
106
 
81
107
  def resume
82
- @running = true
83
108
  @group.list.each(&:wakeup)
109
+ @running = true
84
110
  end
85
111
 
86
- def turn_off
112
+ def shut_down
87
113
  @group.list.each(&:kill)
88
114
  end
89
115
 
@@ -0,0 +1,61 @@
1
+ module WallE
2
+ class Button
3
+
4
+ # Public: Initialize a Button.
5
+ #
6
+ # pin - the Pin the Button is attached to.
7
+ # hold_time - the Integer seconds to indicate that the button is being held (default: 0.07).
8
+ def initialize(pin, hold_time = 0.7)
9
+ @pin = pin
10
+ @pin.set_mode(Pin::INPUT)
11
+ @pin.start_reporting
12
+ @hold_time = hold_time
13
+ @pin.on("digital-read-#{@pin.number}", method(:handler))
14
+ end
15
+
16
+ # Public: What to do when the button is pressed.
17
+ #
18
+ # block - the block that will run when the button is pressed.
19
+ #
20
+ # Returns nothing.
21
+ def pressed(&block)
22
+ @down_callback = block
23
+ end
24
+
25
+ # Public: What to do when the button is released.
26
+ #
27
+ # block - the block that will run when the button is released.
28
+ #
29
+ # Returns nothing.
30
+ def released(&block)
31
+ @up_callback = block
32
+ end
33
+
34
+ # Public: What to do when the button is held.
35
+ #
36
+ # block - the block that will be run when the button is held.
37
+ #
38
+ # Returns nothing.
39
+ def held(&block)
40
+ @hold_callback = block
41
+ end
42
+
43
+ # Internal: The handler for the Pin's read event.
44
+ #
45
+ # value - the Integer returned from the read event.
46
+ #
47
+ # Returns nothing.
48
+ def handler(value)
49
+ if value.zero?
50
+ @up_callback.call if @up_callback
51
+ else
52
+ @last_press = Time.now
53
+ @down_callback.call if @down_callback
54
+ end
55
+
56
+ if value.zero? && (Time.now - @last_press) > @hold_time
57
+ @hold_callback.call if @hold_callback
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,5 +1,8 @@
1
+ require 'forwardable'
2
+
1
3
  module WallE
2
4
  class Pin
5
+ extend Forwardable
3
6
  class UnsupportedModeError < StandardError; end
4
7
 
5
8
  # Internal: Fixnum byte for pin mode input.
@@ -19,6 +22,8 @@ module WallE
19
22
  # Public: Returns the Integer pin number.
20
23
  attr_reader :number
21
24
 
25
+ def_delegators :@board, :on, :off, :once
26
+
22
27
  # Public: Initialize a Pin
23
28
  #
24
29
  # number - the Integer pin number on the board.
@@ -80,5 +85,9 @@ module WallE
80
85
  def value
81
86
  @onboard_pin.value
82
87
  end
88
+
89
+ def start_reporting
90
+ @board.start_pin_reporting
91
+ end
83
92
  end
84
93
  end
@@ -1,3 +1,3 @@
1
1
  module WallE
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,65 @@
1
+ require_relative 'test_helper'
2
+ require_relative '../lib/wall_e/components/button'
3
+
4
+ class ButtonTest < MiniTest::Unit::TestCase
5
+ DummyPin = Class.new do
6
+ def set_mode(mode); end
7
+ def start_reporting; end
8
+ def number; 7; end
9
+ def on(*args); end
10
+ end
11
+
12
+ def test_sets_up_pin
13
+ pin = MiniTest::Mock.new
14
+ pin.expect(:set_mode, 1, [WallE::Pin::INPUT])
15
+ pin.expect(:start_reporting, 1)
16
+ pin.expect(:number, 7)
17
+ pin.expect(:on, 1, ['digital-read-7', Method])
18
+
19
+ WallE::Button.new(pin)
20
+
21
+ pin.verify
22
+ end
23
+
24
+ def test_pressed
25
+ blk = MiniTest::Mock.new
26
+ blk.expect(:run, nil)
27
+
28
+ button = WallE::Button.new(DummyPin.new)
29
+ button.pressed { blk.run }
30
+
31
+ button.handler(1)
32
+
33
+ blk.verify
34
+ end
35
+
36
+ def test_released
37
+ blk = MiniTest::Mock.new
38
+ blk.expect(:run, nil)
39
+
40
+ button = WallE::Button.new(DummyPin.new)
41
+ button.released { blk.run }
42
+
43
+ button.pressed { }
44
+ button.handler(1)
45
+
46
+ button.handler(0)
47
+
48
+ blk.verify
49
+ end
50
+
51
+ def test_held
52
+ blk = MiniTest::Mock.new
53
+ blk.expect(:run, nil)
54
+
55
+ button = WallE::Button.new(DummyPin.new, 0.2)
56
+ button.held { blk.run }
57
+ button.released { }
58
+ button.pressed { }
59
+ button.handler(1)
60
+ sleep 0.2
61
+ button.handler(0)
62
+
63
+ blk.verify
64
+ end
65
+ end
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
 
18
18
 
19
19
  gem.add_development_dependency("minitest", "~> 3.3.0")
20
- gem.add_runtime_dependency("firmata", "~> 0.0.6")
20
+ gem.add_runtime_dependency("firmata", "~> 0.0.7")
21
21
  gem.add_runtime_dependency("event_spitter", "~> 0.5.0")
22
22
  gem.add_runtime_dependency("pry", "~> 0.9.10")
23
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wall_e
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-17 00:00:00.000000000 Z
12
+ date: 2012-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 0.0.6
37
+ version: 0.0.7
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 0.0.6
45
+ version: 0.0.7
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: event_spitter
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +87,7 @@ files:
87
87
  - LICENSE
88
88
  - README.md
89
89
  - Rakefile
90
+ - cookbook/Button/toggle_led.rb
90
91
  - cookbook/Led/blink.rb
91
92
  - cookbook/Led/fade_in.rb
92
93
  - cookbook/Led/fade_out.rb
@@ -94,6 +95,7 @@ files:
94
95
  - cookbook/Piezo/morse_code.rb
95
96
  - cookbook/Servo/sweep.rb
96
97
  - lib/wall_e.rb
98
+ - lib/wall_e/components/button.rb
97
99
  - lib/wall_e/components/claw.rb
98
100
  - lib/wall_e/components/led.rb
99
101
  - lib/wall_e/components/piezo.rb
@@ -102,6 +104,7 @@ files:
102
104
  - lib/wall_e/pin.rb
103
105
  - lib/wall_e/serial_snoop.rb
104
106
  - lib/wall_e/version.rb
107
+ - test/button_test.rb
105
108
  - test/claw_test.rb
106
109
  - test/led_test.rb
107
110
  - test/piezo_test.rb
@@ -135,6 +138,7 @@ signing_key:
135
138
  specification_version: 3
136
139
  summary: ''
137
140
  test_files:
141
+ - test/button_test.rb
138
142
  - test/claw_test.rb
139
143
  - test/led_test.rb
140
144
  - test/piezo_test.rb