arduino_mega 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arduino_mega.rb +170 -0
- metadata +56 -0
data/lib/arduino_mega.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# A ruby library to talk to Arduino without
|
4
|
+
# having to burn programs repeatedly to the board.
|
5
|
+
#
|
6
|
+
# Author:: Akash Manohar (akash@akash.im)
|
7
|
+
# Copyright:: Copyright (c) 2010 Akash Manohar
|
8
|
+
# License:: MIT License
|
9
|
+
|
10
|
+
require "serialport"
|
11
|
+
|
12
|
+
# The main Arduino class.
|
13
|
+
# Allows managing connection to board and getting & setting pin states.
|
14
|
+
|
15
|
+
class Arduino
|
16
|
+
|
17
|
+
# initialize port and baudrate
|
18
|
+
def initialize(port, baudrate=115200)
|
19
|
+
puts "initialized"
|
20
|
+
data_bits = 8
|
21
|
+
stop_bits = 1
|
22
|
+
parity = SerialPort::NONE
|
23
|
+
|
24
|
+
@serial = SerialPort.new port, baudrate
|
25
|
+
@serial.read_timeout = 2
|
26
|
+
@serial.sync
|
27
|
+
|
28
|
+
@port = port
|
29
|
+
@output_pins = []
|
30
|
+
@pin_states = {}
|
31
|
+
end
|
32
|
+
|
33
|
+
# Print information about connected board
|
34
|
+
def to_s
|
35
|
+
"Arduino is on port #{@port} at #{@serial.baud} baudrate"
|
36
|
+
end
|
37
|
+
|
38
|
+
# Set output pins. This is a must.
|
39
|
+
def output(*pin_list)
|
40
|
+
send_data(pin_list.length)
|
41
|
+
if pin_list.class==Array
|
42
|
+
@output_pins = pin_list
|
43
|
+
pin_list.each do |pin|
|
44
|
+
send_pin(pin)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
raise ArgumentError, "Arguments must be a list of pin numbers"
|
48
|
+
end
|
49
|
+
puts "return pinlist"
|
50
|
+
return pin_list
|
51
|
+
end
|
52
|
+
|
53
|
+
# Set a pin state to low
|
54
|
+
def set_low(pin)
|
55
|
+
save_state(pin, false)
|
56
|
+
send_data('0')
|
57
|
+
send_pin(pin)
|
58
|
+
end
|
59
|
+
|
60
|
+
def is_low?(pin)
|
61
|
+
if !get_state(pin)
|
62
|
+
return true
|
63
|
+
else
|
64
|
+
return false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Set a pin state to high
|
69
|
+
def set_high(pin)
|
70
|
+
save_state(pin, true)
|
71
|
+
send_data('1')
|
72
|
+
send_pin(pin)
|
73
|
+
end
|
74
|
+
|
75
|
+
def is_high?(pin)
|
76
|
+
if get_state(pin)
|
77
|
+
return true
|
78
|
+
else
|
79
|
+
return false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def save_state(pin, state)
|
84
|
+
@pin_states[pin.to_s] = state
|
85
|
+
end
|
86
|
+
|
87
|
+
# Get state of a digital pin. Returns true if high and false if low.
|
88
|
+
def get_state(pin)
|
89
|
+
if @pin_states.key?(pin.to_s)
|
90
|
+
return @pin_states[pin.to_s]
|
91
|
+
end
|
92
|
+
return false
|
93
|
+
end
|
94
|
+
|
95
|
+
# Write to an analog pin
|
96
|
+
def analog_write(pin, value)
|
97
|
+
send_data('3')
|
98
|
+
full_hex_value = value.to_s(base=16)
|
99
|
+
hex_value = hex_value[2..full_hex_value.length]
|
100
|
+
if(hex_value.length==1)
|
101
|
+
send_data('0')
|
102
|
+
else
|
103
|
+
send_data(hex_value[0])
|
104
|
+
end
|
105
|
+
send_data(hex_value[1])
|
106
|
+
end
|
107
|
+
|
108
|
+
# Read from an analog pin
|
109
|
+
def analog_read(pin)
|
110
|
+
send_data('4')
|
111
|
+
send_pin(pin)
|
112
|
+
get_data()
|
113
|
+
end
|
114
|
+
|
115
|
+
# set all pins to low
|
116
|
+
def turn_off
|
117
|
+
@output_pins.each do |pin|
|
118
|
+
set_low(pin)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# close serial connection to connected board
|
123
|
+
def close
|
124
|
+
# stops executing arduino code
|
125
|
+
@serial.write '5'.chr
|
126
|
+
# resets the arduino board (not on windows)
|
127
|
+
@serial.dtr=(0)
|
128
|
+
# close serial connection
|
129
|
+
@serial.close
|
130
|
+
p "closed"
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def send_pin(pin)
|
136
|
+
pin_in_char = (pin+48)
|
137
|
+
send_data(pin_in_char)
|
138
|
+
end
|
139
|
+
|
140
|
+
def send_data(serial_data)
|
141
|
+
while true
|
142
|
+
break if get_data()=="?"
|
143
|
+
end
|
144
|
+
s = String(serial_data.chr)
|
145
|
+
x = @serial.write s
|
146
|
+
end
|
147
|
+
|
148
|
+
def get_data
|
149
|
+
clean_data = @serial.readlines()
|
150
|
+
clean_data = clean_data.join("").gsub("\n","").gsub("\r","")
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
# file: arduino_mega.rb
|
155
|
+
|
156
|
+
class ArduinoMega < Arduino
|
157
|
+
|
158
|
+
attr_accessor :p13h
|
159
|
+
|
160
|
+
def initialize(args=[])
|
161
|
+
super(*args)
|
162
|
+
output(13) # declare output pins
|
163
|
+
end
|
164
|
+
|
165
|
+
def p13h=(bflag)
|
166
|
+
send (bflag == true ? :set_high : :set_low), 13
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arduino_mega
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Robertson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-04-24 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email:
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/arduino_mega.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage:
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.5.2
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: arduino_mega
|
55
|
+
test_files: []
|
56
|
+
|