Class: Arduino
- Inherits:
-
Object
- Object
- Arduino
- Defined in:
- lib/arduino.rb
Overview
The main Arduino class. Allows managing connection to board and getting & setting pin states.
Instance Method Summary collapse
-
#analogRead(pin) ⇒ Object
Read from an analog pin.
-
#analogWrite(pin, value) ⇒ Object
Write to an analog pin.
-
#close ⇒ Object
close serial connection to connected board.
-
#getState(pin) ⇒ Object
Get state of a digital pin.
-
#initialize(port, baudrate = 115200) ⇒ Arduino
constructor
initialize port and baudrate.
- #isHigh?(pin) ⇒ Boolean
- #isLow?(pin) ⇒ Boolean
-
#output(*pinList) ⇒ Object
Set output pins.
- #saveState(pin, state) ⇒ Object
-
#setHigh(pin) ⇒ Object
Set a pin state to high.
-
#setLow(pin) ⇒ Object
Set a pin state to low.
-
#to_s ⇒ Object
Print information about connected board.
-
#turnOff ⇒ Object
set all pins to low.
Constructor Details
#initialize(port, baudrate = 115200) ⇒ Arduino
initialize port and baudrate
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/arduino.rb', line 16 def initialize(port, baudrate=115200) puts "initialized" data_bits = 8 stop_bits = 1 parity = SerialPort::NONE @serial = SerialPort.new port, baudrate @serial.read_timeout = 2 @serial.sync @port = port @outputPins = [] @pinStates = {} end |
Instance Method Details
#analogRead(pin) ⇒ Object
Read from an analog pin
107 108 109 110 111 |
# File 'lib/arduino.rb', line 107 def analogRead(pin) sendData('4') sendPin(pin) getData() end |
#analogWrite(pin, value) ⇒ Object
Write to an analog pin
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/arduino.rb', line 94 def analogWrite(pin, value) sendData('3') fullHexValue = value.to_s(base=16) hexValue = hexValue[2..fullHexValue.length] if(hexValue.length==1) sendData('0') else sendData(hexValue[0]) end sendData(hexValue[1]) end |
#close ⇒ Object
close serial connection to connected board
121 122 123 124 125 126 127 128 129 |
# File 'lib/arduino.rb', line 121 def close # stops executing arduino code @serial.write '5'.chr # resets the arduino board (not on windows) @serial.dtr=(0) # close serial connection @serial.close p "closed" end |
#getState(pin) ⇒ Object
Get state of a digital pin. Returns true if high and false if low.
86 87 88 89 90 91 |
# File 'lib/arduino.rb', line 86 def getState(pin) if @pinStates.key?(pin.to_s) return @pinStates[pin.to_s] end return false end |
#isHigh?(pin) ⇒ Boolean
73 74 75 76 77 78 79 |
# File 'lib/arduino.rb', line 73 def isHigh?(pin) if getState(pin) return true else return false end end |
#isLow?(pin) ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/arduino.rb', line 58 def isLow?(pin) if !getState(pin) return true else return false end end |
#output(*pinList) ⇒ Object
Set output pins. This is a must.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/arduino.rb', line 37 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 end |
#saveState(pin, state) ⇒ Object
81 82 83 |
# File 'lib/arduino.rb', line 81 def saveState(pin, state) @pinStates[pin.to_s] = state end |
#setHigh(pin) ⇒ Object
Set a pin state to high
67 68 69 70 71 |
# File 'lib/arduino.rb', line 67 def setHigh(pin) saveState(pin, true) sendData('1') sendPin(pin) end |
#setLow(pin) ⇒ Object
Set a pin state to low
52 53 54 55 56 |
# File 'lib/arduino.rb', line 52 def setLow(pin) saveState(pin, false) sendData('0') sendPin(pin) end |
#to_s ⇒ Object
Print information about connected board
32 33 34 |
# File 'lib/arduino.rb', line 32 def to_s "Arduino is on port #{@port} at #{@serial.baud} baudrate" end |
#turnOff ⇒ Object
set all pins to low
114 115 116 117 118 |
# File 'lib/arduino.rb', line 114 def turnOff @outputPins.each do |pin| setLow(pin) end end |