arduino_firmata 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *#*
2
+ *~
3
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'hoe'
4
+ gem 'newgem'
5
+ gem 'serialport'
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ RedCloth (4.2.9)
5
+ activesupport (2.3.14)
6
+ hoe (3.1.0)
7
+ rake (~> 0.8)
8
+ newgem (1.5.3)
9
+ RedCloth (>= 4.1.1)
10
+ activesupport (~> 2.3.4)
11
+ hoe (>= 2.4.0)
12
+ rubigen (>= 1.5.3)
13
+ syntax (>= 1.0.0)
14
+ rake (0.9.2.2)
15
+ rubigen (1.5.8)
16
+ activesupport (>= 2.3.5, < 3.2.0)
17
+ serialport (1.1.0)
18
+ syntax (1.0.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ hoe
25
+ newgem
26
+ serialport
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 0.0.1 2012-10-24
2
+
3
+ * digital/analog read/write
data/Manifest.txt ADDED
@@ -0,0 +1,20 @@
1
+ .gitignore
2
+ Gemfile
3
+ Gemfile.lock
4
+ History.txt
5
+ Manifest.txt
6
+ README.rdoc
7
+ Rakefile
8
+ lib/arduino_firmata.rb
9
+ lib/arduino_firmata/const.rb
10
+ lib/arduino_firmata/main.rb
11
+ samples/analog_read_write.rb
12
+ samples/digital_read.rb
13
+ samples/led_blink.rb
14
+ samples/led_fade.rb
15
+ samples/tweet_temperature.rb
16
+ script/console
17
+ script/destroy
18
+ script/generate
19
+ test/test_arduino_firmata.rb
20
+ test/test_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,83 @@
1
+ = arduino_firmata
2
+
3
+ * http://github.com/shokai/arduino-firmata-ruby
4
+
5
+ == INSTALL:
6
+
7
+ * gem install arduino_firmata
8
+
9
+ == DESCRIPTION:
10
+
11
+ Arduino Firmata protocol (http://firmata.org) implementation on Ruby.
12
+
13
+ == SYNOPSIS:
14
+
15
+ arduino = ArduinoFirmata::Arduino.new
16
+
17
+ or
18
+
19
+ arduino = ArduinoFirmata::Arduino.new '/dev/tty.usb-device-name'
20
+
21
+ Board Version
22
+
23
+ puts "firmata version #{arduino.version}"
24
+
25
+
26
+ Digital Write
27
+
28
+ arduino.digital_write 13, true
29
+ arduino.digital_write 13, false
30
+
31
+
32
+ Digital Read
33
+
34
+ arduino.pin_mode 7, ArduinoFirmata::INPUT
35
+ puts arduino.digital_read 7 # => true/false
36
+
37
+
38
+ Analog Write (PWM)
39
+
40
+ 0.upto(255) do |i|
41
+ arduino.analog_write 11, i
42
+ sleep 0.01
43
+ end
44
+
45
+ Analog Read
46
+
47
+ puts arduino.analog_read 0 # => 0 ~ 1023
48
+
49
+ see samples https://github.com/shokai/arduino-firmata-ruby/tree/gem/samples
50
+
51
+ == REQUIREMENTS:
52
+
53
+ * Arduino Standard Firmata v2.2
54
+ Arduino IDE -> [File] -> [Examples] -> [Firmata] -> [StandardFirmata]
55
+
56
+ * Ruby 1.8.7+
57
+ * Ruby 1.9.2+
58
+
59
+
60
+ == LICENSE:
61
+
62
+ (The MIT License)
63
+
64
+ Copyright (c) 2012 Sho Hashimoto
65
+
66
+ Permission is hereby granted, free of charge, to any person obtaining
67
+ a copy of this software and associated documentation files (the
68
+ 'Software'), to deal in the Software without restriction, including
69
+ without limitation the rights to use, copy, modify, merge, publish,
70
+ distribute, sublicense, and/or sell copies of the Software, and to
71
+ permit persons to whom the Software is furnished to do so, subject to
72
+ the following conditions:
73
+
74
+ The above copyright notice and this permission notice shall be
75
+ included in all copies or substantial portions of the Software.
76
+
77
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
78
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
79
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
80
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
81
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
82
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
83
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/arduino_firmata'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'arduino_firmata' do
14
+ self.developer 'Sho Hashimoto', 'hashimoto@shokai.org'
15
+ #self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ self.extra_deps = [['serialport','>= 1.1.0', '< 2.0.0']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'arduino_firmata/const'
5
+ require 'arduino_firmata/main'
6
+
7
+ module ArduinoFirmata
8
+ VERSION = '0.0.1'
9
+ end
@@ -0,0 +1,28 @@
1
+ module ArduinoFirmata
2
+
3
+ def self.list
4
+ Dir.entries('/dev').grep(/tty\.usb/).map{|fname| "/dev/#{fname}"}
5
+ end
6
+
7
+ INPUT = 0
8
+ OUTPUT = 1
9
+ ANALOG = 2
10
+ PWM = 3
11
+ SERVO = 4
12
+ SHIFT = 5
13
+ I2C = 6
14
+ LOW = 0
15
+ HIGH = 1
16
+
17
+ MAX_DATA_BYTES = 32
18
+ DIGITAL_MESSAGE = 0x90 # send data for a digital port
19
+ ANALOG_MESSAGE = 0xE0 # send data for an analog pin (or PWM)
20
+ REPORT_ANALOG = 0xC0 # enable analog input by pin
21
+ REPORT_DIGITAL = 0xD0 # enable digital input by port
22
+ SET_PIN_MODE = 0xF4 # set a pin to INPUT/OUTPUT/PWM/etc
23
+ REPORT_VERSION = 0xF9 # report firmware version
24
+ SYSTEM_RESET = 0xFF # reset from MIDI
25
+ START_SYSEX = 0xF0 # start a MIDI SysEx message
26
+ END_SYSEX = 0xF7 # end a MIDI SysEx message
27
+
28
+ end
@@ -0,0 +1,130 @@
1
+ require 'serialport'
2
+ require 'stringio'
3
+
4
+ module ArduinoFirmata
5
+
6
+ class Arduino
7
+
8
+ attr_reader :version
9
+
10
+ def initialize(serial_name=nil, bps=57600)
11
+ serial_name = ArduinoFirmata.list[0] unless serial_name
12
+ @wait_for_data = 0
13
+ @execute_multi_byte_command = 0
14
+ @multi_byte_channel = 0
15
+ @stored_input_data = []
16
+ @parsing_sysex = false
17
+ @sysex_bytes_read
18
+
19
+ @digital_output_data = Array.new(16, 0)
20
+ @digital_input_data = Array.new(16, 0)
21
+ @analog_input_data = Array.new(16, 0)
22
+
23
+ @version
24
+
25
+ @serial = SerialPort.new(serial_name, bps, 8, 1, 0)
26
+ sleep 3
27
+
28
+ Thread.new{
29
+ loop do
30
+ process_input
31
+ sleep 0.1
32
+ end
33
+ }.run
34
+
35
+ (0...6).each do |i|
36
+ write(REPORT_ANALOG | i)
37
+ write 1
38
+ end
39
+ (0...2).each do |i|
40
+ write(REPORT_DIGITAL | i)
41
+ write 1
42
+ end
43
+
44
+ end
45
+
46
+ def digital_read(pin)
47
+ (@digital_input_data[pin >> 3] >> (pin & 0x07)) & 0x01 > 0
48
+ end
49
+
50
+ def analog_read(pin)
51
+ @analog_input_data[pin]
52
+ end
53
+
54
+ def pin_mode(pin, mode)
55
+ write SET_PIN_MODE
56
+ write pin
57
+ write mode
58
+ end
59
+
60
+ def digital_write(pin, value)
61
+ port_num = (pin >> 3) & 0x0F
62
+ if value == 0 or value == true
63
+ @digital_output_data[port_num] &= ~(1 << (pin & 0x07))
64
+ else
65
+ @digital_output_data[port_num] |= (1 << (pin & 0x07))
66
+ end
67
+
68
+ write(DIGITAL_MESSAGE | port_num)
69
+ write(@digital_output_data[port_num] & 0x7F)
70
+ write(@digital_output_data[port_num] >> 7)
71
+ end
72
+
73
+ def analog_write(pin, value)
74
+ pin_mode pin, PWM
75
+ write(ANALOG_MESSAGE | (pin & 0x0F))
76
+ write(value & 0x7F)
77
+ write(value >> 7)
78
+ end
79
+
80
+ private
81
+ def write(cmd)
82
+ @serial.write_nonblock cmd.chr
83
+ end
84
+
85
+ def read
86
+ @serial.read_nonblock 9600 rescue EOFError
87
+ end
88
+
89
+ def process_input
90
+ StringIO.new(String read).bytes.each do |input_data|
91
+ command = nil
92
+
93
+ if @parsing_sysex
94
+ if input_data == END_SYSEX
95
+ @parsing_sysex = FALSE
96
+ else
97
+ @stored_input_data[@sysex_bytes_read] = input_data
98
+ @sysex_bytes_read += 1
99
+ end
100
+ elsif @wait_for_data > 0 and input_data < 128
101
+ @wait_for_data -= 1
102
+ @stored_input_data[@wait_for_data] = input_data
103
+ if @execute_multi_byte_command != 0 and @wait_for_data == 0
104
+ case @execute_multi_byte_command
105
+ when DIGITAL_MESSAGE
106
+ @digital_input_data[@multi_byte_channel] = (@stored_input_data[0] << 7) + @stored_input_data[1]
107
+ when ANALOG_MESSAGE
108
+ @analog_input_data[@multi_byte_channel] = (@stored_input_data[0] << 7) + @stored_input_data[1]
109
+ when REPORT_VERSION
110
+ @version = "#{@stored_input_data[1]}.#{@stored_input_data[0]}"
111
+ end
112
+ end
113
+ else
114
+ if input_data < 0xF0
115
+ command = input_data & 0xF0
116
+ @multi_byte_channel = input_data & 0x0F
117
+ else
118
+ command = input_data
119
+ end
120
+ if [DIGITAL_MESSAGE, ANALOG_MESSAGE, REPORT_VERSION].include? command
121
+ @wait_for_data = 2
122
+ @execute_multi_byte_command = command
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,13 @@
1
+ #usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'arduino_firmata'
5
+
6
+ arduino = ArduinoFirmata::Arduino.new ARGV.shift
7
+
8
+ loop do
9
+ an = arduino.analog_read 0
10
+ puts an
11
+ arduino.analog_write 9, an/4
12
+ sleep 0.1
13
+ end
@@ -0,0 +1,19 @@
1
+ #usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'arduino_firmata'
5
+
6
+ arduino = ArduinoFirmata::Arduino.new ARGV.shift
7
+
8
+ arduino.pin_mode 7, ArduinoFirmata::INPUT
9
+
10
+ loop do
11
+ if arduino.digital_read 7
12
+ puts "on"
13
+ arduino.digital_write 13, true
14
+ else
15
+ puts "off"
16
+ arduino.digital_write 13, false
17
+ end
18
+ sleep 0.1
19
+ end
@@ -0,0 +1,16 @@
1
+ #usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'arduino_firmata'
5
+
6
+ arduino = ArduinoFirmata::Arduino.new ARGV.shift
7
+ puts "firmata version #{arduino.version}"
8
+
9
+ stat = true
10
+ loop do
11
+ puts stat
12
+ arduino.digital_write 13, stat
13
+ stat = !stat
14
+ sleep 0.1
15
+ end
16
+
@@ -0,0 +1,21 @@
1
+ #usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'arduino_firmata'
5
+
6
+ arduino = ArduinoFirmata::Arduino.new ARGV.shift
7
+ pin_num = 9
8
+
9
+ loop do
10
+ puts "0 -> 255"
11
+ 0.upto(255) do |i|
12
+ arduino.analog_write pin_num, i
13
+ sleep 0.01
14
+ end
15
+
16
+ puts "255 -> 0"
17
+ 255.downto(0) do |i|
18
+ arduino.analog_write pin_num, i
19
+ sleep 0.01
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ #usr/bin/env ruby
3
+ require 'rubygems'
4
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
5
+ require 'arduino_firmata'
6
+ require 'tw'
7
+
8
+ arduino = ArduinoFirmata::Arduino.new
9
+ sleep 3
10
+
11
+ ## LM35DZ -> Analog 0 PIN
12
+ puts temp = arduino.analog_read(0)*100*5/1024
13
+ client = Tw::Client.new
14
+ client.auth
15
+ client.tweet "現在の温度 #{temp}度"
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/arduino_firmata.rb'}"
9
+ puts "Loading arduino_firmata gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestArduinoFirmata < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/arduino_firmata'
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arduino_firmata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sho Hashimoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: serialport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.0
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: 2.0.0
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.10'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: '3.10'
52
+ - !ruby/object:Gem::Dependency
53
+ name: newgem
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 1.5.3
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.5.3
68
+ - !ruby/object:Gem::Dependency
69
+ name: hoe
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: '3.1'
84
+ description: Arduino Firmata protocol (http://firmata.org) implementation on Ruby.
85
+ email:
86
+ - hashimoto@shokai.org
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files:
90
+ - History.txt
91
+ - Manifest.txt
92
+ - README.rdoc
93
+ files:
94
+ - .gitignore
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - History.txt
98
+ - Manifest.txt
99
+ - README.rdoc
100
+ - Rakefile
101
+ - lib/arduino_firmata.rb
102
+ - lib/arduino_firmata/const.rb
103
+ - lib/arduino_firmata/main.rb
104
+ - samples/analog_read_write.rb
105
+ - samples/digital_read.rb
106
+ - samples/led_blink.rb
107
+ - samples/led_fade.rb
108
+ - samples/tweet_temperature.rb
109
+ - script/console
110
+ - script/destroy
111
+ - script/generate
112
+ - test/test_arduino_firmata.rb
113
+ - test/test_helper.rb
114
+ - .gemtest
115
+ homepage: http://github.com/shokai/arduino-firmata-ruby
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options:
119
+ - --main
120
+ - README.rdoc
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ segments:
130
+ - 0
131
+ hash: -658418046029011194
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project: arduino_firmata
140
+ rubygems_version: 1.8.24
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: Arduino Firmata protocol (http://firmata.org) implementation on Ruby.
144
+ test_files:
145
+ - test/test_arduino_firmata.rb
146
+ - test/test_helper.rb