arduino 0.2 → 0.3

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,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ arduino (0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ serialport (1.0.4)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ arduino!
16
+ serialport
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009-2010 Akash Manohar J <akash@akash.im>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Arduino ruby gem
2
+
3
+ Arduino is a prototyping API for Arduino in Ruby. Helps prototype Arduino programs quickly from the computer, without the need to burn to the board frequently.
4
+
5
+ #### Setup:
6
+ 1. Install the gem: `gem install arduino`
7
+ 2. Load arduino.pde onto your Arduino dev board (just once and for all).
8
+ 3. Import the arduino gem: `require "arduino"`
9
+
10
+ ## Methods
11
+
12
+ #### Initializing:
13
+
14
+ #Arduino.new(port, baudrate)
15
+ board = Arduino.new("/dev/ttyUSB1")
16
+
17
+ Port is something like "/dev/ttyUSB0" on linux and COM*x* (COM1/COM2) on windows. Baudrate is optional. It is 115200 by default.
18
+
19
+ #### Setting output pins
20
+
21
+ The output pins must be set explicitly.
22
+
23
+ #Arduino.output(list_of_output_pins)
24
+ board.output(10,11,13)
25
+
26
+
27
+ **Digital I/O**
28
+
29
+ 1. `Arduino.setHigh(pin)`
30
+ 2. `Arduino.setLow(pin)`
31
+ 3. `Arduino.getState(pin)` - returns `true` if pin state is high, else it returns `false`
32
+
33
+ **Analog I/O**
34
+
35
+ 1. `Arduino.analogRead(pin)` - returns the analog value
36
+ 2. `Arduino.analogRead(pin, value)` - sets the analog value
37
+
38
+ **Misc**
39
+
40
+ 1.) `Arduino.turnOff` - sets all the pins to low state
41
+
42
+ 2.) `Arduino.close` - closes serial connection. Using this makes sure that you won't have to disconnect & reconnect the Arduino again to recover the serial port.
43
+
44
+ ## Usage example
45
+
46
+ # This is the blink program.
47
+
48
+ require "arduino"
49
+
50
+ #specify the port Baudrate is optional and set to 115200 by default
51
+ myBoard = Arduino.new("/dev/ttyUSB1")
52
+
53
+ #declare output pins
54
+ myBoard.output(13)
55
+
56
+ #perform operations
57
+ 10.times do
58
+ myBoard.setHigh(13)
59
+ sleep(1)
60
+ myBoard.setLow(13)
61
+ sleep(1)
62
+ end
63
+
64
+ # Developed for the love of programming by
65
+ > &copy; 2010 Akash Manohar <akash@akash.im>
66
+ > under the MIT License
67
+
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.email = ["akash@akash.im"]
11
11
  s.homepage = ""
12
12
  s.summary = %q{Arduino Prototyping API for Ruby}
13
- s.description = %q{A library to talk to Arduino in Ruby without having to burn programs repeatedly to the board.}
13
+ s.description = %q{A ruby library to talk to Arduino without having to burn programs repeatedly to the board}
14
14
 
15
15
  s.rubyforge_project = "arduino"
16
16
 
@@ -0,0 +1,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
+
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
+
@@ -0,0 +1,15 @@
1
+ require "arduino"
2
+
3
+ #specify the port as an argument
4
+ myBoard = Arduino.new('/dev/ttyUSB2')
5
+
6
+ #declare output pins
7
+ myBoard.output(13)
8
+
9
+ #perform operations
10
+ 10.times do
11
+ myBoard.setHigh(13)
12
+ sleep(1)
13
+ myBoard.setLow(13)
14
+ sleep(1)
15
+ end
@@ -1,18 +1,35 @@
1
+ # The program takes an initial word or phrase from
2
+ # the command line (or in the absence of a
3
+ # parameter from the first line of standard
4
+ # input). In then reads successive words or
5
+ # phrases from standard input and reports whether
6
+ # they are angrams of the first word.
7
+ #
8
+ # Author:: Dave Thomas (mailto:dave@x.y)
9
+ # Copyright:: Copyright (c) 2002 The Pragmatic Programmers, LLC
10
+ # License:: Distributes under the same terms as Ruby
11
+
1
12
  require "serialport"
