Permalink
Browse files

cleaning up for v0.3

Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information...
0 parents commit e5119ea10e28ea3a3e81bf9c62a610b612776a9e @HashNuke HashNuke committed Jan 1, 2011
Showing with 381 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +5 −0 Gemfile
  3. +16 −0 Gemfile.lock
  4. +19 −0 MIT-license.txt
  5. +67 −0 README.md
  6. +2 −0 Rakefile
  7. +21 −0 arduino.gemspec
  8. +131 −0 arduino.pde
  9. +15 −0 example_blink.rb
  10. +99 −0 lib/arduino.rb
  11. +3 −0 lib/arduino/version.rb
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
@@ -0,0 +1,5 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in arduino.gemspec
+gemspec
+gem "serialport"
@@ -0,0 +1,16 @@
+PATH
+ remote: .
+ specs:
+ arduino (0.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ serialport (1.0.4)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ arduino!
+ serialport
@@ -0,0 +1,19 @@
+Copyright (c) 2009-2010 Akash Manohar J <akash@akash.im>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,67 @@
+# Arduino ruby gem
+
+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.
+
+#### Setup:
+1. Install the gem: `gem install arduino`
+2. Load arduino.pde onto your Arduino dev board (just once and for all).
+3. Import the arduino gem: `require "arduino"`
+
+## Methods
+
+#### Initializing:
+
+ #Arduino.new(port, baudrate)
+ board = Arduino.new("/dev/ttyUSB1")
+
+Port is something like "/dev/ttyUSB0" on linux and COM*x* (COM1/COM2) on windows. Baudrate is optional. It is 115200 by default.
+
+#### Setting output pins
+
+The output pins must be set explicitly.
+
+ #Arduino.output(list_of_output_pins)
+ board.output(10,11,13)
+
+
+**Digital I/O**
+
+1. `Arduino.setHigh(pin)`
+2. `Arduino.setLow(pin)`
+3. `Arduino.getState(pin)` - returns `true` if pin state is high, else it returns `false`
+
+**Analog I/O**
+
+1. `Arduino.analogRead(pin)` - returns the analog value
+2. `Arduino.analogRead(pin, value)` - sets the analog value
+
+**Misc**
+
+1.) `Arduino.turnOff` - sets all the pins to low state
+
+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.
+
+## Usage example
+
+ # This is the blink program.
+
+ require "arduino"
+
+ #specify the port Baudrate is optional and set to 115200 by default
+ myBoard = Arduino.new("/dev/ttyUSB1")
+
+ #declare output pins
+ myBoard.output(13)
+
+ #perform operations
+ 10.times do
+ myBoard.setHigh(13)
+ sleep(1)
+ myBoard.setLow(13)
+ sleep(1)
+ end
+
+# Developed for the love of programming by
+> &copy; 2010 Akash Manohar <akash@akash.im>
+> under the MIT License
+
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "arduino/version"
+
+Gem::Specification.new do |s|
+ s.name = "arduino"
+ s.version = Arduino::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Akash Manohar"]
+ s.email = ["akash@akash.im"]
+ s.homepage = ""
+ s.summary = %q{Arduino Prototyping API for Ruby}
+ s.description = %q{A ruby library to talk to Arduino without having to burn programs repeatedly to the board}
+
+ s.rubyforge_project = "arduino"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
@@ -0,0 +1,131 @@
+// variable to store the data from the serial port
+int cmd = 0;
+
+// command arguments
+int cmd_arg[2];
+
+
+void setup() {
+ // connect to the serial port
+ Serial.begin(115200);
+ // confirm ready state
+
+ while(Serial.available()<1)
+ {
+ // get number of output pins and convert to int
+ cmd = int(readData()) - 48;
+ for(int i=0; i<cmd; i++)
+ {
+ cmd_arg[0] = int(readData()) - 48;
+ pinMode(cmd_arg[0], OUTPUT);
+ }
+ break;
+ }
+}
+
+void loop()
+{
+
+ askCmd();
+
+ if(Serial.available()>0)
+ {
+ cmd = int(Serial.read()) - 48;
+
+ if(cmd==0) //set digital low
+ {
+ cmd_arg[0] = int(readData()) - 48;
+ digitalWrite(cmd_arg[0],LOW);
+ }
+
+ if(cmd==1) //set digital high
+ {
+ cmd_arg[0] = int(readData()) - 48;
+ digitalWrite(cmd_arg[0],HIGH);
+ }
+
+ if(cmd==2) //get digital value
+ {
+ cmd_arg[0] = int(readData()) - 48;
+ cmd_arg[0] = digitalRead(cmd_arg[0]);
+ Serial.println(cmd_arg[0]);
+ }
+
+ if(cmd==3) // set analog value
+ {
+ Serial.println("I'm in the right place");
+ cmd_arg[0] = int(readData()) - 48;
+ cmd_arg[1] = readHexValue();
+ analogWrite(cmd_arg[0],cmd_arg[1]);
+ }
+
+ if(cmd==4) //read analog value
+ {
+ cmd_arg[0] = int(readData()) - 48;
+ cmd_arg[0] = analogRead(cmd_arg[0]);
+ Serial.println(cmd_arg[0]);
+ }
+ }
+}
+
+char readData()
+{
+ askData();
+
+ while(1)
+ {
+ if(Serial.available()>0)
+ {
+ return Serial.read();
+ }
+ }
+}
+
+
+//read hex value from serial and convert to integer
+int readHexValue()
+{
+ int strval[2];
+ int converted_str;
+
+ while(1)
+ {
+ if(Serial.available()>0)
+ {
+ strval[0] = convert_hex_to_int(Serial.read());
+ break;
+ }
+ }
+
+ askData();
+
+ while(1)
+ {
+ if(Serial.available()>0)
+ {
+ strval[1] = convert_hex_to_int(Serial.read());
+ break;
+ }
+ }
+
+ converted_str = (strval[0]*16) + strval[1];
+ return converted_str;
+}
+
+int convert_hex_to_int(char c)
+{
+ return (c <= '9') ? c-'0' : c-'a'+10;
+}
+
+void askData()
+{
+ Serial.println("?");
+}
+
+void askCmd()
+{
+ askData();
+ while(Serial.available()<=0)
+ {}
+}
+
@@ -0,0 +1,15 @@
+require "arduino"
+
+#specify the port as an argument
+myBoard = Arduino.new('/dev/ttyUSB2')
+
+#declare output pins
+myBoard.output(13)
+
+#perform operations
+10.times do
+ myBoard.setHigh(13)
+ sleep(1)
+ myBoard.setLow(13)
+ sleep(1)
+end
@@ -0,0 +1,99 @@
+require "serialport"
+
+class Arduino
+
+ def initialize(port, baudrate=115200)
+ @serial = SerialPort.new port, baudrate
+ @serial.sync
+ @port = port #Cannot get connected port via SerialPort class
+ @outputPins = []
+ end
+
+ def to_s
+ "Arduino is on port #{@port} at #{@serial.baud} baudrate"
+ end
+
+ def output(*pinList)
+ sendData(pinList.length)
+
+ if pinList.class==Array
+ @outputPins = pinList
+ pinList.each do |pin|
+ sendPin(pin)
+ end
+ else
+ raise ArgumentError, "Arguments must be a list of pin numbers"
+ end
+ end
+
+ def getState(pin)
+ sendData('2')
+ sendPin(pin)
+ return formatPinState(getData())
+ end
+
+ def setLow(pin)
+ sendData('0')
+ sendPin(pin)
+ end
+
+ def setHigh(pin)
+ sendData('1')
+ sendPin(pin)
+ end
+
+ def analogWrite(pin, value)
+ sendData('3')
+ fullHexValue = value.to_s(base=16)
+ hexValue = hexValue[2..fullHexValue.length]
+ if(hexValue.length==1)
+ sendData('0')
+ else
+ sendData(hexValue[0])
+ end
+ sendData(hexValue[1])
+ end
+
+ def analogRead(pin)
+ sendData('4')
+ sendPin(pin)
+ getData()
+ end
+
+ def turnOff
+ @outputPins.each do |pin|
+ setLow(pin)
+ end
+ end
+
+ def close
+ @serial.close
+ end
+
+ private
+
+ def sendPin(pin)
+ pinInChar = (pin+48)
+ sendData(pinInChar)
+ end
+
+ def sendData(serialData)
+ while true
+ break if getData()=="?"
+ end
+ s = String(serialData.chr)
+ x = @serial.write (s)
+ end
+
+ def getData
+ cleanData = @serial.readlines()
+ cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
+ end
+
+ def formatPinState(pinValue)
+ return true if pinValue=="1"
+ return false #if pinValue=="0"
+ #raise Exception, "Data/Connection error"
+ end
+
+end
@@ -0,0 +1,3 @@
+module Arduino
+ VERSION = "0.3"
+end

0 comments on commit e5119ea

Please sign in to comment.