Skip to content

Commit 601c124

Browse files
committed
added isHigh?(pin) and isLow?(pin). URGENT TODO: switch to Firmata protocol.
Signed-off-by: Akash Manohar J <[email protected]>
1 parent 961bdda commit 601c124

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

arduino.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
33

44
Gem::Specification.new do |s|
55
s.name = "arduino"
6-
s.version = "0.3.2"
6+
s.version = "0.3.3"
77
s.date = %q{2011-01-01}
88
s.platform = Gem::Platform::RUBY
99
s.authors = ["Akash Manohar"]

example_blink.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
require "./lib/arduino"
22

33
#specify the port as an argument
4-
myBoard = Arduino.new('/dev/ttyUSB0')
4+
board = Arduino.new('/dev/ttyUSB0')
55

66
#declare output pins
7-
myBoard.output(13)
7+
board.output(13)
88

99
#perform operations
1010
10.times do
11-
myBoard.setHigh(13)
11+
board.setHigh(13)
12+
puts "High" if board.getState(13)
1213
sleep(1)
13-
myBoard.setLow(13)
14+
board.setLow(13)
15+
puts "Low" if !board.getState(13)
1416
sleep(1)
1517
end

lib/arduino.rb

+32-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def initialize(port, baudrate=115200)
1818
@serial.sync
1919
@port = port
2020
@outputPins = []
21+
@pinStates = {}
2122
end
2223

2324
# Print information about connected board
@@ -37,27 +38,51 @@ def output(*pinList)
3738
else
3839
raise ArgumentError, "Arguments must be a list of pin numbers"
3940
end
40-
end
41-
42-
# Get state of a digital pin. Returns true if high and false if low.
43-
def getState(pin)
44-
sendData('2')
45-
sendPin(pin)
46-
return formatPinState(getData())
41+
return pinList
4742
end
4843

4944
# Set a pin state to low
5045
def setLow(pin)
46+
saveState(pin, false)
5147
sendData('0')
5248
sendPin(pin)
5349
end
5450

51+
def isLow?(pin)
52+
if !getState(pin)
53+
return true
54+
else
55+
return false
56+
end
57+
end
58+
5559
# Set a pin state to high
5660
def setHigh(pin)
61+
saveState(pin, true)
5762
sendData('1')
5863
sendPin(pin)
5964
end
6065

66+
def isHigh?(pin)
67+
if getState(pin)
68+
return true
69+
else
70+
return false
71+
end
72+
end
73+
74+
def saveState(pin, state)
75+
@pinStates[pin.to_s] = state
76+
end
77+
78+
# Get state of a digital pin. Returns true if high and false if low.
79+
def getState(pin)
80+
if @pinStates.key?(pin.to_s)
81+
return @pinStates[pin.to_s]
82+
end
83+
return false
84+
end
85+
6186
# Write to an analog pin
6287
def analogWrite(pin, value)
6388
sendData('3')
@@ -109,10 +134,4 @@ def getData
109134
cleanData = @serial.readlines()
110135
cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
111136
end
112-
113-
def formatPinState(pinValue)
114-
return true if pinValue=="1"
115-
return false #if pinValue=="0"
116-
end
117-
118137
end

0 commit comments

Comments
 (0)