arduino 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/arduino.rb +91 -93
  2. data/lib/arduino/version.rb +1 -1
  3. metadata +2 -2
@@ -1,101 +1,99 @@
1
- module Arduino
2
- require "serialport"
1
+ require "serialport"
3
2
 
4
- class Arduino
3
+ class Arduino
5
4
 
6
- def initialize(port, baudrate=115200)
7
- @serial = SerialPort.new port, baudrate
8
- @serial.sync
9
- @port = port #Cannot get connected port via SerialPort class
10
- @outputPins = []
11
- end
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
- def close
71
- @serial.close
72
- end
12
+ def to_s
13
+ "Arduino is on port #{@port} at #{@serial.baud} baudrate"
14
+ end
73
15
 
74
- private
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
- def getData
90
- cleanData = @serial.readlines()
91
- cleanData = cleanData.join("").gsub("\n","").gsub("\r","")
92
- end
93
-
94
- def formatPinState(pinValue)
95
- return 1 if pinValue=="1"
96
- return 0 #if pinValue=="0"
97
- #raise Exception, "Data/Connection error"
98
- end
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
@@ -1,3 +1,3 @@
1
1
  module Arduino
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- version: "0.1"
7
+ - 2
8
+ version: "0.2"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Akash Manohar