monome_serial 1.0.0

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.
@@ -0,0 +1,16 @@
1
+ module MonomeSerial
2
+ module SerialCommunicator
3
+ class Communicator
4
+ def write(strings)
5
+ raise ArgumentError, "The collection of stringified bytes passed into SerialCommunicator#write needs to respond to #[] and #size" unless strings.respond_to?("[]") && strings.respond_to?("size")
6
+ end
7
+
8
+ def read
9
+ end
10
+
11
+ def real?
12
+ raise "please implement me"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module MonomeSerial
2
+ module SerialCommunicator
3
+ #Dummy class with no actual serial connection (so tests can execute
4
+ #in other Ruby environments)
5
+ class DummyCommunicator < Communicator
6
+ def initialize
7
+ end
8
+
9
+ def read
10
+ #super
11
+ [:keydown, 1, 1]
12
+ end
13
+
14
+ def write(strings)
15
+ end
16
+
17
+ def real?
18
+ false
19
+ end
20
+
21
+ def model
22
+ "256"
23
+ end
24
+
25
+ def serial
26
+ "007"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,97 @@
1
+ module MonomeSerial
2
+ module SerialCommunicator
3
+ class RealCommunicator < Communicator
4
+ attr_reader :model, :serial
5
+ def initialize(tty_path)
6
+ #make sure tty_path exists (such as "/dev/tty.usbserial-m256-203")
7
+ raise ArgumentError, "path to tty IO file does not exist" unless File.exists?(tty_path)
8
+
9
+ #match = tty_path.match /m(\d+)-(\d+)/
10
+ #@model = match[1]
11
+
12
+ #pull out this Monome's individual serial number
13
+ #@serial = match[2]
14
+
15
+ #Open up the virtual serial port
16
+ @dev = dev_open(tty_path)
17
+
18
+ #bless the file with Termios powers
19
+ @dev.extend Termios
20
+
21
+ #create a new Termios struct
22
+ newtio = Termios::new_termios()
23
+ newtio.iflag = Termios::IGNPAR
24
+ newtio.oflag = 0
25
+ newtio.cflag = Termios::CLOCAL
26
+ newtio.lflag = 0
27
+ newtio.cc[Termios::VTIME] = 0
28
+ newtio.cc[Termios::VMIN] = 1
29
+ newtio.ispeed = Termios::B115200
30
+ newtio.ospeed = Termios::B115200
31
+
32
+ #discard data on both the input and output queues
33
+ flush
34
+
35
+ #set the termios struct in the OS with the newly
36
+ #defined info in newtio
37
+ @dev.tcsetattr(Termios::TCSANOW, newtio)
38
+ end
39
+
40
+ def real?
41
+ true
42
+ end
43
+
44
+ def read
45
+ #read action message
46
+ action_bin = @dev.read(1)
47
+
48
+ #interpret action:
49
+ #0000000 => keydown
50
+ #0001000 => keyup
51
+ action = action_bin.unpack('B8').first[3] == "1" ? :keyup : :keydown
52
+
53
+ #read position message
54
+ pos_bin = @dev.read(1)
55
+
56
+ #convert it to a binary string
57
+ pos_bin_s = pos_bin.unpack('B8').first
58
+
59
+ #interpret position:
60
+ #xxxxyyyy => x position and y position in base 2
61
+ x = Integer("0b" + pos_bin_s[0..3])
62
+ y = Integer("0b" + pos_bin_s[4..8])
63
+
64
+ return action, x, y
65
+ end
66
+
67
+ def write(strings)
68
+
69
+ super
70
+
71
+ #convert integer params to 8bit binary representation
72
+ bin_strings = strings.map{|s| s.to_s}
73
+
74
+ case bin_strings.size
75
+ when 1 then @dev.write(bin_strings.pack('B8'))
76
+ when 2 then @dev.write(bin_strings.pack('B8B8'))
77
+ when 3 then @dev.write(bin_strings.pack('B8B8B8'))
78
+ when 9 then @dev.write(bin_strings.pack('B8B8B8B8B8B8B8B8B8'))
79
+ else raise ArgumentError, "SerialCommunicator#write only supports sending one, two, three or nine bytes at a time. You tried to send #{bin_strings.size} bytes."
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def flush
86
+ @dev.tcflush(Termios::TCIOFLUSH)
87
+ end
88
+
89
+ def dev_open(path)
90
+ dev = open(path , File::RDWR | File::NONBLOCK)
91
+ mode = dev.fcntl(Fcntl::F_GETFL, 0)
92
+ dev.fcntl(Fcntl::F_SETFL, mode & ~File::NONBLOCK)
93
+ return dev
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{monome_serial}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sam Aaron"]
12
+ s.date = %q{2009-12-02}
13
+ s.description = %q{Communicate directly with your monome with this handy Ruby library.}
14
+ s.email = %q{samaaron@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "docs/CONTRIBUTORS",
24
+ "docs/LICENSE",
25
+ "docs/TODO",
26
+ "lib/monome_serial.rb",
27
+ "lib/monome_serial/examples/toggle.rb",
28
+ "lib/monome_serial/monome_communicator.rb",
29
+ "lib/monome_serial/serial_communicator.rb",
30
+ "lib/monome_serial/serial_communicator/binary_patterns/fourtyh.rb",
31
+ "lib/monome_serial/serial_communicator/binary_patterns/series.rb",
32
+ "lib/monome_serial/serial_communicator/communicator.rb",
33
+ "lib/monome_serial/serial_communicator/dummy_communicator.rb",
34
+ "lib/monome_serial/serial_communicator/real_communicator.rb",
35
+ "monome_serial.gemspec",
36
+ "spec/spec.opts",
37
+ "spec/spec_helper.rb",
38
+ "spec/unit/monome_communicator_spec.rb",
39
+ "spec/unit/monome_serial_spec.rb"
40
+ ]
41
+ s.homepage = %q{http://sam.aaron.name/projects/ruby_monome_serial.html}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.5}
45
+ s.summary = %q{Ruby serial library for communicating with a monome.}
46
+ s.test_files = [
47
+ "spec/spec_helper.rb",
48
+ "spec/unit/monome_communicator_spec.rb",
49
+ "spec/unit/monome_serial_spec.rb",
50
+ "examples/toggler.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
59
+ s.add_runtime_dependency(%q<arika-ruby-termios>, [">= 0.9.6"])
60
+ else
61
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
62
+ s.add_dependency(%q<arika-ruby-termios>, [">= 0.9.6"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
66
+ s.add_dependency(%q<arika-ruby-termios>, [">= 0.9.6"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format nested
3
+ --reverse
4
+ --loadby mtime
@@ -0,0 +1,10 @@
1
+ #require tosca
2
+ require 'rubygems'
3
+ require 'monome_serial'
4
+
5
+
6
+ #require rspec
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+
10
+ MonomeSerial::SerialCommunicator.suppress_warnings = true
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+ include MonomeSerial
3
+
4
+ describe MonomeCommunicator do
5
+ it "should exist" do
6
+ MonomeCommunicator.should_not be_nil
7
+ end
8
+
9
+ it "should raise an error when not initializing with a correct protocol name" do
10
+ lambda{MonomeCommunicator.new('/blah', "phonycol" )}.should raise_error(ArgumentError)
11
+ end
12
+
13
+ describe "A default monome with an unusual tty_path" do
14
+ before(:each) do
15
+ @monome = MonomeCommunicator.new('unusual_path')
16
+ end
17
+
18
+ it "should default to the series protocol" do
19
+ @monome.protocol.should == "series"
20
+ end
21
+
22
+ it "should default the serial to Serial Unknown" do
23
+ @monome.serial.should == "Serial Unknown"
24
+ end
25
+
26
+ it "should have a dummy communicator" do
27
+ @monome.communicator.class.should == SerialCommunicator::DummyCommunicator
28
+ end
29
+ end
30
+
31
+ describe "A default monome with the 40h protocol" do
32
+ before(:each) do
33
+ @monome = MonomeCommunicator.new('/foo/tty.usbserial-m256-203', '40h')
34
+ end
35
+
36
+ it "should raise an exception when a communicating method is used" do
37
+ lambda{@monome.illuminate_lamp(0,0)}.should raise_error(NotImplementedError)
38
+ end
39
+ end
40
+
41
+ describe "A monome with a normal tty_path and a dummy communicator" do
42
+ before(:each) do
43
+ SerialCommunicator.should_receive(:get_communicator).and_return SerialCommunicator::DummyCommunicator.new
44
+ @monome = MonomeCommunicator.new('/foo/tty.usbserial-m256-203')
45
+ @comm = @monome.communicator
46
+ end
47
+
48
+ describe "sending system calls" do
49
+
50
+ it "should send the correct binary string to light all lamps" do
51
+ @comm.should_receive(:write).with(["10010001"])
52
+ @monome.illuminate_all
53
+ end
54
+
55
+ it "should send the correct binary string to turn all lamps off" do
56
+ @comm.should_receive(:write).with(["10010000"])
57
+ @monome.extinguish_all
58
+ end
59
+
60
+ it "should send the correct binary string to set the brightness to 0" do
61
+ @comm.should_receive(:write).with(["10100000"])
62
+ @monome.brightness = 0
63
+ end
64
+
65
+ it "should send the correct binary string to set the brightness to 5" do
66
+ @comm.should_receive(:write).with(["10100101"])
67
+ @monome.brightness = 5
68
+ end
69
+
70
+ it "should send the correct binary string to set the brightness to 15" do
71
+ @comm.should_receive(:write).with(["10101111"])
72
+ @monome.brightness = 15
73
+ end
74
+
75
+ it "should raise an error if a negative brightness is set" do
76
+ lambda{@monome.brightness = -1}.should raise_error(ArgumentError)
77
+ end
78
+
79
+ it "should raise an error if a brightness is set to an integer greater than 15" do
80
+ lambda{@monome.brightness = 16}.should raise_error(ArgumentError)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+ include MonomeSerial
3
+
4
+ describe SerialCommunicator do
5
+ it "should exist" do
6
+ SerialCommunicator.should_not be_nil
7
+ end
8
+
9
+ describe "A dummy serial connection" do
10
+ before(:each) do
11
+ @comm = SerialCommunicator.get_communicator("fake")
12
+ end
13
+
14
+ it "should be a dummy connection" do
15
+ @comm.should_not be_real
16
+ end
17
+
18
+ describe "read" do
19
+ it "should raise an ArgumentError if a non-array-like collection is passed as an argument" do
20
+ lambda{@comm.read(1)}.should raise_error ArgumentError
21
+ end
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monome_serial
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sam Aaron
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-07 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.4
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: arika-ruby-termios
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.6
34
+ version:
35
+ description: Communicate directly with your monome with this handy Ruby library.
36
+ email: samaaron@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - .gitignore
45
+ - README
46
+ - Rakefile
47
+ - VERSION
48
+ - assets/monome_serial_final.png
49
+ - assets/monome_serial_logo.graffle
50
+ - docs/CONTRIBUTORS
51
+ - docs/LICENSE
52
+ - docs/TODO
53
+ - examples/toggler.rb
54
+ - lib/monome_serial.rb
55
+ - lib/monome_serial/examples/toggle.rb
56
+ - lib/monome_serial/monome_communicator.rb
57
+ - lib/monome_serial/serial_communicator.rb
58
+ - lib/monome_serial/serial_communicator/binary_patterns/fourtyh.rb
59
+ - lib/monome_serial/serial_communicator/binary_patterns/series.rb
60
+ - lib/monome_serial/serial_communicator/communicator.rb
61
+ - lib/monome_serial/serial_communicator/dummy_communicator.rb
62
+ - lib/monome_serial/serial_communicator/real_communicator.rb
63
+ - monome_serial.gemspec
64
+ - spec/spec.opts
65
+ - spec/spec_helper.rb
66
+ - spec/unit/monome_communicator_spec.rb
67
+ - spec/unit/monome_serial_spec.rb
68
+ has_rdoc: true
69
+ homepage: http://sam.aaron.name/projects/ruby_monome_serial.html
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.5
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Ruby serial library for communicating with a monome.
96
+ test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/unit/monome_communicator_spec.rb
99
+ - spec/unit/monome_serial_spec.rb
100
+ - examples/toggler.rb