From b0714371c772d3879fd6862d2004d622fb23d8c1 Mon Sep 17 00:00:00 2001 From: Mike Canann Date: Thu, 12 Apr 2012 22:08:58 -0400 Subject: [PATCH 1/5] changed communication to only use binary --- arduino.pde | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/arduino.pde b/arduino.pde index 51cb859..6ea6b09 100644 --- a/arduino.pde +++ b/arduino.pde @@ -26,23 +26,23 @@ void loop() { if(Serial.available()>0) { - cmd = int(Serial.read()) - 48; + cmd = Serial.read(); if(cmd==0) //set digital low { - cmd_arg[0] = int(readData()) - 48; + cmd_arg[0] = readData(); digitalWrite(cmd_arg[0],LOW); } if(cmd==1) //set digital high { - cmd_arg[0] = int(readData()) - 48; + cmd_arg[0] = readData(); digitalWrite(cmd_arg[0],HIGH); } if(cmd==2) //get digital value { - cmd_arg[0] = int(readData()) - 48; + cmd_arg[0] = readData(); cmd_arg[0] = digitalRead(cmd_arg[0]); Serial.println(cmd_arg[0]); } @@ -50,14 +50,14 @@ void loop() if(cmd==3) // set analog value { Serial.println("I'm in the right place"); - cmd_arg[0] = int(readData()) - 48; + cmd_arg[0] = readData(); 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] = readData(); cmd_arg[0] = analogRead(cmd_arg[0]); Serial.println(cmd_arg[0]); } @@ -137,13 +137,15 @@ void askCmd() void setupPins() { + // wait for serial data, to define the outputs while(Serial.available()<1) { - // get number of output pins and convert to int - cmd = int(readData()) - 48; - for(int i=0; i Date: Thu, 12 Apr 2012 22:10:07 -0400 Subject: [PATCH 2/5] changed communication to only use binary --- lib/arduino.rb | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/arduino.rb b/lib/arduino.rb index 2e21d6d..fd2d36a 100644 --- a/lib/arduino.rb +++ b/lib/arduino.rb @@ -1,9 +1,9 @@ -# A ruby library to talk to Arduino without +# A ruby library to talk to Arduino without # having to burn programs repeatedly to the board. # -# Author:: Akash Manohar (akash@akash.im) +# Author:: Akash Manohar (akash@akash.im) # Copyright:: Copyright (c) 2010 Akash Manohar -# License:: MIT License +# License:: MIT License require "serialport" @@ -19,7 +19,7 @@ def initialize(port, baudrate=115200) stop_bits = 1 parity = SerialPort::NONE - @serial = SerialPort.new port, baudrate + @serial = SerialPort.new(port, baudrate, data_bits, stop_bits, parity) @serial.read_timeout = 2 @serial.sync @@ -36,7 +36,7 @@ def to_s # Set output pins. This is a must. def output(*pinList) sendData(pinList.length) - if pinList.class==Array + if(pinList.class == Array) @outputPins = pinList pinList.each do |pin| sendPin(pin) @@ -51,7 +51,7 @@ def output(*pinList) # Set a pin state to low def setLow(pin) saveState(pin, false) - sendData('0') + sendData(0) sendPin(pin) end @@ -66,7 +66,7 @@ def isLow?(pin) # Set a pin state to high def setHigh(pin) saveState(pin, true) - sendData('1') + sendData(1) sendPin(pin) end @@ -92,11 +92,11 @@ def getState(pin) # Write to an analog pin def analogWrite(pin, value) - sendData('3') + sendData(3) fullHexValue = value.to_s(base=16) hexValue = hexValue[2..fullHexValue.length] if(hexValue.length==1) - sendData('0') + sendData(0) else sendData(hexValue[0]) end @@ -105,7 +105,7 @@ def analogWrite(pin, value) # Read from an analog pin def analogRead(pin) - sendData('4') + sendData(4) sendPin(pin) getData() end @@ -120,9 +120,10 @@ def turnOff # close serial connection to connected board def close # stops executing arduino code - @serial.write '5'.chr - # resets the arduino board (not on windows) - @serial.dtr=(0) + #@serial.write '5'[0].chr + @serial.write 5 + # resets the arduino board (not on windows) + @serial.dtr = (0) # close serial connection @serial.close p "closed" @@ -131,15 +132,17 @@ def close private def sendPin(pin) - pinInChar = (pin+48) - sendData(pinInChar) + #pinInChar = (pin + 48) + #sendData(pinInChar) + sendData(pin) end def sendData(serialData) while true - break if getData()=="?" + break if(getData() == "?") end - s = String(serialData.chr) + # send all the data as binary + s = serialData.chr x = @serial.write s end From 85286025922c222b94a90a6a6633188a56d11405 Mon Sep 17 00:00:00 2001 From: Mike Canann Date: Sat, 14 Apr 2012 17:36:07 -0400 Subject: [PATCH 3/5] updated to now check for digital inputs (ie. buttons) --- arduino.pde | 227 +++++++++++++++++++++++++++---------------------- lib/arduino.rb | 68 ++++++++++----- 2 files changed, 170 insertions(+), 125 deletions(-) diff --git a/arduino.pde b/arduino.pde index 6ea6b09..7d10134 100644 --- a/arduino.pde +++ b/arduino.pde @@ -6,148 +6,167 @@ int cmd_arg[2]; int serialStatus = 0; -void setup() { - // connect to the serial port - Serial.begin(115200); - setupPins(); - serialStatus = 1; +void setup() +{ + // connect to the serial port + Serial.begin(115200); + // setup the output and input pins + setupPins(); + serialStatus = 1; } void loop() { - if(serialStatus==0) - { - Serial.flush(); - setupPins(); - } - askCmd(); - - { - if(Serial.available()>0) - { - cmd = Serial.read(); - - if(cmd==0) //set digital low - { - cmd_arg[0] = readData(); - digitalWrite(cmd_arg[0],LOW); - } - - if(cmd==1) //set digital high - { - cmd_arg[0] = readData(); - digitalWrite(cmd_arg[0],HIGH); - } - - if(cmd==2) //get digital value - { - cmd_arg[0] = readData(); - 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] = readData(); - cmd_arg[1] = readHexValue(); - analogWrite(cmd_arg[0],cmd_arg[1]); - } - - if(cmd==4) //read analog value - { - cmd_arg[0] = readData(); - cmd_arg[0] = analogRead(cmd_arg[0]); - Serial.println(cmd_arg[0]); - } - - if(cmd==5) + if(serialStatus == 0) + { + Serial.flush(); + // setup the output and input pins + setupPins(); + serialStatus = 1; + } + askCmd(); + + + + { + if(Serial.available()>0) { - serialStatus = 0; + cmd = Serial.read(); + + if(cmd == 0) //set digital low + { + cmd_arg[0] = readData(); + digitalWrite(cmd_arg[0],LOW); + } + else if(cmd == 1) //set digital high + { + cmd_arg[0] = readData(); + digitalWrite(cmd_arg[0],HIGH); + } + else if(cmd == 2) //get digital value + { + cmd_arg[0] = readData(); + cmd_arg[0] = digitalRead(cmd_arg[0]); + Serial.write(cmd_arg[0]); + } + else if(cmd == 3) // set analog value + { + Serial.println("I'm in the right place"); + cmd_arg[0] = readData(); + cmd_arg[1] = readHexValue(); + analogWrite(cmd_arg[0],cmd_arg[1]); + } + else if(cmd == 4) //read analog value + { + cmd_arg[0] = readData(); + cmd_arg[0] = analogRead(cmd_arg[0]); + Serial.println(cmd_arg[0]); + } + else if(cmd == 5) + { + serialStatus = 0; + } + else + { + // invalid command + } } - } - } + } } char readData() { - askData(); - - while(1) - { - if(Serial.available()>0) - { - return Serial.read(); - } - } + 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; - } - } + int strval[2]; + int converted_str; - askData(); + while(1) + { + if(Serial.available() > 0) + { + strval[0] = convert_hex_to_int(Serial.read()); + break; + } + } - while(1) - { - if(Serial.available()>0) - { - strval[1] = 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; + 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; + return (c <= '9') ? c-'0' : c-'a'+10; } void askData() { - Serial.println("?"); + Serial.println("?"); } void askCmd() { - askData(); - while(Serial.available()<=0) - {} + askData(); + while(Serial.available() <= 0) + {} } void setupPins() { - // wait for serial data, to define the outputs - while(Serial.available()<1) - { - // first byte of number of pins, - // then each byte is a pin to activate - cmd = readData(); - for(int i=0; i < cmd; i++) - { - cmd_arg[0] = readData(); - pinMode(cmd_arg[0], OUTPUT); - } - break; - } + // wait for serial data, to define the outputs + while(Serial.available() < 1) + { + // first byte of number of pins, + // then each byte is a pin to activate + cmd = readData(); + for(int i=0; i < cmd; i++) + { + cmd_arg[0] = readData(); + pinMode(cmd_arg[0], OUTPUT); + } + break; + } + + // wait for serial data, to define the inputs + while(Serial.available() < 1) + { + // first byte of number of pins, + // then each byte is a pin to activate + cmd = readData(); + for(int i=0; i < cmd; i++) + { + cmd_arg[0] = readData(); + pinMode(cmd_arg[0], INPUT); + } + break; + } } diff --git a/lib/arduino.rb b/lib/arduino.rb index fd2d36a..ba043d4 100644 --- a/lib/arduino.rb +++ b/lib/arduino.rb @@ -25,6 +25,7 @@ def initialize(port, baudrate=115200) @port = port @outputPins = [] + @inputPins = [] @pinStates = {} end @@ -33,21 +34,36 @@ def to_s "Arduino is on port #{@port} at #{@serial.baud} baudrate" end - # Set output pins. This is a must. - 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 - puts "return pinlist" - return pinList + # Set output pins. This is a must. + 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 + puts "return output pinlist" + return pinList end + # Set output pins. This is a must. + def input(*pinList) + sendData(pinList.length) + if(pinList.class == Array) + @inputPins = pinList + pinList.each do |pin| + sendPin(pin) + end + else + raise ArgumentError, "Arguments must be a list of pin numbers" + end + puts "return input pinlist" + return pinList + end + # Set a pin state to low def setLow(pin) saveState(pin, false) @@ -84,10 +100,18 @@ def saveState(pin, state) # Get state of a digital pin. Returns true if high and false if low. def getState(pin) - if @pinStates.key?(pin.to_s) - return @pinStates[pin.to_s] + sendData(2) + sendPin(pin) + + while true + rval = @serial.getbyte() + if(rval == 0) + return(false) + end + if(rval == 1) + return(true) + end end - return false end # Write to an analog pin @@ -109,7 +133,7 @@ def analogRead(pin) sendPin(pin) getData() end - + # set all pins to low def turnOff @outputPins.each do |pin| @@ -120,10 +144,14 @@ def turnOff # close serial connection to connected board def close # stops executing arduino code - #@serial.write '5'[0].chr - @serial.write 5 + + # reset the serial communication in the Arduino code + sendData(5) + # resets the arduino board (not on windows) + # Data Terminal Ready @serial.dtr = (0) + # close serial connection @serial.close p "closed" @@ -132,8 +160,6 @@ def close private def sendPin(pin) - #pinInChar = (pin + 48) - #sendData(pinInChar) sendData(pin) end From 02e543672d1f86f964d458b8ce95ab9ed5157e19 Mon Sep 17 00:00:00 2001 From: Mike Canann Date: Sat, 14 Apr 2012 17:37:05 -0400 Subject: [PATCH 4/5] example file with inputs and outputs --- example_io.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 example_io.rb diff --git a/example_io.rb b/example_io.rb new file mode 100644 index 0000000..f1ba3ab --- /dev/null +++ b/example_io.rb @@ -0,0 +1,47 @@ + +require 'rubygems' +require './lib/arduino.rb' +#require 'arduino' + +#specify the port Baudrate is optional and set to 115200 by default +#board = Arduino.new("/dev/ttyAMC0") +board = Arduino.new("/dev/ttyUSB0" ) + +puts "connected" + +#declare output pins +#board.output(12, 13) # multiple outputs +board.output(13) + +puts "output defined" + +#declare input pins +#board.input(10, 11) # multiple inputs +#board.input() # no inputs +board.input(10) + +puts "input defined" + +puts board.to_s + +puts "running... " + +while true + + #puts "checking input" + + # checking a normally open button + # when the button is pressed, turn on the LEDs + if(true == board.isLow?(10)) + #puts "on" + board.setHigh(13) + else + #puts "off" + board.setLow(13) + end + +end + +board.close + + From 5d132a1c2f25e428f8b4b8555ef1e894d59c2dfd Mon Sep 17 00:00:00 2001 From: Mike Canann Date: Sat, 14 Apr 2012 17:38:50 -0400 Subject: [PATCH 5/5] updated to include new mandatory initialization function call --- example_blink.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/example_blink.rb b/example_blink.rb index 5780450..73b3747 100644 --- a/example_blink.rb +++ b/example_blink.rb @@ -6,7 +6,10 @@ board = Arduino.new(port) #declare output pins -board.output(13) +board.output(13) # manditory function call +#declare input pins +board.input() # manditory function call + #perform operations 5.times do