Skip to content
Browse files

added isHigh?(pin) and isLow?(pin). URGENT TODO: switch to Firmata pr…

…otocol.

Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information...
1 parent 961bdda commit 601c12437f91f8129d0ee260ea138b2d96097143 @HashNuke HashNuke committed Jan 2, 2011
Showing with 39 additions and 18 deletions.
  1. +1 −1 arduino.gemspec
  2. +6 −4 example_blink.rb
  3. +32 −13 lib/arduino.rb
View
2 arduino.gemspec
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = "arduino"
- s.version = "0.3.2"
+ s.version = "0.3.3"
s.date = %q{2011-01-01}
s.platform = Gem::Platform::RUBY
s.authors = ["Akash Manohar"]
View
10 example_blink.rb
@@ -1,15 +1,17 @@
require "./lib/arduino"
#specify the port as an argument
-myBoard = Arduino.new('/dev/ttyUSB0')
+board = Arduino.new('/dev/ttyUSB0')
#declare output pins
-myBoard.output(13)
+board.output(13)
#perform operations
10.times do
- myBoard.setHigh(13)
+ board.setHigh(13)
+ puts "High" if board.getState(13)
sleep(1)
- myBoard.setLow(13)
+ board.setLow(13)
+ puts "Low" if !board.getState(13)
sleep(1)
end
View
45 lib/arduino.rb
@@ -18,6 +18,7 @@ def initialize(port, baudrate=115200)
@serial.sync
@port = port
@outputPins = []
+ @pinStates = {}
end
# Print information about connected board
@@ -37,27 +38,51 @@ def output(*pinList)
else
raise ArgumentError, "Arguments must be a list of pin numbers"
end
- end
-
- # Get state of a digital pin. Returns true if high and false if low.
- def getState(pin)
- sendData('2')
- sendPin(pin)
- return formatPinState(getData())
+ return pinList
end
# Set a pin state to low
def setLow(pin)
+ saveState(pin, false)
sendData('0')
sendPin(pin)
end
+ def isLow?(pin)
+ if !getState(pin)
+ return true
+ else
+ return false
+ end
+ end
+
# Set a pin state to high
def setHigh(pin)
+ saveState(pin, true)
sendData('1')
sendPin(pin)
end
+ def isHigh?(pin)
+ if getState(pin)
+ return true
+ else
+ return false
+ end
+ end
+
+ def saveState(pin, state)
+ @pinStates[pin.to_s] = state
+ end
+
+ # 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]
+ end
+ return false
+ end
+
# Write to an analog pin
def analogWrite(pin, value)
sendData('3')
@@ -109,10 +134,4 @@ 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"
- end
-
end

0 comments on commit 601c124

Please sign in to comment.
Something went wrong with that request. Please try again.