Skip to content

Commit a28494d

Browse files
committed
v0.3 source
Signed-off-by: Akash Manohar J <[email protected]>
1 parent e5119ea commit a28494d

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

lib/arduino.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1+
# The program takes an initial word or phrase from
2+
# the command line (or in the absence of a
3+
# parameter from the first line of standard
4+
# input). In then reads successive words or
5+
# phrases from standard input and reports whether
6+
# they are angrams of the first word.
7+
#
8+
# Author:: Dave Thomas (mailto:[email protected])
9+
# Copyright:: Copyright (c) 2002 The Pragmatic Programmers, LLC
10+
# License:: Distributes under the same terms as Ruby
11+
112
require "serialport"
213

14+
# The main Arduino class.
15+
# Allows managing connection to board and getting & setting pin states.
16+
317
class Arduino
418

19+
# initialize port and baudrate
520
def initialize(port, baudrate=115200)
621
@serial = SerialPort.new port, baudrate
722
@serial.sync
8-
@port = port #Cannot get connected port via SerialPort class
23+
@port = port
924
@outputPins = []
1025
end
1126

27+
# Print information about connected board
1228
def to_s
1329
"Arduino is on port #{@port} at #{@serial.baud} baudrate"
1430
end
15-
31+
32+
# Set output pins. This is a must.
1633
def output(*pinList)
1734
sendData(pinList.length)
1835

@@ -26,22 +43,26 @@ def output(*pinList)
2643
end
2744
end
2845

46+
# Get state of a digital pin. Returns true if high and false if low.
2947
def getState(pin)
3048
sendData('2')
3149
sendPin(pin)
3250
return formatPinState(getData())
3351
end
3452

53+
# Set a pin state to low
3554
def setLow(pin)
3655
sendData('0')
3756
sendPin(pin)
3857
end
39-
58+
59+
# Set a pin state to high
4060
def setHigh(pin)
4161
sendData('1')
4262
sendPin(pin)
4363
end
44-
64+
65+
# Write to an analog pin
4566
def analogWrite(pin, value)
4667
sendData('3')
4768
fullHexValue = value.to_s(base=16)
@@ -54,18 +75,21 @@ def analogWrite(pin, value)
5475
sendData(hexValue[1])
5576
end
5677

78+
# Read from an analog pin
5779
def analogRead(pin)
5880
sendData('4')
5981
sendPin(pin)
6082
getData()
6183
end
6284

85+
# set all pins to low
6386
def turnOff
6487
@outputPins.each do |pin|
6588
setLow(pin)
6689
end
6790
end
6891

92+
# close serial connection to connected board
6993
def close
7094
@serial.close
7195
end
@@ -93,7 +117,6 @@ def getData
93117
def formatPinState(pinValue)
94118
return true if pinValue=="1"
95119
return false #if pinValue=="0"
96-
#raise Exception, "Data/Connection error"
97120
end
98121

99122
end

0 commit comments

Comments
 (0)