2
13
 
14
+ # The main Arduino class.
15
+ # Allows managing connection to board and getting & setting pin states.
16
+
3
17
  class Arduino
4
18
 
19
+ # initialize port and baudrate
5
20
  def initialize(port, baudrate=115200)
6
21
  @serial = SerialPort.new port, baudrate
7
22
  @serial.sync
8
- @port = port #Cannot get connected port via SerialPort class
23
+ @port = port
9
24
  @outputPins = []
10
25
  end
11
26
 
27
+ # Print information about connected board
12
28
  def to_s
13
29
  "Arduino is on port #{@port} at #{@serial.baud} baudrate"
14
30
  end
15
-
31
+
32
+ # Set output pins. This is a must.
16
33
  def output(*pinList)
17
34
  sendData(pinList.length)
18
35
 
@@ -26,22 +43,26 @@ class Arduino
26
43
  end
27
44
  end
28
45
 
46
+ # Get state of a digital pin. Returns true if high and false if low.
29
47
  def getState(pin)
30
48
  sendData('2')
31
49
  sendPin(pin)
32
50
  return formatPinState(getData())
33
51
  end
34
52
 
53
+ # Set a pin state to low
35
54
  def setLow(pin)
36
55
  sendData('0')
37
56
  sendPin(pin)
38
57
  end
39
-
58
+
59
+ # Set a pin state to high
40
60
  def setHigh(pin)
41
61
  sendData('1')
42
62
  sendPin(pin)
43
63
  end
44
-
64
+
65
+ # Write to an analog pin
45
66
  def analogWrite(pin, value)
46
67
  sendData('3')
47
68
  fullHexValue = value.to_s(base=16)
@@ -54,18 +75,21 @@ class Arduino
54
75
  sendData(hexValue[1])
55
76
  end
56
77
 
78
+ # Read from an analog pin
57
79
  def analogRead(pin)
58
80
  sendData('4')
59
81
  sendPin(pin)
60
82
  getData()
61
83
  end
62
84
 
85
+ # set all pins to low
63
86
  def turnOff
64
87
  @outputPins.each do |pin|
65
88
  setLow(pin)
66
89
  end
67
90
  end
68
91
 
92
+ # close serial connection to connected board
69
93
  def close
70
94
  @serial.close
71
95
  end
@@ -91,9 +115,8 @@ class Arduino
91
115
  end
92
116
 
93
117
  def formatPinState(pinValue)
94
- return 1 if pinValue=="1"
95
- return 0 #if pinValue=="0"
96
- #raise Exception, "Data/Connection error"
118
+ return true if pinValue=="1"
119
+ return false #if pinValue=="0"
97
120
  end
98
121
 
99
122
  end
@@ -1,3 +1,3 @@
1
1
  module Arduino
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- version: "0.2"
7
+ - 3
8
+ version: "0.3"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Akash Manohar
@@ -17,7 +17,7 @@ date: 2011-01-01 00:00:00 +05:30
17
17
  default_executable:
18
18
  dependencies: []
19
19
 
20
- description: A library to talk to Arduino in Ruby without having to burn programs repeatedly to the board.
20
+ description: A ruby library to talk to Arduino without having to burn programs repeatedly to the board
21
21
  email:
22
22
  - akash@akash.im
23
23
  executables: []
@@ -29,8 +29,13 @@ extra_rdoc_files: []
29
29
  files:
30
30
  - .gitignore
31
31
  - Gemfile
32
+ - Gemfile.lock
33
+ - MIT-license.txt
34
+ - README.md
32
35
  - Rakefile
33
36
  - arduino.gemspec
37
+ - arduino.pde
38
+ - example_blink.rb
34
39
  - lib/arduino.rb
35
40
  - lib/arduino/version.rb
36
41
  has_rdoc: true