arduino 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/arduino.rb +91 -93
- data/lib/arduino/version.rb +1 -1
- metadata +2 -2
data/lib/arduino.rb
CHANGED
|
@@ -1,101 +1,99 @@
|
|
|
1
|
-
|
|
2
|
-
require "serialport"
|
|
1
|
+
require "serialport"
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
class Arduino
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def to_s
|
|
14
|
-
"Arduino is on port #{@port} at #{@serial.baud} baudrate"
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def output(*pinList)
|
|
18
|
-
sendData(pinList.length)
|
|
19
|
-
|
|
20
|
-
if pinList.class==Array
|
|
21
|
-
@outputPins = pinList
|
|
22
|
-
pinList.each do |pin|
|
|
23
|
-
sendPin(pin)
|
|
24
|
-
end
|
|
25
|
-
else
|
|
26
|
-
raise ArgumentError, "Arguments must be a list of pin numbers"
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def getState(pin)
|
|
31
|
-
sendData('2')
|
|
32
|
-
sendPin(pin)
|
|
33
|
-
return formatPinState(getData())
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def setLow(pin)
|
|
37
|
-
sendData('0')
|
|
38
|
-
sendPin(pin)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def setHigh(pin)
|
|
42
|
-
sendData('1')
|
|
43
|
-
sendPin(pin)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def analogWrite(pin, value)
|
|
47
|
-
sendData('3')
|
|
48
|
-
fullHexValue = value.to_s(base=16)
|
|
49
|
-
hexValue = hexValue[2..fullHexValue.length]
|
|
50
|
-
if(hexValue.length==1)
|
|
51
|
-
sendData('0')
|
|
52
|
-
else
|
|
53
|
-
sendData(hexValue[0])
|
|
54
|
-
end
|
|
55
|
-
sendData(hexValue[1])
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def analogRead(pin)
|
|
59
|
-
sendData('4')
|
|
60
|
-
sendPin(pin)
|
|
61
|
-
getData()
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def turnOff
|
|
65
|
-
@outputPins.each do |pin|
|
|
66
|
-
setLow(pin)
|
|
67
|
-
end
|
|
68
|
-
end
|
|
5
|
+
def initialize(port, baudrate=115200)
|
|
6
|
+
@serial = SerialPort.new port, baudrate
|
|
7
|
+
@serial.sync
|
|
8
|
+
@port = port #Cannot get connected port via SerialPort class
|
|
9
|
+
@outputPins = []
|
|
10
|
+
end
|
|
69
11
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
12
|
+
def to_s
|
|
13
|
+
"Arduino is on port #{@port} at #{@serial.baud} baudrate"
|
|
14
|
+
end
|
|
73
15
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def sendPin(pin)
|
|
77
|
-
pinInChar = (pin+48)
|
|
78
|
-
sendData(pinInChar)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def sendData(serialData)
|
|
82
|
-
while true
|
|
83
|
-
break if getData()=="?"
|
|
84
|
-
end
|
|
85
|
-
s = String(serialData.chr)
|
|
86
|
-
x = @serial.write (s)
|
|
87
|
-
end
|
|
16
|
+
def output(*pinList)
|
|
17
|
+
sendData(pinList.length)
|
|
88
18
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
19
|
+
if pinList.class==Array
|
|
20
|
+
@outputPins = pinList
|
|
21
|
+
pinList.each do |pin|
|
|
22
|
+
sendPin(pin)
|
|
23
|
+
end
|
|
24
|
+
else
|
|
25
|
+
raise ArgumentError, "Arguments must be a list of pin numbers"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def getState(pin)
|
|
30
|
+
sendData('2')
|
|
31
|
+
sendPin(pin)
|
|
32
|
+
return formatPinState(getData())
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def setLow(pin)
|
|
36
|
+
sendData('0')
|
|
37
|
+
sendPin(pin)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def setHigh(pin)
|
|
41
|
+
sendData('1')
|
|
42
|
+
sendPin(pin)
|
|
43
|
+
end
|
|
99
44
|
|
|
45
|
+
def analogWrite(pin, value)
|
|
46
|
+
sendData('3')
|
|
47
|
+
fullHexValue = value.to_s(base=16)
|
|
48
|
+
hexValue = hexValue[2..fullHexValue.length]
|
|
49
|
+
if(hexValue.length==1)
|
|
50
|
+
sendData('0')
|
|
51
|
+
else
|
|
52
|
+
sendData(hexValue[0])
|
|
53
|
+
end
|
|
54
|
+
sendData(hexValue[1])
|
|
100
55
|
end
|
|
56
|
+
|
|
57
|
+
def analogRead(pin)
|
|
58
|
+
sendData('4')
|
|
59
|
+
sendPin(pin)
|
|
60
|
+
getData()
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def turnOff
|
|
64
|
+
@outputPins.each do |pin|
|
|
65
|
+
setLow(pin)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def close
|
|
70
|
+
@serial.close
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def sendPin(pin)
|
|
76
|
+
pinInChar = (pin+48)
|
|
77
|
+
sendData(pinInChar)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def sendData(serialData)
|
|
81
|
+
while true
|
|
82
|
+
break if getData()=="?"
|
|
83
|
+
end
|
|
84
|
+
s = String(serialData.chr)
|
|
85
|
+
x = @serial.write (s)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def getData
|
|
89
|
+
cleanData = @serial.readlines()
|
|
90
|
+
cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def formatPinState(pinValue)
|
|
94
|
+
return 1 if pinValue=="1"
|
|
95
|
+
return 0 #if pinValue=="0"
|
|
96
|
+
#raise Exception, "Data/Connection error"
|
|
97
|
+
end
|
|
98
|
+
|
|
101
99
|
end
|
data/lib/arduino/version.rb
CHANGED