pbosetti-rubitlash 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +36 -7
- data/example/simple.rb +32 -4
- data/lib/rubitlash.rb +11 -4
- metadata +1 -1
data/README.markdown
CHANGED
@@ -19,15 +19,44 @@ I'm still writing few docs, but the library itself is quite short. Look at it to
|
|
19
19
|
EXAMPLE
|
20
20
|
=======
|
21
21
|
|
22
|
-
require "rubitlash"
|
23
|
-
|
22
|
+
require "../lib/rubitlash"
|
23
|
+
|
24
|
+
# Instantiate the connection. Verbose mode is false
|
24
25
|
bl = Bitlash.new("/dev/tty.usbserial-A6004aLr", 57600, false)
|
26
|
+
|
27
|
+
# talk() writes its argument to Arduino and reports the reply:
|
25
28
|
r = bl.talk "print free()"
|
26
29
|
puts "Bytes free: #{r}"
|
27
|
-
|
28
|
-
|
29
|
-
bl
|
30
|
+
|
31
|
+
# variables are accessed as keys of the Bitlash instance:
|
32
|
+
bl[:a] = 2 # writing
|
33
|
+
puts "a = #{bl[:a]}" # reading
|
34
|
+
bl.talk "d12=1" # low-level
|
30
35
|
sleep 2
|
31
|
-
|
36
|
+
|
37
|
+
# Assigning Bitlash variables:
|
38
|
+
bl[:f] = bl.free # commands and functions are mapped as methods
|
32
39
|
puts "f = #{bl[:f]}"
|
33
|
-
|
40
|
+
|
41
|
+
# Macro example
|
42
|
+
bl.macro "toggle13", "d13=!d13" # define a new macro
|
43
|
+
puts "\nMacros:"
|
44
|
+
bl.ls.each_with_index {|m,i| puts "#{i}:\t#{m}"}
|
45
|
+
bl.toggle13 # calls the new macro
|
46
|
+
sleep 1
|
47
|
+
# but also:
|
48
|
+
# bl.run :toggle13 # this runs in background on Arduino
|
49
|
+
# sleep 1
|
50
|
+
|
51
|
+
# Some play
|
52
|
+
puts "\nStarting cyclic fade"
|
53
|
+
bl.pinmode(11,1)
|
54
|
+
t = t0 = Time.now.to_f
|
55
|
+
while (t - t0) < 5 do
|
56
|
+
t = Time.now.to_f
|
57
|
+
bl[:a11] = ((1.0 - Math::cos((t - t0)*Math::PI))*100).to_i # 2 Hz cycle
|
58
|
+
sleep 0.01
|
59
|
+
end
|
60
|
+
|
61
|
+
bl.close
|
62
|
+
|
data/example/simple.rb
CHANGED
@@ -5,13 +5,41 @@
|
|
5
5
|
# reserved.
|
6
6
|
require "../lib/rubitlash"
|
7
7
|
|
8
|
+
# Instantiate the connection. Verbose mode is false
|
8
9
|
bl = Bitlash.new("/dev/tty.usbserial-A6004aLr", 57600, false)
|
10
|
+
|
11
|
+
# talk() writes its argument to Arduino and reports the reply:
|
9
12
|
r = bl.talk "print free()"
|
10
13
|
puts "Bytes free: #{r}"
|
11
|
-
|
12
|
-
|
13
|
-
bl
|
14
|
+
|
15
|
+
# variables are accessed as keys of the Bitlash instance:
|
16
|
+
bl[:a] = 2 # writing
|
17
|
+
puts "a = #{bl[:a]}" # reading
|
18
|
+
bl.talk "d12=1" # low-level
|
14
19
|
sleep 2
|
15
|
-
|
20
|
+
|
21
|
+
# Assigning Bitlash variables:
|
22
|
+
bl[:f] = bl.free # commands and functions are mapped as methods
|
16
23
|
puts "f = #{bl[:f]}"
|
24
|
+
|
25
|
+
# Macro example
|
26
|
+
bl.macro "toggle13", "d13=!d13" # define a new macro
|
27
|
+
puts "\nMacros:"
|
28
|
+
bl.ls.each_with_index {|m,i| puts "#{i}:\t#{m}"}
|
29
|
+
bl.toggle13 # calls the new macro
|
30
|
+
sleep 1
|
31
|
+
# but also:
|
32
|
+
# bl.run :toggle13 # this runs in background on Arduino
|
33
|
+
# sleep 1
|
34
|
+
|
35
|
+
# Some play
|
36
|
+
puts "\nStarting cyclic fade"
|
37
|
+
bl.pinmode(11,1)
|
38
|
+
t = t0 = Time.now.to_f
|
39
|
+
while (t - t0) < 5 do
|
40
|
+
t = Time.now.to_f
|
41
|
+
bl[:a11] = ((1.0 - Math::cos((t - t0)*Math::PI))*100).to_i # 2 Hz cycle
|
42
|
+
sleep 0.01
|
43
|
+
end
|
44
|
+
|
17
45
|
bl.close
|
data/lib/rubitlash.rb
CHANGED
@@ -22,6 +22,7 @@ require 'expect'
|
|
22
22
|
|
23
23
|
class Bitlash
|
24
24
|
PROMPT = ">\s"
|
25
|
+
COMMANDS = %w[boot help if ls peep print ps rm run stop while]
|
25
26
|
attr_accessor :port_addr
|
26
27
|
attr_reader :port, :baud
|
27
28
|
def initialize(port_addr, baud=57600, verbose=false, name="Bitflash##{(rand*1000).to_i}")
|
@@ -43,9 +44,8 @@ class Bitlash
|
|
43
44
|
|
44
45
|
def talk(string)
|
45
46
|
@port.puts string
|
46
|
-
reply = @port.expect(/#{PROMPT}/)[0]
|
47
|
-
reply
|
48
|
-
return $2
|
47
|
+
reply = @port.expect(/#{PROMPT}/)[0].split("\n\r")[1...-1]
|
48
|
+
return reply.size == 1 ? reply[0] : reply
|
49
49
|
end
|
50
50
|
|
51
51
|
def [](var)
|
@@ -60,8 +60,15 @@ class Bitlash
|
|
60
60
|
self.talk "#{name}:=\"#{cmd}\""
|
61
61
|
end
|
62
62
|
|
63
|
+
# def run(macro); self.talk macro.to_s; end
|
64
|
+
# def background(macro); self.talk "run #{macro}"; end
|
65
|
+
|
63
66
|
def method_missing(m, *args)
|
64
|
-
|
67
|
+
if COMMANDS.include? m.to_s
|
68
|
+
cmd = args.size > 0 ? "#{m.to_s} #{args * ','}" : "#{m.to_s}"
|
69
|
+
else
|
70
|
+
cmd = args.size > 0 ? "print #{m.to_s}(#{args * ','})" : "print #{m.to_s}"
|
71
|
+
end
|
65
72
|
self.talk(cmd)
|
66
73
|
end
|
67
74
|
|