arduino_firmata 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.4 2012-10-24
2
+
3
+ * use ArduinoFirmata.connect
4
+
1
5
  === 0.0.3 2012-10-24
2
6
 
3
7
  * get ArduinoFirmata::list on ubuntu
data/Manifest.txt CHANGED
@@ -6,6 +6,7 @@ Manifest.txt
6
6
  README.rdoc
7
7
  Rakefile
8
8
  lib/arduino_firmata.rb
9
+ lib/arduino_firmata/arduino.rb
9
10
  lib/arduino_firmata/const.rb
10
11
  lib/arduino_firmata/main.rb
11
12
  samples/README.md
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = arduino_firmata
2
2
 
3
- * http://github.com/shokai/arduino-firmata-ruby
3
+ * http://github.com/shokai/arduino_firmata
4
4
 
5
5
  == INSTALL:
6
6
 
@@ -12,11 +12,11 @@ Arduino Firmata protocol (http://firmata.org) implementation on Ruby.
12
12
 
13
13
  == SYNOPSIS:
14
14
 
15
- arduino = ArduinoFirmata::Arduino.new
15
+ arduino = ArduinoFirmata.connect
16
16
 
17
17
  or
18
18
 
19
- arduino = ArduinoFirmata::Arduino.new '/dev/tty.usb-device-name'
19
+ arduino = ArduinoFirmata.connect '/dev/tty.usb-device-name'
20
20
 
21
21
  Board Version
22
22
 
@@ -46,7 +46,7 @@ Analog Read
46
46
 
47
47
  puts arduino.analog_read 0 # => 0 ~ 1023
48
48
 
49
- see samples https://github.com/shokai/arduino-firmata-ruby/tree/gem/samples
49
+ see samples https://github.com/shokai/arduino_firmata/tree/gem/samples
50
50
 
51
51
  == REQUIREMENTS:
52
52
 
@@ -1,9 +1,12 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
- require 'arduino_firmata/const'
4
+ require 'serialport'
5
+ require 'stringio'
5
6
  require 'arduino_firmata/main'
7
+ require 'arduino_firmata/const'
8
+ require 'arduino_firmata/arduino'
6
9
 
7
10
  module ArduinoFirmata
8
- VERSION = '0.0.3'
11
+ VERSION = '0.0.4'
9
12
  end
@@ -0,0 +1,132 @@
1
+ module ArduinoFirmata
2
+
3
+ class Arduino
4
+
5
+ attr_reader :version
6
+
7
+ def initialize(serial_name, params)
8
+ @wait_for_data = 0
9
+ @execute_multi_byte_command = 0
10
+ @multi_byte_channel = 0
11
+ @stored_input_data = []
12
+ @parsing_sysex = false
13
+ @sysex_bytes_read
14
+
15
+ @digital_output_data = Array.new(16, 0)
16
+ @digital_input_data = Array.new(16, 0)
17
+ @analog_input_data = Array.new(16, 0)
18
+
19
+ @version = nil
20
+
21
+ @serial = SerialPort.new(serial_name, params[:bps], params[:bit], params[:stopbit], params[:parity])
22
+ sleep 3
23
+
24
+ Thread.new{
25
+ loop do
26
+ process_input
27
+ sleep 0.1
28
+ end
29
+ }.run
30
+
31
+ (0...6).each do |i|
32
+ write(REPORT_ANALOG | i)
33
+ write 1
34
+ end
35
+ (0...2).each do |i|
36
+ write(REPORT_DIGITAL | i)
37
+ write 1
38
+ end
39
+
40
+ loop do
41
+ break if @version
42
+ sleep 0.3
43
+ end
44
+ sleep 0.5
45
+
46
+ end
47
+
48
+ def digital_read(pin)
49
+ (@digital_input_data[pin >> 3] >> (pin & 0x07)) & 0x01 > 0
50
+ end
51
+
52
+ def analog_read(pin)
53
+ @analog_input_data[pin]
54
+ end
55
+
56
+ def pin_mode(pin, mode)
57
+ write SET_PIN_MODE
58
+ write pin
59
+ write mode
60
+ end
61
+
62
+ def digital_write(pin, value)
63
+ port_num = (pin >> 3) & 0x0F
64
+ if value == 0 or value == true
65
+ @digital_output_data[port_num] &= ~(1 << (pin & 0x07))
66
+ else
67
+ @digital_output_data[port_num] |= (1 << (pin & 0x07))
68
+ end
69
+
70
+ write(DIGITAL_MESSAGE | port_num)
71
+ write(@digital_output_data[port_num] & 0x7F)
72
+ write(@digital_output_data[port_num] >> 7)
73
+ end
74
+
75
+ def analog_write(pin, value)
76
+ pin_mode pin, PWM
77
+ write(ANALOG_MESSAGE | (pin & 0x0F))
78
+ write(value & 0x7F)
79
+ write(value >> 7)
80
+ end
81
+
82
+ private
83
+ def write(cmd)
84
+ @serial.write_nonblock cmd.chr
85
+ end
86
+
87
+ def read
88
+ @serial.read_nonblock 9600 rescue EOFError
89
+ end
90
+
91
+ def process_input
92
+ StringIO.new(String read).bytes.each do |input_data|
93
+ command = nil
94
+
95
+ if @parsing_sysex
96
+ if input_data == END_SYSEX
97
+ @parsing_sysex = FALSE
98
+ else
99
+ @stored_input_data[@sysex_bytes_read] = input_data
100
+ @sysex_bytes_read += 1
101
+ end
102
+ elsif @wait_for_data > 0 and input_data < 128
103
+ @wait_for_data -= 1
104
+ @stored_input_data[@wait_for_data] = input_data
105
+ if @execute_multi_byte_command != 0 and @wait_for_data == 0
106
+ case @execute_multi_byte_command
107
+ when DIGITAL_MESSAGE
108
+ @digital_input_data[@multi_byte_channel] = (@stored_input_data[0] << 7) + @stored_input_data[1]
109
+ when ANALOG_MESSAGE
110
+ @analog_input_data[@multi_byte_channel] = (@stored_input_data[0] << 7) + @stored_input_data[1]
111
+ when REPORT_VERSION
112
+ @version = "#{@stored_input_data[1]}.#{@stored_input_data[0]}"
113
+ end
114
+ end
115
+ else
116
+ if input_data < 0xF0
117
+ command = input_data & 0xF0
118
+ @multi_byte_channel = input_data & 0x0F
119
+ else
120
+ command = input_data
121
+ end
122
+ if [DIGITAL_MESSAGE, ANALOG_MESSAGE, REPORT_VERSION].include? command
123
+ @wait_for_data = 2
124
+ @execute_multi_byte_command = command
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ end
@@ -1,7 +1,14 @@
1
1
  module ArduinoFirmata
2
2
 
3
- def self.list
4
- Dir.entries('/dev').grep(/tty\.?usb/i).map{|fname| "/dev/#{fname}"}
3
+ class Params
4
+ def self.default
5
+ {
6
+ :bps => 57600,
7
+ :bit => 8,
8
+ :parity => 0,
9
+ :stopbit => 1
10
+ }
11
+ end
5
12
  end
6
13
 
7
14
  INPUT = 0
@@ -1,136 +1,17 @@
1
- require 'serialport'
2
- require 'stringio'
3
-
4
1
  module ArduinoFirmata
5
2
 
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 = nil
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
- loop do
45
- break if @version
46
- sleep 0.3
47
- end
48
- sleep 0.5
49
-
50
- end
51
-
52
- def digital_read(pin)
53
- (@digital_input_data[pin >> 3] >> (pin & 0x07)) & 0x01 > 0
54
- end
55
-
56
- def analog_read(pin)
57
- @analog_input_data[pin]
58
- end
59
-
60
- def pin_mode(pin, mode)
61
- write SET_PIN_MODE
62
- write pin
63
- write mode
64
- end
65
-
66
- def digital_write(pin, value)
67
- port_num = (pin >> 3) & 0x0F
68
- if value == 0 or value == true
69
- @digital_output_data[port_num] &= ~(1 << (pin & 0x07))
70
- else
71
- @digital_output_data[port_num] |= (1 << (pin & 0x07))
72
- end
73
-
74
- write(DIGITAL_MESSAGE | port_num)
75
- write(@digital_output_data[port_num] & 0x7F)
76
- write(@digital_output_data[port_num] >> 7)
77
- end
78
-
79
- def analog_write(pin, value)
80
- pin_mode pin, PWM
81
- write(ANALOG_MESSAGE | (pin & 0x0F))
82
- write(value & 0x7F)
83
- write(value >> 7)
84
- end
3
+ def self.list
4
+ Dir.entries('/dev').grep(/tty\.?usb/i).map{|fname| "/dev/#{fname}"}
5
+ end
85
6
 
86
- private
87
- def write(cmd)
88
- @serial.write_nonblock cmd.chr
89
- end
7
+ def self.connect(serial_name=nil, params={})
8
+ serial_name = self.list[0] unless serial_name
90
9
 
91
- def read
92
- @serial.read_nonblock 9600 rescue EOFError
10
+ Params.default.each do |k,v|
11
+ params[k] = v unless params[k]
93
12
  end
94
13
 
95
- def process_input
96
- StringIO.new(String read).bytes.each do |input_data|
97
- command = nil
98
-
99
- if @parsing_sysex
100
- if input_data == END_SYSEX
101
- @parsing_sysex = FALSE
102
- else
103
- @stored_input_data[@sysex_bytes_read] = input_data
104
- @sysex_bytes_read += 1
105
- end
106
- elsif @wait_for_data > 0 and input_data < 128
107
- @wait_for_data -= 1
108
- @stored_input_data[@wait_for_data] = input_data
109
- if @execute_multi_byte_command != 0 and @wait_for_data == 0
110
- case @execute_multi_byte_command
111
- when DIGITAL_MESSAGE
112
- @digital_input_data[@multi_byte_channel] = (@stored_input_data[0] << 7) + @stored_input_data[1]
113
- when ANALOG_MESSAGE
114
- @analog_input_data[@multi_byte_channel] = (@stored_input_data[0] << 7) + @stored_input_data[1]
115
- when REPORT_VERSION
116
- @version = "#{@stored_input_data[1]}.#{@stored_input_data[0]}"
117
- end
118
- end
119
- else
120
- if input_data < 0xF0
121
- command = input_data & 0xF0
122
- @multi_byte_channel = input_data & 0x0F
123
- else
124
- command = input_data
125
- end
126
- if [DIGITAL_MESSAGE, ANALOG_MESSAGE, REPORT_VERSION].include? command
127
- @wait_for_data = 2
128
- @execute_multi_byte_command = command
129
- end
130
- end
131
- end
132
- end
133
-
14
+ Arduino.new serial_name, params
134
15
  end
135
-
16
+
136
17
  end
data/samples/README.md CHANGED
@@ -1 +1,16 @@
1
- # arduino firmata samples
1
+ # Arduino Firmata Samples
2
+
3
+ Digital/Analog IO
4
+ -----------------
5
+ - analog_read_write.rb
6
+ - digital_read.rb
7
+ - led_blink.rb
8
+ - led_fade.rb
9
+
10
+
11
+ Tweet Temperature
12
+ -----------------
13
+
14
+ % gem install tw
15
+ % tw --user:add
16
+ % ruby tweet_temperature.rb
@@ -3,11 +3,11 @@ require 'rubygems'
3
3
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
4
  require 'arduino_firmata'
5
5
 
6
- arduino = ArduinoFirmata::Arduino.new ARGV.shift
6
+ arduino = ArduinoFirmata.connect ARGV.shift
7
7
 
8
8
  loop do
9
9
  an = arduino.analog_read 0
10
10
  puts an
11
- arduino.analog_write 9, an/4
11
+ arduino.analog_write 11, an/4
12
12
  sleep 0.1
13
13
  end
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
4
  require 'arduino_firmata'
5
5
 
6
- arduino = ArduinoFirmata::Arduino.new ARGV.shift
6
+ arduino = ArduinoFirmata.connect ARGV.shift
7
7
 
8
8
  arduino.pin_mode 7, ArduinoFirmata::INPUT
9
9
 
data/samples/led_blink.rb CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
4
  require 'arduino_firmata'
5
5
 
6
- arduino = ArduinoFirmata::Arduino.new ARGV.shift
6
+ arduino = ArduinoFirmata.connect ARGV.shift
7
7
  puts "firmata version #{arduino.version}"
8
8
 
9
9
  stat = true
data/samples/led_fade.rb CHANGED
@@ -3,18 +3,20 @@ require 'rubygems'
3
3
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
4
  require 'arduino_firmata'
5
5
 
6
- arduino = ArduinoFirmata::Arduino.new ARGV.shift
7
- pin_num = 9
6
+ arduino = ArduinoFirmata.connect ARGV.shift
7
+ pin_num = 11
8
8
 
9
9
  loop do
10
- puts "0 -> 255"
10
+ puts "-"*10
11
11
  0.upto(255) do |i|
12
+ puts i
12
13
  arduino.analog_write pin_num, i
13
14
  sleep 0.01
14
15
  end
15
-
16
- puts "255 -> 0"
16
+
17
+ puts "-"*10
17
18
  255.downto(0) do |i|
19
+ puts i
18
20
  arduino.analog_write pin_num, i
19
21
  sleep 0.01
20
22
  end
@@ -5,7 +5,7 @@ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
5
5
  require 'arduino_firmata'
6
6
  require 'tw'
7
7
 
8
- arduino = ArduinoFirmata::Arduino.new
8
+ arduino = ArduinoFirmata.connect
9
9
 
10
10
  ## LM35DZ -> Analog 1 PIN
11
11
  puts temp = arduino.analog_read(1)*100*5/1024
metadata CHANGED
@@ -1,96 +1,104 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: arduino_firmata
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 4
10
+ version: 0.0.4
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Sho Hashimoto
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-10-24 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: serialport
16
- requirement: !ruby/object:Gem::Requirement
17
+
18
+ date: 2012-10-24 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
17
23
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 19
28
+ segments:
29
+ - 1
30
+ - 1
31
+ - 0
21
32
  version: 1.1.0
22
33
  - - <
23
- - !ruby/object:Gem::Version
34
+ - !ruby/object:Gem::Version
35
+ hash: 15
36
+ segments:
37
+ - 2
38
+ - 0
39
+ - 0
24
40
  version: 2.0.0
25
41
  type: :runtime
42
+ name: serialport
43
+ version_requirements: *id001
44
+ - !ruby/object:Gem::Dependency
26
45
  prerelease: false
27
- version_requirements: !ruby/object:Gem::Requirement
46
+ requirement: &id002 !ruby/object:Gem::Requirement
28
47
  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:
48
+ requirements:
41
49
  - - ~>
42
- - !ruby/object:Gem::Version
43
- version: '3.10'
50
+ - !ruby/object:Gem::Version
51
+ hash: 19
52
+ segments:
53
+ - 3
54
+ - 10
55
+ version: "3.10"
44
56
  type: :development
57
+ name: rdoc
58
+ version_requirements: *id002
59
+ - !ruby/object:Gem::Dependency
45
60
  prerelease: false
46
- version_requirements: !ruby/object:Gem::Requirement
61
+ requirement: &id003 !ruby/object:Gem::Requirement
47
62
  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
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 5
67
+ segments:
68
+ - 1
69
+ - 5
70
+ - 3
59
71
  version: 1.5.3
60
72
  type: :development
73
+ name: newgem
74
+ version_requirements: *id003
75
+ - !ruby/object:Gem::Dependency
61
76
  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
77
+ requirement: &id004 !ruby/object:Gem::Requirement
71
78
  none: false
72
- requirements:
79
+ requirements:
73
80
  - - ~>
74
- - !ruby/object:Gem::Version
75
- version: '3.1'
81
+ - !ruby/object:Gem::Version
82
+ hash: 5
83
+ segments:
84
+ - 3
85
+ - 1
86
+ version: "3.1"
76
87
  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'
88
+ name: hoe
89
+ version_requirements: *id004
84
90
  description: Arduino Firmata protocol (http://firmata.org) implementation on Ruby.
85
- email:
91
+ email:
86
92
  - hashimoto@shokai.org
87
93
  executables: []
94
+
88
95
  extensions: []
89
- extra_rdoc_files:
96
+
97
+ extra_rdoc_files:
90
98
  - History.txt
91
99
  - Manifest.txt
92
100
  - README.rdoc
93
- files:
101
+ files:
94
102
  - .gitignore
95
103
  - Gemfile
96
104
  - Gemfile.lock
@@ -99,6 +107,7 @@ files:
99
107
  - README.rdoc
100
108
  - Rakefile
101
109
  - lib/arduino_firmata.rb
110
+ - lib/arduino_firmata/arduino.rb
102
111
  - lib/arduino_firmata/const.rb
103
112
  - lib/arduino_firmata/main.rb
104
113
  - samples/README.md
@@ -113,35 +122,40 @@ files:
113
122
  - test/test_arduino_firmata.rb
114
123
  - test/test_helper.rb
115
124
  - .gemtest
116
- homepage: http://github.com/shokai/arduino-firmata-ruby
125
+ homepage: http://github.com/shokai/arduino_firmata
117
126
  licenses: []
127
+
118
128
  post_install_message:
119
- rdoc_options:
129
+ rdoc_options:
120
130
  - --main
121
131
  - README.rdoc
122
- require_paths:
132
+ require_paths:
123
133
  - lib
124
- required_ruby_version: !ruby/object:Gem::Requirement
134
+ required_ruby_version: !ruby/object:Gem::Requirement
125
135
  none: false
126
- requirements:
127
- - - ! '>='
128
- - !ruby/object:Gem::Version
129
- version: '0'
130
- segments:
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
131
141
  - 0
132
- hash: -3581276081025473849
133
- required_rubygems_version: !ruby/object:Gem::Requirement
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
144
  none: false
135
- requirements:
136
- - - ! '>='
137
- - !ruby/object:Gem::Version
138
- version: '0'
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
139
152
  requirements: []
153
+
140
154
  rubyforge_project: arduino_firmata
141
155
  rubygems_version: 1.8.24
142
156
  signing_key:
143
157
  specification_version: 3
144
158
  summary: Arduino Firmata protocol (http://firmata.org) implementation on Ruby.
145
- test_files:
159
+ test_files:
146
160
  - test/test_arduino_firmata.rb
147
161
  - test/test_helper.rb