arduino 0.3.3 → 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.
Files changed (6) hide show
  1. data/README.md +7 -1
  2. data/arduino.gemspec +3 -3
  3. data/arduino.pde +151 -131
  4. data/example_blink.rb +6 -2
  5. data/lib/arduino.rb +133 -120
  6. metadata +34 -64
data/README.md CHANGED
@@ -4,7 +4,7 @@ This gem is a prototyping API for Arduino in Ruby. Helps prototype Arduino progr
4
4
 
5
5
  #### Setup:
6
6
  1. Install the gem: `gem install arduino`
7
- 2. Load [arduino.pde](https://github.com/SingAlong/arduino/raw/master/arduino.pde) onto your Arduino dev board (just once and for all).
7
+ 2. Load [arduino.pde](https://github.com/HashNuke/arduino/raw/master/arduino.pde) onto your Arduino dev board (just once and for all).
8
8
  3. Import the arduino gem: `require "arduino"`
9
9
 
10
10
  ## Methods
@@ -65,3 +65,9 @@ The output pins must be set explicitly.
65
65
  > &copy; 2010 Akash Manohar <akash@akash.im>
66
66
  > under the MIT License
67
67
 
68
+ # Credits
69
+
70
+ Thanks to the following people:
71
+
72
+ * [@unsymbol](http://github.com/unsymbol) - for fixing the Ubuntu reset problem
73
+
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "arduino"
6
- s.version = "0.3.3"
6
+ s.version = "0.4"
7
7
  s.date = %q{2011-01-01}
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Akash Manohar"]
@@ -18,10 +18,10 @@ Thank you for installing the arduino gem
18
18
 
19
19
  Load the arduino.pde program from http://ln.akash.im/arduino.pde to your board before using this library
20
20
 
21
- Source: https://github.com/SingAlong/arduino/
21
+ Source: https://github.com/HashNuke/arduino/
22
22
 
23
23
  --
24
- SingAlong
24
+ @HashNuke
25
25
  (http://akash.im)
26
26
  ==============================================
27
27
  }
@@ -1,131 +1,151 @@
1
- // variable to store the data from the serial port
2
- int cmd = 0;
3
-
4
- // command arguments
5
- int cmd_arg[2];
6
-
7
-
8
- void setup() {
9
- // connect to the serial port
10
- Serial.begin(115200);
11
- // confirm ready state
12
-
13
- while(Serial.available()<1)
14
- {
15
- // get number of output pins and convert to int
16
- cmd = int(readData()) - 48;
17
- for(int i=0; i<cmd; i++)
18
- {
19
- cmd_arg[0] = int(readData()) - 48;
20
- pinMode(cmd_arg[0], OUTPUT);
21
- }
22
- break;
23
- }
24
- }
25
-
26
- void loop()
27
- {
28
-
29
- askCmd();
30
-
31
- if(Serial.available()>0)
32
- {
33
- cmd = int(Serial.read()) - 48;
34
-
35
- if(cmd==0) //set digital low
36
- {
37
- cmd_arg[0] = int(readData()) - 48;
38
- digitalWrite(cmd_arg[0],LOW);
39
- }
40
-
41
- if(cmd==1) //set digital high
42
- {
43
- cmd_arg[0] = int(readData()) - 48;
44
- digitalWrite(cmd_arg[0],HIGH);
45
- }
46
-
47
- if(cmd==2) //get digital value
48
- {
49
- cmd_arg[0] = int(readData()) - 48;
50
- cmd_arg[0] = digitalRead(cmd_arg[0]);
51
- Serial.println(cmd_arg[0]);
52
- }
53
-
54
- if(cmd==3) // set analog value
55
- {
56
- Serial.println("I'm in the right place");
57
- cmd_arg[0] = int(readData()) - 48;
58
- cmd_arg[1] = readHexValue();
59
- analogWrite(cmd_arg[0],cmd_arg[1]);
60
- }
61
-
62
- if(cmd==4) //read analog value
63
- {
64
- cmd_arg[0] = int(readData()) - 48;
65
- cmd_arg[0] = analogRead(cmd_arg[0]);
66
- Serial.println(cmd_arg[0]);
67
- }
68
- }
69
- }
70
-
71
- char readData()
72
- {
73
- askData();
74
-
75
- while(1)
76
- {
77
- if(Serial.available()>0)
78
- {
79
- return Serial.read();
80
- }
81
- }
82
- }
83
-
84
-
85
- //read hex value from serial and convert to integer
86
- int readHexValue()
87
- {
88
- int strval[2];
89
- int converted_str;
90
-
91
- while(1)
92
- {
93
- if(Serial.available()>0)
94
- {
95
- strval[0] = convert_hex_to_int(Serial.read());
96
- break;
97
- }
98
- }
99
-
100
- askData();
101
-
102
- while(1)
103
- {
104
- if(Serial.available()>0)
105
- {
106
- strval[1] = convert_hex_to_int(Serial.read());
107
- break;
108
- }
109
- }
110
-
111
- converted_str = (strval[0]*16) + strval[1];
112
- return converted_str;
113
- }
114
-
115
- int convert_hex_to_int(char c)
116
- {
117
- return (c <= '9') ? c-'0' : c-'a'+10;
118
- }
119
-
120
- void askData()
121
- {
122
- Serial.println("?");
123
- }
124
-
125
- void askCmd()
126
- {
127
- askData();
128
- while(Serial.available()<=0)
129
- {}
130
- }
131
-
1
+ // variable to store the data from the serial port
2
+ int cmd = 0;
3
+
4
+ // command arguments
5
+ int cmd_arg[2];
6
+
7
+ int serialStatus = 0;
8
+
9
+ void setup() {
10
+ // connect to the serial port
11
+ Serial.begin(115200);
12
+ setupPins();
13
+ serialStatus = 1;
14
+ }
15
+
16
+ void loop()
17
+ {
18
+
19
+ if(serialStatus==0)
20
+ {
21
+ Serial.flush();
22
+ setupPins();
23
+ }
24
+ askCmd();
25
+
26
+ {
27
+ if(Serial.available()>0)
28
+ {
29
+ cmd = int(Serial.read()) - 48;
30
+
31
+ if(cmd==0) //set digital low
32
+ {
33
+ cmd_arg[0] = int(readData()) - 48;
34
+ digitalWrite(cmd_arg[0],LOW);
35
+ }
36
+
37
+ if(cmd==1) //set digital high
38
+ {
39
+ cmd_arg[0] = int(readData()) - 48;
40
+ digitalWrite(cmd_arg[0],HIGH);
41
+ }
42
+
43
+ if(cmd==2) //get digital value
44
+ {
45
+ cmd_arg[0] = int(readData()) - 48;
46
+ cmd_arg[0] = digitalRead(cmd_arg[0]);
47
+ Serial.println(cmd_arg[0]);
48
+ }
49
+
50
+ if(cmd==3) // set analog value
51
+ {
52
+ Serial.println("I'm in the right place");
53
+ cmd_arg[0] = int(readData()) - 48;
54
+ cmd_arg[1] = readHexValue();
55
+ analogWrite(cmd_arg[0],cmd_arg[1]);
56
+ }
57
+
58
+ if(cmd==4) //read analog value
59
+ {
60
+ cmd_arg[0] = int(readData()) - 48;
61
+ cmd_arg[0] = analogRead(cmd_arg[0]);
62
+ Serial.println(cmd_arg[0]);
63
+ }
64
+
65
+ if(cmd==5)
66
+ {
67
+ serialStatus = 0;
68
+ }
69
+ }
70
+ }
71
+ }
72
+
73
+ char readData()
74
+ {
75
+ askData();
76
+
77
+ while(1)
78
+ {
79
+ if(Serial.available()>0)
80
+ {
81
+ return Serial.read();
82
+ }
83
+ }
84
+ }
85
+
86
+
87
+ //read hex value from serial and convert to integer
88
+ int readHexValue()
89
+ {
90
+ int strval[2];
91
+ int converted_str;
92
+
93
+ while(1)
94
+ {
95
+ if(Serial.available()>0)
96
+ {
97
+ strval[0] = convert_hex_to_int(Serial.read());
98
+ break;
99
+ }
100
+ }
101
+
102
+ askData();
103
+
104
+ while(1)
105
+ {
106
+ if(Serial.available()>0)
107
+ {
108
+ strval[1] = convert_hex_to_int(Serial.read());
109
+ break;
110
+ }
111
+ }
112
+
113
+ converted_str = (strval[0]*16) + strval[1];
114
+ return converted_str;
115
+ }
116
+
117
+
118
+ int convert_hex_to_int(char c)
119
+ {
120
+ return (c <= '9') ? c-'0' : c-'a'+10;
121
+ }
122
+
123
+
124
+ void askData()
125
+ {
126
+ Serial.println("?");
127
+ }
128
+
129
+
130
+ void askCmd()
131
+ {
132
+ askData();
133
+ while(Serial.available()<=0)
134
+ {}
135
+ }
136
+
137
+
138
+ void setupPins()
139
+ {
140
+ while(Serial.available()<1)
141
+ {
142
+ // get number of output pins and convert to int
143
+ cmd = int(readData()) - 48;
144
+ for(int i=0; i<cmd; i++)
145
+ {
146
+ cmd_arg[0] = int(readData()) - 48;
147
+ pinMode(cmd_arg[0], OUTPUT);
148
+ }
149
+ break;
150
+ }
151
+ }
@@ -1,13 +1,15 @@
1
1
  require "./lib/arduino"
2
2
 
3
+ port = '/dev/ttyUSB0'
4
+
3
5
  #specify the port as an argument
4
- board = Arduino.new('/dev/ttyUSB0')
6
+ board = Arduino.new(port)
5
7
 
6
8
  #declare output pins
7
9
  board.output(13)
8
10
 
9
11
  #perform operations
10
- 10.times do
12
+ 5.times do
11
13
  board.setHigh(13)
12
14
  puts "High" if board.getState(13)
13
15
  sleep(1)
@@ -15,3 +17,5 @@ board.output(13)
15
17
  puts "Low" if !board.getState(13)
16
18
  sleep(1)
17
19
  end
20
+
21
+ board.close
@@ -12,126 +12,139 @@ require "serialport"
12
12
 
13
13
  class Arduino
14
14
 
15
- # initialize port and baudrate
16
- def initialize(port, baudrate=115200)
17
- @serial = SerialPort.new port, baudrate
18
- @serial.sync
19
- @port = port
20
- @outputPins = []
21
- @pinStates = {}
22
- end
15
+ # initialize port and baudrate
16
+ def initialize(port, baudrate=115200)
17
+ puts "initialized"
18
+ data_bits = 8
19
+ stop_bits = 1
20
+ parity = SerialPort::NONE
23
21
 
24
- # Print information about connected board
25
- def to_s
26
- "Arduino is on port #{@port} at #{@serial.baud} baudrate"
27
- end
28
-
29
- # Set output pins. This is a must.
30
- def output(*pinList)
31
- sendData(pinList.length)
32
-
33
- if pinList.class==Array
34
- @outputPins = pinList
35
- pinList.each do |pin|
36
- sendPin(pin)
37
- end
38
- else
39
- raise ArgumentError, "Arguments must be a list of pin numbers"
40
- end
41
- return pinList
42
- end
22
+ @serial = SerialPort.new port, baudrate
23
+ @serial.read_timeout = 2
24
+ @serial.sync
25
+
26
+ @port = port
27
+ @outputPins = []
28
+ @pinStates = {}
29
+ end
30
+
31
+ # Print information about connected board
32
+ def to_s
33
+ "Arduino is on port #{@port} at #{@serial.baud} baudrate"
34
+ end
35
+
36
+ # Set output pins. This is a must.
37
+ def output(*pinList)
38
+ sendData(pinList.length)
39
+ if pinList.class==Array
40
+ @outputPins = pinList
41
+ pinList.each do |pin|
42
+ sendPin(pin)
43
+ end
44
+ else
45
+ raise ArgumentError, "Arguments must be a list of pin numbers"
46
+ end
47
+ puts "return pinlist"
48
+ return pinList
49
+ end
50
+
51
+ # Set a pin state to low
52
+ def setLow(pin)
53
+ saveState(pin, false)
54
+ sendData('0')
55
+ sendPin(pin)
56
+ end
57
+
58
+ def isLow?(pin)
59
+ if !getState(pin)
60
+ return true
61
+ else
62
+ return false
63
+ end
64
+ end
65
+
66
+ # Set a pin state to high
67
+ def setHigh(pin)
68
+ saveState(pin, true)
69
+ sendData('1')
70
+ sendPin(pin)
71
+ end
72
+
73
+ def isHigh?(pin)
74
+ if getState(pin)
75
+ return true
76
+ else
77
+ return false
78
+ end
79
+ end
80
+
81
+ def saveState(pin, state)
82
+ @pinStates[pin.to_s] = state
83
+ end
84
+
85
+ # Get state of a digital pin. Returns true if high and false if low.
86
+ def getState(pin)
87
+ if @pinStates.key?(pin.to_s)
88
+ return @pinStates[pin.to_s]
89
+ end
90
+ return false
91
+ end
92
+
93
+ # Write to an analog pin
94
+ def analogWrite(pin, value)
95
+ sendData('3')
96
+ fullHexValue = value.to_s(base=16)
97
+ hexValue = hexValue[2..fullHexValue.length]
98
+ if(hexValue.length==1)
99
+ sendData('0')
100
+ else
101
+ sendData(hexValue[0])
102
+ end
103
+ sendData(hexValue[1])
104
+ end
105
+
106
+ # Read from an analog pin
107
+ def analogRead(pin)
108
+ sendData('4')
109
+ sendPin(pin)
110
+ getData()
111
+ end
112
+
113
+ # set all pins to low
114
+ def turnOff
115
+ @outputPins.each do |pin|
116
+ setLow(pin)
117
+ end
118
+ end
119
+
120
+ # close serial connection to connected board
121
+ def close
122
+ # stops executing arduino code
123
+ @serial.write '5'.chr
124
+ # resets the arduino board (not on windows)
125
+ @serial.dtr=(0)
126
+ # close serial connection
127
+ @serial.close
128
+ p "closed"
129
+ end
130
+
131
+ private
132
+
133
+ def sendPin(pin)
134
+ pinInChar = (pin+48)
135
+ sendData(pinInChar)
136
+ end
43
137
 
44
- # Set a pin state to low
45
- def setLow(pin)
46
- saveState(pin, false)
47
- sendData('0')
48
- sendPin(pin)
49
- end
50
-
51
- def isLow?(pin)
52
- if !getState(pin)
53
- return true
54
- else
55
- return false
56
- end
57
- end
58
-
59
- # Set a pin state to high
60
- def setHigh(pin)
61
- saveState(pin, true)
62
- sendData('1')
63
- sendPin(pin)
64
- end
65
-
66
- def isHigh?(pin)
67
- if getState(pin)
68
- return true
69
- else
70
- return false
71
- end
72
- end
73
-
74
- def saveState(pin, state)
75
- @pinStates[pin.to_s] = state
76
- end
77
-
78
- # Get state of a digital pin. Returns true if high and false if low.
79
- def getState(pin)
80
- if @pinStates.key?(pin.to_s)
81
- return @pinStates[pin.to_s]
82
- end
83
- return false
84
- end
85
-
86
- # Write to an analog pin
87
- def analogWrite(pin, value)
88
- sendData('3')
89
- fullHexValue = value.to_s(base=16)
90
- hexValue = hexValue[2..fullHexValue.length]
91
- if(hexValue.length==1)
92
- sendData('0')
93
- else
94
- sendData(hexValue[0])
95
- end
96
- sendData(hexValue[1])
97
- end
98
-
99
- # Read from an analog pin
100
- def analogRead(pin)
101
- sendData('4')
102
- sendPin(pin)
103
- getData()
104
- end
105
-
106
- # set all pins to low
107
- def turnOff
108
- @outputPins.each do |pin|
109
- setLow(pin)
110
- end
111
- end
112
-
113
- # close serial connection to connected board
114
- def close
115
- @serial.close
116
- end
117
-
118
- private
119
-
120
- def sendPin(pin)
121
- pinInChar = (pin+48)
122
- sendData(pinInChar)
123
- end
124
-
125
- def sendData(serialData)
126
- while true
127
- break if getData()=="?"
128
- end
129
- s = String(serialData.chr)
130
- x = @serial.write (s)
131
- end
132
-
133
- def getData
134
- cleanData = @serial.readlines()
135
- cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
136
- end
138
+ def sendData(serialData)
139
+ while true
140
+ break if getData()=="?"
141
+ end
142
+ s = String(serialData.chr)
143
+ x = @serial.write s
144
+ end
145
+
146
+ def getData
147
+ cleanData = @serial.readlines()
148
+ cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
149
+ end
137
150
  end
metadata CHANGED
@@ -1,47 +1,35 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: arduino
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 3
8
- - 3
9
- version: 0.3.3
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Akash Manohar
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-01-01 00:00:00 +05:30
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-01-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: serialport
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70182320491400 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 1
30
- - 0
31
- - 4
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
32
21
  version: 1.0.4
33
22
  type: :runtime
34
- version_requirements: *id001
35
- description: A ruby library to talk to Arduino without having to burn programs repeatedly to the board
36
- email:
23
+ prerelease: false
24
+ version_requirements: *70182320491400
25
+ description: A ruby library to talk to Arduino without having to burn programs repeatedly
26
+ to the board
27
+ email:
37
28
  - akash@akash.im
38
29
  executables: []
39
-
40
30
  extensions: []
41
-
42
31
  extra_rdoc_files: []
43
-
44
- files:
32
+ files:
45
33
  - .gitignore
46
34
  - Gemfile
47
35
  - Gemfile.lock
@@ -52,49 +40,31 @@ files:
52
40
  - arduino.pde
53
41
  - example_blink.rb
54
42
  - lib/arduino.rb
55
- has_rdoc: true
56
43
  homepage: http://akash.im/arduino-ruby
57
44
  licenses: []
58
-
59
- post_install_message: "\n\
60
- ==============================================\n\
61
- Thank you for installing the arduino gem\n\
62
- ----------------------------------------------\n\
63
- \t\n\
64
- Load the arduino.pde program from http://ln.akash.im/arduino.pde to your board before using this library\n\
65
- \t\n\
66
- Source: https://github.com/SingAlong/arduino/\n\n\
67
- --\n\
68
- SingAlong\n\
69
- (http://akash.im)\n\
70
- ==============================================\n\
71
- \t"
45
+ post_install_message: ! "\n==============================================\nThank you
46
+ for installing the arduino gem\n----------------------------------------------\n\t\nLoad
47
+ the arduino.pde program from http://ln.akash.im/arduino.pde to your board before
48
+ using this library\n\t\nSource: https://github.com/HashNuke/arduino/\n\n--\n@HashNuke\n(http://akash.im)\n==============================================\n\t"
72
49
  rdoc_options: []
73
-
74
- require_paths:
50
+ require_paths:
75
51
  - lib
76
- required_ruby_version: !ruby/object:Gem::Requirement
52
+ required_ruby_version: !ruby/object:Gem::Requirement
77
53
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- segments:
82
- - 0
83
- version: "0"
84
- required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
59
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- segments:
90
- - 0
91
- version: "0"
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
92
64
  requirements: []
93
-
94
65
  rubyforge_project: arduino
95
- rubygems_version: 1.3.7
66
+ rubygems_version: 1.8.10
96
67
  signing_key:
97
68
  specification_version: 3
98
69
  summary: Arduino Prototyping API for Ruby
99
70
  test_files: []
100
-