Skip to content

Commit e5119ea

Browse files
committed
cleaning up for v0.3
Signed-off-by: Akash Manohar J <[email protected]>
0 parents  commit e5119ea

11 files changed

+381
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pkg/*
2+
*.gem
3+
.bundle

Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "http://rubygems.org"
2+
3+
# Specify your gem's dependencies in arduino.gemspec
4+
gemspec
5+
gem "serialport"

Gemfile.lock

+16
Original file line numberDiff line numberDiff line change
@@ -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

MIT-license.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2009-2010 Akash Manohar J <[email protected]>
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.

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -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 <[email protected]>
66+
> under the MIT License
67+

Rakefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'bundler'
2+
Bundler::GemHelper.install_tasks

arduino.gemspec

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "arduino/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "arduino"
7+
s.version = Arduino::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.authors = ["Akash Manohar"]
10+
s.email = ["[email protected]"]
11+
s.homepage = ""
12+
s.summary = %q{Arduino Prototyping API for Ruby}
13+
s.description = %q{A ruby library to talk to Arduino without having to burn programs repeatedly to the board}
14+
15+
s.rubyforge_project = "arduino"
16+
17+
s.files = `git ls-files`.split("\n")
18+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20+
s.require_paths = ["lib"]
21+
end

arduino.pde

+131
Original file line numberDiff line numberDiff line change
@@ -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+

example_blink.rb

+15
Original file line numberDiff line numberDiff line change
@@ -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

lib/arduino.rb

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
require "serialport"
2+
3+
class Arduino
4+
5+
def initialize(port, baudrate=115200)
6+
@serial = SerialPort.new port, baudrate
7+
@serial.sync
8+
@port = port #Cannot get connected port via SerialPort class
9+
@outputPins = []
10+
end
11+
12+
def to_s
13+
"Arduino is on port #{@port} at #{@serial.baud} baudrate"
14+
end
15+
16+
def output(*pinList)
17+
sendData(pinList.length)
18+
19+
if pinList.class==Array
20+
@outputPins = pinList
21+
pinList.each do |pin|
22+
sendPin(pin)
23+
end
24+
else
25+
raise ArgumentError, "Arguments must be a list of pin numbers"
26+
end
27+
end
28+
29+
def getState(pin)
30+
sendData('2')
31+
sendPin(pin)
32+
return formatPinState(getData())
33+
end
34+
35+
def setLow(pin)
36+
sendData('0')
37+
sendPin(pin)
38+
end
39+
40+
def setHigh(pin)
41+
sendData('1')
42+
sendPin(pin)
43+
end
44+
45+
def analogWrite(pin, value)
46+
sendData('3')
47+
fullHexValue = value.to_s(base=16)
48+
hexValue = hexValue[2..fullHexValue.length]
49+
if(hexValue.length==1)
50+
sendData('0')
51+
else
52+
sendData(hexValue[0])
53+
end
54+
sendData(hexValue[1])
55+
end
56+
57+
def analogRead(pin)
58+
sendData('4')
59+
sendPin(pin)
60+
getData()
61+
end
62+
63+
def turnOff
64+
@outputPins.each do |pin|
65+
setLow(pin)
66+
end
67+
end
68+
69+
def close
70+
@serial.close
71+
end
72+
73+
private
74+
75+
def sendPin(pin)
76+
pinInChar = (pin+48)
77+
sendData(pinInChar)
78+
end
79+
80+
def sendData(serialData)
81+
while true
82+
break if getData()=="?"
83+
end
84+
s = String(serialData.chr)
85+
x = @serial.write (s)
86+
end
87+
88+
def getData
89+
cleanData = @serial.readlines()
90+
cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
91+
end
92+
93+
def formatPinState(pinValue)
94+
return true if pinValue=="1"
95+
return false #if pinValue=="0"
96+
#raise Exception, "Data/Connection error"
97+
end
98+
99+
end

lib/arduino/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Arduino
2+
VERSION = "0.3"
3+
end

0 commit comments

Comments
 (0)