├── .gitignore ├── Gemfile ├── Gemfile.lock ├── MIT-license.txt ├── README.md ├── Rakefile ├── arduino.gemspec ├── arduino.pde ├── example_blink.rb └── lib └── arduino.rb /.gitignore: -------------------------------------------------------------------------------- 1 | pkg/* 2 | *.gem 3 | .bundle 4 | *.lock 5 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in arduino.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | arduino (0.3.1) 5 | serialport (>= 1.0.4) 6 | 7 | GEM 8 | remote: http://rubygems.org/ 9 | specs: 10 | serialport (1.0.4) 11 | 12 | PLATFORMS 13 | ruby 14 | 15 | DEPENDENCIES 16 | arduino! 17 | serialport (>= 1.0.4) 18 | -------------------------------------------------------------------------------- /MIT-license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2010 Akash Manohar J 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. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino ruby gem 2 | 3 | This gem 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](https://github.com/HashNuke/arduino/raw/master/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 | You don't need to declare input pins since digital pins are input by default according to the page on the Arduino site - 28 | 29 | **Digital I/O** 30 | 31 | 1. `Arduino.setHigh(pin)` 32 | 2. `Arduino.setLow(pin)` 33 | 3. `Arduino.getState(pin)` - returns `true` if pin state is high, else it returns `false` 34 | 35 | **Analog I/O** 36 | 37 | 1. `Arduino.analogRead(pin)` - returns the analog value 38 | 2. `Arduino.analogRead(pin, value)` - sets the analog value 39 | 40 | **Misc** 41 | 42 | 1.) `Arduino.turnOff` - sets all the pins to low state 43 | 44 | 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. 45 | 46 | ## Usage example 47 | 48 | # This is the blink program. 49 | 50 | require "arduino" 51 | 52 | #specify the port Baudrate is optional and set to 115200 by default 53 | board = Arduino.new("/dev/ttyUSB1") 54 | 55 | #declare output pins 56 | board.output(13) 57 | 58 | #perform operations 59 | 10.times do 60 | board.setHigh(13) 61 | sleep(1) 62 | board.setLow(13) 63 | sleep(1) 64 | end 65 | 66 | # Developed for the love of creating stuff by 67 | > © 2010 Akash Manohar 68 | > under the MIT License 69 | 70 | # Credits 71 | 72 | Thanks to the following people: 73 | 74 | * [@unsymbol](http://github.com/unsymbol) - for fixing the Ubuntu reset problem 75 | 76 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | -------------------------------------------------------------------------------- /arduino.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | 4 | Gem::Specification.new do |s| 5 | s.name = "arduino" 6 | s.version = "0.4" 7 | s.date = %q{2011-01-01} 8 | s.platform = Gem::Platform::RUBY 9 | s.authors = ["Akash Manohar"] 10 | s.email = ["akash@akash.im"] 11 | s.homepage = "http://akash.im/arduino-ruby" 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 | s.post_install_message = %q{ 15 | ============================================== 16 | Thank you for installing the arduino gem 17 | ---------------------------------------------- 18 | 19 | Load the arduino.pde program from http://ln.akash.im/arduino.pde to your board before using this library 20 | 21 | Source: https://github.com/HashNuke/arduino/ 22 | 23 | -- 24 | @HashNuke 25 | (http://akash.im) 26 | ============================================== 27 | } 28 | s.rubyforge_project = "arduino" 29 | 30 | s.files = `git ls-files`.split("\n") 31 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 32 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 33 | s.require_paths = ["lib"] 34 | s.add_runtime_dependency(%q, [">= 1.0.4"]) 35 | end 36 | -------------------------------------------------------------------------------- /arduino.pde: -------------------------------------------------------------------------------- 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