xplane 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.
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ pkg
3
+ *~
4
+ #*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'rspec'
5
+ end
6
+
7
+ # Specify your gem's dependencies in xplane.gemspec
8
+ gemspec
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ xplane (0.0.1)
5
+ eventmachine
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ eventmachine (1.0.3)
12
+ rake (10.1.0)
13
+ rspec (2.13.0)
14
+ rspec-core (~> 2.13.0)
15
+ rspec-expectations (~> 2.13.0)
16
+ rspec-mocks (~> 2.13.0)
17
+ rspec-core (2.13.1)
18
+ rspec-expectations (2.13.0)
19
+ diff-lcs (>= 1.1.3, < 2.0)
20
+ rspec-mocks (2.13.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.3)
27
+ rake
28
+ rspec
29
+ xplane!
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eloy Gomez
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.
@@ -0,0 +1,23 @@
1
+ * Xplane Ruby
2
+ ** Usage
3
+ #+BEGIN_SRC ruby
4
+ #!/usr/bin/env ruby
5
+
6
+ require 'xplane'
7
+
8
+ Xplane.config do |c|
9
+ c.xplane_host = "10.254.0.110" # default "127.0.0.1"
10
+ c.xplane_port = 49000 # default 49000
11
+ c.listen_port = 49000 # default 49001
12
+ c.listen_address = "0.0.0.0" # default "127.0.0.1"
13
+ end
14
+
15
+ Xplane.on_data_received do |msg|
16
+ puts "Pitch: #{msg.pitch}\t Roll: #{msg.roll}\t Heading: #{msg.heading}\tAlt: #{msg.alt}"
17
+ msg.send throttle_cmd_1: 1.0, throttle_cmd_2: 1.0, throttle_cmd_3: 1.0, throttle_cmd_4: 1.0
18
+ end
19
+ #+END_SRC
20
+
21
+ * Contribute
22
+ At this moment lib/xplane/data_mapper.rb is very incomplete. Pull
23
+ requests with new elements will be welcome.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ require 'socket'
2
+ require 'eventmachine'
3
+ require 'xplane/configuration'
4
+ require 'xplane/data_mapper'
5
+ require 'xplane/message_data_formatter'
6
+ require 'xplane/message'
7
+ require 'xplane/connection'
8
+ require "xplane/version"
9
+
10
+ module Xplane
11
+
12
+ def self.on_data_received(&block)
13
+ listen_address = Xplane.config.listen_address
14
+ port = Xplane.config.listen_port
15
+ EM.run do
16
+ EM.open_datagram_socket(listen_address, port, XplaneConnection, block)
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,18 @@
1
+ module Xplane
2
+ class Configuration
3
+ attr_accessor :xplane_host, :xplane_port, :listen_address, :listen_port
4
+ def initialize
5
+ xplane_host = "127.0.0.1"
6
+ xplane_port = 49000
7
+ listen_port = 49001
8
+ listen_address = "127.0.0.1"
9
+ end
10
+ end
11
+ @@config ||= Configuration.new
12
+
13
+ def self.config(&block)
14
+ block.call @@config if block
15
+ return @@config
16
+ end
17
+
18
+ end
@@ -0,0 +1,14 @@
1
+ module Xplane
2
+
3
+ class XplaneConnection < EventMachine::Connection
4
+ def initialize(block)
5
+ @block = block
6
+ end
7
+
8
+ def receive_data(binary_data)
9
+ message = Message.new self, binary_data
10
+ @block.call message
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,8 @@
1
+ module Xplane
2
+ XPLANE_DATA_MAPPER = {
3
+ 17 => [:pitch, :roll, :heading, :magnetic_heading],
4
+ 20 => [:lattitude, :longitude, :alt, :alt_agl],
5
+ 25 => [:throttle_cmd_1, :throttle_cmd_2, :throttle_cmd_3, :throttle_cmd_4 ],
6
+ 26 => [:throttle_1, :throttle_2, :throttle_3, :throttle_4 ]
7
+ }
8
+ end
@@ -0,0 +1,27 @@
1
+ class Xplane::Message
2
+ include Xplane::MessageDataFormater
3
+
4
+ def initialize(connection, binary_data = false)
5
+ @conn = connection
6
+ @data = parse_binary_data(binary_data) if binary_data
7
+ end
8
+
9
+ # Send data back to Xplane
10
+ def send(data={})
11
+ binary_data = build_binary_data data
12
+ @conn.send_datagram binary_data, Xplane.config.xplane_host, Xplane.config.xplane_port
13
+ end
14
+
15
+
16
+ # Define getters
17
+ #----------------------------------------------------------------------
18
+
19
+ Xplane::XPLANE_DATA_MAPPER.values.each do |data_element|
20
+ data_element.each do |key|
21
+ define_method(key) do
22
+ @data[key]
23
+ end
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,74 @@
1
+ module Xplane::MessageDataFormater
2
+
3
+ # Binary data formats
4
+ #----------------------------------------------------------------------
5
+
6
+ HEADER_FORMAT = "CCCCC"
7
+ DATA_FORMAT = "iffffffff"
8
+
9
+ DATA_ELEMENTS = 8
10
+
11
+
12
+ # Read and parse data received from XPlane
13
+ #----------------------------------------------------------------------
14
+
15
+ def parse_binary_data(binary_data)
16
+ chunk_size = DATA_ELEMENTS + 1 # plus header
17
+ data_elements = binary_data.length / DATA_FORMAT.length
18
+ data_format = binary_format data_elements
19
+ unpacked = binary_data.unpack(data_format)
20
+ buffer = unpacked[HEADER_FORMAT.length..-1]
21
+ data = {}
22
+ data_elements.times do |element_index|
23
+ offset = element_index * chunk_size
24
+ chunk = buffer[offset..offset + chunk_size]
25
+ element_id = chunk[0]
26
+ map = Xplane::XPLANE_DATA_MAPPER[element_id]
27
+ if map
28
+ map.each_with_index do |key, i|
29
+ data[key] = chunk[i + 1] # plus header
30
+ end
31
+ end
32
+ end
33
+ return data
34
+ end
35
+
36
+ # Create a DATA package with the given data
37
+ def build_binary_data(data={})
38
+ buffer = [68, 65, 84, 65, 0] # D A T A
39
+ elements_to_send = []
40
+ keys_to_send = data.keys
41
+
42
+ Xplane::XPLANE_DATA_MAPPER.each do |element_id, included_elements|
43
+ element = nil
44
+ included_elements.each_with_index do |element_name, index|
45
+ if keys_to_send.include? element_name
46
+ element ||= new_element(element_id)
47
+ element[index + 1] = data[element_name]
48
+ end
49
+ end
50
+ elements_to_send.push element unless element.nil?
51
+ end
52
+
53
+ elements_to_send.each do |element|
54
+ buffer += element
55
+ end
56
+ buffer.pack binary_format(elements_to_send.length)
57
+ end
58
+
59
+ private
60
+
61
+ def binary_format(elements=1)
62
+ HEADER_FORMAT + DATA_FORMAT * elements
63
+ end
64
+
65
+ # Build an empty element
66
+ def new_element(element_id)
67
+ e = [element_id]
68
+ DATA_ELEMENTS.times do
69
+ e.push -999 # Ignore
70
+ end
71
+ return e
72
+ end
73
+
74
+ end
@@ -0,0 +1,3 @@
1
+ module Xplane
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+
9
+ require 'xplane'
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ class Formatter
4
+ include Xplane::MessageDataFormater
5
+ end
6
+
7
+ describe Xplane::MessageDataFormater do
8
+ let(:formatter) { Formatter.new }
9
+
10
+ describe 'MessageDataFormater#build_binary_data' do
11
+ it 'should create a binary message from the given binary data' do
12
+ binary_data = formatter.build_binary_data(throttle_cmd_1: 1.1)
13
+ data = formatter.parse_binary_data(binary_data)
14
+ expect(data[:throttle_cmd_1].round(1)).to eq 1.1
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xplane/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xplane"
8
+ spec.version = Xplane::VERSION
9
+ spec.authors = ["Eloy Gomez"]
10
+ spec.email = ["eloy@indeos.es"]
11
+ spec.description = "Gem for communicate with X-Plane via UDP packages"
12
+ spec.summary = "EventMachine based library for send and receive data from X-Plane Flight Simulator with ruby."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency 'eventmachine'
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xplane
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eloy Gomez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: eventmachine
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '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'
62
+ description: Gem for communicate with X-Plane via UDP packages
63
+ email:
64
+ - eloy@indeos.es
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.org
75
+ - Rakefile
76
+ - lib/xplane.rb
77
+ - lib/xplane/configuration.rb
78
+ - lib/xplane/connection.rb
79
+ - lib/xplane/data_mapper.rb
80
+ - lib/xplane/message.rb
81
+ - lib/xplane/message_data_formatter.rb
82
+ - lib/xplane/version.rb
83
+ - spec/spec_helper.rb
84
+ - spec/unit/message_data_formatter_spec.rb
85
+ - xplane.gemspec
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.23
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: EventMachine based library for send and receive data from X-Plane Flight
111
+ Simulator with ruby.
112
+ test_files:
113
+ - spec/spec_helper.rb
114
+ - spec/unit/message_data_formatter_spec.rb
115
+ has_rdoc: