rubitlash 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.
Files changed (4) hide show
  1. data/README.markdown +62 -0
  2. data/example/simple.rb +45 -0
  3. data/lib/rubitlash.rb +76 -0
  4. metadata +67 -0
data/README.markdown ADDED
@@ -0,0 +1,62 @@
1
+ INTRODUCTION
2
+ ============
3
+ RuBitlash is a Ruby interface with the wonderful Bitlash library for Arduino.
4
+ [Bitlash](http://bitlash.net/) is an [Arduino](http://arduino.cc) library that allows direct interaction via serial port with the microcontroller.
5
+ Thanks to Bill Roy for writing it!
6
+
7
+ INSTALLATION
8
+ ============
9
+ First of all you have to [buy or own an Arduino](http://arduino.cc), then install on it Bitlash (v0.95a or later) following the instructions on [Bill Roy's site](http://bitlash.net/).
10
+
11
+ Then install RuBitlash gem and start playing with it:
12
+
13
+ > sudo gem install pbosetti-rubitlash --source=http://gems.github.com
14
+
15
+ USAGE
16
+ =====
17
+ I'm still writing few docs, but the library itself is quite short. Look at it to know how to use it.
18
+
19
+ EXAMPLE
20
+ =======
21
+
22
+ require "../lib/rubitlash"
23
+
24
+ # Instantiate the connection. Verbose mode is false
25
+ bl = Bitlash.new("/dev/tty.usbserial-A6004aLr", 57600, false)
26
+
27
+ # talk() writes its argument to Arduino and reports the reply:
28
+ r = bl.talk "print free()"
29
+ puts "Bytes free: #{r}"
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
35
+ sleep 2
36
+
37
+ # Assigning Bitlash variables:
38
+ bl[:f] = bl.free # commands and functions are mapped as methods
39
+ puts "f = #{bl[:f]}"
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 ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Paolo Bosetti on 2009-07-02.
4
+ # Copyright (c) 2009 University of Trento. All rights
5
+ # reserved.
6
+ require "../lib/rubitlash"
7
+
8
+ # Instantiate the connection. Verbose mode is false
9
+ bl = Bitlash.new("/dev/tty.usbserial-A6004aLr", 57600, false)
10
+
11
+ # talk() writes its argument to Arduino and reports the reply:
12
+ r = bl.talk "print free()"
13
+ puts "Bytes free: #{r}"
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
19
+ sleep 2
20
+
21
+ # Assigning Bitlash variables:
22
+ bl[:f] = bl.free # commands and functions are mapped as methods
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
+
45
+ bl.close
data/lib/rubitlash.rb ADDED
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Paolo Bosetti on 2009-07-02.
4
+ # Copyright (c) 2009 University of Trento. All rights
5
+ # reserved.
6
+ require 'rubygems'
7
+ require "serialport"
8
+ require 'expect'
9
+ # sp = SerialPort.new("/dev/tty.usbserial-A6004aLr", 57600, 8, 1, SerialPort::NONE)
10
+ #
11
+ # sp.expect(/>\s/)
12
+ # sp.puts "print free()"
13
+ # sp.expect(/>\s/)
14
+ # sp.puts "a=10"
15
+ # sp.expect(/>\s/)
16
+ # sp.puts "print a"
17
+ # r = sp.expect(/>\s/)[0]
18
+ # r =~ /\n\r(.*)\n\r/
19
+ # p $1
20
+ #
21
+ # sp.close
22
+
23
+ class Bitlash
24
+ PROMPT = ">\s"
25
+ COMMANDS = %w[boot help if ls peep print ps rm run stop while]
26
+ attr_accessor :port_addr
27
+ attr_reader :port, :baud
28
+
29
+ def initialize(port_addr, baud=57600, verbose=false, name="Bitflash##{(rand*1000).to_i}")
30
+ @port_addr, @baud, @name = port_addr, baud, name
31
+ self.verbose = verbose
32
+ self.open
33
+ end
34
+
35
+ def open
36
+ @port = SerialPort.new(@port_addr, 57600, 8, 1, SerialPort::NONE)
37
+ @port.expect(/>\s/)
38
+ end
39
+
40
+ def close; @port.close; end
41
+
42
+ def verbose; $expect_verbose; end
43
+
44
+ def verbose=(bool); $expect_verbose=bool; end
45
+
46
+ def talk(string)
47
+ @port.puts string
48
+ reply = @port.expect(/#{PROMPT}/)[0].split("\n\r")[1...-1]
49
+ return reply.size == 1 ? reply[0] : reply
50
+ end
51
+
52
+ def [](var)
53
+ self.talk "print #{var.to_s}"
54
+ end
55
+
56
+ def []=(var,value)
57
+ self.talk "#{var.to_s}=#{value.to_i}"
58
+ end
59
+
60
+ def macro(name,cmd)
61
+ self.talk "#{name}:=\"#{cmd}\""
62
+ end
63
+
64
+ # def run(macro); self.talk macro.to_s; end
65
+ # def background(macro); self.talk "run #{macro}"; end
66
+
67
+ def method_missing(m, *args)
68
+ if COMMANDS.include? m.to_s
69
+ cmd = args.size > 0 ? "#{m.to_s} #{args * ','}" : "#{m.to_s}"
70
+ else
71
+ cmd = args.size > 0 ? "print #{m.to_s}(#{args * ','})" : "print #{m.to_s}"
72
+ end
73
+ self.talk(cmd)
74
+ end
75
+
76
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubitlash
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Paolo Bosetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-03 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-serialport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.0
24
+ version:
25
+ description: Rubitlash is a Ruby interface with Bitlash for Arduino
26
+ email: paolo.bosetti@me.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.markdown
35
+ - lib/rubitlash.rb
36
+ - example/simple.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/pbosetti/rubitlash
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --inline-source
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: rubitlash
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: Rubitlash is a Ruby interface with Bitlash for Arduino
66
+ test_files: []
67
+