moodcube 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,42 @@
1
+
2
+ = MoodCube API
3
+
4
+ The MoodCube API provides an easy way to react on MoodCube events.
5
+
6
+
7
+ == Basic Usage
8
+
9
+ You can use the MoodCube API in 2 different ways:
10
+
11
+
12
+ === 1. Use the <tt>moodcube-reader</tt> script
13
+
14
+ This script listens to all MoodCube's from you local network and prints a line
15
+ containing the ip-address from the MoodCube reciever device and the
16
+ MoodCube side number (from 1..6) to STDOUT each time you are changing the
17
+ position of a MoodCube.
18
+
19
+
20
+ === 2. Use the API to react to MoodCube events
21
+
22
+ For futher examples look into the <tt>examples/</tt> directory.
23
+
24
+ require 'moodcube'
25
+
26
+ MoodCube::MoodCubeReader.new.listen do |moodcube, side|
27
+ # ..
28
+ end
29
+
30
+
31
+
32
+ == License
33
+
34
+ Artistic License
35
+
36
+
37
+ == Credits & Support
38
+
39
+ The MoodCube homepage is http://prodev.idmedia.com/moodcube
40
+
41
+ For other information feel free to contact mailto:prodev@idmedia.com
42
+
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright (c) 2006 by I-D Media AG (Berlin, Germany). All rights reserved.
3
+ # EMail: wolfger.schramm@idmedia.com
4
+
5
+ require 'optparse'
6
+
7
+ #$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'rubygems'
9
+ require 'moodcube'
10
+ include MoodCube
11
+
12
+ port = 2343
13
+ inet = ''
14
+ $verbose = false
15
+
16
+ opts = OptionParser.new
17
+ opts.on("-p", "--port PORT", Integer) { |i| port = i }
18
+ opts.on("-i", "--inet IP", String) { |s| inet = s }
19
+ opts.on("-v", "--verbose") { $verbose = true }
20
+ opts.parse(*ARGV)
21
+
22
+ puts "listening for *moodcube* data on UDP (port=#{port}, inet='#{inet}') .." if $verbose
23
+
24
+ reader = MoodCubeReader.new(port, inet)
25
+ reader.listen { |moodcube, side|
26
+ puts "#{moodcube.ip_addr} #{side}"
27
+ }
28
+
29
+ ###### CREATING TOMORROW ## CREATING TOMORROW ## CREATING TOMORROW #########
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright (c) 2006 by I-D Media AG (Berlin, Germany). All rights reserved.
3
+ # EMail: wolfger.schramm@idmedia.com
4
+
5
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'moodcube'
7
+
8
+ script = { 1 => "MoodCube_activity.hs",
9
+ 2 => "MoodCube_dreamly.hs",
10
+ 3 => "MoodCube_lazy.hs",
11
+ 4 => "MoodCube_lounge.hs",
12
+ 5 => "MoodCube_moodoff.hs",
13
+ 6 => "MoodCube_recreate.hs" }
14
+
15
+ def send_homespeak stmt
16
+ begin
17
+ puts stmt
18
+ t = TCPSocket.new('127.0.0.1', 1975)
19
+ t.puts stmt
20
+ t.close
21
+ rescue => e
22
+ p e
23
+ end
24
+ end
25
+
26
+ MoodCube::MoodCubeReader.new.listen do |moodcube, side|
27
+ send_homespeak "new message(play script='#{script[side]}') to macroplayer on mcp"
28
+ end
29
+
30
+ ###### CREATING TOMORROW ## CREATING TOMORROW ## CREATING TOMORROW #########
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright (c) 2006 by I-D Media AG (Berlin, Germany)
3
+ # EMail: wolfger.schramm@idmedia.com
4
+
5
+ require 'socket'
6
+ require 'ostruct'
7
+ require 'monitor'
8
+
9
+
10
+ module MoodCube
11
+
12
+ MOODCUBE_MID_BORDER = 0.0
13
+ MOODCUBE_TURN_BORDER = 0.1
14
+
15
+ SIDE_DETECT_INTERVAL = 0.1 # sec
16
+ SIDE_HOLDUP_TIME = 0.5 # sec
17
+
18
+
19
+ class MoodCube < Monitor # -----------------------------------------------
20
+ attr_reader :side
21
+ attr_reader :min
22
+ attr_reader :max
23
+ attr_accessor :ip_addr
24
+
25
+ def initialize side_switch_proc
26
+ super()
27
+ @pulse_time = Time.now
28
+ @side = @side_pulse = 0
29
+ @min = [MOODCUBE_MID_BORDER, MOODCUBE_MID_BORDER, MOODCUBE_MID_BORDER]
30
+ @max = @min.clone
31
+ Thread.new do
32
+ loop do
33
+ sleep(SIDE_DETECT_INTERVAL)
34
+ detect_side(side_switch_proc)
35
+ end
36
+ end
37
+ end
38
+
39
+ def detect_side side_switch
40
+ synchronize do
41
+ if Time.now - @pulse_time > SIDE_HOLDUP_TIME && @side != @side_pulse
42
+ @side = @side_pulse
43
+ side_switch.call(self, @side)
44
+ end
45
+ end
46
+ end
47
+ private :detect_side
48
+
49
+ def side_pulse side
50
+ if (1..6).include? side
51
+ synchronize do
52
+ if @side_pulse != side
53
+ @pulse_time = Time.now
54
+ @side_pulse = side
55
+ end
56
+ end
57
+ else
58
+ synchronize do
59
+ @pulse_time = Time.now
60
+ end
61
+ end
62
+ end
63
+ end # MoodCube -----------------------------------------------------------
64
+
65
+ # TODO handle different moodcubes!
66
+ class MoodCubeReader # ---------------------------------------------------
67
+
68
+ def initialize port=2343, inet=''
69
+ @server = UDPSocket.open
70
+ @server.bind(inet, port)
71
+ end
72
+
73
+ def listen &side_switch
74
+ moodcube = MoodCube.new(side_switch)
75
+ ip = nil
76
+ oldside = -1
77
+ loop do
78
+ data, sender = @server.recvfrom(6) #, Socket::SO_BROADCAST)
79
+ moodcube.ip_addr = sender[3] # XXX remove this hack if reader
80
+ # can handle multiple moodcubes!
81
+ if ip != sender[3]
82
+ ip = sender[3]
83
+ puts "\n--- [#{ip}] --- [#{sender[2]}] ---" if $verbose
84
+ end
85
+ begin
86
+ values = [OpenStruct.new, OpenStruct.new, OpenStruct.new]
87
+ values.each_with_index do |v,n|
88
+
89
+ v.raw = data[n*2]<<8|data[n*2+1]
90
+ v.norm = Float(v.raw)/512.0 - 1.0
91
+
92
+ moodcube.min[n] = v.norm if moodcube.min[n] > v.norm
93
+ moodcube.max[n] = v.norm if moodcube.max[n] < v.norm
94
+ end
95
+
96
+ if $verbose
97
+ printf "%+1.3f %+1.3f %+1.3f [%+1.3f %+1.3f] [%+1.3f %+1.3f] [%+1.3f %+1.3f] ",
98
+ values[0].norm, values[1].norm, values[2].norm,
99
+ moodcube.min[0], moodcube.max[0],
100
+ moodcube.min[1], moodcube.max[1],
101
+ moodcube.min[2], moodcube.max[2]
102
+ end
103
+
104
+ side = identify_side(values)
105
+ moodcube.side_pulse(side)
106
+
107
+ printf "%i\n", side if $verbose
108
+ rescue => e
109
+ p e if $verbose
110
+ end
111
+ end
112
+ end
113
+
114
+ def identify_side values
115
+ z = 0
116
+ values.each_with_index do |v,n|
117
+ z |= 1<<2*n if v.norm > (MOODCUBE_MID_BORDER + MOODCUBE_TURN_BORDER)
118
+ z |= 1<<2*n+1 if v.norm < (MOODCUBE_MID_BORDER - MOODCUBE_TURN_BORDER)
119
+ end
120
+ if $verbose
121
+ _z = ""
122
+ (0..5).each { |n| _z = z & 1<<n > 0 ? "X#{_z}" : "_#{_z}" }
123
+ printf "%s %2i ", _z, z
124
+ end
125
+ return nil if z == 0
126
+ (1..6).find { |n| z == z & 1<<n-1 }
127
+ end
128
+ private :identify_side
129
+
130
+ end # MoodCubeReader ---------------------------------------------------------
131
+
132
+ end
133
+
134
+ ###### CREATING TOMORROW ## CREATING TOMORROW ## CREATING TOMORROW #########
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: moodcube
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.9.1
7
+ date: 2007-04-10 00:00:00 +02:00
8
+ summary: MoodCube API
9
+ require_paths:
10
+ - lib
11
+ email: prodev@idmedia.com
12
+ homepage: http://prodev.idmedia.com/moodcube
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: moodcube
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Wolfger Schramm
31
+ files:
32
+ - lib/moodcube.rb
33
+ - bin/moodcube-reader
34
+ - examples/moodcube-2-homespeak.rb
35
+ - README
36
+ test_files: []
37
+
38
+ rdoc_options:
39
+ - --main
40
+ - README
41
+ extra_rdoc_files:
42
+ - README
43
+ executables:
44
+ - moodcube-reader
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies: []
50
+