artoo-keyboard 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f41477fdff80e45257fb74795bec70cd8197556
4
+ data.tar.gz: 4776bd4442a24100f51a8576eafaaf542b25bcf2
5
+ SHA512:
6
+ metadata.gz: 5546d761ba4cf5219626e6f73467d0d89fbeffaa2a9626b51897494910ea58714fb139bcd0e0255d4fd05504392e8e24deb0f8c587028769604137224f9ac7a4
7
+ data.tar.gz: 1a801fe44686b793f2d2347c110e4fc8410d35495f0b8c4ea599aaf3fb6d60056f9cfeeb6c200ad313287f597aaf2010620dd09c2766377d7e53f75fb7c0c9fc
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in artoo-keyboard.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2014 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.
@@ -0,0 +1,55 @@
1
+ # Artoo Adaptor For Keyboard Input
2
+
3
+ This repository contains the Artoo (http://artoo.io/) adaptor for keyboard input.
4
+
5
+ Artoo is a open source micro-framework for robotics using Ruby.
6
+
7
+ For more information about Artoo, check out our repo at https://github.com/hybridgroup/artoo
8
+
9
+ ## Installing
10
+
11
+ ```
12
+ gem install artoo-keyboard
13
+ ```
14
+
15
+ ## Using
16
+
17
+ ```ruby
18
+ require 'artoo'
19
+
20
+ connection :keyboard, adaptor: :keyboard
21
+ device :keyboard, driver: :keyboard, connection: :keyboard
22
+
23
+ work do
24
+ on keyboard, :key => :keypress
25
+ end
26
+
27
+ def keypress(sender, key)
28
+ puts key
29
+ end
30
+ ```
31
+
32
+ ## Documentation
33
+
34
+ Check out our [documentation](http://artoo.io/documentation/) for lots of information about how to use Artoo.
35
+
36
+ ## IRC
37
+
38
+ Need more help? Just want to say "Hello"? Come visit us on IRC freenode #artoo
39
+
40
+ ## Contributing
41
+
42
+ * All patches must be provided under the Apache 2.0 License
43
+ * Please use the -s option in git to "sign off" that the commit is your work and you are providing it under the Apache 2.0 License
44
+ * Submit a Github Pull Request to the appropriate branch and ideally discuss the changes with us in IRC.
45
+ * We will look at the patch, test it out, and give you feedback.
46
+ * Avoid doing minor whitespace changes, renamings, etc. along with merged content. These will be done by the maintainers from time to time but they can complicate merges and should be done seperately.
47
+ * Take care to maintain the existing coding style.
48
+ * Add unit tests for any new or changed functionality.
49
+ * All pull requests should be "fast forward"
50
+ * If there are commits after yours use “git rebase -i <new_head_branch>”
51
+ * If you have local changes you may need to use “git stash”
52
+ * For git help see [progit](http://git-scm.com/book) which is an awesome (and free) book on git
53
+
54
+
55
+ (c) 2012-2014 The Hybrid Group
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "artoo-keyboard/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "artoo-keyboard"
7
+ s.version = Artoo::Keyboard::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Andrew Stewart", "Ron Evans"]
10
+ s.email = ["artoo@hybridgroup.com"]
11
+ s.homepage = "https://github.com/hybridgroup/artoo-keyboard"
12
+ s.summary = %q{Artoo adaptor for keyboard input}
13
+ s.description = %q{Artoo adaptor for keyboard input}
14
+
15
+ s.rubyforge_project = "artoo-keyboard"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_runtime_dependency 'artoo', '>= 1.6.0'
23
+ s.add_development_dependency 'minitest', '>= 5.0'
24
+ s.add_development_dependency 'minitest-happy'
25
+ s.add_development_dependency 'mocha', '>= 1.0.0'
26
+ end
@@ -0,0 +1 @@
1
+ # Commands
@@ -0,0 +1,11 @@
1
+ # Events
2
+
3
+ ## start_driver
4
+
5
+ Start driver and any required connections.
6
+
7
+ ## key
8
+
9
+ Gets triggered when we press a key on the keyboard.
10
+
11
+ - **keys supported** - up arrow, down arrow, left arrow, right arrow, esc and any alphanumeric characters.
@@ -0,0 +1,14 @@
1
+ require 'artoo'
2
+
3
+ connection :keyboard, adaptor: :keyboard
4
+ device :keyboard, driver: :keyboard, connection: :keyboard
5
+
6
+ work do
7
+ puts "working now..."
8
+
9
+ on keyboard, :key => :keypress
10
+ end
11
+
12
+ def keypress(sender, key)
13
+ puts key
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'lib/artoo/adaptors/keyboard'
2
+ require 'lib/artoo/drivers/keyboard'
3
+ require 'lib/artoo-keyboard/version'
@@ -0,0 +1,5 @@
1
+ module Artoo
2
+ module Keyboard
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,133 @@
1
+ require 'artoo/adaptors/adaptor'
2
+
3
+ module Artoo
4
+ module Adaptors
5
+ # Connect to a keyboard device
6
+ # @see device documentation for more information
7
+ class Keyboard < Adaptor
8
+ KEY_CTRL_C = ?\C-c
9
+ KEY_ESCAPE = 0x1B
10
+ KEY_ARROW_UP = 0x41
11
+ KEY_ARROW_DOWN = 0x42
12
+ KEY_ARROW_RIGHT = 0x43
13
+ KEY_ARROW_LEFT = 0x44
14
+
15
+ attr_reader :tty, :chars, :buffer
16
+
17
+ # Creates a connection with device
18
+ # @return [Boolean]
19
+ def connect
20
+ @tty = TTY.new
21
+ @tty.configure
22
+
23
+ @chars = Queue.new
24
+ @buffer = []
25
+
26
+ super
27
+ end
28
+
29
+ # Closes connection with device
30
+ # @return [Boolean]
31
+ def disconnect
32
+ tty.restore
33
+ tty.puts
34
+
35
+ super
36
+ end
37
+
38
+ # Name of device
39
+ # @return [String]
40
+ def name
41
+ "keyboard"
42
+ end
43
+
44
+ # Version of device
45
+ # @return [String]
46
+ def version
47
+ Artoo::Keyboard::VERSION
48
+ end
49
+
50
+ def get_char
51
+ parse_char tty.get_char
52
+ chars.pop
53
+ end
54
+
55
+ private
56
+
57
+ def quit
58
+ puts "QUITTING"
59
+ tty.restore
60
+ Artoo::Master.stop_work
61
+ exit(0)
62
+ end
63
+
64
+ def parse_char(char)
65
+ case char
66
+ when KEY_CTRL_C then quit
67
+ when " " then chars.push("space")
68
+ when KEY_ESCAPE.chr then
69
+ next_char = tty.get_char
70
+ if next_char
71
+ next_char = tty.get_char
72
+ case next_char
73
+ when KEY_ARROW_UP.chr then chars.push('up')
74
+ when KEY_ARROW_DOWN.chr then chars.push('down')
75
+ when KEY_ARROW_RIGHT.chr then chars.push('right')
76
+ when KEY_ARROW_LEFT.chr then chars.push('left')
77
+ end
78
+ else
79
+ chars.push(char) # just escape
80
+ end
81
+ when /[[:print:]]/ then chars.push(char)
82
+ else chars.push(nil) # unknown or non-qwerty char
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ class TTY
90
+ attr_reader :original_stty_state, :in_file, :out_file
91
+
92
+ def initialize
93
+ @in_file = File.open("/dev/tty", "r")
94
+ @out_file = File.open("/dev/tty", "w")
95
+ end
96
+
97
+ def get_char
98
+ in_file.getc
99
+ end
100
+
101
+ def puts
102
+ out_file.puts
103
+ end
104
+
105
+ def stty(args)
106
+ command("stty #{args}")
107
+ end
108
+
109
+ def configure
110
+ @original_stty_state = stty "-g"
111
+ # raw: Disable input and output processing
112
+ # -echo: Don't echo keys back
113
+ # cbreak: Set up lots of standard stuff, including INTR signal on ^C
114
+ stty "raw -echo cbreak"
115
+ end
116
+
117
+ def restore
118
+ stty "#{@original_stty_state}"
119
+ end
120
+
121
+ private
122
+
123
+ # Run a command with the TTY as stdin, capturing the output via a pipe
124
+ def command(command)
125
+ IO.pipe do |read_io, write_io|
126
+ pid = Process.spawn(command, :in => "/dev/tty", :out => write_io)
127
+ Process.wait(pid)
128
+ raise "Command failed: #{command.inspect}" unless $?.success?
129
+ write_io.close
130
+ read_io.read
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,21 @@
1
+ require 'artoo/drivers/driver'
2
+
3
+ module Artoo
4
+ module Drivers
5
+ # The keyboard driver behaviors
6
+ class Keyboard < Driver
7
+
8
+ # Public: Start driver and any required connections.
9
+ #
10
+ # Returns null.
11
+ def start_driver
12
+ every(0.05) do
13
+ key = connection.get_char # get the next char if any...
14
+ publish(event_topic_name("key"), key) if key
15
+ end
16
+
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,95 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/adaptors/keyboard'
3
+
4
+ class MockTTY
5
+ attr_reader :chars
6
+
7
+ def initialize
8
+ @chars = []
9
+ end
10
+
11
+ def get_char
12
+ chars.pop
13
+ end
14
+
15
+ # no-op methods
16
+ def configure ; end
17
+ def restore ; end
18
+ def puts ; end
19
+ end
20
+
21
+ describe Artoo::Adaptors::Keyboard do
22
+ before do
23
+ @adaptor = Artoo::Adaptors::Keyboard.new
24
+ @tty = MockTTY.new
25
+ TTY.stubs(:new).returns(@tty)
26
+ end
27
+
28
+ describe "#name" do
29
+ it "identifies as 'keyboard'" do
30
+ @adaptor.name.must_equal 'keyboard'
31
+ end
32
+ end
33
+
34
+ describe "#connect" do
35
+ before do
36
+ @tty.expects(:configure)
37
+ @adaptor.connect
38
+ end
39
+
40
+ it "sets up a TTY" do
41
+ @adaptor.tty.must_equal @tty
42
+ end
43
+
44
+ it 'creates a queue' do
45
+ @adaptor.chars.must_be_instance_of Thread::Queue
46
+ end
47
+
48
+ it 'creates a buffer array' do
49
+ @buffer = []
50
+ end
51
+ end
52
+
53
+ describe "#get_char" do
54
+ before do
55
+ @adaptor.connect
56
+ end
57
+
58
+ it "parses chars from the TTY" do
59
+ @tty.chars << "a"
60
+ @adaptor.get_char.must_equal "a"
61
+ end
62
+
63
+ it "catches the spacebar" do
64
+ @tty.chars << " "
65
+ @adaptor.get_char.must_equal "space"
66
+ end
67
+
68
+ it "passes through escape" do
69
+ @tty.chars << "\e"
70
+ @adaptor.get_char.must_equal "\e"
71
+ end
72
+
73
+ describe "arrow keys" do
74
+ it "recognizes the up arrow key" do
75
+ ["A", "[", "\e"].each { |chr| @tty.chars << chr }
76
+ @adaptor.get_char.must_equal "up"
77
+ end
78
+
79
+ it "recognizes the down arrow key" do
80
+ ["B", "[", "\e"].each { |chr| @tty.chars << chr }
81
+ @adaptor.get_char.must_equal "down"
82
+ end
83
+
84
+ it "recognizes the left arrow key" do
85
+ ["D", "[", "\e"].each { |chr| @tty.chars << chr }
86
+ @adaptor.get_char.must_equal "left"
87
+ end
88
+
89
+ it "recognizes the right arrow key" do
90
+ ["C", "[", "\e"].each { |chr| @tty.chars << chr }
91
+ @adaptor.get_char.must_equal "right"
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'artoo/drivers/keyboard'
3
+
4
+ # TODO: test for your driver goes here...
@@ -0,0 +1,10 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/mini_test'
3
+ require 'artoo/robot'
4
+
5
+ Celluloid.logger = nil
6
+
7
+ MiniTest::Spec.before do
8
+ Celluloid.shutdown
9
+ Celluloid.boot
10
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artoo-keyboard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Stewart
8
+ - Ron Evans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: artoo
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.6.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.6.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '5.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '5.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest-happy
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: mocha
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.0.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ description: Artoo adaptor for keyboard input
71
+ email:
72
+ - artoo@hybridgroup.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - artoo-keyboard.gemspec
83
+ - docs/commands.md
84
+ - docs/events.md
85
+ - examples/keyboard.rb
86
+ - lib/artoo-keyboard.rb
87
+ - lib/artoo-keyboard/version.rb
88
+ - lib/artoo/adaptors/keyboard.rb
89
+ - lib/artoo/drivers/keyboard.rb
90
+ - test/adaptors/keyboard_adaptor_test.rb
91
+ - test/drivers/keyboard_driver_test.rb
92
+ - test/test_helper.rb
93
+ homepage: https://github.com/hybridgroup/artoo-keyboard
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project: artoo-keyboard
112
+ rubygems_version: 2.2.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Artoo adaptor for keyboard input
116
+ test_files: []